Student Activity
Student Activity Guide:
1) Conditional Statements (if, elif, else)
Create a program that takes user input and determines if a number is positive, negative, or zero.
Example Code:
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")2) Looping Constructs (for, while)
Write a
forloop that prints numbers from 1 to 10.Implement a
whileloop that asks for user input until they enter "exit".
Example Code:
# For loop example
for i in range(1, 11):
print(i)
# While loop example
while True:
user_input = input("Type 'exit' to stop: ")
if user_input.lower() == "exit":
break3) Iterators and range Function
Use
range()to print even numbers from 2 to 20.Iterate over a list using an iterator.
Example Code:
# Using range() to print even numbers
for i in range(2, 21, 2):
print(i)
# Using an iterator
numbers = [10, 20, 30, 40]
num_iterator = iter(numbers)
print(next(num_iterator)) # Output: 10
print(next(num_iterator)) # Output: 204) Nested Loops and Loop Control Statements (break, continue, pass)
Use a nested loop to print a pattern.
Demonstrate
break,continue, andpass.
Example Code:
# Nested loop pattern
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()
# Break, continue, and pass examples
for num in range(1, 10):
if num == 5:
break # Stops the loop at 5
elif num == 3:
continue # Skips 3
elif num == 7:
pass # Placeholder, does nothing
print(num)Student Exercises:
Write a program that asks for a number and prints whether it is even or odd.
Create a countdown timer using a while loop.
Implement a function that prints numbers from 1 to 50 but skips numbers divisible by 5 using
continue.Write a nested loop to generate a multiplication table from 1 to 10.
Last updated