PROGRAM:
n=5
for i in range(n, 0, -1):
for j in range(n - i + 1):
print(i, end=' ')
print()
OUTPUT:
44
333
2222
11111
PROGRAM:
ch = input("Enter a single character: ")
if len(ch) != 1:
print("Please enter only a single character.")
else: if ch >= '0' and ch <= '9':
print("It is a Digit.")
elif ch >= 'a' and ch <= 'z':
print("It is a Lowercase Character.")
elif ch >= 'A' and ch <= 'Z':
print("It is an Uppercase Character.")
else:
print("It is a Special Character.")
OUTPUT:
Enter a single character: G
It is an Uppercase Character.
Enter a single character: 9
It is a Digit.
Enter a single character: $
It is a Special Character.
Enter a single character: k
It is a Lowercase Character.
PROGRAM:
a, b = 0, 1
count = 0
n = int(input("Enter the number of terms: "))
if n <= 0:
print("Please enter a positive integer.")
else:
print("Fibonacci sequence:")
while count < n:
print(a, end=' ')
a, b = b, a + b
count += 1
OUTPUT:
Enter the number of terms: 7
Fibonacci sequence:
0112358
PROGRAM:
my_list = []
my_list.append(10)
my_list.append(20)
my_list.append(30)
print("List after appending:", my_list)
my_list.extend([40, 50])
print("List after extending:", my_list)
my_list.remove(20)
my_list.pop(0)
print("Final list after removals:", my_list)
OUTPUT:
List after appending: [10, 20, 30]
List after extending: [10, 20, 30, 40, 50]
Final list after removals: [30, 40, 50]
PROGRAM:
my_tuple = (10, 20, 30, 20, 40, 50)
print("First element:", my_tuple[0])
print("Elements from index 1 to 3:", my_tuple[1:4])
new_tuple = my_tuple + (60, 70)
print("Concatenated tuple:", new_tuple)
repeated_tuple = my_tuple * 2
print("Repeated tuple:", repeated_tuple)
count_20 = my_tuple.count(20)
index_30 = my_tuple.index(30)
print("Count of 20:", count_20)
print("Index of 30:", index_30)
OUTPUT:
First element: 10
Elements from index 1 to 3: (20, 30, 20)
Concatenated tuple: (10, 20, 30, 20, 40, 50, 60, 70)
Repeated tuple: (10, 20, 30, 20, 40, 50, 10, 20, 30, 20, 40, 50)
Count of 20: 2
Index of 30: 2
PROGRAM:
my_dict = {}
my_dict["name"] = "Alice"
my_dict["age"] = 25
my_dict["city"] = "New York"
print("Dictionary after adding elements:", my_dict)
my_dict["age"] = 30
print("Dictionary after changing age:", my_dict)
del my_dict["city"]
print("Dictionary after removing city:", my_dict)
OUTPUT:
Dictionary after adding elements: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Dictionary after changing age: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Dictionary after removing city: {'name': 'Alice', 'age': 30}
PROGRAM:
def add(x, y):
return x + y
def subtract(x, y):
return x – y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Cannot divide by zero."
return x / y
print("Basic Calculator")
print("Select Operation:")
print("1. Add") print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
else:
print("Invalid input")
OUTPUT:
Basic Calculator
Select Operation:
1. Add
2. Subtract
3. Multiply
4. Divide Enter choice (1/2/3/4): 1
Enter first number: 10
Enter second number: 5
Result: 15.0
PROGRAM:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number to find factorial: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print("Factorial of", num, "is", factorial(num))
OUTPUT:
Enter a number to find factorial: 5
Factorial of 5 is 120
PROGRAM:
def palindrome(s):
s = [Link]()
s = [Link](" ", "")
return s == s[::-1]
word = input("Enter a string: ")
if palindrome(word):
print("It is a palindrome.")
else:
print("It is not a palindrome.")
OUTPUT:
Enter a string: madam
It is a palindrome.
PROGRAM:
a)PRINT EACH WORD OF A FILE IN REVERSE ORDER
file = open("[Link]", "r")
for line in file:
words = [Link]()
for word in words:
print(word[::-1], end=' ')
print()
[Link]()
b)PRINT EACH LINE OF A FILE IN REVERSE ORDER
file = open("[Link]", "r")
for line in file:
words = [Link]().split()
reversed_line = " ".join(reversed(words))
print(reversed_line)
[Link]()
c)DISPLAY THE CONTENT WITHOUT WHITESPACES
file = open("[Link]", "r")
content = [Link]()
print("".join([Link]()))
[Link]()
OUTPUT:
a) Each word in reverse order:
nohtyP gnimmargorP
si nuf
b) Each line in reverse word order:
Programming Python
fun is
c) Content without whitespaces:
PythonProgrammingisfun
PROGRAM:
# File handling and word analysis
with open("[Link]", "r") as src:
content = [Link]()
with open("[Link]", "w") as dst:
[Link](content)
words = [Link]()
print("Word count:", len(words))
print("Longest word:", max(words, key=len))
OUTPUT:
Word count: 12
Longest word: programming
PROGRAM:
# Divide by zero
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
print("Result:", a / b)
except ZeroDivisionError:
print("Cannot divide by zero")
# Age check
age = int(input("Enter age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
# Marks validation
marks = int(input("Enter marks: "))
if 0 <= marks <= 100:
print("Valid marks")
else:
print("Invalid marks")
OUTPUT:
Result: 4.0
Eligible to vote
Valid marks
PROGRAM:
# Using numpy and pandas
import numpy as np
import pandas as pd
a = [Link]([1, 2, 3])
print("NumPy Array:", a)
df = [Link]({'Name': ['Tom', 'Jerry'], 'Age': [20, 21]})
print("Pandas DataFrame:\n", df)
OUTPUT:
NumPy Array: [1 2 3]
Pandas DataFrame:
Name Age
0 Tom 20
1 Jerry 21
PROGRAM:
# Regular expression to find substrings
import re
text = "Python exercises, PHPexercises"
pattern = r"exercises"
matches = [Link](pattern, text)
for match in matches:
print("Found:", match)
OUTPUT:
Found: exercises
Found: exercises