1. Write a program to check if a number is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
2. Write a program to find the largest of three numbers.
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))
if a >= b and a >= c:
print(a)
elif b >= a and b >= c:
print(b)
else:
print(c)
3. Write a program to check whether a given year is a leap year or not.
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")
4. Write a program to check if a number is positive, negative, or zero. Loops.
num = float(input("Enter number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
5. Write a program to print numbers from 1 to 10 using a for loop.
for i in range(1, 11):
print(i)
6. Write a program to print the multiplication table of a given number.
n = int(input("Enter number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
7. Write a program to find the sum of all numbers from 1 to n.
n = int(input("Enter n: "))
total = 0
for i in range(1, n + 1):
total += i
print("Sum =", total)
8. Write a program to find the factorial of a number.
n = int(input("Enter number: "))
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial =", fact)
9. Write a program to print the Fibonacci sequence up to n terms. Lists & Strings.
n = int(input("Enter terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
10. Write a program to find the largest element in a list.
lst = [3, 7, 2, 9, 5]
max_num = lst[0]
for i in lst:
if i > max_num:
max_num = i
print("Largest:", max_num)
11. Write a program to count the number of even and odd numbers in a list.
lst = [1, 2, 3, 4, 5, 6]
even = 0
odd = 0
for i in lst:
if i % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even)
print("Odd:", odd)
12. Write a program to reverse a string.
s = input("Enter string: ")
rev = ""
for ch in s:
rev = ch + rev
print("Reversed:", rev)
13. Write a program to check if a string is a palindrome.
s = input("Enter string: ")
rev = ""
for ch in s:
rev = ch + rev
if s == rev:
print("Palindrome")
else:
print("Not Palindrome")
14. Write a program to find the sum of all elements in a list. Functions
lst = [2, 4, 6, 8]
total = 0
for i in lst:
total += i
print("Sum:", total)
15. Write a function to find the square of a number.
def square(n):
return n * n
print(square(5))
16. Write a function that returns the maximum of two numbers.
def maximum(a, b):
if a > b:
return a
else:
return b
print(maximum(10, 20))
17. Write a function to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(7))
18. Write a function to count vowels in a given string.
def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for ch in s:
if ch in vowels:
count += 1
return count
print(count_vowels("Hello World"))
19. Write a function to calculate the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
20. Write a program to create a dictionary of 3 items and print its keys and values.
my_dict = {'name': 'Alice', 'age': 25, 'city': 'Paris'}
for key in my_dict:
print(key, ":", my_dict[key])