[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

Master Python Programs All Levels

The document provides a series of beginner Python programs, including examples such as printing 'Hello World', adding two numbers, checking if a number is even or odd, and finding the largest of three numbers. Each program includes an explanation of key functions and operators used, along with the corresponding code. Additional programs cover topics like simple calculators, reversing numbers, and working with arrays.

Uploaded by

drrain03
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)
4 views6 pages

Master Python Programs All Levels

The document provides a series of beginner Python programs, including examples such as printing 'Hello World', adding two numbers, checking if a number is even or odd, and finding the largest of three numbers. Each program includes an explanation of key functions and operators used, along with the corresponding code. Additional programs cover topics like simple calculators, reversing numbers, and working with arrays.

Uploaded by

drrain03
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
You are on page 1/ 6

Master Python Programs for Beginners

1. Print Hello World

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!")

2. Add Two Numbers

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)

3. Check Even or Odd

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")

4. Find Largest of Three Numbers

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)

1. Print Hello World

Explanation:
This prints a message on the screen using the print() function.

Code:
print("Hello, World!")

2. Add Two Numbers

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)

10. Floyd’s Triangle

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()

11. Pascal’s Triangle

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()

12. Find Max in Array

Explanation:
Uses built-in max() to find largest value.
Code:
arr = [int(x) for x in input("Enter numbers: ").split()]
print("Max:", max(arr))

13. Reverse Array

Explanation:
Uses list slicing to reverse.

Code:
arr = [int(x) for x in input("Enter numbers: ").split()]
print("Reversed:", arr[::-1])

14. Duplicate Elements

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))

15. Count Vowels

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)

You might also like