0% found this document useful (0 votes)
15 views7 pages

Vansh HHW

The document contains a collection of basic programming examples in Python, covering various topics such as calculating factorials, compound interest, checking prime numbers, generating Fibonacci sequences, and more. It also includes list operations like finding the length, reversing, and summing elements, as well as string manipulations and pattern printing. 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)
15 views7 pages

Vansh HHW

The document contains a collection of basic programming examples in Python, covering various topics such as calculating factorials, compound interest, checking prime numbers, generating Fibonacci sequences, and more. It also includes list operations like finding the length, reversing, and summing elements, as well as string manipulations and pattern printing. 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
You are on page 1/ 7

Basic Programs

1. Factorial of a Number

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


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

2. Compound Interest

p = float(input("Principal amount: "))


r = float(input("Rate of interest: "))
t = float(input("Time in years: "))
a = p * (1 + r / 100) ** t
ci = a - p
print("Compound Interest:", ci)

3. Prime Number Check

n = int(input("Enter a 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

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


a, b = 0, 1
for i in range(count):
print(a, end=" ")
a, b = b, a + b

5. ASCII Value of a Character

ch = input("Enter a character: ")


print("ASCII value of", ch, "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("Your grade is:", g)

7. Leap Year Check

y = int(input("Enter a 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 a string: ")


for ch in s:
print(ch)
9. Normal Table

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


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

10. Reverse Table

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


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

List Programs
1. Length of List

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

2. Element Exists in List

s = [10, 20, 30, 40]


v = int(input("Enter number: "))
if v in s:
print("Found in list")
else:
print("Not in list")

3. Reverse a List
h = [2, 4, 6, 8]
rev = []
for i in range(len(h) - 1, -1, -1):
rev.append(h[i])
print("Reversed list:", rev)

4. Sum of List

n = [3, 6, 9, 12]
total = 0
for i in n:
total += i
print("Sum:", total)

5. Smallest in List

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

6. Largest in List

l = [5, 8, 2, 10]
max_val = l[0]
for i in l:
if i > max_val:
max_val = i
print("Largest:", max_val)

7. Even Numbers in a List

v = [3, 6, 9, 12, 15]


for i in v:
if i % 2 == 0:
print("Even:", i)
8. Odd Numbers in a List

a = [4, 5, 6, 7, 8]
for i in a:
if i % 2 != 0:
print("Odd:", i)

9. Even Numbers 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 Numbers in Range

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

11. Count Positive and Negative

h = [-3, 0, 5, -2, 6]
pos = 0
neg = 0
for i in h:
if i > 0:
pos += 1
elif i < 0:
neg += 1
print("Positive:", pos)
print("Negative:", neg)
String Programs
1. Length of a String

a = input("Enter a string: ")


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

2. Even Length Words in a String

s = input("Enter a sentence: ")


words = s.split()
for word in words:
if len(word) % 2 == 0:
print(word)

3. Split and Join a String

text = input("Enter text: ")


parts = text.split()
print("After split:", parts)
joined = "-".join(parts)
print("After join:", 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