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

Essential Python Programs Guide

The document contains a collection of basic programming examples in Python, covering topics such as calculating factorials, compound interest, prime number checking, and generating Fibonacci numbers. It also includes list operations like finding the length, reversing, and summing elements, as well as string manipulations and pattern generation. Each program is presented with code snippets and prompts for user input.

Uploaded by

Aradhya Rawat
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)
17 views6 pages

Essential Python Programs Guide

The document contains a collection of basic programming examples in Python, covering topics such as calculating factorials, compound interest, prime number checking, and generating Fibonacci numbers. It also includes list operations like finding the length, reversing, and summing elements, as well as string manipulations and pattern generation. Each program is presented with code snippets and prompts for user input.

Uploaded by

Aradhya Rawat
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

Basic Programs

1. Factorial of a Number

p = int(input("Enter a number: "))


f=1
for i in range(1, p + 1):
f *= i
print("Factorial is:", f)

2. Compound Interest

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


r = float(input("Enter rate: "))
t = float(input("Enter time: "))
a = p * (1 + r / 100) ** t
c=a-p
print("Compound Interest:", c)

3. Prime Number Check

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


if n > 1:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime Number")
else:
print("Not Prime")

4. Fibonacci Numbers

y = int(input("How many terms? "))


a, b = 0, 1
for i in range(y):
print(a, end=" ")
a, b = b, a + b
5. ASCII Value of a Character

ch = input("Enter a character: ")


print("ASCII value is", ord(ch))

6. Know the Grade

m = int(input("Enter marks: "))


if m >= 90:
g = "A+"
elif m >= 80:
g = "A"
elif m >= 70:
g = "B+"
elif m >= 60:
g = "B"
elif m >= 50:
g = "C"
else:
g = "Fail"
print("Grade:", g)

7. Leap Year Check

y = int(input("Enter year: "))


if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

8. Looping Through a String

s = input("Enter string: ")


for ch in s:
print(ch)

9. Normal Table

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


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

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


i = 10
while i >= 1:
print(u, "x", i, "=", u * i)
i -= 1

List Programs
1. Length of List

p = [1, 2, 3, 4, 5]
l=0
for i in p:
l += 1
print("Length:", l)

2. Element Exists in List

s = [5, 10, 15, 20]


h = int(input("Check number: "))
if h in s:
print("Found")
else:
print("Not Found")

3. Reverse a List

n = [1, 2, 3, 4]
rev = []
for i in range(len(n) - 1, -1, -1):
[Link](n[i])
print("Reversed list:", rev)

4. Sum of List

y = [2, 4, 6, 8]
sum = 0
for i in y:
sum += i
print("Sum:", sum)

5. Smallest in List

s = [5, 3, 9, 1]
minn = s[0]
for i in s:
if i < minn:
minn = i
print("Smallest:", minn)

6. Largest in List

r = [4, 10, 3, 7]
maxx = r[0]
for i in r:
if i > maxx:
maxx = i
print("Largest:", maxx)

7. Even Numbers

u = [1, 2, 3, 4, 5, 6]
for i in u:
if i % 2 == 0:
print("Even:", i)

8. Odd Numbers

p = [1, 2, 3, 4, 5, 6]
for i in p:
if i % 2 != 0:
print("Odd:", i)

9. Even in Range

start = int(input("Start: "))


end = int(input("End: "))
for i in range(start, end + 1):
if i % 2 == 0:
print(i)

10. Odd in Range

start = int(input("Start: "))


end = int(input("End: "))
for i in range(start, end + 1):
if i % 2 != 0:
print(i)

11. Count Pos & Neg

n = [-2, 3, -1, 5, -6]


pos = 0
neg = 0
for i in n:
if i > 0:
pos += 1
elif i < 0:
neg += 1
print("Positive:", pos)
print("Negative:", neg)

String Programs
1. Length of String

h = input("Enter string: ")


count = 0
for ch in h:
count += 1
print("Length is:", count)

2. Even Length Words

s = input("Enter sentence: ")


words = [Link]()
for w in words:
if len(w) % 2 == 0:
print(w)

3. Split and Join

text = input("Enter text: ")


parts = [Link]()
print("Split:", parts)
joined = "-".join(parts)
print("Joined:", joined)

Pattern Programs
Pattern 1

rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()

Pattern 2

rows = 5
for i in range(1, rows + 1):
for j in range(i):
print("*", end="")
print()

Pattern 3

rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(chr(65 + j), end="")
print()

You might also like