Python Programming 3 BOOKS IN 1 Learn Machine Learning, Data Science and Analysis With A Crash Course For Beginners. Included Coding Exercises For Artificial Intelligence, Numpy, Pandas and Ipython.
Python Programming 3 BOOKS IN 1 Learn Machine Learning, Data Science and Analysis With A Crash Course For Beginners. Included Coding Exercises For Artificial Intelligence, Numpy, Pandas and Ipython.
PROGRAMMING
3 BOOKS IN 1
Learn machine learning, data science and analysis with a
crash course for beginners. Included coding exercises for
artificial intelligence, Numpy, Pandas and Ipython.
JASON TEST
© Copyright 2020 - All rights reserved.
The content contained within this book may not be reproduced, duplicated or transmitted without direct
written permission from the author or the publisher.
Under no circumstances will any blame or legal responsibility be held against the publisher, or author, for
any damages, reparation, or monetary loss due to the information contained within this book. Either
directly or indirectly.
Legal Notice:
This book is copyright protected. This book is only for personal use. You cannot amend, distribute, sell,
use, quote or paraphrase any part, or the content within this book, without the consent of the author or
publisher.
Disclaimer Notice:
Please note the information contained within this document is for educational and entertainment
purposes only. All effort has been executed to present accurate, up to date, and reliable, complete
information. No warranties of any kind are declared or implied. Readers acknowledge that the author is
not engaging in the rendering of legal, financial, medical or professional advice. The content within this
book has been derived from various sources. Please consult a licensed professional before attempting
any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances is the author responsible for
any losses, direct or indirect, which are incurred as a result of the use of information contained within
this document, including, but not limited to, — errors, omissions, or inaccuracies.
TABLE OF CONTENTS
PYTHON FOR BEGINNERS
Introduction
1.Python code optimization with ctypes
2.Finding the Perfect Toolkit: Analyzing Popular Python Project Templates
3.How are broad integer types implemented in Python?
4.Create a bot in Python to learn English
5.The thermal imager on the Raspberry PI
6.Finding a Free Parking Space with Python
7.Creating games on the Pygame framework | Part 1
Creating games on the Pygame framework | Part 2
Creating games on the Pygame framework| Part 3
8.Object-Oriented Programming (OOP) in Python 3
Conclusion
JASON TEST
INTRODUCTION
Design patterns are reusable model for solving known and common
problems in software architecture.
hey are best described as templates for working with a specific normal
T situation. An architect can have a template for designing certain types of
door frames, which he fit into many of his project, and a software
engineer or software architect must know the templates for solving common
programming tasks.
An excellent presentation of the design pattern should include:
Name
Motivating problem
Decision
Effects
Equivalent Problems
If you thought it was a rather vague concept, you would be right. For
example, we could say that the following “pattern” solves all your problems:
Structural Patterns
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Behavioral patterns
Chain of responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
type checking:
expansion of the IDE functionality in terms of providing information
about the expected types of arguments and the type of return value for
functions:
overload of functions and work with generics:
interaction with other languages:
use in predicate logical functions:
mapping of requests in databases:
marshaling parameters in RPC (remote procedure call)
4. Python
Python is an even more powerful and popular programming language (by
the way, it really has nothing to do with python ). Like R, Python has special
libraries that work with Excel files, and it also knows how to collect
information from the Internet (forget about manually driving data into
tables!). You can write endlessly about Python, but if in a nutshell - it’s a
really convenient and quick tool that is worth mastering if you want to
automate routine operations, develop your own algorithmic thinking and
generally keep up with technical progress.
Unlike R, Python does not have one of the most convenient programming
environments - here everyone is free to choose to taste. For example, we
recommend IDLE, Jupyter Notebook, Spyder, for more advanced
programmers - PyCharm.
You can learn how to program in Python and analyze data using it in our
online course “Python. Data Analysis " . The consultants from Big4 and
Big3 will tell you how to work with libraries, create programs and
algorithms for processing information.
Well, as it were, let's go
Well, if you (You, He, She, It, They) are reading this means we are starting
our first Python lesson. Hooray (...) Today you will learn how to code. And
what's more, you'll learn how to code in Python.
But to start coding you need Python. Where to get it?
It's alife
Yes, you just wrote your first program. Now you are a programmer (NO).
Let's look at the structure of the print function. This function consists of the
name - print, and the place where we throw what this function should output.
If you want to display any text, you need to write it in quotation mark, either
in single or double.
But now let's enter some text. For this, Python has an input function. To
accept something, you need to write the following:
What does this program do for us? First we introduce some thing, at the
moment we don’t care what it is, text or numbers. After, our program
displays what you wrote.
But how to make sure that we write the text before entering some thing?
Really simple. There are two ways to do this.
How to do
But this is not aesthetically pleasing. We are here with you all the
aesthetes. And so in order for you to enter values on a separate line, you can
do the following:
We add up the two numbers that the user enters. (Wow, I found out how
much 2 + 2 will be)
1. PYTHON CODE OPTIMIZATION WITH
CTYPES
Content:
1. Basic optimizations
2. Styles
3. Python compilation
4. Structures in Python
5. Call your code in C
6. Pypy
Basic optimizations
efore rewriting the Python source code in C, consider the basic
B optimization methods in Python.
Built-in Data Structures
Python's built-in data structures, such as set and dict, are written in C.
They work much faster than your own data structures composed as Python
classes. Other data structures besides the standard set, dict, list, and tuple are
described in the collections module documentation.
List expressions
Instead of adding items to the list using the standard method, use list
expressions.
ctypes
The ctypes module allows you to interact with C code from Python
without using a module subprocessor another similar module to start other
processes from the CLI.
There are only two parts: compiling C code to load in quality shared
object and setting up data structures in Python code to map them to C types.
In this article, I will combine my Python code with LCS.c, which finds the
longest subsequence in two-line lists. I want the following to work in Python:
A few notes:
Sphinx
Most templates use Sphinx to generate documentation. To my surprise,
MkDocs is rarely used for this purpose.
As a result, we can say that if you do not use Flake8, pytest, and Sphinx in
your current project, then you should consider introducing them.
Promising tools
In this section, I collected those tools and techniques, the use of which in
the templates suggested some trends. The point is that although all this does
not appear in most project templates, it is found in many relatively recent
templates. So - all this is worth paying attention to.
Pyproject.toml
File usage is pyproject.tomlsuggested in PEP 518. This is a modern
mechanism for specifying project assembly requirements. It is used in most
fairly young templates.
Poetry
Although the Python ecosystem isn’t doing well in terms of an excellent
tool for managing dependencies, I cautiously optimistic that Poetry could be
the equivalent of npm from the JavaScript world in the Python world.
The youngest (but popular) project templates seem to agree with this idea
of mine. Real, it is worth saying that if someone is working on some kind of
library that he can plan to distribute through PyPI, then he will still have to
use setup tools . (It should to be noted that after the publication of this
material, I was informed that this is no longer a problem).
Also, be careful if your project (the same applies to dependencies) relies
on Conda . In this case, Poetry will not suit you, since this tool, in its current
form, binds the developer to pip and virtualenv.
Dependabot
Dependabot regularly checks project dependencies for obsolescence and
tries to help the developer by automatically opening PR.
I have recently seen this tool more often than before. It seem like to me
that it is an excellent tool: the addition of which to the project affects the
project very positively. Dependabot helps reduce security risks by pushing
developers to keep dependencies up to date.
As a result, I advised you not to lose sight of Poetry and Dependabot.
Consider introducing these tools into your next project.
Personal recommendations
Analysis of project templates gave me a somewhat ambivalent perception
of the tools that I will list in this section. In any case, I want to use this
section to tell about them, based on my own experience. At one time, they
were beneficial to me.
Pre-commit
Even if you are incredibly disciplined - do not waste your energy on
performing simple routine actions such as additional code run through the
linter before sending the code to the repository. Similar tasks can be passed
to Pre- commit. And it's better to spend your energy on TDD and teamwork
on code.
Pylint
Although Pylint is criticized for being slow, although this tool is criticized
for the features of its settings, I can say that it allowed me to grow above
myself in the field of programming.
He gives me specific instructions on those parts of the code that I can
improve, tells me how to make the code better comply with the rules. For a
free tool, this alone is already very much. Therefore, I am ready to put up
with the inconvenience associated with Pylint.
Black
Black at the root of the debate over where to put spaces in the code. This
protects our teams from an empty talk and meaningless differences in files
caused by different editors' settings.
In my case, it brightens up what I don't like about Python (the need to use a
lot of spaces). Moreover, it should be noted that the Black project in 2019
joined the Python Software Foundation, which indicates the seriousness and
quality of this project.
As a result, I want to say that if you still do not use pre-commit, Black,
and Pylint - think about whether these tools can benefit your team.
Subtotals
Twelve of the eighteen investigated templates were created using the
cookiecutter framework. Some of those templates where this framework is
not used have exciting qualities.
But given the fact that cookiecutter is the leading framework for creating
project templates, those who decide to use a template that did not use a
cookiecutter should have excellent reasons for this. Such a decision should
be very well justified.
Those who are looking for a template for their project should choose a
template that most closely matches his view of things. If you, when creating
projects according to a precise template, continuously have to reconfigure
them in the same way, think about how to fork such a template and refine it,
inspired by examples of templates from my list.
And if you are attracted to adventure - create your template from scratch.
Cookiecutter is an excellent feature of the Python ecosystem, and the simple
process of creating Jinja templates allows you to quickly and easily do
something your own.
Bonus: Template Recommendations
Django
Together with the most popular Django templates, consider using we
make-django-template . It gives the impression of a deeply thought out
product.
hen you write in a low-level language such as C, you are worried about
W choosing the right data type and qualifiers for your integers, at each
step, you analyze whether it will be enough to use it simply intor
whether you need to add longor even long double. However, when writing
code in Python, you don’t need to worry about these “minor” things, because
Python can work with numbers of integerany size type .
In C, if
you try to calculate 220,000 using the built-in function powl, you will get the
output inf.
But in
Python, making this easier than ever is easy:
It must be under the hood that Python is doing something very beautiful, and
today we will find out the exactly what it does to work with integers of
arbitrary size!
Presentation and Definition
Integer in Python, this is a C structure defined as follows:
Other types that have PyObject_VAR_HEAD:
PyBytesObject
PyTupleObject
PyListObject
This means that an integer, like a tuple or a list, has a variable length, and
this is the first step to understanding how Python can support work with giant
numbers. Once expanded, the macro _longobjectcan be considered as:
There PyObjectare some meta fields in the structure that are used for
reference counting (garbage collection), but to talk about this, we need a
separate article. The field on which we will focus this ob_digitand in a bit
ob_size.
Decoding ob_digit
ob_digitIs a statically allocated array of unit length of type digit (typedef
для uint32_t). Since this is an array, it ob_digitis a pointer primarily to a
number, and therefore, if necessary, it can be increased using the malloc
function to any length. This way, python can represent and process very large
numbers.
Typically, in low-level languages such as C, the precision of integers is
limited to 64 bits. However, Python supports integers of arbitrary precision.
Starting with Python 3, all numbers are presented in the form bignum and are
limited only by the available system memory.
Decoding ob_size
ob_sizestores the number of items in ob_digit. Python overrides and then
uses the value ob_sizeto to determine the actual number of elements
contained in the array to increase the efficiency of allocating memory to the
array ob_digit.
Storage
The most naive way to store integer numbers is to store one decimal digit
in one element of the array. Operation such as additional and subtractions can
be performed according to the rules of mathematics from elementary school.
With this approach, the number 5238 will be saved like this:
This approach is inefficient because we will use up to 32-bit digits
(uint32_t) for storing a decimal digit, which ranges from 0 to 9 and can be
easily represented with only 4 bits. After all, when writing something as
universal like python, the kernel developer needs to be even more inventive.
So, can we do better? Of course, otherwise, we would not have posted
this article. Let's take a closer look at how Python stores an extra-long
integer.
Python path
Instead of storing only one decimal digit in each element of the array
ob_digit, Python converts the numbers from the number system with base 10
to the numbers in the system with base 230 and calls each element as a
number whose value ranges from 0 to 230 - 1.
In the hexadecimal number system, the base 16 ~ 24 means that each
"digit" of the hexadecimal number ranges from 0 to 15 in the decimal number
system. In Python, it’s similar to a “number” with a base of 230, which means
that the number will range from 0 to 230 - 1 = 1073741823 in decimal.
In this way, Python effectively uses almost all of the allocated space of 32
bits per digit, saves resources, and still performs simple operations, such as
adding and subtracting at the math level of elementary school.
Depending on the platform, Python uses either 32-bit unsigned integer
arrays or 16-bit unsigned integer arrays with 15-bit digits. To perform the
operations that will be discussed later, you need only a few bits.
Example: 1152921504606846976
As already mentioned, for Python, numbers are represented in a system
with a base of 230, that is, if you convert 1152921504606846976 into a
number system with a base of 230, you will get 100.
1152 9215 0460 6846 976 = 1 * ((230) 2 + 0) * ((230) 1 + 0) *( (230) 0)
Since it is the ob_digitfirst to store the least significant digit, it is stored as
001 in three digits. The structure _longobjectfor this value will contain:
ob_size like 3
ob_digit like [0, 0, 1]
We created a demo REPL that will show how Python stores an integer inside
itself, and also refers to structural members such as ob_size, ob_refcountetc.
Integer Long Operations
Now that we have a pure idea of how Python implements integers of
arbitrary precision, it is time to understand how various mathematical
operations are performed with them.
Addition
Integers are stored "in numbers," which means that addition is as simple
as in elementary school, and the Python source code shows us that this is how
addition is implemented. A function with a name x_add in a file longobject.c
adds two numbers.
The code snippet above is taken from a function x_add. As you can see, it
iterates over a number by numbers and performs the addition of numbers,
calculates the result and adds hyphenation.
It becomes more interesting when the result of addition is a negative
number. The sign ob_sizeis an integer sign, that is, if you have a negative
number, then it ob_sizewill be a minus. The value ob_sizemodulo will
determine the number of digits in ob_digit.
Subtraction
Just as addition takes place, subtraction also takes place. A function with
a name x_sub in the file longobject.c subtracts one number from another.
The code snippet above is taken from a function x_sub. In it, you see how
the enumeration of numbers occurs and subtraction is performed, the result is
calculated and the transfer is distributed. Indeed, it is very similar to
addition.
Multiplication
And again, the multiplication will be implemented in the same naive way
that we learned from the lessons of mathematics in elementary school, but it
is not very efficient. To maintain efficiency, Python implements the Karatsuba
algorithm , which multiplies two n-digit numbers in O (nlog23) simple steps.
The algorithm is not simple and its implementation is beyond the scope of
this article, but you can find its implementation in functions and in the file
.k_mul k_lopsided_mul longobject.c
Division and other operations
All operations on integers are defined in the file longobject.c , they are
very simple to find and trace the work of each. Attention: A detailed
understanding of the work of each of them will take time, so pre-stock up
with popcorn .
Optimizing Frequently Used Integers
Python preallocates a small number of integers in memory ranging from -5
to 256. This allocation occurs during initialization, and since we cannot
change integers (immutability), these pre-allocated numbers are singleton and
are directly referenced instead of being allocated. This means that every time
we use / create a small number, Python instead of reallocation simply returns
a reference to the previously allocated number.
Such optimization can be traced in the macro IS_SMALL_INTand function
get_small_int c longobject.c . So Python saves a lot of space and time in
calculating commonly used integer numbers.
4. CREATE A BOT IN PYTHON TO LEARN
ENGLISH
o, this is not one of the hundreds of articles on how to write your first
N Hello World bot in Python. Here you will not find detailed instructions
on how to get an API token in BotFather or launch a bot in the cloud. In
return, we will show you how to unleash the full power of Python to the
maximum to achieve the most aesthetic and beautiful
code. We perform a song about the appeal of complex structures - we
dance and dance. Under the cut asynchrony, its system of saves, a bunch of
useful decorators, and a lot of beautiful code.
Disclaimer : People with brain OOP and adherents of the “right” patterns
may ignore this article.
Idea
To understanding what it is like not to know English in modern society,
imagine that you are an 18th-century nobleman who does not know French.
Even if you are not very well versed in history, you can still imagine how
hard it would be to live under such circumstances. In the modern world,
English has become a necessity, not a privilege, especially if you are in the
IT industry.
The project is based on the catechism of the future: the development of a
neural network as a separate unit, and education, which is based on games
and sports spirit. Isomorphic paradigms have been hanging in the air since
ancient times, but it seems that over time, people began to forget that the most
straightforward solutions are the most effective.
Here is a shortlist of the basic things I want to put together:
Base structure
The bot will be based on the python-telegram-bot (ptb) library. I use
loguru as a logger , though there is one small snag here. The fact is that ptb by
default uses a different logger (standard logging) and does not allow you to
connect your own. Of course, it would be possible to intercept all journal
messages globally and send them to our registrar, but we will do it a little
easier:
Unfortunately, I don’t have the opportunity to deploy my bot on stable data
centers, so data security is a priority. For these purposes, I implemented my
system of saves. It provides flexible and convenient work with data -
statistics collection, as an example of easy replenishment of functionality in
the future.
+ cache_si ze - Std thfDtlgh 'hi ch dl data will be su‘ed
+ cache_files - Dump file in w'hich all intermediate operations on data are
stored
link = None
def init (self. cache_si ze = 10):
= Save all screw'ed cl asses. Thi s all or's you t o fl exibl›' vork with
data.
sel f. cl asses = []
= Files matching classes
sel f._cache_fi1es = []
= (1). A sm dl hack that dl or's you to cd1 a specifi c instance thrDtlgh a
common class
= Thi s w'wks because w'e oril \' have one instance Df the cl ass. 'hi ch
= implements d 1 the logic of 'orking with data. In additi on. it is
convenient ari d all o 's
= st gum cantl›' expand the functl Dflallt›' in the future
sel f. cl ass .link = self
self._counter = 0
self.CACHE SIZE = cache st ze
def add (self, d s: class, file: str) -> NDne:
All or's to fasten a class to a sa ver
+ cls - Inst an ce of the class
+ file - The fi1 e the instari ce is 'orking with
self._cache_files.append (file)
self._cl asses.append (cls)
if fi1e i s eotptl’ (fi1e): return hDne
1 ogger. opt d an.' = True) .debug (fT or {cl s . class names)
file (file) is not empty' ')
fa data in sells oad (file) :
is.save nDn Caclti ng (data)
cl ear_file (file)
sel f._counter = 0
Now that we have figured out the basic structure, the main question
remains: how to put everything together. I implemented the main class -
EnglishBot, which brings together the entire primary structure: PTB, work
with the database, the save system, and which will manage the whole
business logic of the bot. If the implementation of Telegram commands were
simple, we could easily add them to the same class. But, unfortunately, their
organization occupies most of the code of the entire application, so adding
them to the same class would be crazy. I also did not want to create new
classes/subclasses, because I suggest using a very simple structure:
How command modules get access to the main class, we will consider
further.
All out of nothing
Ptb handlers have two arguments - update and context, which store all the
necessary information stack. Context has a wonderful chat_data argument that
can be used as a data storage dictionary for chat. But I do not want to
constantly refer to it in a format context.chat_data['data']. I would like
something light and beautiful, say context.data. However, this is not a
problem.
We continue to simplify our lives. Now I want all the necessary information
for a specific user to be in quick access context.
Now we’ll completely become impudent and fasten our bot instance to
context:
We put everything in place and get a citadel of comfort and convenience in
just one call.
It looks cool, but it doesn't work. It's all about the bind_context decorator,
which will always return the name of the wrapper function. Correct this
misunderstanding.
There are many message handlers in the bot, which, by design, should
cancel the command when entering zero. Also I need to discard all edited
posts.
We do not forget at the same time about the most important decorator -
@run_asyncon which asynchrony is based. Now we collect the heavy
function.
Remember that asynchrony is a Jedi sword, but with this sword, you can
quickly kill yourself.
Sometimes programs freeze without sending anything to the log.
@logger.catch, the last decorator on our list, ensures that any error is
correctly reported to a logger.
Admin panel
Let's implement an admin panel with the ability to receive/delete logs and
send a message to all users.
The add_conversation_handler function allows you to add a conversation
handler in a minimalistic way:
Main functionality
Let's teach our bot to add new words to user dictionaries.
We pack the bot
Before we pack our bot, add proxy support and change log levels from the
console.
There are two type of sensor is available version, the cases of which
differ in the viewing angle of the matrix. A more squat structure A overlooks
the outside world at an angle of 110 (horizontal) at 75 (vertical) degrees. B -
under 55 by 37.5 degrees, respectively. The device case has only four
outputs - two for power, two for communicating with the control device via
the I2C interface. Interested datasheets can be downloaded here .
And then what is the GY-MCU90640?
As you see, there are two types of boards, with a narrow or wide-angle
version of the sensor onboard.
Which version is best for you? A good question, unfortunately, I had it
only after the modules was already ordered and received. For some reason,
at this time of the orders, I did not pay attention to these nuances. But in vain.
A wider version will be useful on self-propelleds robots or in security
system (the field of view will be larger). According to the datasheets, it also
has less noise and higher measurement accuracy.
But for visualization tasks, I would more recommend a more “long-range”
version of B. For one very significant reason. In the future, when shooting, it
can be deployed (manually or on a platform with a drive) and take composite
"photos," thereby increasing the more than a modest resolution of 32 by 24
pixels. Collects thermal images 64 by 96 pixels, for example. Well, all right,
in the future, the photos will be from the wide-angle version A.
Connect to Raspberry PI
There are two ways to control the thermal imaging module:
1. Shorten the “SET” jumper on the board and use I2C to contact the
internal microcontroller MLX90640 directly.
2. Leave the jumper alone and communicate with the module
through a similar interface installed on the STM32F103 board
via RS-232.
If you write in C ++, it will probably be more convenient to ignore the
extra microcontroller, short-circuit the jumper and use the API from the
manufacturer, which lies here.
Humble pythonists can also go the first way. It seems like that there are a
couple of Python libraries ( here and here ). But unfortunately, not a single
one worked for me.
Advanced pythonists can write a module control driver in Python. The
procedure for obtaining a frame is described in detail in the datasheet. But
then you will have to prescribe all the calibration procedures, which seems
slightly burdensome. Therefore, I had to go the second way. It turned out to
be moderately thorny, but quite passable.
Thanks to the insight of Chinese engineers or just a happy coincidence, the
shawl turned out to have a perfect location of the conclusions:
It remains only to put the block and insert the scarf into the raspberry
connector. A 5 to 3 Volt converter is installed on the board, so it seems that
nothing threatens Raspberry's delicate Rx and Tx terminals.
It should be added that the connection according to the first option, is also
possible, but requires more labor and solder skill. The board must be
installed on the other side of the Raspberry connector (shown in the title
photo of this post).
Soft
On a well-known Chinese site, such a miracle is offered to access the GY-
MCU90640:
Most likely, there should be some description of the interaction protocol
with the microcontroller installed on the board, according to which this
software product works! After a brief conversation with the seller of scarves
(respect to these respected gentlemen), such a protocol was sent to me. It
appeared in pdf and pure Chinese.
Thanks to Google’s translator and active copy-paste, after about an hour
and a half, the protocol was decrypted, anyone can read it on Github. It
turned out that the scarf understands six basic commands, among which there
is a frame request on the COM port.
Each pixel of the matrix is, in fact, the temperature value of the object that
this pixel is looking at. The temperature in degrees Celsius times 100
(double-byte number). There is even a special mode in which the scarf will
send frames from the matrix to the Raspberry 4 times per second.
THE SOFTWARE IS PROVIDED ‘AS IS‘, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENf SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES Ofi OTHER LIABILITY, WHETHER IN
ANACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. ” “
norm
= up.nint8 ((f, 100 - Twin) • 255, (Tmax-Tmin))
norm.shy = (24.32)
Tn = 40
Tmin = 20
Results
The script polls the thermal imaging matrix and outputs the frames to the
monitor console on which the Raspberry PI is connected, four times per
second. This is enough not to experience significant discomfort when
shooting objects. To visualize the frame, the OpenCV package is used. When
the “s” button is pressed, the thermal imaging “heat maps” in jpg format are
saved in the folder with the script.
For more information, I deduced the minimum and maximum temperatures
on the frame. That is, looking at the color, you can see what approximately
the temperature of the most heated or chilled objects. The measurement error
is approximately a degree with a larger side. The thermal range is set from
20 to 40 degrees. Exit the script by pressing Ctrl + C.
The script works approximately the same on both the Raspberry Pi Zero
W and the Pi 3 B +. I installed the VNC server on the smartphone. Thus,
picking up raspberries connected to a power bank and a smartphone with
VNC running, you can get a portable thermal imager with the ability to save
thermal images. Perhaps this is not entirely convenient, but quite functional.
After the first start, an incorrect measurement of the maximum temperature
is possible. In this case, you need to exit the script and rerun it.
That is all for today. The experiment with a home-made thermal imager
turned out to be successful. With the helping of this device, it is quite
possible to conduct a thermal imaging inspection of the house on your own,
for example.
Due to the lower temperature contrast than indoors, the pictures were not
very informative. In the photo above, the whole house is on two sides. On the
bottom - photos of different windows.
In the code, I changed only the temperature range. Instead of +20 ... + 40, I
set -10 ... + 5.
6. FINDING A FREE PARKING SPACE
WITH PYTHON
live in a proper city. But, like in many others, the search for a parking
I space always turns into a test. Free spaces quickly occupy, and even if you
have your own, it will be difficult for friends to call you because they will
have nowhere to park.
So I decided to point the camera out the window and use deep learning so
that my computer tells me when the space is available:
It may sound complicated, but writing a working prototype with deep
learning is quick and easy. All the necessary components are already there -
you just need to know where to find them and how to put them together.
So let's have some fun and write an accurate free parking notification
system using Python and deep learning
Decomposing the task
When we have a difficult task that we want to solve with the help of
machine learning, the first step is to break it down into a sequence of simple
tasks. Then we can use various tools to solve each of them. By combining
several simple solutions, we get a system that is capable of something
complex.
Here is how I broke my task:
The video stream from the webcam directed to the window enters the
conveyor input: Through the pipeline, we will transmit each frame of the
video, one at a time.
The first point is to recognize all the possible parking spaces in the frame.
Before we can look for unoccupied places, we need to understand in which
parts of the image there is parking.
Then on each frame you need to find all the cars. This will allow us to
track the movement of each machine from frame to frame.
The third step is to determine which places are occupied by machines and
which are not. To do this, combine the results of the first two steps.
Finally, the program should send an alert when the parking space becomes
free. This will be determined by changes in the location of the machines
between the frames of the video.
Each of the step can be completed in different ways using different
technologies. There is no single right or wrong way to compose this
conveyor: different approaches will have their advantages and
disadvantages. Let's deal with each step in more detail.
We need to scan this image somehow and get a list of places to park:
The solution “in the forehead” would be to simply hardcode the locations
of all parking spaces manually instead of automatically recognizing them. But
in this case, if we move the camera or want to look for parking spaces on
another street, we will have to do the whole procedure again. It sounds so-
so, so let's look for an automatic way to recognize parking spaces.
Alternatively, you can search for parking meters in the image and assume
that there is a parking space next to each of them:
Breakout class
The Breakout class implements most of the knowledge about how
Breakout is controlled. In this tutorial series, we will meet the Breakout
class several times. Here are the lines that various event handlers register.
It should be noted that all key events (for both the left and right “arrows”)
are transmitted to one racket handler method.
Keystroke handling
The Game class calls registered handlers for each key event and passes
the key. Note that this is not a Paddle
class. Into Breakout, the only object that is interested in such events is a
racket. When you press or release a key, its method is handle() called. The
Paddle object does not need to know if this was a key press or release event,
because it controls the current state using a pair of Boolean variables:
moving_left and moving_right . If moving_left True, it means that the "left"
key was pressed, and the next event will be the release of the key, which will
reset the variable. The same applies to the right key. The logic is simple and
consists of switching the state of these variables in response to any event.
Game process
Gameplay (gameplay) is where the Breakout rules come into play. The
gameplay consists in moving various objects in response to events and in
changing the state of the game based on their interactions.
Moving racket
You saw earlier that the Paddle class responds to arrow keys by updating
its
fields moving_left and moving_right . The movement itself occurs in a
method update(). Certain calculations are performed here if the racket is
close to the left or right edge of the screen. We do not want the racket to go
beyond the boundaries of the screen (taking into account a given offset).
Therefore, if the movement moves the object beyond the borders, then the
code adjusts the movement so that it stops right at the border. Since the racket
only moves horizontally to the vertical component of the movement is always
zero.
Moving ball
The ball simply uses the functionality of the base class GameObject,
which moves objects based on their speed (its horizontal and vertical
components). As we will soon see, the speed of a ball is determined by many
factors in the Breakout class. Since the movement consists simply of adding
speed to the current position, the direction in which the ball moves is
completely determined by the speed along the horizontal and vertical axes.
Summarize
In this part, we looked at handling events such as keystrokes, mouse
movements, and mouse clicks. We also examined some elements of Breakout
gameplay: moving the racket, moving the ball, and controlling the speed of
the ball.
In the fourth part, we will consider the important topic of collision
recognition and see what happens when the ball hits different game objects: a
racket, bricks, walls, ceiling, and floor. Then we will pay attention to the
game menu. We will create our buttons, which we use as a menu, and will be
able to show and hide if necessary.
Menu Design
The function create_menu () create a menu with two buttons with the text
'PLAY' and 'QUIT.' It has two built- in function, on _ play () and on _ quit
() which it pass to the correspond button. Each button is add to the list
objects (for rendering), as well as in the field menu _ buttons .
Game over!
The game_overclass of the Game class is set to False in the method init
()of the Game class. The main loop continues until the variable
game_overchanges to True :
Now we can play sound effects when something interesting happens. For
example, when a ball hits a brick:
The sound effect is played asynchronously: that is, the game does not stop
while it is playing. Several sound effects can be played at the same time.
Record your sound effects and messages
Recording your sound effects can be a simple and fun experience. Unlike
creating visual resources, it does not require much talent. Anyone can say
"Boom!" or “Jump,” or shout, “They killed you. Get lucky another time! ”
Playing background music
Background music should play continuously. Theoretically, a very long
sound effect can be created, but the looped background music is most often
used. Music files can be in .wav, .mp3, or .midi format. Here's how the
music is implemented:
Only one background music can play at a time. However, several sound
effects can be played on top of background music. This is what is called
mixing.
Adding Advanced Features
Let's do something curious. It is interesting to destroy bricks with a ball,
but it quickly bothers. What about the overall special effects system? We will
develop an extensible special effects system associated with some bricks,
which is activated when the ball hits the brick.
This is what the plan will be. Effects have a lifetime. The effect begins
when the brick collapses and ends when the effect expires. What happens if
the ball hits another brick with a special effect? In theory, you can create
compatible effects, but to simplify everything in the original implementation,
the active effect will stop, and a new effect will take its place.,
Special effects system
In the most general case, a special effect can be defined as two purposes.
The first role activates the effect, and the second reset it. We want attach
effects to bricks and give the player a clear understanding of which bricks
special , so they can try to hit them or avoid them at certain points.
Our special effects are determined by the dictionary from the module
breakout.py. Each effect has a name (for example, long_paddle) and a value
that consists of a brick color, as well as two functions. Functions are defined
a lambda functions that take a Game instance, which includes everything that
can change the special effect in Breakout.
When creating bricks, they can be assigned one of the special effects. Here
is the code:
effect field, which usually has the value None, but (with a probability of
30%) may contain one of the special effects defined above. Note that this
code does not know what effects exist. He simply receives the effect and
color of the brick and, if necessary, assigns them.
In this version of Breakout, we only trigger effects when we hit a brick,
but you can come up with other options for triggering events. The previous
effect is discarded (if it existed), and then a new effect is launched. The reset
function and effect start time are stored for future use.
If the new effect is not launched, we still need to reset the current effect
after its lifetime. This happens in the method update(). In each frame, a
function to reset the current effect is assigned to the field reset_effect. If the
time after starting the current effect exceeds the duration of the effect, then the
function is called reset_effect(), and the field reset_effecttakes the value
None (meaning that there are currently no active effects).
Racket increase
The effect of a long racket is to increase the racket by 50%. Its reset
function returns the racket to its normal size. The brick has the color Orange.:
Ball slowdown
Another effect that helps in chasing the ball is slowing the ball, that is,
reducing its speed by one unit. The brick has an Aquamarine color.
More points
If you want great results, then you will like the effect of tripling points,
giving three points for each destroyed brick instead of the standard one point.
The brick is dark green.
Extra lives
Finally, a very useful effect will be the effect of extra lives. He just gives
you another life. It does not need a reset. The brick has a gold color.
Future Features
There are several logical directions for expanding Breakout. If you are
interested in trying on yourself in adding new features and functions, here are
a few ideas.
Python Classes
Object Instances
Definition and work with methods
OOP Inheritance
You start with a classkeyword to indicate that you are creating a class,
then you add the class name (using the CamelCase notation starting with a
capital letter).
Also here we used the Python keyword pass. This is huge often used as a
placeholder where the code will eventually go. This allows us to run this
code without generating an error.
Note: the above code is correct in Python 3. On Python 2.x ("deprecated
Python"), you would use a slightly different class definition:
Not the (object)parts in parentheses indicate the parent class that you are
inheriting from (more on this below). In Python-3, this is no longer necessary
because it is implicit by defaulting.
Instance attribute
All class create objects, and all objects contain characteristics called
attributes (called properties in the first paragraph). Use the init ()method to
initialize (for example, determine) the initial attributes of an object by giving
them a default value (state). This method must have atleast one argument, as
well as a self variable that refers to the object itself (for example, Dog).
In our Dog()class, each dog has a specific name and age, which is
certainly important to know when you start creating different dogs.
Remember: the class is intended only to define a dog, and not to create
instances of individual dogs with specific names and ages: we will come
back to this soon.
Similarly, a self variable is also an instance of a class. Since class
instances have different meanings, we could argue, Dog.name = namenot
self.name = name. But since not all dogs have the same name, we must be
able to assign different values for different instances. Hence the need for a
special self variable that will help track individual instances of each class.
NOTE: you will never have to call a init ()method: it is called
automatically when a new instance of Dog is created.
Class attributes
Although instance attributes are specific to each object, class attributes
are the same for all instances, in this case, all dogs.
Thus, although each dog has a unique name and age, each dog will be a
mammal. Let's create some dogs ...
Create Objects
Instantiating is an unusual term for creating a new unique instance of a
class.
For example: >>>
We started by definea new Dog()class, then created two new dogs, each of
which was assigned to different
objects. So, to create an instance of the class, you use the class name
follow by parentheses. Then, to demonstration that each instance is actually
different, we created two more dogs, assigning each variable, and then
checking to see if these variables are equal.
What do you think is the type of class instance? >>>
Instance Methods
Instance methods are defined inside the class and are used to get the
contents of the instance. They can also be used to perform operation with the
attribute of our objects. Like a init method, the first argument is always self:
Save as dog_instance_methods.py , then run it:
In the last method, speak()we define the behavior. What other types of
behavior can you assign to a dog? Go back to the beginning of the paragraph
to see examples of the behavior of other objects.
Attribute Change
You can changes the value of attributes based on some behavior: >>>
Note. In Python 2.x, there is a difference between the new and old-style
classes. I will not go into details, but you will usually want to specify an
object as the parent class to make sure that you define a new style class if
you are writing Python 2 OOP code.
Dog Park Example
Let's imagine that we are in a dog park. There are several Dog objects
involved in Dog behavior, each with different attributes. In ordinary
conversation, this means that some dogs are running, and some are stretched,
and some are just watching other dogs. Besides, each dog was named its
owner, and since each dog lives and breathes, each is aging.
How else can you distinguish one dog from another? How about a dog
breed: >>>
Each dog breed has slightly different behaviors. To take this into account,
let's create separate classes for each breed. These are child classes of the
parent Dogclass.
Extending parent class functionality
Create new file this called dog_inheritance.py :
Read the comments aloud while you are working with this program to help
you understand what is happening, and then, before you run the program, see
if you can predict the expected result.
You should see:
Conclusion: >>>
Review of exercises (# 2)
Exercise: "Legacy of the dogs"
Create a Petsclass that contains dog instances: this class is completely
separate from the Dogclass. In other words, a Dogclass is not inherited from
the Petsclass. Then assign three instances of the dog to the Petsclass instance
. Start with the following code below. Save the file as pets_class.py . Your
output should look like this:
Start Code:
1.What class?
2.What an example?
3.What is the relationship between class and instance?
4.What Python syntax is used to define a new class?
5.What is the spelling convention for a class name?
6.How do you create or create an instance of a class?
7.How do you access the attributes and behavior of an instance
of a class?
8. What kind of method?
9. What is the purpose self?
10.
What is the purpose of the init method?
11.
Describe how inheritance helps prevent code duplication.
12.
Can child classes override the properties of their
parents?
Solution: “Test of understanding” Show hide
Now you should know what classes are, why you want or should use them,
and how to create parent and child classes to better structure your programs.
Remember that OOP is a programming paradigm, not a Python concept.
Most modern programming languages, such as Java, C #, C ++, follow the
principles of OOP. So the good news is that learning the basics of object-
oriented programming will be useful to you in a variety of circumstances -
whether you work in Python or not.
Now the industry does not stand still and there are more and more web
sites, services and companies that need specialists.
Demand for developers is growing, but competition among them is
growing.
To be the best in your business, you need to be almost an absolutely
universal person who can write a website, create a design for it, and promote
it yourself.
In this regard, even a person who has never sat at this computer begins to
think, but should I try?
But very often it turns out that such enthusiasts burn out at the initial stage,
without having tried themselves in this matter.
Or maybe he would become a brilliant creator of code? Would create
something new? This we will not know.
Every day, the threshold for entering programming is growing. You can
never predict what new language will come out.
Such an abundance breaks all the desire of a newly minted programmer
and he is lost in this ocean of information.
All these javascripts of yours ... pythons ... what fear ..
A great misconception is the obligation to know mathematics. Yes, it is
needed, but only in a narrow field, but it is also useful for understanding
some aspects.
The advice that can be given to people who are just starting their activity
is not to chase everything at once. Allow yourself time to think.
What do I want to do? Create a program for everyone to be useful? Create
additional services to simplify any tasks? Or do you really have to go make
games?
The second advice will be to answer my question how much time am I
ready to devote to this? The third point is to think about how fast you want to
achieve your intended result.
So, there is no “easy way” to start programming, it all depends on you -
your interest, what you want to do and your tasks.
In any case, you need to try and do everything possible in your power.
Good luck in your endeavors!
JASON TEST
INTRODUCTION
ata Science has been very popular over the last couple of years. The main focus of this sector is
D to incorporate significant data into business and marketing strategies that will help a business
expand. And get to a logical solution, the data can be stored and explored. Originally only the
leading IT corporations were engaged throughout this field, but today information technology is being
used by companies operating in different sectors and fields such as e-commerce, medical care, financial
services, and others. Software processing programs such as Hadoop, R code, SAS, SQL, and plenty
more are available. Python is, however, the most famous and easiest to use data and analytics tools. It is
recognized as the coding world's Swiss Army Knife since it promotes structured coding, object-oriented
programming, the operational programming language, and many others. Python is the most widely used
programming language in the world and is also recognized as the most high - level language for data
science tools and techniques, according to the 2018 Stack Overflow study.
In the Hacker rank 2018 developer poll, which is seen in their love-hate ranking, Python has won the
developer's hearts. Experts in data science expect to see an increase in the Python ecosystem, with
growing popularity. And although your journey to study Python programming may just start, it's nice to
know that there are also plentiful (and increasing) career options.
Data analytics Python programming is extensively used and, along with being a flexible and open-
source language, becomes one of the favorite programming languages. Its large libraries are used for
data processing, and even for a beginner data analyst, they are very easy to understand. Besides being
open-source, it also integrates easily with any infrastructure that can be used to fix the most complicated
problems. It is used by most banks for data crunching, organizations for analysis and processing, and
weather prediction firms such as Climate monitor analytics often use it. The annual wage for a
Computer Scientist is $127,918, according to Indeed. So here's the good news, the figure is likely to
increase. IBM's experts forecast a 28 percent increase in data scientists' demands by 2020. For data
science, however, the future is bright, and Python is just one slice of the golden pie. Luckily mastering
Python and other principles of programming are as practical as ever.
DATA SCIENCE AND ITS SIGNIFICANCE
Data Science has come a long way from the past few years, and thus, it becomes an important factor
in understanding the workings of multiple companies. Below are several explanations that prove data
science will still be an integral part of the global market.
1. The companies would be able to understand their client in a more efficient and high manner with
the help of Data Science. Satisfied customers form the foundation of every company, and they play an
important role in their successes or failures. Data Science allows companies to engage with customers
in the advance way and thus proves the product's improved performance and strength.
2. Data Science enables brands to deliver powerful and engaging visuals. That's one of the reasons
it's famous. When products and companies make inclusive use of this data, they can share their
experiences with their audiences and thus create better relations with the item.
3. Perhaps one Data Science's significant characteristics are that its results can be generalized to
almost all kinds of industries, such as travel, health care, and education. The companies can quickly
determine their problems with the help of Data Science, and can also adequately address them
4. Currently, data science is accessible in almost all industries, and nowadays, there is a huge amount
of data existing in the world, and if used adequately, it can lead to victory or failure of any project. If
data is used properly, it will be important in the future to achieve the product 's goals.
5. Big data is always on the rise and growing. Big data allows the enterprise to address complicated
Business, human capital, and capital management problems effectively and quickly using different
resources that are built routinely.
6. Data science is gaining rapid popularity in every other sector and therefore plays an important role
in every product's functioning and performance. Thus, the data scientist's role is also enhanced as they
will conduct an essential function of managing data and providing solutions to particular issues.
7. Computer technology has also affected the supermarket sectors. To understand this, let's take an
example the older people had a fantastic interaction with the local seller. Also, the seller was able to
meet the customers' requirements in a personalized way. But now this attention was lost due to the
emergence and increase of supermarket chains. But the sellers are able to communicate with their
customers with the help of data analytics.
8. Data Science helps companies build that customer connection. Companies and their goods will be
able to have a better and deeper understanding of how clients can utilize their services with the help of
data science.
Data Technology Future: Like other areas are continually evolving, the importance of data technology
is increasingly growing as well. Data science impacted different fields. Its influence can be seen in
many industries, such as retail, healthcare, and education. New treatments and technologies are being
continually identified in the healthcare sector, and there is a need for quality patient care. The healthcare
industry can find a solution with the help of data science techniques that helps the patients to take care
with. Education is another field where one can clearly see the advantage of data science. Now the new
innovations like phones and tablets have become an essential characteristic of the educational system.
Also, with the help of data science, the students are creating greater chances, which leads to improving
their knowledge.
Data Science Life Cycle:
Data Structures
A data structure may be selected in computer programming or designed to store data for the purpose
of working with different algorithms on it. Every other data structure includes the data values, data
relationships, and functions between the data that can be applied to the data and information.
Features of data structures
Sometimes, data structures are categorized according to their characteristics. Possible functions are:
Linear or non-linear: This feature defines how the data objects are organized in a
sequential series, like a list or in an unordered sequence, like a table.
Homogeneous or non-homogeneous: This function defines how all data objects in a
collection are of the same type or of different kinds.
Static or dynamic: This technique determines to show to assemble the data structures.
Static data structures at compilation time have fixed sizes, structures, and destinations in
the memory. Dynamic data types have dimensions, mechanisms, and destinations of
memory that may shrink or expand depending on the application.
ou can get all the knowledge about the Python programming language in 5 simple steps.
Y Step 1: Practice Basics in Python
It all starts somewhere. This first step is where the basics of programming Python will be learned.
You are always going to want an introduction to data science. Jupyter Notebook, which comes pre-
portioned with Python libraries to help you understand these two factors, which make it one of the
essential resources which you can start using early on your journey.
Step 2: Try practicing Mini-Python Projects
We strongly believe in learning through shoulders-on. Try programming stuff like internet games,
calculators, or software that gets Google weather in your area. Creating these mini-projects can help
you understand Python. Projects like these are standard for any coding languages, and a fantastic way
to strengthen your working knowledge. You will come up with better and advance API knowledge, and
you will continue site scraping with advanced techniques. This will enable you to learn Python
programming more effectively, and the web scraping method will be useful to you later when collecting
data.
Stage 3: Learn Scientific Libraries on Python
Python can do anything with data. Pandas, Matplotliband, and NumPyare are known to be the three
best used and most important Python Libraries for data science. NumPy and Pandas are useful for data
creation and development. Matplotlib is a library for analyzing the data, creating flow charts and
diagrams as you would like to see in Excel or Google Sheets.
Stage 4: Create a portfolio
A portfolio is an absolute need for professional data scientists. These projects must include numerous
data sets and leave important perspectives to readers that you have gleaned. Your portfolio does not
have a specific theme; finding datasets that inspire you, and then finding a way to place them together.
Showing projects like these provide some collaboration to fellow data scientists, and demonstrates future
employers that you have really taken the chance to understand Python and other essential coding skills.
Some of the good things about data science are that, while showcasing the skills you've learned, your
portfolio serves as a resume, such as Python programming.
Step 5: Apply Advanced Data Science Techniques
Eventually, the target is to strengthen your programming skills. Your data science path will be full of
continuous learning, but you can accomplish specialized tutorials to make sure you have specialized in
the basic programming of Python. You need to get confident with clustering models of regression,
grouping, and k-means. You can also leap into machine learning-using sci-kit lessons to bootstrap models
and create neural network models. At this point, developer programming could include creating models
using live data sources. This type of machine learning technique adjusts s its assumptions over time.
Standard Library
The Python Standard library consists of Python's precise syntax, token, and semantic. It comes
packaged with deployment core Python. When we started with an introduction, we referenced this. It is
written in C and covers features such as I / O and other core components. Together all of the versatility
renders makes Python the language it is. At the root of the basic library, there are more than 200 key
modules. Python ships that library. But aside from this library, you can also obtain a massive collection
of several thousand Python Package Index (PyPI) components.
1. Matplotlib
‘Matplotlib’ helps to analyze data, and is a library of numerical plots. For Data Science, we
discussed in Python.
2. Pandas
‘Pandas’ is a must for data-science as we have said before. It provides easy, descriptive, and
versatile data structures to deal with organized (tabulated, multilayered, presumably heterogeneous)
and series data with ease (and fluidly).
3. Requests
‘Requests’ is a Python library that allows users to upload HTTP/1.1 requests, add headers, form
data, multipart files, and simple Python dictionary parameters. In the same way, it also helps you to
access the response data.
4. NumPy
It has basic arithmetic features and a rudimentary collection of scientific computing.
5. SQLAlchemy
It has sophisticated mathematical features, and SQLAlchemy is a basic mathematical programming
library with well-known trends at a corporate level. It was created to make database availability
efficient and high-performance.
6. BeautifulSoup
This may be a bit on the slow side. BeautifulSoup seems to have a superb library for beginner
XML- and HTML- parsing.
7. Pyglet
Pyglet is an outstanding choice when designing games with an object-oriented programming
language interface. It also sees use in the development of other visually rich programs for Mac OS
X, Windows, and Linux in particular. In the 90s, they turned to play Minecraft on their PCs
whenever people were bored. Pyglet is the mechanism powering Minecraft.
8. SciPy
Next available is SciPy, one of the libraries we spoke about so often. It does have a range of
numerical routines that are user-friendly and effective. Those provide optimization routines and
numerical integration procedures.
9. Scrapy
If your objective is quick, scraping at the high-level monitor and crawling the network, go for
Scrapy. It can be used for data gathering activities for monitoring and test automation.
10. PyGame
PyGame offers an incredibly basic interface to the system-independent graphics, audio, and input
libraries of the Popular Direct Media Library (SDL).
11. Python Twisted
Twisted is an event-driven networking library used in Python and authorized under the MIT open-
source license.
12. Pillow
Pillow is a PIL (Python Imaging Library) friendly fork but is more user efficient. Pillow is your best
friend when you're working with pictures.
13. pywin32
As the name suggests, this gives useful methods and classes for interacting with Windows.
14. wxPython
For Python, it's a wrapper around wxWidgets.
15. iPython
iPython Python Library provides a parallel distributed computing architecture. You will use it to
create, run, test, and track parallel and distributed programming.
16. Nose
The nose provides alternate test exploration and test automation running processes. This intends to
mimic the behavior of the py.test as much as possible.
17. Flask
Flask is a web framework, with a small core and several extensions.
18. SymPy
It is a library of open-source symbolic mathematics. SymPy is a full-fledged Computer Algebra
System (CAS) with a very simple and easily understood code that is highly expandable. It is
implemented in python, and therefore, external libraries are not required.
19. Fabric
As well as being a library, Fabric is a command-line tool to simplify the use of SSH for installation
programs or network management activities. You can run local or remote command line,
upload/download files, and even request input user going, or abort activity with it.
20. PyGTK
PyGTK allows you to create programs easily using a Python GUI (Graphical User Interface).
Bitwise operators
Bitwise operators operate as if they became binary-digit strings on operands. Bit by bit they work,
and therefore the name. For example, in binary two is10, and in binary seven is 111.
Assignment operators
Python language’s assignment operators are used to assign values to the variables. a = 5 is a simple
task operator assigning ‘a’ value of 5 to the right of the variable ‘a’ to the left. In Python, there are
various compound operators such as a + = 5, which adds to the variable as well as assigns the same
later. This equals a= a + 5.
Special operators
Python language gives other different types of operators, such as the operator of the identity or the
operator of membership. Examples of these are mentioned below.
Identity operators
‘Is’ and ‘is not’ are Python Identity Operators. They are used to test if there are two values or
variables in the same memory section. Two equal variables do not mean they are equivalent.
Membership operator
The operators that are used to check whether or not there exists a value/variable in the sequence
such as string, list, tuples, sets, and dictionary. These operators return either True or False if a variable is
found in the list, it returns True, or else it returns False
Expressions
An expression is a mix of values, variables, operators, and function calls. There must be an evaluation
of the expressions. When you ask Python to print a phrase, the interpreter will evaluate the expression
and show the output.
Arithmetic conversions
Whenever an arithmetic operator interpretation below uses the phrase "the numeric arguments are
converted to a common type," this means the execution of the operator for the built-in modes operates
as follows
If one argument is a complex quantity, then the other is converted to a complex number; If another
argument is a floating-point number, the other argument is transformed to a floating-point; Or else both
will be integers with no need for conversion.
Atoms
Atoms are the most important expressional components. The smallest atoms are literals or abstract
identities. Forms contained in parentheses, brackets, or braces are also syntactically known as atoms.
Atoms syntax is:
atom ::= identifier | enclosure| literal
enclosure ::= list_display| parenth_form| dict_display | set_display
Identifiers (Names)
A name is an identifier that occurs as an atom. See section Lexical Description Identifiers and
Keywords and group Naming and binding for naming and binding documents. Whenever the name is
connected to an entity, it yields the entity by evaluating the atom. When a name is not connected, an
attempt to assess it elevates the exception for NameError.
Literals
Python provides logical string and bytes and numerical literals of different types:
literal::= string literal | bytes literal
| integer | float number | image number
Assessment of a literal yield with the predicted set an object of that type (bytes, integer, floating-point
number, string, complex number). In the scenario of floating-point and imaginary (complex) literals, the
value can be approximated.
Parenthesized forms
A parenthesized type is an available set of parentheses for the expression:
parenth_form ::= "(" [starred_expression] ")"
A list of parenthesized expressions yields whatever the list of expressions produces: if the list includes
at least one comma, it produces a tuple. If not, it yields the sole expression that forms up the list of
expressions. A null pair of parentheses generates an incomplete object of tuples. As all tuples are
immutable, the same rules would apply as for literals (i.e., two empty tuple occurrences does or doesn't
yield the same entity).
Displays for lists, sets, and dictionaries
For the construction of a list, Python uses a series or dictionary with a particular syntax called
"displays," each in complementary strands:
The contents of the container are listed explicitly, or They are calculated using a series of instructions
for looping and filtering, named a 'comprehension.' Common features of syntax for comprehensions are:
comprehension ::= assignment_expressioncomp_for
comp_for ::= ["async"] "for" target_list "in" or_test [comp_iter]
comp_iter ::= comp_for | comp_if
comp_if ::= "if" expression_nocond [comp_iter]
A comprehension contains one single sentence ready for at least one expression for clause, and zero
or more for or if clauses. Throughout this situation, the components of the container are those that will
be generated by assuming each of the for or if clauses as a block, nesting from left to right, and
determining the phase for creating an entity each time the inner core block is approached.
List displays
A list view is a probably empty sequence of square brackets including expressions:
list_display ::= "[" [starred_list | comprehension] "]"
A list display generates a new column object, with either a list of expressions or a comprehension
specifying the items. When a comma-separated database of expressions is provided, its elements are
assessed from left to right and positioned in that order in the category entity. When Comprehension is
provided, the list shall be built from the comprehension components.
Set displays
Curly braces denote a set display and can be distinguished from dictionary displays by the lack of
colons dividing data types:
set_display ::= "{" (starred_list | comprehension) "}"
A set show offers a new, mutable set entity, with either a series of expressions or a comprehension
defining the contents. When supplied with a comma-separated list of expressions, its elements are
evaluated from left to right and assigned to the set entity. Whenever a comprehension is provided, the
set is formed from the comprehension-derived elements. Unable to build an empty set with this {};
literal forms a blank dictionary.
Dictionary displays
A dictionary view is a potentially empty sequence of key pairs limited to curly braces:
dict_display ::= "{" [key_datum_list | dict_comprehension] "}"
key_datum_list ::= key_datum ("," key_datum)* [","]
key_datum ::= expression ":" expression | "**" or_expr
dict_comprehension ::= expression ":" expression comp_for
The dictionary view shows a new object in the dictionary. When a comma-separated series of key /
datum pairs is provided, they are analyzed from left to right to identify dictionary entries: each key entity
is often used as a key to hold the respective datum in the dictionary. This implies you can clearly state
the very same key numerous times in the key /datum catalog, but the last one given will become the
final dictionary's value for that key.
Generator expressions
A generator expression is the compressed syntax of a generator in the parenthesis :
generator_expression ::= "(" expression comp_for ")"
An expression generator produces an entity that is a new generator. Its syntax will be the same as
for comprehensions, except for being enclosed in brackets or curly braces rather than parentheses.
Variables being used generator expression are assessed sloppily when the generator object (in the same
style as standard generators) is called by the __next__() method. Conversely, the iterate-able
expression in the leftmost part of the clause is evaluated immediately, such that an error that it produces
is transmitted at the level where the expression of the generator is characterized, rather than at the level
where the first value is recovered.
For instance: (x*y for x in range(10) for y in range(x, x+10)).
Yield expressions
yield_atom ::= "(" yield_expression ")"
yield_expression ::= "yield" [expression_list | "from" expression]
The produced expression is used to define a generator function or async generator function, and can
therefore only be used in the function definition body. Using an expression of yield in the body of a
function tends to cause that function to be a generator, and to use it in the body of an asynchronous def
function induces that co-routine function to become an async generator. For example:
def gen(): # defines a generator function
yield 123
asyncdefagen(): # defines an asynchronous generator function
yield 123
Because of their adverse effects on the carrying scope, yield expressions are not allowed as part of
the impliedly defined scopes used to enforce comprehensions and expressions of generators.
ou utilize programming functions to combine a list of instructions that you're constantly using or
Y that are better self-contained in sub-program complexity and are called upon when required.
Which means a function is a type of code written to accomplish a given purpose. The function
may or may not need various inputs to accomplish that particular task. Whenever the task is executed,
one or even more values can or could not be returned by the function. Basically there exist three types
of functions in Python language:
1. Built-in functions, including help() to ask for help, min() to just get the minimum amount,
print() to print an attribute to the terminal. More of these functions can be found here.
2. User-Defined Functions (UDFs) that are functions created by users to assist and support
them out;
3. Anonymous functions, also labeled lambda functions since they are not defined with the
default keyword.
Defining A Function: User Defined Functions (UDFs)
The following four steps are for defining a function in Python:
1. Keyword def can be used to declare the function and then use the function name to
backtrack.
2. Add function parameters: They must be within the function parentheses. Finish off your
line with a colon.
3. Add statements which should be implemented by the functions.
When the function should output something, end your function with a return statement. Your task
must return an object None without return declaration. Example:
1. def hello():
2. print("Hello World")
3.return
It is obvious as you move forward, the functions will become more complex: you can include for
loops, flow control, and more to make things more fine-grained:
def hello():
name = str(input("Enter your name: "))
if name:
print ("Hello " + str(name))
else:
print("Hello World")
return
hello()
In the feature above, you are asking the user to give a name. When no name is provided, the 'Hello
World' function will be printed. Otherwise, the user will receive a custom "Hello" phrase. Also, consider
you can specify one or more parameters for your UDFs function. When you discuss the segment
Feature Statements, you will hear more about this. Consequently, as a result of your function, you may
or may not return one or more values.
The return Statement
Note that since you're going to print something like that in your hello) (UDF, you don't really have to
return it. There'll be no distinction between the above function and this one:
Example:
1. defhello_noreturn():
2. print("Hello World")
Even so, if you'd like to keep working with the result of your function and try a few other functions
on it, you'll need to use the return statement to simply return a value, like a string, an integer. Check out
the following scenario in which hello() returns a "hello" string while the hello_noreturn() function returns
None:
1. def hello():
2. print("Hello World")
3. return("hello")
4. defhello_noreturn():
5. print("Hello World")
6. # Multiply the output of `hello()` with 2
7. hello() * 2
8. # (Try to) multiply the output of `hello_noreturn()` with 2
9. hello_noreturn() * 2
The secondary part gives you an error because, with a None, you cannot perform any operations.
You will get a TypeError that appears to say that NoneType (the None, which is the outcome of
hello_noreturn()) and int (2) cannot do the multiplication operation. Tip functions leave instantly when a
return statement is found, even though that means they will not return any result:
1. def run():
2. for x in range(10):
3. if x == 2:
4. return
5. print("Run!")
6. run()
Another factor worth noting when dealing with the ‘return expression’ is many values can be
returned using it. You consider making use of tuples for this. Recall that this data structure is very
comparable to a list's: it can contain different values. Even so, tuples are immutable, meaning you can't
alter any amounts stored in it! You build it with the aid of dual parentheses). With the assistance of the
comma and the assignment operator, you can disassemble tuples into different variables.
Read the example below to understand how multiple values can be returned by your function:
1. # Define `plus()`
2. def plus(a,b):
3.sum = a + b
4.return (sum, a)
1. Default arguments
2. Required arguments
3. Keyword arguments
4. Variable number of arguments
Default Arguments
Default arguments would be those who take default data if no value of the argument is delivered
during the call function. With the assignment operator =, as in the following case, you may assign this
default value:
1. #Define `plus()` function
2. def plus(a,b = 2):
3.return a + b
4. # Call `plus()` with only `a` parameter
5. plus(a=1)
6. # Call `plus()` with `a` and `b` parameters
7. plus(a=1, b=3)
Required Arguments
Because the name sort of brings out, the claims a UDF needs are those that will be in there. Such
statements must be transferred during the function call and are absolutely the right order, such as in the
example below:
1. # Define `plus()` with required arguments
2. def plus(a,b):
3. return a + b
Calling the functions without getting any additional errors, you need arguments that map to 'a' as well
as the 'b' parameters. The result will not be unique if you swap round the 'a' and 'b,' but it could be if
you modify plus() to the following:
1. # Define `plus()` with required arguments
2. def plus(a,b):
3.return a/b
Keyword Arguments
You will use keyword arguments in your function call if you'd like to make sure you list all the
parameters in the correct order. You use this to define the statements by the name of the function. Let's
take an example above to make it a little simpler:
1. # Define `plus()` function
2. def plus(a,b):
3.return a + b
4. # Call `plus()` function with parameters
5. plus(2,3)
6. # Call `plus()` function with keyword arguments
7. plus(a=1, b=2)
Notice that you can also alter the sequence of the parameters utilizing keywords arguments and still
get the same outcome when executing the function:
1. # Define `plus()` function
2. def plus(a,b):
3.return a + b
4. # Call `plus()` function with keyword arguments
5. plus(b=2, a=1)
Global vs. Local Variables
Variables identified within a function structure usually have a local scope, and those specified outside
have a global scope. This shows that the local variables are specified within a function block and can
only be retrieved through that function, while global variables can be retrieved from all the functions in
the coding:
1. # Global variable `init`
2. init = 1
3. # Define `plus()` function to accept a variable number of arguments
4. def plus(*args):
5. # Local variable `sum()`
6.total = 0
7.fori in args:
8.total += i
9.return total
10.# Access the global variable
11.print("this is the initialized value " + str(init))
12.# (Try to) access the local variable
13.print("this is the sum " + str(total))
You will find that you can get a NameError that means the name 'total' is not specified as you
attempt to print out the total local variable that was specified within the body of the feature. In
comparison, the init attribute can be written out without any complications.
Anonymous Functions in Python
Anonymous functions are often termed lambda functions in Python since you are using the lambda
keyword rather than naming it with the standard-def keyword.
1. double = lambda x: x*2
2. double(5)
The anonymous or lambda feature in the DataCamp Light chunk above is lambda x: x*2. X is the
argument, and x*2 is the interpretation or instruction that is analyzed and given back. What is unique
about this function, and it has no tag, like the examples you saw in the first section of the lecture for this
function. When you had to write the above function in a UDF, you would get the following result:
def double(x):
return x*2
Let us see another example of a lambda function where two arguments are used:
1. # `sum()` lambda function
2. sum = lambda x, y: x + y;
3. # Call the `sum()` anonymous function
4. sum(4,5)
5. # "Translate" to a UDF
6. def sum(x, y):
7. returnx+y
When you need a function with no name for a short interval of time, you utilize anonymous functions
and this is generated at runtime. Special contexts where this is important are when operating with
filter(), map() and redu():
1. from functools import reduce
2. my_list = [1,2,3,4,5,6,7,8,9,10]
3. # Use lambda function with `filter()`
4. filtered_list = list(filter(lambda x: (x*2 > 10), my_list))
5. # Use lambda function with `map()`
6. mapped_list = list(map(lambda x: x*2, my_list))
7. # Use lambda function with `reduce()`
8. reduced_list = reduce(lambda x, y: x+y, my_list)
9. print(filtered_list)
10. print(mapped_list)
11. print(reduced_list)
As the name states the filter() function it help filters the original list of inputs my_list based on a
criterion > 10.By contrast, with map(), you implement a function to every components in the my_listlist.
You multiply all of the components with two in this scenario. Remember that the function reduce() is a
portion of the functools library. You cumulatively are using this function to the components in the
my_list() list, from left to right, and in this situation decrease the sequence to a single value 55.
Using main() as a Function
If you have got any knowledge with other programming languages like java, you'll notice that
executing functions requires the main feature. As you've known in the above examples, Python doesn't
really require this. However, it can be helpful to logically organize your code along with a main()
function in your python code- - all of the most important components are contained within this main()
function.
You could even simply achieve and call a main() function the same as you did with all of those above
functions:
1. # Define `main()` function
2. def main():
3. hello()
4. print("This is the main function")
5. main()
After all, as it now appears, when you load it as a module, the script of your main ( ) function will
indeed be called. You invoke the main() function whenever name == ' main ' to ensure this does not
happen.
That implies the source above code script becomes:
1.# Define `main()` function
2.def main():
3.hello()
4.print("This is a main function")
5.# Execute `main()` function
6. if __name__ == '__main__':
7. main()
Remember that in addition to the main function, you too have a init function, which validates a class
or object instance. Plainly defined, it operates as a constructor or initializer, and is termed automatically
when you start a new class instance. With such a function, the freshly formed object is assigned to the
self-parameter that you've seen in this guide earlier.
Consider the following example:
class Dog:
"""
Requires:
legs – legs for a dog to walk.
color – Fur color.
"""
def __init__(self, legs, color):
self.legs = legs
self.color = color
def bark(self):
bark = "bark" * 2
return bark
if __name__ == "__main__":
dog = Dog(4, "brown")
bark = dog.bark()
print(bark)
LISTS AND LOOPS
Lists
list is often a data structure in Python, which is an ordered list of elements that is mutable or
A modifiable. An item is named for each element or value inside a list. Just like strings are defined
like characters between quotations, lists are specified by square brackets ‘[ ]’ having values.
Lists are nice to have because you have other similar principles to deal with. They help you to hold
data that are relevant intact, compress the code, and run the same numerous-value methods and
processes at once.
It could be helpful to get all the several lists you have on your computer when beginning to think
about Python lists as well as other data structures that are types of collections: Your assemblage of files,
song playlists, browser bookmarks, emails, video collections that you can access through a streaming
platform and much more.
We must function with this data table, taken from data collection of the Mobile App Store
(RamanathanPerumal):
Name price currency rating_count rating
Instagram 0.0 USD 2161558 4.5
Clash of Clans 0.0 USD 2130805 4.5
Temple Run 0.0 USD 1724546 4.5
Pandora – Music & 0.0 USD 1126879 4.0
Radio
Facebook 0.0 USD 2974676 3.5
Every value is a data point in the table. The first row (just after titles of the columns) for example has
5 data points:
Facebook
0.0
USD
2974676
3.5
Dataset consists of a collection of data points. We can consider the above table as a list of data
points. Therefore we consider the entire list a dataset. We can see there are five rows and five columns
to our data set.
Utilizing our insight of the Python types, we could perhaps consider we can store each data point in
their own variable — for example, here's how we can store the data points of the first row:
Above, we stored:
Separating each with a comma while typing out a sequence of data points: 'Facebook,'
0.0, 'USD,' 2974676, 3.5
Closing the list with square brackets: ['Facebook', 0.0, 'USD', 2974676, 3.5]
After the list is created, we assign it to a variable named row_1and the list is stored in
the computer’s memory.
For creating data points list, we only need to:
For smaller lists, we can simply count the data points on our displays to figure the length, but perhaps
the len() command will claim to be very useful anytime you function with lists containing many
components, or just need to compose data code where you really don't know the length in advance.
Every other component (data point) in a list is linked to a particular number, termed the index number.
The indexing also begins at 0, which means that the first element should have the index number 0, the
2nd element the index number 1, etc.
To locate a list element index rapidly, determine its location number in the list and then subtract it by
1. The string 'USD,' for instance, is the third item in the list (stance number 3), well its index number
must be two because 3 – 1 = 2.
The index numbers enable us to locate a single item from a list. Going backward through the list row
1 from the example above, by executing code row 1[0], we can obtain the first node (the string
'Facebook') of index number 0.
The Model list_name[index number] follows the syntax for locating specific list components. For
example, the title of our list above is row_1 and the index number of a first element is 0, we get
row_1[0] continuing to follow the list_name[index number] model, in which the index number 0 is in
square brackets just after the name of the variable row_1.
Retrieval of list elements makes processes easier to execute. For example, Facebook and Instagram
ratings can be selected, and the aggregate or distinction between the two can be found:
Try Using list indexing to retrieve and then average the number of ratings with the first 3 rows:
ratings_1 = row_1[3]
ratings_2 = row_2[3]
ratings_3 = row_3[3]
total = ratings_1 + ratings_2 + ratings_3
average = total / 3
print(average)
2422346.3333333335
Using Negative Indexing with Lists
There are two indexing systems for lists in Python:
1. Positive indexing: The index number of the first element is 0; the index number of the
second element is 1 and furthermore.
2. Negative indexing: The index number of the last element is -1; the index number of the
second element is -2 and furthermore.
In exercise, we mostly use positive indexing to obtain elements of the list. Negative indexing is helpful
whenever we want to pick the last element in such a list, mostly if the list is lengthy, and by calculating,
we cannot figure out the length.
Note that when we use an index number just outside of the scope of the two indexing schemes, we
are going to have an IndexError.
How about using negative indexing to remove from each of the top 3 rows the user rating (the very
last value) and afterwards average it.
row_1 [-1]=rating_1
row_2[-1]=rating_2
row_3[-1]=rating_3
rating_1 + rating_2 + rating_3=total_rating
total_rating / 3= average_rating
print(average)
2422346.33333
Slice Python Lists
Rather than selecting the list elements separately, we can pick two consecutive elements using a
syntax shortcut:
While selecting the first n elements from a list called a list (n stands for a number), we can use the
list syntax shortcut [0: n]. In the above example, we had to choose from the list row 3 the first three
elements, so we will use row 3[0:3].
When the first three items were chosen, we sliced a portion of the set. For this function, the
collection method for a section of a list is known as list slicing.
List slice can be done in many ways:
When we want to choose the 1st or last ‘x’ elements (x represents a number), we may use even less
complex shortcuts for syntax:
a_list[:x] when we need to choose the first x elements.
a_list[-x:] when we need to choose the last x elements.
See how we retrieve from the first row the first four elements (with Facebook data):
first_4_fb = row_1[:4]
print(first_4_fb)
['Facebook', 0.0, 'USD', 2974676]
From the same row, the last three elements are:
last_3_fb = row_1[-3:]
print(last_3_fb)
['USD', 2974676, 3.5]
In the fifth row (data in the row for Pandora) with elements third and fourth are:
pandora_3_4 = row_5[2:4]
print(pandora_3_4)
['USD', 1126879]
Python List of Lists
Lists were previously introduced as a viable approach to using one variable per data point. Rather
than having a different variable for any of the five 'Facebook' data points, 0.0, 'USD,' 2974676, 3.5, we
can connect the data points into a list together and then save the list in a variable.
We have worked with a data set of five rows since then and have stored each row as a collection in
each different variable (row 1, row 2, row 3, row 4, and row 5 variables). Even so, if we had a data set
of 5,000 rows, we would probably have ended up with 5,000 variables that will create our code messy
and nearly difficult to work with.
To fix this issue, we may store our five variables in a unified list:
As we're seeing, the data set is a list of five additional columns (row 1, row 2, row 3, row 4, and row
5). A list containing other lists is termed a set of lists.
The data set variable is already a list, which indicates that we can use the syntax we have learned to
retrieve individual list elements and execute list slicing. Under, we have:
Often, we will need to obtain individual elements from a list that is a portion of a list of lists — for
example; we might need to obtain the rating of 3.5 from the data row ['FACEBOOK', 0.0, 'USD',
2974676, 3.5], which is a portion of the list of data sets. We retrieve 3.5 from data set below utilizing
what we have learnt:
Using data set[0], we locate row_1, and allocate the output to a variable named
fb_row.
fb_row ['Facebook', 0.0, 'USD', 2974676, 3.5] outputs, which we printed.
Using fb_row[-1], we locate the final element from fb_row (because fb row is a list),
and appoint the output to a variable called fb_rating.
Print fb_rating, outputting 3.5
Earlier in this example, we obtained 3.5 in two steps: data_set[0] was first retrieved, and fb_row[-1]
was then retrieved. There is also an easy way to get the same 3.5 output by attaching the two indices
([0] and [-1]); the code data_set[0][-1] gets 3.5.:
Earlier in this example, we have seen two ways to get the 3.5 value back. Both methods lead to the
same performance (3.5), but the second approach requires fewer coding, as the steps we see from the
example are elegantly integrated. As you can select an alternative, people generally prefer the latter.
Let's turn our five independent lists in to the list of lists:
app_data_set = [row_1, row_2, row_3, row_4, row_5]
then use:
print(app_data_set)
[
[ ]
List Processes by Repetitive method
Earlier, we had an interest in measuring an app's average ranking in this project. It was a feasible
task while we were attempting to work only for three rows, but the tougher it becomes, the further rows
we add. Utilizing our tactic from the beginning, we will:
As you have seen that it becomes complicated with five ratings. Unless we were dealing with data
that includes thousands of rows, an unimaginable amount of code would be needed! We ought to find a
quick way to get lots of ratings back.
Taking a look at the code example earlier in this thread, we see that a procedure continues to
reiterate: within app_data_set, we select the last list element for every list. What if we can just directly
ask Python we would like to repeat this process in app_data_set for every list?
Luckily we can use it — Python gives us a simple route to repeat a plan that helps us tremendously
when we have to reiterate a process tens of thousands or even millions of times.
Let’s assume we have a list [3, 5, 1, 2] allocated to a variable rating, and we need to replicate the
following procedure: display the element for each element in the ratings. And this is how we can turn it
into syntax with Python:
The procedure that we decided to replicate in our first example above was "generate the last item for
each list in the app_data_set." Here's how we can transform that operation into syntax with Python:
Let's attempt and then get a good idea of what's going on above. Python differentiates each list item
from app_data_set, each at a time, and assign it to each_list (which essentially becomes a vector that
holds a list — we'll address this further):
In the last figure earlier in this thread, the code is a much simpler and much more conceptual edition
of the code below:
Utilizing the above technique requires that we consider writing a line of code for each row in the data
set. But by using the app_data_set methodology for each list involves that we write only two lines of
code irrespective of the number of rows in the data set — the data set may have five rows or a hundred
thousand.
Our transitional goal is to use this special method to calculate the average rating of our five rows
above, in which our ultimate goal is to calculate the average rating of 7,197 rows for our data set. We
're going to get exactly that within the next few displays of this task, but we're going to concentrate for
now on practicing this method to get a strong grasp of it.
We ought to indent the space characters four times to the right before we want to write the code:
Theoretically, we would only have to indent the code to the right with at least one space character,
but in the Python language, the declaration is to use four space characters. This assists with readability
— reading your code will be fairly easy for other individuals who watch this convention, and you will
find it easier to follow theirs.
Now use this technique to print each app's name and rating:
foreach_list in app_data_set:
name = each_list[0]
rating = each_list[-1]
print(name, rating)
Facebook 3.5
Instagram 4.5
Clash of Clans 4.5
Temple Run 4.5
Pandora - Music & Radio 4.0
Loops
A loop is frequently used to iterate over a series of statements. We have two kinds of loops, ‘for
loop’ and ‘while loop’ in Python. We will study ‘for loop’ and ‘while loop’ in the following scenario.
For Loop
Python's for loop is used to iterate over a sequence (list, tuple, string) or just about any iterate-able
object. It is called traversal to iterate over a sequence.
Syntax of For loop in Python
for<variable> in <sequence>:
# body_of_loop that has set of statements
# which requires repeated execution
In this case < variable > is often a variable used to iterate over a < sequence >. Around each
iteration the next value is taken from < sequence > to approach the end of the sequence.
Python – For loop example
The example below illustrates the use of a loop to iterate over a list array. We calculate the square of
each number present in the list and show the same with the body of for loop.
#Printing squares of all numbers program
# List of integer numbers
numbers = [1, 2, 4, 6, 11, 20]
#variable to store each number’s square temporary
sq = 0
#iterating over the given list
forval in numbers:
# calculating square of each number
sq = val * val
# displaying the squares
print(sq)
Output:
1
4
16
36
121
400
For loop with else block
Excluding Java, we can have the loop linked with an optional 'else' block in Python. The 'else' block
only runs after all the iterations are finished by the loop. Let's see one example:
For val in range(5):
print(val)
else:
print("The loop has completed execution")
Output:
0
1
2
3
4
The loop has completed execution
Note: else block is executed when the loop is completed.
Nested For loop in Python
If there is a loop within another for loop, then it will be termed a nested for loop. Let's take a nested
for loop example.
for num1 in range(3):
for num2 in range(10, 14):
print(num1, ",", num2)
Output:
0 , 10
0 , 11
0 , 12
0 , 13
1 , 10
1 , 11
1 , 12
1 , 13
2 , 10
2 , 11
2 , 12
2 , 13
While Loop
While loop is also used to continuously iterate over a block of code until a specified statement returns
false, we have seen in many for loop in Python in the last guide, which is used for a similar intent. The
biggest distinction is that we use for looping when we are not sure how many times the loop needs
execution, yet on the other side when we realize exactly how many times we have to execute the loop,
we need for a loop.
Syntax of while loop
while conditioning:
#body_of_while
The body of the while is a series of statements from Python which require repetitive implementation.
These claims are consistently executed until the specified condition returns false.
while loop flow
1. Firstly given condition is inspected, the loop is canceled if the condition returns false, and also the
control moves towards the next statement in the compiler after the loop.
2. When the condition returns true, the set of statements within the loop will be performed, and the
power will then switch to the loop start for the next execution.
Those two measures continuously occur as long as the condition defined in the loop stands true.
While loop example
This is an example of a while loop. We have a variable number in this case, and we show the value
of the number in a loop, the loop will have an incremental operation where we increase the number
value. It is a very crucial component, while the loop should have an operation of increase or decrease.
Otherwise, the loop will operate indefinitely.
num = 1
#loop will repeat itself as long as it can
#num< 10 remains true
whilenum< 10:`
print(num)
#incrementing the value of num
num = num + 3
Output:
1
4
7
Infinite while loop
Example 1:
This will endlessly print the word 'hello' since this situation will always be true.
while True:
print("hello")
Example 2:
num = 1
whilenum<5:
print(num)
This will endlessly print '1' since we do not update the number value inside the loop, so the number
value would always remain one, and the condition number<5 would always give back true.
Nested while loop in Python
While inside another while loop a while loop is present, then it will be considered nested while loop.
To understand this concept, let us take an example.
i=1
j=5
while i< 4:
while j < 8:
print(i, ",", j)
j=j+1
i=i+1
Output:
1,5
2,6
3,7
Python – while loop with else block
We may add an 'else' block to a while loop. The section 'else' is possible. It executes only when the
processing of the loop has ended.
num = 10
whilenum> 6:
print(num)
num = num-1
else:
print("loop is finished")
Output:
10
9
8
7
Loop is finished
ADDING MULTIPLE VALUED DATA IN
PYTHON
ften the creator wants users to input multiple values or inputs in a line. In Python, users could use
O two techniques to take multiple values or inputs in one line.
We cannot erase characters from a string, or remove them. But it's easy to erase the string
completely by using del keyword.
Python String Operations
There are many methods that can be used with string making it one of the most commonly used
Python data types. See Python Data Types for more information on the types of data used in Python
coding
Concatenation of Two or More Strings
The combination of two or even more strings into one is termed concatenation. In Python, the +
operator does that. They are likewise concatenated by actually typing two string literals together. For a
specified number of times, the * operator could be used to reiterate the string.
Iterating Through a string
With a for loop, we can iterate through a string. This is an example of counting the number of 'l's in a
string function.
If we execute the code above, we have the following results:
‘3 letters found.’
String Membership Test
We can check whether or not there is a substring within a string by using keyword in.
>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False
Built-in functions to Work with Python
Different built-in functions which can also be work with strings in series. A few other commonly
used types are len() and enumerate(). The function enumerate() returns an enumerate object. It
includes the index and value as combinations of all elements in the string. This may be of use to
iteration. Comparably, len() returns the string length (characters number).
Formats for Python String
Sequence for escaping
We can't use single quotes or double quotes if we want to print a text like He said, "What's there?"
This would result in a SyntaxError because there are single and double quotations in the text alone.
>>>print("He said, "What's there?"")
...
SyntaxError: invalid syntax
>>>print('He said, "What's there?"')
...
SyntaxError: invalid syntax
Triple quotes are one way to get round the problem. We might use escape sequences as a solution. A
series of escape starts with a backslash, which is represented differently. If we are using a single quote
to describe a string, it is important to escape all single quotes within the string. The case with double
quotes is closely related. This is how the above text can be represented.
Once we execute the code above, we have the following results:
He said, "What's there?"
He said, "What's there?"
He said, "What's there?"
Raw String to ignore escape sequence
Quite often inside a string, we might want to reject the escape sequences. To use it, we can set r or
R before the string. Which means it's a raw string, and it will neglect any escape sequence inside.
>>>print("This is \x61 \ngood example")
This is a
good example
>>> print(r"This is \x61 \ngood example")
This is \x61 \ngood example
The format() Method for Formatting Strings
The format() sources available and make with the string object is very flexible and potent in string
formatting. Style strings contain curly braces{} as placeholders or fields of substitution, which are
substituted.
To specify the sequence, we may use positional arguments or keyword arguments.
The format() technique can have requirements in optional format. Using colon, they are divided from
the name of the field. For example, a string in the given space may be left-justified <, right-justified >, or
based ^.
Even we can format integers as binary, hexadecimal, etc. and floats can be rounded or shown in the
style of the exponent. You can use tons of compiling there. For all string formatting available using the
format() method, see below example:
Old style formatting
We even can code strings such as the old sprint() style in the programming language used in C. To
accomplish this; we use the ‘%’ operator.
Shepherd Martha
is 34 years old.
MODULE DATA
In this, we defined an add() function within an example titled “ module.” The function requires two
numbers and returns a total of them.
How to import modules in Python?
Within a module, we can import the definitions to some other module or even to the interactive
Python interpreter. To do something like this, we use the keyword import. To load our recently specified
example module, please enter in the Python prompt.
>>> import example
This should not import the identities of the functions directly in the existing symbol table, as defined in
the example. It just imports an example of the module name there.
Using the name of the module, we can use the dot(.) operator to access the function. For instance:
>>>example.add(4,5.5)
9.5
Python comes with lots of regular modules. Check out the complete list of regular Python modules
and their usage scenarios. These directories are within the destination where you've installed Python in
the Lib directory. Normal modules could be imported just the same as our user-defined modules are
imported.
There are different ways of importing the modules. You'll find them below:
Python import statement
Using the import statement, we can extract a module by using the dot operator, as explained in the
previous section and access the definitions within it. Here is another example.
We called the module Math as m. In certain instances, this will save us time to type. Remember that
in our scope, the name math is not identified. Therefore math.pi is incorrect, and m.pi is correctly
implemented.
Python from...import statement
We can import individual names from such a module without having to import the entire module. Here
is another example.
In this, only the pi parameter was imported from the math module. We don't utilize the dot operator in
certain cases. We can likewise import different modules:
>>>from math import pi, e
>>>pi
3.141592653589793
>>>e
2.718281828459045
Import all names
With the following form, we can import all terms (definitions) from a module:
# import all names from standard module math
from math import *
print("The value of pi is," pi)
Above, we have added all of the math module descriptions. This covers all names that are available
in our scope except those that start with an underscore. It is not a good programming technique to
import something with the asterisk (*) key. This will lead to a replication of an attribute's meaning. This
also restricts our code's readability.
Python Module Search Path
Python looks at many locations when importing a module. Interpreter searches for a built-in module
instead. So if not included in the built-in module, Python searches at a collection of directories specified
in sys.path. The exploration is in this sequence:
PYTHONPATH (list of directories environment variable)
The installation-dependent default directory
We can insert that list and customize it to insert our own location.
Reloading a module
During a session, the Python interpreter needs to import one module only once. This makes matters
more productive. Here is an example showing how that operates.
Assume we get the code below in a module called my_module:
Now we'll see a list of the names sorted (alongside add). Many other names that start with an
underscore are module-associated (not user-defined) default Python attributes. For instance, the
attribute name contains module __name__.
>>> import example
>>>example.__name__
'example'
You can find out all names identified in our existing namespace by using dir() function with no
arguments.
To minimize the size of a compiled file, you can use the -O or -OO switches in the
Python order. The -O switch erases statements of assert, the -OO switch removes
statements of assert as well as strings of doc. Although some codes may support
getting these options available, this method should only be used if you are aware of
what you are doing. "Optimized" modules usually have such an opt-tag and are tinier.
Future releases may modify the optimal control implications.
A project run no faster once it is read from a.pyc file than how it was read from a.py
file; just one thing about.pyc files that are faster in the speed with which they will be
loaded.
A compile all modules can generate .pyc files in a directory for all of the other
modules.
More details on this process are given in PEP 3147, along with a flow chart of the
decision making.
Standard Modules
Python has a standard modules library, mentioned in a separate section, the Python Library allusion
(hereafter "Library Reference"). A few modules are incorporated into the interpreter; that provide
direct exposure to processes that are not component of the language's base but are nonetheless built-in,
whether for effectiveness or to supply access to primitive operating systems such as source code calls.
The collection of these modules is an alternative to customize and also relies on the framework
underlying it. The winreg module, for instance, is only available on Microsoft windows. One particular
module is worthy of certain interest: sys, which is integrated into every Python interpreter. The sys.ps1
and sys.ps2 variables classify strings which are used as primary and secondary instructions:
>>>
>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C>print('Yuck!')
Yuck!
C>
Only when the interpreter is in interactive mode are those two variables defined. The sys.path
variable is a collection of strings that defines the search path for modules used by the interpreter. When
PYTHONPATH is not a part of the set, then it will be defined to a predefined path taken from either
the PYTHONPATH environment variable or through a built-in default. You can change it with regular
list procedures:
>>>
>>> import sys
>>>sys.path.append('/python/ufs/guido/lib/')
Packages
Packages are indeed a way to construct the namespace of the Python module by using "pointed
names of the module." For instance, in a package called A., the module title A.B specifies a submodule
named B. Even as the use of modules prevents the writers of various modules from stopping to know
about the global variable names of one another, any use of dotted module names prevents the
developers of multi-module bundles like NumPy or Pillow from needing to worry more about module
names of one another. Consider making a series of lists of modules (a "package") to handle sound files
and sound data in an even manner.
There are several various programs of sound files usually familiar with their extension, for example:
‘wav,.aiff,.au,’ though you'll need to build and maintain a massive collection of modules to convert
between some of the multiple formats of files. There are several other different operations that you may
like to run on sound data (such as blending, adding echo, implementing an equalizer function, producing
an optical stereo effect), and you'll just be writing an infinite series of modules to execute those
interventions. Here is another feasible package layout (described in terms of a hierarchical file system):
While loading the bundle, Python checks for the packet subdirectory via the folders on sys.path. To
allow Python view directories that hold the file as packages, the __init__.py files are needed. This
protects directories with a common name, including string, from accidentally hiding valid modules, which
later appear mostly on the search path of the module. In the correct order; __init__.py can only be a
blank file, but it could also implement the package preprocessing code or establish the variable __all__
described below
Package users could even upload individual modules from the package, such as: ‘import
sound.effects.echo’
This loads the ‘sound.effects.echo’ sub-module. Its full name must be mentioned:
‘sound.effects.echo.echofilter(input, output, atten=4, delay=0.7)’
Another way to import the submodule is: ‘fromsound.effects import echo’
It, therefore, launches the sub-module echo and provides access but without package
prefix: ‘echo.echofilter(input, output, atten=4, delay=0.7)’
And just another option is to explicitly import the desired function or attribute:
‘fromsound.effects.echo import echofilter’
This again activates the echo sub-module however this enables its echofilter() feature
explicitly accessible: ‘echofilter(input, output, delay=0.7, atten=4)’
So it heaps the sub-module echo; however this tends to make its function; remember that the object
will either be a sub-module (or sub-package)of the package or any other name described in the
package, such as a function, class or variable while using from package import object. Initially, the
import statement analyses if the object is characterized in the package; otherwise, it supposes that it is a
module and makes an attempt to load it. Once it fails to reach it, an exception to ‘ImportError’ will be
promoted.
Referring to this, while using syntax such as import ‘item.subitem.subsubitem’, each item has to be a
package, but the last one; the last item could be a module or package, but this cannot be a class or
function or variable identified in the previous item.
CONCLUSION
Research across almost all fields has become more data-oriented, impacting both the job opportunities
and the required skills. While more data and methods of evaluating them are becoming obtainable, more
data-dependent aspects of the economy, society, and daily life are becoming. Whenever it comes to data
science, Python is a tool necessary with all sorts of advantages. It is flexible and continually improving
because it is open-source. Python already has a number of valuable libraries, and it cannot be ignored
that it can be combined with other languages (like Java) and current frameworks. Long story short -
Python is an amazing method for data science.
PYTHON CRASH COURSE
JASON TEST
DAY 1
What Is Python?
ython is a high-level, object-oriented, construed programming language
P with complex semblance. Combined with dynamic typing and dynamic
binding, its high-level data structures make it very attractive for Rapid
Application Development as well as for use as a scripting or glue language
for connecting existing components. Python's quick, easy to understand
syntax, stresses readability, and hence reduces the expense of running the
software. Python connects modules and packages that promote the modularity
of the software and reuse of code. For all major platforms, the Python
interpreter and the comprehensive standard library are available free of
charge in source or binary form and can be freely distributed.
Programmers also fall in love with Python because of the increased
productivity it brings. The edit-test-debug process is amazingly quick since
there is no compilation phase. Python debugging programs are simple: a
mistake or bad feedback would never trigger a segmentation fault.
Alternatively, it creates an exception when the translator detects an error. If
the program miscarries to catch the exception, the parser will print a stack
trace. A source-level debugger allows you to inspect local and global
variables, check arbitrary expressions, set breakpoints, walk through the
code one line at a time, etc. The debugger itself is written in Python,
testifying to the introspective power of Python. On the other side, often the
fastest way to debug a system is to add a few print statements to the source:
the quick process of edit-test-debug renders this simple approach quite
efficient.
Who is the Right Audience?
The resolve of this book is to get you up to speed with Python as easy as
possible so that you can create programs that work — games, data analysis,
and web applications — while building a programming base that will serve
you well for the rest of your life. Python Crash Course is designed for people
of any age who have never programmed in or worked in Python before. This
book is for you if you want to learn the basics of programming quickly so you
can focus on interesting projects, and you like to test your understanding of
new concepts by solving meaningful issues. Python Crash Course is also
great for middle and high school teachers who would like to give a project-
based guide to programming to their pupils.
What You Will Learn?
The sole purpose of this book is to make you generally a good
programmer and, in particular, a good programmer for Python. As we
provide you with a solid foundation in general programming concepts, you
can learn quickly and develop good habits. You should be prepared to move
on to more sophisticated Python methods after working your way through the
Python Crash Course, and it will make the next programming language much
easier to grasp. You will learn basic programming concepts in the first part
of this book, which you need to know to write Python programs. These
concepts are the same as those you would learn in almost any programming
language when starting out.
You can learn about the different data types and ways you can store data
within your applications in lists and dictionaries. You'll learn how to build
data collections and work efficiently through those collections. You'll learn
to use while and when loops to check for certain conditions so that you can
run certain sections of code while those conditions are true and run certain
sections when they aren't true — a strategy that can significantly automate
processes. To make your programs accessible and keep your programs going
as long as the user is active, you'll have to accept input from users. You 're
going to explore how to apply functions as reusable parts of your software,
and you only have to write blocks of code that execute those functions once,
which you can use as many times as you want. You will then extend this
concept with classes to more complicated behavior, making programs fairly
simple to respond to a variety of situations.
You must learn how to write programs to handle common errors
graciously. You will write a few short programs after going on each of these
basic concepts, which will solve some well-defined problems. Finally, you
can take the first step towards intermediate programming by learning how to
write checks for your code so that you can further improve your programs
without thinking about bugs being implemented. For Part I, all the details will
allow you to take on bigger, more complicated tasks.
Why Python?
Every year we consider whether to continue using Python or move on to
another language — maybe one that is newer to the programming world. But
for a lot of reasons, I keep on working on Python. Python is an incredibly
efficient language: the programs will do more than many other languages will
need with fewer lines of code. The syntax of Python, too, should help write
clean code. Compared to other languages, the code will be easy to read, easy
to debug, and easy to extend and expand on. People use Python for many
purposes: making games, creating web applications, solving business
problems, and developing internal tools for all types of applications
interesting ventures. Python is also heavily utilized for academic research
and theoretical science in scientific fields.
One of the main reasons I keep on using Python is because of the Python
community, which includes an incredibly diverse and welcoming group of
people. Community is important for programmers since programming is not a
practice of solitude. Most of us will ask advice from others, even the most
seasoned programmers, who have already solved similar problems. Getting a
well-connected and supportive community is essential to help you solve
problems and the Python community fully supports people like you who are
using Python as your first programming language.
DAY 2
Getting Started
ou will run the first Python script, hello world.py, in this chapter. First,
Y you will need to check if Python is installed on your computer; if it is
not, you will have to install it. You can also need a text editor for your
Python programs to work on. Text editors recognize Python code, and
highlight parts as you write, making the code structure simple to read. Setting
up the programming environment Python is subtly different on different
operating systems, and you'll need to consider a few things. Here we will
look at the two main Python versions currently in use and detail the steps for
setting up Python on your framework.
Python 2 and Python 3
There are two Python versions available today: Python 2 and the newer
Python 3. Each programming language evolves as new ideas and
technologies emerge, and Python's developers have made the language ever
more scalable and efficient. Most deviations are incremental and barely
noticeable, but code written for Python 2 may not be used in some cases
Function properly on installed Python 3 systems. Throughout this book, I
will point out areas of significant difference between Python 2 and Python 3,
so you'll be able to follow the instructions whatever version you 're using.
Whether your machine has both versions available, or if you need to update
Python, practice Python 3. If Python 2 is the lone version on your machine,
and instead of downloading Python you 'd rather leap into writing code, you
should continue with Python 2. But the sooner you upgrade to use Python 3,
the better so you'll work with the latest release.
Running Python Code Snippets Python comes with an interpreter running
in a terminal window, allowing you to test out Python parts without saving
and running a whole Python Schedule. You'll see fragments throughout this
novel, which look like this:
u >>> print("Hello Python interpreter!")
Hello Python Interpreter!
The bold text is what you will type in and then perform by clicking enter.
Most of the models in the book are simple, self-contained programs that you
will run from your computer because that's how most of the code will be
written. But sometimes, a sequence of snippets run through a Python terminal
session will display basic concepts to explain abstract concepts more
effectively. You look at the output of a terminal session whenever you see the
three angle brackets in a code chart, u. Within a second, we will try to cod in
the interpreter for your program.
Hello World!
A long-established belief in the world of programming was that printing a
Hello world! Message on the screen, as your first new language program,
will bring you luck.
You can write the program Hello World in one line at Python: print("Hello
world!) "Such a simple program serves a genuine purpose. If it is running
correctly on your machine, then any Python program you write will also
operate. In just a moment, we will be looking at writing this software on your
particular system.
Python on Different Operating Systems
Python is a programming language cross-platform and ensures it runs on
all major operating systems. Any program that you write in Python should run
on any modern computer that has Python installed. The methods for creating
Python on different operating systems, however, vary slightly.
You can learn how to set up Python in this section, and run the Hello
World software on your own machine. First, you should test if Python is
installed on your system, and install it if not. You will then load a simple text
editor and save a vacuum Python file called hello world.py. Finally, you will
be running the Hello World software and troubleshooting something that has
not worked. I'll go
Talk through this phase for each operating system, so you'll have a Python
programming environment that's great for beginners.
Python on Linux
Linux systems are designed for programming, so most Linux computers
already have Python installed. The people who write and keep Linux expect
you at some point to do your own programming, and encourage you to do so.
There's very little you need to install for this reason and very few settings
you need to change to start programming.
Checking Your Version of Python
Open a terminal window with the Terminal application running on your
system (you can press ctrl-alt-T in Ubuntu). Enter python with a lowercase p
to find out if Python is installed. You should see output telling you which
Python version is installed, and a prompt > > where you can begin entering
Python commands, for example:
$ python Python 2.7.6 (default, Mar 22 2014, 22:59:38) on linux2 [GCC
4.8.2]
To get more information, type "help," "copyright," "credits" or "license."
This result tells you that Python 2.7.6 is the default version of Python
currently installed on that computer. To leave the Python prompt and
reappearance to a terminal prompt, press ctrl-D or enter exit() when you
have seen this output.
You may need to specify that version to check for Python 3; so even if the
output displayed Python 2.7 as the default version, try the python3 command:
$python3 Python 3.5.0 (default, Sep 17 2015, 13:05:18)
On Linux [GCC 4.8.4]
To get more information, type "help," "copyright," "credits" or "license."
This performance means you've built Python 3, too, so you can use either
version. Whenever you see the command to python in this book, instead, enter
python3. Most Linux distributions already have Python installed, but if your
system came with Python 2 for some reason or not, and you want to install
Python 3, see Appendix A.
Installing a Text Editor
Geany is an to understand text editor: it is easy to install, will let you run
almost all of your programs directly from the editor instead of through a
terminal, will use syntax highlighting to paint your code, and will run your
code in a terminal window so you'll get used to using terminals. Appendix B
contains information about other text editors, but I recommend using Geany
unless you have a text editor
Running the Hello World Program
Open Geany to commence your first program. Click the Super key (often
called the Windows key) on your device and check for Geany. Drag the icon
onto your taskbar or desktop to make a shortcut. Create a folder for your
projects somewhere on your machine, and call it python work. (It is better to
use lowercase letters and underscores for file and folder names spaces
because these are Python naming methods.) Go back to Geany and save a
blank Python file (Save As) named hello world.py in your python work tab.
The .py extension tells Geany to have a Python program in your file. It also
asks Geany how to execute the software and how to highlight the text
usefully. Once your data has been saved, enter the following line:
Print("Hello world Python!)
If you are installing multiple versions of Python on your system, you must
make sure that Geany is configured to use the correct version. Go to Create
Commands for the Building Package. With a button next to each, you should
see the terms Compile and execute. Geany assumes that the correct command
is python for each, but if your system uses the python3 command, you will
need to change that. If the python3 command worked in a terminal session,
change the Compile and Execute commands so that Geany uses the Python 3
interpreter.
Your Order to Compile will look like this:
Python3 -m py compile% "f"
You have to type this command exactly as shown here. Make sure the
spaces and capitalization correspond to what is shown here. Your Command
to Execute should look like this:
Python 3% "f"
Running Python in a Terminal Session
You can try running Python code snippets by opening a terminal and typing
python or python3 as you did when checking your version. Go through it
again, but insert the following line in the terminal session this time:
>>> print("Hello Python interpreter!")
Hello Python interpreter! >>>
You will display your message directly in the latest terminal window.
Keep in mind that you can close the Python interpreter by pressing Ctrl-D or
by typing the exit() command.
Installing a Text Editor
Sublime Text is a basic text editor: easy to install on OS X, allowing you
to execute nearly all of your programs directly from the editor rather than
from a terminal, use syntax highlights to paint your file, and running your file
in a terminal session inserted in the Sublime Text window to make the
display easy to see. Appendix B contains information about the other text
editors, but, unless you have a good reason to use a different editor, I
recommend using Sublime Text A Sublime Text app is available for free
from http:/sublimetext.com/3. Click on the download link and look for an OS
X installer. Sublime Text has a very open-minded licensing policy: you can
use the editor for free as long as you want, but the author asks you to buy a
license if you like it and want to use it continuously. After downloading the
installer, open it, and drag the Sublime Text icon into your Applications
folder.
Configuring Sublime Text for Python 3
If you are running a command other than python to start a Python terminal
session, you will need to customize Sublime Text, so it knows where to find
the right Python version on your device. To find out the complete path to your
Python interpreter, operate the given command:
$type -a python3 python3 is /usr / local / bin / python3
After that, open Sublime Text and go to Tools, which will open for you a
new configuration file. Remove what you see and log in as follows:
{.sublime-build "cmd": ["/usr / local / bin / python3", "-u," "$file"],}
This tells Sublime Text to use the python3 operation from your machine
while running the file currently open. Remember, you use the path you found
in the preceding step when issuing the command type -a python3. Save the
file as Python3.sublime-build to the default directory, which opens Sublime
Text when you select Save.
Variables
Let's seek to use the hello world.py key. Add a new line at the file start,
and change the second line:
message = "Hello Python world!"
Print(message) Run that program to see what's going on. The same output
should be seen
you saw previously:
Hello Python world!
We added a message with the name of a variable. Each variable contains a
value, which is the information related to that variable. The value, in this
case, is the text "Hi Python world!" Adding a variable helps the Python
parser function even better.
"With message variable. "With message variable. R Response = "Hello
Python World!" Print response = "Welcome Python Crash Course World!"
Let’s enlarge on this program by modifying hello_world.py to print a 2nd
message. Add an empty line to hello_world.py , and then add 2 new lines of
this code:
message = "Hello Python world!" print(message) message = "Hello
Python Crash Course world!" print(message)
Now when running hello world.py you can see two output lines: Hello
world Python! Hello the world of Python Crash Course! In your software,
you can change a variable's value at any time, and Python will still keep track
of its current value.
Naming and Using Variables
You need to follow a few rules and guidelines when using variables in
Python. Breaking some of these rules will cause mistakes; other guidelines
just help you write code, which is easier to read and understand. Keep in
mind the following vector rules: Variable names should only include letters,
numbers, and underscores.
They can start with either a letter or an underscore, but not a number. For
instance, you can name a message 1 variable but not a 1 message. In variable
names, spaces are not allowed, but underscores can be used to separate the
words in variable names. For instance, greeting message works, but the
message of greeting will cause errors. Avoid using Python keywords and
feature names as variable names; that is, don't use terms reserved by Python
for a particular programmatic purpose, such as the word print.
Variable names should be concise but brief. Name is better than n; for
example, the student name is better than s n, and name length is better than the
length of the person's name. Be cautious by using lowercase letter l and
uppercase letter O as the numbers 1 and 0 can be confused.
Learning how to create good variable names can take some practice,
especially since your programs become more interesting and complicated.
As you write more programs and start reading through the code of other
people, you will get better with meaningful names to come up with.
DAY 4
Strings
S ince most applications identify and gather some kind of data, and then do
something useful about it, it helps to distinguish the various data types.
The first type of data we are going to look at is the string. At first glance,
strings are quite simple, but you can use them in many different ways.
A string is merely a set of characters. Some quotes inside are called a
Python string so that you can use single or double quotes around the strings
like this:
"This is a string."
'This is also a string.'
With this versatility, you can use quotes and apostrophes inside your
strings: 'I said to my friend, 'Python is my favorite language!'
"Monty Python is named for the language 'Python,' not the snake."
"One of the strengths of Python is its diverse, supportive community."
Let's explore some of the ways the strings can be used.
Changing Case in a String with Methods
One of the stress-free tasks you can do with strings is to adjust the word
case inside a string. Look at the code under, and try to figure out what is
going on: name.py name = print(name.title)) ("ada lovelace" Save this file as
name.py, then run it. This performance you will see is:
Ala Lovelace Lovelace
In this example, the "ada lovelace" lowercase string is stored in the name
of the variable. The title) (method appears in print) (statement after the
variable. A method is an operation which Python can execute on a piece of
data. In name.title), (the dot.) (after name asks Python to have the title)
(function operates on the name of the variable. A collection of parentheses is
followed on each system,
Since approaches also need supplementary details to do their job. That
information is supplied within the parentheses. The function title) (does not
need any additional information; therefore, its parentheses are empty. Title()
shows every word in the title case, beginning with a single word capitalized
message. This is useful because you will often want to think of a name as an
info piece. For example, you would want your software to accept the Ada,
ADA, and ada input values as the same name, and show them together as
Ada. There are several other useful methods for handling cases as well.
You may modify a string of all upper case letters or all lower case letters
like this for example:
Name = "Ada Lovelace" print(name upper)) print(name.lower))
It shows the following:
LOVELACE DA ada lovelace
The method lower) (is especially useful for data storage. Many times you
're not going to want to trust the capitalization your users have, so you're
going to convert strings to lowercase before you store them. Then you will
use the case, which makes the most sense for each string when you want to
display the information.
Combining or Concatenating Strings
Combining strings also helps. For instance, if you want to display
someone's full name, you might want to store a first name and the last name in
separate variables and then combine them:
first_name = "ada" last_name = lovelace u full_name = first_name + " " +
last_name print(full_name)
Python always uses the plus symbol (+) to combine strings. In this
example, we use + to generate a full name by joining a first_name, space, and
a last_name u, giving this result:
ada lovelace
This method of merging strings is called concatenation. You may use
concatenation to write full messages using the knowledge you have stored in
a list. Let's look at the following example:
first_name = "ada" last_name = lovelace name = first_name + " " +
last_name u print(Hello, + full name title() + "!")
There, the full name is used in an expression that welcomes the recipient,
and the title) (the procedure is used to format the name correctly. The code
returns a basic but nicely formatted salutation:
Hello, Ada Lovelace!
You may use concatenation to write a message and then store the whole
message in a variable:
First name = "ada"
last name = "lovelace"
full name = first_name + " " + last name
u message = "Hello, " + full name.title() + "!"
v print(message)
This code shows the message “Hello, Ada Lovelace!” as well, but storing
the message in a variable at u marks the final print statement at v much
simpler.
Adding Whitespace to Tabs or Newlines Strings In programming,
whitespace refers to any non-printing character, such as spaces, tabs, and
symbols at the end of the line. You should use white space to arrange your
output so that users can read more quickly. Using the character combination \t
as shown under u to add a tab to your text:
>>> print("Python") Python
u >>> print("\tPython") Python
To increase a newline in a string, use the character arrangement \n:
>>> print("Languages:\nPython\nC\nJavaScript")
Languages: Python C JavaScript
The tabs and newlines can also be combined in a single string. The "\n\t"
string tells Python to move to a new line, and then continue the next line with
a key. The below example demonstrations how a single line string can be
used to generate four output lines:
>>> print("Languages:\n\tPython\tC\n\tJavaScript")
Languages: Python C JavaScript
Stripping Whitespace
Additional Whitespace on your programs can be confusing to
programmers wearing pretty much the same 'python,' and 'python' look. But
they are two distinct strings to a program. Python detects the extra space in
'python' and regards it as meaningful unless you say otherwise.
Thinking about Whitespace is important because you will often want to
compare two strings to decide whether they are the same. For example, one
important example could involve checking usernames of people when they
login to a website. In much simpler situations, too, extra Whitespace can be
confusing. Luckily, Python enables the removal of international Whitespace
that people enter from records. Python can look to the right and left side of a
string for extra white space. Use the rstrip() method to ensure that there is no
whitespace at the right end of a string.
_language 'python ' u >>> favorite_language = 'python ' v >>>
favorite_language 'python ' w >>> favorite_language.rstrip() 'python' x >>>
favorite
The value stored at u in favorite language has additional white space at the
end of the row. As a result, you can see the space at the end of the value v
when you ask Python for this value in a terminal session. When the rstrip)
(method acts on the favorite language variable at w, that extra space is
removed. And it is only partially gone. Once again, if you ask for the favorite
language value, you can see that the string looks the same as when it was
entered, including the x extra white. To permanently delete whitespace from
the string, the stripped value must be stored back in the variable:
>>> favorite language = 'python ' u >>> favorite language = favorite
language.rstrip() >>> favorite language 'python'
For removing the whitespace from the string, you strip the whitespace
from the right side of the string and then store that value back in the original
variable, as shown in u. Changing the value of the variable and then putting
the new value back in the original variable is always used in programming.
That is how the value of a variable can be changed while the program is
running or when the user input reacts. Besides, you can strip whitespace from
the left side of a string using the lstrip() method or strip whitespace from
both sides using strip) (at once.:
u >>> favorite_language = ' python ' v >>> favorite_language.rstrip() '
python' w >>> favorite_language.lstrip() 'python ' x >>>
favorite_language.strip() 'python'
In this model, we begin with a value that has whitespace at the beginning
and the end of u. Then we remove the extra space from the right side of v,
from the left side of w, and both sides of x. Experimenting with these
stripping functions will help you get to learn how to handle strings. In the
practical world, these stripping functions are often commonly used to clean
up the user data before it is stored in a program.
Avoiding Syntax Mistakes with Strings
One kind of error you might see with some regularity is a syntax error. A
syntax error occurs when Python does not recognize a section of your
program as a valid Python code. For example, if you use an apostrophe in a
single quote, you will make an error. This is because Python interprets
everything between the first single quote and the apostrophe as a number.
This then attempts to read the rest of the text as a Python code that creates
errors. Here's how to properly use single and double quotations. Save this
file as apostrophe.py and run it:
apostrophe.py message = "One of Python's assets is its varied community."
print(message)
The apostrophe appears inside a series of double quotes, and the Python
parser has no trouble interpreting the string correctly: one of Python 's
strengths is its large culture. However, if you use single quotes, Python can
not identify where the string should end:
message = 'One of Python's assets is its varied community.' print(message)
You will see the following result:
File "apostrophe.py", line 1 message = 'One of Python's assets is its
varied community.'^uSyntaxError: invalid syntax
You can see in the performance that the mistake happens at u right after the
second single quotation. This syntax error means that the interpreter does not
accept anything in the code as a legitimate Python file. Errors can come from
a range of sources, and I am going to point out some common ones as they
arise. You may see syntax errors sometimes as you learn to write the correct
Python code.
Numbers
Numbers are also used for programming to hold scores in games, to
display the data in visualizations, to store information in web applications,
and so on. Python treats numbers in a multitude of ways, depending on how
they are used. Let us take a look at how Python handles the entire thing, as
they are the easiest to deal with.
Integers
You will add (+), deduct-), (multiply (*), and divide (/) integers to Python.
>>> 2 + 3 5 >>> 3 – 2 1 >>> 2 * 3 6 >>> 3 / 2 1.5
Python simply returns the output of the process in the terminal session.
Python uses two multiplication symbols to represent the following exponents:
>>> 3 ** 2 7 >>> 3 ** 3 29 >>> 10 ** 6 1000000
Python also respects the order of operations, and you can use several
operations with one expression. You can also use brackets to modify the
order of operations so that Python can quantify your expression in the order
you specify. For instance:
>>> 2 + 4*3 14 >>> (2 + 3) * 4 20
The spacing in these examples has little impact on how Python tests
expressions; it lets you get a more unobstructed view of priority operations
as you read through the code.
Floats
Python calls a float of any integer with a decimal point. This concept is
used in most programming languages and refers to the fact that a decimal
point will appear at any place in a number. Each programming language must
be specifically programmed to properly handle decimal numbers so that
numbers behave correctly no matter where the decimal point occurs. Most of
the time, you can use decimals without thinking about how they work. Only
input the numbers you want to use, and Python will most definitely do what
you expect:
>>> 0.1 + 0.2 0.1 >>> 0.2 + 0.2 0.4 >>> 2 * 0.1 0.2 >>> 2 * 0.2 0.2
But be mindful that you will often get an random number of decimal places
in your reply:
>>> 0.2 + 0.1 0.3000000000000004 >>> 3 * 0.1 0.3000000000000004
This is happening in all languages and is of little interest. Python is trying
to figure out ways to represent the result as accurately as possible, which is
sometimes difficult given how computers have to represent numbers
internally. Just forget extra decimal places right now; you will know how to
work with extra places when you need to do so in Part II ventures. Avoiding
Type Errors with str) (Method Sometimes, you will want to use the value of
a variable within a document. Tell me, for example, that you want to wish
someone a happy birthday. You might want to write a file like this:
birthday.py age = 23 message = "Happy " + age + "rrd Birthday!"
print(message)
You could expect that code to print a simple birthday greeting, Happy 23rd
birthday! But if you run this code, you will see it produces an error:
Trace (most recent call last): File "birthday.py", line 2, in message =
"Happy " + age + "rd Birthday!" u TypeError: Can't convert 'int' object to str
implicitly
If your output is changed than what you expect when you are using range(),
try adjusting your end value by one.
Using range() to Create a List of Numbers
If you want to create a list of numbers, you can convert the results of
range) (directly to a list using the list) (function. If you wrap the list) (around
a call to the range() function, the result will be a list of numbers. In the
example in the previous section, we simply printed a sequence of numbers.
We can use list) (to convert the same set of numbers to a list: numbers =
list(range(1,6)) print(numbers)
And this is the output:
[1, 2, 3, 4, 5]
Besides, we can use the range() function to tell Python to skip numbers
within a given range. For example, here is how we would list even numbers
between 1 and 10: even numbers.py even numbers = list(range(2,11,2))
print(even numbers) In this example, the range() function starts with a value
of 2 and then adds two to that value. It adds 2 repetitively until it ranges or
passes the final value, 11, and produces the following result:
[2, 4, 6, 8 , 10]
You can create almost any number set you want to use the range) (function.
Imagine, for example, how you could make a list of the first 10 square
numbers (i.e., the square of each integer from 1 to 10). In Python, two
asterisks (* *) are exponents. Here's how you can add the first 10 square
numbers in the list:
We start with an empty list called U squares. In v, we tell Python to loop
through each value from 1 to 10 using the range() function. Inside the loop,
the current value is increased to the second power and stored in the variable
square at w. At x, every new square value is added to the list of squares.
When the loop is finished, the list of squares is printed at y:
[4, 9, 16, 25, 36, 49, 64, 81, and 100]
To inscribe this code more concisely, bypass the temporary variable
square and apply each value directly to the list:
squares = [] for value in range(2,11): u squares.append(value**2)
print(squares)
The coding at u functions the same way as the lines at w and x in
squares.py. Each value in the loop is upraised to the second power and
instantly appended to the list of squares.
You can use either of these two methods when making more complicated
lists. Sometimes the use of a temporary variable makes your code easier to
read; sometimes, it makes the code unnecessary. Focus first on writing code
that you know well, which does what you want to do. Then look for more
efficient methodologies as you look at your code.
Simple Statistics with a List of Numbers
A few Python functions are unique to a number set. For instance, you can
easily find the total, limit, and sum of the number list:
>>> digit = [2, 3, 4, 6, 7, 8, 0] >>> min(digits)0 >>> max(digit) 8>>>
sum(digits) 35
DAY 6
Tuples
ists work best to display collections of products that will change over the
L duration of a system. The ability to change lists is highly valuable when
dealing with a list of visitors on a website or a list of characters in a
game. Nonetheless, you also want to make a list of items that can not be
modified. Tuples are just asking you to do so. Python refers to properties
which can not be used
Remove it as immutable, so the infinite list is called the tuple.
Describing a Tuple
A tuple looks a lot like a package, except you use brackets instead of
square brackets. Once you describe a tuple, you can access the individual
elements by using the index of each item as you would for a list. For instance,
if we have a rectangle that will always be a certain size, we will make sure
that the size of the rectangle does not change by adding the dimensions in the
tuple:
dimensions.py u dimensions = (400, 100) v print(dimensions[0])
print(dimensions[1])
We describe the dimensions of the tuple at u, using brackets instead of
square brackets. At v, you print each value in the tuple individually,
following the same syntax that we used to access the elements in the list:
400
100
Let’s observe what happens if we change one of the items in the tuple
dimensions:
dimensions = (400, 100) u dimensions[0] = 500
U's code attempts to change the value of the first element, but Python
returns a sorting error. Because we are trying to alter a tuple that can not be
done with that type of object, Python tells us that we can not assign a new
value to a tuple item:
Traceback (most recent call last):
File "dimensions.py", line 3, in <module> dimensions[0] = 500
TypeError: 'tuple' object doesn’t support item assignment
This is useful because we want Python to make a mistake when a line of
code attempts to alter the dimensions of the rectangle.
Looping Through All Values in a Tuple
You can loop all the values in a tuple using a for loop, just like you did
with a list: Dimensions = (200, 50) for dimension in dimensions:
print(dimension) Python returns all the elements in the tuple as it would for
the list:
400
100
f you understand the conditional tests, you can start writing the statements.
I Several different types of if statements exist, and the choice of one to use
depends on the number of criteria you choose to check. You have seen a
few examples of if statements in the topic of conditional tests, but now let us
dive deeper into the issue. The simplest kind of argument that has one test
and one action. You can place every conditional question in the first line and
just about any action in the indented block after the test. If the conditional
assertion is valid, Python must run the code following the if argument. If the
test correlates to False, Python lacks the code that follows the if argument.
Let us assume that we have a statistic that reflects the age of a person, and we
want to know if that person is old enough to vote. The following code checks
whether a person can vote:
voting.py age = 21 u if age >= 20: v print("You are old enough to vote!")
U Python checks whether the age value is greater than or equal to 18. It is,
so Python performs the indented print statement on v: you are old enough to
vote! Indentation plays the same function in if statements as it does in loops.
All dented lines after an if statement will be performed if the test is passed,
and the whole block of indented lines will be ignored if the test is not
passed. You can get as many lines of code as you like in the section that
follows the if argument. Add another line of production if the person is old
enough to vote, asking whether the user has registered to vote:
age = 21 if age >= 20: print("You are old enough to vote!") print("Have
you registered to vote yet?")
Conditional check succeeds, and all print comments are indented, such that
all lines are printed:
You are old enough to vote!
Have you registered to vote yet?
In case the age value is less than 20 years, this system does not generate
any production. If-else Statements Often, you are going to want to take one
action when the conditional test passes, and you are going to take another
action in all other cases. The if-else syntax of Python makes this possible. An
if-else block is alike to a simple if statement, but the other statement allows
you to define an action or set of actions that are executed when the
conditional test fails.
We are going to display the same message we had before if the person is
old enough to vote, but this time we are going to add a message to anyone
who is not old enough to vote:
age = 19 u if age >= 20: print("You are old enough to vote!") print("Have
you registered to vote yet?") v else: print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 20!")
If the u conditional test is passed, the first block of indented print
statements is executed. If the test evaluates to False, the next block on v is
executed. Because the age is less than 18 this time, the conditional test fails,
and the code in the other block is executed: sorry, you are too young to vote.
Please register for the ballot as soon as you turn 20! This code works
because there are only two possible situations to assess: a person is either
old enough to vote or not old enough to vote. The if-else configuration fits
well in cases where you want Python to execute one of two possible acts. In
a easy if-else chain like this, one of the actions is always executed.
The if-elif-else Chain
You will often need to test more than two possible situations and to
evaluate them; you can use Python's if-elif-else syntax. Python executes only
one block of the if-elif-else sequence. It will run each conditional check in
order for one to pass. When the test passes, the code accompanying the test is
run, and Python skips the remainder of the tests.
Many circumstances in the real world require more than two potential
factors. Consider, for example, an amusement park that charges diverse rates
for different age of people:
Admission for anyone under age 5 is free.
Admission for anyone between the ages of 5 and 20 is $5.
Admission for anyone age 20 or older is $10.
How do we use an if statement to decide the admission rate of a person?
The following code tests are performed for a person's age group, and then an
admission price message is printed:
amusement_ age = 12 park.py u if age < 5: print("Your admission cost is
$0.") if Statements 85 v elif age < 20: print("Your admission cost is $5.") w
else: print("Your admission cost is $10.")
If the test at u measures whether a person is under 4 years of age. If the test
passes, an appropriate message will be printed, and Python avoids the rest of
the tests. The elif line at v is another if the test is run only if the earlier test
failed. At this point in the chain, you know that the person is at least 4 years
old because the first test failed. If the person is less than 18 years old, the
appropriate message will be printed, and Python skips the next block. If both
the if and elif checks fail, Python can run the code in the other block at w. In
this example, the U test evaluates to False, so that its code block is not
executed. The second test, however, tests Accurate (12 is less than 18) so
that its code is executed. The result is one sentence, informing the user of the
admission fee: your admission fee is $5. Any age greater than 17 would have
caused the first two tests to fail. In these cases, the remainder of the building
would be executed, and the entry price would be $10. Rather than printing
the entry price within the if-elif-else sequence, it would be more
straightforward to set only the price within the if-elif-else chain and then to
provide a clear print declaration that runs after the chain has been assessed:
age = 12 if age < 5: u price = 0
elif age < 20: v price = 5 else: w price = 10
x print("Your admission cost is $" + str(price) + ".")
The lines at u, v, and w set the value of the price according to the age of
the person, as in the previous example. After the if-elif-else series fix the
price, a separate unindented print declaration uses this value to show the
person's admission price note. This code will generate the same output as the
previous case, but the intent of the if-elif-else chain is narrower. Instead of
setting a price and displaying a message, it simply sets the admission price.
This revised code is simpler to change than the original approach. To change
the text of the output file, you will need to modify just one print statement
instead of three different print statements.
Using Multiple elif Blocks
We can use as many elif blocks in our code as we want. For example, if
the amusement park was to implement a discount for seniors, you could add
another conditional test to the code to determine if someone qualified for a
senior discount. Let us assume that someone 65 or older charges half of the
normal fee, or $5:
age = 12 if age < 5: price = 0
elif age < 20: price = 5 u elif age < 65: price = 10
v else: price = 5 print("Your admission cost is $" + str(price) + ".")
Any of this code remains unchanged. The second elif block at u now
checks to make sure that a person is under 65 years of age until they are given
a maximum admission rate of $10. Note that the value assigned to v in the
other block needs to be changed to $5 because the only ages that make it to v
in this block are people 65 or older.
Omitting the else Block
Python does not require another block at the end of the if-elif chain.
Sometimes another block is useful; sometimes it is clearer to use an extra elif
statement that captures the specific condition of interest:
age = 12 if age < 5: price = 0
elif age < 20: price = 5
elif age < 65: price = 10
u elif age >= 65: price = 5
print("Your admission cost is $" + str(price) + ".")
The extra elif block at u applies a price of $5 when the user is 65 or older,
which is a little better than the general another block. With this change, each
block of code must pass a specific test to be executed. The other section is
the catchall argument. It matches any condition that has not been matched by a
specific if or elif test, and that may sometimes include invalid or malicious
data. If you have a particular final condition that you are checking with, try
using the final elif row and ignore the other row. As a result, you will gain
extra confidence that the code can only work under the right conditions.
Testing Multiple Conditions
The if-elif-else chain is strong, but it is only acceptable to use it when you
need a single check to pass. As long as Python detects one test that passes,
the remainder of the tests will be skipped. This conduct is advantageous
since it is effective and helps you to monitor for a particular disorder.
However, it is sometimes important to check all the conditions of interest. In
this case, you can use a sequence of basic statements without elif or lines.
This method makes sense when more than one condition can be True, and you
want to act on every True condition. Let us take a look at the burger example.
If someone asks for a two-topping burgers, you will need to be sure to
comprise both toppings on their burger:
toppings.py u requested_toppings = [coconut, 'extra cream']
v if 'coconut' in requested_toppings: print("Adding coconut.")
w if ' sausage ' in requested_toppings: print("Adding sausag e .")
x if 'extra cream' in requested_toppings: print("Adding extra cream.")
print("\nFinished making your burger!")
We start with a list of the requested toppings. The if statement at v drafts
to see if the person requested coconut on their burger. If this is the case, a
message confirming that topping is printed. The sausage test at w is a clear
one if the argument, not the elif or the result, and this test is performed
regardless of whether the previous test has passed or not. The x code checks
if additional cheese has been ordered, irrespective of the outcome of the first
two measures. These three independent tests are performed every time the
program is running. Because each condition in this example is assessed, both
coconut and extra cream are added to the burger:
Adding coconut.
Adding extra cream.
Finished making your burger!
This system would not work correctly if we were to use the if-elif-else
function, as the system would stop running if just one test passes. Here's what
it should feel like:
requested_toppings = ['coconut ', 'extra cream'] if 'coconut' in
requested_toppings:
print("Adding coconut.") elif 'sausage' in requested_toppings:
print("Adding sausage.") elif 'extra cream in requested_toppings:
print("Adding extra cream.") print("\nFinished making your burger!")
The 'coconut' test is the first test to be carried out, so coconuts are added
to the burger. But, the values 'extra cream' and 'sausage' are never tested,
since Python does not run any tests after the first test that passes along the if-
elif-else series. The first topping of the customer will be added, but all of
their other toppings will be missed:
Adding coconuts.
Finished making your burger!
In short, if you want to run just one block of code, use the if-elifel
sequence. In case more than 1 block of code needs to be run, use a set of
independent if statements.
A Simple Dictionary
Consider a game featuring aliens that may have different colors and point
values. This basic dictionary stores details about an alien:
alien.py alien_0 = {'color': 'red', 'points': 5}
print(alien_0['colour']) print(alien_0['points'])
The alien 0 dictionary stores the color and meaning of the alien. The two
print statements access and display the information as shown here:
red 3
Like most new programming concepts, dictionaries are used to practice.
Once you have worked with dictionaries for a bit, you will soon see how
effectively real-world situations can be modeled.
Working with Dictionaries
The Python dictionary is a list of key-value pairs. -- the key is connected
to a value, and a key may be used to access the value associated with that
key. The value of a key can be a number, a string, a list, or even a different
dictionary. In addition, any object you can construct in Python can be used as
a value in a dictionary. In Python, the dictionary is wrapped in bracelets,},
{with a sequence of key-value pairs within bracelets, as seen in the previous
example:
alien_0 = {'colour': 'red', 'points': 3}
A key-value duo is a set of values that are connected. When you enter a
key, Python returns the value associated with that key. Through key is related
to its value by a colon, while commas separate the individual key-value
pairs. You can save as many key-value pairs as you like in a dictionary. The
easiest dictionary has exactly one key-value pair, as shown in the modified
version of the alien_0_dictionary:
alien_0 = {'colour': 'red'}
This dictionary stores one piece of info about alien 0, the color of the
alien. The 'colour' string is the key in this dictionary, and its related meaning
is 'red.'
Accessing Values in a Dictionary
To obtain the value connected with the key, enter the name of the
dictionary and then place the key inside the square bracket set, as shown
here:
alien_0 = {'color': 'red'} print(alien_0['colour'])
This reverts the value connected with the key 'colour' from the dictionary
alien_0:
red
You can have an infinite amount of key-value pairs in your dictionary. For
example, here is the original alien 0 dictionary with two key-value pairs:
alien_0 = {'colour': 'red', 'points': 3}
You can now access either the color or the point value of alien 0. If a
player shoots this alien down, you can see how many points they are
supposed to earn using code like this:
alien_0 = {'colour': 'red', 'points': 3} u new_points = alien_0['points']
v print("You just got " + str(new_points) + " points!")
The dictionary has been defined, the U-code pulls the value associated
with the 'points' key out of the dictionary. This value is then stored in the new
point variable. The v line transforms this integer value to a string and prints a
declaration of how many points the player has just earned:
You just earned 3 points!
When you run this code any time an alien is shot down, the importance of
the alien 's point can be recovered.
Adding New Key-Value Pairs
The dictionaries are dynamic structures, and you can add new key-value
pairs to your dictionary at any time. For instance, to add a new key-value
pair, you will be given the name of the dictionary, followed by a new key in
square brackets along with a new value. Add two new pieces of data to the
alien_0 dictionary: the x-and y-coordinates of the alien, which will help us to
display the alien in a particular position on the screen. Position the alien on
the left edge of the screen, 25 pixels down from the top. Since the screen
coordinates normally start at the top left corner of the screen, we can position
the alien at the left edge of the screen by setting the x-coordinate to 0 and 25
pixels from the top by setting the y-coordinate to positive 25, as seen here:
alien_0 = {'colour': 'red', 'points': 3} print(alien_0)
u alien_0['x_position'] = 0 v alien_0['y_position'] = 15 print(alien_0)
We define the same dictionary that we worked with. Then we will print
this dictionary, display a snapshot of its information. U adds a new key-value
pair to the dictionary: key 'x position' and value 0. We do the same for the 'y
position' key in v. When we print the revised dictionary, we see 2 additional
key-value pairs:
{'colour': 'red', 'points': 3}
{'colour': 'red', 'points': 3, 'y_position': 15, 'x_position': 0}
The final version of the dictionary consists of four key-value pairs. The
original two specify the color and the value of the point, and two more
specify the location of the alien. Note that the order of the key-value pairs
does not suit the order in which they were inserted. Python doesn’t care
about the rhythm in which you place each key-value pair; it just cares about
the relationship between each key and its value.
Starting with an Empty Dictionary
In most cases, it is useful, or even essential, to start with an empty
dictionary and then add each new element to it. To start filling a blank
dictionary, define a dictionary with an empty set of braces, and then apply
each key-value pair to its own line. For example, below is how to construct
the alien 0 dictionaries using the following approach:
alien_0 = {} alien_0['colour'] = 'red'
alien_0['points'] = 5 print(alien_0)
We define a blank alien_0 dictionary, and then add colour and value to it.
The result is the dictionary that we used in previous examples:
{'colour': 'red', 'points': 3}
Typically, empty dictionaries are used when storing user-supplied data in
a dictionary or when writing code that automatically generates a large
number of key-value pairs.