Example python programs ex 1 & 2
Example python programs ex 1 & 2
Write a simple python program "iterating the given string printing each letter using the for loop"
string = "hello world"
for letter in string:
print(letter)
2. Write a program to use python while loops for the fibonacci_series =(1, 1, 2, 3, 5, 8, 13, 21, 34,
55, 89)
fibonacci_series = (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
index = 0
while index < len(fibonacci_series):
number = fibonacci_series[index]
print(number)
index += 1
4. Problem: Write a program to find the sum of the first N natural numbers using a loop.
(e.g., 5! = 5 * 4 * 3 * 2 * 1).
6. Create some code to generate the following pattern using two "for loops"
66666
5555
444
33
2
7. Create some code to generate the following pattern using two "for loops"
6
55
444
3333
22222
for i in range(6, 0, -1):
for j in range(i, 6):
print(i, end=' ')
print()
8. Write a simple python program using a while loop to calculate the mean (=total/25) of a set of
numbers using a while loop
total = 0
count = 0
while count < 25:
number = float(input("Enter a number: "))
total += number
count += 1
mean = total / 25
print("The mean is:", mean)
9. Create a simple python code using for loop that outputs all the multiples of 5 between 108 and
508
for number in range(108, 509):
if number % 5 == 0:
print(number)
10. Write a simple python program using a while loop that multiples two integer number (1,9) using
repeated addition (without using the * operator)
num1 = int(input("Enter the first number (1-9): "))
num2 = int(input("Enter the second number (1-9): "))
result = 0
count = 0
while count < num2:
result += num1
count += 1
print ("The product is:", result)
11. Create python code that uses an iterative for-else loop to cycle through the fruits strings.
Given list of fruits=['Banana', 'Jackfruit','Lemon','Mango', 'Orange', 'Papaya', 'Rambutan', 'Star
Apple', and 'Watermelon']
fruits = ['Banana', 'Jackfruit', 'Lemon', 'Mango', 'Orange', 'Papaya', 'Rambutan',
'Star Apple', 'Watermelon']
for fruit in fruits:
if fruit == 'Mango':
print ("Mango found!")
break
else:
print ("Mango not found.")
12. Create a code that uses an iterative for-else loop with a pass to cycle through the fruit
strings. Given sequence strings are jiuce=["Amla juice", "Orange juice", "Pomegranate juice",
and "Pineapple juice"].
Healthiest juice is the optional declared variable to be used in for loop.
juices = ["Amla juice", "Orange juice", "Pomegranate juice", "Pineapple juice"]
healthiest_juice = "Amla juice"
1for juice in juices:
if juice == healthiest_juice:
print ("The healthiest juice is:", juice)
break
else:
pass
13. create a script in python without using function that determines if the given number is a
palindrome.
Given number i)7890987, ii)324576
numbers = [7890987, 324576]
for number in numbers:
number_str = str(number)
reversed_str = number_str[::-1]
if number_str == reversed_str:
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")