Week 1-4
Week 1-4
1. 1. Write a program to find product of two user supplied integers and if the product is
equal to or lower than 5000 then return sum of the two numbers.
total = 0
for i in range(1, 11):
total += i
print("Sum of first 10 numbers:", total)
3. Write a program to Iterate the supplied list of 10 numbers by the user and print only those
numbers which are divisible by 5.
numbers = []
for _ in range(20):
num = int(input("Enter a number: "))
numbers.append(num)
def is_palindrome(num):
original_num = num
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
return original_num == reversed_num
5. Write a program to calculate the cube of all numbers from 1 to a given number.
def calculate_cubes(n):
cubes = [i ** 3 for i in range(1, n + 1)]
return cubes
1. Write a Program to extract each digit from an integer in the reverse order.
def reverse_digit_extraction(num):
while num > 0:
digit = num % 10
print(digit, end=" ")
num //= 10
2. Write a program to count the total number of digits in a number using a while loop.
def count_digits(num):
count = 0
while num > 0:
num //= 10
count += 1
return count
number = int(input("Enter an integer: "))
digit_count = count_digits(number)
print("Number of digits:", digit_count)
3. Write a program to display all prime numbers within a range.
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
4. Write a program to use the loop to find the factorial of a given number
def factorial(num):
result = 1
for i in range(1, num + 1):
result *= i
return result
def sum_of_digits(num):
total = 0
while num > 0:
digit = num % 10
total += digit
num //= 10
return total
1. Write a program to print the following pattern using the for loop:
54321
4321
321
21
1
2. Write a program to print the following start pattern using the for loop:
*
**
***
****
*****
****
***
**
*
3. Write a program to print characters from a string which are present at an even index
numbers.
5. Write a program to remove characters from a string starting from n to last and return a
new string. Example: remove_chars("aligarh", 3) so output must be al.
1. Write a program to create function cal_sum_sub() such that it can accept two
variables and calculate addition and subtraction. Also, it must return both addition
and subtraction in a single return call.
result = cal_sum_sub(10, 5)
print("Addition:", result[0])
print("Subtraction:", result[1])
2. Write a function to return True if the first and last number of a given list is same. If
numbers are different then return False
def is_first_last_same(numbers):
if len(numbers) < 2:
return False
return numbers[0] == numbers[-1]
my_list = [1, 2, 3, 4, 1]
result = is_first_last_same(my_list)
print(result)
3. Given a list of numbers. Write a program to turn every item of a list into its square.
def square_list(numbers):
squared_list = [x ** 2 for x in numbers]
return squared_list
my_list = [1, 2, 3, 4, 5]
result = square_list(my_list)
print(result)
4. Given a two Python list. Write a program to iterate both lists simultaneously and
display items from list 1 in original order and items from list 2 in reverse order.
list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']