1
+ [ Contents] ( ../Contents ) \| [ Previous (1.1 Python)] ( 01_Python ) \| [ Next (1.3 Numbers)] ( 03_Numbers )
2
+
1
3
# 1.2 A First Program
2
4
3
5
This section discusses the creation of your first program, running the
@@ -46,19 +48,24 @@ hello world
46
48
>> >
47
49
```
48
50
49
- This so-called * read-eval* loop is very useful for debugging and exploration.
51
+ This so-called * read-eval-print-loop * (or REPL) is very useful for debugging and exploration.
50
52
51
- Let's take a closer look at the elements:
53
+ ** STOP** : If you can't figure out how to interact with Python, stop what you're doing
54
+ and figure out how to do it. If you're using an IDE, it might be hidden behind a
55
+ menu option or other window. Many parts of this course assume that you can
56
+ interact with the interpreter.
57
+
58
+ Let's take a closer look at the elements of the REPL:
52
59
53
60
- ` >>> ` is the interpreter prompt for starting a new statement.
54
61
- ` ... ` is the interpreter prompt for continuing a statements. Enter a blank line to finish typing and run the statements.
55
62
56
- The ` ... ` prompt may or may not be shown depending on how you are using Python. For this course,
63
+ The ` ... ` prompt may or may not be shown depending on your environment. For this course,
57
64
it is shown as blanks to make it easier to cut/paste code samples.
58
65
59
66
The underscore ` _ ` holds the last result.
60
67
61
- ``` pycon
68
+ ``` python
62
69
>> > 37 * 42
63
70
1554
64
71
>> > _ * 2
@@ -94,7 +101,7 @@ bash %
94
101
95
102
Or from the Windows shell:
96
103
97
- ``` shell
104
+ ```
98
105
C:\SomeFolder>hello.py
99
106
hello world
100
107
@@ -103,6 +110,8 @@ hello world
103
110
```
104
111
105
112
Note: On Windows, you may need to specify a full path to the Python interpreter such as ` c:\python36\python ` .
113
+ However, if Python is installed in its usual way, you might be able to just type the name of the program
114
+ such as ` hello.py ` .
106
115
107
116
### A Sample Program
108
117
@@ -134,7 +143,8 @@ print('Final height', num_bills * bill_thickness)
134
143
When you run it, you get the following output:
135
144
136
145
``` bash
137
- bash % python3 sears.py 1 1 0.00011
146
+ bash % python3 sears.py
147
+ 1 1 0.00011
138
148
2 2 0.00022
139
149
3 4 0.00044
140
150
4 8 0.00088
@@ -197,6 +207,9 @@ height = 442 # Floating point
197
207
height = ' Really tall' # A string
198
208
```
199
209
210
+ Python is dynamically typed. The perceived "type" of a variable might change
211
+ as a program executes depending on the current value assigned to it.
212
+
200
213
### Case Sensitivity
201
214
202
215
Python is case sensitive. Upper and lower-case letters are considered different letters.
@@ -228,11 +241,11 @@ while num_bills * bill_thickness < sears_height:
228
241
print (' Number of days' , days)
229
242
```
230
243
231
- The statements below the ` while ` will execute as long as the expression after the ` while ` is ` true ` .
244
+ The statements indented below the ` while ` will execute as long as the expression after the ` while ` is ` true ` .
232
245
233
246
### Indentation
234
247
235
- Indentation in Python is used to denote a set of statements that go together.
248
+ Indentation is used to denote groups of statements that go together.
236
249
Consider the previous example:
237
250
238
251
``` python
@@ -244,17 +257,16 @@ while num_bills * bill_thickness < sears_height:
244
257
print (' Number of days' , days)
245
258
```
246
259
247
- Indentation groups the following statements together as the operations that
248
- execute repeatedly:
260
+ Indentation groups the following statements together as the operations that repeat:
249
261
250
262
``` python
251
263
print (day, num_bills, num_bills * bill_thickness)
252
264
day = day + 1
253
265
num_bills = num_bills * 2
254
266
```
255
267
256
- Because the ` print() ` statement at the end is not indented, it means
257
- that it does not belong to the loop. The empty line is just for
268
+ Because the ` print() ` statement at the end is not indented, it
269
+ does not belong to the loop. The empty line is just for
258
270
readability. It does not affect the execution.
259
271
260
272
### Indentation best practices
@@ -286,8 +298,6 @@ else:
286
298
print (' Computer says yes' )
287
299
```
288
300
289
- Depending on the values of ` a ` and ` b ` , the execution will jump to ` print('Computer says no') ` or ` print('Computer says yes') ` .
290
-
291
301
You can check for multiple conditions by adding extra checks using ` elif ` .
292
302
293
303
``` python
@@ -324,7 +334,7 @@ name = 'Jake'
324
334
print (' My name is' , name) # Print the text 'My name is Jake'
325
335
```
326
336
327
- ` print() ` always puts a new line at the end.
337
+ ` print() ` always puts a newline at the end.
328
338
329
339
``` python
330
340
print (' Hello' )
@@ -364,7 +374,7 @@ print('Your name is', name)
364
374
This is useful for small programs, learning exercises or simple debugging.
365
375
It is not widely used for real programs.
366
376
367
- ### ` pass ` statement
377
+ ### pass statement
368
378
369
379
Sometimes you need to specify an empty code block. The keyword ` pass ` is used for it.
370
380
@@ -379,10 +389,19 @@ This is also called a "no-op" statement. It does nothing. It serves as a placeho
379
389
380
390
## Exercises
381
391
392
+ This is the first set of exercises where you need to create Python
393
+ files and run them. From this point forward, it is assumed that you
394
+ are editing files in the ` practical-python/Work/ ` directory. To help
395
+ you locate the proper place, a number of empty starter files have
396
+ been created with the appropriate filenames. Look for the file
397
+ ` Work/bounce.py ` that's used in the first exercise.
398
+
382
399
### Exercise 1.5: The Bouncing Ball
383
400
384
- A rubber ball is dropped from a height of 100 meters and each time it hits the ground, it bounces back up to 3/5 the height it fell.
385
- Write a program "bounce.py" that prints a table showing the height of the first 10 bounces.
401
+ A rubber ball is dropped from a height of 100 meters and each time it
402
+ hits the ground, it bounces back up to 3/5 the height it fell. Write
403
+ a program ` bounce.py ` that prints a table showing the height of the
404
+ first 10 bounces.
386
405
387
406
Your program should make a table that looks something like this:
388
407
0 commit comments