-
Notifications
You must be signed in to change notification settings - Fork 0
/
python1.qmd
executable file
·434 lines (303 loc) · 12.3 KB
/
python1.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
---
title: Python Unit 1
execute:
enabled: false
---
## How to run Python
You will use Python in two different modes:
Interactive mode
: When on the shell, start Python by entering
```bash
python
```
This will display a message similar to
```
Python 3.10.2 (main, Jan 15 2022, 19:56:27)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license"
for more information.
>>>
```
and allow you to enter Python code next to the signs `>>>` line by line. Each line of code will be executed as soon as you press Enter. For example:
```python
print("Hello from the interactive shell")
```
Script mode
: Write Python commands into a text file (e.g., by using nano to create a file `my_program.py`). If you are still in the interactive mode: exit the python console by typing `exit()` or the key combination `Ctrl-D` (`Ctrl` is `Strg` on a German keyboard), then open and edit the file in a text editor such as nano.
```bash
nano my_program.py
```
Then enter python code into the file.
```python
print("Hello from the python script")
```
Then, exit nano to get back to the bash shell and pass the name of this file to the `python` command as argument:
```bash
python my_program.py
```
This makes Python execute the code stored in the file.
## Data types and operators
To use Python, please connect to Corso via `ssh`; on this virtual machine, all required programs have been installed.
Execute the following statements in Python's interactive mode. Try to understand the result of each statement. If you encounter any difficulties, please do not hesitate to contact your course instructor.
### Numeric types
Numeric types represent numbers.
```python
42 # the integer number forty-two
3.1415 # a decimal number (float) that approximates pi
```
The hash sign `#` starts a *comment*, so Python will ignore all remaining characters on the line. (Thus, you may omit all comments when you enter the following code snippets into the Python interpreter!).
Numbers may be manipulated via *arithmetic operators*:
```python
1 + 2 # addition
7 - 4 # subtraction
5 * 6 # multiplication
5 ** 2 # exponentiation ("5 to the power of 2")
54 / 7 # division
10 % 4 # modulo – returns remainder
```
The *assignment operator* assigns a name to a value:
```python
a = 343
b = 14
```
Now, the name (also called *variable*) may be used instead of the value in any statement:
```python
a
a + b
```
Variable names may comprise an arbitrary number of letters, digits, and underscores. However, a name must not start with a digit, and certain keywords (e.g., `if` and `for`) are disallowed as names.
```python
x = 1 # ok
first_value = 1 # ok
number5 = 1 # ok
2much = 1 # syntax error – name starts with a digit
for = 1 # syntax error – for is a keyword
```
Let's calculate the circumference and area of a circle with radius 5:
```python
# do the math
pi = 3.1415792
radius = 5
circumference = 2 * radius * pi
area = (radius ** 2) * pi
# print results
circumference
area
```
We added explicit parentheses around `radius ** 2` to indicate that Python should first calculate the power and then the product. (Actually, we could have omitted these parentheses, since the precedence of exponentiation is higher than the one of multiplication.) However, parentheses are required if the default *operator precedence* should be changed.
```python
3 * 5 + 8 # equivalent to (3 * 5) + 8
3 * (5 + 8)
```
:::{.callout-warning}
When in doubt, always use parentheses to structure your calculations!
:::
Moreover, never distract from the actual order of calculations by using spaces like
```python
3 * 5+8 # might imply that 5 and 8 are added first
```
### Boolean type
There are only two Boolean values: `True` and `False`. Boolean types typically arise when using *comparison operators*:
```python
3 > 2 # greater than
7 < 4 # less than
10 >= 8 # greater than or equal
2 <= 2 # less than or equal
5 == 5 # equality
10 != 9 # inequality
```
*Logical operators* connect Boolean values to obtain new Booleans.
```python
(2 < 3) and (15 / 4 > 1) # conjunction, true if both operands are true
(5 != 5) or (12 >= 13) # disjunction, true if at least one operand is true
not (5 < 7) # negation changes true to false and vice versa
```
### Text type
A *string* is a sequence of Unicode characters enclosed in single or double quotes.
```python
str_a = "a string"
str_b = 'also a string'
str_c = "a string with 'single' quotes"
```
You may also use multiline strings, which must be enclosed in triple quotes.
```python
'''a
multi-
line
string'''
```
String *concatenation* is done via the `+` operator:
```python
str_a + str_b
```
Since strings are sequences of characters, you can access a single character within a string by *indexing*.
```python
enzyme = "adenylyl cyclase"
enzyme[0] # first character
enzyme[3] # fourth character
enzyme[-2] # second character counting from the end of the string
```
::: {.callout-warning}
Python uses *zero-based indexing*, i.e., the first element has index `0`.
:::
*Slicing* extracts a substring, i.e., several characters from a string:
```python
enzyme[2:8] # substring from the character at index 2
# (i.e., the 3rd character from the left)
# to the character before (!) index 8
# (i.e., the 8th character from the left)
```
When using the slicing syntax `[start index : end index]`, the character at the start index is included, while the character at the end index is *excluded* (!).
Both start index and end index are optional:
```python
enzyme[:4] # substring up to index 3 (remember: the character
# at the end index is NOT included!)
enzyme[9:] # substring starting at index 9
enzyme[:] # a copy of the whole string
enzyme[-3:] # substring starting at index 3 counting from the right
```
### `None` type
Python also knows a `None` type, which explicitly denotes the “nothing”:
```python
nothing = None
nothing
```
The `is` operator checks whether a variable is `None` (do not use a comparison operator in this case):
```python
nothing is None # preferred
nothing == None # avoid
```
## Input and output
Use the `print()` function to display output on the screen.
```python
print("Hello world!")
print(3 + 5)
```
By contrast, the `input()` function reads values supplied by the user:
```python
value = input("Please enter any value: ")
value
```
After calling `input()`, Python reads an arbitrary number of characters until the user presses Enter.
Note that `input()` always reads a string. Thus, the following program will not work as expected:
```python
number = input("Please enter a number: ")
print(number + 10) # a string cannot be added to an integer!
```
If we plan to use the entered value for calculations, we must *convert* it to a numeric type via `int()` or `float()`:
```python
x = "10" # x contains the string "10"
y = int(x) # convert the string "10" to an integer 10
y * 2 # this works as expected
a = float("2.3") # a is now the float 2.3
a / 5.6 # this works as expected
```
Note, however, that you can apparently “multiply” a string with an integer. In this case, Python returns a new string that contains the original string n times (where n is the value of the integer).
```python
x * 2 # what happens here?!
"abc" * 3 # another example
```
::: {.callout-note #example-input}
#### Example
The following program asks the user to enter two numbers and then calculates their product. Store these statements in the file `product.py` and execute this file with Python (`python product.py`)!
```python
print("Calculate the product of two numbers")
a = input("First number: ")
a = float(a)
b = input("Second number: ")
b = float(b)
print("The result is", a * b)
```
:::
## Command line arguments
A Python program may also process values supplied on the command line (similar to a shell command). To enable this functionality, we have to import the `sys` module (unit 2 explains modules in more detail). We create a Python script `arguments.py` containing the following lines:
```python
from sys import argv
print(argv)
print(argv[0])
print(argv[1:])
```
We then execute the script as follows:
```bash
python arguments.py a 12 third_argument
```
The program prints three lines that tell us:
1. `argv` is a *list* with four elements. (The list type will be treated in unit 2. For now, you only need to know that you may access the elements of a list like characters in a string: Variable name followed by the index in brackets.)
2. The first element `argv[0]` contains the name of the script that was called (`'arguments.py'`).
3. The remaining elements `argv[1:]` contain the command line arguments in the given order.
::: {.callout-note #example-argv}
#### Example
By using command line arguments, we may calculate the product of two numbers as shown below:
```python
from sys import argv
a = float(argv[1])
b = float(argv[2])
print("The result is", a * b)
```
Store these statements in `product2.py` and execute the program:
```bash
python product2.py 35 10
```
:::
## Exercises
Each unit concludes with several exercises which you should solve on your own. Your solutions will be graded, and the scores will be used to obtain your final grade for the course. The maximum number of points for the correct solution of each exercise is indicated.
Store all files that you generate for unit 1 in the folder `python1` in your home directory.
### Exercise 1.1 (3 P)
You have successfully solved this exercise as soon as you have worked through this unit. In particular, the folder `python1` must contain the following files, which you have created in the course of this unit:
- `product.py`
- `arguments.py`
- `product2.py`
### Exercise 1.2 (2 P)
Write a Python program that executes the following statements consecutively. Store the program in `exercise_1_2.py`.
1. Store the integer 32 in the variable `z` and the float 2.5 in the variable `a`.
2. Store the product of `z` and `a` in the variable `b`.
3. Divide `a` by 8 and store the result in `a`.
4. Print whether `a` is greater than `b` (here, the program should print a single Boolean value.)
5. Print whether the sum of `z` and 11 is unequal to 44 (again, only print a single Boolean value).
:::{.callout-tip collapse="true"}
See section [Data types and operators](#data-types-and-operators).
:::
### Exercise 1.3 (2 P)
Write a program `exercise_1_3.py` that prompts the user to enter three numbers and then displays whether the division of the first number by the second number yields a remainder that is equal to the third number.
An exemplary run of the program may yield the following output:
```default
$ python exercise_1_3.py
dividend: 10
divisor: 4
remainder: 2
True
```
The user entered the three values 10, 4, and 2; subsequently, the program printed `True`.
A different run of the program may look like
```default
$ python exercise_1_3.py
dividend: 835
divisor: 111
remainder: 20
False
```
It does not matter whether your program prints anything in the input lines (such as “dividend:” above). However, it is important that the last printed line contains a single Boolean value that has been calculated correctly.
:::{.callout-tip collapse="true"}
See [this example](#example-input).
:::
### Exercise 1.4 (3 P)
Write a program `exercise_1_4.py` that reads four command line arguments. (Below, these arguments are called `dna`, `x`, `a`, `e`.). The first argument is a string of arbitrary length which represents a DNA sequence. Arguments 2 to 4 are integers. The program should print two lines:
- The first line contains the `x`-th base from the left in `dna`.
- The second line contains the subsequence of `dna` that starts two bases before the position given by `a` and ends at the position given by `e`.
Bases are numbered starting from one (thus, the first base in the sequence is actually numbered “1”).
You may check that your program works correctly by using the following exemplary calls:
```default
x a e
↓ ↓ ↓
$ python exercise_1_4.py AGCTATAGTAATCCAAT 2 5 9
G
CTATAGT
a x e
↓ ↓ ↓
$ python exercise_1_4.py ATCTACGCGATATCGCGATAGCCGATGCTGACGACTGACTTGACG 13 7 20
T
ACGCGATATCGCGATA
```
:::{.callout-tip collapse="true"}
See [this example](#example-argv) and the section on [string indexing and slicing](#text-type).
:::