Simple statements - Exercises
Now, let me take a break from talking.
Over to you now to get more hands-on experience with some lab exercises.
This is divided into two parts:
- Part A: Involves writing Python commands directly into the terminal
- Part B: Involves writing programs and executing these programs from the terminal
Many thanks to the following people for contributing to these exercises: Joe Stacey, Oana Cocarascu, Josiah Wang
Part A: Writing Python commands directly into the terminal
Using Python in the terminal
I assume that you still have your interactive Python prompt up and running, and have been trying out everything we have discussed so far? 😃
Now, try typing the following lines of Python to see what the terminal will output:
>>> print("Hello, this is the terminal speaking")
>>> 2 * 4
>>> 3 ** 3
>>> round(2.5)
>>> a = 2
>>> a = a + 8
>>> print(a)
Answer the following questions by writing Python directly into the terminal:
Ex1: Can you predict the type of the variable ‘a’ (float, int, str, bool) after using the following commands? Which of these lines will return an error message?
You can use type(a)
after typing each line to see the type of the variable.
>>> a = "hello" + " world"
>>> a = "My age is " + 22
>>> a = 4 + 2
>>> a = 12/5
>>> a = "hello world " * 4
>>> a = 12/4
>>> a = (25 == 5*5)
>>> a = (25 = 5*5)
Part B: Writing programs and executing these programs from the terminal
Introduction to writing programs using the terminal
If you exit python3 by typing exit(), you can create new files from your terminal, including ‘.py’ files where you can write your own Python scripts.
You may use any text editor. If you are planning to connect remotely to the lab machines a lot, you can consider a command-line text-based editor called VIM. In VIM, everything is done via the keyboard.
If you need a tutorial on VIM, you can download a quick one-page tutorial from last year’s session (Page 8). I aslo recommend the book A Byte of VIM. It does take some time to get used to using VIM!
- Create a new file called python_script.py by typing:
vim python_script.py
- Go into insert mode: by typing
i
- Write your Python script within this file, for example
print("This is the terminal speaking once again!")
- Save and close the file (typing
:
beforewq
and then pressing enter) - Now you can run the script by typing
python3 python_script.py
To import variables from the terminal you can do the following:
- Add the following lines of code to your Python script: ‘import sys’, then ‘var1 = sys.argv[1]’ and ‘var2 = sys.argv[2]’
- To test your code is accepting these variables, try adding print statements: ‘print(var1)’ and ‘print(var2)’
- Then when you execute the script from the terminal, you can type: python python_script.py my_var1 my_var2
Ex2: Write a program that computes the values of a+aa+aaa for a given integer digit a
- For example for the digit 3 this would be
3+33+333 = 369
Ex3: Can you write a program that achieves this by concatenating strings?
- For example using the fact that
'a' + 'b' = 'ab'
Ex4: Write a program which uses Heron’s Formula, and defines the length of the three sides of a triangle, computes its area before printing it.
Heron’s formula is:
\[Area =\ \sqrt{p(p-a)(p-b)(p-c)}\] \[p = \frac{(a + b + c)}{2}\]You can use Python’s math.sqrt()
function for computing the square root.
import math
math.sqrt(9)