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

Python - Lists, For Loop Continuation

Uploaded by

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

Python - Lists, For Loop Continuation

Uploaded by

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

Python

For Loops, Lists continuation

P20) Write a Python function that takes a list of numbers as input and returns the sum of all the
numbers in the list.
numbers = [1, 2, 3, 4, 5]
total_sum_loop = 0
for num in numbers:
total_sum_loop += num
print("Sum using loop:", total_sum_loop)

no_of_numbers = int(input("Enter the total numbers to be added: "))

numbers = []
for i in range(no_of_numbers):
num = int(input("Enter number: "))
numbers.append(num)

print("Sum is: ", sum(numbers))

P21) Given a list of numbers, write a Python function to find and print all the even numbers in the list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
print("Even numbers:", even_numbers)

P22) Write a Python function that takes a list of numbers and returns a new list with only the positive
numbers.
numbers = [-2, -1, 0, 1, 2, 3, 4, 5]
positive_numbers = []

for num in numbers:


if num > 0:
positive_numbers.append(num)

print("Positive numbers:", positive_numbers)

P23) Given a list of strings, write a Python function to find and print all the strings that have a length
of 4 characters.
strings = ["apple", "banana", "orange", "grape", "kiwi", "pear"]
for string in strings:
if len(string) == 4:
print(string)
P24) Write a Python function that takes a list of integers and returns a new list with the square of each
number.
numbers = [1, 2, 3, 4, 5]
squared_numbers = []

for num in numbers:


squared_numbers.append(num ** 2)

print("Squared numbers:", squared_numbers)

P25) Take input a string, and print the number of consonants and vowels.
user_input = input("Enter a string: ")
vowels = "aeiouAEIOU"
consonants_count = 0
vowels_count = 0

for char in user_input:


if char.isalpha():
if char in vowels:
vowels_count += 1
else:
consonants_count += 1

print("Number of consonants:", consonants_count)


print("Number of vowels:", vowels_count)

P26) Write a Python program to calculate the factorial of a given number using a for loop.
num = int(input("Enter a number: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = 1
for i in range(1, num + 1):
result *= i
print("Factorial of", num, "is", result)

P27) Write a Python program to print all the prime numbers between 1 and N using a for loop.
N = int(input("Enter the value of N: "))

print("Prime numbers between 1 and", N, ":")


for num in range(2, N + 1):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")
P28) Write a Python program to check if a given number is a palindrome using a for loop.
string = input("Enter a number: ")
length = len(string)
is_palindrome = True

for i in range(int(length / 2)):


if string[i] != string[-(i+1)]:
is_palindrome = False
break

if is_palindrome:
print("Palindrome")
else:
print("Not a Palindrome")

P29) Write a Python program to print all the factors of a given number using a for loop.
num = int(input("Enter a number: "))
if num < 1:
print("Factors are only defined for positive integers.")
else:
print("Factors of", num, ":")
for i in range(1, num + 1):
if num % i == 0:
print(i, end=", ")

P30) Write a Python program to find the smallest and largest number from a list of numbers.
numbers = [12, 45, 23, 67, 1, 90, 34, 78]

if not numbers:
print("The list is empty.")
else:
smallest = largest = numbers[0]

for num in numbers:


if num < smallest:
smallest = num
if num > largest:
largest = num

print("Smallest number:", smallest)


print("Largest number:", largest)

P31) Remove Duplicates from a list.


original_list = [3, 5, 2, 8, 5, 2, 9, 7]
unique_list = []

for num in original_list:


if num not in unique_list:
unique_list.append(num)

print("Unique elements:", unique_list)


P32) Print a list in reversed order.
original_list = [7, 3, 9, 1, 5]
reversed_list = []

for i in range(len(original_list)-1, -1, -1):


reversed_list.append(original_list[i])

print("Reversed list:", reversed_list)

P33) Check for Armstrong numbers.


number = 153
original_number = number
num_digits = len(str(number))
sum_of_cubes = 0

while number > 0:


digit = number % 10
sum_of_cubes += digit ** num_digits
number //= 10

if sum_of_cubes == original_number:
print(f"{original_number} is an Armstrong number.")
else:
print(f"{original_number} is not an Armstrong number.")

P34) Check for Perfect Number.


number = int(input("Enter a number: "))
divisors_sum = 0

for i in range(1, number):


if number % i == 0:
divisors_sum += i

if divisors_sum == number:
print(number," is a perfect number.")
else:
print(number," is not a perfect number.")

P35) Remove vowels from a string.


text = "hello world"
vowels = "aeiou"
text_without_vowels = ""

for char in text:


if char.lower() not in vowels:
text_without_vowels += char

print("Text without vowels:", text_without_vowels)

You might also like