0% found this document useful (0 votes)
3 views8 pages

Basic Python Programs for Beginners

The document contains a collection of basic Python programs categorized into three sets: basic operations, using loops, and general codes. Each program demonstrates fundamental programming concepts such as arithmetic operations, control flow, and user input handling. The examples cover a variety of tasks including calculating areas, checking for odd/even numbers, and generating patterns.

Uploaded by

v81264290
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Basic Python Programs for Beginners

The document contains a collection of basic Python programs categorized into three sets: basic operations, using loops, and general codes. Each program demonstrates fundamental programming concepts such as arithmetic operations, control flow, and user input handling. The examples cover a variety of tasks including calculating areas, checking for odd/even numbers, and generating patterns.

Uploaded by

v81264290
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

✅ SET 1 – BASIC PYTHON PROGRAMS

1️⃣Sum, Product, Difference, Quotient & Remainder of two numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Sum =", a + b)
print("Product =", a * b)
print("Difference =", a - b)
print("Quotient =", a / b)
print("Remainder =", a % b)

2️⃣Swap two variables


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

a, b = b, a

print("After swapping:")
print("a =", a)
print("b =", b)

3️⃣Area according to shape (square / rectangle)


shape = input("Enter shape (square/rectangle): ")

if shape == "square":
side = int(input("Enter side: "))
print("Area =", side * side)

elif shape == "rectangle":


l = int(input("Enter length: "))
b = int(input("Enter breadth: "))
print("Area =", l * b)

else:
print("Invalid shape")

4️⃣Circumference & Area of Circle


r = float(input("Enter radius: "))
pi = 3.14

print("Circumference =", 2 * pi * r)
print("Area =", pi * r * r)
5️⃣Simple Interest
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))

si = (p * r * t) / 100
print("Simple Interest =", si)

6️⃣Odd or Even
n = int(input("Enter a number: "))

if n % 2 == 0:
print("Even")
else:
print("Odd")

7️⃣Positive or Negative
n = int(input("Enter a number: "))

if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")

8️⃣Vowel or Consonant
ch = input("Enter a letter: ").lower()

if ch in 'aeiou':
print("Vowel")
else:
print("Consonant")

9️⃣Even or Odd → if Even check multiple of 7


n = int(input("Enter number: "))

if n % 2 == 0:
print("Even")
if n % 7 == 0:
print("Multiple of 7")
else:
print("Not a multiple of 7")
else:
print("Odd")
🔟 Day of week
n = int(input("Enter week number (1-7): "))

days =
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

if 1 <= n <= 7:
print(days[n-1])
else:
print("Invalid input")

1️⃣1️⃣Grade according to percentage


p = float(input("Enter percentage: "))

if 91 <= p <= 100:


print("A1")
elif 81 <= p <= 90:
print("A2")
elif 71 <= p <= 80:
print("B1")
elif 61 <= p <= 70:
print("B2")
elif 51 <= p <= 60:
print("C1")
elif 41 <= p <= 50:
print("C2")
else:
print("Needs Improvement")

1️⃣2️⃣Triangle type (Scalene, Isosceles, Equilateral)


a = int(input("Side 1: "))
b = int(input("Side 2: "))
c = int(input("Side 3: "))

if a == b == c:
print("Equilateral")
elif a == b or b == c or a == c:
print("Isosceles")
else:
print("Scalene")

1️⃣3️⃣Right-angled triangle
a = int(input("Side 1: "))
b = int(input("Side 2: "))
c = int(input("Side 3: "))

if a*a + b*b == c*c or b*b + c*c == a*a or a*a + c*c == b*b:


print("Right angled triangle")
else:
print("Not right angled")

✅ SET 2 – USING for LOOP

1️⃣First n even numbers


n = int(input("Enter n: "))

for i in range(1, n+1):


print(2*i)

2️⃣Odd numbers less than n


n = int(input("Enter number: "))

for i in range(1, n):


if i % 2 != 0:
print(i)

3️⃣Print name n times


name = input("Enter name: ")
n = int(input("Enter times: "))

for i in range(n):
print(name)

4️⃣Check odd or even for n numbers


n = int(input("Enter count: "))

for i in range(n):
x = int(input("Enter number: "))
if x % 2 == 0:
print("Even")
else:
print("Odd")

5️⃣Series 1/2, 2/4, … n/n²


n = int(input("Enter n: "))

for i in range(1, n+1):


print(f"{i}/{i*i}")
6️⃣Square of numbers less than n
n = int(input("Enter number: "))

for i in range(n):
print(i*i)

7️⃣Divisible by 7 → print name 7 times


n = int(input("Enter number: "))
name = input("Enter name: ")

if n % 7 == 0:
for i in range(7):
print(name)

8️⃣Rainbow colors if black entered


color = input("Enter color code: ")

if [Link]() == "black":
print("Violet Indigo Blue Green Yellow Orange Red")

✅ SET 3 – USING while LOOP

1️⃣Stop when multiple of 5 entered


while True:
n = int(input("Enter number: "))
print(n)
if n % 5 == 0:
break

2️⃣Add elements to list


lst = []
while True:
x = input("Enter element or 'yes' to stop: ")
if x == "yes":
break
[Link](x)

print(lst)
print("Total elements:", len(lst))
3️⃣Pyramid & Reverse Pyramid
ch = input("Enter character: ")
n = int(input("Enter rows: "))

for i in range(1, n+1):


print(ch*i)

for i in range(n-1, 0, -1):


print(ch*i)

4️⃣Even or Odd printing


choice = input("Enter even or odd: ")
num = 2 if choice == "even" else 1

while input("Say yes to continue: ") == "yes":


print(num)
num += 2

5️⃣Length = breadth → square area


while True:
l = int(input("Length: "))
b = int(input("Breadth: "))
if l == b:
print("Area of square =", l*l)
break

6️⃣Multiple of 5 odd/even logic


n = int(input("Enter number: "))
k = int(input("Enter n: "))

if n % 5 == 0:
if n % 2 != 0:
for i in range(1, k+1):
print(n+i)
else:
for i in range(1, k+1):
print(n-i)

7️⃣Print till even entered


count = 0
while True:
n = int(input("Enter number: "))
if n % 2 != 0:
break
print(n)
count += 1
print("Even numbers printed:", count)

8️⃣Print till yes, sum & count


count = 0
total = 0

while input("Enter number? yes/no: ") == "yes":


n = int(input("Enter number: "))
total += n
count += 1

print("Count:", count)
print("Sum:", total)

✅ GENERAL CODES

Pyramid of symbol
ch = input("Enter symbol: ")
n = int(input("Rows: "))

for i in range(1, n+1):


print(ch*i)

Multiplication table
n = int(input("Enter number: "))

for i in range(1,11):
print(n, "x", i, "=", n*i)

Square of first n even numbers


n = int(input("Enter n: "))

for i in range(1, n+1):


print((2*i)**2)

Print name vertically


name = input("Enter name: ")

for ch in name:
print(ch)
Prime number check
n = int(input("Enter number: "))

flag = True
if n <= 1:
flag = False
else:
for i in range(2, n):
if n % i == 0:
flag = False
break

if flag:
print("Prime")
else:
print("Not Prime")

Leap year check


year = int(input("Enter year of birth: "))

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:


print("Leap Year")
else:
print("Not a Leap Year")

You might also like