Python session3


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Python session3

  1. 1. AGENDA Decision Making Loops
  2. 2. Python Decision Making  Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
  3. 3. Types of decision making statements  IF statements  IF...ELSE statements  Nested IF statements
  4. 4. IF statements Flow Diagram: Test Exp FALSE TRUE Body of if EXIT
  5. 5. IF...ELSE statements Flow Diagram: Test Exp FALSE If Block TRUE Body of Else  EXIT The else statement is an optional statement and there could be at most only one else statement following if .
  6. 6. LOOPS   A loop statement allows us to execute a statement or group of statements multiple times Python programming language provides following types of loops to handle looping requirements  While loop  For loop
  7. 7. While Loop Example: count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
  8. 8. For Loop If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted. Example: for letter in 'Python': print 'Current Letter :', letter String
  9. 9. Loop Control Statements: Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.  break statement  continue statement  pass statement
  10. 10. Break Statement: Example for letter in 'Python': if letter == 'h': break Break Statement print 'Current Letter :', letter
  11. 11. continue statement The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. for letter in 'Python': if letter == 'h': continue continue statement print 'Current Letter :', letter
  12. 12. pass statement The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. for letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letter
  13. 13. Reference 