1+ [ Contents] ( ../Contents ) \| [ Previous (1.1 Python)] ( 01_Python ) \| [ Next (1.3 Numbers)] ( 03_Numbers )
2+
13# 1.2 A First Program
24
35This section discusses the creation of your first program, running the
@@ -46,19 +48,24 @@ hello world
4648>> >
4749```
4850
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.
5052
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:
5259
5360- ` >>> ` is the interpreter prompt for starting a new statement.
5461- ` ... ` is the interpreter prompt for continuing a statements. Enter a blank line to finish typing and run the statements.
5562
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,
5764it is shown as blanks to make it easier to cut/paste code samples.
5865
5966The underscore ` _ ` holds the last result.
6067
61- ``` pycon
68+ ``` python
6269>> > 37 * 42
63701554
6471>> > _ * 2
@@ -94,7 +101,7 @@ bash %
94101
95102Or from the Windows shell:
96103
97- ``` shell
104+ ```
98105C:\SomeFolder>hello.py
99106hello world
100107
@@ -103,6 +110,8 @@ hello world
103110```
104111
105112Note: 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 ` .
106115
107116### A Sample Program
108117
@@ -134,7 +143,8 @@ print('Final height', num_bills * bill_thickness)
134143When you run it, you get the following output:
135144
136145``` bash
137- bash % python3 sears.py 1 1 0.00011
146+ bash % python3 sears.py
147+ 1 1 0.00011
1381482 2 0.00022
1391493 4 0.00044
1401504 8 0.00088
@@ -197,6 +207,9 @@ height = 442 # Floating point
197207height = ' Really tall' # A string
198208```
199209
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+
200213### Case Sensitivity
201214
202215Python is case sensitive. Upper and lower-case letters are considered different letters.
@@ -228,11 +241,11 @@ while num_bills * bill_thickness < sears_height:
228241print (' Number of days' , days)
229242```
230243
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 ` .
232245
233246### Indentation
234247
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.
236249Consider the previous example:
237250
238251``` python
@@ -244,17 +257,16 @@ while num_bills * bill_thickness < sears_height:
244257print (' Number of days' , days)
245258```
246259
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:
249261
250262``` python
251263 print (day, num_bills, num_bills * bill_thickness)
252264 day = day + 1
253265 num_bills = num_bills * 2
254266```
255267
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
258270readability. It does not affect the execution.
259271
260272### Indentation best practices
@@ -286,8 +298,6 @@ else:
286298 print (' Computer says yes' )
287299```
288300
289- Depending on the values of ` a ` and ` b ` , the execution will jump to ` print('Computer says no') ` or ` print('Computer says yes') ` .
290-
291301You can check for multiple conditions by adding extra checks using ` elif ` .
292302
293303``` python
@@ -324,7 +334,7 @@ name = 'Jake'
324334print (' My name is' , name) # Print the text 'My name is Jake'
325335```
326336
327- ` print() ` always puts a new line at the end.
337+ ` print() ` always puts a newline at the end.
328338
329339``` python
330340print (' Hello' )
@@ -364,7 +374,7 @@ print('Your name is', name)
364374This is useful for small programs, learning exercises or simple debugging.
365375It is not widely used for real programs.
366376
367- ### ` pass ` statement
377+ ### pass statement
368378
369379Sometimes you need to specify an empty code block. The keyword ` pass ` is used for it.
370380
@@ -379,10 +389,19 @@ This is also called a "no-op" statement. It does nothing. It serves as a placeho
379389
380390## Exercises
381391
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+
382399### Exercise 1.5: The Bouncing Ball
383400
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.
386405
387406Your program should make a table that looks something like this:
388407
0 commit comments