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

Python Programming Basics and Examples

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 views3 pages

Python Programming Basics and Examples

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

DEPARTMENT OF COMPUTER APPLICATIONS

PYTHON PROGRAMMING – 120C11

1. PASCAL TRIANGLE
def print_pascal(n):
row = [1]
for i in range(n):
print(' ' * (n - i), end='')
print(' '.join(str(j) for j in row))
row = [sum(pair) for pair in zip([0]+row, row+[0])]
print_pascal(5)

OUTPUT

2. PATTERN USING NESTED LOOP


n=5
for i in range(n):
print((' '.join('*' * (i+1))).center(n*14))
for i in range(n-1, 0, -1):
print((' '.join('*' * i)).center(n*14))
OUTPUT
3.F TO C AND C TO F
def convert_temp():
while True:
choice = input("Enter 'F' for F to C, 'C' for C to F, or 'Q' to quit: ").upper()
if choice == 'Q':
print("Goodbye!"); break
temp = float(input("Enter temperature: "))
if choice == 'F':
print(f"{temp}°F = {(temp - 32) * 5/9:.2f}°C\n")
elif choice == 'C':
print(f"{temp}°C = {temp * 9/5 + 32:.2f}°F\n")
else:
print("Invalid choice.\n")
convert_temp()
OUTPUT

[Link] NUMBERS LESS THAN 20


def print_primes(n):
for num in range(2, n):
if all(num % i != 0 for i in range(2, num)):
print(num)
print_primes(20)

OUTPUT
[Link]’S GRADE

subjects = ['TAMIL', 'ENGLISH', 'FOC', 'STATISTICS', 'COMPUTER SCIENCE']


marks = []

for subject in subjects:


[Link](int(input(f"Enter marks for {subject}: ")))

total_marks = sum(marks)
percentage = (total_marks / (5*100)) * 100

print(f"Total Marks: {total_marks}")


print(f"Percentage: {percentage}%")

if percentage >= 80:


grade = 'A'
elif percentage >= 70:
grade = 'B'
elif percentage >= 60:
grade = 'C'
elif percentage >= 40:
grade = 'D'
else:
grade = 'E'

print(f"Grade: {grade}")

OUTPUT

You might also like