4. Flow Control of the Program
Selection (If/Else)
Repetition (Loops)
5. Conditional Execution
In order to write useful programs, we almost always need to check
some conditions and change the program accordingly. Conditional
Statements gives us this ability. The simplest form is if statement.
General Form:
if [expression1]:
body1
elif [expression2]:
body2
else:
bodyN
6. Python Comparison Operators
Operator Description
== Checks if the value of two operands are equal or not, if yes
then condition becomes true.
!= Checks if the value of two operands are equal or not, if
values are not equal then condition becomes true.
< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
> Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal
to the value of right operand, if yes then condition becomes
true.
<= Checks if the value of left operand is less than or equal to
the value of right operand, if yes then condition becomes
true.
7. Python Logical Operators
Operator Description
and Called Logical AND operator. If both the operands are
true then then condition becomes true.
or Called Logical OR Operator. If any of the two operands
are non zero then then condition becomes true.
not Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true then Logical NOT
operator will make false.
9. Python Loops
Introduction:
There may be a situation when you need to execute a block of code
several number of times. In general, statements are executed sequentially:
The first statement in a function is executed first, followed by the second,
and so on.
Programming languages provide various control structures that allow for
more complicated execution paths.
A loop statement allows us to execute a statement or group of statements
multiple times.
11. Python Loop Types
while Loop
•Repeats a statement or group of statements
while a given condition is true. It tests the
condition before executing the loop body.
for Loop
•Executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
12. Loop Control Statements
break statement
Terminates the loop statement and transfers
execution to the statement immediately following
the loop.
continue statement
Causes the loop to skip the remainder of its body
and immediately retest its condition prior to
reiterating.
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.
13. for Loop
Syntax:
for iterating_var in sequence:
statements(s)
Example:
for letter in “FirstLoop”:
print letter
14. while Loop
Syntax:
while expression:
statement(s)
Example:
while (True):
print “Anything”
16. Class Exercise 1
Write a function that takes an string and prints it ten times.
17. Class Exercise 2
Write a function that takes an integer and prints the multiplication
table of the given integer.
18. Class Exercise 3
Write a program to prompt for a score between 0.0 and 1.0. If the
score is out of range, print an error. If the score is between 0.0 and
1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message
and exit. For the test, enter a score of 0.85.
19. Class Exercise 4
Write a program to prompt the user for hours and rate per hour using
raw_input to compute gross pay. Pay the hourly rate for the hours
up to 40 and 1.5 times the hourly rate for all hours worked above 40
hours. Use 45 hours and a rate of 10.50 per hour to test the program
(the pay should be 498.75). You should use raw_input to read a
string and float() to convert the string to a number. Do not worry
about error checking the user input - assume the user types numbers
properly.