Department of Electrical
Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore
Program: [Link]. Electrical engineering Semester: II
Subject: CSL-212 Object Oriented Programming Date: ……………….
Experiment 12: CONTROL STATEMENTS AMD LOOPS IN
PYTHON
OBJECTIVES:
(i) To study IF, ELIF control, and LOOPs in PYTHON
Name: Syed Aoun Muhammad Shah
SAP:58147
No. Lab Evaluation Scheme Obtained
Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total
Remarks (if any): ………………………………….
Name & Signature of faculty: …………………………………
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Theory
Python supports the usual logical conditions from mathematics:
Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
Example
a = 33
b = 200
if b > a:
print("b is greater than a")
Example
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Elif: The elif keyword is Python's way of saying "if the previous conditions were not true,
then try this condition"
Example
a = 33 Oriented Programming
Object 2nd Semester-EE RCST Lahore
b = 33
if b > a:
print("b is greater than a")
elif a == b:
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Else Keyword
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python Loops
The while Loop
count = 5
while (count > 0):
print(count)
count = count - 1
Example
x=5
while (x > 0):
print(x)
x=x-1
Object
else: Oriented Programming 2nd Semester-EE RCST Lahore
print('counter is 0')
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
The for Loop
Example
# Prints out the numbers 0,1,2,3,4
for x in range(5):
print(x) # Prints out 3,4,5
for x in range(3, 6):
print(x) # Prints out 3,5,7
for x in range(3, 8, 2):
print(x)
Lab Task
Task 1
The University of Guiness charges $3000 per semester for in-state tuition and $4500 per
semester for out-of-state tuition. In addition, room and board is $2500 per semester for in-state
students and $3500 per semester for out-of-state students. Write a program that prompts the
user for their residential status (i.e., in-state or out-of-state) and whether they require room and
board (Y or N). The program should then compute and output their bill for that semester. Use
the sample output below:
Sample Run:
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Please input "I" if you are in-state or O" if you are out-of-state:
Please input "Y" if you require room and board and "N" if you do not:N
Your total bill for this semester is $3000
Task 2
Write and run a program that gives the user only three choices: convert from Fahrenheit to
Celsius, convert from Celsius to Fahrenheit, or quit. If the third choice is selected, the
program stops. If one of the first two choices is selected, the program should prompt the user
for either a Fahrenheit or Celsius temperature, as appropriate, or then compute and display a
corresponding temperature. Use the conversion formulas
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Task 3
Take 10 integers from keyboard using loop and print their average value on the screen.
Task 4
Write a program to make the following triangle using loop.
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Conclusion:
Object Oriented Programming 2nd Semester-EE RCST Lahore