2021 - Python For Absolute Beginners

Download as pdf or txt
Download as pdf or txt
You are on page 1of 158

Python

For
Absolute Beginners

Rocket through the basics in an


afternoon!

Oliver Theobald
First Edition
Copyright © 2021 by Oliver Theobald
All rights reserved. No part of this publication may be reproduced,
distributed, or transmitted in any form or by any means, including
photocopying, recording, or other electronic or mechanical
methods, without the prior written permission of the publisher,
except in the case of brief quotations embodied in critical reviews
and certain other non-commercial uses permitted by copyright law.
Find Us On:
Monthly Newsletter
http://eepurl.com/gKjQij
Enjoy book recommendations, free giveaways of future book releases from
the author, and other blog posts and news concerning machine learning,
trends, and data science.

Teachable
http://scatterplotpress.com/
For introductory video courses on machine learning.

Skillshare
www.skillshare.com/user/machinelearning_beginners
For introductory video courses on coding and video lessons from other
instructors.

Instagram
machinelearning_beginners
For mini-lessons, book quotes, and more!
TABLE OF CONTENTS
PROLOGUE
Practical Approach
About Python
Python 3
Resources
INSTALLING JUPYTER NOTEBOOK
FIRST STEPS
INPUTS AND OUTPUTS
1.1 Basic Arithmetic in Python
1.2 Comments
1.3 Spacing
1.4 Other Operators
1.5 Practical Challenge
VARIABLES
2.1 About Variables
2.2 The Equals Operator
2.3 Variable Naming Conventions
2.4 Variables Are Variable!
2.5 Using Print
DATA TYPES
BASIC DATA TYPES
3.1 Integers
3.2 Floating-point
3.3 Strings
3.4 Booleans
COMPOUND DATA TYPES
4.1 List
4.2 Tuples
4.3 Dictionaries
4.4 Sets
4.5 The Type Function
4.6 Unique Data Types
4.7 Summary
METHODS
5.1 index
5.2 Count
5.3 Different Methods
5.5 Methods Documentation
5.6 Summary
LISTS
6.1 Creating a List
6.2 Indexing
6.3 Negative Indexing
6.4 Indexing Subsets
6.5 Slicing
6.7 Updating Elements
6.8 Adding Elements
6.9 Removing Elements
6.10 Concatenating Lists
6.11 Checking Elements Exist
6.12 List Methods
DICTIONARIES
7.1 Dictionaries vs Lists
7.2 Creating a Dictionary
7.3 Dictionaries Inside Dictionaries
7.4 Dictionary Methods
7.5 Summary
TUPLES
8.1 Indexing and Slicing
8.2 Immutability
8.3 Tuple Methods
STRINGS
9.1 Inserting a Variable Value
9.2 The f String Method
9.3 Indexing & Slicing
9.4 Methods
SETS
10.1 Adding Elements
10.2 Removing Elements
10.3 The Set Function
FUNCTIONS & LOOPS
BASIC FUNCTIONS
11.1 Max
11.2 Min
11.3 Len
11.4 Round
11.5 Sorted
11.6 Function Arguments
COMPARISON OPERATORS
12.1 Making Comparisons in Python
12.2 The And Operator
12.3 The Or Operator
12.4 The Not Operator
CONDITIONAL STATEMENTS
13.1 The If Statement
13.2 The Else Statement
13.3 The Elif Statement
LOOPS
14.1 For Loops
14.2 Enumerate
14.3 Using a For Loop to Create a New List
14.4 While Loops
14.5 Range
14.6 Create Your Own Functions
14.7 Map
14.8 Lambda
14.9 Filter
PYTHON LIBRARIES
PANDAS
15.1 About Pandas
15.2 Converting to a Dataframe
15.3 Importing a CSV as a Dataframe
15.4 Other Pandas Functions
15.5 General Python Functions
15.6 Code Challenge
OBJECT-ORIENTED PROGRAMMING
INTRODUCTION TO OOP
16.1 What are Objects?
16.2 What is a Class?
16.3 Creating a Class
16.4 The Constructor Method
16.5 Assigning Class-level Values
16.6 Summary
INHERITANCE AND POLYMORPHISM
17.1 Inheritance
17.2 Adding Child Class Attributes
17.3 Polymorphism
17.4 Code Challenge
FURTHER RESOURCES
PROLOGUE
Recent years have witnessed accelerated demand for Python programming
skills and especially from companies operating in the fields of data science,
machine learning, and content aggregation.

Figure 1: % of Stack Overflow questions that month, Source: Stack Overflow

From a programmer’s perspective, Python has also overtaken Javascript as


the programming language of choice with the biggest percentage of user
questions submitted on Stack Overflow—the ‘Quora’ of coding. This trend
not only consolidates the dominant use of Python within the developer
community but also growing interest from new programmers
troubleshooting their code questions online.
The recent popularity and the long-term staying power of Python over
several decades can be attributed to several of its programming features.
The first feature is utility, as Python can be applied to everything from
backend web development to artificial intelligence. Second, Python offers a
broad range of code libraries that dramatically simplify the code needed to
perform complex tasks such as machine learning and web scraping. Third,
because the syntax is easy to read and write, Python is relatively
straightforward for beginners making their first steps in learning how to
code. Unlike other programming languages, for instance, Python typically
uses English keywords rather than punctuation symbols, which are easier to
read and remember.
Despite Python’s relative ease of use, mastering a new programming
language can be daunting and especially with regard to learning abstract
concepts such as object-oriented programming. For newcomers unsure
where to begin and in need of a gentle introduction, Python for Absolute
Beginners provides the perfect starting point. With the goal of making
Python more accessible and appealing to a wider audience, this step-by-step
guide is designed to teach you the basics without losing you in the first
chapter with lines and lines of complex code.
By the end of the book, you will have the skills, resources, and confidence
to code basic applications using object-oriented programming, create your
own loops, and define your own functions.

Practical Approach
This book takes a hands-on approach to learning Python. This includes
chapter quizzes, code examples, and free video resources to walk you
through the practical exercises explored in the book.

About Python
Designed by Guido van Rossum at the National Research Institute for
Mathematics and Computer Science in the Netherlands in the late 1980s,
Python was created to empower developers to write programs with fewer
lines of code than other programming languages. The language itself is
derived from the Unix shell command-line interpreter and other
programming languages including C and C++. It's also free, open-source,
and runs on all major platforms including Windows, Linux, Mac OS X, and
UNIX (including FreeBSD).
Python is an interpreted language, meaning that the Python interpreter reads
code and interprets one line at a time, thereby executing each command
sequentially. If the Python interpreter runs into any bugs along the way,
such as incorrect formatting, misspelled functions, or random characters left
somewhere in the code, it’s unable to perform the desired output. This leads
to what’s called an error. To avoid errors, it’s important to learn the basic
syntax and concepts of programming in Python to help you write fluid and
efficient code.

Python 3
In this book, we use the most recent version of Python, which is Python 3.
To practice the code examples and exercises contained in this book, you do
not need to download Python on your computer (although this step is
worthwhile for future learning and programming). Instead, each of the code
samples and exercises included in this book can be performed inside a user-
friendly development environment called Jupyter Notebook. Instructions
for downloading Jupyter Notebook are included on the next page.
If you wish to download Python 3 on your terminal (Mac) or command line
(Windows/Linux) for personal use, please go to python.org/downloads and
follow the steps.

Resources
To find the official documentation for Python, you can go to
https://docs.python.org/3/. You will also inevitably stumble on Stack
Overflow (www.stackoverflow.com) in your search for solutions to errors;
there you can find answers from other programmers explained in plain
English.

Free accompanying quizzes, notebooks, and video resources can be found


at: https://scatterplotpress.teachable.com/p/python-for-absolute-beginners
INSTALLING JUPYTER
NOTEBOOK
As the examples and practical exercises delivered in this book use Jupyter
Notebook as the chosen development environment for Python 3, this section
serves as a guide for installing Jupyter Notebook.

Jupyter Notebook is a popular choice for practitioners and online courses


