Nothing Special   »   [go: up one dir, main page]

Kirubel Teka Part6 Fill Table

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Addis Ababa Institute of Technology

Center of Information Technology and Scientific Computing


Fundamentals of Computer Science and Programming

Lab2 (Variables and Data Types)

Part 1: Printing

1 print("I will now count my chickens:" )


2
3 print("Hens", 25 + 30 / 6)
4 print("Roosters", 100 - 25 * 3 % 4)
5
6 print("Continue Printing” )
7
8 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
9
10 print(3 + 2 < 5 - 7)
11
12 print("What is 3 + 2?", 3 + 2)
13

Comments and Pound Character

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:

1 # A comment, this is so you can read your program later.


2 # Anything after the # is ignored by python.

3 print("I could have code like this.") # the comment is ignored


Part 2: Printing, Printing, Printing

1 # remember type it exactly.


2
3 days = "Mon Tue Wed Thu Fri Sat Sun"
4 months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
5
6 print("Here are the days:\t", days)
7 print("Here are the months: ", months)
8
9 print("""
10 There's something going on here.
11 With the three double- quotes.
12 We'll be able to type as much as we like.
13 Even 4 lines if we want, or 5, or 6.
14 """)
15

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

1 age = input(“Enter your age: ”)


2 print(age)
3 # What would be the age after 12 years(roughly)
4 print(age + 12)
5 # What is the result?
6 # What do you think is the problem?
7 type(age)

Part 6: Casting

Type error 1.0


Type error Type error
'2' '2'
value error value error
1 1
1.0 1.0
'1.0' '1.0'
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)

Part 6: Booleans and Expressions

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

True True
False False

True True

False False
Exercise
1. Evaluate an expression from user

Accept an algebraic expression from user and print out the result of the expression

1 expression = input(‘Enter an expression: ’)


2 #Your code goes here
3 #Hint check help(eval) on python shell

2.

Write code to swap the values of variable a and b

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)

Try changing the secret number(i.e secret_num). Why is it working?

You might also like