Master Python Programs All Levels
Master Python Programs All Levels
Explanation:
This is the first program. It uses the print() function to show text on
the screen.
- print(): A function used to display messages.
Code:
# This program prints a simple message.
print("Hello, World!")
Explanation:
This program takes two numbers from the user and adds them.
- input(): Takes input from the user as text.
- int(): Converts text to integer (number without decimal).
- +: Adds the numbers.
Code:
# This program adds two numbers entered by the user.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("The sum is:", sum)
Explanation:
We use % to find remainder. If remainder is 0, it's even.
- % (modulo): Gives the remainder.
- if...else: Used to make decisions.
Code:
# This program checks whether a number is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Explanation:
We compare three numbers using if...elif...else.
- and: Logical operator to check two conditions together.
- >=: Greater than or equal to.
Code:
# This program finds the largest among three numbers.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest is:", a)
elif b >= a and b >= c:
print("Largest is:", b)
else:
print("Largest is:", c)
Explanation:
This prints a message on the screen using the print() function.
Code:
print("Hello, World!")
Explanation:
Takes two numbers from the user and prints their sum. Uses input(),
int(), and print().
Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
3. Even or Odd
Explanation:
Checks if number is divisible by 2 using % operator.
Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
4. Largest of 3 Numbers
Explanation:
Uses if...elif...else to compare three values.
Code:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
if a >= b and a >= c:
print("Largest is:", a)
elif b >= a and b >= c:
print("Largest is:", b)
else:
print("Largest is:", c)
5. Simple Calculator
Explanation:
Performs addition, subtraction, multiplication or division based on
user input.
Code:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
op = input("Enter operation (+,-,*,/): ")
if op == "+": print("Sum:", a + b)
elif op == "-": print("Difference:", a - b)
elif op == "*": print("Product:", a * b)
elif op == "/": print("Quotient:", a / b)
6. Sum of Digits
Explanation:
Uses loop and % to extract digits one by one.
Code:
num = int(input("Enter number: "))
sum = 0
while num > 0:
sum += num % 10
num //= 10
print("Sum of digits:", sum)
7. Reverse a Number
Explanation:
Reverses digits by multiplying and adding in reverse order.
Code:
num = int(input("Enter number: "))
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed number:", rev)
8. Palindrome Number
Explanation:
Checks if number is same forwards and backwards.
Code:
num = int(input("Enter number: "))
original = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
if original == rev:
print("Palindrome")
else:
print("Not palindrome")
9. Print Triangle Pattern
Explanation:
Prints a triangle using nested loops.
Code:
n = int(input("Enter rows: "))
for i in range(1, n+1):
print("*" * i)
Explanation:
Numbers increase row by row in triangle format.
Code:
n = int(input("Enter rows: "))
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=" ")
num += 1
print()
Explanation:
Uses combination formula comb(n, k) to build triangle.
Code:
from math import comb
n = int(input("Enter rows: "))
for i in range(n):
for j in range(i+1):
print(comb(i, j), end=" ")
print()
Explanation:
Uses built-in max() to find largest value.
Code:
arr = [int(x) for x in input("Enter numbers: ").split()]
print("Max:", max(arr))
Explanation:
Uses list slicing to reverse.
Code:
arr = [int(x) for x in input("Enter numbers: ").split()]
print("Reversed:", arr[::-1])
Explanation:
Tracks seen items using set to find duplicates.
Code:
arr = [int(x) for x in input("Enter numbers: ").split()]
seen = set()
dups = set()
for x in arr:
if x in seen:
dups.add(x)
seen.add(x)
print("Duplicates:", list(dups))
Explanation:
Counts how many vowels in string using loop.
Code:
s = input("Enter string: ").lower()
vowels = "aeiou"
count = sum(1 for c in s if c in vowels)
print("Vowel count:", count)