alike, as it combines live code, explanatory notes, and visualizations into
one convenient workspace and runs from any web browser.
Jupyter Notebook can be installed using the Anaconda Distribution or
Python’s package manager, pip. As an experienced Python user, you may
wish to install Jupyter Notebook via pip, and there are instructions available
on the Jupyter Notebook website (http://jupyter.org/install.html) outlining
this option.
For beginners, I recommend choosing the Anaconda Distribution option,
which offers an easy click-and-drag setup
(https://www.anaconda.com/distribution/). This installation option will
direct you to the Anaconda website. From there, you can select your
preferred installation for Windows, macOS, or Linux. Again, you can find
instructions available on the Anaconda website based on your operating
system.
After installing Anaconda on your machine, you’ll have access to a number
of data science applications including rstudio, Jupyter Notebook, and
graphviz for data visualization through the Anaconda application.
For general programming in Python, we will be using Jupyter Notebook,
which you can select by clicking on “Launch” inside the Jupyter Notebook
tab located on the homepage.
Figure 2: The Anaconda Navigator portal

To initiate Jupyter Notebook, run the following command from the


Terminal (for Mac/Linux) or Command Prompt (for Windows):

jupyter notebook

Terminal/Command Prompt will then generate a URL for you to copy and
paste into your web browser.
Figure 3: Copy URL and paste into browser

Copy and paste the generated URL into your browser to access Jupyter
Notebook. Once you have Jupyter Notebook open in your browser, click on
“New” in the top right-hand corner of the web application to create a new
notebook project, and select “Python 3.”

Figure 4: Inside Jupyter Notebook

You’ve now successfully set up a sandbox environment in your web


browser using Jupyter Notebook. This means that the following
experimentation and code changes will not affect resources outside of the
isolated testing environment. Phew!

Figure 5: A new Jupyter notebook ready for coding


FIRST STEPS
1

INPUTS AND OUTPUTS


After setting up a development environment for Python 3, we are ready to
begin coding! Let’s start by exploring how input code is deciphered by the
Python interpreter to generate an output. The easiest way to demonstrate
the input-output relationship is to input basic equations and then run the
Python interpreter to solve each equation.

1.1 Basic Arithmetic in Python


Similar to inputting numbers and commands into a handheld calculator, we
can use Python to perform basic arithmetic including addition and
multiplication. Let’s start with using addition.

Example 1

After typing 2 + 1 as input, we can click on Run from the Jupyter Notebook
header toolbar, which will generate an output as Out. You can also navigate
to Cell > Run Cells from the header menu to run your code.
Figure 6: Using Run to execute an output

Just like a handheld calculator, the Python interpreter sums the two values
to produce an output, which as we expected, is 3

Figure 7: The output after clicking Run

1.2 Comments
In computer programming, it’s good practice to leave reminders for yourself
and others explaining the purpose of your code. This might involve
describing code functions, background context, or potential problems to
avoid.
To document code or simply share what you were thinking at the time of
writing it, you can insert what is called comments, which are read-only and
non-executable notes about your code.
In Python, you can add comments using the # (hash) symbol. Everything
placed after the hash symbol on that same line of code is treated as blank
space and will be ignored by the Python interpreter. Comments are typically
placed on the line above the code that we are trying to explain. However,
comments can also be inserted on the same line, next to the code the
comment is describing.
Keep in mind, too, that Python has two types of comments: single-line and
multi-line comments. Single-line comments start with a hash (#) and run
until the end of the line, while multi-line comments use triple quotes (""")
placed before and after the comment.

Example 2

In Example 2, the second line of code will be executed up until the hash
symbol, while the single-line comment written on the first line will be
completely ignored by the Python interpreter.
As mentioned, you can also add multiple lines of comments, which might
be useful for documentation when describing customized functions. To
write multiple lines of comments, you need to type in between the triple
quotes as shown in Example 3.

Example 3

1.3 Spacing
Regular spaces between punctuation, numbers, text, or code keywords are
mostly ignored by the Python interpreter. For instance 8 + 4 + 12 can also be
written as 8+4+12 . In Python, the output of these two equations is exactly the
same. However, to aid human eyes, you might like to use spacing within
your code for enhanced readability.
Spacing is also crucial for indentation. This is a special technique of
grouping statements to form a block of code. An indentation consists of
four consecutive spaces and can be created automatically using the Tab or
⇥ key (for Mac users) on your keyboard. Unlike single spaces, the Python
interpreter is extremely sensitive to incorrect indentation, which means
you’ll need to be careful to input the exact length of space and indent only
when required. We will explore indentation further in Chapter 11 for
writing basic functions.

Example 4

1.4 Other Operators


Beyond addition, Python can also be used to perform:
- subtraction
- multiplication
- division
- mod function (the value of the remainder after division)
- floor division (removes the remainder after the decimal point)
- exponent (raises the number to its left to the power of the number to
its right)
Each of these mathematical methods comes with its own unique operator.
An operator is a mathematical symbol; for multiplication the operator is *
and for division the operator is /. Examples of each mathematical method,
the operator, and an input/output example are displayed in the following
table.
Table 1: Mathematical operators in Python

In Python, we can also include more than one operator to solve an equation
as demonstrated in Example 5.

Example 5

After running the script, the Python interpreter generates an output of 5. To


solve this equation, Python followed the same sequence as standard
mathematics, with multiplication executed prior to addition.

Example 6

In this next example, the Python interpreter first divides 2 by 4 and then
adds 2 to produce an output of 2.5.
As with standard arithmetic, parentheses can also be added to modify the
sequence of operations as we can see in Example 7.

Example 7

Here, the Python interpreter sums 2 + 2 inside the parentheses and divides
that sum value by 4 to produce the final output of 1.0.

1.5 Practical Challenge


Let’s consolidate what we’ve learned so far with a practical challenge.
Suppose you have $10,000 to invest with a promised annual return of 4%.
This means that after 12 months, the initial investment (called the
‘principal’) will have grown to $10,400.
Task: Complete the code to calculate how much your investment will be
worth after 5 years of investing.
Hints: You will need to use the exponent operator (**). Remember to click
Run to execute your input code when ready.
SOLUTION

The answer is 12166.529024000001.


To solve this problem, we can multiply the principal amount of $10,000 by
1.04 (4% interest) and use the exponent operator to raise the amount to the
power of 5 (for 5 years).
2

VARIABLES
While solving mathematical equations can help us to familiarize ourselves
with inputs and outputs, and operating the Python interpreter, we’re not yet
utilizing the many awesome features that come packed in with Python.
Let’s unbox some of these features, beginning with the ability to create and
assign variables.

2.1 About Variables


Variables are a fundamental part of Python and many other programming
languages. In mathematics, a variable is something that varies, such as the
price of a product item or the height of a person. However, in computer
programming, the role of a variable is to store a defined value in the
computer’s memory for ongoing use. This enables earlier code to be saved,
referenced, and manipulated later down the line. Once you have created a
new variable name and assigned a value to it, you can ask the Python
interpreter to call the saved value by entering that unique variable name.
To create a variable in Python, you first need to type out a unique name and
assign it to a value using the equals (=) operator. For instance, using the
equals operator, we can assign the value 0.2 as our income tax rate and
50000 as our annual income.

Example 1
Now, anytime we refer to the variable tax_rate , the Python interpreter will
understand this as equal to 0.2. Likewise, anytime we refer to the variable
annual_income , the Python interpreter will understand this as equal to 50000,
as demonstrated in the next example.

Example 2

The Python interpreter recognizes the variable names tax_rate and


annual_income and automatically multiplies these variables based on their
assigned values (0.2 * 50000 = 10000.0).

2.2 The Equals Operator


In Python, it’s important to remember that the equals operator does not
perform the same role as an equals sign in mathematics. While the equals
operator is used in Python to assign a variable and save an associated value,
it is not connected to any mathematical logic.

Example 3

In the example above, the value 2 + 2 is stored in a variable called one .


This example shows that the variable name doesn’t have to be equal or even
relate to what’s packed inside the variable. However, it does make sense to
name your variables based on what value is contained inside. Similar to the
way you might pack boxes labeled “kitchen” and “garage” when moving to
a new home, naming conventions in Python help you to find and manage
variables more efficiently in the future.
If you don’t want to create a variable and instead want to solve a
mathematical equation in Python, you can run the code without adding an
equals operator as demonstrated in Chapter 1, i.e. 2 + 2 .
Also, if you want to confirm whether a mathematical relationship in Python
is True or False, you can use two equals operators (==) as seen here in
Example 4.

Example 4

2.3 Variable Naming Conventions


In terms of using the correct syntax for creating a variable name, there are a
few important rules to follow. The first rule is that variable names are case-
sensitive. This means that data and Data are two different variables. While
the two variable names are spelled the same, one is capitalized and the other
is not. As Python variables are case-sensitive, these two variable names are
therefore treated by the Python interpreter as two completely different
variables and cannot be used to refer to the same target value.
The second rule is the variable name cannot contain any spaces. This means
that to connect two words, you must always use an underscore (_) in
between, as seen in Example 5.

Example 5
In regards to the specific naming of a variable, you can select any name as
long as it fits with these three requirements:
The variable name contains only alpha-numeric characters and
underscores (A-Z, 0-9, _ )
The name starts with a letter or underscore and not a number
The variable name does not imitate a Python keyword such as
“return” (we will learn more about common Python keywords
including “print” and “class” in future chapters)

2.4 Variables Are Variable!


One of the great features of variables is their “variable” nature. Rather than
being immutable or unchangeable, we can modify and update the value
stored inside a variable simply by adding more code. (Note, however, that
this does not apply to tuples, which we will introduce in the next chapter.)
As a demonstration, let’s create two new variables called savings and
interest and assign these variables with the initial values of 10000 and 1.05
respectively.

Example 6

After establishing two new variables ( savings and interest ), let’s change the
value of the savings variable to 20000 and rerun the code to recalculate our
expected return on investment.
As we can see, the value for the variable savings has been modified, which
means the output value also changes accordingly (after re-running the
code).
The Python interpreter automatically takes the most recent (or last) line of
code as the final value. This means that you can modify the value of a
variable anytime by retyping the variable name and assigning it a new value
in order to override the previous value, as shown again in Example 7.

Example 7

Here, the value of the variable savings has been updated to 20000. Also, you
may have noticed that you can simply type the name of the established
variable and run your code to output the stored value of that variable.

2.5 Using Print


In Python, the print() function is used to specify an output, such as the value
of a variable or a text message (also known as a string). This ability to print
an output is extremely useful for formatting and customizing outputs. For
instance, if you create a variable and then run the Python interpreter to
output that variable, the interpreter spits out the saved value but there is no
context of what that output actually means and represents.
Let’s take a look at an example.

Example 8

While we can clearly see the output in this code example is Mike , it might
be useful to describe what this output represents. Using the print() function,
we can do just that by adding text, referred to as string, inside the
parentheses. In this case, we want to write a string (describing the output) as
well as the variable name (to output), and then merge the two objects using
the addition operator. Note also that the string must be wrapped inside quote
marks.

Example 9

As shown above, the print function is useful for providing context and
adding clarity by annotating important aspects of the outputted code—
especially as code comments cannot be shown as output. Without the print
function, all we would see is an unlabeled output value, which is even more
confusing if the end-user is unable to see or read your initial input code.
Note that when printing a string, the print function automatically removes
the quote marks after you run the code. If you wish to include quote marks
in the output, you can add single quote marks inside double quote marks as
shown here.
Example 10

To print on a new line, we can use \n , and for indentation (four spaces), we
can use \t .

Example 11
2.6 Code Exercise
A fun and easy way to practice assigning variables and running Python
commands is to play with a special code library called Turtle. A library is a
collection of functions used for performing set tasks in a specialized domain
such as web crawling, machine learning, or visualization.
Similar to a rudimentary computer or mobile game like Snake, the Turtle
library enables you to create vector graphics using simple commands while
simultaneously controlling the movements of an interactive on-screen turtle
(in the form of a simple arrow). The ability to see each code output visually
on the screen makes Turtle an excellent tool for understanding inputs and
outputs and especially if you want to teach Python to kids in the future.
To access the Turtle, type the following command into your notebook to
import the Turtle library.

Step 1

Next, give the turtle a name by assigning a variable with the name you wish
to give your turtle. After the equals operator, type the library name turtle
with a dot afterward, followed by the Turtle() function.

Step 2
In this code example, the turtle’s name is ninja . Now, we can start telling
our turtle what to do using the following commands:

.forward()
.back()
.left()
.right()

Inside each of these commands, we can insert a number inside the


parentheses to tell the turtle how many pixels to move, such as .forward(100)
to move 100 pixels forward.

Step 3
Let’s run through the full code script using a combination of the moves
listed in Step 2.
At the end of your code, you can add turtle.done() to close the code.

Step 4
Let’s now click Run to execute our code, which will automatically open a
pop-up graphical interface. Now we can watch on as the Python interpreter
renders the commands to move our turtle around on the screen.
Step 5
Beyond moving forward, back, left, and right, we can also change the color
of our turtle using .color() and then inserting the name of a color as a string
inside the parentheses.

Now it’s your turn to create your own variable! This includes giving it a
variable name, your favorite color, and telling it where to go. To start
out, try moving your turtle so it forms a square using a combination of
moves.

To explore further commands in Turtle, you can check out the


documentation via the link below.
Link: https://docs.python.org/3.3/library/turtle.html
QUIZ
1) Which of the following is correctly formatted as a Python variable?
a. bank account = "lots of money"
b. bank_account = 5566.90
c. print = "lots of money"
d. 5566.90_total = 5566.90

2) Update the variable bank_account to 3000 using the most efficient


method
a. bank_account = +3000
b. bank_account = 5566.90 – 2566.9
c. bank_account = 3000
d. bank_account == 3000

3) Which of the following code commands will print the output across
two lines of code?
a. bank_account = print("Bank account:\n3000")
b. bank_account = print("Bank account:/n3000")
c. bank_account = print("Bank account:/t3000")
d. bank_account = print("Bank account:\t3000")
SOLUTIONS
1) Which of the following is correctly formatted as a Python variable?
a. bank account = "lots of money" # Variable names cannot contain spaces
b. bank_account = 5566.90 # CORRECT
c. print = "lots of money" # Variable names cannot use a fixed Python keyword
d. 5566.90_total = 5566.90 # Variable names cannot start with a number

2) Update the variable called bank_account to 3000 using the most


efficient method
a. bank_account = +3000 # Invalid code
b. bank_account = 5566.90 – 2566.9 # This works but is not the most efficient
method
c. bank_account = 3000 # CORRECT
d. bank_account == 3000 # Not the command we’re looking for

3) Which of the following code commands will print the output across
two lines of code?
a. bank_account = print("Bank account:\n3000") # CORRECT
b. bank_account = print("Bank account:/n3000") # Incorrect, forward slash
c. bank_account = print("Bank account:/t3000") # Incorrect
d. bank_account = print("Bank account:\t3000") # Prints an indent
DATA TYPES
3

BASIC DATA TYPES


In the previous chapter we talked about assigning numerical values to a
variable using the equals operator. Using this exact same technique, we can
also assign other data types to a variable, including strings, lists, sets, and
tuples. We’ll explore each of these data types later in this section, but for
now, let’s define each of the basic data types beginning with numerical
values, which come in two forms: integer and floating-point.

3.1 Integers
The first type of number is an integer, which is a whole number that doesn’t
contain a decimal point, i.e. 8, 88, and 1.

Example 1

3.2 Floating-point
The second type of number is a floating-point number or float, which is a
number holding a decimal point, i.e. 7.7, 77.0, and 0.7. Percentages can also
be described using floating-point numbers, i.e. 0.8 (=80%).

Example 2
Let’s now take a look at other common data types, beginning with strings.

3.3 Strings
A string is a sequence of characters including alphabetical characters,
punctuation symbols, or even numbers (with no mathematical significance).
In Python, strings use the syntax of either double( " ) or single( ') quote
marks. A mixture of single and double quote marks won’t work, such as
starting a string with a single quote and ending with a double quote.

Example 3

The string created in this example is a combination of words, spaces, and


numbers. As this data type is formatted as a string (as defined by the use of
quote marks), the number 31 has no mathematical meaning and cannot be
processed using the mathematical operators we learned in Chapter 1.
Below is an example of a string and integer. Note the use of quote marks in
the case of the string.

Example 4
3.4 Booleans
A Boolean is a True or False value, which in everyday language simply
means “Yes” and “No”. Booleans are primarily used as comparison
operators to compare two elements, as will be discussed in Chapter 12.

Example 5

As 1 is not equal to 2, the output is False .

Example 6

As 1 is less than 3, the output is True .

Example 7
As these two strings are not the same, the output is therefore False .
QUIZ
1) Which of the following variable values is a floating-point number?
a. bank_account = 5000
b. bank_account = 5566.9
c. bank_account = ' lots of money '
d. bank_account = 5

2) The output of 9 < 10 is:


a. False
b. True
c. No
d. Yes

3) A string must have what syntax qualities?


a. quote marks
b. a mixture of single and double quote marks
c. alphabetical characters, numbers, punctuation symbols, spaces
d. capitalization
SOLUTIONS
1) Which of the following variables is a floating-point number?
a. bank_account = 5000 # Integer
b. bank_account = 5566.9 # CORRECT
c. bank_account = ' lots of money ' # String
d. bank_account = 5 # Integer

2) The output of 9 < 10 is:


a. False
b. True # CORRECT
c. No
d. Yes

3) A string must have what syntax qualities?


a. quote marks # CORRECT
b. a mixture of single and double quote marks # Invalid
c. alphabetical characters, numbers, punctuation symbols, or spaces #
CORRECT
d. capitalization # Not strictly necessary
4

COMPOUND DATA TYPES


Having looked at basic data types including numerical values (integer and
floating-point), strings, and Booleans, let’s proceed to compound data types,
which can group multiple values inside a single data structure.

4.1 List
The most common compound data type is a list, which is an ordered
collection of values (also called “elements”). These values can be any type,
including integers, floating-point numbers, strings, Booleans, or even other
lists to form a nested list. This means that a list can contain any Python data
type and the list itself is a Python data type too.
A list is denoted in Python by the use of square brackets [ ] and individual
values inside the list separated by commas.

Example 1

The list in Example 1 holds three integers separated by commas. Although


not as common, a list can also contain different data types such as strings
and integers.

Example 2
4.2 Tuples
Similar to a list, a tuple is an ordered sequence of values but unlike a list,
the values contained in a tuple are immutable and cannot be manipulated.
Special thought and a specific function are required to override the existing
data, which guarantees the integrity of the data by preventing accidental
changes in complex pieces of code. You can therefore use a tuple when you
want to make your code safer or write-protect by preventing others from
changing the stored sequence of values.
Tuples are also useful if you're working with a constant set of values that
don’t need to be updated and you simply need to iterate through those
values using a loop (discussed in Chapter 14) .
Tuples are denoted by the use of parentheses and the values contained
inside the tuple are separated by commas.

Example 3

Example 4

4.3 Dictionaries
The next compound data type is a dictionary, which is a collection of one or
more key-value pairs. This data type allows the storing of an individual
value under its own key name. The key consists of a string such as a name
and is linked to a value such as a number or a string.
The key and value are linked using a colon and are nested inside quote
marks and curly brackets.

Example 5

In this example, the name of the key is mike and the value is 50 (which is
mike’s age). Using this key-value pair, we can now access Mike’s age by
looking up the value of mike inside square brackets and running the code.

If you have multiple key-value pairs in the same dictionary, you can
separate each key-value pair using a comma with all key-values enclosed in
curly brackets.

Example 6

In the example above, mike is the first key name and 50 is the value; the
second key name is sara and the value assigned to sara is 60 .

4.4 Sets
Sets are a collection of unique unordered objects. Sets are housed inside
curly brackets with each value separated by a comma.
Example 7

It’s important to note that a set can only contain unique elements. This
means that if we try to include multiples of the same element, the set will
automatically reduce the data to its unique elements.

Example 8

As shown, the output of the variable data is reduced to the three unique
elements, despite the original five elements initially placed inside the set.

4.5 The Type Function


In Python, you can check the type of a variable using what’s called the
type() function with the name of the variable inserted inside the parentheses.

Example 9
As seen in Example 9, this function allows you to quickly check the data
type of any variable.

4.6 Unique Data Types


Data types in Python not only look different but behave differently too.
When you sum two integers using the addition (+) operator, the Python
interpreter sums those two values, but when you place the addition operator
in between two strings, the strings are pasted together to form one larger
string.

Example 10

In the first code example, the + operator performs addition, and in the
second example, it does what’s called concatenation, which is the action of
merging strings together.
Concatenation is often used for generating custom messages as shown in
Example 11.

Example 11
Note that if you attempt to place the equals operator between a string and
another data type (such as an integer), this will immediately lead to an error.
This is because you can’t merge a string with a non-string. We can see this
error occurring below when 10000 and 11000 are expressed as integers
(without quote marks) instead of strings (as they were in Example 11).

Example 12

An error will also occur if you try to merge a string directly with a non-
string as seen in Example 13.

Example 13

The code example above cannot run as the input code is attempting to
merge strings with two variables whose data type isn’t compatible. To
resolve this problem, we can describe the variables as a string using str() .

Example 14

By describing the values of 10000 and 11000 as strings, the Python


interpreter is able to successfully concatenate all four strings.
Similar functions such as int() for integer, float() for floating-point number
and bool() for Boolean can also be used to specify or convert Python values
into a designated data type.

4.7 Summary
We have explored a range of different data types including numbers (integer
and floating-point), strings (text), and Booleans (True/False). In addition to
these basic data types, there are also compound data types that can store the
same data type as an internal element (i.e. a list within a list) or a mixture of
data types (i.e. strings within a list).
Compound data types include lists, tuples, sets, and dictionaries. Lists,
tuples, and sets are similar, except that sets contain only unique values and
tuples are immutable. Dictionaries, meanwhile, are structured as a key-
value pair, which makes it easier and more intuitive for retrieving
information.
Table 2: Summary of data types

Also, to check a data type in Python, you can use the type() function with
the name of the variable placed inside the parentheses.
Lastly, data types behave differently in relation to mathematical operators
and other input code (as covered in later chapters). To avoid compatibility
issues between code commands and a certain data type, you may need to
describe or even convert the data type using a description such as str() , int() ,
bool() , or float() .
QUIZ
1) Which of the following is a list?
a. bank_accounts = { " mike " ,1000, " sara " ,6000}
b. bank_accounts = [ " mike " ,1000, " sara " ,6000]
c. bank_accounts = { " mike " :1000, " sara " :6000}
d. bank_accounts = ( " mike " ,1000, " sara " ,6000)

2) Concatenation is:
a. a method of merging data types
b. a method of converting an integer to a variable
c. a method of merging strings
d. a mathematical operator

3) Complete the following code: print("My bank account has $" + __(10000))
a. int
b. str
c. bool
d. float
SOLUTIONS
1) Which of the following is a list?
a. bank_accounts = { " mike " ,1000, " sara " ,6000} # Set
b. bank_accounts = [ " mike " ,1000, " sara " ,6000] # CORRECT
c. bank_accounts = { " mike " :1000, " sara " :6000} # Dictionary
d. bank_accounts = ( " mike " ,1000, " sara " ,6000) # Tuple

2) Concatenation is:
a. a method of merging data types
b. a method of converting an integer to a variable
c. a method of merging strings # CORRECT
d. a mathematical operator

3) Complete the following code: print("My bank account has $" + __(10000))
a. int
b. str # CORRECT
c. bool
d. float
5

METHODS
Alongside their ability to store information assigned to a variable, data
types also come with their own built-in operations and functionality in the
form of methods, which take place at the element-level. By element-level,
we are referring to individual values of a data object. A single string, for
example, is one object consisting of numerous elements in the form of
letters, numbers, spaces, and punctuation symbols. Meanwhile, a list storing
three individual numbers holds each as one element, with three elements in
total.
Each data type has its own unique methods for manipulating objects and
methods will therefore vary with each data type. Let’s first look at two
common methods that you might use with a list or a set.

5.1 index
Index is a method of finding the position of an element in a sequence of
elements contained within a list or set. To use this method, we have to call
the method .index() after a variable name using dot notation (a dot in
between the variable name and the method).

Example 1
Here, the .index() method tells us that silver is indexed at position 2 in the
list. Note that positional indexing in Python starts at 0. This means that
"gold" is indexed at position 0, "iron_ore" at position 1, and "silver" at
position 2.

5.2 Count
The next method, .count() , tallies the number of times an element occurs
within a list or a set. Again, we must use dot notation after the variable
name to execute this method.

Example 2

As the element "silver" only appears once in the list, the output is 1.

5.3 Different Methods


As mentioned earlier, methods can change depending on the data type. So
far, we’ve looked at methods that apply to sets and lists, but tuples,
dictionaries, and strings also have their own built-in methods.
With a string object, for instance, we can use the capitalize() and replace()
methods. The capitalize method returns a string with the first letter
capitalized and the replace() method replaces part of the string with a new
element.

Example 3
Example 4

We’ll explore more methods for strings and other data types in the coming
chapters as we dive deeper into managing different data types.

5.5 Methods Documentation


To look up all the available methods for a given data type in Jupyter
Notebook, you can press the “Tab” or “ ⇥ ” key (for Mac users) on your
keyboard.
Figure 8: A reference list of the available methods for a list data object inside Jupyter
Notebook

First, though, you need to create a new variable and assign it a data object
such as a list or a dictionary. Next, type the name of the variable with dot
notation (insert a dot after the variable name) and hit the Tab or ⇥ key on
your keyboard to activate a dropdown list of available methods.
You can also select a method from the list and the method will be instantly
pasted into your code script.
Also, if you’re unsure of how to use a method, you can apply the help()
function with the name of the variable and the method placed inside the
parentheses using dot notation and then click on Run to output the
documentation.

Figure 9: Documentation for using the count method, which returns the number of occurrences
of a value

5.6 Summary
To summarize, everything stored in a data object is an element and elements
can be inspected or rearranged using methods. The methods available vary
depending on the data type of the object. The capitalize method, for
example, can be used with a string object but not with a list object.
Finally, it’s important to remember that some methods can be used with
different data object types using the exact same method name. The index
method, for example, can be used with lists, strings, and sets. When used
with a list or a set, the Python interpreter returns the index position of the
element, and in the case of a string, it returns the index position of the
element in the string. This also means that we need to be wary of methods
behaving slightly differently depending on the data type.
6

LISTS
Having learned about different data types and the functionality of methods,
let’s now explore the common use cases of a list and special techniques
including indexing and slicing.

6.1 Creating a List


Let’s begin by creating five variables containing the price of five stocks you
want to buy. The name of each variable will be the name of the stock and
the price will be represented as a floating-point number.

Example 1

Next, let’s convert these five variables into a list and save them as a new
variable called stocks . We can do this by first typing the name of each stock
as a string (wrapped in quote marks), and then on the right, type the name
of the variable. To see the output of the newly created list, type print(stocks )
and run the code.
After running the code, the Python interpreter prints a list containing the
name of each stock as a string alongside the numerical value assigned to
each variable.
Rather than storing all stock names and prices as individual elements, let’s
now create a series of nested lists to group the name and value of each
individual stock as a sub-list.

Now if we print stocks , each variable name and value has its own nested
list, which will make it easier to retrieve and manipulate individual stocks
in the future.

6.2 Indexing
Let’s say that we want to retrieve one stock from our list of nested lists, but
we don’t want to output the entire list. To solve this problem, we can use
indexing, which is a technique of selecting and outputting a single element
from within a data type, such as a list or string. Note that this technique is
slightly different from the index method learned in the previous chapter.
Rather than identifying the index position of an element, this technique is
used for extracting a single element.
Data elements can be indexed by calling the index position number inside
square brackets after the variable name. Again, note that each element
contained within the data object is numerically indexed starting at 0.
Using the list we created called stocks , let’s run through some examples of
indexing.

Example 2

In this example, the output is the object in position one of the list, which is
the Apple list. While Apple is actually the second nested list inside the list
stocks , indexing starts at 0 and not 1, meaning the Python interpreter goes to
the second element positioned at index 1.

6.3 Negative Indexing


Indexing can also be performed using negative indexing from right to left.
Let’s try to find the exact same nested list as the previous example using
negative indexing.

Example 3
This time, the Python interpreter indexes the list from the right to find the
exact same nested list (now at index -4).

6.4 Indexing Subsets


To restrict the output to a subset, such as the value of Apple’s stock and not
the name, we can use a chain of square brackets. The first square brackets
refer to the index position of the nested list, and the second square brackets
refer to the index position within that nested list.

Example 4

Using a chain of square brackets, this time we only see the value of Apple’s
stock as output.

6.5 Slicing
Rather than pulling a single element from a data type, we can use slicing to
output multiple elements using a colon (:) in between the start and end
points. Slicing will begin at the start point and go up to but not collect the
element positioned at the end point.

Using slicing, let’s try extracting multiple nested lists from our original
stocks list.

Example 5

In this example, slicing, starts at index 1 and goes up to index 2 but does
not include the element at index 3, thereby returning the nested lists for
Apple and Tesla.
If you don't specify the start of the splice, the Python interpreter assumes
that you want to start slicing from zero (the beginning of the list). Likewise,
if you don't specify the end point, the slicing will go all the way to the last
element of your list.

Example 6
Example 7

6.7 Updating Elements


To update an element within a list, we can also use indexing and/or slicing
to manipulate and modify the list. To show how this is done, let’s update the
stock value of Boeing to 300 using indexing.
To make this update, we first need to find the index position of the nested
Boeing list [4]. Then, using the second set of square brackets, we can
specify the index position of the stock price [1] within the nested list.
Finally, using the equals operator, we can update its value to 300, i.e.
stocks[4][1] = 300

Example 8
After printing the element indexed at position four of the list, we can see the
updated nested list and the value of the Boeing stock updated to 300.

6.8 Adding Elements


Let’s say a new IPO (initial public offering) brings a new stock onto the
market and we want to add it to our Python investment portfolio. We can
add that stock to our list by using the addition (+) operator to append a new
nested list to the original stock list. Note the following use of double square
brackets to add a new nested list.

Example 9

As shown, this code adds the new list ["Coinbase", 297.64] as a nested list to
the existing list.

6.9 Removing Elements


Similar to adding elements, we can also remove elements using the del()
function and stating the index position of the nested list. To demonstrate,
let’s remove Coinbase from our stock list in light of a troublesome IPO.

Example 10

With just one line of code, we were able to successfully remove this nested
list by using indexing to specify the nested Coinbase list (indexed at
position 5 within the stocks list).
Pay careful attention after adding or removing an element, as the index of
specific elements can change. For example, index -1 is now the Boeing list,
but in the previous example, index -1 was the Coinbase list.

6.10 Concatenating Lists


As with concatenating a string, we can also use the addition operator to
merge one or more lists together.

Example 11

6.11 Checking Elements Exist


To check if an element is contained inside a variable such as a list, you can
simply query that element using in .
Example 12

6.12 List Methods


Carrying on from the introduction of list methods in the previous chapter,
including .index() and .count() , let’s review two more list methods starting
with .pop() . Remember, methods use the dot notation next to the name of
the variable.

Pop
The .pop() method outputs the last object from a list.

Example 13

You can also use an index number to pop a specific object from a list.

Example 14
This will pop the first item from the stocks list.

Append
This second list method, .append() , allows you to add any item to the end of
an existing list.

Example 15
7

DICTIONARIES
Dictionaries, as we will explore in this chapter, provide an alternative
method for storing multiple objects within a single data structure.

7.1 Dictionaries vs Lists


To understand the benefit of using a dictionary, let’s begin by looking at the
capital and country lists seen here in Example 1.

Example 1

The data in its current form is spread across two lists, making it
cumbersome to retrieve related information. For example, to print the
capital and country of the third country we need to refer to index positions
in both lists.
Moreover, a potential problem arises when you have many elements, as you
need to know the exact index number of the element you wish to index. A
better way to store and look up related information is to store those values
in a dictionary, which is a collection of one or more key-value pairs.
For demonstration purposes, let’s convert our two lists into a single
dictionary.

Let’s now find the capital of Germany using square brackets and the key
name "Germany" .

This approach of storing related variables in a dictionary is not only


intuitive but also efficient from a coding perspective. Using a dictionary, it’s
must faster to type the key name of a key-value pair than it is to track down
the index number of a list in order to locate a given element.

7.2 Creating a Dictionary


The format for creating a dictionary is as follows:

my_dict = {"key1":"value1","key2":"value2"}

Key names and their values don’t necessarily have to be strings, they can be
numbers too. Each key name, though, must be unique. If you try to add
more than one value with the same key name, only the last value that you
specified is stored as part of the key-value pair. Also, the key cannot be a
list because this type of object is mutable (meaning that it can be modified
once created). As a rule, a key must be immutable, which means the key
cannot be changed once created. Objects that are immutable and can be
used as a key include strings, Booleans, integers, and floating-point
numbers.
Now that we know how to build a dictionary and access the key-value pair
using square brackets, let's learn how to update an existing dictionary. To
perform this action, all we need to do is type the dictionary’s key name
inside quote marks and square brackets and assign the new value using the
equals operator.

Example 2

This will update the key’s original value from Berlin to Frankfurt. As each
dictionary key is unique and immutable, Python recognizes that you are not
creating a new pair but rather updating the existing key-value pair.
You can also remove an object from the dictionary using the del() function
and specifying the value you want to delete.
Example 3

As shown in the output above, the key-value pair Germany has been
successfully removed from the dictionary.

7.3 Dictionaries Inside Dictionaries


Similar to how a list can contain other lists, dictionaries can also contain
nested dictionaries.

Example 4

To retrieve an object, we now need to use a chain of square brackets to


select objects. To find the GDP for Germany, for example, we need to enter
world_cities["Germany"]["gdp"] .
7.4 Dictionary Methods
Using this first method, we can use .keys() to look up the keys of a
dictionary.

Example 5

Alternatively, we can also use the .values() method to look up the value of
each key-value pair.

Example 6

7.5 Summary
As noted at the top of the chapter, dictionaries share similarities with lists
including the use of square brackets to select and update elements.
However, a list is a sequence of values that are indexed using numbers. A
dictionary, on the other hand, is indexed using unique keys.
In general, a list is used when the order of values matters and you want to
be able to easily select subsets of the data. A dictionary, meanwhile, is
useful when you want to quickly look up specific values using keywords
like a lookup table.

Table 3: Comparison of dictionaries and lists


8

TUPLES
The next compound data type, tuples, are very similar to lists except for the
fact they are immutable, which means we can’t reassign elements as we can
with a list. Also, rather than use square brackets, tuples use parentheses.
As you will see in this chapter, some of the list methods and techniques also
apply to tuples.
Let’s start with indexing and slicing.

8.1 Indexing and Slicing


Exactly as you would with a list, indexing and slicing can be applied to
extract elements contained inside a tuple.
Let’s begin with indexing in order to select the first unique element from a
tuple.

Example 1

Using indexing, we are able to extract the element positioned at index


position 0, which is "Amazon" .
Next, let’s use slicing to extract the first three elements.

Example 2

As we did with a list, we can use the same code here to output the first three
elements from our tuple.

8.2 Immutability
Next, let’s explore the feature of immutability. As mentioned, elements
assigned to a tuple cannot be manipulated. This means that if we attempt to
replace an existing element using indexing, we are greeted with an error
message.

Example 3

The error message informs us that because a tuple object does not support
item assignment, we cannot replace the existing element at index 0 with a
new value.
However, this technique of replacing an element using indexing would
work if applied to a list object as seen in Example 4.

Example 4
The fact that tuples are immutable means there are fewer methods available
than with lists. Also, unless there’s an explicit need for your data to be fixed
and immutable, you will often use a list rather than a tuple (given the
broader range of available methods).

8.3 Tuple Methods


As with a list, the count method allows you to tally the total number of
instances of a single unique element using .count() .

Example 5

Here, the count method tallies the number of False elements, which in this
tuple is 2.
Lastly, let’s apply the index() method to look up the index position of 3.

Example 6
9

STRINGS
While they are not a compound data type, strings come with a broad range
of methods and management options as we will explore in this chapter.

9.1 Inserting a Variable Value


In Python, you will often need to insert a variable value into your string for
printing a message as shown in Example 1.

Example 1

The variable name is used to insert a pre-defined name into the print
message. However, we can also customize the name input using the .format
method. Rather than creating a fixed variable, we can use curly brackets {}
as a placeholder for the variable value to be inputted. The input value can
then be added or updated inside the dot format parentheses.

Example 2
As shown, using the .format() method and open parentheses, we can
customize the string output with one line of code.
To add more than one variable, we can apply the same method by simply
inserting more open parentheses and adding variables to fill inside .format() .
The variables should be separated by commas and are filled sequentially
with the first variable filling the first open parentheses, and the second
variable filling the second open parentheses, as shown in Example 3.

Example 3

Alternatively, you make like to use keywords to specify the position of the
inputs, which is useful if a variable has to be added more than once.

Example 4

By using keywords for each unique variable input, we can use variable b
(“$100”) multiple times, while also minimizing code input, as we only need
to type .format() once at the end of the string.
Another similar technique is to insert each variable by index position using
index numbers inside the parentheses.

Example 5

As the variables “Tom” and “$100” can be indexed by their position, 0 and
1 respectively, we can insert the relevant index number inside the
parentheses so as to generate our custom message.

9.2 The f String Method


Another way to insert variables into a string is to use f string method (also
known as f string literal), which can be used to format strings and is
instantly recognized by the use of f directly before the string.

Example 6

Using the f method, we can insert a variable name from elsewhere in the
code by calling the variable name inside curly brackets.

Example 7
9.3 Indexing & Slicing
As strings are ordered sequences, we can use indexing and slicing to select
and output elements of the string. Note that strings, like tuples, are
immutable. This means that strings can’t be mutated or changed using
indexing to change individual elements as can be done with lists.
As we did with a list, we can use the square brackets [] after the variable
name to grab a single element from the string. The following example is a
quick demonstration of indexing.

Example 8

The element at index [1] is "o".


For strings, slicing can be done in much the same way as indexing, except
that we need to add the start and end point. Again, pay attention to the end
point, as the Python interpreter will not capture the element at that
designated end point.

Example 9
Using splicing, we can select the first two elements of the string, with the
starting point left to the default of 0 and the endpoint set to 2. This means
that the element at index position 2, which is m, will not be outputted by the
Python interpreter.

9.4 Methods
Let’s now look at the methods available for strings. In addition to
.capitalize() and .replace() discussed in Chapter 5, other popular methods
include .split() , .upper() , and .lower() . Let’s start with split, which is useful
for analyzing text as part of natural language processing.
The split method works by splitting everything based on spacing or another
argument you set inside the parentheses. By default, split will separate parts
based on spaces.
Example 10

As we left the parentheses empty, the split method automatically outputs


everything between the spaces as an individual element of a new list.
If we were to make the split by a hashtag, then we would see this.

Example 11

This code splits the original string based on the position of the hashtag.
Note, also, that the hashtag on the second line must be wrapped in quote
marks, otherwise the Python interpreter will assume you are writing a
comment (to be ignored).
Next, let’s look at the upper and lower methods to change the casing
(capitalization) of a string.

Example 12

The upper method transforms the original string into full upper casing. The
lower method, meanwhile, transforms the entire string into lower casing.

Example 13
10

SETS
Sets, as explored earlier, are unordered collections of unique elements and
are recognizable by their use of curly brackets. The ability to reduce data to
its unique elements is a key feature of a set and is useful for manipulating
data such as removing duplicates.

10.1 Adding Elements


To add elements to a set, we can use the .add() method.

Example 1

However, if we try to add another identical element, we will see no update


to the contents of the original set.

Example 2
As shown in Example 2, the add method is unable to add an additional
element. This is because an element of the same value already exists in the
original set.

10.2 Removing Elements


We can also remove elements from a set using the built-in .remove() method.

Example 3

10.3 The Set Function


Lastly, a set can also be used as a function to reduce a list to its unique
elements.

Example 4
The set function, here, converts the list with five elements into a set stripped
to its three unique elements. Naturally, if you try to add the exact same
element again, the Python interpreter will ignore the request as each
element of the set must be unique.
FUNCTIONS & LOOPS
11

BASIC FUNCTIONS
Separate from methods, Python also offers what are called functions, which
work as reusable code aimed at solving specific tasks. Instead of having to
write lots of code yourself, you can call functions to do the work for you!
Note that methods use dot notation, take place at the object-level and are
available based on the data type (i.e. string, list, etc), whereas functions use
parentheses, take place at the variable-level, and are not restricted to a given
data type.
Examples of functions were briefly introduced in an earlier section with the
use of str() , int() , bool() , and float() for specifying or converting data types,
as well as the print() and type() functions. Unlike a method, the variable
name is placed inside the function’s parentheses.

Example 1
Common Python functions including max() , min() , round() , sorted() , and
len() . Let’s explore each of these functions one by one.

11.1 Max
By inserting the name of a variable inside parentheses, this function outputs
the highest numerical value contained inside that variable.

Example 2

11.2 Min
The min() function is the opposite of the max() function. It outputs the
lowest numerical value contained inside a data object.

Example 3

11.3 Len
This third function outputs the length or quantity of elements in a data
object.

Example 4
We can also check the length of a string in characters (including spaces and
punctuation) using the same len function.

Example 5

11.4 Round
This next function is slightly more complex in that there are now two
inputs. The first input is the number you want to round and the second input
is how precise you want the rounding (i.e. to one decimal place or two
decimal places). Note too that these two inputs must be separated by a
comma.

Example 6

In this example, we instructed the Python interpreter to locate the element


that we wanted to round (11.36) and then stated the number of decimal
placings, which was 1 decimal place.
If you want to round a floating-point number to an integer, you don’t need
to add the second input, as shown in Example 7.
Example 7

As we didn't specify the second input, the Python interpreter automatically


rounds the number to the closest integer, which is 11.

11.5 Sorted
Lastly, sorted() is used to sort a sequence of numbers into a logical order,
from lowest to highest.

Example 8

11.6 Function Arguments


Functions can also come with optional arguments, such as reverse in the
case of sorted() .

Example 9
12

COMPARISON OPERATORS
As a programmer, you will often need to set defined conditions for your
code in order to define an output based on different input data. You can
achieve this in Python by using comparison operators.

Table 4: Comparison operators

Comparison operators allow you to compare two values and output a


Boolean value to summarize the relationship as True or False.

12.1 Making Comparisons in Python


Let’s take a look now at each of the six comparison operators, as shown
earlier in Table 4.

Examples 1-3
Next, to check if two values or variables are equal, you can use two equals
operators (==), or check for inequality using !=.

Examples 4-7
In addition to integers, floating-point numbers, and Booleans, it’s also
possible to compare strings using comparison operators, such as == and !=.
To compare strings as less than or greater than, the Python interpreter
determines that relationship based on alphabetical order (from A to Z) in
relation to the first letter of the string as shown in the following example.

Example 8

China is deemed as less than Japan because the first letter of China (C)
comes before the first letter of Japan (J) in alphabetical order.
Also, keep in mind, that in order to make a comparison in Python, you must
be comparing objects of the same data structure. For example, you can’t
compare an integer with a string using a comparison operator.

Example 9

In Example 9, the Python interpreter produces an error message after we


attempted to compare an integer to a string.
Next, to express equal or less than and equal or greater than, we can
combine the less than (<) and greater than (>) operators with an equals
(=) operator. Pay super special attention to the order, as the equals operator
must be placed after the less than/greater than symbol and not before!

Example 10-11

Another way to make comparisons in Python is to compare Booleans using


the and , or , and not Boolean comparison operators.
Table 5: Boolean comparison operators

12.2 The And Operator


The and operator compares two Booleans and returns True if both values
are True. This means that if one or both values are False, the and operator
returns False.

Example 12

The and operator can also be used to compare the outcome of two different
comparisons using one or more variables.

Example 13

As both x > 3 and x < 5 are True, the output of the two comparisons is True.
12.3 The Or Operator
In the case of the or operator, at least one of the Booleans must be True.
This means that True or True , False or True , and True or False each output as
True. Meanwhile, only False or False results in False.

Example 14

As with the and operator, you can also use the or operator to compare
variables.

Example 15

In this example, the presence of at least one True statement ( y < 10 ) is


enough to give a True output.

12.4 The Not Operator


Thirdly, we have the not operator which negates a Boolean. This operator
is typically used for combining different Booleans and then negating the
result.

Example 16
13

CONDITIONAL STATEMENTS
In the previous chapter, we established the role of both standard comparison
and Boolean comparison operators. Let’s now explore how to combine
comparison operators with conditional statements to define the flow of our
Python script.
The conditional statements we will examine in this chapter are if , elif , and
else .

13.1 The If Statement


Anytime the Python interpreter encounters an if statement, it checks the
condition and assesses whether that condition is True.

if condition:
action

The if conditional statement is structured as an “if” argument, followed by


a condition and a colon at the end of the first line. The second line of code
then contains the action (that is activated if the condition is True) and is
indented (four spaces) to the right. (Unlike other programming languages,
Python uses indentation to group code statements, such as functions and
loops, rather than keywords or punctuation to separate blocks of code.)

Example 1
In this example, we have the variable x = 6 . For this function, we apply the
modulo operator and if the result of the equation x % 2 is 0 (no remainder),
then Python prints the string "x is even" . Conversely, if the outcome is not
equal to zero and there is a remainder, then the condition does not hold and
Python won’t print the statement "x is even" .

13.2 The Else Statement


Following the logic of the if statement, an else statement can be added to
provide an alternative command given the condition set in the first line of
code is not True. This means that using the else statement, there’s no need to
specify a new condition as the statement is based on the condition set by if .
The else statement will thus only run when the if conditional statement is
not True.
Note that the else conditional statement is optional.

if condition:
action
else:
action

Example 2
In special cases where both the if and else conditions hold True, the Python
interpreter will automatically activate the first condition and leave the code
below unread. This means the second condition set by else is never triggered
and there's no corresponding output.

13.3 The Elif Statement


Similar to the else condition, you can also add elif (which means “else if”)
as another optional conditional statement before adding a final else
statement.

if condition:
action
elif condition:
action
else:
action

Let’s review a basic example.

Example 3
As the if conditional statement failed, the Python interpreter moves to the
elif statement, which is True, thereby printing "medium" .
Let’s use modulo division to review another example.

Example 4

In this example, both the if and elif conditional statements are False,
thereby activating the final else condition.
Lastly, it’s important to mention that you can write more than one elif
statement to achieve the goals of your program.

Example 5
In this example, the first elif condition isn’t triggered, allowing a chance for
the second elif condition to be triggered successfully.
14

LOOPS
Let’s talk about loops. In programming, loops are used to iterate through a
series of instructions until a specific condition is reached. The output could
be one line or many lines of code depending on how long it takes the
program to execute and reach a specific condition. Loops can also save time
by executing a set of instructions more than once without you having to
recode a part of the program each time it runs.
In Python, there are many different types of loops and each type serves a
different purpose. The type of loop you use depends on your desired
outcome. This chapter will explore the two common types of Python loops:
for and while loops.

14.1 For Loops


For loops allow you to iterate through a sequence and perform multiple
actions inside one block of code. This ability to perform multiple tasks
drastically reduces the volume of code needed to perform important actions.
As part of a demonstration, let’s create a new list called data .

Example 1

Normally, if we wanted to print each element contained in the list on a new


line, we would need to manually print each element as shown.
While this input code works, it is very repetitive. Instead, we can use a for
loop as input to automate this entire process. The format for a for loop is
shown below.

for item in seq:


action

This format can be understood as: for each item in a sequence of data,
execute a set action.
Using the format of a for loop, let’s create a loop through our list and
specify an action below, which in this example is to print each item
(element). To format the action, we need to use indentation on the second
line of code.

Example 2
It’s worth noting that item isn’t a fixed Python term. We can call this
temporary variable anything we like. We can rename it yellow , for instance,
and the output won’t change. Naturally, it makes sense to name this
temporary variable based on what you are iterating. For example, a suitable
name for this variable could be num , short for “number”.

Also, the action below the for loop doesn’t have to be related to the actual
data. We could, for instance, simply print "hello" for each element in the list.
14.2 Enumerate
If you want to print each item in a sequence as well as display the index of
that item, you can use what’s called the enumerate() function. Using this
function, the Python interpreter is able to output two values: the index value
and the value itself. To achieve this, we need to add the Python keyword
index to the for loop, and insert name of the variable inside the enumerate()
function.

Example 3

Note that on the last line of the input code, the use of str() was used to
concatenate everything as a string.
Next, if we want to edit the indexing to start at 1 (to make the output more
readable for non-programmers), we can add +1 after index on the final line
of input code.
14.3 Using a For Loop to Create a New List
Building on the concept of iteration, we can also use Python to create a
new list from the output of a for loop. To illustrate, let’s create a new list.

Example 4

Next, let’s name the new list that we want to create new_list and assign it to
empty square brackets [ ]. We will fill these empty square brackets later in
the code.

Step 3 is to create a for loop to iterate through each number of the list
contained in data .
For step four, we need to append each num to the new list while also
multiplying it to the power of 2.

Lastly, let’s print the entire function to generate our new list.

The output is a new list, which is essentially the original list with each
element multiplied to the power of two.
This action of transforming and appending elements to a new list is so
common that Python has a shortcut called list comprehension. This is a
way of quickly creating lists based on existing lists in fewer lines of code.
For instance, to generate the same list outputted in the previous example,
we can define the original list and then use a for loop inside square brackets
on the second line of code and assign that as the new list.

Example 5

Now the output has been executed in three lines of code as compared to the
original five, thereby saving time and code. We’ve retained using a for loop
and set an action (num**2) but we no longer need to manually append the
new values.

14.4 While Loops


Given that we know how to use a for loop, we can now advance to coding a
while loop. Similar to the if statement, the while loop executes the code
when a set condition is True. However, unlike the if condition and the for
loop, the while loop executes the code over and over again as long as the
condition is True.
Although a while loop is not as common as a for loop or the if statement, it
can be useful for repeating an action until a condition is met, such as
automating investing above a set price threshold.

while condition:
action

Let’s create a new variable called x and assign it a value of 1.

Example 6
Next, we need to create a while loop using the keyword while in order to set
a condition. Note also the use of a colon( : ) at the end of the code.

For this while loop, we have created a condition that while x is less than 4,
an action will be executed by the Python interpreter. However, we haven’t
yet defined what that action is.
For this example, let’s print the string "x is: {}" and assign x to populate
within the curly brackets using .format() . Again, we need to use indentation
to format this action.

We now have a variable, a while condition, and an action. However, the


code in its current form will lead to infinite outputs because x will always
be equal to 1 and there is no mechanism or condition to stop the while loop
from re-iterating. The loop will simply continue to repeat without stopping.
We therefore need to add an argument to increase the variable value over
time in order to cause a condition that will terminate the loop. To fix this,
let’s try adding the argument x = x + 1 .
Now if we run the while loop, the Python interpreter will run four times and
perform the defined action until x is no longer less than 4.

For this example, we added a condition to prevent the model from running
endlessly by using x = x + 1 . While this condition was effective, it’s not
always necessary. Take the following while as an example.

Example 7

The error starts at 100 but because it is divided by 4 at each round, the
while loop eventually comes to a stop when the output no longer meets the
requirement of error > 2 . In the case of this example, the while loop stopped
after three loops.
Let’s now look at another useful Python function called range, which is
used to generate a sequence of numbers.

14.5 Range
When programming, you will often need to do something a certain number
of times. In the previous example, we had to add the argument x = x + 1 to
prevent the while loop from running infinitely. Similarly, we can use the
range function to limit the number of loop iterations while also being more
explicit in defining the start and end of the loop.
You set the range by inserting the start and end points inside parentheses
after the Python keyword range .

Example 8

Here, 0 is the start point and five is the end point. Next, let’s create a for
loop on the next line of code and state an action on the third line.

Example 9

Note that this loop stops at 4, which is the number indexed at position 5.
Using the list() function, we can also use range() to generate a list.

Example 10

14.6 Create Your Own Functions


Functions are an effective way to reduce the quantity of code needed to
perform an action. In this next section, we’re going to write our own
function that we can call again in the future.
The Python keyword for defining is def and is followed by the custom
name of the function. In terms of formatting the name of functions, it’s
common practice to apply snake casing, which means using underscores in
between each word as well as all letters in lowercase, i.e. name_of_function() .
Parentheses and a colon are also placed after the function name.

Example 11

Let’s now create our own function called my_func() with the variable name
inside parentheses as a placeholder. (Later, we will able to add someone’s
name into the function to print a message, but first we have to set up the
function using this placeholder variable.)

Example 12

On the next line of code, we need to state the action, which is to print a
string message, while using indentation to the right.

We have successfully created and defined a customized function that will


print a string message with a person’s name inserted into the message.
Let’s now call the function by referring to the function’s name and adding
the value of the name variable inside the parentheses, which can be your
own name for example.
After running the code, the output is Hello Oliver , which is a combination of
the string "Hello " and the name variable.
There is, though, a limitation with this custom function in that the output is
not saved to memory. This is because we haven’t created a new variable
containing the message. Instead of using print() , we can use return to save
this output as a new variable. Take note that you don’t need to use
parentheses with return as you would with print.

If we used print instead of return, we’d be unable to replicate the same


output. This is because print does not assign the function’s output to the
new variable ( my_name ). Print simply prints out what it’s told to print but
this output cannot be transferred to the new variable as we can see below.
Instead, we need to use return to save the output to the function in order to
save that output in a new variable for later use.
Let’s now look over another example using arithmetic this time to sum two
numbers. For this function, we will need to create two input variable
placeholders and use return to define and store the action, which is to sum
the two input variables.

Example 13

As shown in the output, this new function called sum_num takes two input
numbers ( num1 and num2 ) and adds them together. As the input variables
values were both integers, the addition operator sums the two inputs.
However, if we were to input two string values, the addition operator would
concatenate the two inputs as shown in the next code example.
14.7 Map
Let’s suppose we want to use a function to double every element in a list
and then output the results as a new list. One way to achieve this goal is to
create a for loop and append the results to a new list (say using list
comprehension). The second way is to use a built-in Python function called
map() .
Using map() , we can link the function with a data type such as a list and
output the result. First, though, we need to define a new function.
Let’s start by defining a function called double and multiple each element
by 2 (using var as a placeholder for each element contained in the data
object). The data object, in this case, will be a list containing 5 elements.

Example 14

Let’s now use map to output a new list by inserting the function name and
list name inside the parentheses. In order to output the results as a new list,
we also need to use the list() function.
In this example, map() was used to bundle our defined function with a list
object. However, sometimes it might not be necessary to define the
function. The function, for example, might be for one-time use and not
reusable. In such circumstances, we can minimize the code using what’s
called the lambda expression, which is a one-time function.

14.8 Lambda
The lambda expression, also called an anonymous function, enables us to
forego the use of the keywords def and return as we don’t need to define or
name the one-time function.

Example 15

Using the lambda expression above, we’re able to code a one-time function
in just one line of code and then start using that function right away to triple
an input variable.
Moreover, we can also use this code inside the map() and list() functions to
generate a list based on the output of our anonymous function, as shown in
Example 16.

Example 16
Now, using lambda we longer need to define a function formally and using
list() and map() , we can do it all with just one line of code!

14.9 Filter
Filter has a similar structure to map, but instead of being a function that
applies to every element in a sequence, this function is used to filter
elements out of a sequence based on a set condition.
To demonstrate, let’s create a new function called check_even , to check that
elements in a list are even.
The function works by using modulo division ( num%2 ) and a condition of
True or False (==0) to return the values that are True (even).

Example 17

Next, let’s introduce the filter function to filter out the elements that are
True and output those elements as a new list using the list function.
Based on the function we created to check that a number is even, we can
see that the filter and list functions can be bundled with the aforementioned
function as well as the list data to filter/output the even numbers as a new
list.
We can also recreate this function using a lambda expression by replacing
def , function name, and return with the syntax of an anonymous function.

Example 18

Again, we receive the same output, except with fewer lines of code.
PYTHON LIBRARIES
15

PANDAS
One of the benefits of learning Python is the range and depth of available
code libraries. In fact, without libraries, Python has few practical
applications.
Libraries can be understood as a collection of pre-written code and
standardized routines used to perform specific outcomes. For instance,
rather than write lines and lines of code to create a simple graph or scrape
content from the web, you can use one function name from a relevant
library to achieve a sophisticated outcome.
After introducing the Turtle library in Chapter 2, we will now focus on the
Pandas library, which is popular in the field of data science.

15.1 About Pandas


Pandas is a library for managing and presenting your data. Its name comes
from the term “panel data,” which refers to Panda’s ability to create a series
of panels, similar to sheets in Excel, which will become more evident over
the course of this chapter. First, though, let’s explain the need for Pandas
and the need for an alternative way of managing and presenting data in
Python.
As a developer, the structure of the data you manage will vary greatly. This
includes different types of data types like strings and numbers. To manage
multiple types of data, for example, we need to use a compound data
structure such as a list or a dictionary (shown below).

Example 1
Lists and dictionaries, however, possess constraints in relation to scalability
and understandability. This is because the more data that we add to a list or
a dictionary, the more cumbersome it is to manage and review that data.
This problem is more acute in the case of a list, where we need to know the
exact index location of each element we wish to select.
Wouldn’t it be easier to put our data in an Excel spreadsheet? With an Excel
spreadsheet, we can easily look up values from an organized schema of
rows and columns. In Table 6, we can see an Excel spreadsheet with the
same information organized neatly into rows and columns with no
confusion or frustration.

Table 6: Example of an Excel table


To enjoy the versatility and user-friendly interface of a spreadsheet, Python
offers a special library called Pandas. This library allows you to store data
in a tabular data structure similar to an Excel spreadsheet, with each row
containing a measurement or observation and each column storing a
different variable. As shown below, the data we previously presented as a
dictionary can be converted into a rectangular table, which in Pandas is
called a dataframe.

15.2 Converting to a Dataframe


Using a Pandas conversion technique, we can create our own dataframe
with labelled rows and columns from an existing dictionary or list.
To perform this task, we first need to import the Pandas library into our
notebook by typing import pandas .

Step 1

As we’ll be using Pandas more than once in our code, it’s good practice to
add a shortcut for referring to this library. Instead of typing out "pandas"
every time we use this library, let’s call it pd instead. This way, whenever
we need a Pandas function, all we have to do is type pd . (Admittedly, we
only conserve three letters using this technique, but performing this upfront
shortcut leads to more concise code further down the line.)

Step 3
Let’s convert our existing dictionary ( new_dict ) into a dataframe using the
Pandas function DataFrame(dict) . To tell the Python interpreter that we want
to use the Pandas library, we first have to use pd with dot notation in
advance of the Pandas function.

Step 4
Now we can type new_dict to review the updated data structure and click
Run to execute the code.

After clicking Run, the Python interpreter will output the original data
structure as a new Pandas dataframe as shown below.
15.3 Importing a CSV as a Dataframe
Although it’s easy to convert an existing dictionary into a dataframe, the
end-to-end process of creating a dictionary or another data structure and
then converting it to a dataframe can be time-consuming, especially as more
data is added. In reality, it’s common to bypass the need for internal
conversion and directly import data from an external file such as a CSV
(comma-separated values) into the development environment as a Pandas
dataframe.
To import a CSV file as a Pandas dataframe, we can use the read_csv()
command and insert the file directory path or URL address inside the
parentheses. We also need to assign a new variable, using df or another
variable name, to store the data.

Example 2

Now, to preview the data, we can type the name of the variable or use the
Pandas head() command using dot notation after the name of the variable.

Example 3
15.4 Other Pandas Functions
To view our dataframe’s columns, we can apply square brackets with the
name of the column inserted inside the brackets as a string. Note that by
default, Python will only output the first five rows.

Example 4

To view other rows from the dataframe, we can again use square brackets in
combination with slicing (as we would use with a list).
Example 5

Shape
To look up the number of rows and columns contained in a dataframe, we
can use the Pandas shape method. This is a useful technique because the
dimensions of the dataset are likely to change as you add or remove rows
and columns.
To query the number of rows and columns in the dataframe, you can use the
shape method preceded by the name of the dataset using dot notation.

Example 6

Describe
Pandas also has a function called describe() which is used for generating a
summary of the dataframe’s mean, standard deviation, and IQR
(interquartile range) values. Note that this function performs optimally with
continuous values (integers or floating-point numbers that can be easily
aggregated).

Example 7

Click Run to generate the description of the dataset as shown below.

Corr
We can also conduct analysis on our dataframe and tease out the correlation
between pairs of variables using the corr() function.

Example 8
Once you click Run, the Python interpreter will generate a table of the
correlation between the variables expressed as numbers, which in this case
is total_bill , tip , and size .

15.5 General Python Functions


Now that we are familiar with the contents of our dataframe, let’s start
applying some of the Python functions we learned in Chapter 11, including
max and min .

Example 9

The highest tip is $10.


Example 10

The lowest tip is $1.

Example 11

The total number of instances for the sex variable is 244. Pandas also has a
built-in function called unique() that we can append to our len function to
output the number of unique instances only.

Example 12

The total number of unique instances for the sex variable is 2 (female and
male).

15.6 Code Challenge

1. Display the first 5 rows of the tips dataset

2. How many rows are in the size column?

3. What was the highest total_bill?


4. What was the lowest total_bill?

5. How many days of the week is the restaurant open?


SOLUTIONS

1) N/A

2) 244

3) 50.81

4) 3.07

5) 4
OBJECT-ORIENTED
PROGRAMMING
16

INTRODUCTION TO OOP
Till now, the code we’ve explored has been prescriptive and procedural in
nature, meaning that we’ve relied on pre-defined code to create loops,
define functions, and achieve other programming outcomes. Object-
oriented programming (OOP), as we will cover in this final section of the
book, introduces a different approach to coding. This approach forces us to
analyze patterns in our code and take stock of what it is we want to achieve
before creating custom programming to achieve our goals. This also gives
our program greater flexibility while using fewer lines of code.
In addition, by organizing and simplifying repeatable tasks using modular
and reusable code, object-oriented programming allows us to create our
own code libraries that can be used by other programmers.

16.1 What are Objects?


As a popular programming paradigm, object-oriented programming
represents the world as objects. These objects, including strings, lists, tuples
and other data structures contain information as well as methods for
interacting with other objects. Each object also has its own state and
behavior.

Object = state + behavior

An object representing an investment stock, for example, can have the


variables “Name” and “Price” as its state, and encoded methods such as
“buy”, “sell”, and “place order” as its behavior. The state and behavior of
an object are organized together under what’s called a class, which is a
blueprint for defining the state and behavior of an object.
Organizing data under classes not only gives us more control over our code
but also enables us to refer to objects in a more unified way. The existence
of a unified interface, for example, allows us to use a method such as type()
on any Python object and look up its data type.

16.2 What is a Class?


The main strength of OOP comes with using classes, which as stated, act as
a blueprint or a design for managing objects. Specifically, classes define the
possible states and behavior of an object, such as “every cryptocurrency has
a three-letter stock code” (state) and can be sold by clicking the “sell”
button (behavior).
In Python, states are represented by variables consisting of numbers,
strings, Booleans, etc., that define the attributes of an object, and behavior
is methods represented as functions using dot syntax.

Class = attributes + methods

Using classes, you can set the default data structure of an object (i.e. list or
string) and set the default value for variables or possible object-level
functions (methods) used by that object. For example, the data type for a
cryptocurrency code can be set to a string with the default value set to the
first three letters of the cryptocurrency’s name, i.e. “ETH” for Ethereum. As
an object, Ethereum, with its defined state values or attribute values is
subsequently the production of a class. Similarly, methods can also be set
according to your needs as the programmer.

16.3 Creating a Class


In Chapter 6, we discussed the use of list methods such as .pop() and
.append() , and in this chapter we are going to create our own methods using
OOP. To recap, methods are mini-functions that use information about an
object type to return or change that object in some way. Using OOP, we can
create our own object-level methods as well as attributes, which are the
variables or characteristics of an object, such as the brand, price, and model
of a car.
car = attributes (i.e brand, price, model) + methods (i.e. buy, sell)

Much like the theory behind object-oriented programming, the code for
object-oriented programming can also be confusing on first reading,
especially regarding the argument of self .

class NameOfClass():
def set_method(self, attribute):
self.attribute = attribute

The best strategy for learning how to write object-oriented programs is to


study the design of existing classes. With this in mind, let’s now explore the
general syntax for OOP, beginning with the first important keyword class .
Before defining an object in terms of attributes and methods, we first need
to input the Python keyword class and assign a name to that class.

class NameOfClass():

This code tells the Python interpreter that we are designing and naming a
class, which can be referred to later in our program. The name of the class
should use camel casing, followed by open-close parentheses and a colon.
Camel casing simply means capitalizing the initials of each word. Although
camel casing is not strictly needed for naming a class, it is good practice to
do so, as it helps to differentiate classes from the snake casing (lowercase)
convention used for naming functions in Python.
Let’s now name the following class MyBike using camel casing.

Example 1

The next step is to define the first method for this class using the def
keyword, which is typed on the next line and indented to the right. While
def may look familiar as a keyword for a function, it’s technically referring
to a method when used inside of a class.

Next to def is the name of the new method, which in this case is set_brand .
To the right of the method name is the self keyword and the attribute of that
method ( brand ), which are both enclosed in parentheses and followed by
another colon. This second line creates the method set_brand , which defines
the brand attribute of the object.
For programmers new to object-oriented programming, the logic of the
keyword self can initially appear confusing. The naming of this keyword,
though, is not important and the name can actually be replaced with another
word of your choosing. What’s important to understand, though, is the role
of this custom keyword, which is to connect the object with variable
attributes by referring to itself (thus the name “self”). For example, using
the same keyword self on the third line of code, we can now bind the
method (behavior) for this class with the brand attribute (state).

This means that any time we use the set_brand method, we are effectively
updating the attribute of the object. Let’s take a look at a full example.
In this example, we created a new instance of an object called your_bike by
assigning the MyBike() class as a new variable. We were then able to assign
the attribute of this new object by using the set_brand method to define the
brand as "Giant" . Lastly, we printed the attribute brand using dot notation
after the name of the object ( your_bike.brand ).
In real production code, classes may actually have dozens of methods with
complicated logic as well as many attributes, which is why you will often
see another technique called the constructor method used for defining the
methods and attributes of a class.

16.4 The Constructor Method


Rather than define attributes one by one using various methods, we can use
what’s called the constructor method, also known as the initializer.

class MyBike():
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute2
self.attribute2 = attribute2
While the code looks similar to defining a single attribute, using the
constructor __init__ , we can now define multiple states of an object using
attributes from the attribute list (attribute1, attribute2, etc.) or even retrieve
other attributes inherited from a parent class (discussed in the next chapter).
The self.attribute = attribute formula is then used to connect the attributes to
the object type.

Example 2

The __init__ method above sets brand , price , and year as three individual
attributes of the class MyBike . Now, using the class name MyBike and
assigning it to a new variable name, we can set the attribute values of an
individual object, called an instance, without using multiple different
methods.

For this example, we set the attribute of the brand to Trek , which is a
popular bicycle brand, the price to $1000, and the year of release as 2010.
We also named the instance david_bike , which is the variable containing this
instance of the object.
We can also create another instance called sally_bike with new attribute
values as follows.
As shown in both these examples, the __init__ method makes it fast and
easy to set the attributes of an object. Without the __init__ method, we
would need to create multiple methods in order to manually populate the
attributes of a new object instance.

16.5 Assigning Class-level Values


Another useful feature of the constructor method is the ability to assign the
default values of a variable at the class-level. This technique is useful when
you want to establish a default value for all object instances created under a
specific class.
To better understand this approach, we first need to distinguish between
instance-level data and class-level data. Instance-level data is a specific
object, whose attributes such as brand and price are assigned object by
object. Class-level data, meanwhile, is stored and shared among all objects
of a class. This means that the data does not differ between individual
object instances and we can define attributes directly in the class body to
serve as a “global variable”. The practice of defining objects at the class-
level is common when you have a minimum, maximum, or limit value that
applies to all instances, such as a minimum salary or a fixed transaction fee.
Let’s look at an example.

Example 3
As shown in the code, the constructor makes it very easy to assign default
values for all objects at the class-level, which might be useful if every bike
is of the same brand.
This technique, though, might not be suitable for all scenarios. For example,
perhaps not every object instance is a Trek bike. Each object (bike),
therefore, needs to be assigned individually at the instance-level without a
default value for that attribute.
In some situations, it might be useful to set a default value for each object
using the constructor method and then making adjustments using a new
method to modify that attribute value. The following example demonstrates
this process, in which the default salary of an object instance( david ) is
modified using a new method called add_payraise .

Example 4
The key step in the code above was creating a new method called add_raise ,
which used the combined addition and equals symbols += to connect the
add_raise method with the salary attribute and its default value of 1000.
Later, after creating an object instance called david , the add_raise method
was used with an attribute value of 100 that automatically links with the
salary attribute to update David’s salary to 1100 (100 more than the default
salary of other instances/employees).
While methods can be shared among instances, they can also be set at the
class-level, which means you can feed data directly into each object
instance. This approach, though, prevents you from using these methods at
the instance-level to modify individual instances.

16.6 Summary
Object-oriented programming is a useful approach in situations where there
are complex interactions of many objects. In this chapter, we focused on the
concept of encapsulation, which bundles the state (attributes) and behavior
(methods) of an object under a custom-defined blueprint called a class.
Encapsulation is a core design practice of object-oriented programming,
and in the following chapters, we will explore the other two core design
practices of polymorphism and inheritance.
17

INHERITANCE AND
POLYMORPHISM
In addition to encapsulation, OOP also supports inheritance and
polymorphism. In this chapter, we will explore both of these two design
practices.

17.1 Inheritance
In object-oriented programming, inheritance refers to the ability of one
class, called the parent, to pass its attributes and methods to another class,
called the child class. This means that when a child inherits from its parent,
it automatically gains all the features of that parent class without the need to
re-implement existing code.
To create a class that inherits from another class, you can simply add
parentheses after the child class name and then insert the class from which
to inherit. To demonstrate, let’s create a parent class for GoDaddy’s basic
web hosting service called BasicHosting with four attributes: storageGB,
domain, ssl, and price.

Example 1
Now that we have a parent class, we can create a child class, which is
GoDaddy’s premium web hosting service called DeluxeHosting .

To create the child class, we just have to insert the parent class inside
parentheses next to the new class and use the pass keyword on the line
below (indented to the right). This automatically transfers the attributes of
Basic Hosting to Deluxe Hosting without executing the code for now.

Table 7: Basic Hosting vs Deluxe Hosting

Next, we need to modify the pricing and upgrade the storage quota, while
the domain and SSL attributes remain unchanged.
Finally, let’s print the updated attribute values of storageGB and price for
Deluxe hosting.

Here, we have effectively created a new class for Deluxe Hosting that
inherits all four attributes from Basic Hosting along with updates to pricing
and storage using new two methods called add_storage and add_price . As
seen in the code above, you can call on data from both the child and the
parent class within the child class. In this example, we combined the parent
class price (6.99) with a new input (3) at the child-level to generate a new
price of $9.99.
Note, however, that you cannot use methods established in the child class
inside the parent class. You can, though, initiate methods in the parent class
that you wish to serve as global methods, which can be used within both the
parent class and the child class.
17.2 Adding Child Class Attributes
To add new attributes to a child class, you can use the same code as you
would with a standard class using the __init__ method.
To demonstrate, let’s add two new attributes ( SEO_optimizer and test_site ),
which are unique to Deluxe Hosting, using the __init__ method.

Example 2

Let’s now check that the two new attributes have been successfully added to
the child class by creating an object instance and then printing the two new
attributes ( SEO_optimizer and test_site ).

Yay! We have successfully created a new class that inherits data from the
parent class while also adding new two attributes specific to the child class.
Lastly, although we didn’t create any global methods in this exercise, you
can also create methods at the parent level (that can be used by child
classes) using the standard code for creating a method.
Example 3

In this example, we created a new global method called discount_price that


can now be used with any child class using the same technique of inheriting
a parent class.

17.3 Polymorphism
While not as commonly used as encapsulation and inheritance,
Polymorphism allows you to call the same method on different classes
while producing a result unique to the class of each object. In short,
polymorphism allows you to create multiple versions of a single method
using the same method name.
The term polymorphism itself means to 'have many forms', and is used in
programming to describe an object that can be treated as any one of its
derived classes. For example, let’s say we have a Shape class and it has two
child classes: Circle and Triangle. If you want to find the area of both
shapes, we can create a new method called .area() that first checks if the
child class object is a Circle or Triangle and then calculate its area
according to its shape. The same method can be called on either of the two
child classes but the calculation and outcome will be unique to that
particular child class. In addition, you don’t need to check the class type of
an object before selecting and running the method. You can simply use the
same method on different class-type objects and let the method discern
which version of the method to execute.

Example 4
To demonstrate polymorphism in its simplest form, let’s create two child
classes of a parent class for app users.

As shown above, we can use the same method .greeting() on both child
classes and produce a customized message based on the language of the
user.

17.4 Code Challenge


Below is a parent class for Basic Hosting and a child class for Deluxe
Hosting. The parent class has three attributes (domain, coupon, and price)
and one method (as used earlier in the inheritance code example) to
discount the price attribute.

Below is the child class, called DeluxeHosting .

Your challenge now is to nullify the discount_price method if a customer is


an existing user of Deluxe Hosting. However, for new users, the discounted
price remains valid. The discount also applies to Basic Hosting irrespective
of whether the user is a new or existing user.
Note that the method discount_price needs to be prefaced by the parent class
name when used in a child class (i.e. BasicHosting.discount_price ).
Options
a. BasicHosting.discount_price(self, discount=1)
b. BasicHosting.discount_price(self, discount)
c. BasicHosting.discount_price(self, discount=0)
d. BasicHosting.discount_price(self, price)
SOLUTION
a. BasicHosting.discount_price(self, discount=1)

By modifying the method for Deluxe Hosting so that discount is equal to 1,


we can cancel any discount as the original price attribute is multiplied by 1
regardless of the discount offered to the user.
Let’s test this solution by creating two new objects, one of the child (Deluxe
Hosting) and one of the parent class (Basic Hosting) and set the attributes of
coupon and user to Yes.

The output of the price attribute for user1 is equal to the original price
($10), while the price attribute for user2 has been effectively discounted by
10% from $5 to $4.50. While both existing users have a discount coupon,
the first user does not enjoy any discount as they are purchasing Deluxe
Hosting, which is not eligible for a discount.
FROM THE AUTHOR
If you have any direct feedback, both positive and negative, or suggestions
to improve this book please feel free to drop me an email at
[email protected]. This feedback is highly valued and
I look forward to hearing from you.

Please note that free accompanying quizzes, notebooks, and video resources
can be found at: https://scatterplotpress.teachable.com/p/python-for-absolute-beginners

You can also follow and find free learning materials and videos on my
Instagram channel at machinelearning_beginners.

BUG BOUNTY
Thank you for purchasing this book. We offer a financial reward to readers
for locating errors or bugs in this book. Some apparent errors could be
mistakes made in interpreting a diagram or following along with the
concepts introduced in the book, so we invite all readers to contact the
author first for clarification and a possible reward, before posting a one-star
review! Just send an email to [email protected]
explaining the error or mistake you encountered.
This way, we can also supply further explanations and examples over email
to calibrate your understanding, or in cases where you’re right and we’re
wrong, we offer a monetary reward through PayPal or an Amazon gift card.
This way you can make a tidy profit from your feedback and we can update
the book to improve the standard of content for future readers.
FURTHER RESOURCES
This section lists relevant learning materials for readers that wish to
progress further in their programming skills.

Learning Python, 5th Edition


Format: E-Book, Book
Author: Mark Lutz
Suggested Audience: Those with a general interest in Python
A comprehensive introduction to Python published by O’Reilly Media.

Hands-On Machine Learning with Scikit-Learn and TensorFlow:


Concepts, Tools, and Techniques to Build Intelligent Systems
Format: E-Book, Book
Author: Aurélien Géron
Suggested Audience: Those with an interest in machine learning
As a popular O’Reilly Media book written by machine learning consultant
Aurélien Géron, this is an excellent advanced resource for anyone with a
solid foundation in machine learning and computer programming.

Code Academy
Format: Online learning
Suggested Audience: Those with a particular interest in a subdomain such
as scraping, data analysis, automation, etc.
Code Academy provides a diverse library of self-paced online video
courses to take your Python skills to the next level.
OTHER BOOKS BY THE AUTHOR
Machine Learning with Python for Beginners
Progress your Python skills by learning how to code machine learning
models in Python and solve real-life problems.

Machine Learning for Absolute Beginners


Learn the fundamentals of machine learning, as explained in plain English.

Machine Learning: Make Your Own Recommender System


Learn how to make your own recommender system in an afternoon using
Python.

Data Analytics for Absolute Beginners


Make better decisions using every variable with this deconstructed
introduction to data analytics.

Statistics for Absolute Beginners


Master the fundamentals of inferential and descriptive statistics with a mix
of practical demonstrations, visual examples, historical origins, and plain
English explanations.
Leave a review?
To leave a review of this book, please tap here.
This le was downloaded from Z-Library project

Your gateway to knowledge and culture. Accessible for everyone.

z-library.se singlelogin.re go-to-zlibrary.se single-login.ru

O cial Telegram channel

Z-Access

https://wikipedia.org/wiki/Z-Library
ffi
fi

You might also like