9. 3D ANIMATION
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
from vpython import *
def show_curve():
curve(x=arange(100), y=arange(100)**0.5, color=color.red)
def show_sphere():
ball = sphere(pos=vector(1,2,1), radius=0.5)
def show_cone():
cone(textures=textures.stucco,axis=vector(-1,4,0),up=vector(1,2,2,))
def show_arrow():
arrow(pos=vector(-2,0,0),color=vector(1,0,1),axis=vector(1,2,2),up=vector(-1,5,2))
def show_rings():
ring(pos=vector(1, 1, 1), axis=vector(0, 1, 0), radius=0.5, thickness=0.1)
def show_cylinder():
rod= cylinder(pos=vector(0, 2, 1), axis=vector(5, 0, 0), radius=1)
Curve
Sphere
Cone
Arrow
Rings
Cylinder
9. 3D ANIMATION
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
def menu():
print('1. Curve')
print('2. Sphere')
print('3. Cone')
print('4. Arrow')
print('5. Rings')
print('6. Cylinder')
ch= 'y'
while(ch=='y'):
menu()
choice = int(input('Enter choice..'))
if(choice == 1):
show_curve()
elif(choice == 2):
show_sphere()
elif(choice == 3):
show_cone()
elif(choice == 4):
show_arrow()
elif(choice == 5):
show_rings()
elif(choice == 6):
show_cylinder()
else:
print('Wrong choice')
ch= input('Do you want to continue (y/n)...')
Print
Menu
Curve
Sphere
Cone
Arrow
Rings
Cylinder
else Print
10. PULSE Vs HEIGHT
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
import matplotlib.pyplot as plt
n=int(input('enter the number of people:'))
p=[]
h=[]
for i in range(n):
p_i=int(input(f"enter pulse rate of person{i+1}:"))
h_i=int(input(f"enter height no of person{i+1}:"))
p.append(p_i)
h.append(h_i)
plt.scatter(h,p)
plt.title("pulse rate vs height")
plt.xlabel("height(cm)")
plt.ylabel("pusle rate(bpm)")
plt.show()
if __name__=='__main__':
main()
Package & init
p, h Declared
Label & Value
11. MASS Vs TIME
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
import matplotlib.pyplot as plt
def calculate_mass(t):
return 60/(t+2)
time_values = [i*0.5 for i in range(21)]
mass_values = [calculate_mass(t) for t in time_values]
plt.plot(time_values, mass_values)
plt.title('Graph of t vs. m')
plt.xlabel('Time (hours)')
plt.ylabel('Mass (grams)')
plt.show()
Package & Calculation
Label & Values
12. PASSWORD
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
import re
p=input("Input your password:")
x=True
while x:
if(len(p)<6 or len(p)>12):
break
elif not re.search("[a-z]",p):
break
elif not re.search("[0-9]",p):
break
elif not re.search("[A-Z]",p):
break
elif not re.search("[$#@]",p):
break
elif not re.search("s",p):
break
else:
print("valid password")
x=False
break
if x:
print("Not a valid password")
Package & Print
if.. else Statement (User Authentication)
13. VELOCITY
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
import matplotlib.pyplot as plt
u = float(input("Enter the initial velocity: "))
a = float(input("Enter the acceleration: "))
time_values = [i*0.5 for i in range(21)
velocity_values = [u + a*t for t in time_values]
distance_values_time = [u*t + 0.5*a*t**2 for t in time_values]
velocity_squared_values = [(u + a*t)**2 for t in time_values]
distance_values_velocity = [(v**2 - u**2)/(2*a) for v in velocity_squared_values]
plt.plot(time_values, velocity_values)
plt.title('Velocity vs. Time')
plt.xlabel('Time (s)')
plt.ylabel('Velocity (m/s)')
plt.show()
Package & Print
Range
Calculation
Label
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
plt.plot(time_values, distance_values_time)
plt.title('Distance vs. Time')
plt.xlabel('Time (s)')
plt.ylabel('Distance (m)')
plt.show()
plt.plot(velocity_squared_values, distance_values_velocity)
plt.title('Distance vs. Velocity')
plt.xlabel('Velocity Squared (m^2/s^2)')
plt.ylabel('Distance (m)')
plt.show()
Label
Label
13. VELOCITY
14. ROBOT
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
import math
def compute_distance(movements):
x, y = 0, 0
for move in movements:
direction, steps = move.split()
steps = int(steps)
if direction == 'UP':
y += steps
elif direction == 'DOWN':
y -= steps
elif direction == 'LEFT':
x -= steps
elif direction == 'RIGHT':
x += steps
distance = math.sqrt(x**2 + y**2)
return round(distance)
# Take input from the user
moves = []
while True:
move = input("Enter a movement (or 'done' to finish): ")
if move == 'done':
break
moves.append(move)
# Compute and print the distance
distance = compute_distance(moves)
print("The distance from the original point is:", distance)
15. TUPLES
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
def get_info():
name, age, height = input().split(',')
return name.strip(), int(age.strip()), int(height.strip())
n = int(input("Enter the number of tuples: "))
tuples = []
for i in range(n):
tuple = get_info()
tuples.append(tuple)
sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2]))
print("Sorted Tuples:")
for tuple in sorted_tuples:
print(", ".join(str(i) for i in tuple))
15. TUPLES
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
def get_info():
name, age, height = input().split(',')
return name.strip(), int(age.strip()), int(height.strip())
n = int(input("Enter the number of tuples: "))
tuples = []
for i in range(n):
tuple = get_info()
tuples.append(tuple)
sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2]))
print("Sorted Tuples:")
for tuple in sorted_tuples:
print(", ".join(str(i) for i in tuple))
15. TUPLES
Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga.,
def get_info():
name, age, height = input().split(',')
return name.strip(), int(age.strip()), int(height.strip())
n = int(input("Enter the number of tuples: "))
tuples = []
for i in range(n):
tuple = get_info()
tuples.append(tuple)
sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2]))
print("Sorted Tuples:")
for tuple in sorted_tuples:
print(", ".join(str(i) for i in tuple))

