Computer Programming I - Lecture 2 Python Fundamentals
Computer Programming I - Lecture 2 Python Fundamentals
Usman Ali
Lecture Overview
Recap of Lecture 1
Learning activities and outcomes
Workload and assessment breakdown
Programming real world application
Programming languages and python
Writing, running and debugging python programs.
Basic data types and operations
Python arrays and tuples
2
Sequence, Selection and Iteration- The Building Blocks of
Programming Languages
Sequence Selection Iteration
A=10
B=0
print(A,B)
3
Selection in Python
Definition: Selection structures allow a program to make decisions, branching the flow of
execution depending on conditions.
Purpose: Used to execute different parts of code based on whether certain conditions are
true or false.
if condition1:
# code to execute if condition1 is True
else:
# code to execute if above conditions is False
T = 25
T = 25 if T > 30:
if T > 20: print("It's a hot day.")
print("It's a warm day.") elif T > 20:
else: print("It's a warm day.")
print("It's a cool day.") else:
print("It's a cool day.") 4
Real-World Example: Museums Ticket Pricing Based on Age
Scenario: A Museum offers different ticket prices based on the customer's age. This example
can help students understand how programs can make decisions using if-else conditions.
5
Iteration in Python
Definition: Iteration refers to the technique of executing a sequence of statements
repeatedly either a set number of times or until a condition is met."
Purpose: Used to perform repetitive tasks efficiently.
count = 0
while count < 100:
print("Hello World!")
print("Hello World!")
print("Hello World!") print("Hello World!")
count += 1
print("Hello World!") # Repeated 98 more times...
print("Hello World!") print("Hello World!")
print("Hello World!")
for i in range(100):
print("Hello World!")
6
Iteration in Python: While vs Loop
while– show you how to execute a code block as long as a condition is True.
for loop – show you how to execute a code block for a fixed number of times by using the for
loop with range() function.
7
Real-World Example: Displaying Days of the Week
Scenario: Suppose you have an application that needs to display all days of the week one by
one. This is a common requirement for applications involving calendars, scheduling, or even
basic planning tools.
8
Today's Key Takeaways and Preview of Next Lecture
Next Lecture:
"Defining and Calling Functions in Python“
9
Let's Review with an un-graded Quiz!
10