~gasp-lessons-team/gasp-lessons/gasp-lessons

« back to all changes in this revision

Viewing changes to gasp_course/source/1-intro.rst

  • Committer: Jeffrey Elkner
  • Date: 2019-02-15 18:14:56 UTC
  • Revision ID: jeff@elkner.net-20190215181456-z79ghp04i0nniged
Convert Sheets 1 & 2 to Python3

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    >>> 200 * 300
53
53
    60000
54
54
    >>> 12 / 4               # Use / for division.
55
 
    3
56
 
 
57
 
Now, here's a bit of a surprise.
58
 
 
59
 
.. sourcecode:: python
60
 
    
61
 
    >>> 7 / 3
62
 
    2
63
 
 
64
 
You might have expected it to say 2.3333333 or 2 1/3, but in fact the remainder
65
 
just gets thrown away. There are ways of getting a more accurate answer; you'll
66
 
find out about them later.
 
55
    3.0
67
56
 
68
57
Try experimenting some more with using Python as a calculator. Maybe try having
69
58
it do some more complicated things --- experiment with more numbers and bigger
157
146
    >>> 123456 * 3
158
147
    370368
159
148
    >>> 123456 / 6
160
 
    20576
 
149
    20576.0
161
150
    >>> 123456 - 1000
162
151
    122456
163
152
 
176
165
    >>> salary * 4
177
166
    493824
178
167
    >>> salary / 12
179
 
    10288
 
168
    10288.0
180
169
    >>> salary
181
170
    123456
182
171
 
210
199
.. sourcecode:: python
211
200
    
212
201
    >>> for x in 1, 2, 3, 4, 5:
213
 
    ...     print x, x * x   # The prompt changes - Python is expecting more.
 
202
    ...     print(x, x * x)  # The prompt changes - Python is expecting more.
214
203
    ...                      # Just press Enter.
215
204
 
216
205
Can you guess what this will do?
296
285
 
297
286
    .. sourcecode:: python
298
287
    
299
 
        print "Experiment 1:"
 
288
        print("Experiment 1: ")
300
289
        1 + 2
301
 
        print 1 + 2
 
290
        print(1 + 2)
302
291
        "How come this did not print?"
303
 
        print "but this did?"
304
 
        print
305
 
        print
306
 
    
307
 
        print "Experiment 2:"
308
 
        for number in 2, 4, 6:
309
 
            print 2 * number
310
 
    
311
 
        for number in 2, 4, 6:
312
 
            print 3 * number,
 
292
        print("but this did?")
 
293
        print
 
294
        print
 
295
    
 
296
        print("Experiment 2:")
 
297
        for number in 2, 4, 6:
 
298
            print(2 * number)
 
299
    
 
300
        for number in 2, 4, 6:
 
301
            print(3 * number, end="")
313
302
 
314
303
    Run the program and look carefully at its output. What happens when you put
315
304
    an expression on a line without a ``print`` command in a Python script?