Python Programming Exercises 1 Notes: throughout these exercises >>> preceeds code that should be typed directly into the Python interpreter. To get the most out of these exercises, don t just follow them to the letter, but experiment and be curious about the results you get. 1. Open a console and create a new directory for these exercises called ex1 (or whatever you want to call it). We will save all of our files from this session in the same directory. You should do this at the start of each practical session. mkdir ex1 cd ex1 Start the Python interpreter. This is what my Mac prints to the console when I start python3: python3 Python 3.4.2 (default, Feb 6 2015, 12:06:24) [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/apple/clang-421.0.60))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> Everything in this course assumes you are using Python 3! As you can see, I am running version 3.4.2. If you are not running at least Python version 3.4, then either refer to the installation guide or ask one of the demonstrators for help. 2. The Python interpreter can be exited in two different ways: typing Ctrl-D (this is the EOF character) or typing quit() and pressing enter. Try both. 1
3. If you input a value to the Python interpreter, it will echo it to the console. Try a few examples of integers and floating-point numbers both in decimal and scientific notation: 1 >>> 10 2 10 3 >>> 0.001 4 0.001 5 >>> 1e-3 6 0.001 4. All values have a type. In these exercises we will focus on int (integer), float (floating-point) and str (character string) data types. The function type( ) will tell you the type of a value: 1 >>> type(0.001) Try it on a few of the values you used from the previous question. You do not need to type out everything all over again. Use the arrow keys to step through previous lines (up and down arrows) and move the cursor (left and right arrows). 5. Python assumes that numbers without a decimal place (e.g. 17) are integers and numbers with a decimal place (e.g. 17.0 or 17.000001) are floating-point numbers. You can force a variable to be the type you want by being explicit with int( ) and float( ). 1 >>> type(17) 2 >>> type(float(17)) Do the same by explicitly converting a float to an int. When we convert a float to an int, does the number get rounded up or down? 6. Strings are sequences of alphanumeric characters enclosed in double (e.g. hello ) or single (e.g. hello ) quotation marks. Strings containing numbers can be converted to integers and floats using int( ) and float( ) as well: 2
1 >>> "17" 2 >>> type("17") 3 >>> float("17") 4 >>> type(float("17")) Play around with converting strings to ints and floats. This is important because input from the user will often be in the form of a string, but needs to be converted to a numeric type to be used in calculations. 7. Python supports arithmetic operators for addition (+), subtraction (-), multiplication (*) and division (/). In the expression a + b, a and b are refered to as the operands (a fancy word for arguments) of the operator +. Try out some examples using ints with different operators, e.g. 1 >>> 10 * 3 Note that the type returned from division is not the same as the others: 1 >>> 10 / 3 8. Redo the above examples, but this time use different types as operands and observe the types of the returned values, i.e. int + float and float + float. Inspect the result with type( ) if necessary: 1 >>> type(10 * 3.0) 9. Python additionally supports modulus (%), exponentiation (**) and floor division (//). Test these operators out to ensure you understand what they do: 1 >>> 10 % 3 2 >>> 10 ** 3 3 >>> 10 // 3 3
10. Write an expression to calculate the following: x 2 + 3x + 1. Hint: you will need to declare a variable called x first, e.g. 1 >>> x = 5 11. Write an expression to calculate the following: 1 3x 2 +1. 12. You almost know enough to use Python as a scientific calculator. The missing pieces are useful constants like π and e and functions for roots, trigonometry, etc. Mathematical functions are declared in the math module: 1 >>> import math 2 >>> math.pi 3 >>> math.sqrt(16) Declare a variable called radius: 1 >>> radius = 3 and write an expression to calculate the volume of a sphere: 4 3 πr3. 13. Create a new file called helloworld.py and type into it the hello world example from the lecture slides: 1 # example hello world program 2 message = "hello world" 3 print(message) Run the program to ensure you have typed it in correctly: 4
python3 helloworld.py 14. The print function can accept multiple arguments. Change your program to be like the following and run it again: 1 # example hello world program 2 message1 = "hello" 3 message2 = "world" 4 print(message1, message2) Note that spaces are printed between each variable passed to print. 15. The input() function can be used to capture text typed by the user. input() can optionally take a string argument to give the user instructions (normally called a prompt). Take the program above and replace the value of the variable called message2 with a call to input with the argument Enter your name:. When you run the script it should read: python3 helloworld.py Enter your name: Alan hello Alan 16. Take your expression for the volume of a sphere and write a program that prints out the following: The volume of a sphere with radius 1.0 is 4.188790204786390 Change the value of the radius manually and rerun the program. 17. Change the script you just wrote to prompt the user for the radius. 5