0% found this document useful (0 votes)
3 views

Loops

The document explains the use of if, else, and elif statements in Python for decision-making in programs, providing examples for various scenarios. It also introduces for loops, detailing how they iterate over collections and includes multiple examples of their usage. Additionally, it presents a series of programming questions to practice these concepts.

Uploaded by

mrimpatient04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Loops

The document explains the use of if, else, and elif statements in Python for decision-making in programs, providing examples for various scenarios. It also introduces for loops, detailing how they iterate over collections and includes multiple examples of their usage. Additionally, it presents a series of programming questions to practice these concepts.

Uploaded by

mrimpatient04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

if, else and else if

In Python, if, else, and elif (else if) are used to control how a program makes
decisions.
•if: This checks if a condition is true. If it is, the code inside the if block runs.
•else: This runs when the if condition is false.
•elif: Short for "else if," this checks another condition if the first if is false.

• In this example:
age = 18
• If age is less than 18, it prints "You
are a minor." if age < 18:
print("You are a minor.")
• If age is exactly 18, it prints "You just elif age == 18:
print("You just turned 18!")
turned 18!" else:
• Otherwise, it prints "You are an print("You are an adult.")

adult."
Some Examples:
1. Checking if a number is 2. Checking if a person can vote:
positive or negative:

number = 5 age = 17

if age >= 18: print("You are old enough to


if number > 0:
print("The number is positive.") vote.")

else: else: print("You are not old enough to vote.")


print("The number is negative.")
3. Even or odd number check: 4. Checking if a person is a teenager:

num = 4 age = 15
if age >= 13 and age <= 19:
if num % 2 == 0: print("You are a teenager.")
else: print("You are not a teenager.")
print("The number is even.")

else: print("The number is odd.")


5. Temperature check: 6. Grading system:

marks = 85
temperature = 30
if marks >= 90:
if temperature > 30: print("Grade: A")
print("It's hot!") elif marks >= 80:
print("Grade: B")
elif temperature < 15:
elif marks >= 70:
print("It's cold!") print("Grade: C")
else: else:
print("The weather is nice.") print("Grade: D")
7. Checking if a person is tall 8. Checking if a number is divisible by
enough for a ride: both 3 and 5:

number = 15
height = 150
if number % 3 == 0 and number % 5 ==
if height >= 120: 0:
print("You can go on the ride!") print("The number is divisible by both 3
and 5.")
else: else:
print("You are not tall enough for print("The number is not divisible by
the ride.") both.")
9. Checking time of the day:
10. Simple password check:
time = 10
password = "python123"
if time < 12:
print("Good morning!") if password == "python123":
elif time < 18: print("Access granted.")
print("Good afternoon!") else:
else: print("Access denied.")
print("Good evening!")
For loop:
• A for loop in Python is used to repeat a block of code multiple times.
It allows you to "loop" over a collection of items, such as a list, range
of numbers, or even a string. For each item in the collection, the code
inside the loop will run.
How It Works:
• A for loop takes each item from a collection (like a list or a range of
numbers) and performs the same action with each item.
• The loop runs until all the items in the collection have been processed.
Some Example:

Example 1: Looping through a list Example 2: Looping through a range


of numbers of numbers

for i in range(5):
numbers = [1, 2, 3, 4, 5]
print(i)

for num in numbers:


print(num)
• Range(5)gives the numbers from 0 to 4
This loop will print each number in (it stops before 5).
the list one by one. • The loop will print:0,1,2,3,4
Example 3: Looping through a Example 4: Looping through a list
string of items

word = "hello" fruits = ["apple", "banana", "cherry"]

for letter in word: for fruit in fruits:


print(letter) print(fruit)

• This loop will print each letter in • The loop prints each fruit from the
the word hello one by one. list: “apple”, “banana”, “cherry”
Some questions:
1. Number Comparison: Write a program that takes two numbers as input and prints which one
is larger or if they are equal.
2. Leap Year Checker: Write a program to check whether a given year is a leap year or not. A
leap year is divisible by 4 but not by 100, except if it is divisible by 400.
3. Discount Calculator: Write a program that calculates a discount based on a total purchase
amount. If the amount is over $100, apply a 10% discount, otherwise apply a 5% discount.
4. Temperature Advice: Write a program that gives advice based on temperature input:
• Below 0°C: "It's freezing!"
• 0°C to 20°C: "It's cold."
• 21°C to 30°C: "It's warm."
• Above 30°C: "It's hot!“

5. Vowel or Consonant: Write a program that checks whether a given letter is a vowel (a, e, i, o,
u) or a consonant.
6. Sum of Numbers: Write a program that calculates the sum of all numbers from
1 to 50 using a for loop
7. Multiplication Table: Write a program that prints the multiplication table for a
given number (e.g., 5) up to 10.
8. Counting Even Numbers: Write a program that counts how many even
numbers there are between 1 and 100 using a for loop.
9. Factorial Calculation: Write a program that calculates the factorial of a number
using a for loop. The factorial of a number n is the product of all positive integers
from 1 to n.
10. List Reversal: Write a program that reverses a given list using a for loop.

You might also like