python_lab_A_B
python_lab_A_B
Output
Enter the number you want to check: 4
Given number 4 is not a fibonacci number
Enter the number you want to check: 5
Given number 5 is fibonacci number
import math
a = int(input('Enter a:'))
b = int(input('Enter b:'))
c=int(input('Enter C:'))
dis=b*b-4*a*c
sqrt_dis=math.sqrt(abs(dis))
if dis > 0:
print(" Real and different roots are : ")4
print((-b + sqrt_dis) / (2*a))
print((-b - sqrt_dis) / (2 * a))
elif dis==0:
print(" Real and same roots are: ")
print(-b / (2 * a))
else:
print("Complex Roots are : ")
print(- b / (2 * a), " + ", sqrt_dis, "j")
print(- b / (2 * a), " - ", sqrt_dis, "j")
output
Enter a:1
Enter b:2
Enter C:3
Complex Roots are :
-1.0 + 2.8284271247461903 j
-1.0 - 2.8284271247461903 j
Output
Enter a number: 5
The sum of 5 natural numbers = 15
output
Output
Enter a number: 6
6 is not a prime number
Enter a number: 3
3 is a prime number
Output
output
Given string : Python is powerful programming language.
String in Uppercase: PYTHON IS POWERFUL PROGRAMMING LANGUAGE.
string in lowercase: python is powerful programming
language.check if the given string starts with character 'P':
True
check if the given string ends with string 'Powerful':
FalseCount of character'g' in the given string: 4
String with each word in uppercase: Python Is Powerful Programming Language.
String with each word in upper and lowercase swapped: pYTHON IS
POWERFULPROGRAMMING LANGUAGE.
Find occurrence of sub string'Language' in the given string: -1
String after replacement of string 'powerful' by 'high level': Python is high level
programminglanguage.
string splitting: ['Python is powerful programming language.']
string after joining: Python is powerful programming language.and portable
output
The array after sorting in Ascending Order by selection sort is:
file_path="example.txt"
def write_to_file(file_path,content):
with open(file_path,'w') as file:
file.write(content)
print("content written to the file succesfully!")
def read_from_file(file_path):
with open(file_path,'r') as file:
content=file.read()
print("content read from the file succesfully!")
print(content)
write_to_file(file_path,"hello")
read_from_file(file_path)
PART B
1. Demonstrate use of basic regular expression
import re
str="Python Programming"
x=re.findall("Program",str)
print(x)
x=re.findall("y",str)
print(x)
x=re.findall("ing",str)
print(x)
x=re.search("Programming",str)
print(x)
print(x.span())
print(x.start())
print(x.end())
print(x.group())
x=re.split(" ",str)
print(x)
x=re.sub("\s","$",str)
print(x)
Output
['Program']
['y']
['ing']
<re.Match object; span=(7, 18),
match='Programming'> (7, 18)
7
18
Programming
['Python',
'Programming']
Python$Programming
import re
if name ==' main ':
def validate_email(email:bool):
regex=re.compile('^[a-z0-9]+[\\.\_]?[a-z0-9]+[@]\w+[.]\w{2,3}$')
matchobj=re.search(regex,email)
asbool=bool(matchobj)
return asbool
my_email=input("Enter an email address")
res=validate_email(my_email)
if res==True:
print("Email is valid")
else:
print("Email is not valid")
Output
Enter an email address:
[email protected]
Email is valid
name=[]
marks=[]
for i in range(5):
n=input("Enter student name")
m=int(input("Enter marks of student"))
name.append(n)
marks.append(m)
high=max(marks)
low=min(marks)
print("Highest marks is",high)
print("Lowest marks is",low)
for i in range(5):
if high==marks[i]:
print("Student having highest marks is",name[i])
if low==marks[i]:
print("Student having lowest marks is",name[i])
Output
Enter student name:
Arun Enter marks of
student: 76 Enter student
name: Arav Enter marks
of student: 69 Enter
student name: Astha
Enter marks of student:
82 Enter student name:
Briti Enter marks of
student: 86
Enter student name: Manthhan
Enter marks of student: 85
Highest marks is 86
Lowest marks is 69
Student having lowest marks is Arav
Student having highest marks is Briti
4. Demonstrate use of dictionary
Employee_name Salary
Kaashvi 12000
Samarth 14000
Ashwin 11500
import time
from tkinter import *
canvas = Tk()
canvas.title("Digital Clock")
canvas.geometry("350x200")
canvas.resizable(1,1)
label = Label(canvas, font=("Courier", 30, 'bold'), bg="blue", fg="white",
bd =30)
label.grid(row =0, column=1)
def digitalclock():
text_input = time.strftime("%H:%M:%S")
label.config(text=text_input)
label.after(200, digitalclock)
digitalclock()
canvas.mainloop()
try:
a = int(input("Enter value for a"))
b = int(input("Enter value for b"))
print(a / b)
age = int(input("How old are you?"))
print(f"You are {age} years old")
print("Select your favroite fruit")
list=["Apple","Cherry","Mango"]
print(list[1])
except ZeroDivisionError:
print('Division by zero not allowed')
except ValueError:
print("Invalid age was entered")
except IndexError:
print("Invalid index was used for the list")
except:
print("some other exception occured")
finally:
print("End of program")
Output
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(dict(data=[2, 4, 1, 5, 9, 6, 0, 7]))
fig, ax = plt.subplots()
df['data'].plot(kind='bar', color='red')
df['data'].plot(kind='line', marker='*', color='black', ms=10)
plt.show()
import numpy as np
print('First array:')
print(arr)
print('\nSecond array:')
print(arr1)
First array:
[ 7 16 25]
Second array:
[4 8 6]
arrays: [ 3 8 19]
[1.75 24.16666667]
[3 0 1]
power for
array [ 49 256
625]