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

Example python programs ex 1 & 2

Uploaded by

laila godson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Example python programs ex 1 & 2

Uploaded by

laila godson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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

3. Write a simple python program "iterating through a


fibonacci_series=(1,1,2,3,5,8,13,21,34,55,89) by using the for loop as the primary control
structure.

fibonacci_series = (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)


for number in fibonacci_series:
print(number)

4. Problem: Write a program to find the sum of the first N natural numbers using a loop.

 Using for loop:

N = 10 # Example: Sum of first 10 numbers


total_sum = 0
for i in range(1, N + 1): # Loops from 1 to N (inclusive)
total_sum += i
print ("Sum of first", N, "natural numbers is:", total_sum)

Output: Sum of first 10 natural numbers is: 55

 Using while loop:

N = 10 # Example: Sum of first 10 numbers


total_sum = 0
i=1
while i <= N:
total_sum += i
i += 1
print ("Sum of first", N, "natural numbers is:", total_sum)
5. Problem: Write a program to find the factorial of a given number n. The factorial of a number
is the product of all positive integers less than or equal to that number

(e.g., 5! = 5 * 4 * 3 * 2 * 1).

 Using for loop:

n = 5 # Example: Find factorial of 5


factorial = 1
for i in range(1, n + 1):
factorial *= i
print("Factorial of", n, "is:", factorial)

Output: Factorial of 5 is: 120

 Using while loop:

n = 5 # Example: Find factorial of 5


factorial = 1
i=1
while i <= n:
factorial *= i
i += 1
print("Factorial of", n, "is:", factorial)

6. Create some code to generate the following pattern using two "for loops"

66666
5555
444
33
2

for i in range(6, 0, -1):


for j in range(i):
print(i, end=' ')
print()

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.")

You might also like