Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters
Instant ebooks textbook Python Programming for Data Analysis 1st Edition José Unpingco download all chapters
com
https://textbookfull.com/product/python-programming-for-
data-analysis-1st-edition-jose-unpingco/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-jose-unpingco/
textboxfull.com
https://textbookfull.com/product/python-for-probability-statistics-
and-machine-learning-2nd-edition-jose-unpingco/
textboxfull.com
https://textbookfull.com/product/practical-machine-learning-for-data-
analysis-using-python-1st-edition-abdulhamit-subasi/
textboxfull.com
Python for Data Analysis Data Wrangling with Pandas NumPy
and IPython Wes Mckinney
https://textbookfull.com/product/python-for-data-analysis-data-
wrangling-with-pandas-numpy-and-ipython-wes-mckinney/
textboxfull.com
https://textbookfull.com/product/python-for-excel-a-modern-
environment-for-automation-and-data-analysis-1st-edition-felix-
zumstein/
textboxfull.com
https://textbookfull.com/product/python-for-data-analysis-data-
wrangling-with-pandas-numpy-and-jupyter-3rd-edition-wes-mckinney/
textboxfull.com
Python
Programming
for Data
Analysis
Python Programming for Data Analysis
José Unpingco
Python Programming
for Data Analysis
José Unpingco
University of California
San Diego
CA, USA
© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland
AG 2021
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To Irene, Nicholas, and Daniella, for all their
patient support.
Preface
This book grew out of notes for the ECE143 Programming for Data Analysis class
that I have been teaching at the University of California, San Diego, which is a
requirement for both graduate and undergraduate degrees in Machine Learning and
Data Science. The reader is assumed to have some basic programming knowledge
and experience using another language, such as Matlab or Java. The Python idioms
and methods we discuss here focus on data analysis, notwithstanding Python’s
usage across many other topics. Specifically, because raw data is typically a mess
and requires much work to prepare, this text focuses on specific Python language
features to facilitate such cleanup, as opposed to only focusing on particular Python
modules for this.
As with ECE143, here we discuss why things are the way they are in Python
instead of just that they are this way. I have found that providing this kind of
context helps students make better engineering design choices in their codes, which
is especially helpful for newcomers to both Python and data analysis. The text is
sprinkled with little tricks of the trade to make it easier to create readable and
maintainable code suitable for use in both production and development.
The text focuses on using the Python language itself effectively and then moves
on to key third-party modules. This approach improves effectiveness in different
environments, which may or may not have access to such third-party modules. The
Numpy numerical array module is covered in depth because it is the foundation
of all data science and machine learning in Python. We discuss the Numpy array
data structure in detail, especially its memory aspects. Next, we move on to Pandas
and develop its many features for effective and fluid data processing. Because data
visualization is key to data science and machine learning, third-party modules such
as Matplotlib are developed in depth, as well as web-based modules such as Bokeh,
Holoviews, Plotly, and Altair.
On the other hand, I would not recommend this book to someone with no
programming experience at all, but if you can do a little Python already and want to
improve by understanding how and why Python works the way it does, then this is
a good book for you.
vii
viii Preface
To get the most out of this book, open a Python interpreter and type-along with
the many code samples. I worked hard to ensure that all of the given code samples
work as advertised.
Acknowledgments I would like to acknowledge the help of Brian Granger and Fernando Perez,
two of the originators of the Jupyter Notebook, for all their great work, as well as the Python
community as a whole, for all their contributions that made this book possible. Hans Petter
Langtangen was the author of the Doconce [1] document preparation system that was used to
write this text. Thanks to Geoffrey Poore [2] for his work with PythonTeX and LATEX, both key
technologies used to produce this book.
References
1 Basic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Basic Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Reserved Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.4 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.6 Loops and Conditionals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.1.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.1.8 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.1.9 Dealing with Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.1.10 Power Python Features to Master . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.1.11 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.1.12 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
1.1.13 Iteration and Iterables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
1.1.14 Using Python Assertions to Pre-debug Code . . . . . . . . . . . . . . . 60
1.1.15 Stack Tracing with sys.settrace . . . . . . . . . . . . . . . . . . . . . . 61
1.1.16 Debugging Using IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
1.1.17 Logging from Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2 Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.1 Properties/Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.2 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.4 Class Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.5 Class Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.6 Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
2.7 Hashing Hides Parent Variables from Children . . . . . . . . . . . . . . . . . . . . . . 76
2.8 Delegating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.9 Using super for Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.10 Metaprogramming: Monkey Patching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
ix
x Contents
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Chapter 1
Basic Programming
Before we get into the details, it is a good idea to get a high-level orientation to
Python. This will improve your decision-making later regarding software develop-
ment for your own projects, especially as these get bigger and more complex. Python
grew out of a language called ABC, which was developed in the Netherlands in the
1980s as an alternative to BASIC to get scientists to utilize microcomputers, which
were new at the time. The important impulse was to make non-specialist scientists
able to productively utilize these new computers. Indeed, this pragmatic approach
continues to this day in Python which is a direct descendent of the ABC language.
There is a saying in Python—come for the language, stay for the community.
Python is an open source project that is community driven so there is no corporate
business entity making top-down decisions for the language. It would seem that such
an approach would lead to chaos but Python has benefited over many years from
the patient and pragmatic leadership of Guido van Rossum, the originator of the
language. Nowadays, there is a separate governance committee that has taken over
this role since Guido’s retirement. The open design of the language and the quality
of the source code has made it possible for Python to enjoy many contributions from
talented developers all over the world over many years, as embodied by the richness
of the standard library. Python is also legendary for having a welcoming community
for newcomers so it is easy to find help online for getting started with Python.
The pragmatism of the language and the generosity of the community have long
made Python a great way to develop web applications. Before the advent of data
science and machine learning, upwards of 80% of the Python community were web
developers. In the last five years (at the time of this writing), the balance is tilted to
an almost even split between web developers and data scientists. This is the reason
you find a lot of web protocols and technology in the standard library.
Python is an interpreted language as opposed to a compiled language like C
or FORTRAN. Although both cases start with a source code file, the compiler
© The Editor(s) (if applicable) and The Author(s), under exclusive license 1
to Springer Nature Switzerland AG 2021
J. Unpingco, Python Programming for Data Analysis,
https://doi.org/10.1007/978-3-030-68952-0_1
2 1 Basic Programming
examines the source code end-to-end and produces an executable that is linked to
system-specific library files. Once the executable is created, there is no longer any
need for the compiler. You can just run the executable on the system. On the other
hand, an interpreted language like Python you must always have a running Python
process to execute the code. This is because the Python process is an abstraction on
the platform it is running on and thus must interpret the instructions in the source
code to execute them on the platform. As the intermediary between the source
code on the platform, the Python interpreter is responsible for the platform specific
issues. The advantage of this is that source code can be run on different platforms as
long as there is a working Python interpreter on each platform. This makes Python
source codes portable between platforms because the platform specific details are
handled by the respective Python interpreters. Portability between platforms was
a key advantage of Python, especially in the early days. Going back to compiled
languages, because the platform specific details are embedded in the executable, the
executable is tied to a specific platform and to those specific libraries that have been
linked into the executable. This makes these codes are less portable than Python,
but because the compiler is able to link to the specific platform it has the option
to exploit platform- specific level optimizations and libraries. Furthermore, the
compiler can study the source code file and apply compiler-level optimizations that
accelerate the resulting executable. In broad strokes, those are the key differences
between interpreted and compiled languages. We will later see that there are many
compromises between these two extremes for accelerating Python codes.
It is sometimes said that Python is slow as compared to compiled languages,
and pursuant to the differences we discussed above, that may be expected, but it
is really a question of where the clock starts. If you start the clock to account for
developer time, not just code runtime, then Python is clearly faster, just because
the development iteration cycle does not require a tedious compile and link step.
Furthermore, Python is just simpler to use than compiled languages because so
many tricky elements like memory management are handled automatically. Pythons
quick turnaround time is a major advantage for product development, which requires
rapid iteration. On the other hand, codes that are compute-limited and must run
on specialized hardware are not good use-cases for Python. These include solving
systems of parallel differential equations simulating large-scale fluid mechanics or
other large-scale physics calculations. Note that Python is used in such settings but
mainly for staging these computations or postprocessing the resulting data.
Your primary interface to the Python interpreter is the commandline. You can type
in python in your terminal you should see something like the following,
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
1.1 Basic Language 3
There is a lot of useful information including the version of Python and its
provenance. This matters because sometimes the Python interpreter is compiled to
allow fast access to certain preferred modules (i.e., math module). We will discuss
this more when we talk about Python modules.
Although Python will not stop you, do not use these as variable or function names.
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
For example, a common mistake is to assign sum=10, not realizing that now the
sum() Python function is no longer available.
1.1.3 Numbers
The operator has many other subtle uses and was introduced to improve readability
in certain situations. You can also cast among the numeric types in a common-sense
way:
>>> int(1.33333)
1
>>> float(1)
1.0
>>> type(1)
<class 'int'>
>>> type(float(1))
<class 'float'>
Importantly, Python integers are of arbitrary length and can handle extremely large
integers. This is because they are stored as a list of digits internally. This means that
they are slower to manipulate than Numpy integers which have fixed bit-lengths,
provided that the integer can fit into allocated memory.
1.1.5 Strings
Python strings have C-style escape characters for newlinewsnewlines, tabs, etc.
String literals defined this way are by default encoded using UTF-8 instead of
ASCII, as in Python 2. The triple single (’) or triple double quote (") denotes a
block with embedded newlines or other quotes. This is particularly important for
function documentation docstrings that we will discussed later.
>>> print( '''Usage: thingy [OPTIONS]
... and more lines
... here and
... here
... ''')
Usage: thingy [OPTIONS]
and more lines
here and
here
Strings can be controlled with a character before the single or double quote. For
example, see the comments (#) below for each step,
>>> # the 'r' makes this a 'raw' string
>>> hello = r"This long string contains newline characters \n, as
→ in C"
>>> print(hello)
This long string contains newline characters \n, as in C
>>> # otherwise, you get the newline \n acting as such
>>> hello = "This long string contains newline characters \n, as
→ in C"
>>> print(hello)
This long string contains newline characters
, as in C
1.1 Basic Language 7
Note that a f-string evaluates (i.e., interpolates) the Python variables in the
current scope,
>>> x = 10
>>> s = f'{x}'
>>> type(s)
<class 'str'>
>>> s
'10'
Beware that an f-string is not resolved until run-time because it has to resolve
the embedded variables. This means that you cannot use f-strings as docstrings.
Importantly, Python strings are immutable which means that once a string is created,
it cannot be changed in-place. For example,
>>> x = 'strings are immutable '
>>> x[0] = 'S' # not allowed!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
This means you have to create new strings to make this kind of change.
Strings vs Bytes In Python 3, the default string encoding for string literals is UTF-
8. The main thing to keep in mind is that bytes and strings are now distinct objects,
as opposed to both deriving from basestring in Python 2. For example, given
the following unicode string,
>>> x='Ø'
>>> isinstance(x,str) # True
True
>>> isinstance(x,bytes) # False
False
>>> x.encode('utf8') # convert to bytes with encode
b'\xc3\x98'
Note the distinction between bytes and strings. We can convert bytes to strings using
decode,
>>> x=b'\xc3\x98'
>>> isinstance(x,bytes) # True
True
>>> isinstance(x,str) # False
False
>>> x.decode('utf8')
'Ø'
8 1 Basic Programming
An important consequence is that you cannot append strings and bytes as in the
following: u"hello"+b"goodbye". This used to work fine in Python 2 because
bytes would automatically be decoded using ASCII, but this no longer works in
Python 3. To get this behavior, you have to explicitly decode/encode. For
example,
>>> x=b'\xc3\x98'
>>> isinstance(x,bytes) # True
True
>>> y='banana'
>>> isinstance(y,str) # True
True
>>> x+y.encode()
b'\xc3\x98banana'
>>> x.decode()+y
'Øbanana'
Slicing Strings Python is a zero-indexed language (like C). The colon (:) character
denotes .
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>> word[-1] # The last character
'A'
>>> word[-2] # The last-but-one character
'p'
>>> word[-2:] # The last two characters
'pA'
>>> word[:-2] # Everything except the last two characters
'Hel'
Python has a built-in and very powerful regular expression module (re) for string
manipulations. String substitution creates new strings.
>>> x = 'This is a string'
>>> x.replace('string','newstring')
'This is a newstring'
>>> x # x hasn't changed
'This is a string'
1.1 Basic Language 9
Formatting Strings There are so many ways to format strings in Python, but here
is the simplest that follows C-language sprintf conventions in conjunction with
the modulo operator %.
>>> 'this is a decimal number %d'%(10)
'this is a decimal number 10'
>>> 'this is a float %3.2f'%(10.33)
'this is a float 10.33'
>>> x = 1.03
>>> 'this is a variable %e' % (x) # exponential format
'this is a variable 1.030000e+00'
The advantage of format is you can reuse the placeholders as in the following,
>>> 'x = {0},{1},{0}; y = {1}'.format(x,y)
'x = 10,20,10; y = 20'
Basic Data Structures Python provides many powerful data structures. The two
most powerful and fundamental are the list and dictionary. Data structures and
10 1 Basic Programming
Lists can also be sorted in-place using the sort() list method,
>>> x = [1,9,8,2]
>>> x.sort()
>>> x
[1, 2, 8, 9]
Both of these use the powerful Timsort algorithm. Later, we will see more
variations and uses for these sorting functions.
Now that we have a feel for how to index and use lists, let us talk about the invariant
that it provides: as long as you index a list within its bounds, it provides the next
ordered element of the list. For example,
>>> x = ['a',10,'c']
>>> x[1] # return 10
10
>>> x.remove(10)
>>> x[1] # next element
'c'
Notice that the list data structure filled in the gap after removing 10. This is extra
work that the list data structure did for you without explicitly programming. Also,
list elements are accessible via integer indices and integers have a natural ordering
and thus so does the list. The work of maintaining the invariant does not come for
free, however. Consider the following,
>>> x = [1,3,'a']
>>> x.insert(0,10) # insert at beginning
12 1 Basic Programming
>>> x
[10, 1, 3, 'a']
Seem harmless? Sure, for small lists, but not so for large lists. This is because
to maintain the invariant the list has to scoot (i.e., memory copy) the remaining
elements over to the right to accommodate the new element added at the beginning.
Over a large list with millions of elements and in a loop, this can take a substantial
amount of time. This is why the default append() and pop() list methods work
at the tail end of the list, where there is no need to scoot items to the right.
Tuples Tuples are another general purpose sequential container in Python, very
similar to lists, but these are immutable. Tuples are delimited by commas (parenthe-
ses are grouping symbols). Here are some examples,
>>> a = 1,2,3 # no parenthesis needed!
>>> type(a)
<class 'tuple'>
>>> pets=('dog','cat','bird')
>>> pets[0]
'dog'
>>> pets + pets # addition
('dog', 'cat', 'bird', 'dog', 'cat', 'bird')
>>> pets*3
('dog', 'cat', 'bird', 'dog', 'cat', 'bird', 'dog', 'cat', 'bird')
>>> pets[0]='rat' # assignment not work!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
It may seem redundant to have tuples which behave in terms of their indexing like
lists, but the key difference is that tuples are immutable, as the last line above shows.
The key advantage of immutability is that it comes with less overhead for Python
memory management. In this sense, they are lighter weight and provide stability for
codes that pass tuples around. Later, we will see this for function signatures, which
is where the major advantages of tuples arise.
(continued)
1.1 Basic Language 13
This is more consequential for mutable data structures like lists. Consider the
following,
>>> x = y = [1,3,4]
>>> x[0] = 'a'
>>> x
['a', 3, 4]
>>> y
['a', 3, 4]
>>> id(x),id(y)
(140271930505344, 140271930505344)
Because x and y are merely two labels for the same underlying list, changes to
one of the labels affects both lists. Python is inherently stingy about allocating
new memory so if you want to have two different lists with the same content,
you can force a copy as in the following,
>>> x = [1,3,4]
>>> y = x[:] # force copy
>>> id(x),id(y) # different ids now!
(140271930004160, 140271929640448)
>>> x[1] = 99
>>> x
[1, 99, 4]
>>> y # retains original data
[1, 3, 4]
Note how the z variable collected the remaining items in the assignment. You can
also change the order of the chunking,
>>> x,*y,z = 1,2,3,4,5
>>> x
1
14 1 Basic Programming
>>> y
[2, 3, 4]
>>> z
5
This unpacking is sometimes called de-structuring, or splat, in case you read this
term elsewhere.
Dictionaries Python dictionaries are central to Python because many other ele-
ments (e.g., functions, classes) are built around them. Effectively programming
Python means using dictionaries effectively. Dictionaries are general containers that
implement the mapping data structure, which is sometimes called a hash table or
associative array. Dictionaries require a key/value pair, which maps the key to the
value.
>>> x = {'key': 'value'}
The curly braces and the colon make the dictionary. To retrieve the value from the
x dictionary, you must index it with the key as shown,
>>> x['key']
'value'
Pretty slick.
The invariant that the dictionary provides is that as long as you provide a valid key,
then it will always retrieve the corresponding value; or, in the case of assignment,
store the value reliably. Recall that lists are ordered data structures in the sense that
when elements are indexed, the next element can be found by a relative offset from
the prior one. This means that these elements are laid out contiguously in memory.
Dictionaries do not have this property because they will put values wherever they
can find memory, contiguous or not. This is because dictionaries do not rely upon
relative offsets for indexing, they rely instead on a hash function. Consider the
following,
>>> x = {0: 'zero', 1: 'one'}
>>> y = ['zero','one']
>>> x[1] # dictionary
'one'
>>> y[1] # list
'one'
Indexing both variables looks notationally the same in both cases, but the process
is different. When given a key, the dictionary computes a hash function and the
stores the value at a memory location based upon the hash function. What is a hash
function? A hash function takes an input and is designed to return, with very high
probability, a value that is unique to the key. In particular, this means that two keys
cannot have the same hash, or, equivalently, cannot store different values in the
same memory location. Here are two keys which are almost identical, but have very
different hashes.
>>> hash('12345')
3973217705519425393
>>> hash('12346')
3824627720283660249
All this is with respect to probability, though. Because memory is finite, it could
happen that the hash function produces values that are the same. This is known
as a hash collision and Python implements fallback algorithms to handle this
16 1 Basic Programming
Let us think about why this happens. Remember that the hash function guarantees
that when given a key it will always be able to retrieve the value. Suppose that it
were possible use mutable keys in dictionaries. In the above code, we would have
hash(a) -> 132334, as an example, and let us suppose the 10 value is inserted
in that memory slot. Later in the code, we could change the contents of a as in
a[0]=3. Now, because the hash function is guaranteed to produce different outputs
for different inputs, the hash function output would be different from 132334 and
thus the dictionary could not retrieve the corresponding value, which would violate
its invariant. Thus we have arrived at a contradiction that explains why dictionary
keys must be immutable.
Sets Python provides mathematical sets and corresponding operations with the
set() data structure, which are basically dictionaries without values.
>>> set([1,2,11,1]) # union-izes elements
{1, 2, 11}
>>> set([1,2,3]) & set([2,3,4]) # bitwise intersection
{2, 3}
>>> set([1,2,3]) and set([2,3,4])
{2, 3, 4}
>>> set([1,2,3]) ^ set([2,3,4]) # bitwise exclusive OR
{1, 4}
>>> set([1,2,3]) | set([2,3,4]) # OR
{1, 2, 3, 4}
>>> set([ [1,2,3],[2,3,4] ]) # no sets of lists
1.1 Basic Language 17
Note that since Python 3.6+, keys can be used as set objects, as in the following,
>>> d = dict(one=1,two=2)
>>> {'one','two'} & d.keys() # intersection
{'one', 'two'}
>>> {'one','three'} | d.keys() # union
{'one', 'two', 'three'}
This also works for dictionary items if the values are hashable,
>>> d = dict(one='ball',two='play')
>>> {'ball','play'} | d.items()
{'ball', 'play', ('one', 'ball'), ('two', 'play')}
Once you create a set, you can add individual elements or remove them as follows:,
>>> s = {'one',1,3,'10'}
>>> s.add('11')
>>> s
{1, 3, 'one', '11', '10'}
>>> s.discard(3)
>>> s
{1, 'one', '11', '10'}
Remember sets are not ordered and you cannot directly index any of the constituent
items. Also, the subset() method is for a proper subset not a partial subset. For
example,
>>> a = {1,3,4,5}
>>> b = {1,3,4,5,6}
>>> a.issubset(b)
True
>>> a.add(1000)
>>> a.issubset(b)
False
And likewise for issuperset. Sets are optimal for fast lookups in Python, as in
the following,
>>> a = {1,3,4,5,6}
>>> 1 in a
True
>>> 11 in a
False
There are two primary looping constructs in Python: the for loop and the while
loop. The syntax of the for loop is straightforward:
18 1 Basic Programming
Note the colon character at the end. This is your hint that the next line should be
indented. In Python, blocks are denoted by whitespace indentation (four spaces
is recommended) which makes the code more readable anyway. The for loop
iterates over items that provided by the iterator, which is the range(3) list
in the above example. Python abstracts the idea of iterable out of the looping
construction so that some Python objects are iterable all by themselves and are just
waiting for an iteration provider like a for or while loop to get them going.
Interestingly, Python has an else clause, which is used to determine whether or
not the loop exited with a break3 or not.
>>> for i in [1,2,3]:
... if i>20:
... break # won't happen
... else:
... print('no break here!')
...
no break here!
The else block is only executed when the loop terminates without breaking.
The while loop has a similar straightforward construct:
>>> i = 0
>>> while i < 3:
... i += 1
... print(i)
...
1
2
3
This also has a corresponding optional else block. Again, note that the presence
of the colon character hints at indenting the following line. The while loop will
continue until the boolean expression (i.e., i<10) evaluates False. Let us consider
boolean and membership in Python.
Logic and Membership Python is a truthy language in the sense that things are
true except for the following:
• None
• False
• zero of any numeric type, for example, 0, 0L, 0.0, 0j.
• any empty sequence, for example, ”, (), [].
3 There is also a continue statement which will jump to the top of the for or while loop.
Other documents randomly have
different content
— Onko joku ollut kenraalin puheilla?
— Tekö?… Kiitos!
— On.
— Hyvä! Jos hän tulee yksin, niin heti hänet nähtyänne hyökätkää
hänen päälleen ja tukkikaa hänen suunsa. Voitte työntää hänen
oman lakkinsa hänen suuhunsa.
— Riippuu.
Ovi narisi, ja Kuklinowski tuli sisälle, mutta samassa kaksi rautaista
kouraa puristi hänen kurkkuaan ja tukahdutti hänen huutonsa.
Kosma ja Damian tottuneesti kuin ammattirosvot ainakin paiskasivat
hänet maahan, painoivat polvillaan hänen rintaansa niin että
kylkiluut rutisivat ja tukkivat silmänräpäyksessä hänen suunsa.
— Hevosten selkään!
He lähtivät riihestä.
Puolen tunnin kuluttua levisivät neljän ratsastajan ympärillä tyhjät
ja äänettömät kentät. He hengittivät raikasta ilmaa, jossa ei ollut
ruudin hajua. Kmicic ratsasti edellä, Kiemliczit hänen jäljessään.
Nämä puhelivat hiljaa keskenään, Kmicic rukoili itsekseen
aamurukouksia, sillä kohta alkoi päivä sarastaa.
— Kuinka? Ruotsalaistenko?
Mutta Mülleriä tämä tuuma miellytti. Hän tiesi, että luostarissa oli
eräs, tosin heikko, puolue, joka halusi antautumista, ja saattoihan
pelko levitä varusväen keskuuteen ja vallata nekin, jotka tähän
saakka olivat halunneet taistella viimeiseen asti.
— Kuklinowski on kuollut!
— Kuklinowski on tapettu!
— Niin! Tein ristinmerkin, sillä luulin, että tämä oli lumousta. Vasta
kun näin kolmen sotamiehen ruumiit, valkeni minulle asian oikea
laita. Tuo hirveä mies oli ne tappanut, ripustanut tämän orteen,
kärventänyt pahasti ja sitten poistunut.
Syntyi äänettömyys.
— Kyllä.
— Luotatteko sotamiehiinne?
— Entä Kuklinowski?
— Nostakaa peite!
Hurjaa vauhtia lähdettiin heti luostaria kohti, niin että lumi sinkoili
kavioista. Sata Sadowskin ratsumiestä yhtyi joukkoon ollakseen
Müllerin apuna. Tiellä he näkivät hajoitetun ratsuväkiosaston miehiä
epäjärjestyksessä pakenemassa. Niin olivat jo tähän saakka
verrattomat ruotsalaiset sotilaat menettäneet miehuutensa! Olipa
patteritkin jätetty oman onnensa nojaan, vaikka niitä ei uhannut
mikään vaara. Muutamia pakolaisia pistivät upseerit ja ratsumiehet
kuoliaiksi.
— Jos niin on, niin kirjoittakaamme heille, että eivät suotta meitä
säälisi, vaan räjähdyttäisivät!
— Entä se iso tykki! Kenen ansio on, että emme pelkää huomista
pommitusta?
— Pelastaa kunnia!
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com