Lab 02
Lab 02
Part 1: Printing
Comments are very important in your programs. They are used to tell you what
something does in English, and they also are used to disable parts of your program if you
need to remove them temporarily. Here’s how you use comments in Python:
Part 3: Variables
1 cars = 100
2 space_in_a_car = 4.0
3 drivers = 30
4 passengers = 90
5 cars_not_driven = cars - drivers
6 cars_driven = drivers
7 carpool_capacity = cars_driven * space_in_a_car
8 average_passengers_per_car = passengers / cars_driven
9
10 type(cars) # Try the other variables
11 print( "There are", cars, "cars available." )
12 print( "There are only", drivers, "drivers available." )
13 print( "There will be", cars_not_driven, "empty cars today." )
14 print( "We can transport", carpool_capacity, "people today." )
15 print( "We have", passengers, "to carpool today." )
Part 4: User Input
1 name = input()
2 # Enter Name
3 print(name)
4 print(‘Hello ’, name)
5 # What problem do you see?
6 name = input(‘Enter your name: ’)
7 print(‘Hi ’, name)
Part 5: Debugging
Part 6: Casting
1 age = input(“Enter your age: ”)
2 age = int(age)
3 print(age)
4 # What would be the age after 12 years(roughly)
5 print(age + 12)
6 # What is the result?
7 type(age)
8 float_age = float(age)
9 str_age = str(age)
10 type(float_age)
11 type(str_age)
12 age + 12
13 print(age)
1 print(5 < 3)
2 5 > 3
3 True and False
4 Type(5 < 3)
5 ‘hi’ > ‘hello’
6 5 = 5
7 #What went wrong?
8 favorite_color = ‘blue’
9 user_favorite = input(‘Enter your favorite color: ’)
10 favorite_color == user_favorite
Exercise
1. Evaluate an expression from user
Accept an algebraic expression from user and print out the result of the expression
2.
3.
secret_num = 10
temp = secret_num + 8 #evaluate secret + 8, and put the result
#in temp
temp = temp * 2 #evaluate temp * 2, and put the results
#in temp
temp = temp / 4
answer = temp - secret_num / 2
print(answer)