CA116 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
Selection and Iteration in Python
Deepen understanding of selection and iteration in Python.
Practical coding examples and exercises.
Solving real-world problems.
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.
# Ask the user for their age
Ticket Price age = int(input("Enter your age: "))
# Determine the ticket price based on age
Child €5 if age < 13:
print("Child ticket price: €5")
Teen €7 elif age < 18:
print("Teen ticket price: €7")
Adult €10 else:
print("Adult ticket price: €10")
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.
while condition: for index in range(n):
statement statement
count = 0 for i in range(100):
while count < 100: print("Hello World!")
print("Hello World!")
count += 1
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.
# List containing all days of the week
days_of_the_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
# Loop through the list and print each day
for day in days_of_the_week:
print(day)
8
Today's Key Takeaways and Preview of Next Lecture
Summarize Today's Lecture:
Sequence in Python Programming
Selection (Decision-Making)
Iteration (Loops)
Next Lecture:
"Defining and Calling Functions in Python“
Practice writing own selection structures and loops
Learning Resources:
DCU Loop (Moodle)
https://www.101computing.net/sequencing-selection-iteration/
9
Let's Review with an un-graded Quiz!
• Purpose of the Quiz:
This quiz will help us review today's key concepts
0460 5620
and ensure everyone understands the material.
• Instructions for Joining:
Step-by-step instructions on how to join the Kahoot
quiz:
1. Step 1: "Open www.kahoot.it on your
device (smartphone, tablet, or laptop)."
2. Step 2: "Enter the Game PIN shown on the
screen."
3. Step 3: "Enter your nickname or real name
when prompted, so we can see who’s who
in the game results!"
10