PythonProgrammingTutorial Day09

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Python Programming Tutorial

Day 09
Agenda
1) Nested if statement
2) While loop in Python
3) For Loop
Nested if statement:
A nested if statement is a conditional statement (if statement) that is placed
inside another if statement.
# Input three numbers from the user
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
# Check which number is the biggest using nested if statements
if num1 >= num2:
if num1 >= num3:
biggest = num1
else:
biggest = num3
else:
if num2 >= num3:
biggest = num2
else:
biggest = num3

# Print the biggest number


print("The biggest number is:", biggest)

Looping Statements:
looping statements are used to execute a block of code repeatedly. There are two
main types of looping statements: for loops and while loops.
While Loop:
A while loop is used to repeatedly execute a block of code as long as a condition
is True. It is used when we don’t know number of iterations in advance

29
# Example of a while loop
i=1
while i <=10:
print(f"{i}",end=" ")
i =i+1
Output:
1 2 3 4 5 6 7 8 9 10
Example:1
# Calculate the sum of natural numbers up to a given number using a while loop
num = int(input("Enter a positive integer: "))
sum = 0
i=1
while i <= num:
sum =sum+i
i=i+1
print("The sum of natural numbers up to", num, "is:", sum)

Output:
Enter a positive integer: 10
The sum of natural numbers up to 10 is: 55
Example:2
# Take input from the user
num = int(input("Enter a number: "))
# Initialize sum variable to store the sum of digits
sum_of_digits = 0
# Loop through each digit of the number
while num > 0:
# Extract the rightmost digit
digit = num % 10
# Add the digit to the sum
sum_of_digits =sum_of_digits+ digit
# Remove the rightmost digit from the number
num = num // 10
# Print the sum of digits
print("Sum of digits:", sum_of_digits)

Output:
Enter a number: 456
Sum of digits: 15

30
For Loop:
A for loop is used to iterate over a sequence (such as a list, tuple, string, or
range) and execute a block of code for each item in the sequence.
For loop with range:
In Python, `range()` is a built-in function that generates a sequence of numbers.
It's commonly used in conjunction with loops, particularly `for` loops, to iterate
over a sequence of numbers a specified number of times.
The `range()` function can take one, two, or three arguments:
1. If only one argument is provided, `range(stop)`, it generates a sequence of
numbers from 0 up to (but not including) the specified stop value.
2. If two arguments are provided, `range(start, stop)`, it generates a sequence of
numbers starting from the `start` value up to (but not including) the `stop` value.
3. If three arguments are provided, `range(start, stop, step)`, it generates a
sequence of numbers starting from the `start` value, up to (but not including) the
`stop` value, incrementing each time by the `step` value.
Here are a few examples to illustrate the usage of the `range()` function:
# Example 1: range(stop)
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
# Example 2: range(start, stop)
for i in range(2, 6):
print(i) # Output: 2, 3, 4, 5
# Example 3: range(start, stop, step)
for i in range(1, 10, 2):
print(i) # Output: 1, 3, 5, 7, 9
For loop with list
# Example of a for loop iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

31

You might also like