Python Programs:
1. Write a Python program to print "Hello, World!".
2. Write a program to take the user’s name and age as input and display them.
3. Write a program to swap the values of two variables.
4. Write a program to check if a number entered by the user is even or odd.
5. Write a program to calculate the area of a rectangle.
6. Write a program to find the largest of two numbers entered by the user.
7. Write a program to print the multiplication table of a number entered by the user up
to 10.
8. Write a program to find the sum of the first 10 natural numbers using a loop.
9. Write a program to count from 1 to 10 using a while loop.
10. Write a program to take a list of numbers from the user and display the sum of the
numbers.
11. Write a program to append a new element to a list and display the updated list.
12. Write a program to display all even numbers between 1 and 50.
13. Write a program to calculate the factorial of a number entered by the user.
14. Write a python program using a while loop to print numbers from 10 down to 1.
15. Write a program to read 5 positive elements in list. Display elements of list. Reverse
elements of list and display.
16. Write a program to find the first and last character of a string.
17. Write a program to count how many times a particular character appears in a string.
18. Write a program to read a string. Display entered string. Reverse the list and display
result.
19. Write a program to read 5 elements. display Greatest and smallest number.
20. Write a program to enter two integers and perform all arithmetic operations on them.
Code :
# 1. Print "Hello, World!"
print("Hello, World!")
# 2. Take user's name and age as input
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Name:", name)
print("Age:", age)
# 3. Swap values of two variables
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping: a =", a, ", b =", b)
# 4. Check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
# 5. Area of rectangle
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
print("Area of rectangle =", length * breadth)
# 6. Largest of two numbers
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
if x > y:
print(x, "is largest")
else:
print(y, "is largest")
# 7. Multiplication table
n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
# 8. Sum of first 10 natural numbers
s=0
for i in range(1, 11):
s += i
print("Sum of first 10 natural numbers =", s)
# 9. Count 1 to 10 using while loop
i=1
while i <= 10:
print(i)
i += 1
# 10. Sum of numbers in a list
nums = list(map(int, input("Enter numbers separated by space: ").split()))
print("Sum of numbers =", sum(nums))
# 11. Append new element to list
lst = [1, 2, 3, 4]
new = int(input("Enter element to append: "))
lst.append(new)
print("Updated list:", lst)
# 12. All even numbers between 1 and 50
for i in range(2, 51, 2):
print(i, end=" ")
# 13. Factorial of a number
n = int(input("\nEnter a number: "))
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial =", fact)
# 14. Print numbers 10 to 1 using while loop
i = 10
while i >= 1:
print(i)
i -= 1
# 15. Read 5 positive elements, display and reverse
lst = []
for i in range(5):
num = int(input("Enter positive number: "))
lst.append(num)
print("Original list:", lst)
lst.reverse()
print("Reversed list:", lst)
# 16. First and last character of a string
s = input("Enter a string: ")
print("First character:", s[0])
print("Last character:", s[-1])
# 17. Count occurrence of a character in string
s = input("Enter a string: ")
ch = input("Enter character to count: ")
print(ch, "appears", s.count(ch), "times")
# 18. Read a string, display and reverse
s = input("Enter a string: ")
print("Entered string:", s)
print("Reversed string:", s[::-1])
# 19. Greatest and smallest number from 5 elements
lst = []
for i in range(5):
num = int(input("Enter number: "))
lst.append(num)
print("Greatest =", max(lst))
print("Smallest =", min(lst))
# 20. Arithmetic operations on two integers
a = int(input("Enter first integer: "))
b = int(input("Enter second integer: "))
print("Sum =", a + b)
print("Difference =", a - b)
print("Product =", a * b)
print("Quotient =", a / b if b != 0 else "Division by zero not allowed")
print("Modulus =", a % b if b != 0 else "Not defined")
print("Exponentiation =", a ** b)