-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.py
More file actions
2042 lines (1941 loc) · 90.1 KB
/
basics.py
File metadata and controls
2042 lines (1941 loc) · 90.1 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The Python Interpreter
The Python interpreter is a program that reads Python program statements and executes them immediately. To use the interpreter, you need
to open a terminal window or command prompt on your workstation. The interpreter operates in two modes.1
Interactive Mode
You can use the interpreter as an interactive tool. In interactive mode, you run the Python program and you will see a new prompt, >>>, and you can then enter Python statements one by one. In Microsoft Windows, you might see something like this:
C:\Users\Paul>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> _
The interpreter executes program statements immediately. Interactive mode is really useful when you want to experiment or try things out. For example, sometimes you need to see how a particular function (that you haven’t used before) behaves. On other occasions, you might need to see exactly what a piece of failing code does in isolation.
The >>> prompt can be used to enter one-line commands or code blocks that define
classes or functions (discussed later). Some example commands are shown here:
1 >>> dir(print)
2 ['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__','__name__','__ne__',
'__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__text_signature__']
3 >>>
4 >>> 123.456 + 987.654
5 1111.11
6 >>>
7 >>> 'This'+'is'+'a'+'joined'+'up'+'string.'
8 'Thisisajoinedupstring.'
9 >>>
10 >>> len('Thisisajoinedupstring.')
11 22
The dir() command on line 1 lists all the attributes of an object, helpful if you need to know what you can do with an object type. dir() run without an argument tells you what modules you have available. dir(print) shows a list of all the built-in methods for print(), most of which you’ll never need.
Ifyoutypeanexpressionvalue,asonline4,123.456 + 987.654theinterpreterwill execute the calculation and provide the result. The expression on line 7 joins the strings of characters into one long string. The len() function on line 10 gives you the length of a string in characters.
If you define a new function2 in interactive mode, the interpreter prompts you to complete the definition and will treat a blank line as the end of the function.
1 >>> def addTwoNumbers(a,b):
2 ... result = a + b
3 ... return result
4 ...
5 >>> addTwoNumbers(3,6)
69
7 >>>
Note that when the interpreter expects more code to be supplied in a function, for example, it prints the ellipsis prompt (...). In the case of function definitions, a blank line (see line 4 above) completes the function definition.
We define the function in lines 1 through 3 (note the indentation), and the blank line 4 ends the definition. We call the function on line 5 and add 6 + 3, and the result is (correctly) 9.
One other feature of the interactive interpreter is the help() function. You can use
this to see the documentation of built-in keywords and functions. For example:
>>> help(open)
Help on built-in function open in module io:
open(...)
open(file, mode='r', buffering=-1, encoding=None, errors=None,
newline=None, closefd=True, opener=None) -> file object
... etc. etc.
Command-Line Mode
In command-line mode, the Python program is still run at the command line but you add the name of a file (that contains your program) to the command:
python myprogram.py
The interpreter reads the contents of the file (myprogram.py in this case), scans and validates the code, compiles the program, and then executes the program. If the interpreter encounters a fault in the syntax of your program, it will report a compilation error. If the program fails during execution, you will see a runtime error. If the program executes successfully, you will see the output(s) of the program.
You don’t need to worry about how the interpreter does what it does, but you do need to be familiar with the types of error messages it produces.
We use command-line mode to execute our programs in files.
Coding, Testing and Debugging Python Programs
The normal sequence of steps when creating a new program is as follows:
1. Create a new .py file that will contain the Python program (sometimes called source code).
2. Edit your .py file to create new code (or amend existing code) and save the file.
3. Run your program at the command prompt to test it, and interpret the outcome.
4. If the program does not work as required, or you need to add more features, figure out what changes are required and go to Step 2.
It’s usually a good idea to document your code with comments. This is part of the editing process, Step 2. If you need to make changes to a working program, again, you start at Step 2.
Writing new programs is often called coding. When your programs don’t work properly, getting programs to do exactly what you want them to do is often called debugging.
Comments, Code Blocks, and Indentation
Python, like all programming languages has conventions that we must follow. Some programming languages use punctuation such as braces ({}) and semicolons (;) to structure code blocks. Python is somewhat different (and easier on the eye) because it uses white space and indentation to define code structure.3 Sometimes code needs a little explanation, so we use comments to help readers of the code (including you) understand it.
We introduce indentation and comments with some examples.
#
# some text after hashes
#
brdr = 2 # thick border
def my_func(a, b, c):
d = a + b + c
... ...
if this_var==23:
doThis()
doThat()
... else:
do_other()
...
def addTwoNumbers(a, b):
"adds two numbers"
return a + b
if long_var is True && \
middle==10 && \
small_var is False:
...
...
Any text that appears after a hash character (#) is ignored by the interpreter and treated as a comment. We use comments to provide documentation.
The colon character (:) denotes the end of aheaderlinethatdemarksacodeblock. The statements that follow the header line should be indented.
Colons are most often used at the end of if, elif, else, while, and for statements, and function definitions (that start with the def keyword).
In this example the text in quotes
is a docsctring. This text is what a help(addTwoNumbers) command would display in the interactive interpreter.
The backslash character (\) at the end
of the line indicates that the statement extends onto the next line. Some very long statements might extend over several lines.
3Python 3 disallows mixed spaces and tabs, by the way (unlike version 2).
xxxxxxxxxxxxxx:
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxx:
xxxxxxxxxxx:
All code blocks are indented once a
header line with a colon appears. All the statements in that block must have the same indentation.
Code blocks can be nested within each other, with the same rule: All code in a block has the same indentation.
Indentation is most often achieved using four-space increments.
The semicolon character (;) can be used to join multiple statements in a single line. The first line is equivalent to the two lines that follow it.
a=b +
a=b+ p=q+
xxxxxxx
xxxxxxxxxxx:
xxxx xxxx
c;p=q+ r
c r
Variables
A variable is a named location in the program’s memory that can be used to store some data. There are some rules for naming variables:
• The first character must be a letter or underscore ( _ ).
• Additional characters may be alphanumeric or underscore.
• Names are case-sensitive.
Common Assignment Operations
When you store data in a variable it is called assignment. An assignment statement places a value or the result of an expression into variable(s). The general format of an assignment is:
var = expression
An expression could be a literal, a calculation, a call to a function, or a combination
of all three. Some expressions generate a list of values; for example:
var1, var2, var3 = expression
Here are some more examples:
>>> # 3 into integer myint
>>> myint = 3
>>>
>>> # a string of characters into a string variable
>>> text = 'Some text'
>>> # a floating point number
>>> cost = 3 * 123.45
>>> # a longer string
>>> Name = 'Mr' + ' ' + 'Fred' + ' ' + 'Bloggs'
>>> # a list
>>> shoppingList = ['ham','eggs','mushrooms']
>>> # multiple assignment (a=1, b=2, b=3)
>>> a, b, c = 1, 2, 3
Other Assignment Operations
Augmented assignment provides a slightly shorter notation, where a variable has its value adjusted in some way.
This assignment
x+=1
x-=23
x/=6
x*=2.3
Is equivalent to
x = x + 1
x = x – 23
x = x / 6
x = x * 2.3
Multiple assignment provides a slightly shorter notation, where several variables are given the same value at once.
This assignment Is equivalent to
a=b=c=1 a =1 b =1 c =1
So-called multuple assignment provides a slightly shorter notation, where several variables are given their values at once.
This assignment
x, y, z = 99, 100, 'OK'
p, q, r = myFunc()
Python Keywords
Explanation
Results in:
x=99, y= 100,andz='OK'
IfmyFunc()returnsthreevalues,p,q, and r are assigned those three values.
Like all programming languages, in Python, some words have defined meanings and are reserved for the Python interpreter. You must not use these words as variable names. Note that they are all lowercase.
and
class
elif
finally
if
lambda
print
while
as assert break
continue def del
else except exec
for from global
import in is
not or pass
raise return try
with yield
There are a large number of built-in names that you must not use, except for their intended purpose. The cases of True, False, and None are important. The most common ones are listed here.
True False
all any
dir eval
float format
max min
open print
round set
tuple type
None abs
chr dict
exit file
input int
next object
quit range
str sum
vars zip
To see a list of these built-ins, list the contents of the __builtins__ module in the shell like this:
>>> dir(__builtins__)
Special Identifiers
Python also provides some special identifiers that use underscores. Their name will be of the form:
_xxx
__xxx__
__xxx
Mostly, you can ignore these.4 However, one that you might encounter in your
programming is the special system variable:
__name__
This variable specifies how the module was called. __name__ contains:
• The name of the module if imported.
• The string '__main__' if executed directly.
You often see the following code at the bottom of modules. The interpreter loads your program and runs it if necessary.
if __name__ == '__main__':
main()
Python Modules
Python code is usually stored in text files that are read by the Python interpreter at runtime. Often, programs get so large that it makes sense to split them into smaller ones called modules. One module can be imported into others using the import statement.
import othermod # makes the code in othermod import mymodule # and mymodule available
Typical Program Structure
The same program or module structure appears again and again, so you should try and follow it. In this way, you know what to expect from other programmers and they will know what to expect from you.
#!/usr/bin/python
#
# this module does
# interesting things like
# calculate salaries
#
Used only in Linux/Unix environments (tells the shell where to find the Python program).
Modules should have some explanatory text describing or documenting their behavior.
(continued)
4The only ones you really need to know are the __name__ variable and the __init__() method called when a new object is created. Don’t start your variable names with an underscore and you’ll be fine.
from datetime import datetime
now =datetime.now()
class bookClass(object):
"Book object"
def __init__(self,title):
self.title=title
return
def testbook():
"testing testing..."
title="How to test Py"
book=bookClass(title)
print("Tested the book")
if __name__=='__main__':
testBook()
Module imports come first so their content
can be used later in the module.
Createaglobalvariablethatisaccessibleto all classes and functions in the module.
Class definitions appear first. Code that imports this module can then use these classes.
Functions are defined next. When imported, functions are accessed as module.function().
If imported, the module defines classes and functions. If this module is run, the code here (e.g., testBook()) is executed.
PYTHON OBJECTS
Every variable in Python is actually an object. You don’t have to write object-oriented (OO) code to use Python, but the way Python is constructed encourages an OO approach.1
Object Types
All variables have a type that can be seen by using the built-in type() function.
>>> type(23)
<type 'int'>
>>> type('some more text')
<type 'str'>
>>> c=[1,2,'some more text']
>>> type(c)
<type 'list'>
Other types are 'class', 'module', 'function', 'file', 'bool', 'NoneType', and 'long'.
The special constants True and False are 'bool' types. The special constant None is a 'NoneType'.
To see a textual definition of any object, you can use the str() function; for example, the variable c can be represented as a string:
>>> str(c)
"[1,2,'some text']"
Factory Functions
There are a series of functions that create variable types directly. Here are the most commonly used ones.
int(4.0)
str(4)
list(1, 2, 3, 4)
tuple(1, 2, 3, 4)
dict(one=1, two=2)
Numbers
Creates integer 4
Creates string '4'
Creates list [1,2,3,4]
Creates tuple (1,2,3,4)
Creates dictionary {'one':1,'two':2}
Integer numbers have no realistic limit; you can store and manipulate numbers with 1,000 digits, for example. These are stored as “long” numbers, for example:
>>> 12345678901234567890
12345678901234567890
Real numbers (i.e., those with decimal points) are stored with what is called double- precision. For example:
>>>1 / 7.0
0.14285714285714285
The actual degree of precision depends on the architecture of the hardware you use. Very large or very small real numbers can be described using scientific notation.
>>> x = 1E20
>>> x / 7.0
1.4285714285714285e+19
>>> int(x/7.0)
14285714285714285714286592
>>> y = 1E-20
>>> y / 7.0
1.4285714285714285e-21
Arithmetic Operators
The four arithmetic operators work as expected.
# Addition
2 + 3
2.0 + 3
# Subtraction
3 - 2
3.0 - 2
# Multiplication
3 * 2
3 * 2.0
3.0 * 2.0
# Division
3 / 2
-6 / 2
-6 / 4
3 / 2.0
Other Operators
# Modulus
15 % 4
# Exponentiation
4 ** 3
-4 ** 3
4 ** -3
# Integer 5
# Real 5.0 (if one or more operands
# are real)
# Integer 1
# Real 1.0
# Integer 6
# Real 6.0
# Real 6.0
All divisions produce real numbers
# 1.5
# -3.0
# -1.5
# 1.3
# Real 3.0 (remainder after
# dividing 15 by 4)
# Integer 64
# Integer -64 (the '–' applies to
# the result)
# Real 0.015625 (NB negative
# exponents force operand to real
# numbers
CHAPTER 2 ■ PYTHON OBJECTS
Conversion Functions
int(1.234)
int(-1.234)
long(1.234)
long(-1.234)
long('1234')
long('1.234')
long(float('1.234'))
float(4)
float('4.321')
# Integer 1
# Integer -1
# Long1L
# Long -1L
# Long 1234L
# ** error ** needs 2
# conversions
# Long 1L (after two
# conversions)
# Real 4.0
# Real 4.321
Boolean Numbers
Booleans are actually held as integers but have a value of either True or False.
bool(23)
bool(0)
bool('any text')
bool('')
bool([])
# True - all nonzero integers # False -zero
# True – any string
# False – zero length strings
# False – empty lists
Random Numbers
Two random number generators are useful (you need to import the random module).
import random
random.randint(a,b)
random.random()
# Generates a random integer
# between a and b inclusive.
# Generates a random real
# number between 0.0 and 1.0
Sequences: Strings, Lists, and Tuples
So far, we have looked at variables that hold a single value. A sequence is a variable that holds multiple values as an array. Each element can be addressed by its position in the sequence as an offset from the first element. The three types of sequence are as follows:
• Strings: A sequence of characters that together form a text string.
• Lists: A sequence of values where each value can be accessed
using an offset from the first entry in the list.
• Tuples: A sequence of values, very much like a list, but the entries in a tuple are immutable; they cannot be changed.
We’ll look at the Python features that are common to all sequences and then look at the three types separately.
Sequence Storage and Access
The elements of a sequence are stored as a contiguous series of memory locations. The first element in the sequence can be accessed at position 0 and the last element at position n – 1 where n is the number of elements in the sequence (see Figure 2-1).
You can iterate through the elements of a sequence x having n elements starting at elementx[0]andadding+1eachtimex[1], x[2] 1⁄4 x[n-1],andsoon.Youcanalso iteratefromtheendandsubtract1eachtime:x[n-1], x[n-2] 1⁄4 x[0].
Membership
A common check is to determine whether a value exists in a sequence. For example:
'a' in 'track'
9 in [1,2,3,4,5,6]
'x' not in 'next'
'red' not in ['tan','pink']
# True # False
# False # True
Concatenation2
Two or more sequences can be added together to make longer sequences. The plus sign
(+) is used to concatenate strings, lists, or tuples.
sequence1 + sequence2 # results in a new sequence # that appends sequence2 to
# sequence1 'mr'+'joe'+'soap' # 'mrjoesoap'
2It is recommended that you use the join() string method to join a list of strings or a tuple, as it is more efficient. For example:
>>> '-'.join(('a','b','c','d'))
'a-b-c-d'
Sequence Elements and Slices
A sequence is an ordered list of elements, so a single element is identified by its offset from the first. A slice is a convenient way to select a subset of these elements in sequence, producing a new sequence. A slice is identified using this notation:
[startindex:endindex]
The slice will consist of elements starting as the startindex up to but not including endindex.
Some examples will make it easier to understand:
mylist=['a','b','c','d','e']
mylist[0]
mylist[3]
mylist[5]
mylist[-1]
mylist[1:3]
mylist[:4]
mylist[3:]
# a list with five
# elements
# 'a'
# 'd'
# results in an error
# 'e'
# ['b','c']
# ['a','b','c']
# ['d','e']
Sequences can be nested and elements accessed using multiple indexes, for example:
mylist = [1,2,3,['a','b','c'],5]
mylist[2]
mylist[3]
mylist[3][1]
# 3
# ['a','b','c']
# 'b'
Sequence Built-In Functions
mylist=[4,5,6,7,1,2,3]
len(seq)
len(mylist)
max(seq)
max(mylist)
min(mylist)
Strings
# the length of seq #7
# maximum value inseq #7
# 1 – the minimum
A string is a sequence of characters that make up a piece of text. Strings are immutable, but you can update the value of a string by assigning a new string to the same string variable.
>>> mystr = 'Paddington Station'
>>> mystr=mystr.upper() # replaces mystr
>>> mystr
PADDINGTON STATION
Assignment
You can delimit strings with either single (') or double (") quotes, as long as they are matched. You can embed either quote inside the other.
>>> text = 'Hello World!'
>>> longtext = "A longer piece of text"
>>> print(text)
Hello World!
>>>longtext
'A longer piece of text'
>>> text = 'Paul said, "Hello World!"'
>>>print(text)
Paul said, "Hello World!"
Accessing Substrings
You access substrings with slices, of course:
text='Paul said, "Hi"'
text[:4]
text[-4:]
text[5:9]
text[0:4] + text[12:14]
String Comparison Strings can be compared3 as follows:
'mcr'>'liv'
'liv'>'tot'
'mcr'=='X'
'X'>'t'
# 'Paul'
# '"Hi"'
# 'said'
# 'PaulHi'
# True
# False
# False
# False
Membership (Searching)
We can check whether a substring is in a string, character by character or using substrings. The outcome is a Boolean.
'a' in 'the task'
'w' in 'the task'
'as' in 'the task'
'job' not in 'the task'
'task' in 'the task'
# True
# False
# True
# True
# True
Special Characters and Escaping
A string can contain nonprinting and control characters (e.g., tab, newline, and other special characters) by “escaping” them with a backslash (\). Common escape characters are the following:
\0 Null character
\t Horizontal tab
\n Newline character
\' Single quote
\" Double quote
\\ Backslash
>>> multiline='Line 1\nLine 2\nLine 3'
>>> print(multiline)
Line 1
Line 2
Line 3
Triple Quotes
Longer pieces of text with embedded newlines can be assigned using the triple quotes notation; for example:
>>> multiline="""Line1 1⁄4 Line 2
1⁄4 Line 3"""
>>> multiline
'Line 1\nLine 2\nLine 3'
text = 'This is text'
nums = '123456'
# finding text
text.find('is')
text.find('your')
# Validation checks
text.isalpha()
text.isdigit()
nums.isdigit()
# concatenation
''.join((text,nums))
' '.join((text,nums))
# case changing
text.upper()
text.lower()
# splitting string
text.split(' ')
# substitution
text.replace('is','was')
# stripping
text.rstrip()
text.lstrip()
text.strip()
Lists
# returns2
# returns -1
# all alphas?True
# all digits? False
# True
#'This is text123456'
#'This is text 123456'
# 'THIS IS TEXT'
# 'this is text'
# list of strings:
#['This','is','text']
# This was text
# remove trailing space
# remove leading space
# remove trailing and leading spaces
Lists are widely used to store values that are collected and processed in sequence, such as lines of text read from or written to a text file, or where prepared values are looked up by their position or offset in the array.
Creating Lists
mylist = []
names=['Tom','Dick','Harry']
mixedlist = [1,2,3,'four']
elist = [1,2,3,[4,5,6]]
# an empty list
# list of strings
# list of mixed
# types
# embedded list
You can find the length of lists using the len() function. The length is the number of elements in the list. The last element index of a list mylist would be accessed as mylist[len(mylist)-1].
l = len(names) # 3
Values in the preceding lists are accessed as shown in the following code fragments.
CHAPTER 2 ■ PYTHON OBJECTS
names[1]
mixedlist[0]
mixedlist[3]
mixedlist[2:4]
elist[2]
elist[3]
elist[3][1]
# 'Dick'
# 1
# 'four'
# [3,'four']
# 3
# [4,5,6] # 5
If you try to access a nonexistent element in the list, you will get a 'list index out of range'error.
Updating Lists
Use the append() method to add entries to the end of a list. Use the del statement to delete an entry. For example:
mylist = []
mylist.append('Tom')
mylist.append('Dick')
mylist.append('Harry')
# Change an entry
mylist[1]='Bill'
# Delete an entry
del mylist[1]
Indexing
# an empty list
# ['Tom']
# ['Tom','Dick']
# ['Tom','Dick','Harry']
# ['Tom','Bill','Harry']
# ['Tom','Harry']
Whereas the membership (in, not in) operators return a Boolean True or False, the index() method finds an entry in your list and returns the offset of that entry. If the entry cannot be found, it returns an error.
mylist=['Tom','Dick','Harry']
mylist.index('Dick')
mylist.index('Henry')
# 1
# ValueError: Henry
# not in list
Sequence Operations and Functions
The sequence operators—comparisons, slices, membership, and concatenation—all work the same as they do with strings.
Thesequencefunctions—len(), max(), min(), sum(), sorted()and reversed()—all work as expected.
Tuples
Like numbers and strings, tuples are immutable. They are useful for preset lookups or validators you might reuse.
Creating Tuples
To distinguish a tuple from a list, Python uses parentheses () to enclose the entries in a tuple.
>>> mynumbers = (1,2,3,4,5,6,7)
>>> months=('Jan','Feb','Mar','Apr','May','Jun',
1⁄4 'Jul','Aug','Sep','Oct','Nov','Dec')
>>> mixed = ('a',123,'some text',[1,2,3,'testing'])
# accessing tuples
>>> mynumbers[3]
>>> months[3:6]
>>> mixed[2]+' '+mixed[3][3]
# 4
# ('Apr','May','Jun')
# 'some text testing'
You can find the length of tuples using the len() function. The length is the number of elements in the list.
If you try to access a nonexistent element in the list, you will get a 'tuple index out of range'error.
Sequence Operations and Functions
The sequence operators—comparisons, slices, membership, and concatenation—all work as expected. The index() method works exactly as that for lists. The sequence functions—len(), max(), min(), sum(), sorted(), and reversed()—all work as expected.
Dictionaries
If we want our program to remember a collection of values, we can use lists and we can access the entries using the index to those values. To find the value we want, though, we must know the offset to that value (or search for it).
Dictionaries provide a lookup facility based on key/value pairs. The order of the entries in a dictionary is not defined (in fact, it is somewhat random), but every entry can be retrieved by using its key. Keys must be unique; there can only be one entry in a dictionary for each key.
Creating a Dictionary
You can create a dictionary using a set of key/value pairs.
>>> # days of week – seven key-value pairs >>> wdays={'M':'Monday','T':'Tuesday',
1⁄4 'W':'Wednesday','Th':'Thursday',
1⁄4 'F':'Friday','Sa':'Saturday',
1⁄4 'Su':'Sunday'} >>> wdays['M'] 'Monday'
>>> wdays['W'] 'Wednesday'
>>> wdays['Su']
'Sunday'
>>> newdict = {}
# empty dictionary
Updating a Dictionary
You can update dictionaries using the dict[key] convention.
>>> newdict = {} # empty dictionary
>>> newdict['1st'] = 'first entry' # add 1st entry
>>> newdict['2nd'] = 'second entry'# add 2nd entry
>>> newdict['1st'] = 'new value'
>>> del newdict['2nd']
>>> len(newdict)
Dictionary Operations
# update 1st entry
# delete 2nd entry
# 1
The sequence operators—comparisons, membership, and concatenation—all work as expected. Here are a few dictionary operations that you might find useful:
# days of week – seven key/value pairs
# key existence
>>> 'Sa' in wdays
True
>>> 'Sp' in wdays
False
# create list of keys
>>> wdays.keys()
['M','T','W','Th','F','Sa','Su']
# create an iterable list of values
>>> wdays.values()
dict_values(['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday',
'Sunday'])
# look up a key with a default if key not found
>>> wdays.get('X','Not a day')
'Not a day'
PROGRAM Structure
Decision Making
Some simple utilities might be a short list of statements run in order, but a useful program usually needs to make decisions and choices. The decision making in a program determines the path that the program takes. Decisions are made by if statements.
The if Statement
if statements depend on some form of test or condition. The normal format of an if statement is shown here:
if test:
statement1
statement2
statement3
# note the colon ':'.
# The statements following the
# if are indented and executed
# if the test is True.
In this case, the three statements are executed if test is True. Otherwise these statements are skipped and the interpreter skips to the statement following the code block.
Often, the False outcome of the test has its own code block, as follows:
if test:
DoThis()
else:
DoThat()
# note the colon ':'.
# DoThis() ... if test=True
# note the colon after 'else'
# DoThat() ... if test=False
The else: keyword separates the True outcome of the test from the False outcome of the test and directs the compiler to take the alternate code block: DoThat().
So far, we’ve seen a binary decision with only a True and a False outcome. Some if statements make a choice from more than two alternatives. In this case, the elif: keyword separates the different choices and the else: keyword is the final choice if no other test is met. For example:
1 If code=='RED':
2 SoundRedAlert()
3 4 5 6
elif code=='AMBER':
GiveWarning()
else: pass
■ Note that the indentation of the if and its corresponding elif and else keywords must all be the same.
The pass Statement
In the preceding example, depending on the value of code, the program makes a choice to do something, but the third choice was pass. The pass statement is a “do nothing” statement. The third clause (else) is not strictly necessary, but pass is often useful to show explicitly what the program is doing (or not doing).
Types of Test
The tests that are applied in an if statement can take many forms, but the main patterns are summarized here.
26
• Comparisons
var1 > var2
var1 == var2
var1 != var2
# greater than
# equal to
# not equal to
• Sequence (list, tuple, or dictionary) membership var in seq
var not in seq
• Sequence length len(x)>0
• Boolean value fileopen
not fileopen
• Has a value? var
# sequence has entries?
# fileopen==True?
# fileopen==False?
# not None (or zero or '')
• Validation
var.isalpha() # alphanumeric?
var.isdigit() # is all digits?
• Calculations
(price*quantity) > 100.0 # cost>100?
(cost-budget) > 0.0 # overbudget?
In the case of calculations it is often better to use braces to force the calculations than to rely on the default operator precedence.
Some decisions are more complex and require multiple tests to implement. For example:
if hungry and foodInFridge and notTooTired:
cookAMeal()
else:
getTakeAway()
In this case, the and operator joins the three conditions and all must be True for cookAMeal() to be executed.
There are three logical operators—and, or, and not—that can be used to link decisions.
Decisions can be nested; that is, they can appear inside the indented code blocks of other decisions. For example:
if age>19:
if carValue>10000:
if gotConvictions:
rejectInsuranceApplication()
Loops and Iteration
Some features need to perform activities repetitively to process a number of items, including the following:
• Lists of values.
• Entries in a dictionary.
• Rows of data in a database.
• Lines of text in a disk file.
These constructs, usually called loops, perform a defined code block repeatedly on some item of data until some condition or test is met (or not met). These loops are implemented using for and while statements.
For Statement
The for statement acts as a header statement for a code block that is to be executed until some condition is met. The for statement operates on an iterable set of elements, often a sequence.
>>> theTeam=['Julia','Jane','Tom','Dick','Harry']
>>> for person in theTeam:
... print('%s is in the team' % person)
Julia is in the team
Jane is in the team
Tom is in the team
Dick is in the team
Harry is in the team
Thegeneralformatis'for var in seq:'.
For each iteration through the members of the sequence, the variable var takes the value of the entry in the list or tuple, or it takes the value of the key in a dictionary.
Sometimes we don’t want to iterate through a list or dictionary, but we want to execute a loop a specific number of times. Python provides a useful range() function that generates an iterable list of specific size for us. It can take three arguments—start, end, and step—that specify the first number, the maximum number, and the increment between elements in the generated range, respectively. If you provide just one number argument, it creates a list with that number of integer elements starting with zero.
>>> range(10)
range(0,10)
>>> range(1,10)
range(1,10)
>>> range(1,20,3)
range(1,20,3)
# a list of ten elements 0-9
# default step=1
# list: [1,2,3,4,5,6,7,8,9]
# steps of 3
# list: [1,4,7,10,13,16,19]
>>> for i in range(3):
... print(i)
0
1
2
While Statement
The while statement is similar to the for statement in that it provides a header statement for a code block to be repeated a number of times. Instead of an iterable set of elements, the while statement repeats the loop until a test is not met.
>>> n=4
>>> while n>0:
... print(n)
... n-=1
4 3 2 1
Often, the condition to be met is a counter that is decremented in the loop or a Boolean value the value of which is changed inside the loop, and then the loop is terminated.
>>> foundSmith=False
>>> while not foundSmith:
... name=getNextName() # get next person record
... if name=='smith': # name is 'smith'?
foundSmith=True
Break Statement
A break statement is used to terminate the current loop and continue to the next statement after the for or while code block.
1 2 3 4 5 6 7
while True: # this is an infinite loop
command=input('Enter command:')
if command=='exit': # infinite till user exits
break # skips to line 7
else:
doCommand(command) # execute command
print('bye')
Continue Statement: (In the examples that follow, the doCommand() function needs to be defined, of course.)
A continue statement is used in the code block of a loop to exit from the current code block and skip to the next iteration of the loop. The while or for loop test is checked as normal.
1 2 3 4 5 6 7 8 9 10
while True: # this is an infinite loop
command=input('Enter command:')
if len(command)==0: # no command - try again
continue # goes to next loop (line 1)
elif command=='exit': # user exit
print('Goodbye')
break # skips to line 10
else:
doCommand(command)
print('bye')
List Comprehensions
A list comprehension (also known as a listcomp) is a way of dynamically creating a list of elements in an elegant shorthand. Suppose you wanted to create a list of the squares of the first ten integers. You could use this code:
squares=[]
for i in range(1,11):
squares.append(i*i)
Or you could use this:
squares=[i*i for i in range(1,11)]
The syntax for listcomps is:
[expr for element in iterable if condition]
The if condition can be used to select elements from the iterable. Here are some examples of this syntax in use:
# a list of even numbers between 1 and 100
evens = [i for i in range(1,100) if not i % 2]
# a list of strings in lines containing 'True'
trulines = [l for l in lines if l.find('True')>-1]
Using Functions
Why Write Functions?
When you write more complicated programs, you can choose to write them in long, complicated modules, but complicated modules are harder to write and difficult to understand. A better approach is to modularize a complicated program into smaller, simpler, more focused modules and functions.
The main motivation for splitting large programs into modules and functions is to better manage the complexity of the process.
• Modularization “divides and conquers” the complexity into smaller chunks of less complex code, so design is easier.
• Functions that do one thing well are easier to understand and can be very useful to you and other programmers.
• Functions can often be reused in different parts of a system to avoid duplicating code.
• If you want to change some behavior, if it’s in a function, you only need to change code in one place.
• Smaller functions are easier to test, debug, and get working.
Importantly, if you choose to use a function written by someone else, you shouldn’t
need to worry too much how it works, but you need to trust it.2
What Is a Function?
A function is a piece of program code that is:
• A self-contained coherent piece of functionality.
• Callable by other programs and modules.
• Passed data using arguments (if required) by the calling module.
• Capable of returning results to its caller (if required).
You already know about quite a few built-in Python functions. One of these is the len() function. We just call len() and pass a sequence as a parameter. We don’t need to write our own len() function, but suppose we did write one of our own (for lists and dictionaries only). It might look something like this:
>>> def lenDictList(seq):
...
...
...
...
...
...
... return nelems
...
The header line has a distinct format:
if type(seq) not in [list,dict]: # a seq?
return -1
nelems=0
# no - fail!
# length zero
# for elem
# add one
# length
for elem in seq:
nelems+=1
• The keyword def to signify it is a new function.
• A function name lenDictList (that meets the variable naming rules).
• Braces to enclose the arguments (none, 1 or more).
• A colon to denote the end of the header line.
The code inside the function is indented. The code uses the arguments in the function definitions and does not need to define them (they will be passed by the calling module). Here are some examples:
>>> l = [1,2,3,4,5]
>>> d = {1:'one',2:'two',3:'three'}
>>> lenDictList(l)
5
>>> lenDictList(d)
3
>>> lenDictList(34)
-1
Note that the real len() handles any sequence including tuples and strings and does better error-handling. This is a much oversimplified version.
Return Values
The results of the function are returned to the caller using the return statement. In the preceding example, there is one return value: the length of the list or dictionary provided or it is –1 if the argument is neither.
Some functions do not return a result; they simply exit.
It is for the programmer to choose how to design his or her functions. Here are some example return statements.
return
return
return
return
return
True
False
r1, r2, r3
dict(a=v1,b=v2)
# does not return a value
# True – perhaps success?
# False – perhaps a failure?
# returns three results
# returns a dictionary
Calling a Function
Functions are called by using their name and adding parentheses enclosing the variables or values to be passed as arguments. You know len() already. The other functions are invented to illustrate how functions are used.
>>> count = len(seq) # length of a sequence
>>>
>>> # the call below returns three results, the
>>> # maximum, the minimum, and the average of
>>> # the numbers in a list
>>> max, min, average = analyse(numlist)
>>>
>>> # the next call provides three parameters
>>> # and the function calculates a fee
>>> fee = calculateFee(hours, rate, taxfactor)
Note: The number of variables on the left of the assignment must match the number of return values provided by the function.
Named Arguments
If a function just has a single argument, then you might not worry what its name is in a function call. Sometimes, though, not all arguments are required to be provided and they can take a default value. In this case you don’t have to provide a value for the argument. If you do name some arguments in the function call, then you must provide the named arguments after the unnamed arguments. Here is an example:
def fn(a, b, c=1.0):
return a*b*c
fn(1,2,3)
fn(1,2)
fn(1,b=2)
fn(a=1,b=2,c=3)
fn(1,b=2,3)
# 1*2*3 = 6
# 1*2*1 = 2 – c=default 1.0
# 1*2*1 = 2 – same result
# 1*2*3 = 6 - as before
# error! You must provide
# named args *after* unnamed
# args
■ Note In your code, you must define a function before you can call it. A function call must not appear earlier in the code than the definition of that function or you will get an “undefined” error.
Variable Scope
The variables that are defined and used inside a function are not visible or usable to other functions or code outside the function. However, if you define a variable in a module and call a function inside that module, then that variable is available in the called function. If a variable is defined outside all the functions in a module, the variable is available to all of the functions in the module.3 For example:
sharedvar="I'm sharable"
def first():
print(sharedvar)
firstvar='Not shared'
return
def second():
print(sharedvar)
print(firstvar)
return
# a var shared by both
# functions
# this is OK
# this is unique to first
# this is OK
# this would fail!
INPUT AND OUTPUT
If a program did not produce any output, it wouldn’t be very useful, would it? If a program did not accept some data that varied from time to time, it would produce the same result again and again and again, and that wouldn’t be very useful either (after its first run at least). Most programs, therefore, need to accept some inputs or input data, so that they can product output data, outputs, or results.
In this chapter, we cover three important input/output mechanisms:
• Displayed output.
• Getting data from the user through the keyboard.
• Getting input from and writing output to disk files.