You Try!: "Enter The Second Number:" "Enter The Second Number:"
You Try!: "Enter The Second Number:" "Enter The Second Number:"
2. Why use the line continuation character if the Python Interpreter does not care
how long a line is?
We do this for the code to be easily read and debugged,as it can appear all at once on the
editor without needing to move a lot to finish reading the line.
3. Write a program (using the Mu editor) that requests the user to enter two
numbers and prints the sum, product, difference and quotient of the two numbers.
print()
print("The sum of them is ",num_1+num_2)#add them and output
the result
print("The product of them is ",num_1*num_2)#multiply instead
this time
print("The difference between them is ",num_1-num_2)#subtract
to find difference
print("By dividing the first over the second,their quotient
is ",num_1/num_2)#divide for quotient
4. Predict the output of the following statements, then check using Python:
■ print( 6 + 3 * 5 – 1) 20
■ print( 2 * -2 ) -4
■ print( 9 / 2 ) 4.5
■ print( 9 // 2 ) 4
■ print( 9 // 2.0 ) 4.0
■ print( 9 % 2 ) 1
■ print( 7 + 3 * 6 / 2 – 1 ) 15.0
■ print( (6 + 2) * 3 ) 24
■ print( 14 // (7 – 1) ) 2
■ print( 2 % 2 + 2 * 2 – 2 / 2 ) 3.0
■ print( ( 2 ** 3 * ( 4 + ( 5 * 6 / ( 10 ) ) ) ) ) 56.0
■ print( float(2) + int(3.6) ) 5.0
7. Write a program that reads in the radius of a circle and displays the circle's
diameter (2r), circumference (πd) and area (πr^2). Use the constant value
3.14159265 for π. Do these calculations in print statements, and round to 2
decimal places.
8. Write a program that accepts a binary number from the user (as a string) and
converts it to decimal (base-10). Hint: type help(int) in the Python shell to learn
other features of the int() function.
#class int(object)
# | int([x]) -> integer
# | int(x, base=10) -> integer
# |Base 0 means to interpret the base from the string as an
#integer literal.
# | >>> int('0b100', base=0)
decimal = int(num,base=0)
print(decimal)
Here's a sample run, assuming the user types 17, 13, and Ms. Tajalli the
Computer Teacher as input:
Enter the name of the quest leader: Ms. Tajalli the Computer
Teacher
Only 4 survived.
The party was about to give up when they stumbled upon the buried
They split the loot and Ms. Tajalli the Computer Teacher kept the
extra 2 gold pieces.
story = []#this list will have all the lines of the story for
easier storing of each line on its own with proper spacing
story.append(leader + ' led ' + explorers + ' adventurers on
a quest for gold.')
story.append('The group fought a band of ogres and lost ' +
lost + ' members.Only ' + str(int(explorers) - int(lost)) + '
survived.')#changed explorers and lost to integers to be able
to calculate the difference and turned the difference to a
string after
story.append('The party was about to give up when they
stumbled upon the buried fortune of 750 gold pieces.' )
story.append('They split the loot and ' + leader + ' kept the
extra ' + str(750%(int(explorers) - int(lost))) + ' gold
pieces.')#same as line 10 # used % to know the reminder that
the leader will keep