Introduction to Python

davidbpython.com




Project Warmup Exercises, Session 1



PLEASE NOTE: EXERCISES ARE NOT TO BE TURNED IN. FOR HOMEWORK TO BE TURNED IN, PLEASE SEE 'PROJECTS'

 
Ex. 1.1 Numeric addition. Assign two new integer objects and one float object to three variable names. Sum up the three variables and assign the resulting object to a new variable name. Indicate (by printing) the value and type of this resulting object.
 
Ex. 1.2 Numeric multiplication. Assign two new integer objects to two variable names. Multiply the two variables and assign the resulting object to a new variable name. Indicate (by printing) the value and type of this resulting variable's object.
 
Ex. 1.3 Use strings as numbers. Starting with the sample code, sum up the values to produce the integer value 15. (Hint: apply a conversion function to each string so it can be used as a number.)
var = '5'
var2 = '10'
Expected Output:
15
 
Ex. 1.4 Use input values as numbers. Take user input for an integer and print that value doubled.
Sample Output:
Please enter an integer:  5
5 doubled is 10.
 
Ex. 1.5 Use input as strings. Take user input for a 'place' and then greet the place enthusiastically.
Sample program runs:
Please enter a place name:  Hawaii
Hello, Hawaii!
Please enter a place name:  Kathmandu
Hello, Kathmandu!
 
Ex. 1.6 String repetition. Take user input for an integer and apply that many exclamation points to the phrase, "Hello, world!" (Hint: use the "string repetition" operator)
Sample program runs:
Please enter an integer:  2
Hello, World!!
Please enter an integer:  11
Hello, World!!!!!!!!!!!
 
Ex. 1.7 Round a number to integer. Assign the float value 35.30 to a variable, then round the value to 35.
Expected Output:
35
 
Ex. 1.8 Round a number to float. Assign the float value 35.359958 to a variable, then round the value to two decimal places.
Expected Output:
35.36
 
Ex. 1.9 Use strings in numeric division. Starting with the following code (make sure it is included exactly as written), divide the first number by the second number.
var = "5"
var2 = "4"
Expected Output:
1.25
 
[pr]