0% found this document useful (0 votes)
16 views2 pages

Python Programs

Uploaded by

238a1a4460
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Python Programs

Uploaded by

238a1a4460
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1)Check given number is even or not

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


if num % 2 == 0:
print("Even")
else:
print("Odd")

2)Leap year checking


​ year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a Leap Year")
else:
print(f"{year} is not a Leap Year")

3)Calculate sum of numbers from 1 to N


N = int(input("Enter a number: "))
total = 0
for i in range(1, N+1):
total += i
print(f"Sum of numbers from 1 to {N}: {total}")

4)Calculate factorial of a number using while loop.


num = int(input("Enter a number: "))
factorial = 1
i=1
while i <= num:
factorial *= i
i += 1
print(f"Factorial of {num} is {factorial}")

5)Sum of digits in the given list


​ nums = [1, 2, 3, 4, 5]
total = sum(nums)
print("Sum of elements:", total)

6)Find max and min elements from the given list


nums = [10, 20, 30, 40, 50]
print("Maximum:", max(nums))
print("Minimum:", min(nums))
7)Find length of the tuple
my_tuple = (1, 2, 3, 4, 5)
tuple_length = len(my_tuple)
print("Length of the tuple:", tuple_length)

8)Convert tuple into list


tuple1 = (1, 2, 3, 4)
list_from_tuple = list(tuple1)
print("Converted list:", list_from_tuple)

9)Check given number is prime or not


​ num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num / 2) + 1):
if num % i == 0:
print(f"{num} is not a prime number")
break
else:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")

10) Print prime numbers from 1 to 20 using for loop.


for num in range(2, 21):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)

You might also like