Reference no: EM133091466
Introduction to Python, Operators and Data Types
Exercise 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, Pure Interpretation and Hybrid Implementation. In your own words, describe each of these different techniques and also rank them from fastest to slowest.
Exercise 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!
Exercise 3. Give at least two examples of each of the following types of data:
Data Type
|
Example 1
|
Example 2
|
Integer
|
|
|
Boolean
|
|
|
Float
|
|
|
Double
|
|
|
String
|
|
|
Exercise 4. Provide the operator symbol for each of the following operators:
Operation
|
Symbol
|
Addition
|
|
Subtraction
|
|
Multiplication
|
|
Division (floating point)
|
|
Division (integer)
|
|
Modulus
|
|
Raise to a power
|
|
Equivalence
|
|
Not equal to
|
|
Assignment
|
|
Exercise 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:
i = int(3)
f = float(4.0)
s = "five"
b = True
i_times_f = i * f
print(type(i_times_f), i_times_f) # float
First data type
|
Operation
|
Second data type
|
Resulting data type
|
Integer
|
Multiplied by
|
Float
|
Float
|
Integer
|
Multiplied by
|
Integer
|
|
Integer
|
Divided by
|
Integer
|
|
Integer
|
Integer-divided by
|
Integer
|
|
Float
|
Integer-divided by
|
Integer
|
|
String
|
Multiplied by
|
Integer
|
|
Integer
|
Multiplied by
|
String
|
|
Float
|
Divided by
|
Float
|
|
Float
|
Integer-divided by
|
Float
|
|
String
|
Added to
|
String
|
|
String
|
Subtracted from
|
String
|
|
Exercise 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.
Exercise 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!
Exercise 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...
Whatever you do, don't type the lot in - it's a waste of your time and energy! Instead, use a loop (take a look at the range statement
You can cheat for the last verse if you like by putting it after the loop, or... you could use an if / else comparison if you go look ‘em up.
If you wanted the song to last a bit longer than the split-second it'll take for Python to output the entire lyrics, then put the following as the first line of your program:
import time
Then, somewhere in your loop add the following line so that it waits half a second between each iteration:
time.sleep(0.5)
It's quite possible to complete this entire task including the sleep-delay in less than 10 lines of code- so make Jay-Z proud!