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

Week 1-4

The document contains programming exercises assigned over 4 weeks. Week 1 covers basics like calculating product/sum of inputs, summing the first 10 numbers, and printing numbers divisible by 5 from a list. It also includes checking for palindrome numbers and calculating cubes of numbers up to a given input. Week 2 focuses on reverse digit extraction, digit counting, prime number checking within a range, and finding factorials using loops. Week 3 prints patterns using nested loops and manipulates strings at even/odd indexes. Week 4 defines functions to return addition/subtraction, check if a list's first and last items match, square list items, and iterate two lists simultaneously in original and reverse order. It also counts occurrences of an item in a tuple.

Uploaded by

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

Week 1-4

The document contains programming exercises assigned over 4 weeks. Week 1 covers basics like calculating product/sum of inputs, summing the first 10 numbers, and printing numbers divisible by 5 from a list. It also includes checking for palindrome numbers and calculating cubes of numbers up to a given input. Week 2 focuses on reverse digit extraction, digit counting, prime number checking within a range, and finding factorials using loops. Week 3 prints patterns using nested loops and manipulates strings at even/odd indexes. Week 4 defines functions to return addition/subtraction, check if a list's first and last items match, square list items, and iterate two lists simultaneously in original and reverse order. It also counts occurrences of an item in a tuple.

Uploaded by

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

Week 1

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.

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))

product = num1 * num2


if product <= 5000:
print("Sum:", num1 + num2)
else:
print("Product:", product)

2. Write a program to print sum of first 10 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)

print("Numbers divisible by 5:")


for num in numbers:
if num % 5 == 0:
print(num)

4. Write a program to check if the given number is a palindrome number or not.

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

num = int(input("Enter a number: "))


if is_palindrome(num):
print("It's a palindrome number.")
else:
print("It's not a palindrome number.")

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

num = int(input("Enter a number: "))


cubes = calculate_cubes(num)
print("Cubes of numbers from 1 to", num, ":", cubes)
Week 2

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

number = int(input("Enter an integer: "))


reverse_digit_extraction(number)

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

start = int(input("Enter the starting range: "))


end = int(input("Enter the ending range: "))

print("Prime numbers between", start, "and", end, "are:")


for num in range(start, end + 1):
if is_prime(num):
print(num, end=" ")

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

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


factorial_result = factorial(number)
print("Factorial of", number, "is:", factorial_result)
5. Write a program to find the sum of digits of a supplied integer.

def sum_of_digits(num):
total = 0
while num > 0:
digit = num % 10
total += digit
num //= 10
return total

number = int(input("Enter an integer: "))


digit_sum = sum_of_digits(number)
print("Sum of digits:", digit_sum)
Week – 3

1. Write a program to print the following pattern using the for loop:

54321
4321
321
21
1

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


for j in range(i, 0, -1):
print(j, end=" ")
print()

2. Write a program to print the following start pattern using the for loop:

*
**
***
****
*****
****
***
**
*

for i in range(1, 6):


for j in range(i):
print("*", end=" ")
print()
for i in range(4, 0, -1):
for j in range(i):
print("*", end=" ")
print()

3. Write a program to print characters from a string which are present at an even index
numbers.

text = input("Enter a string: ")


for i in range(0, len(text)-1, 2):
print(text[i+1], end=" ")
4. Write a program to accept a string from the user and display characters that are present
at an even index number.

text = input("Enter a string: ")


even_characters = "".join(text[i] for i in range(0, len(text), 2))
print("Characters at even indexes:", even_characters)

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.

def remove_chars(text, n):


return text[:n]

input_string = input("Enter a string: ")


n = int(input("Enter the value of n: "))
new_string = remove_chars(input_string, n)
print("New string:", new_string)
Week – 4

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.

def cal_sum_sub(a, b):


addition = a + b
subtraction = a - b
return addition, subtraction

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']

for item1, item2 in zip(list1, reversed(list2)):


print(item1, item2)
5. Write a program to count the number of occurrences of item 50 from below tuple
tp1

tp1 = (50, 50, 10, 60, 70, 50)


count = 0
for i in tp1:
if i==50:
count=count+1

print("Number of occurrences of 50:", count)

You might also like