Python manual
Python manual
num = 5
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
11. Write a program to read and write into a file.
file_name = input("Enter the name of the file to create and write to: ")
try:
with open(file_name, 'w') as file:
content = input("Enter the content to write to the file: ")
file.write(content)
print(f"Content has been written to '{file_name}'.")
except FileNotFoundError:
print(f"File '{file_name}' not found.")
try:
with open(file_name, 'r') as file:
content = file.read()
print("File content:")
print(content)
except FileNotFoundError:
print(f"File '{file_name}' not found.")
12. Write a Program to basic regular expression
import re
a=re.findall("l",str)
print(a)
b=re.search("o",str)
print(b.start())
d=re.sub("llo","abc",str)
print(d)
c=re.split("w",str)
print(c)
PART B
13) Demonstrate use of List.
list=[]
print("How many element do you want to insert into a list")
n=int(input())
print("Enter {0} element".format(n))
for i in range(n):
ele=int(input())
list.append(ele)
print(list)
print("The maximum element in the list:",max(list))
print("The minimum element in the list:",min(list))
print("The number of element in the list:",len(list))
list.sort()
print("The list after sorting")
print(list)
list.reverse()
print("The list after reverse")
print(list)
x=["role","age","marks","number"]
y=[3,1,5,6]
plt.subplot(1,2,2)
plt.bar(x,y)
plt.title("bar plot")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()
sub=['p','c','m','b']
mark=[90,34,56,23]
plt.subplot(1,2,2)
plt.pie(mark,labels=sub)
plt.show()
9) Create Array using NumPy and Perform Operations on array.
import numpy as np
a=[[1,2,3],[4,5,6],[7,8,9]]
b=[[1,2,3],[4,5,6],[7,8,9]]
n1=np.array(a)
n2=np.arry(b)
print(“ADD”,n1+n2)
print(“SUB”,n1-n2)
print(“MUL”,n1*n2)
print(“DIV”,n1/n2)
10) Create DataFrameform Excel sheet using Pandas and Perform Operations on DataFrames.
import pandas as pd
df=pd.read_excel(“data.xlsx”)
print(df)
df[‘age’]=df[‘age’]+10
age=df[‘age’]
print(age)