Python Course - IV Conditional Statements
Python Course - IV Conditional Statements
Hand Notes 4
Hand Notes for:
Basic Programming (Flowcharts, Conditional statements IF ELIF ELSE)
Basic Programming (Conditional statements IF ELIF ELSE)
1
Python Programming - IV
Session 6: Flowcharts
FLOWCHARTS
A flowchart is a diagrammatic representation of an algorithm. A flowchart can be helpful for both writing programs
and explaining the program to others.
2
Python Programming - I
3
Python Programming - I
4
Python Programming - I
5
Python Programming - I
Evaluation Order
The following table gives the precedence table for Python, from the lowest precedence (least binding) to the highest
precedence (most binding).
CONDITIONAL STATEMENTS
The if Statement
The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-
block), else we process another block of statements (called the else-block). The else clause is optional.
6
Python Programming - I
Output:
We supply a string to the built-in raw_input function which prints it to the screen and waits for input from the user.
Once we enter something and press enter key, the input() function returns what we entered, as a string. We then
convert this string to an integer using int and then store it in the variable guess . Actually, the int is a class but all you
need to know right now is that you can use it to convert a string to an integer (assuming the string contains a valid
integer in the text).
Next, we compare the guess of the user with the number we have chosen. If they are equal, we print a success
message. Notice that we use indentation levels to tell Python which statements belong to which block. This is why
indentation is so important in Python. Notice how the if statement contains a colon at the end - we are indicating to
Python that a block of statements follows.
There is no switch statement in Python. You can use an if..elif..else statement to do the same thing.
Eng = 0
Maths = 60
Hin = 100
Tel = 50
Sci = 56
SST =50
Total= Eng +Maths+ Hin+Tel+Sci+SST
Avg = Total / 6
print("Average = ",Avg)
if Avg>=70:
print("Congratulations you have got Grade A!")
#
elif Avg>=40:
print("Congratulations you have got Grade B!")
else:
7
Python Programming - I
Eng = 80
Maths = 60
Hin = 100
Tel = 80
Sci = 50
SST = 50
Total = Eng + Maths + Hin + Tel + Sci + SST
Avg = Total / 6
print("Average = ", Avg)
if Avg >=70:
print("Grade A")
if Avg >=40 and Avg <70:
print("Grade B")
if Avg <40:
print("Failed!")
print("Thank you!")
#Once I had been to the post-office to buy stamps of five rupees, two rupees and one
rupee.
# I paid the clerk Rs. 20, and since he did not have change, he gave me three more stamps
of one rupee.
# If the number of stamps of each type that I had ordered initially was more than one,
# what was the total number of stamps that I bought.
#av5 = 2
av5+= bam//v5 #av5=av5 +2
bam %= v5
8
Python Programming - I
else:
print("Your age is not yet 18, hence you can not vote in India")
# WAP to input 3 numbers and display in ascending order DO NOT USE AND, OR, NOT
#Use Nested IF
# 5,9,3 - Input
# 3 <= 5 <=9 - Output
Assignments
1. #WAP enter 3 sides, check if triangle is possible or not
# and also to check if the triangle is Isoceles or Equilateral or Scalene or
Right angled
2. #Modify this program to make it look like your school's rule
# A >=80 B>=70 C>=60, D >=40 <40: Failed
3. Write if statements to do the following:
– If character variable taxCode is ’T’, increase price by adding the taxRate percentage of price to it.
– If integer variable opCode has the value 1, read in double values for X and Y and calculate and print their sum.
– If integer variable currentNumber is odd, change its value so that it is now 3 times currentNumber plus 1, otherwise
change its value so that it is now half of currentNumber (rounded down when currentNumber is odd).
– Assign true to the boolean variable leapYear if the integer variable year is a leap year. (A leap year is a multiple of 4,
and if it is a multiple of 100, it must also be a multiple of 400.)
– Assign a value to double variable cost depending on the value of integer variable distance as follows:
Distance - Cost
----------------------------------- ----------
0 through 100 5.00
More than 100 but not more than 500 8.00
More than 500 but less than 1,000 10.00
1,000 or more 12.00
- - - End of Handout Notes 4 - - -