Welcome to our lesson on while loops in Python! In programming, loops are essential for automating repetitive tasks.
Today, we'll focus on while loops, one of the two main loop constructs in Python.
While loops are used when the number of iterations is not known beforehand or depends on a condition.
Let's dive into the basic syntax:
while condition:
# code to be executed
# (indentation is crucial)
Remember, indentation in Python determines the scope of code blocks. The condition is evaluated at the beginning of each iteration. If it's True, the indented code block is executed.
number = int(input("Enter a number: "))
count = 1
while count <= number:
print(count)
count += 1
We initialize a count variable and iterate until it reaches the user-specified number. Pay attention to how the count variable is incremented within the loop.
With the else statement, we can run a block of code once when the condition no longer is true:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Control statements like break and continue offer flexibility within loops. break allows exiting prematurely, while continue skips the current iteration.
# Example using break
while True:
# Some condition
if condition_met:
break
# Example using continue
while condition:
# Some condition
if condition_met:
continue
# Code here is skipped if condition_met
count = 1
while count <= 5:
print(count)
count += 1
count = 5
while count >= 1:
print(count)
count -= 1
# Calculating the sum of numbers from 1 to 10
sum = 0
num = 1
while num <= 10:
sum += num
num += 1
print("The sum is:", sum)
Create a program that calculates the factorial of a given number using a while loop.
# Input the number
num = int(input("Enter a number: "))
factorial = 1
i = 1
while i <= num:
factorial *= i
i += 1
print("Factorial of", num, "is", factorial)
Create a program that calculates the sum of digits of a given number. e.g 123 = 1+2+3= 6
num = int(input("Enter a number: "))
sum_of_digits = 0
while num > 0:
digit = num % 10
sum_of_digits += digit
num //= 10
print("Sum of digits:", sum_of_digits)
# print below pattern
# *
# **
# ***
rows = int(input("Enter the number of rows: "))
i = 1
while i <= rows:
print("*" * i)
i += 1
Create a program that calculates the sum of even numbers from 1 to a given number.
num = int(input("Enter a number: "))
sum_even = 0
i = 2
while i <= num:
sum_even += i
i += 2
print("Sum of even numbers from 1 to", num, "is", sum_even)
Create a program that reverses a given number.
# input 1234 output - 4321
num = int(input("Enter a number: "))
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed number:", reversed_num)
# Fibonacci series 1, 1, 2, 3, 5, 8 upto n number
n = int(input("Enter the number of terms: "))
a, b = 0, 1
count = 0
print("Fibonacci series:")
while count < n:
print(a, end=" ")
nth = a + b
a = b
b = nth
count += 1
# Palindrome checker
word = input("Enter a word: ")
word = word.lower()
start = 0
end = len(word) - 1
is_palindrome = True
while start < end:
if word[start] != word[end]:
is_palindrome = False
break
start += 1
end -= 1
if is_palindrome == True:
print("It's a palindrome!")
else:
print("It's not a palindrome.")
# Printing a pattern using while loop
# 1
# 12
# 123
i = 1
while i <= 4:
j = 1
while j <= i:
print(j, end="")
j += 1
print()
i += 1