Python lab file
Python lab file
Lab 1:
#Print hello
print("Hello")
#Area of circle
pi=3.14
radius=2
area=pi*(radius**2)
print(area)
#Ternary operator
num=10
result="Even"if num%2==0 else "Odd"
print(f"The number is {num} and it is {result}")
Lab 2:
#Amstrong number
input=153
a=input%10
b=input//10
c=b//10
b=b%10
result=(a**3)+(b**3)+(c**3)
print(result)
#Celsius to Fahrenheit
celsius=38
f=(9/5*celsius)+32
print("The fahrenheit is {}".format(f))
#Compound intereset
p=5000
r=5.5
t=3
a=(p*(1+r/100)**t)-p
print("The compound interset is: ",a)
#Fahrenheit to Celsius:
f=77
c=(5/9)*(f-32)
print("The celsius is:",c)
#Palindrome:
num=45654
x=num%10
y=num//10000
z=(num//10)%10
a=(num//1000)%10
result="palindrome" if(x==y and z==a) else "Not a palindrome"
print(result)
#Positive or negative
num=-15
result="positive" if (num>=0) else "Negative"
print("The result is:{}".format(result))
#Area of circle:
pi=3.14159
radius=2
area=pi*(radius**2)
print(area)
# consider a incandescent lamp 230v nad 100w determine the lamp resistance
lamp current energy consumed by the lamp in 8hrs
import math
voltage=230
power=100
time=8
current=power/voltage
resistance=(math.pow(voltage,2))/power
energy=power*time
print("lamp current is: %5.2f"%current,"A")
print("lamp resistance is:",resistance,"ohms")
print("Energy consumed by lamp in 8hrs is:",energy,"wh")
#Quadratic equations:
'''import math
a=1
b=-3
c=2
e=(b**2)-4*a*c
d=math.sqrt(e)
root_1=(-b+d)/2*a
root_2=(-b-d)/2*a
print("root 1 is:{}".format(root_1))
print("root 2 is:{}".format(root_2))'''
import cmath
a=1
b=-3
c=2
e=(b**2)-4*a*c
d=cmath.sqrt(e)
root_1=(-b+d)/2*a
root_2=(-b-d)/2*a
print("root 1 is:{}".format(root_1))
print("root 2 is:{}".format(root_2))
#Simple interest
p=150
r=17
t=8
si=(p*r*t)/100
print("The simple interest is %4.2f"%si)
# other method
a=5
b=10
print("The value of a before swapping:",a)
print("THe value of b before swapping:",b)
a,b=b,a
print("The value of a after swapping:",a)
print("The value of b after swapping:",b)
#Volume of cylinder
import math
radius=5
height=10
pi=3.14
area=(pi*math.pow(radius,2))*height
print("Area of the cylinder is {}".format(area))
#Grading
sub1=int(input("Enter the mark of Electromagentic theory:"))
sub2=int(input("Enter the mark of Analog integrtaed circuit:"))
sub3=int(input("Enter the mark of signals and systems:"))
sub4=int(input("Enter the mark of Digital electronis:"))
sub5=int(input("Enter the mark of Statistucs and datascience:"))
sub6=int(input("Enetr the mark of python programming:"))
average=(sub1+sub2+sub3+sub4+sub5+sub6)/6
if(average>=90):
print("your grade is O")
elif(average<90 and average>=80):
print("Your grade is A")
elif(average<80 and average>=70):
print("Your grade is B")
elif(average<70 and average>=60):
print("Your grade is C")
else:
print("sorry you are fail!!")
#Leap year
year=int(input("enter a year:"))
if((year%4==0)and (year%400==0 or year%100!=0)):
print("It is a leap year")
else:
print("It is not a leap year")
#odd or even
input=int(input("Enter a number:"))
if(input%2==0):
print("The given number {} is an even number!!".format(input))
else:
print("The given number {} is a odd number!!".format(input))
#user credentials
org_username="ponabirami"
org_password="123@"
name=input("Enter your username:")
if(name==org_username):
password=input("Enter your passsword")
if(password==org_password):
print("welcome!! Login successful")
else:
print("wrong password!! access denied")
else:
print("retry!! invalid username")
#Voting eligibility
age=int(input("Enter your age:"))
if(age>=18):
print("Congrats!!you are eligible to vote")
else:
print("Sorry!! yor are not eligible to vote")
#Voting eligibility using nested if
age=int(input("Enter your age:"))
if(age>0):
if(age>=18):
print("You are eligible to vote!!")
else:
print("Sorry!! you are not eligible to vote!!")
else:
print("Sorry make sure your input age is correct")
#Weekdays
day=int(input("Enter the number of the day:"))
if(day==1):
print("the day is monday")
elif(day==2):
print("The day is tuesday")
elif(day==3):
print("The day is wednesday")
elif(day==4):
print("The day is thursday")
elif(day==5):
print("The day is friday")
elif(day==6):
print("The day is saturday")
elif(day==7):
print("The day is sunday")
else:
print("enter a number between 1-7")
i=7
sum=0
while i<10:
sum=sum+i
i+=1
print(sum)
i=5
sum=0
while i<11:
sum=sum+i
i+=2
print(sum)
#Fibonacci series:
a=0
b=1
print(a)
for c in range(1,10):
c=a+b
a=b
b=c
print(a)
#Reverse order:
for i in range(10,0,-1):
print(i)
#Multipication table from 1-5
for i in range(1,6):
for j in range(1,11):
m=i*j
print("{}X{}={}".format(i,j,m))
#Right triangle star:
for i in range(1,6):
for j in range(1,6):
if(i==j):
print(i*"*")
#Sum of n natural numbers:
sum=0
for i in range(1,101):
sum+=i
print(sum)
n=100
sum1=(n*(n+1))/2
print(sum1)
#Armstrong number:
for i in range (100,1000):
a=i%10
b=i//10
b=b%10
c=i//100
num = ((a*3)+(b*3)+(c*3))
if (num == i):
print("{} is a amstrong number".format(num))
#KVL
r1=int(input("Enter the value of R1:"))
r2=int(input("Enter the value of R2:"))
voltage=int(input("Enter the source voltage:"))
Req=r1+r2
print("The equivalent resistance is: {}".format(Req))
I=(voltage)/(Req)
kvl=(-10)+(r1*I)+(r2*I)
if (kvl==0):
print("The given kvl equation is correct")
print("the sum of voltage in a closed loop is zero ")
else:
print("The given kvl equation is false")
print("The sum of voltages in a closed loop is not zero")
#KCL:
sum1=0
sum2=0
n=input("Is there any entering current:(y/n)")
while n!="n":
I1=int(input("Enter the entering current:"))
i=input("Is there any other current:(y/n)")
if (i=="n"):
break
sum1+=I1
m=input("Is ther any leaving current:(y/n)")
while m!="n":
I2=int(input("Entering the leaving current:"))
j=input("Is there any other current:(y/n)")
if (j=="n"):
break
sum2+=I2
if (sum1==sum2):
print("The sum of entering current is equal to sum of leaving current")
else:
print("The sum of entering current is not equal to sum of leaving current")
#Superposition theorem:
v1=int(input("Enter the voltage 1:"))
v2=int(input("Enter the voltage source 2:"))
R1=int(input("Enter the resistor value 1:"))
R2=int(input("Enter the resistor value 2:"))
R3=int(input("Enter the resistor value 3:"))
print("----When voltage source 1 is activated----")
r1=(R3*R2)/(R3+R2)
Req1=r1+R1
I1=v1/Req1
current1=(I1*R2)/(R3+R2)
print("The value of current when V1 is activated: {}A".format(current1))
print("----When voltage source 2 is activated----")
r2=(R1*R2)/(R1+R2)
Req2=r2+R3
I2=v2/Req2
current2=(I2*R2)/(R2+R1)
print("The value of current when V2 is activated: {}A".format(current2))
total=current1+current2
print("The total current is: {}A".format(total))
i=2
print(L[i-1])
L[1]=5
print(L)
a.insert(0,0)
print(a)
b=[7,8,9]
a.extend(b)
print(a)
print(a.index(5))
a.sort()
print(a)
a.reverse()
print(a)
a.pop()
print(a)
#print(a.pop())
print(a.pop(0))
a.remove(1)
print(a)
print(a.count(6))
c=a.copy()
print(c)
print(len(a))
print(min(a))
print(max(a))
a.clear()
print(a)
'''del(a)
print(a)'''
L=[9,6,0,3]
print(sorted(L))
print(L)
L.sort()
print(L)
L.reverse()
print(L)
# Tuple + list:
T=(1,2,3,4)
L=[5,6,7]
N=tuple(L)
print(N)
T1=T+N
print(T1)
# List + Tuple:
L=[1,2,3]
T=(4,5,6)
L.append(T)
print(L)
#Cloning:
cool=['blue','green','grey']
chill=cool[:]
chill.append('black')
print(chill)
print(cool)
#Alias:
hot=['red','yellow','orange']
warm=hot
warm.append('pink')
print(warm)
print(hot)
#Nested list:
warm=['yellow','orange']
hot=['red']
brightcolors=[warm]
brightcolors.append(hot)
print(brightcolors)
hot.append('pink')
print(hot)
print(brightcolors)
August 22
#Dictionary operations:
grades={'Ana':'B','John':'A+','Denis':'A','Katy':'A'}
#removes the key from the dictionary and the removed value is printed
removed_grade=grades.pop('Denis')
print("Removed key:",removed_grade)
print(grades)
'''#print(grades.popitem())
#a=grades.pop('Nivetha')
last_item=grades.popitem()
print("Removed last element:",last_item)
print("Updated dictionary:",grades)
last_item=grades.popitem()
print("Removed last element:",last_item)
print("Updated dictionary:",grades)
ast_item=grades.popitem()
print("Removed last element:",last_item)
print("Updated dictionary:",grades)'''
# returns value of a key if it is in the dictionary if not inserts the jkey with the
specified value and returns the value
default_grade=grades.setdefault('Mike','B')
print("Mike's grade:",default_grade)
print("Updated dictionary",grades)
# to update dictionary and add another dictionary with the old one
grades={'Ana':'B','John':'A+','Mike':'B'}
grades.update({'Lisa':'A','Mark':'B+'})
print("Updated dictionary:",grades)
grades={'Ana': 'B', 'John': 'A+', 'Mike': 'B', 'Lisa': 'A', 'Mark': 'B+'}
sorted_keys=sorted(grades.keys())
print(sorted_keys)
sorted_items=dict(sorted(grades.items()))
print(sorted_items)
sorted_values=sorted(grades.values())
print(sorted_values)
my_dict={} #empty dictionary
print(my_dict)
grades={'Ana':'B','John':'A+','Denise':'A','Katy':'A','Ana':'C'}
print(grades)
print(grades['John'])
#print(grades['Sylvan']) #produces an error because no such key is available
# to add an entry
grades['Sylvan']='A'
print(grades)
Other method:
grades={3:98.4,1:90.7,2:99.8,4:92.5}
print('input dict',grades)
sorted_g=dict(sorted(grades.items(),key=lambda item:item[1],reverse=True))
print('sorted as per values',sorted_g)
August 29
Apply and prove KVL and KCL:
# Apply and prove KCL and KVL for the given circuit
voltage_source=int(input("Enter the value of voltage source:"))
R1=int(input("enter the value of R1:"))
R2=int(input("Enter the value of R2:"))
R3=int(input("enter the value of R3:"))
Req1=((R2*R3)/(R2+R3))
Req2=R1+Req1
print(Req2)
#KCL
I1=(voltage_source)/Req2
I2=(I1*R3)/(R2+R3)
I3=(I1*R2)/(R2+R3)
i1=format(I1,"3.2f")
i2=format(I2,"3.2f")
i3=format(I3,"3.2f")
print("I1=",format(I1,"3.2f"),"I2=",format(I2,"3.2f"),"I3=",format(I3,"3.2f"))
if (I1==I2+I3):
print("---KCL is verified---")
else:
print("---It does not satisfy KCL law--")
#KVL
v1=R1*I1
print("V1",format(v1,"3.2f"))
v2=R2*I2
print("V2",format(v2,"3.2f"))
v3=R3*I3
print("V3",format(v3,"3.2f"))
loop1=voltage_source-v1-v3
loop2=v2-v3
print("Sum of voltages in loop1",loop1)
print("Sum of voltages in loop2",loop2)
if (loop1==loop2==0):
print("---KVL is satisfied---")
else:
print("---KVL is not satisfied---")
my_dict={}
my_dict.update({R1:i1,R2:i2,R3:i3})
print(my_dict)
#OP-AMP:
R1=220
R2=220
V1=10.3
V2=10.4
IB1=V1/R1
IB2=V2/R2
input_bias=(IB1+IB2)/2
input_offset=(IB2-IB1)
print("Input bias current=",format(input_bias,"3.2f"),"mA")
print("Input offset current=",format(input_offset,"3.4f"),"mA")