Exercise
Question 1
Use float() to create a number from a string like '12.34'. Make sure the result is really a
number!
In [2]: stringy = '12.34'
floaty = float(stringy)
print("Successfully converted string to float - "+stringy)
print(type(floaty))
Successfully converted string to float - 12.34
<class 'float'>
In [1]: ​
--------------------------------------------------------------------------NameError
Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2112/3919013186.py in <module>
----> 1 print(type(floaty))
NameError: name 'floaty' is not defined
In [ ]: ​
In [ ]: ​
In [ ]: ​
Question 2
Try using int() to create an integer from a decimal number like
rounded up or down?
56.78. Did the answer get
In [10]: int integer = 56.78
File "C:\Users\khatr\AppData\Local\Temp/ipykernel_2112/1612767925.py", line
1
int integer = 56.78
^
SyntaxError: invalid syntax
In [9]: print(integer)
56.78
Number is rounded down.
Question 3
Try using int() to create an integer from a string. Make sure the result is really an integer!
In [4]: stringy = '50'
integer = int(stringy)
print(integer)
print(type(integer))
50
<class 'int'>
In [ ]: ​
In [ ]: ​
In [ ]: ​
In [ ]: ​
In [ ]: ​
Question 4
Which of the following are valid string literals in Python.
(a) "Hello"
(b) 'hello'
(c) "Hello'
(d) 'Hello there'
In [ ]: The following are valid string literals
In [ ]: (a) "Hello"
In [ ]: (b) 'hello'
In [ ]: (d) 'Hello there'
In [ ]: (e) ''
Answer: Except c) all are valid string literals.
Question 5
Write a Python program that prompts the user for two floating-point values and displays
the result of the first number divided by the second, in integer only.
In [ ]: # Prompt the user for input
num1 = float(input("Enter the first floating-point number: "))
num2 = float(input("Enter the second floating-point number: "))
​
# Calculate the result as an integer
result = int(num1 / num2)
​
# Display the result
print("The integer result of {num1} / {num2} is {result}")
Question 6
Losing Your Head over Chess
The game of chess is generally believed to have been invented in India in the sixth century for a
ruling king by one of his subjects. The king was supposedly very delighted with the game and
asked the subject what he wanted in return. The subject, being clever, asked for one grain of
wheat on the first square, two grains of wheat on the second square, four grains of wheat on
the third square, and so forth, doubling the amount on each next square. The king thought that
this was a modest reward for such an invention. However, the total amount of wheat would
have been more than 1,000 times the current world production.
Develop and test a Python program that calculates how much wheat this would be in
pounds, using the fact that a grain of wheat weighs approximately
of a pound.
1/7,000
In [5]: # Constants
GRAIN_WEIGHT_POUND = 1 / 7000 # Weight of a grain of wheat in pounds
​
def calculate_wheat_weight():
total_wheat = 0
grains_on_current_square = 1
​
for square in range(64): # There are 64 squares on a chessboard
total_wheat += grains_on_current_square
grains_on_current_square *= 2
​
total_weight_pounds = total_wheat * GRAIN_WEIGHT_POUND
return total_weight_pounds
​
total_weight = calculate_wheat_weight()
print("The total amount of wheat would be approximately {total_weight} pounds.
The total amount of wheat would be approximately 2635249153387079.0 pounds.
In [ ]: ​