Python Programming Assignment
1. Python Program to Print Hello world!
print("Hello, world!")
[Insert Screenshot of Output for Program 1]
2. Python Program to Add Two Numbers
a=5
b=3
print("Sum:", a + b)
[Insert Screenshot of Output for Program 2]
3. Python Program to Find the Square Root
import math
num = 16
print("Square root:", math.sqrt(num))
[Insert Screenshot of Output for Program 3]
4. Python Program to Calculate the Area of a Triangle
a=5
b=6
c=7
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("Area of triangle:", area)
[Insert Screenshot of Output for Program 4]
5. Python Program to Check if a Number is Positive, Negative or 0
num = -2
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
[Insert Screenshot of Output for Program 5]
6. Python Program to Check if a Number is Odd or Even
num = 10
if num % 2 == 0:
print("Even")
else:
print("Odd")
[Insert Screenshot of Output for Program 6]
7. Python Program to Find the Factorial of a Number
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial:", factorial)
[Insert Screenshot of Output for Program 7]
8. Python Program to Display the Multiplication Table
num = 7
for i in range(1, 11):
print(f"{num} x {i} = {num*i}")
[Insert Screenshot of Output for Program 8]
9. Python Program to Remove Duplicate Element From a List
lst = [1, 2, 2, 3, 4, 4, 5]
print("List without duplicates:", list(set(lst)))
[Insert Screenshot of Output for Program 9]
10. Python Program to Find LCM using functions
def lcm(x, y):
greater = max(x, y)
while True:
if greater % x == 0 and greater % y == 0:
return greater
greater += 1
print("LCM:", lcm(4, 6))
[Insert Screenshot of Output for Program 10]
11. Python Program to Find the Factors of a Number using functions
def factors(n):
return [i for i in range(1, n+1) if n % i == 0]
print("Factors:", factors(12))
[Insert Screenshot of Output for Program 11]
12. Python Program to Check Whether a String is Palindrome or Not
string = "madam"
if string == string[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
[Insert Screenshot of Output for Program 12]
13. Python Program to Remove Punctuations From a String
import string as str_lib
text = "Hello, world!"
cleaned = "".join(char for char in text if char not in str_lib.punctuation)
print("Without punctuation:", cleaned)
[Insert Screenshot of Output for Program 13]
14. Python Program to Slice Lists
lst = [0, 1, 2, 3, 4, 5]
print("Sliced list:", lst[2:5])
[Insert Screenshot of Output for Program 14]
15. Python Program Read a File Line by Line Into a List
filename = "example.txt"
try:
with open(filename) as file:
lines = file.readlines()
print("Lines:", [line.strip() for line in lines])
except FileNotFoundError:
print(f"File '{filename}' not found.")
[Insert Screenshot of Output for Program 15]