Python For Beginners An Essential Guide - Conley Walsh
Python For Beginners An Essential Guide - Conley Walsh
Why python
Python for Data Analysis – Basics
Python Operators
Supervised Learning Algorithms
Creating & Accessing Your Python Dictionary
Introducing Variables
Naming Conventions and Comments
Handling Inputs
Working with the Conditional Statements
Exception Handling in the Python Language
Functions, Classes and Methods
Using Linear Regression for Predictions
Replacing and Correcting words
Using Jupyter notebook for user interaction
The Regular Expressions
Data Visualization with Python… AND MORE!!!
Introduction
Running Python on a computer has proven to be a success. As a beginner in
Python, it is key to keep your mind wide open for new encounters. The
above, are the steps required to ensure that your computer is running python
smoothly.
Remain focused, as you are on this journey to learning programming. You
will not become great overnight. It is always a great thing to see a
programmer turn into a millionaire or a programmer changing the lives of
thousands in a single programming day.
Ultimately this book was written with the goal in mind of teaching you not
necessarily about Python, but programming at large.
If you are reading this, it means that you have made it to the end of the
book. One word for you?
When you make a clear and realistic plan of how you aim to learn, the plan
becomes achievable. Learning will definitely be at your own pace. The first
step is the interest that you had, which resulted in you reading this book and
then – action! Challenge yourself with new tasks every day. Programming
is not as hard as it looks or as hard as people make it seem. Nothing is hard
if you are ready to put in the work. Start with the basics .
Most of the people who are new to programming and who want to learn use
Python. Even though most programmers say it is an easy program to learn,
it will all depend on you. If you are to have a chance at being successful in
this, you will need to go the extra mile. Ask questions from experts and
always be ready and eager to learn. Who knows? This could be a step-in
learning all the programming languages out there. Keep an open mind and
you will not find anything hard about this.
Do something with it. You can absolutely use this book as a reference, but it
wasn’t written to necessarily be one - this book was written in order to teach
you the essence of programming and everything that you need to do to start
programming in Python, as well as the essential tools of the language that
you’ll have to know how to use as a beginner.
Chapter 1: Why python
One of the difficulties that developers face when working with joint
processing in the Python programming language (in particular, CPython,
the reference implementation of Python written in C) is GIL. The GIL
mechanism is a deadlock mechanism that protects access to Python objects,
preventing the simultaneous execution of Python byte codes in multiple
threads. This locking is necessary mainly because CPython's memory
management is not thread-safe. CPython uses a reference counter to
implement its memory management. This results in the fact that multiple
threads can access and execute Python code at the same time; this situation
is undesirable and it can cause incorrect data processing and we say that this
type of memory management is not oriented towards multi-threaded
management. To solve this problem, there is a GIL, as its name indicates, a
certain lock that allows only one thread to access the code and Python
objects. However, this also means that in order to implement programs with
multiple threads in CPython, developers must be aware of the GIL and
circumvent it. It is for this reason that many who have problems
implementing co-processing systems in Python.
So why do we even use Python for collaborative processing? Even though
GIL does not allow CPython programs with multiple threads to get all the
advantages of multiprocessor systems in certain situations, most of the
operations with blocking or long- term execution, such as input / output,
image processing, as well as grinding numbers in NumPy, occur outside the
existing one. Gil Thus, the GIL itself becomes a potential bottleneck only
for programs with many threads that spend significant time inside the GIL.
For example, applications with many processes that do not share any
common resources between processes, such as input / output, image
processing, or grinding NumPy numbers, can work seamlessly with GIL.
In addition, Python has gained increasing popularity in the programming
community. Thanks to user-friendly syntax and general readability, more
and more people believe that it is relatively easy to use Python in their
development, whether it’s a beginner learning a new programming
language, users with an average level of training in searching for available
modern Python functionality, or experienced Programmers using Python to
solve complex problems. There are estimates that Python code development
can be up to 10 times faster than C / C ++ coding.
A large number of developers using Python have come about as a result of a
powerful, still growing community. Every day, Python libraries and
packages are developed and released, supplying various tasks and
technologies. Currently, Python supports an incredibly wide range of
programming - namely, software development, GUI workstations, video
game design, web and Internet development, as well as scientific and
numerical calculations. In recent years, Python has also grown as one of the
top tools in data science, Big Data, and machine learning, competing with a
long-term player in the field, R.
The huge number of development tools available in Python has prompted
an increasing number of developers to start programming in Python, which
makes Python even more popular and easy to use; I call it the vicious circle
of Python . David Robinson, DataCamp's research supervisor, wrote a blog
post about Python's incredible growth and called it the most popular
programming language.
However, Python is slow, at least slower than other popular programming
languages. This is because Python is a dynamic typing language that
interprets in which values are not stored in tight buffers, but in scattered
objects. This is a direct result of the readability and usability of Python.
Fortunately, there are various options for making your Python program run
faster, with concurrency being one of the most difficult among them; and
this is exactly what we are going to master in this book.
Customizing Your Python Environment
Before we move on, let's go through a number of descriptions about how to
configure the necessary tools that you will use in this book.
General installation
Let's look at the actual process of obtaining a certain Python distribution for
your system and the corresponding development environment :
All developers can get their own Pyton distribution from https: / /
www.python. Org / downloads / .
Even though both versions of Python 2 and Python 3 are supported and
maintained, throughout this book we will use Python 3.
For this book, a flexible option would be an IDE ( integrated development
environment ). Although it is technically possible to develop Python
applications with a minimal text editor such as Notepad or TextEdit, it is
usually much easier to read and write code using an IDE purposefully
developed for Python. They include: IDLE , PyCharm , Sublime Text, and
Atom .
Sample Code Unloading
To get the code that has been used throughout this book, you can download
the repository from GitHub, which contains all the examples and project
code described in the book:
To get started, visit https: / / github.com/ PacktPublishing / Mastering-
Concurrency-in-Python .
To upload the corresponding repository, simply click on the Clone or
download button in the upper right corner of your window. Select
Download ZIP to download the necessary compressed repository to your
computer:
To download the necessary repository, click on Download ZI P
Unzip the downloaded file to create the folder we are looking for. This
folder should have a name Mastering-Concurrency-in-Python.
findings
Now you have learned the basics of the concepts of joint and parallel
programming. All this relates to the design and structural programming of
commands and instructions so that different sections of your program can
be executed in some kind of effective way, and at the same time share the
same resources. Since when some commands and instructions are executed
at the same time, time is saved, parallel programming provides a significant
improvement in the execution time of the program when compared with
traditional sequential programming.
However, when designing a parallel program, various factors should be
taken into account. While there are special tasks that can easily be divided
into independent sections that can be executed in parallel (stunning parallel
tasks), others require different types of coordination between the existing
program commands so that such shared resources can be applied correctly
and efficiently. There are also tasks with sequential processing that is
intrinsically intrinsic to them, in which no joint processing and parallelism
can be applied to obtain program acceleration. You should be aware of such
fundamental differences between these tasks so that you can carefully
design your co-processing programs .
In recent times, a paradigm shift has occurred that facilitated the
implementation of joint processing in most aspects of the existing
programming universe. Now joint processing can be found everywhere:
desktop and mobile applications, video games, web and Internet
development, AI and so on. Collaborative processing is still growing and is
expected to continue to grow in the future. Therefore, it is extremely
important for any experienced programmer to understand the joint
processing and related concepts, as well as to know how to integrate these
concepts into their applications.
Python, on the other hand, is one of the most (if not the most) popular
programming languages. It provides options in most programming sub-
sectors. Combining collaborative processing and Python in this way is one
of the most important topics to learn and master in programming.
Chapter 2: Python for Data Analysis – Basics
Now that we have everything setup, we can start to get used to the
programming language. Therefore, we will go in details about the basic syntax,
flow control blocks and intrinsic peculiarities. All these topics work as a
presentation to the language for new users, or as a reminder for readers with a
good knowledge.
Python REPL
As soon as the Python installation is completed, we can immediately start to
use it interactively using its Read Eval Print Loop (REPL). It works as a
dynamics script in which you can create and modify variables and see instant
results.
The main advantage of the REPL is that you can start experimenting right
away. Therefore, it works as a great tool to test basic syntax of the language. If
any code is taking too long to run in the REPL
§ Starting the REPL
To run it, you just need to or search for “Python” or type python in the
command line:
§ Interfac e
At first glance, it seems like just another CLI. Initially, it shows basic
information of the installed Python version.
§ Input Field
The input field is represented by the triple ‘>’ symbol, it is where the
commands/script should be typed.
Python Basic - Data Types and Operators
Now, let’s be familiar with all the basics types and operations of the language.
Comments
Any command starting with “#” are completely ignored.
Basic Data Types
As many programming languages, Python has all the standard data types
implemented.
If you want to know a type of any object in Python, just use type(object) and
the resulting type will be shown (which is a class in Python) .
§ Numeric and Boolean Data Types
floa
Floating point type can be represented in scientific 8.5,
t
notation. 1e3 = 1x103 =1000. 0.0,
-1.7e-
5
boo
Boolean value True,
l
False
§ Strings
In Python Strings are expressed in double (“…”) or singles (‘…’) quotes. If
you want to quote in your string, you can scape them using \, or use a different
quote in its definition.
Strings in python are represented by the class str. Different of some
programming languages, Python doesn’t have a character type. Therefore, a
single character is also a string. Additionally, strings have many built-in
methods that facilitate its manipulation and transformation. Most of these
methods will be presented later in the book. Also, should be noted that strings
are immutable objects, therefore their values can not be altered. If you want a
different string, a new one should be created. You can cast different types to
strings using the str() function.
§ None Typ e
The None keyword represents the NoneType in Python. It is a type that
represents no values. In general, it is used to show that a function did not
resulted in any values.
Variable Assignment
Assignment represents the binding of a name to a value or expression. As in
multiple programming languages, variable assignment is done using the (=)
operator. Variable names are case sensitive, and keyword of the language
cannot be used to represent variables names. You can have simple, multiple
and same values assignments.
Arithmetic Operators
Most arithmetic operators work just like expected between numeric values.
-
Subtracts the values >>> 8 -
4
*
Multiply values >>> 21
*2
42
/
Divide the values, the returned values are always >>> 15
a float. /2
7.5
//
Floor division, it ignores any fractional values >>> 15
from results /2
%
Modulus, returns the remainder of the division >>> 6
%4
**
Raises to power >>>
2**8
256
()
Ranks the order of operations >>> (1
+ 3) / 2
2.0
OBS 1.: Operations between bool and int/float are automatically converted to
the numeric type (int/float). In which True is 1 and False is 0.
OBS 2.: Operations between float and int are automatically converted to float
type.
OBS 3.: The operators + and * have a special effect on str type.
For example :
As can be seen from the examples above, the str*int operation repeats the
string values multiple times, and the str+str concatenates the strings.
§ Precedence and Associativity
Each operator has a precedence in that expression. In other words, some
operations are solved before other. The order is shown in the table below:
Order Operators Description
1
() Parentheses
2
** Exponent
3
-x, +x Unary minus or plus
4
*, /, //, Multiplication, division, floor division, modulus
%
5
+, - Addition, subtraction
0.5
256
This means that any expression will follow this order for resolution. First, the
order in Precedence Table , then the order in Associativity Table .
§ Self-Increment
Multiple times, it is necessary to increment a variable. There is a small shortcut
to perform this using the operators seen before. The shortcut operations and
their representation are shown below.
Operator Example Equivalent Representation
+= x += 2 x=x+2
-= x -= 3 x=x–3
*= x *= 4 x=x*4
/= x /= 5 x=x/5
%/ x %= 6 x=x%6
True
>=, <=
Greater/less than or equal >>> 4 <= 2 + 2
True
==
Equal to >>> 6 == 2 * 3
True
!=
Different of >>> 3 != 3
False
OBS.: You can compare multiple things in Python but be careful with the result
of the comparison. For example, strings can be compared, but results in the
comparison between their ASCII values of their characters. Therefore, careful
when using these operators with non-numeric types.
§ Boolean Logic
When you want to combine multiple Boolean logic operations you can use and,
or, not keywords. When used with Boolean values, operator and returns true
only when both are true, operator or returns true when at least one is true, not
inverts the Boolean value.
Basic Containers – Lists and Tuples
There are two basic types of ordered sequences in Python: list and tuple. Both
stores ordered values but have a crucial difference: mutability. While list can be
easily modified, tuples (just like strings) are immutable. Due to this difference,
they are used in different cases. In general, lists are used when you need to
modify, remove or append values, while tuples when you only need to read the
values. Thanks to the immutable restriction, iterations on tuples are faster than
on lists.
List s
They can contain multiple types of data. To define a list, you just need to write
a sequence of comma-separated values between square brackets.
§ Defining Lists
Square brackets are used in list definitions.
§ Function list()
Beyond creating an empty list, you can also create list from previous objects
such as strings.
§ Indexing
Each value of the list is accessible by an index. The index represents position
of the value in the list, starting by position 0. Additionally, negative index can
be used counting from the end of the list. As lists are mutable, the index can
also be used to change the values.
§ Slicing
Sometimes, instead of accessing a single value from a list, you could want to
select a sub-list. For this, there is the slicing operation. Generally, slicing is
used with [start:end] resulting in values from the start position until the end, the
end position is excluded of the resulting substring. If omitted, the start defaults
to 0 and the end to the length of the list. Furthermore, slicing can be used to
change multiple values at once. The examples below show how slicing works.
A good way to create intuition about how slicing works is to think of the
indexes as points between the values.
In the image above, slicing [-4:-1] returns [12, 13, 14] and [0:2] return [11, 12].
§ Concatenating and Repeating
Like strings, the operators + and * can be used to concatenate and repeat lists,
respectively. The result is a new list. Additionally, list ’s have the method
append that attaches a value to its end.
Tuples
Tuples are immutable and can contain multiple types of data.
Therefore, they are generally used to read static data.
§ Defining Tuples
Parenthesis are used to define tuples. To differentiate a tuple with a single
value to a simple parenthesis expression it must contain a comma. Examples:
§ Function tuple()
Like lists, you can also create tuples from previous objects such as strings or
lists.
§ Indexin g
Just like lists, tuples can be indexed, and the same indexing rules are applied
except for modifying its values. Examples:
§ Slicing
Tuples support slicing too. Of course, not allowing change of the values.
§ Concatenating and Repeating
The operators + and * works like lists. As tuples are not mutable the result is a
new tuple created from the concatenation or repetition. Examples:
Function len
The len function can be used in most containers types. It shows the number of
elements in this container whether a list , tuple or string . Some examples that
show its usage.
Modules
The Python REP L is great to test small code snippets and language syntax.
However, after closed, all the variables and operations defined are lost.
Therefore, to write longer programs and save the results a text editor is
necessary. In the editor, you can create a new file that contains the instructions
and variable definitions, this file is called a script. Python scripts are saved
with the .py extension.
§ Running Scripts
Consider a script called myscript.py . In order to run this script, follow these
instructions:
1.
Open your system terminal.
2.
Navigate to the script folder (where the myscript.py file is located).
3.
Run the command:
After all code in the script will be executed in the current terminal window.
Try to run the examples below. Paste their code in the text editor and save a
example1.py and
example2.py files. Then follow the instructions above to see the code outputs.
After invoking the modules, their instructions will be executed .
OBS.: In Visual Studio Code, the script could be executed directly in the IDE
using the run button.
Conditional Statements
Like other languages, the code under the if statement is only executed if the
expression is evaluated as True . Additionally, the optional else keyword can be
used to execute another code when the statement is False . The overall format
is show below.
The example shows the overall syntax of the if and else statements. Here we
can see an important aspect of the Python programming language: Indentation .
§ Indentation
Indentation describes the spaces between the left margin and the start of text. In
Python, indentation mean that the code belongs to a block of code , in other
words, it indicates whether a line is in the same block or not. Commonly, 2 or 4
“white spaces” or 1 “tab” can be used for indentation. However, it is highly
recommended to only one kind should be used throughout the code, either
whitespaces of tabs.
§ Multiple Conditions
Sometimes we need to test various conditions, for this we can have multiple
if…else statements. Additionally, a shorter term for else if statements can be
used: elif .
Here we used the print function to show the results.
§ Nested Statements
In order to test multiple dependent conditions, if statements can be nested
together, resulting in various blocks of code.
Loops
There are two basic loops in Python, for and while . Both are used to loop
through a block of code but have different use-cases.
While
The while loop repeats a block of code while a given expression is evaluated as
True. The condition is tested before each execution.
Since evaluation occurs before the block execution the block might not even
run. Notice the presence of indentation for the block of code.
In the examples above we can see how while loop works. After each code
block execution, the expression is re-evaluated to check if the block will be re-
run again.
For
When you need a finer control for the total number of executions, the for loop
is used. In python, for loop uses a sequence of elements defined as an iterator .
The for loop iterates over each value in this sequence.
During each pass through the loop, a new value from the sequence is passed to
the variable VALUE , until the sequence end is reached. Pay attention to the use
of the in keyword before the operator.
OBS1.: As in any other programming language, it is highly discouraged to
modify the iterating object during the loop, since this can easily cause
undesired behaviour.
OBS2.: The in keyword are also used to see if a value is present in a sequence
(list, tuple, string, …). In this case it returns a Boolean value.
§ Iterators
Simply put, an iterator corresponds a sequence of elements. In Python, lists ,
tuples and strings are example of iterable objects. If you want to create a
sequence of integer to iterate over, the range function can be used.
The examples bellow shows different kinds of iterators being used in for loops.
§ Range Function
This built-in function created a sequence of integers. It can be used with 1, 2 or
3 arguments. With one argument, the sequence starts from 0 until the given
argument (excluded) with step unitary. Using with two arguments, the first
correspond to initial(included) value or the sequence and the second to the final
(excluded) value. Finally, with three arguments is like with two, but the third
argument corresponds to the increment size. Check the examples below.
The examples show the usage of the range function and in the for loop. By
now, you could have noticed that the range function could be used to generate
index for elements in lists. Indeed, this could be done, but using the sequence
directly would be a more “Pythonic” approach. The example below illustrates
this case.
As you can see, the range function can be used to generate index of each
element in a list but looping using the sequence directly is a more simple and
understandable code.
Extra – Zen of Python
As a small Easter egg, Python has a collection of guiding principles for writing
computer programs. You can check this by running the command bellow in the
Python REPL.
After running the command, a text from Tim Peters will be printed. It shows
the principles that were considered in the design of Python as a programming
language .
Even though this is an Easter egg, all these tips are valuable, and any
programmer should keep them in mind when creating new projects.
Exercises
Answer the question below, then and check your responses using the Python
REPL .
What’s the type of each of these expressions?
>>> 1e-3
>>> 2
>>> 3.
>>> 5 > 2
The string definition below are valid? Mark as True of False.
>>> “String\’s” ( )
>>> “HelloWorld’ ( )
>>> ‘This is a “quote”’ ( )
>>> ‘That’s fine!’ ( )
>>> -3 * 1
>>> 5 % 3
>>> 2 + 3 * 3
>>> True + 3
>>> 3 ** False
>>> type(3 / 3)
>>> type(3. + 2)
>>> type(False + True)
>>> ‘123’ * 2
>>> 2 – 2 / 4
>>> (2 - 2) / 4
>>> 2 ** 2 ** 4
>>> 3 ** False
>>> 3 % 5 + (2 ** (6 / 3))
What are the results of the sequence of commands?
>>> a = b = 3
>>> c, d = 1, 2
>>> a + c == d * b - 2
>>> s = “a”
>>> s *= 3
>>> s + “b”
>>> a = 0
>>> b /= 2
>>> b != 21 or c/b == 2
>>> b = False
>>> c = not b
>>> a * 11
>>> a = [1, 2]
>>> b = [3, 4]
>>> a + b
Consider the list li = [42, 1, 2, 3, ‘A’, ‘B’], what the result of each alternative?
a) >>> li[3]
b) >>> li[-2]
c) >>> li[:-3 ]
d) >>> li[-5:]
Which of the alternatives throws an error when executed?
>>> a = (1,2,3)
>>> a[3]
>>> b = [5, 6, 7, 8]
>>> b[-5]
>>> a = (1, 2)
>>> b = (3, 4)
>>> c = a / b
>>> a = (1,2,3)
>>> b = (1,2,3)
>>> a + b
>>> a = (1,2,3)
>>> b = (1,2,3 )
>>> a * b
Write a script to solve the problem: consider a list of size n, if n is odd the
script shows the value in the middle of the list, if n is even, it shows the two
values at the center of the list.
Examples:
list1 = [1,5,11,12,16] -> 11
list2 = [1,5,11,12] -> [5, 11]
Chapter 3: Python Operator s
Python operators help us manipulate the value of operands in operations.
Example:
10 * 34 = 340
In the above example, the values 10 and 34 are known as operands, while *
is known as the operator. Python supports different types of operators.
Arithmetic Operators
These are the operators used for performing basic mathematical operations.
They include multiplication (*), addition (+), subtraction (-), division (/),
modulus (%) and others. Example:
#!/usr/bin/python3
n1 = 6
n2 = 5
n3 = 0
n3 = n1 + n2
print
( "The value of sum is: ", n3)
n3 = n1 - n2
print
( "The result of subtraction is: ", n3 )
n3 = n1 * n2
print
( "The result of multiplication is:", n3 )
n3 = n1 / n2
print
( "The result of division is: ", n3 )
n3 = n1 % n2
print
( "The remainder after division is: ", n3)
n1 = 2
n2 = 3
n3 = n1** n2
print
( "The exponential value is: ", n3)
n1 = 20
n2 = 4
n3 = n1// n2
print
( "The result of floor division is: ", n3)
The code prints the followig when executed:
Assignment Operators
These operators the combination of the assignment operator (=) with the
other operators. A good example of an assignment operator is “+=”. The
expression p+=q means “p=p + q”. The expression “p/=q” means that “p=p
/ q”. The assignment operators involve combining the assignment operator
with the rest of the other operators.
Example:
#!/usr/bin/python3
n1 = 6
n2 = 5
n3 = 0
n3 = n1 + n2
print
( "The value of n3 is: ", n3)
n3 += n1
print
( "The value of n3 is: ", n3 )
n3 *= n1
print
( "The value of n3 is: ", n3 )
n3 /= n1
print
( "The value of n3 ", n3 )
n3 = 2
n3 %= n1
print
( "The value of n3 is: ", n3)
n3 **= n1
print
( "The value of n3 is: ", n3)
n3 //= n1
print
( "The value of n3 is: ", n3)
The code will print the following when executed :
The statement “n3 = n1 + n2” is very straight forward as we are just adding
the value of n1 to that of n2. In the expression “n3 += n1”, we are adding
the value of n3 to that of n1 and then assign the result to n3. However, note
that in the previous statement, the value of n3 changed to “11” after adding
n1 to n2. So we have 11+6, which gives 17. After that, the new value of the
variable n3 will be 17. The expression “n3 *= n1” means “n3= n3 * n1”.
This will be 17 * 6, and the result will be 102. That is how these operators
work in Python!
Membership Operators
These are the operators which are used for testing membership in a certain
sequence of elements. The sequence of elements can be a string, a list or a
tuple. The two membership operators include “in” and “not in”.
The “in” operator returns true if the value you specify is found in the
sequence. The operator “not in” will evaluate to a true if the specified
element is not found in the sequence.
Example:
#!/usr/bin/python3
n1 = 7
n2 = 21
ls = [ 10, 20, 30, 40, 50 ]
if
( n1 in ls ):
print
( "n1 was found in the list" )
else
:
print
( "n1 was not found in the list")
if
( n2 not in ls ):
print
( "n2 was not found in the list")
else
:
print
( "n2 was found in the list")
n3= n2/ n1
if
( n3 in ls ):
print
( "n1 was found in the list")
else
:
print
( "n1 was not found in the list")
The code will print the following once executed:
The value of num1 is 7. This is not part of our list, and that is why the use
of the “in” operator returns a false. This causes the “else” part to be
executed. The value of n2 is 21. This is not on the list. This expression
returns a true, and the first part below the expression is executed. 21 divide
by 7 is 3. This value is not on the list. The use of the last “in” operator
evaluates to a false, and that is why the “else” part below it is executed.
Identity Operators
These operators are used to compare the values of two memory locations.
Python has a method named “id()” that returns the unique identifier of the
object. Python has two identity operators:
is- this operator evaluates to a true in case the variables used on
either side of the operator are pointing to a similar object. It
evaluates to false otherwise.
Integers
Floats
Longs
Complexes
☐ Strings
☐ Lists
☐ Tuples
☐ Dictionarie s
For now you don’t need to learn about lists, tuples and dictionaries but if
you just can’t wait then skip ahead to the called ‘Advanced Variables’.
Performing Operations on Variables
Remember how we said that variables allowed our programs to change?
That’s because we can change the value of the variable whenever we like
throughout our code.
As we’ve already seen, we can set the value of a variable very simply like
so at the start of our code:
MyNumber = 3
But if we later want to change the value of our number we can simply
change it again later on. For example:
MyNumber = 3
print(MyNumber)
MyNumber = 4
print(MyNumber)
If you run this program then it will simply output ‘3’ and then ‘4’. Like so:
But likewise, we can also change our variables using the very same
operations that we used in 4. For example:
MyNumber1 = 3
MyNumber2 = 4
MyNumber3 = MyNumber1 + MyNumber2
print(MyNumber3)
Can you guess what this will output?
Or how about:
MyNumber1 = 3 * 2
MyNumber2 = 4
MyNumber1 = MyNumber2 – 3
MyNumber3 = MyNumber1 + MyNumber2
print(MyNumber3)
You can even let a variable refer to itself. For example, what do you think
this line of code does:
MyNumber1 = MyNumber1 + 1
Essentially, this increases MyNumber1 by ‘1’. So you are saying that the
value of the variable will become whatever it is right now plus one.
Right now this might all seem a little strange but as we go through and look
at the different commands you can use in Python it should start to make a
lot more sense!
The next question you might be wondering is whether or not you can also
do this kind of thing with strings? And the good news is that you can and it
actually works just the same! Of course you can’t multiply a string by a
string (because that doesn’t mean anything!) but what you can do is to add
two strings together to create a new one. For example:
FirstName = “Bill”
LastName = “Gates”
FullName = FirstName + LastName
print(FullName)
You’ll see why all this is important soon!
(Actually, I wasn’t being strictly honest when I said you couldn’t multiply a
string. In fact, writing FirstName = “Bill” * 10 would make a string with
the value “BillBillBillBillBillBillBillBillBillBill”!)
Chapter 7: Naming Conventions and Comments
Variables are one of the most common elements in programming because
they allow things to change in a controlled manner – thus allowing for
dynamic programs that change over time or in response to input.
One way in which variables are different in other programming languages
though, is that they must first be defined. In Python, we can simply say:
x=3
At any point in our program and then ‘x’ will be created with the value ‘3’.
In Java though (another popular programming language), x would first have
to be created with a statement like:
Int x;
x = 3;
Part of the reason for this is that other programming languages use a much
wider range of variables – such a ‘Booleans’ which can only equal one or
zero. Because Python only has a few types of variable, it doesn’t need as
much structure.
What’s also important though is to think carefully when naming your
variables. The objective here is to write our code in such a logical and
elegant way that a complete stranger could instantly know what it does –
that will help if you ever need to collaborate on a project, or if you come
back to old code you’ve written.
When using variables in Python, it’s a good idea to use proper case. We’ve
seen that commands like ‘print’ are normally lower case, so if we use
proper case or capitals for our variables, then they will stand out very easily
when reading through.
So if you are using a string to refer to a user’s name, then what should you
call it? A good option would be: UserName or Name . This will make life
much easier than if you call it Rabbit1112 …
Another tip is to try and choose variables that read well as though they were
written in English when they’re inserted into the code. We’ll touch on this
more in future.
Using Comments
To make finding your way around just that little bit simpler, note that you
can also use comments. ‘Comments’ are lines of plain English that you can
insert into your code. The compiler will ignore these lines when you run
your program but it means you can add instructions, notes and references
for yourself or other members of your team.
To insert a comment, simply precede your line with a ‘#’ symbol. The
comment will then extend to the very end of the physical line. If you’re
coding using IDLE, then the comment will instantly change to a red font
when you get it right:
Think ahead while writing your programs as taking a little time to keep
your code clean now can save you a real headache later on!
Chapter 8: Handling Inputs
At the moment, all the programs we’ve written do is to output information.
As you can imagine… this isn’t very useful! If you’ve never written code
before, you might even be wondering why any of this matters. After all, if
you wanted to say ‘Hello World’ you could just write it on a piece of
paper…
To solve this problem then, we need to start inviting the user to join in and
actually interact with the software we’ve written. One way we can do this is
with the command ‘input’. The great news is that input is actually very easy
to use! Essentially, this just lets the user assign a value to a variable.
So instead of saying:
UserName = “Bill”
You say:
UserName = input(“Please enter your name: ”)
This now means that the program will add a prompt asking the user to enter
their name:
Once the user acquiesces to our request, UserName will then equal
whatever they said. This means we can say:
UserName = input(“Please enter your name: ”)
print(“Hello “ + UserName )
Now, whatever the user says their name is, we can greet them using it!
Congratulations are in order: you just created your very first interactive
program!
(Note that if you’re using Python 2, you’ll need to use raw_input instead of
input . This is our first example of where Python 2 and Python 3 differ – but
it’s no biggie!)
So what else can we do with this? Well of course we can also use the input
command in order to capture and set other types of data. Such as integers!
For example:
UserAge = input(“Please enter your age: ”)
print(“You are “, int(UserAge))
In this example there are a couple of other things going on but you can
probably work out some of it.
First of all, we’re creating a string called UserAge just as we did before.
The ‘prompt’ is asking for the age and input will automatically be
converted into an string.
From there, we are then printing the output. Note that we can’t use ‘+’
when adding a string to an integer, so instead we’re just using a common to
list both values one after the other.
And next, we are converting the value that user gave us into a string by
using the int(UserAge) line. You can also convert integers into strings by
saying str(Number). This way you could convert 4 into “4” .
Let’s have a bit more fun and see what we can do with this information and
a little math…
UserAge = input("How old are you? ")
print(int(UserAge), "is sooo old!")
YearsTo100 = 100 - int(UserAge)
print("In ", YearsTo100, "years, you'll be a hundred!!")
Now you have a program that is basically very rude about your age and that
tells you how many years until you’re 100. Nice!
Or what if we take it one step further?
UserAge = input("How old are you? ")
print(int(UserAge), "is sooo old!")
YearsTo100 = 100 - int(UserAge)
print("In ", YearsTo100, "years, you'll be a hundred!!")
print("That is ", int(UserAge) * 360, " days! Or ", (int(UserAge) * 360) *
24, " hours. Man...")
That extra line means it can now tell us how many hours and days we’ve
been alive. I’m 28, which I’m informed is 10,080 days or 241,920 hours.
The program might be a little offensive… but it’s also starting to pull
together quite a few different things we’ve learned! At this point we’re able
to accept input to create new variables, then perform operations with those
variables and present an answer on the screen!
Loops
Input is one of the most useful operations you’ll find yourself using at first
when you program. But this is still only scratching the surface of what
you’ll be able to do as you learn more and more Python.
Another powerful tool for instance is to use loops. Loops essentially allow a
piece of code to keep running and repeating itself.
There are two different types of loops, which operate based on different
conditions. One of the most common examples of a loop for example is the
‘While’ loop. The clue as to how this loop works is very much in the name.
Basically, a while loop runs while something is true.
So for instance, we could say:
Number = 0
while Number < 100:
Number = Number + 1
print(“Counted to”, Number)
All this would then do is to print numbers from 0-100 on the screen. The
while command is followed by a conditional statement (Number < 100 )
and everything that follows the colon and is indented will then happen
while that loop runs. If we remove the indentation, then this signifies that
we have come to the end of the loop. So the final line in this section of code
will only show once the loop has completed:
Number = 0
while Number < 100:
Number = Number + 1
print("Counted to ", Number)
print("Finished and counted to 100")
Leaving a space between the lines makes no difference.
This is another situation where it becomes very important to be careful with
formatting and planning. Simply pressing backspace and accidentally
deleting an indentation is enough to prevent the code from working
properly! This is quite different to other types of code such as java or C#
where the loops are contained within curly brackets {like this} .
Note that if we hadn’t included the line Number = Number + 1 then the
loop would have run forever!
The other type of loop we can use is the ‘For’ loop. For basically performs
some kind of counting job and finishes once that job is over. For example,
this:
for Counting in range(100):
print(Counting )
Does the same thing that the previous loop did – counts to one hundred! But
we could also do something else interesting – for example we could do this:
for Counting in range(10, 100):
print(Counting)
And thereby count from 10 to 100!
This is another example of something that might seem rather pointless
currently. Why would you want to count to 100 this way when you have
other methods? Well, one example might be to look through a list of items.
What else can you do with loops? Well, one option is to ‘break’ the loop.
This simply terminates the loop early and then continues with the rest of the
program. This can be used in the ‘while’ and ‘for’ loops and is something
we’ll come across later on…
One way that it might be useful as an example though, is if we use while
True . This statement (notice the capital ‘True’) simply runs our loop until
we say otherwise with break .
Another one that will come in handy is ‘continue’. This works similarly to
the ‘break’ statement, except that it simply begins the loop again from the
start. And if you were ‘For’ loop, then the condition that you were testing
would also be reset. Again, it will all make perfect sense soon…
Chapter 9: Working with the Conditional Statements
There are going to be times when you will work on a program in Python
where you will need the program to make some decisions for you, without
you being there to tell it how to answer each response that it gets. You can
set up the conditions that need to be met to help out with this but there is a
lot of unknown when the user is allowed to add in any input that they want,
and the conditional statements are going to help you learn how to do this.
Since you are not able to handle or guess all of the answers or inputs that
the user may give, and so these conditional statements are going to be there
to help you get things done.
You will find that these conditional statements are going to be a bit different
than what you are used to, but they can work on a lot of different programs
that you try to write. They are simple, but it is easy to add to them and
change things up so that you are able to get more done, and you can handle
any of the answers that your users decide to use
When it comes to working with these conditional statements, there are
going to be three options that you are able to work with. These are the if
statement, the elif statement, and the if else statement. These are all
important, but they are going to work in slightly different ways in order to
help you to get some of the codings that you want to be done. Let’s explore
how each of these is going to work inside your own code.
Out of the conditional statements, the most basic of the three and the one
that we are going to focus on first is going to be the if statement. The if
statement is a basic conditional statement, and sometimes is going to seem
too simple to use on a regular basis. In fact, it is more common that you are
going to work with the if else statement or the elif statement when in this
kind of field, but it is still important to learn how to work with the if
statement so that we get some of the basics that come with this kind of
conditional statement:
You can probably already guess that this will cause some problems with
most codes, but it is still important to know how to use these statements. A
simple code that you can work with for these conditional statements
include:
age = int(input(“Enter your age:”))
if (age <=18):
print(“You are not eligible for voting, try next election!”)
print(“Program ends”)
There are a few things that will show up with this code. If you have a user
go to the program and state that their age is under 18, then the program will
work and display the message that is listed there. The user can read this
message and then end the program right there.
Now one thing to notice here is that there is a chance for things to not go as
planned when you are working with this kind of code. If the user wants to
put in an answer for their age that is 18 or higher, then the code is not set up
at this point to handle this issue. Right now, the program is going to only
list out the message that you have when the user puts their age in as
something under 18. But if they wrote out their age as something higher,
then it is not going to meet up with the conditions that you have, and
nothing will happen.
Of course, we do not want to have a code where only one answer is right, at
least now when it comes to the age of the person using the program. They
want to be able to put in their real age at the time, and this may or may not
be below 18. You want to make sure that your program is working the way
that you want, and this is why we will move on to the if else statements.
While the if statements are a good place to get some practice when it comes
to writing your own codes in Python, there are not going to be a ton of
times in programming when you would want to use these at all. It is likely
that you don’t want to leave the screen empty with nothing there for the
user to see. And this is why we want to learn how to work with the if else
statement to handle the issues from above.
The if else statement is going to take the things that we have looked at in
the if statement, but moves them along another step so they make more
sense and can handle more situations. With the example that we had worked
on before, the user is going to be able to see the message we had before if
they put in any age under 18. But then this goes a bit further because it is
also going to provide us with a message if the user is older than 18 as well.
This if else statement is going to make sure that the user is going to get
some kind of message, no matter what age they put into the system at the
time.
With the voting example that we had above, you can implement the
following code to make an if else statement:
age = int(input(“Enter your age:”))
if (age <=18):
print(“You are not eligible for voting, try next election!”)
else
print(“Congratulations! You are eligible to vote. Check out your local
polling station to find out more information!)
print(“Program ends”)
With this option, you are adding in the else statement, which will cover
every age that doesn’t fall under 18. This way, if the user does list that as
their age, something will still pull up on the screen for them. This can
provide you with more freedom when working on your code and you can
even add in a few more layers to this. If you want to divide it up so that you
get four or five age groups and each one gets a different response, you
simply need to add in more if statements to make it happen. The else
statement is at the end to catch everything else.
We can take this a bit further to see how things go. Let’s say that we want to
create a code using the if else statement in order to ask our users what their
favorite color is. You would continue on with the if statements based on the
colors you would like to choose. You are not able to list out all of the colors
that are available because this would take forever and be hard to work on
but maybe you choose five colors and give them if statements with a
message that is attached.
Then, if the user doesn’t pick one of those five colors that you put an if
statement with, it will be caught by the else statement as well. This ensures
that all answers are caught and responded to, without having to worry about
listing out and guessing all of the different possibilities that the user can put
in.
Now that we have had some time to explore a bit with the if statement and
the if else statement, it is time to take a look at how the elif statement is
going to be the same or different, and how you can use it in your code. You
will find that these add in a different kind of level to the codes that you are
writing, but they still have ease of use to them.
It is possible to write in as many of the elif statements as you would like to
the code, and then you also need to add in the else statement, as we did
above with the if else statement, to cover any of the other decisions that
need to be handled in the code. You can think of the elif statement like
something similar to some of the older games out there, the ones that had a
menu of options and allowed the user to pick from one of these. This is
pretty similar to how we are going to set up our elif statements.
When you are working on these elif statements, you are able to add in as
many different options as you would like to finish it up. Sometimes, you
may just add one, two, or three, but you are not limited by this and can add
in as many as you would like. Keep in mind though that the more elif
statements that you add into the code, the messier it gets and the more that
you have to write out. To see how the coding for the elif statements is going
to work, make sure to check out the following code:
Print(“Let’s enjoy a Pizza! Ok, let’s go inside Pizzahut!”)
print(“Waiter, Please select Pizza of your choice from the menu”)
pizzachoice = int(input(“Please enter your choice of Pizza:”))
if pizzachoice == 1:
print(‘I want to enjoy a pizza napoletana’)
elif pizzachoice == 2 :
print(‘I want to enjoy a pizza rustica’)
elif pizzachoice == 3:
print(‘I want to enjoy a pizza capricciosa’)
else:
print(“Sorry, I do not want any of the listed pizza’s, please bring a Coca
Cola for me.”)
With this option, the user is able to choose the type of pizza they want to
enjoy, but you can use the same syntax for anything you need in your code.
If the user pushes the number 2 in the code, they are going to get a pizza
Rustica. If they don’t like any of the options, then they are telling the
program that they just want to have something to drink, in this case, a Coca
Cola.
This is a simple way to use elif statements to give the user a set of choices.
In the other options, the user could add in any choice that they wanted, but
in the elif statement, they can either pick that they want one of the choices
that you provide, or they will have to go with the default option at the end.
This can work well for many games that you may want to pick, for some
tests online, and other programs where you want to limit the choices that
the user gets to pick from in the code.
As you can see, there are a lot of different things that you are able to do
when working on these conditional statements. You are able to choose what
conditions you would like to have in place, and then make sure that the
program is going to behave in the right way the whole time. You can choose
whether the if statement, the if else statement or the elif statement is going
to be the right one for you, and then add these to the code that you are
writing.
Chapter 10: Exception Handling in the Python Language
The next topic that we need to spend some time exploring in this guidebook
is the idea of exception handling. There are going to be times when your
code tries to show up some errors or other problem inside the code that you
are doing, and this is where the exception is going to occur. Knowing when
to recognize these exceptions, how to handle them, and even how to make
some of your own can make a big difference in how well you are able to do
some of your own coding in Python.
These exceptions are going to be brought up by the compiler in a lot of
different situations, and sometimes, they are going to look like a simple
error message like you may have seen on your own computer before. It is
important to really read through any of these exceptions that come up in
your code and read through them so that you can learn what they are about,
and how you can handle them properly at the same time. This isn’t meant to
be something to scare you, but there are things that you can do to handle
these exceptions and to make sure that you are going to be able to handle
them in the proper manner.
Now, there are going to be a few exceptions that are already recognized
inside the library that you are using in Python. If you want to write out a
code with them in there, or if you find that the user is trying to do
something that isn’t proper for your program, then the compiler is going to
send out an exception about this in order to get it to stop. In some cases, the
program you are writing is going to need some extra limitations in place to
handle it, and you can raise up, after creating, your own exception.
A good example of an exception that your compiler is automatically going
to raise up is when you or the user is to divide it by zero. The compiler is
then going to recognize that this is something the user is not able to do, and
it is going to send out that exception as an alert. It can also be something
that is going to be called up if you, as the programmer, are trying to call up
a function and the name is not spelled in the proper manner so there is no
match present to bring up.
There are a few different exceptions that are automatically found in your
Python library. It is a good idea to take some time to look through them and
recognize these exceptions so you can recognize them later on. Some of the
most common exceptions that you need to worry about include:
Finally—this is the action that you will want to use to perform cleanup
actions, whether the exceptions occur or not.
Assert—this condition is going to trigger the exception inside of the code
Raise—the raise command is going to trigger an exception manually inside
of the code.
Try/except—this is when you want to try out a block of code and then it is
recovered thanks to the exceptions that either you or the Python code
raised.
Raising an Exception
The first thing that we need to take a look at, now that we know a bit more
about these exceptions and what they mean, is how to write one out, and
some of the steps that you can use if one of these does end up in your own
code. If you are going through some of the code writing, and you start to
notice that an exception will be raised, know that often the solution is going
to be a simple one. But as the programmer, you need to take the time to get
this fixed. To help us get started here, let’s take a look at what the syntax of
the code for raising an exception is all about.
x = 10
y = 10
result = x/y #trying to divide by zero
print(result)
The output that you are going to get when you try to get the interpreter to
go through this code would be:
>>>
Traceback (most recent call last) :
File “D: \Python34\tt.py”, line 3, in <module>
result = x/y
ZeroDivisionError: division by zero
>>>
As we take a moment to look at the example that we have here, we can see
that the program is going to bring up an exception for us, mainly because
we are trying to divide a number by zero and this is something that is not
allowed in the Python code (and any other coding language for that matter).
If you decide not to make any changes at this point, and you go ahead and
run the program as it is, you could end up with the compiler sending you an
error message. The code is going to tell the user the problem, but as you can
see, the problem is not listed out in an easy-to-understand method and it is
likely the user will have no idea what is going on or how they can fix the
problem at all.
With that example that we worked on above, you have some options. You
can choose to leave the message that is kind of confusing if you don’t know
any coding, or you can add in a new message that is easier to read and
explains why this error has been raised in the first place. It won’t have a lot
of numbers and random letters that only make sense to someone who has
done coding for a bit, which makes the whole thing a bit more user-friendly
overall. The syntax that you can use to control the message that your user is
going to see includes:
x=10
y=0
result = 0
try:
result = x/y
print(result)
except ZeroDivisionError:
print(“You are trying to divide by zero.”)
Take a look at the two codes above. The one that we just did looks a little
bit similar to the one above it, but this one has a message inside. This
message is going to show up when the user raises this particular exception.
You won’t get the string of letters and numbers that don’t make sense, and
with this one, the user will know exactly what has gone wrong and can fix
that error.
Can I define my own exceptions?
In the examples above, we took some time to define and handle the
exceptions that the compiler offered to us and are already found in the
Python library. Now it is time for us to take it a bit further and learn how to
raise a few of our own exceptions in any kind of code that we want to write.
Maybe you are working on a code that only allows for a few choices to the
user, one that only allows them to pick certain numbers or one that only
allows them to have so many chances at guessing. These are common
things that we see when we work with gaming programs but can work well
in other programs that you design.
When you make these kinds of exceptions, the compiler is going to have to
be told that an exception is being raised, because it is not going to see that
there is anything wrong in this part of the code. The programmer has to go
in and let the compiler know what rules it has to follow, and what
exceptions need to be raised in the process. A good example of the syntax
that you can use to make this happen in your own code will be below:
class CustomException(Exception):
def_init_(self, value):
self.parameter = value
def_str_(self):
return repr(self.parameter)
try:
raise CustomException(“This is a CustomError!”)
except CustomException as ex:
print(“Caught:”, ex.parameter )
In this code, you have been successful in setting up your own exceptions
and whenever the user raises one of these exceptions, the message of
“Caught: This is a CustomError!” is going to come up on the screen. This is
the best way to show your users that you have added in a customer
exception into the program, especially if this is just one that you personally
created for this part of the code, and not one that the compiler is going to
recognize on its own.
Just like with the other examples that we went through, we worked with
some generic wording just to show how exceptions are able to work. You
can easily go through and change this up so that you get a message that is
unique for the code that you are writing and will explain to the user what is
going on when they get the error message to show up.
Learning how to work with some of the exceptions that can come up in
your code is one of the best ways to make sure that your codes work the
way that you want, that the user is going to like working with your
program, and that everything is going to proceed as normal and do what
you want. Take some time to practice these examples and see how they
can work for you in order to handle any of the exceptions that come up in
your code.
Chapter 11: Functions, Classes and Methods
This methodology allows us to create objects that will contain data, together
with various attributes and methods that describe it or perform an action on
it. Therefore, you can say that a Python object is nothing more than a
collection of data types and some parameters that describe them or give
them additional functionality.
In this chapter you will be introduced to the concept of object oriented
programming. You will learn the basics behind the methodology and learn
how to define a class and a method.
Introduction to OOP
Objects are built into classes of objects and then we apply a number of
methods which will affect anything that belongs to a class. The theory
might start sounding a bit confusing because we started exploring the
slightly more advanced programming notions, but look at it this way.
Imagine you have a certain car and you define it as a class. The car, just like
the class, has a number of attributes and behaviors that are described. In the
case of your car, these attributes will be the brand, model, fuel type, color,
number of seats, year, etc. The behavior is described by a method which
belongs to the class. Since this is a car, you will probably have a start,
accelerate, and stop method at least. Now that the car class is defined, we
can build a number of objects that will have all of these attributes and
methods in common. Let’s say you will have a Mercedes as your first object
and its attributes will describe the fact that it runs on diesel fuel and that it
was manufactured in 2017. Since the Mercedes is a car, it will share the
same methods as the car class. It can start, accelerate, and stop. Next, you
can create another car object that shares the same information. As you can
see, object oriented programming makes it much easier to create something
with a lot less code. You don’t have to define everything again and again.
All you need is the schematic for the class and then create derivative objects
that share various features with the parent class.
Now let’s get back to a little more theory before putting everything into
practice. Keep in mind that object oriented programming is something that
you will have to eventually master because you cannot create powerful and
efficient applications without it. With that being said, there are four major
principles behind OOP:
With the free throws, one player misses 0.75% of the time
Lowest score is 3
plt.pie(dish_content[0 ][1 :4 ],
autopct='%1.1f%%',
labels=('carbs','fat','protein'),
colors=('palegreen','gold','salmon'))
plt.show()
return make_table(header[:-1 ],dish_content)
The function itself must draw a table. For a header, we can use the header
we have read from a file, discarding the last element "serving size".
Variabledish_content will contain table data as a list of rows .
Rows are lists containing the ingredient name in the first element followed
by the contribution of the given ingredient to the total number of carbs, fats,
proteins, and calories in the dish.
The first row shows the total content in a dish. So, we create a
row["Total:",0,0,0,0] and calculate the total carbs, fats, proteins, and
calories later when the contributions of all the ingredients are known.
The contribution of each component is calculated by a functionget_share.
Finally, we go through the columns of the created table and fill the
Totalvalues in the first row by summing up the values of each column. List
comprehension ondish_contentis used to get a list of values in each column,
and a standard librarysum() function is used to sum them up. The column
sums are stored in the first row of the table.
The total values of the 3 major nutrients are then used to create a pie chart
for the dish content. The functionpie from module matplotlibtakes the
amount of carbs, fats, and proteins, and draws a pie chart using them.
Parameters, labels, and colors are self-explanatory and define the labels and
colors of the sectors, while the parameterautopctdefines a formatting string
for the percentage of each ingredient in a dish. It means that the percentage
should be printed as a floating point number with at least one digit before
the decimal point and one after the decimal point, followed by
'%'.plt.show() to show the chart in the notebook .
Finally, the functionshow_resultscalls amake_table with a header and a list
of rows and returns the HTML element, which ensures the table will be
shown in the notebook.
To create a user interface and an event loop that will automatically call
theshow_resultsfunction, we use the functioninteractimported
fromipywidgets by giving it the name of the function to call when user
changes any controls:
interact(show_results)
Here is the entire listing:
from IPython.display import HTML
from ipywidgets import interact
import matplotlib.pyplot as plt
%matplotlib inline
data= {} # create an empty list to hold data
with open ('ingredients.txt', 'rt') as f:
for i,line in enumerate (f):
fields=line.strip().split('\t ')
if i==0 :
header=fields # remember a header
continue
food=fields[0 ].lower()
try:
numbers=[float (n) for n in fields[1 :]]
except:
print(i,line)
print(i,fields)
continue
numbers=[float (n) for n in fields[1 :]]
data [food ]=numbers # append food info to data list
print(data)
def make_table(header,data):
# Constructing a HTML table
html_string='<table border="1">'
html_string+='<tr style="background: #ccc;"><th>'
# make header row
html_string+='''</th><th style="width: 7em;
text-align:right;">'''.join(header)+"</th></tr>"
for food in data:
food_list=[str (a) for a in food]
# make data row
html_string+="<tr><td>"
html_string+='''</td><td style="width: 7em ;
text-align:right;">'''.join(food_list)
html_string+="</td></tr>"
html_string+="</table>"
# Create a html object from a string
return HTML(html_string)
def get_share(food, quantity):
serving=data[food][4 ]
nutrients=[round (a*quantity/serving,2 )
for a in data[food][:4 ]]
return nutrients
def show_results(chicken=(0.0 ,100.0 ),
pasta=(0.0 ,100.0 ),
parmesan=(0.0 ,100.0 )):
dish_content=[['<b>Total:</>',0 ,0 ,0 ,0 ]]
dish_content.append(['chicken']+
get_share('chicken breast', chicken))
dish_content.append(['pasta']+
get_share('pasta', pasta))
dish_content.append(['parmesan']+
get_share('parmesan grated', parmesan))
for column in range (1 ,5 ) :
column_sum=sum ([a[column]
for a in dish_content[1 :]])
dish_content[0 ][column]=round (column_sum,2 )
plt.pie(dish_content[0 ][1 :4 ],
autopct='%1.1f%%',
labels=('carbs','fat','protein'),
colors=('palegreen','gold','salmon'))
plt.show()
return make_table(header[:-1 ],dish_content)
interact(show_results)
If you find calculations to be time consuming, you might consider another
function -interact_manual. It adds a button to the interface and its target
function will only be called when the button is pressed. So you can adjust
several controls without waiting for unnecessary calculations to complete.
Interact also offers other kinds of widgets: check box, text entry box, drop
down list. The widget kind is guessed from the default arguments of a target
function. The following snippet will create an interface that consists of a
check box, text entry box, drop down list and a Run button you need to
push to call the target function that prints the parameters it receives.
from ipywidgets import interact_manual
def target( a=True ,
b='abc',
c=['one', 'two', 'three'],
d={'apple':1 , 'pear':2 , 'orange':3 }):
print(a,b,c,d)
return
i=interact_manual(target)
Alternatively, an interactive function might be used to create a control box
that serves as a parent of multiple widgets. These widgets might be
constructed by explicit constructor calls, which gives the user more control.
In addition, the state of those controls might be altered from within a
program. See IPython documentation for more information on available
controls.
Chapter 15: The Regular Expressions
We need to take a little detour here and explore a bit what we are able to
do with the regular expressions when working in the Python language. One
of the things that you are going to enjoy when it comes to the larger library
that is in Python is that you can work with something that is known as a
regular expression, or an expression that is responsible for handling any
kind of task that you would like without all of the glitches, and that are able
to handle all of the different searches that you want to do with these.
You will find that working on these regular expressions are going to be
good to use in Python because it helps us to go through a large variety of
text, including text strings if you would like, and it is possible to use these
types of regular expressions to check out the string or the text in your code
to double-check whether everything is going to match up in the code in the
proper manner or not.
These regular expressions can actually be nice to work with and when
you would like to work on one, you can need to stick with the same kind of
expression through it, even if you are going to work with another kind of
coding language along with Python. Let’s say that you are doing some work
and you want to code with not only Python but also with other languages
like C++ or Java. You would still be able to work with the regular
expressions and stick with the syntax that you are familiar with when
working on Python.
At this point, we have talked a bit about the regular expressions, but we
need to dive in a bit deeper and get more information about it. One of the
methods that you can use when it is time to explore these regular
expressions is to do a search through the code for a word that you may have
spelled out in different ways for your text editor. Maybe you went through
and typed out blue in one part, and bleu in another part and you want to get
it fixed out, the regular expressions are going to make it easier for you to
see it happen.
Any time that you would like to work with some of these regular
expressions, it is important to start out by going to the library with Python
and then importing the expressions that they have there. You need to do this
right now before we start to go much further, or it can be a challenge to do
the work later on. Think about all of the different kinds of libraries and
extensions that you will need at the beginning of any project and then add
these in as well.
Basic Patterns
One of the things that a programmer is going to notice when they start
with the expressions is that these don’t just specify out the character
that is fixed that you want to use in the code. In fact, it is possible to
bring them out in some cases and use them to find all of the patterns
that are going to show up in your code. Some of the different patterns
that you need to show up in your statement, as well as in some of the
other parts of a Python code, will include:
One of the ways that you are going to be able to use these regular
expressions is to help you complete a query that you would like. There are
other tasks, but we are going to focus on the idea of using a query to get
things done. The three methods of doing a query with a regular expression
that we are going to focus on are the re.findall(), re.search(), and re.match()
functions. Let’s take a look at what these can do and when we would be
able to use these in our code.
First on the list is going to be the search method. To work with this, the
syntax is going to include the function of search(). This is the one where
you are able to match up things that show up at any location of the string.
There aren’t going to be some restrictions that you have to worry about
when we work on this one, which makes it easier.
With the search method, you get the ability to check whether or not
there is some kind of match that is found in the string. Sometimes, there
will be a match and sometimes, there won’t be based on the query that you
make and what is in the code. If there are no matches in that string, then
you won’t get a result out of this. But if you do the query and the program
comes up with a match within the string, no matter where it is found, then
the result will be given back. With this one, it is only going to return the
information once. There could be ten times the item is listed out, but this
one will just show you that it is there, and how many times it is there. The
code syntax that you are able to use with this one includes :
import re
string = ‘apple, orange, mango, orange’
match = re.search(r’orange’, string)
print(match.group(0))
The second thing that you can do with this is the match method. You
can use this option in the same kind of code that we had before, but it is
going to go through and look to see if the first word in the sequence is
going to match up with your search. If the term is the first word in the
sequence, then it is going to show up. If it is not, then you won’t be able to
get the term that you would like.
In the example above, we would be looking for the orange and seeing if
we could find a match that goes with it. But since orange is not listed out as
the first word in that sequence, we would not be able to get a match, even
though the word orange is present. For this one to work, we need to have it
match up right in the first term that is there.
The third thing that we can work with here is the findall method. If you
would like to do some work and look at a string, and then get a statement to
show up to release all of the possibilities for one word out of the string, then
this is the type of method that you would need to use. So, if you would like
to use the code above and then figure out how many times the word orange
shows up, you would want to work with the findall method instead .
So to keep this one simple and to allow it to work the right way, you
would just need to use the syntax that we talked about above and switch out
the part with the re.search() over to re.findall(). Then you would get a new
result. For this one, since we are looking at orange, we would be able to get
the result of “orange, orange” in the end. This is because this method is
going to be used to tell us if there are patterns or how many times that a
specific word or phrase is going to show up in the code. If you had put the
word in five times in the code above, then the findall method would be able
to list out the word orange five times as a result.
As you can see, all three of these regular expressions are going to work
in a manner that is different in order to help you work on the codes that you
want to write. Each of these methods will work in order to help you find the
information that you need, look to see whether there is a pattern found in
the information, and can help out with so much more. Take some time
practicing these to see how they are able to help you get more done in your
coding.
Chapter 16: Data Visualization with Python
Data visualization can be described as the various ways by which analyzed data
i.e. information is displayed. Sometimes, even well-analyzed data is not
informative enough at a glance. With data visualization, which includes line
graphs, bar charts, pictograms, etc. the results/ analysis being presented become
less abstract to the end-user, and decision-making is enhanced. In this chapter,
we will be learning various techniques for displaying the results of our analysis
with NumPy and Pandas frameworks.
Matplotlib
This is a python library for producing high-quality 2D plots. For those that have
some MATLAB experience, the plotting techniques and visualizations here will
seem familiar. Matplotlib offers a lot of flexibility with plots, in terms of control
over things like the axes, fonts, line styles and size, etc. However, all these
require writing extra lines of code. So, if you do not mind going the extra mile
(with typing code) to fully specify your plots, then matplotlib is your friend. For
extra information about this package, visit the official page at
www.matplotlib.org
There are basically two approaches to plotting data in matplotlib: The
Functional and the object-oriented (OO) approach, respectively. You might
encounter the two terms consistently as you interact with programmers and
other programming languages, but they are just two slightly different
approaches to programming. We will only be considering the functional
approach here, as it is easy to understand for beginners and also requires writing
fewer lines of code. The OO method, however, offers more control over the
plots at the consequence of writing more lines of code.
To start off, let us create a simple cosine plot using the functional approach.
First, let us import the relevant libraries and create plot data:
In []: import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# creating plot values
x = np.linspace(0,10) # x-axis/time-scale
y = np.cos(x) # corresponding cosine values
The %matplotlib inline option in the code ensures all our plots are displayed as
we run each cell. If you are running a different python console, you can put
plt.show() at the end of your code to display your plots. plt.show() is the print()
function equivalent for matplotlib plots.
Functional method
In []: # functional plot
plt.plot(x,y)
Out[]: [<matplotlib.lines.Line2D at 0x25108cf27f0> ]
Notice that we get an Out[] statement. This is because we did not print the
result using plt.show(). While this is not significant if you are using Jupyter, it
might be required for other consoles.
We can also plot multiple functions in one graph.
In []: z = np.sin(x) # Adding an extra plot variable
plt.plot(x,y,x,z);plt.show()
To print multiple graphs, just pass each argument to the plot statement, and
separate the plots with commas.
To make our graphs more meaningful, we can label the axes and give the graph
a title.
In []: plt.plot(x,y,x,z)
plt.xlabel('Time axis') # labelling x-axis
plt.ylabel('Magnitude') # labelling y-axis
plt.title('Sine and Cosine waves') # graph title
plt.show() # printing
Output:
Now, this is a better figure. Since we added more than one plot, there is an extra
functionality called ‘legend’ which helps differentiate between the plots.
The legend function takes in two arguments. The first is usually a string
argument for labeling the graphs in order. The second is for extra functionality,
like where the legend should be. The location argument is specified using
‘loc=value’. For value = 1, upper right corner; 2, for upper left corner; 3, for
lower left corner; and 4 for the lower right corner. However, if you would rather
let matplotlib decide the best location, use ‘value=0’.
In []: plt.plot(x,y,x,z)
plt.xlabel('Time axis')
plt.ylabel('Magnitude')
plt.title('Sine and Cosine waves')
plt.legend(['y','z'],loc=0) # loc=0 means best location
plt.show()
Output:
Assuming we wish to plot both the cosine and sine wave above, but side by
side.
We can use the subplot command to do this. Think of the subplot as an array of
figures with a specification of the number of rows and columns. So, if we want
just two graphs beside each otherhat can be considered as a 1 row, and 2
columns array.
In []:# subplotting
plt.subplot(1,2,1) # plot 1
plt.plot(x,y)
plt.title('Cosine plot')
plt.subplot(1,2,2) # plot 2
plt.plot(x,z)
plt.title('Sine plot')
plt.tight_layout() # avoid overallping plots
plt.show( )
Tip: The tight_layout() line ensures that all subplots are well spaced.
Always use this when sub-plotting to make your plots nicer. Try removing
that line and compare the output!
Output:
To explain the subplot line i.e. subplot(1,2,1) and subplot(1,2,2): The first two
values are the number of rows and columns of the subplot. As seen in the above
result, the plot is on one row, and two columns. The last value specifies the
order of the plot; hence (1,2,1) translates to plotting that figure in the first row,
and the first column out of two.
We can specify the line colors in our plots, as well as the line style. This is
passed as a string after the plot argument. Since all the plot options, including
marker style and the likes are exactly same as for Matlab, here is a link to
Matlab plot documentation to explore all the extra customization feature you
can port to your matplotlib plots:
https://www.mathworks.com/help/matlab/ref/plot.html#btzpndl-1
Let us change the color and fonts in our subplots to illustrate this.
In []: plt.subplot(1,2,1) # plot 1
plt.plot(x,y,'r-x') # red plot with -x marker
plt.title('Cosine plot')
plt.subplot(1,2,2) # plot 2
plt.plot(x,z,'g-o') # green plot with -o marker
plt.title('Sine plot' )
plt.tight_layout() # still avoiding overallping plots
plt.show()
Output:
Exercise: Now that you have learned how to plot using the functional approach,
test your skills.
Plot this:
Tip: use np.arange(0,10,11) for the x-axis.
After creating your plot, you may need to import it into your documents or just
save it on your device. To do this, you can right-click the image in your Jupyter
notebook and click copy. Then, you can paste the copied image into your
document.
If you prefer to save, you can use the plt.savefig(‘figurename.extension’, DPI =
value) method. Here, figurename is the desired name for your saved image; The
extension is the desired format i.e. PNG, JPG, BMP, etc. Finally, the DPI
specifies the quality of the image, the higher the better. Usually for standard
printing quality, about 300 is good enough.
Tip: Learning a bit more about these specifications can really help you
generate better images from your plots.
Seaborn
This is another data visualization library that extends the graphical range of the
matplotlib library. A lot of methods from matplotlib are applicable here, for
customizing plots. However, it generates high quality, dynamic plots in fewer
lines of code .
Seaborn is more optimized for plotting trends in datasets, and we are going to
explore a dataset using this library. Since, Seaborn is pre-loaded with a few
datasets (it can call and load certain datasets from its online repository), we will
just load up one of these for our example.
Example 77 : Loading a seaborn dataset and plotting trends
As with other packages, we have to import seaborn using the standard variable
name ‘sns’.
In []: # importing seaborn
import seaborn as sns
%matplotlib inline
Next, we will load the popular ‘tips’ dataset from Seaborn. This dataset
contains information about a restaurant, the tips given to the waiters, amount of
tip, size of the customer group (e.g. a party of 3 people) etc.
In []:# loading a dataset from seaborn
tips_dataset = sns.load_dataset('tips')
tips_dataset.head()
Out[]:
total_bill tip sex smoker day time size
sex
Female 87 87 87 87 87 87
As expected, notice how the male and female counts of 157 and 87 respectively
correspond with the countplot above.
These and many more are data visualization capabilities of Seaborn. For now,
these are some basic examples to get you started; you may visit the seaborn
official documentation gallery to explore more plots styles and options via this
link: https://seaborn.pydata.org/examples/index.html
Panda s
Well, it's our friendly pandas again. The library also has some highly functional
visualization capabilities. It is quite intuitive at the time to use these built-in
visualization options while working with pandas, unless something more
specialized is required.
First, we import a few familiar libraries:
In []: # importing all necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
You may wonder why all the other libraries apart from Pandas are imported.
Well, your outputs will look much better with these libraries synchronized.
Pandas plots using the matplotlib library functionality -- even though it doesn’t
directly call it, and the seaborn library makes the graphs/plots look better.
Let us work with a different dataset. We can create our own data frame and call
plots off it.
We will create a data frame from a uniform distribution.
In []: # let us create our dictionary
d = {'A':np.random.rand(5),
'B':np.random.rand(5),
'C':np.random.rand(5),
'D':np.random.rand(5) }
# now creating a data frame
df = pd.DataFrame(d)
df
Out[]:
A B C D