Reference no: EM133087548
Introduction to Python, Operators and Data Types
Exercises
Question 1: There are three main ways in which high-level source code can be converted into machine code suitable for running on a processor - Compilation, PureInterpretation and Hybrid Implementation. In your own words, describe each of these different techniques and also rank them from fastest to slowest.
Question 2: Which of the above techniques does Python 3 use? Why do you think this might be? Do a spot of research if you're not sure!
Question 3: Give at least two examples of each of the following types of data:
Data Type
Integer
Boolean
Float
Double
String
Question 4: Provide the operator symbol for each of the following operators:
Operation
Addition
Subtraction
Multiplication
Division (floating point)
Division (integer)
Modulus
Raise to a power
Equivalence
Not equal to
Assignment
Question 5: What is the resulting data type when we combine the following data types in the following ways.Try them out in the Python interpreter if you're not sure (some of them might surprise you), like this for example:
Question 6: What data type is returned when we use Python 3's input function? Assuming the user entered a whole number, give an example of casting this data type into an integer.
Question 7: Write a small Python 3 script that asks the user to enter their birthday as three separate values (day, monthand year) and then prints out how old they are in the following format (for example):
If you were born on 18/01/1978 then that was day 18 of the year.
Use the format function to substitute values into your output. You can use the following snippet of code to calculate the day of the year:
import datetime as dt
date = dt.date(birth_year, birth_month, birth_day) # Create a date from user data
day_of_year = date.timetuple().tm_yday # Get day number from date
This code assumes that when you got the user's birthday details you stored them in variables called birth_day, birth_month and birth_year - if you didn't then either do so or substitute your own variable names into the date line (i.e. the middle line) above!
Question 8: Challenge Task - try this if you're feeling adventurous! You won't get any help with it, it's just to ‘stretch' you a little bit if you're finding all this too easy!
Write a program that outputs the lyrics to the classic love-song 99 Bottles of Beer On The Wall - using a loop!
The song's lyrics are as follows:
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall...
The same verse is repeated, each time with one bottle fewer, until there are none left. On the very last verse when there are zero bottles left, the lyrics change to:
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall...