Basic Python Programs
1. Print 'Hello, World!'
print("Hello, World!")
2. Add two numbers
a = 5 b = 3 print("Sum:", a + b)
3. Check if a number is even or odd
num = int(input("Enter a number: ")) if num % 2 == 0: print("Even") else:
print("Odd")
4. Find the maximum of two numbers
a = 10 b = 20 print("Maximum:", max(a, b))
5. Calculate the area of a rectangle
length = 5 width = 3 area = length * width print("Area:", area)
6. Concatenate two strings
str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result)
7. Find the length of a string
string = "Python" print("Length:", len(string))
8. Check if a string is palindrome or not
s = input("Enter a string: ") if s == s[::-1]: print("Palindrome") else: print("Not
Palindrome")
9. Find the sum of all elements in a list
numbers = [1, 2, 3, 4, 5] print("Sum:", sum(numbers))
10. Find the maximum element in a list
numbers = [10, 25, 8, 99, 45] print("Maximum:", max(numbers))
11. Check if a number is positive, negative or zero
num = int(input("Enter a number: ")) if num > 0: print("Positive") elif num < 0:
print("Negative") else: print("Zero")
12. Find the factorial of a number
num = int(input("Enter a number: ")) fact = 1 for i in range(1, num + 1): fact *= i
print("Factorial:", fact)
13. Check if a string contains a specific word
text = "Python is awesome" word = "Python" if word in text: print("Word found!")
else: print("Word not found!")
14. Find the sum of squares of all numbers in a list
numbers = [1, 2, 3, 4] sum_squares = sum([x**2 for x in numbers]) print("Sum of
squares:", sum_squares)
15. Find the reverse of a string
string = "Python" print("Reversed:", string[::-1])