Lab 3 Python Programming Lab 8-15 MKU.pdf

  • 1.
    9. 3D ANIMATION Mr.V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., from vpython import * def show_curve(): curve(x=arange(100), y=arange(100)**0.5, color=color.red) def show_sphere(): ball = sphere(pos=vector(1,2,1), radius=0.5) def show_cone(): cone(textures=textures.stucco,axis=vector(-1,4,0),up=vector(1,2,2,)) def show_arrow(): arrow(pos=vector(-2,0,0),color=vector(1,0,1),axis=vector(1,2,2),up=vector(-1,5,2)) def show_rings(): ring(pos=vector(1, 1, 1), axis=vector(0, 1, 0), radius=0.5, thickness=0.1) def show_cylinder(): rod= cylinder(pos=vector(0, 2, 1), axis=vector(5, 0, 0), radius=1) Curve Sphere Cone Arrow Rings Cylinder
  • 2.
    9. 3D ANIMATION Mr.V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., def menu(): print('1. Curve') print('2. Sphere') print('3. Cone') print('4. Arrow') print('5. Rings') print('6. Cylinder') ch= 'y' while(ch=='y'): menu() choice = int(input('Enter choice..')) if(choice == 1): show_curve() elif(choice == 2): show_sphere() elif(choice == 3): show_cone() elif(choice == 4): show_arrow() elif(choice == 5): show_rings() elif(choice == 6): show_cylinder() else: print('Wrong choice') ch= input('Do you want to continue (y/n)...') Print Menu Curve Sphere Cone Arrow Rings Cylinder else Print
  • 3.
    10. PULSE VsHEIGHT Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., import matplotlib.pyplot as plt n=int(input('enter the number of people:')) p=[] h=[] for i in range(n): p_i=int(input(f"enter pulse rate of person{i+1}:")) h_i=int(input(f"enter height no of person{i+1}:")) p.append(p_i) h.append(h_i) plt.scatter(h,p) plt.title("pulse rate vs height") plt.xlabel("height(cm)") plt.ylabel("pusle rate(bpm)") plt.show() if __name__=='__main__': main() Package & init p, h Declared Label & Value
  • 4.
    11. MASS VsTIME Mr. V. VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., import matplotlib.pyplot as plt def calculate_mass(t): return 60/(t+2) time_values = [i*0.5 for i in range(21)] mass_values = [calculate_mass(t) for t in time_values] plt.plot(time_values, mass_values) plt.title('Graph of t vs. m') plt.xlabel('Time (hours)') plt.ylabel('Mass (grams)') plt.show() Package & Calculation Label & Values
  • 5.
    12. PASSWORD Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., import re p=input("Input your password:") x=True while x: if(len(p)<6 or len(p)>12): break elif not re.search("[a-z]",p): break elif not re.search("[0-9]",p): break elif not re.search("[A-Z]",p): break elif not re.search("[$#@]",p): break elif not re.search("s",p): break else: print("valid password") x=False break if x: print("Not a valid password") Package & Print if.. else Statement (User Authentication)
  • 6.
    13. VELOCITY Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., import matplotlib.pyplot as plt u = float(input("Enter the initial velocity: ")) a = float(input("Enter the acceleration: ")) time_values = [i*0.5 for i in range(21) velocity_values = [u + a*t for t in time_values] distance_values_time = [u*t + 0.5*a*t**2 for t in time_values] velocity_squared_values = [(u + a*t)**2 for t in time_values] distance_values_velocity = [(v**2 - u**2)/(2*a) for v in velocity_squared_values] plt.plot(time_values, velocity_values) plt.title('Velocity vs. Time') plt.xlabel('Time (s)') plt.ylabel('Velocity (m/s)') plt.show() Package & Print Range Calculation Label
  • 7.
    Mr. V. VEERANAN,M.Sc. Computer Science., Dip.in.Yoga., plt.plot(time_values, distance_values_time) plt.title('Distance vs. Time') plt.xlabel('Time (s)') plt.ylabel('Distance (m)') plt.show() plt.plot(velocity_squared_values, distance_values_velocity) plt.title('Distance vs. Velocity') plt.xlabel('Velocity Squared (m^2/s^2)') plt.ylabel('Distance (m)') plt.show() Label Label 13. VELOCITY
  • 8.
    14. ROBOT Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., import math def compute_distance(movements): x, y = 0, 0 for move in movements: direction, steps = move.split() steps = int(steps) if direction == 'UP': y += steps elif direction == 'DOWN': y -= steps elif direction == 'LEFT': x -= steps elif direction == 'RIGHT': x += steps distance = math.sqrt(x**2 + y**2) return round(distance) # Take input from the user moves = [] while True: move = input("Enter a movement (or 'done' to finish): ") if move == 'done': break moves.append(move) # Compute and print the distance distance = compute_distance(moves) print("The distance from the original point is:", distance)
  • 9.
    15. TUPLES Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., def get_info(): name, age, height = input().split(',') return name.strip(), int(age.strip()), int(height.strip()) n = int(input("Enter the number of tuples: ")) tuples = [] for i in range(n): tuple = get_info() tuples.append(tuple) sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2])) print("Sorted Tuples:") for tuple in sorted_tuples: print(", ".join(str(i) for i in tuple))
  • 10.
    15. TUPLES Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., def get_info(): name, age, height = input().split(',') return name.strip(), int(age.strip()), int(height.strip()) n = int(input("Enter the number of tuples: ")) tuples = [] for i in range(n): tuple = get_info() tuples.append(tuple) sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2])) print("Sorted Tuples:") for tuple in sorted_tuples: print(", ".join(str(i) for i in tuple))
  • 11.
    15. TUPLES Mr. V.VEERANAN, M.Sc. Computer Science., Dip.in.Yoga., def get_info(): name, age, height = input().split(',') return name.strip(), int(age.strip()), int(height.strip()) n = int(input("Enter the number of tuples: ")) tuples = [] for i in range(n): tuple = get_info() tuples.append(tuple) sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1], x[2])) print("Sorted Tuples:") for tuple in sorted_tuples: print(", ".join(str(i) for i in tuple))