You are on page 1of 71

Python for beginners workbook

WORKBOOK WITH QUESTIONS, SOLUTIONS AND


PROJECTS. PRACTICE AND LEARN CODING WITH
A FAST METHOD

Brian Dayton
Table of Contents
Introduction
Chapter 1: Welcome Back to Python
What is Python?
Who Uses Python?
The Benefits of Python
Chapter 2: Working with Our Functions
Exercises for Python Functions
Chapter 3: Conditional Statements and Loops
The Conditional Statements
The Loops
Chapter 4: Different Data Types and How to Use Them
The Array
The List
The Dictionary
The Tuple
Chapter 5: Your Operators
The Different Types of Operators
The Exercises with Operators
Chapter 6: Classes in Python
Chapter 7: How to Make Your Own Inheritance
An Example of Using an Inheritance
Chapter 8: Creating Exceptions
Raising an Exception
An Exercise in Defining Your Exceptions
Chapter 9: The Regular Expressions
Basic Patterns to Remember
The Exercises
Chapter 10: File Input and Output
The Exercises
Chapter 11: Other Projects to Try
The Magic 8 Ball
The Code
The Hangman Game
Python and Email
Overviewing the Email Package
Conclusion
Description
Introduction
Congratulations on purchasing Python for beginners workbook and thank you
for doing so.

The following chapters will discuss some of the basic knowledge that we
already took some time to work on in the past, and work on some practical
exercises that will help us to put those topics to good use along the way. We
make the assumption in this that you have at least a beginning level
knowledge of how to work with Python, and we will take that information
and use it to help us understand more how to work with these different
exercises to get the most out of it.

We will start out this guidebook with a quick introduction to what Python is
all about and how important it can eb for those who want to learn a bit about
coding and how to make a most out of this for their own programming needs
as well. This is a good refresher to take a look at how the Python language
works and what we can do with it even as a beginner.

When that is done, it is time for us to move on to some of the basics we need
to know to work with the different parts of Python. In all of these chapters,
we will include some great exercises that you can try out, using some of the
most common parts of the Python language that beginners need to explore.
This will also include a summary of the basic parts to help refresh us along
the way. this is a good way to practice some of your basics in Python, along
with the answers to see whether this works well or not.

There are many great topics that we can look at throughout this guidebook
and that can help us to really understand how this language works. And it
never hurts to learn a bit more about how to write out our own codes and
make them work for us in no time at all. We will look at how to handle things
like functions, conditional statements, loops, classes, objects, and more. We
can even move on and look a bit more at some more complicated parts of the
process that will teach us how to handle and write our own exceptions and
inheritances.

While the majority of this guidebook will separate out the different topics so
we can look at them one by one, we can then dive into some fun projects that
can combine more than one of these at a time, helping us to get some more
practice as we go through all of this, and ensuring that we can see how some
of these topics are supposed to fit into the work we are doing.

The Python coding language is one of the best options out there when you are
ready to start learning how to work on programming for your personal or
professional needs. It is easy to learn, has a lot of power, and we will dive
into so many of the different parts that we are able to do with this along the
way. when you are ready to get some practical examples of how Python
works, and to get some hands-on examples to teach you more about it, make
sure to check out this guidebook to get started.
Chapter 1: Welcome Back to Python
Welcome back to the book of Python programming language! In our last
book, we spent some time together talking about the Python language and all
of the really neat things that you can do with this language along the way.
now it is time for us to put some of that knowledge to work and see how we
can make several programs, and do a wide variety of exercises to make sure
we are set to see some good results with this in no time.

Before we get into some of those exercises that you can do, we will go
through a little review of the Python language. This will make it easier for us
to remember where we left off, and can help guide the rest of the discussion
we have in this guidebook.

What is Python?
Python is a programming language that is easily available to anyone who
would like to get some work done and can make solving some computer
problems, whether they are simple or more complex, almost as easy as
writing out some of your thoughts about the solution. You can write out the
code just once, and it will run and work well on pretty much any computer
that you like, without you having to come in and change the program up.

Python is considered a programming language that is general-purpose and we


can use it with any modern computer operating system that we would like.
We can use it to process text, images, numbers, scientific data, and anything
else that you may think about saving on your computer. It is used in many
different companies, including the search engine we know and love about
Google, the video-sharing website of YouTube, and more. These are just a
few of the places where Python is able to play a big role in how successful
the government, business, or a non-profit can be.

Python is seen as an interpreted language. What we see with this is that the
code you write will not be changed over to code that is read by the computer
before the program ever runs; this is done during the runtime. In the past, this
was known as a type of scripting language, intimating its use was just for
some small tasks. But thanks to Python and a few other coding languages out
there, there is a bit change in this nomenclature and scripting languages are
able to do a lot more than they did in the past.

In addition, you will find that now more than ever, large applications are
written out almost completely in this Python language. There are so many
ways that we are able to work with Python and apply it including:

1. Programming for a variety of web applications


2. Building our own RSS reader
3. Reading and writing with MySQL for some of your database
needs
4. Working on files.
5. And so much more.

And this is just the start to what we can see when we work on Python. Python
is a great coding language to spend our time on, and learning how to make it
behave and how to do the different parts of the coding can make a world of
difference in how much success we can find.

You will quickly notice that there are a lot of things that we can do when we
bring out the Python language. It is simple to work with, and there are many
programs, and even some smaller codes, that we can use this with. We will
give tons of examples, both simple and more complex, that will show us how
to write out the different types of things that you can do with it. If you have
already gone through the other book in this series, then you have a lot of the
information that you need to get through all of these and can dive right into
these exercises to gain some knowledge and practice.

And that is what a lot of beginners like about working with Python. Even
some of the most basic knowledge can be enough to help you write out all the
codes that you want along the way. This will ensure that you are able to get
your hands dirty and ready to go with coding, even with just a bit of time.
Don’t be scared to dive right into a few of these and see how great it can be
to write codes with confidence.
Who Uses Python?
The next thing that we need to spend some time looking at is who uses
Python. Pretty much anyone can benefit from working with Python. It is
simple, but really powerful, so that will make sure it will get all the work
done, no matter what your project is all about. This makes it the perfect tool
no matter what you need to learn a programming language for.

Anyone can work with Python. If you are looking to learn a new
programming language to help improve your skills, whether it is the first
coding language that you learn or the tenth or somewhere in between, or you
are looking to make sure that you can create an app or something else to
make some money, you will find the Python programming language is the
best option to use.

Many of your favorite companies are already working with Python. YouTube
utilizes this as part of their software, among a few other languages, to provide
good streaming movies and videos to those who use the software. Then there
is Google who will use this to help out with their search engine, and
Facebook has been known to use Python, at least in part, with some of their
apps as well.

If this doesn’t show you some of the great benefits of Python, then nothing
will. If those giants of the industry can see the value that come with Python
and using it for their needs, then it is no wonder that many programs, those
developing apps and games and software for a variety of different uses and
reasons, would be able to find something good out of Python as well. don’t
let this intimidate you. Just because those big names are using Python doesn’t
mean that this is a big scary thing that you will never be able to learn. The
contrary is true; these big names like to use Python and all the tools that come
with it because it is so simple and easy to use.
The Benefits of Python
There are a lot of benefits that we can get when we use Python for some of
our programming needs. It is one of the best programming languages out
there, and learning how to use it can open up many doors in the world for us.
Whether you want to learn how to do this to make some of your own
projects, or you have dreams of bigger and better things with your job, the
Python language is one of the best options for you. Some of the reasons that
you should give Python a try include:

1. It is easy to learn: The Python language is designed for those who


may have no coding experience in the past. It is written out in the
English language, is simple to use, and even after just our first
book, you should know how to write out a few codes. We will
take this even further so you can see more of the simplicity that
comes with Python, and learn the language a bit better.
2. It has a lot of power: You may worry a bit about using Python
because it is simple and because it was designed to work well for
beginners. But even with this simplicity, Python can handle all of
the projects that you need with plenty of power to spare.
3. It is object oriented: This ensures that writing out the programs is
easier, and that each part of the code will have its own spot along
the way. Working with classes and objects will really make it
easier for us to see something happen with our codes.
4. The library with this is big. This is great because it allows you a
way to work on a lot of different projects with ease. Most of what
you need to do in Python will be found inside this library, which
can take away quite a bit of the work that you need to accomplish
along the way. There are also quite a few extensions and more
from other companies that you can add on to the Python language
if there are a few other tasks that you would like to work on and
the regular library can’t help you with them.
5. It has a large community to help you get started: Whether you are
a beginner or have been working with coding for some time, there
may be situations where you get stuck, or where you want to learn
something new and you aren’t sure how to get started. This
language has a large community of people from around the world
who can help you by answering your questions, showing you how
to do different types of codes, and more. This can make it easier to
learn this language and how to make this process work well.
6. It can work on a lot of different parts of your apps and programs.
While some languages, like JavaScript only work with one
specific task that you want to handle, you can take Python and use
it almost anywhere that you would like. This makes it easier for
you to go through and make sure that it works on any computer,
on any system, and for anything that you want along the way. this
is also a big reason why so many people will choose to go with
this option rather than one of the other coding languages out there.
7. It works with other languages: Some people like to go with Python
because it has the ability to work with other coding languages if
you need. If you need to create a strong program and it is best to
write some of it in Python, but then use another language to finish
it up, that is a possibility as well. Python works well with pretty
much any kind of language that you would like so there shouldn’t
be any issues there.
8. It can help with machine learning and other tasks. There are only a
few languages out there designed to handle some of the complex
things that you need to do with machine learning and other
options. Python has a bunch of third-party libraries that attach to
it, and make it really easy to go through and complete any kind of
machine learning algorithms you would like. In our digital age
where data is king, Python is finding even more popularity
because of this feature.
9. It can work with complex ideas: If you want to take some of your
basic knowledge that we explore here and put it to good use with
some more complicated projects later on, such as machine
learning or data analysis, this is possible with Python. Just
learning the basics will make it easier for us to put all of this
together and ensure that we get the best results when we are ready
to take on some of the more complicated parts along the way.

There are a ton of things to enjoy about the Python language. It is a simple
language to focus on, one that even a beginner is able to learn and get
familiar with in little time, which is why it is so popular and why so many
people wan tto learn how to work with it along the way. When you are ready
to work with a coding language that can help further your career and can be a
lot of fun in the process, make sure to check out this guidebook to help you
get started.
Chapter 2: Working with Our Functions
The first thing we need to focus on here are our functions and what we can do
with these. Let’s do a little review to remember how we can handle these
functions and make them work for some of our needs in this guidebook. To
start, the functions will be a set of expressions, or we can call them
statements, that can come with a name or are seen as anonymous. They will
be some of the first objects of our class (which we will review a bit more
later) that are found in our codes. This means that they get to set the rules,
rather than following all of the restrictions.

You would use these functions in a manner that is similar to what we do with
some of the other values out there, including strings and numbers, and they
will come with some various attributes that can help us out here. We can
think about the function as being a block of code that is set up to only run
when your program calls it, and never any other time. You can then pass on
some data, which we will call parameters, into this function, to make it
behave the way that we want. Then that same function will return data as a
result in all of this.

Now these functions can be diversified and there are a number of attributes
that we can use when we would like to add these functions into our codes and
to make sure that they will behave the way that we want. Some of the options
that we can choose for these functions will include:

__doc__: This is going to return the docstring of the function that


you are requesting.
Func_default: This one is going to return a tuple of the values of
your default argument.
Func_globals: This one will return a reference that points to the
dictionary holding the global variables for that function.
Func_dict: This one is responsible for returning the namespace
that will support the attributes for all your arbitrary functions.
Func_closure: This will return to you a tuple of all the cells that
hold the bindings for the free variables inside of the function.

There are different things that you can do with your functions, such as
passing it like an argument over to one of your other functions if you need.
Any function that is able to take on a new one as the argument will be
considered the higher-order function in the code. These are good to learn
because they are important to your code as you move.

Exercises for Python Functions


Now that we know a bit more about how to work with these functions and
what they are all about, it is time for us to dive in a bit more and explore
some of the exercises that we want to use with this one. Take some time to
look at each one and then figure out what the answer would be on your own.
Then we can move on to looking at the answers that come with them.

Exercise 1:

The first thing that we want to work wit here is that we would like to write
out a new function in this language, but we want to make sure that it is the
max of three numbers that we are working with. You can choose any
numbers that you want, but we will have an example of how we would do
this at the end of the chapter.

Answer

def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))

Exercise 2:

The second exercise that we will look at here is going to take a list of
numbers that we have and then figure out what they all equal. We want to use
a function in Python to make this happen. Our sample list for this is (8, 2, 3,
0, 7) and we expect the output to be 20. Try it out and see if you get this same
output for yours as well.
Answer

def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))

Exercise 3:

Now that we have gone through and found the sum of all the numbers, we
want to try some multiplication. In this exercise, we want to make a list of
numbers and have our function in Python multiply all of these numbers. The
sample list that we will use here is (8, 2, 3, 01, 7) and the output that we
should expect if we do this well is -336.

Answer

def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
print(multiply((8, 2, 3, -1, 7)))

Exercise 4

Now it is time for us to go through and work on a function that is a bit more
complicated so that we can take our skills with functions and move them on
to the next level. In this one we want to create a good function that is able to
take on a string and then will work for us to do a calculation to figure out
how many letters there are both the uppercase letters in the string and the
lower case ones as well. the same string that we will use here is “The quick
Brow Fox” the output that we hope to see out of this one is that there are
three uppercase and 12 lowercase letters here.

Answer
def string_test(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", d["UPPER_CASE"])
print ("No. of Lower case Characters : ", d["LOWER_CASE"])

string_test('The quick Brown Fox')

Exercise 5

We took the time to have some fun looking at how many of each type of
letter was in our string. Now we will take a look at how to set up the
parameter that we want to work with as well. For this one to work, we want
to set up the parameter as seeing whether a number is prime or not within our
string. Remember here that a prime number, which can sometimes just go by
the term of prime, then that means tat the number is natural and will be higher
than one, and it will not be divisible by anything other than 1 and itself. Let’s
look to see how we can make this one happen.

Exercise 6

Along the same lines, we will use something that is similar to what we did
above, but now we want to provide the list of numbers, and then have the
compiler list out which ones are even numbers. It is also easy to change this
around and look up the odd numbers as well. we are going to use a list of
numbers that go from one to nine and ask the compiler to return the even
numbers to us. Try out the code before looking at the answer.

Answer

def is_even_num(l):
enum = []
for n in l:
if n % 2 == 0:
enum.append(n)
return enum
print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9]))

Exercise 7
This one is going to take a look at how to make a function that will look to
see whether the number is perfect or not within our string. We need to look at
what we mean by this includes. When we talk about this in the world of
number theory, we can find perfect numbers, and those are basically when we
have a posiive integer ha will be the sum of all he divisors that can go into it
through this as well. Wat we will see is that you can take all of the numbers
that we can divide into this one and add them together, not counting the
actual number, and ten it will equal the number. You could also work with
these perfect numbers and find that is half the sum of all the divisors,
including itself.

It is possible to look at this more and see an example of how to get this done.
The first number that is considered perfect is 6. This is because 1, 2, and 3 are
the proper divisors of it and when you add those together, they will equal six.
You will find that it is also half of the sum of all the positive divisors as well.
this means that if you add the one, two, three, and six together, and then lave
the equation, you will get six. The next one would be 28, and then 496 and
8128.

Let’s take a moment to look at how you would solve this problem and make
it all fit in the way that we need:

Answer

def perfect_number(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
return sum == n
print(perfect_number(6))
Exercise 8:

The next thing that we want to spend some time on here is writing out a good
function that can help us see whether the string that we want to pass is a
palindrome or something else. To help us out with this one, remember that a
what this word palindrome really is. It is basically a phrase that we can use
that, whether we say it the right way or we say it backwards, it will mean the
same thing. A good way to see what this is to look at the following sentence:
nurses run or madam.

Answer

def isPalindrome(string):
left_pos = 0
right_pos = len(string) - 1
while right_pos >= left_pos:
if not string[left_pos] == string[right_pos]:
return False
left_pos += 1
right_pos -= 1
return True
print(isPalindrome('aza'))

Exercise 9

We just took a look at how to make a palindrome, so now let’s look at how
we can go through and check out whether the string we present to the code is
a pangram or not. To help us out with this, we need to know that the pangram
will be sentences or words that will contain all of the letters of the alphabet,
at least one for each letter, but it can be more depending on what we are
working with. A good example of one of these can be “The quick brown fox
jumps over the lazy dog.”

This one takes a bit more work to accomplish, but you are likely to get some
good results if you are able to get it done. Make sure that the phrase that we
have above is included in this to ensure that you can get it done, or the
program will not work with what you want.

Exercise 10:
The final exercise that we will do here is looking to write out a function
program in Python that will take in and work with the sequences of words
separated out by a hyphen as part of the input and then will take those
sequences and prints them out after they are sorted out alphabetically. This
one will be a few steps so be prepared to take those in as you do some of the
work. But for this one, we may put in the words green-red-yellow-black-
white and then we want to get the results of black-green-red-white-yellow
back.

Answer

items=[n for n in input().split('-')]


items.sort()
print('-'.join(items))

There are a lot of times when you will need to work with these functions, and
getting them put together and ready to go can be really great for the programs
that you want to write. When you are ready to work with functions, take
some time to handle these exercises and get some practice to see how great
they can be.
Chapter 3: Conditional Statements and Loops
Now we need to spend some time reviewing how these conditional
statements work and what we can do with them to make sure our codes
behave well and do what we would like. There are so many fun programs that
we can work on that have conditional statements and ones that have loops,
and if you can get this basic part down, you can already write some pretty
impressive codes. We will talk quick about some of the different conditional
statements, and then the types of loops, before diving in and looking at a few
of the exercises that you can do along the way.

The Conditional Statements


There are three main types of conditional statements we can work on,
depending on what we like with our own codes and what we want to see
these codes do. The three main types are the if statement, then we have the if
else statement, and then we can move on to the elif statement. Knowing how
these work will help us to create some great codes.

We can start out with the if statements first. These are considered some of the
most basic out of our three options and will give us some experience with
how these conditional statements can come into play. This is one set up for us
to only have the program work if the answer provides it with the right
answer. If the user adds their answer into this, and based on the conditions
that you put in place it is not seen as true, then nothing will happen in the
program. On the other side, if the answer is seen as true based on those
conditions, then there will be a message or something else that shows up on
the screen for you.

This can bring in a few issues as we go through it and there aren’t many
times that this will be enough for you to see some results. A better option,
which will make sure that your program shows up an answer of some sort
through this, is to work with the if else statement. With this one, no matter
what answer your user puts into the system, there will be a message that
shows up on the screen.

Let’s say that we want to work with for this is to set up the program so that
when the user gets to a certain part, they need to input their own age. If their
age is above 18, then they can go in and see a website that teaches them about
local voting laws and where they can vote. If their age is under 18, we want
to set up the program so that it tells the user they are not eligible to vote right
now. The if else statement can handle all this for us. The code that we can use
to make this one behave includes:

age = int(input(“Enter your age:”))


if (age <=18):
print(“You are not eligible for voting, try next election!”)
else
print(“Congratulations! You are eligible to vote. Check out your
local polling station to find out more information!)
print(“Program ends”)

The final type of conditional statement that you want to work with is the elif
statement. This is similar to some of the other options that we talked about,
but can be more of a menu type option that allows the user to choose a few
options, rather than just inputting it in themselves. You may see that this is
similar to a menu that you would see in a game sometimes and it is easier to
work on than it may seem. You can add in as many choices to this menu as
you would like based on what your needs are here.

The Loops
Now it is time for us to take a look at the loops and how we can use these.
There are a number of loops and they will all handle the conditions we have
set but these loops can help us to cut out some of the work we need to do. If
you want to write out a code that must repeat itself, then these loops will
quickly become your best friend, allowing you to write a ton of code in just a
few lines.

First is the while loop. This is a loop that you can choose when you already
know how many times the loop will cycle through. You would use it to finish
counting up to ten, making sure that you set a condition at the end to help
make sure that the code will stop right on target without getting stuck in an
infinite loop.
Then there is the for loop. This is the loop that will have you put in the
information rather than the user. This will go through an iteration of words or
objects, based on how you set them up and line them in the code. There isn’t
really a need for this input from the user because it will continue through the
iterations until you are all done with it as well.

We can then finish off with the nesting loop. This is a fun loop that allows
you to put two codes together and have them run together to make the code
work the way that you want. Both of the loops will run at the same time, and
both will continue until they complete their iterations. This can be useful in
something like a multiplication table.

Exercise 1:

The first thing we will look at is writing a program in Python that is able to
guess a number. We will just work with the numbers one to nine to make this
simple. When we write this, the user will be the one prompted to put in their
guess. If the user guesses wrongly, then the prompt will continue to appear
until the user can get the right guess. When the user gets their guess right, it
needs to be set up so that they get the message of “Well guessed!” before the
program is done and exits.

Answer

import random
target_num, guess_num = random.randint(1, 10), 0
while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until you get it
right : '))
print('Well guessed!')

Exercise 2:

Now we can work on one of the loops that we want to write. Our goal is to
make up a pattern and then work with the nested loop to make sure that we
can create this pattern. The pattern we need to use for this one includes:

*
**
***
****
*****
****
***
**

Answer

n=5;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')

for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')
Exercise 3:

We can then move on to working with a program in Python that will take and
iterate all of the integers that go from one to fifty. When this program reaches
a multiple of three, it will print out the word of Fizz rather than the number.
And when it gets to a multiple of five instead of writing out that number, it
will print out Buzz. Then, if there is a multiple that does both, such as 30, it
will print out FizzBuzz. Take a moment to try writing out this program before
you look at the answer below:

Answer

for fizzbuzz in range(51):


if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0:
print("fizzbuzz")
continue
elif fizzbuzz % 3 == 0:
print("fizz")
continue
elif fizzbuzz % 5 == 0:
print("buzz")
continue
print(fizzbuzz)

Exercise 5:

We now need to move on to another program that we can write out in Python.
This is a program that can accept a sequence of lines, we want them to be
blank lines so that we can terminate them, as our input. And then we want to
make sure that we can print out those lines as our output. All of the characters
in this one will need to be in lowercase letters to make it easier and maybe
put in a little less work in the process. The steps that we can use for this one
include:

Answer

lines = []
while True:
l = input()
if l:
lines.append(l.upper())
else:
break;

for l in lines:
print(l)

There are many times when you can bring conditional statements and loops
into your codes and that can make them a lot stronger than before. Make sure
to try out a few of these exercises to see just how great working with Python
can be, and how even some of the basic parts will help you write some
powerful codes.
Chapter 4: Different Data Types and How to Use
Them
This is a fun part of the process that will show us how to work on a lot of
different things at once. We will look at the four most common types of data
that fit into this and will use those as a way to ensure we can really get them
down and learn how they are similar, and how they are different from one
another. Let’s explore more about how the array, the list, the dictionary, and
the tuple can work with one another here.
The Array
The first option on our list is the array. A Python array is responsible for
defining a type of object which can represent compactly on an array of basic
values. This can include options like the floating-point numbers, integers, and
characters. To keep it simple, the arrays will be sequence types and they are
similar to what we see with lists. The difference is that the arrays will have
some constraints on it based on the type of objects it is able to store. With
that in mind, we will look at a few exercises that you can do with arrays in
Python.

Exercise 1:

First, we want to create a program in Python that is able to create an array.


We want to create it with five integers and then it displays the items in the
array. We also need to access the individual elements through those indexes
as well. try it out before using the answer below:

Answer

from array import *


array_num = array('i', [1,3,5,7,9])
for i in array_num:
print(i)
print("Access first three items individually")
print(array_num[0])
print(array_num[1])
print(array_num[2])
Exercise 2:

Now we need to work on another program that works well with Python
arrays. In this one, we want to write out a program that helps us to get the
current address of our memory and also the lengths of the elements of that
bugger that we will use at some point to hold onto the contents of the array.
In addition to this, we want to make sure that we can find the size of the
bugger of our memory, doing it in bytes to keep us on the same page. Use
some of the example that we had in the exercise above to help make sure that
you are doing this one well. that should give you some advice and guidance
to see the best out of this option.

Exercise 3:

In this one, we will make the assumption that we already have an array in
existence. We want to be able to take that original array and add in a new
item to it without losing information or having some problems along the way.
To make sure that this one works, we need to go through and come up with a
code that will allow us to insert that new item of our choice before the second
element in that original array.

Answer

from array import *


array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Insert new value 4 before 3:")
array_num.insert(1, 4)
print("New array: "+str(array_num))

Exercise 4:

Then we will take a look at the final option that we will explore when it
comes to arrays. This one will ask us to make some changes to the arrays that
we have as well, which will ensure that we can remove or insert things as
well. in this one, we want to write out a program in the Python language that
will help us to remove a specific item of our choice from the index we have
and we want to do this with the help of an array as well.
Answer

from array import *


array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Remove the third item form the array:")
array_num.pop(2)
print("New array: "+str(array_num))
The List
Now that we have had some time to look at how to work with the Python
array, it is time for us to take a look at a different type of data in the world of
Python. And that brings us to the list. The list is a bit different than some of
the other options but will ensure that we can really see some results and can
use the items that are present in that part of the code in a slightly different
way.

To keep it simple here, the list will basically just be an ordered and
changeable kind of collection in this language. It is different than some of the
other types of data in that you are able to change up the list and move things
around if you would like. We can differentiate the list from some of the other
types of data based on the fact that these lists will come with some square
brackets. These are a bit easier to work with since you can move things
around. Let’s look at a few examples of exercises that you can do with these
lists:

Exercise 1:

The first option that we will look at is going to be pretty simple to give us a
bit of practice. We simply want to go through and get the program we write
in Python to pick out the largest number that is on our list. Give this one a try
and see how it works before looking to the answer for help:

Answer

from array import *


array_num = array('i', [1,3,5,7,9])
for i in array_num:
print(i)
print("Access first three items individually")
print(array_num[0])
print(array_num[1])
print(array_num[2])

Exercise 2:

For this one, we want to take the time to write out a program in Python that is
able to count out how many strings there are, as long as the length of the
string is two or more, and when the last and the first character will come from
the same part of a given list of strings. This adds in a few more parameters,
but can really show us how to make this work with the help of our lists as
well.

Answer:

def match_words(words):
ctr = 0

for word in words:


if len(word) > 1 and word[0] == word[-1]:
ctr += 1
return ctr

print(match_words(['abc', 'xyz', 'aba', '1221']))

Exercise 3:

In this one, we will assume that we have a list and we want to check on it to
see what is there and what is not. We will assume that we have this list, but
we are not certain whether there is actually something on the list or if it is
empty. Take the time to try this out and see what results you can get.

Answer

l = []
if not l:
print("List is empty")

The Dictionary
Now we need to move on a bit more to the dictionary, and how we are able to
utilize this to help us get the best results along the way. there are a lot of great
options that we are able to pick out when we work with the dictionary, and
we just need to make sure that we understand how it works and what we can
actually do with it along the way.

Before we go into some of the activities that we can do with these


dictionaries, we need to learn more about what this is. At first glance, this
appears to be another type of list that we can work with, but we need to see
how it can be a little bit different. To start, the dictionary will have a key that
helps us to find and use the various objects that are in the dictionary. Each of
these keys will be separated out from its value with the colon, the items will
then be separated out with some commas if you want to search for more than
one thing at a time, and then you can enclose all of it with some curly braces.
If you have an empty dictionary that doesn’t have any items in it at all, you
would just use two curly braces with nothing between them.

The keys that you have in the dictionary are unique, though it is possible that
the individual values will not be unique. The values that you add to these
dictionaries will be any type that you would like, which will be nice and
make this easier, but they need to be an immutable data type. This means
they need to be tuples, numbers, or strings for this to work. With this in mind,
let’s take a look at some of the activities and exercises we can use to help us
work more with dictionaries:

Exercise 1:

For this one, we want to make the assumption that we already have a
dictionary created, and that there are already values inside of it that we can
look through. We now want to go through this process and write out a script
in Python that can sort out the values of the dictionary. We want to do this in
ascending to descending order so we can look at them well. try out this
program before looking at the answer we provide for it below:
Exercise 2:

In this one, we want to use that same dictionary that we already have, and
then see whether there is a key that already goes with that dictionary. There
can only be one key for each value out there, and the keys have to be unique
or this will not work. Checking whether the key has been used or not will
make it easier to avoid errors. If that key is already a part of this dictionary,
then you just need to avoid it and try something else to work for the key
instead.

Answer

d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)

Exercise 3:

Now we will make another assumption. We will say that we have two
separate dictionaries, but they have a few parts that are similar. We are
working with a project that needs to rely on both dictionaries, but we don’t
want to jump back and forth and hope we don’t miss something along the
way. We have the option here to merge the two dictionaries together and
make them into one, and that is what we need to do in this part of the process:

Answer

d1 = {'a': 100, 'b': 200}


d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)

Exercise 4

In this option, we want to do something similar to what we listed out before,


but we will make some changes to it to see what will happen. In this one, we
want to make two lists, which are not dictionaries yet, and then combine them
together to make a brand new dictionary that we can use. And we want to do
this without any duplicate values showing up in the dictionary, regardless of
what our lists have in them. So, even if they do have some duplicates in them,
we want to make sure that those only show up once in our new dictionary:

Answer

d1 = {'a': 100, 'b': 200}


d2 = {'x': 300, 'y': 200}
d = d1.copy()
d.update(d2)
print(d)
The Tuple
The final type of data that we will need to spend our time here is the tuple.
This is a bit different compared to some of the other options, though we may
think at first glance that they are exactly the same. knowing how the tuple
varies from the dictionary, list, and array that we work with can make a world
of difference in how successful we can be with writing our codes.

First, we need to look at what a tuple is all about, and then we can dive into a
few of the exercises that you can do with them. First, the tuple is a collection
that is ordered, just like we see with the list. But, the objects that are in the
tuple are unchangeable. This means that once you put them into the code, you
can’t change them around, which is what makes them different from the lists
we talked about before. When we write out a tuple, remember that we will
write them with some round brackets to show how they are different from the
other data types we have discussed.

Exercise 1:

For our first exercise, we need to take some time to look at how to create our
own data type using the tuple. Remember that this means the objects that we
have in the list will be ordered, but they will not be something that we can
change later on. This will be a simple tuple so see if you can remember how
to write one of these before looking at the answer below:

Answer
#Create an empty tuple
x = ()
print(x)
#Create an empty tuple with tuple() function built-in Python
tuplex = tuple()
print(tuplex)

Exercise 2:

Then it is time for us to do a bit of work inside of the tuple and see what we
can accomplish with all of this. We want to first see whether we are able to
take one of the items that are in the tuple, and then remove them. This takes a
few extra steps than we may see with some of the other work that we want to
accomplish here, but it is still important for us to look at. Take some time
here to give it a try and see what answer you can come up with. Remember to
add in a few comments along the way to make it easier for us to understand
what is going on, and even to help you remember what is happening as you
go through this code as well.

Exercise 3:

With that done, we now want to look at how we can change from one type of
data over to another one. We will make the assumption that we already have
a tuple that we can work with, and we want to convert it over to a dictionary
as we talked about before. This does take a few steps, but it is similar to how
we talked about making a list and moving it over to the dictionary earlier so
you can use that for some guidance as you go through this.

Answer

#create a tuple
tuplex = ((2, "w"),(3, "r"))
print(dict((y, x) for x, y in tuplex))

The data types will be so important to a lot of your codes, and using them in
Python can really help you to write some of the codes that you would like
through this process. The four main types that we spent some time on in this
chapter were the array, list, dictionary, and the tuple, and all of them will
work with one another in a slightly different way. This can help us to make
sure that we handle the objects that we need in our codes.

We need to spend some time learning more about which type will work for
the types of ordering that we want. Some will allow for changes in the order
and some will not so that is an important thing to consider before you get
started on this as well. Try out a few of the activities that we talked about
above and then see how well you can start to understand how these processes
work and what we can do with each type of data.
Chapter 5: Your Operators
The next thing that we need to take a look at through here is some of our
operators. This will help us to go through and make sure that we are able to
get the most out of our process and can ensure that we can really see some
good results with the codes that we want to write as well. we will spend a bit
of time in this chapter looks at how to handle these as we go through the
process. Let’s start out by learning a bit about how to work on these
operators, and then we can move on to some of the exercises that we can do
with these as well.

The Different Types of Operators


The first operator type we are going to look at is the arithmetic operator. If
you think back to your high school math class, you will know what most of
these operators are like. These are the ones that are responsible for helping
you complete mathematical equations. It can be as simple as adding together
two of your operands in the code or subtracting two or three numbers from
each other.

Then we can move on to the comparison operators, which will take a


different option that can handle our codes, but they are so useful in all of this
as well. You can also work with your comparison operator when you create
your codes. This is a helpful option to work with because it allows you to
take two (and sometimes more0, values, and statements in your code and
compare them to each other. You will often need to bring out the Boolean
expressions for this because the answer you will get is either true or false. So,
your statements will either be the same as each other or not.

Next on the list is the logical operators. These may not be used as much as
the other two options, but it is still important to spend some time on them.
These operators are the ones that you will use to help evaluate the input that
the user gives you with any of the conditions that you set in your code. When
we work with some kind of code that needs to evaluate what is going on
along the way, then we want to bring out the logical operators.

And the last operator we are going to take a look at is the assignment
operator. This is the one that you have seen quite a bit in this guidebook
because you simply need to use the equal sign to take one of your values and
assign it to a variable. So, if you are looking to take the value of 100 and then
assign them to the chosen variable, then you just need to put the equal sign
between them. This is done with a lot of different values and variables in
your code and you should be pretty used to doing it at this point.

It is also possible to take several values and assign them to the same variable
if you choose. As long as you continue to use the equal sign, you can add as
many values to the same variable as you would like.

Working with these operators can be really simple, but they do make a big
difference when it comes to working on your code. You can add together
variables or choose some of the other mathematical operators. You can assign
a value over to your chosen variable, or even a few values to the same
variable. And you can even use these operators to compare two parts of the
code together and see if they are the same or not. There is just so much that
you can do with these operators and it is important to be on the lookout for
each one.

Even though all of these operators are slightly different from one another,
they are going to all come in and bring us some good codes. They are also
really simple to add to any code that you need without a lot of hassle, and as
we go through some of the activities that we need to do in this guidebook,
you will find that it can actually be pretty easy to work with them as well.

The Exercises with Operators


These operators are usually pretty simple to work with so we will walk
through the different types and see how they are done. First, we will look at
the arithmetic operators. These are similar to what we would see when using
other coding languages and you can divide, multiply, subtract, and addition.
Try writing out a few of these operators, and then look at the example below.

number = 1 + 2 * 3 / 4.0
print(number)

Can you predict what the answer will be? If you run this program and it is
different than your prediction, consider whether you used the order of
operations and see if that gives you the right answer. Keep in mind that there
is one thing that is a bit different from this. If you work with two
multiplication symbols right next to each other, such as 2**2, then this is
going to be the power relationship instead.

Then we can move on to working with operators inside of some of the strings
we have. This is pretty easy to do. We will first look at how we can make one
of these strings with the help of the addition operator, and how that will fit
into this as well:

helloworld = "hello" + " " + "world"


print(helloworld)

There are even more fun things that we can do as we work through this. It is
possible to use these operators to help us out with some of the lists that we
want to create. These lists are simple enough to work with, and we talked
about them a bit earlier so we won’t spend too much time on them right now.
However, we can use the operators to help us join together two or more lists
if that is what we need to get done with our code. This is really simple to do,
and we can use the examples below to help us:

even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)

To finish this chapter off, we need to take a look at an actual exercise that we
can work with to help us understand how all of this is supposed to work. The
target of working on this is so that we can come up with two new lists as we
go through the whole process. We will have a y_list through this and then an
x_list as well, and then have ten instances of the x in the first one ad ten of y
in the second one. You will then need to take this further and create another
list known as the big_list, which will have the variables of x and y, both
showing up ten times, by concatenating the two lists that we already created.
Take a moment to try that out.

There are a lot of times when we will need to bring out these operators and
see how they work. And there are so many different types that we can use
along the way as well. make sure to try out a few of these exercises to see
how they can work for our needs and get more familiar with the process as
well.
Chapter 6: Classes in Python
We won’t be able to get far with our work in Python without looking at the
classes and what they are all about. The main idea that we will see with
Python, and other languages like it, is that this is a coding language that is
based on classes and objects. The way that these work with one another will
help our code to be as organized and ready to go as possible.

There is so much that we can talk about with these classes, but we will try to
keep it to the basics before moving onto some of the different exercises that
we can do along with these to see some great results. Remember that classes
will be like the containers that you use in your code, the ones that will hold
onto all of your objects. When these are in the right places, they can make
sure that all of your code stays organized and that you see some really good
results in the process.

Then we have the objects here as well. These objects can match back to all of
the different choices that we have in our physical world, so we can view them
like this. This makes them a little bit easier to work with compared to some
of the other, and older, coding languages that you may be used to working
with. This will help you to keep things organized.

The objects that you create need to go into the classes that you have as well.
this will make it easier to organize because all of the objects that end up in
the same class need to be similar in some manner. They don’t have to be
exactly the same, but we want to make sure that we can look in that class and
recognize why all the parts are in that same class.

You can store anything that you want inside a class that you design, but you
must ensure that things that are similar end up in the same class. The items
don’t have to be identical to each other, but when someone takes a look at the
class that you worked on, they need to be able to see that those objects belong
together and make sense to be together.

For example, you don’t have to just put cars into the same class, but you
could have different vehicles in the same class. You could have items that are
considered food. You can even have items that are all the same color. You
get some freedom when creating the classes and storing objects in those
classes, but when another programmer looks at the code, they should be able
to figure out what the objects inside that class are about and those objects
should share something in common.

Classes are very important when it comes to writing out your code. These are
going to hold onto the various objects that you write in the code and can
ensure that everything is stored properly. They will also make it easier for
you to call out the different parts of your code when you need them for
execution.
There can also be some subclasses and other parts that show up in this that
will help us to make sure we get the best results while going along here. This
may sound kind of complex, but as we looked at in the other guidebook, they
can be fairly simple to put together, especially when we consider how much
stuff we try to put into them. You will see as we go through some of the
exercises below that these can be fun to work with, and that there are a ton of
ways that we can create the classes that we want.

Now it is time for us to take a look at the different exercises that you can do
when it comes to the classes in Python.

Exercise 1:

In this first exercise, we want to start by making one of our own classes. For
this, we want to be able to take an integer and convert it over into a Roman
numeral to see what will happen. Take a moment to try this one out first and
then look at the example below:

Answer

class py_solution:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i=0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num

print(py_solution().int_to_Roman(1))
print(py_solution().int_to_Roman(4000))

Exercise 2:

Now that we have had a chance to write out our first class, it is time for us to
look at how to work with something else and make a different class. With this
one, we need to write out a class in Python that will help us to find the
validity of a few brackets and parentheses. We are going to check for all three
types of brackets to see how they work. Using some of the examples that we
did before in the last exercise, you should be able to go through here and
work on the coding. See if you can complete this and get some good results
on your own as well.

Exercise 3:

Next, we move on to another type of Python class that we can work on too.
This one will help us to find the elements, in a pair, that are in our given
array. We are going to do this to find the indices out of two particular
numbers we choose and we want their sum to equal the target number that we
pick out. The code we can use to make this one happen includes:

class py_solution:
def twoSum(self, nums, target):
lookup = {}
for i, num in enumerate(nums):
if target - num in lookup:
return (lookup[target - num], i )
lookup[num] = i
print("index1=%d, index2=%d" %
py_solution().twoSum((10,20,10,40,50,60,70),50))

Exercise 4:

The last exercise that we will take a look at here is how to write out a class in
Python that will help us find three elements. What is unique about this is that
they will make the sum tat you want to work with be zero, as long as it is
from the real numbers of n. This one will come in with a few more parts than
we will see with some of the other options, but many classes that we want to
create will be similar in nature. Make sure to give this one a try and see what
answers you are able to get out of it along the way.

The classes are a bit longer than some of the other codes that we have worked
with, but this is because there is so much information that we are trying to put
into them. Take some time to practice how these works and what we can do
with them to make these work out the way that is best for us.
Chapter 7: How to Make Your Own Inheritance
Another option that we need to take a bit of time to look at is creating our
own inheritances. These combine together some of the other topics that we
have explored through this guidebook, and that can help us to practice some
of the classes and other parts that we need to accomplish along the way. They
are a bit more complicated than some of the other things we have discussed,
but as we talk about them, and look at the steps that we want to use for this
one to see if we can make sure it happens, you will find that you already
know quite a few parts that go with this.

There are many times when working on inheritances that adding them into
the code can really enhance what your goals are in the program. These
inheritances will save the programmer a lot of time while helping to clean up
your code and have it look nicer in the long run. When they are used well,
they can help you to take some of the codes you already wrote and then reuse
them, without the added work of rewriting the same code each and every
time. Inheritances are a unique thing that comes up with OOP languages
(which we know Python is one because we studied the classes before), so we
may as well use them.

To keep it simple, when we want to work on these inheritances, our goal is to


take that original code, which is known as the parent code in all of this, and
then we want to change up a few parts of it to get our child code, which is
known as the derived code in Python. You can reuse the parts that are the
most important to what you are doing, and get rid of what you don’t want,
without having to rewrite it all from scratch each time.

Even when you just start out with this kind of coding, you will see that the
inheritance is helpful at keeping things organized and that even with all of
our work we won’t have to deal with a big mess. Plus, who doesn’t want to
save some time when writing codes? You can write as many of these
inheritances as you would like, and if your code needs it, it is also possible to
keep writing the code as an inheritance down the line.

So, to help us out with this, when we create an inheritance, you would take
the original parent code, and then you need to copy it over to another part of
the program. This new code you are working with here will become the child
code, and you can make any changes that you would like to this one, and it
will have no effect on the parent code you have in place. Sometimes, you will
go through this and copy it down one time, but you also have the option to do
this many times with the process being the same each time.

You can have a whole string of this setup and ready to go if you choose. That
is part of the benefit of working with these in the first place. They are all set
up to help us to learn how to make as many child codes as we want, and
change up each one as much as we want. And none of those changes will
cause something different to happen inside the code above it. This gives you
a lot of freedom when writing out some of the codes.

An Example of Using an Inheritance


With that introduction in mind, we need to now take a look at how we can
actually write the inheritance. This will give us a better idea of what we are
talking about with these and can make it easier for us to work on the overall
as well. A good example of an inheritance that you can create includes:

#Example of inheritance
#base class
class Student(object):
def__init__(self, name, rollno):
self.name = name
self.rollno = rollno
#Graduate class inherits or derived from Student class
class GraduateStudent(Student):
def__init__(self, name, rollno, graduate):
Student__init__(self, name, rollno)
self.graduate = graduate

def DisplayGraduateStudent(self):
print”Student Name:”, self.name)
print(“Student Rollno:”, self.rollno)
print(“Study Group:”, self.graduate)

#Post Graduate class inherits from Student class


class PostGraduate(Student):
def__init__(self, name, rollno, postgrad):
Student__init__(self, name, rollno)
self.postgrad = postgrad

def DisplayPostGraduateStudent(self):
print(“Student Name:”, self.name)
print(“Student Rollno:”, self.rollno)
print(“Study Group:”, self.postgrad)

#instantiate from Graduate and PostGraduate classes


objGradStudent = GraduateStudent(“Mainu”, 1, “MS-
Mathematics”)
objPostGradStudent = PostGraduate(“Shainu”, 2, “MS-CS”)
objPostGradStudent.DisplayPostGraduateStudent()

You can type this into the compiler and see what results come out. Take some
time to look and see where the parent class is and where some of the
children's classes are along the way because this will help us to explore more
about how these works. You can definitely go through this process more
times, adding more and more of them down the line, but this is a good way
that you can get it all started and can ensure that you see the best way it all
should come together.
Chapter 8: Creating Exceptions
The next topic that we need to spend some time here is the exceptions and
how they can come into our codes. As you write out some of your own codes,
there are some situations where you will need to look for the raised
exception. You may create one on your own sometimes, but there are also
some that the Python code will raise all on its own and we need to pay
attention to some of those as well. Learning how this works is so important
along the way and we need to look at how to do this with some examples and
exercises as well.

There are several scenarios where these exceptions will show up in our codes,
and there are also times when they appear to be an error message, something
you have probably seen show up during programs you use online. You need
to know how these work as you go through your own program writing to
make sure that they will get solved and your program will work as well as
possible. Don’t be scared about these; they are there to help you write some
codes that will work really well.

Now, you may notice there are a few exceptions that are automatically found
in the Python library. If you try to do some coding and add them in, then the
user comes in and does something that would raise the exception, then these
are found in the Python library. This could be something like the user trying
to divide by zero. The Python library already knows that this is not something
that is allowed. There are also some circumstances where you would create
the exception on your own, based on the game or something else you are
working on, and that takes a few more steps to accomplish.

As we mentioned, there are a few exceptions that we can find automatically


in our library. It is a good idea to learn what these are and to look through
them so you can recognize them a bit better and understand what they are
when they show up. Some of the exceptions that are commonly found
through the Python library will include:

Finally—this is the action that you will want to use to perform


cleanup actions, whether the exceptions occur or not.
Assert—this condition is going to trigger the exception inside of
the code
Raise—the raise command is going to trigger an exception
manually inside of the code.
Try/except—this is when you want to try out a block of code and
then it is recovered thanks to the exceptions that either you or the
Python code raised.

Raising an Exception
Now that we have a bit of that background all set up, it is time for us to go
through and actually raise one of these exceptions. That is one of the best
ways to really understand how this works and what we are able to do with
them in the first place. We need to focus on what one looks like, and what the
steps are for us to take when it is time to raise one in our codes. Often the
issue that these exceptions will run across will be about something simple,
though there are times when the situation is more complex.

Before we take a look at an example, try to think of a time when these


exceptions will show up in your compiler and how you would handle them.
Think about drawing one or creating the code for one to use on your own.
When that is done, it is time to take a look at the example of raising our own
exceptions below:

x = 10
y = 10
result = x/y #trying to divide by zero
print(result)

The output that you are going to get when you try to get the interpreter to go
through this code would be:

>>>
Traceback (most recent call last):
File “D: \Python34\tt.py”, line 3, in <module>
result = x/y
ZeroDivisionError: division by zero
>>>
We then need to look a bit more at this example to see what is going on with
it. This is going to bring us an exception because we are trying to divide by
zero in our program, and that is not possible. Python will not let us try this at
all. If you do not move things around and you continue to get the program to
work well, and that is going to bring up an error message for this point. The
code will let the user know what the problem is, but it is unlikely that the user
will have any idea what this problem really means in its current form. It is
kind of a mess. The good news is that we can make some changes to the code
that we did above in order to have a message that makes more sense to our
users.

With the example above, you will want to add in a message that tells the user
why an exception has been raised. This message can help them figure out
what they did wrong, and won’t include that messy string of numbers and
letters that no one can understand. An example of how you would change the
above code to include a message instead when this exception is raised
includes:

x = 10
y=0
result = 0
try:
result = x/y
print(result)
except ZeroDivisionError:
print(“You are trying to divide by zero.”)

We can now take a look at the codes that are above. The second one is similar
to what we saw with the first one, but it will have a message inside that the
user can understand. This method is a good one to work with because it will
prove easier compared to all of the other options like the one we had above it.
It is important to add this in so your user, especially one who is not versed in
computer languages, can understand what they need to fix along the way.
An Exercise in Defining Your Exceptions
For the first exception, we took some time to look at an exception that was
defined already through the program. But now we need to take this to the
next level and learn how to make some of our own exceptions along the way.
let’s say that you spend some time creating your own code and you want to
make sure that the user can only work with certain numbers at a time, or you
only want a few select actions to be possible in all of this. Then you can use
the same ideas that we talked about before to help you make these
exceptions.

When we spend some time looking at these specific exceptions, the compiler
isn’t going to be the part that brings these up, and it may not recognize that
there is something wrong with what the user is doing. Technically, without
you manually adding this exception in, the code would work just fine with
the user picking that as the answer. But with the exception that you put in,
you will be able to change it up and make this work so the program will stop
and not allow an action to happen. You can even set it up so that the
exception can only allow the user to guess a certain number of times.

Working with these exceptions will be a bit different than what we see with
some of the others we have explored throughout the chapter. This is because
you are creating something completely unique for your program. Take a
moment to consider how you would write out a code that has this kind of
exception in it. Take some time to practice and write it out. You will find that
it is similar to what we were doing with some of the other exceptions above,
but you are teaching Python how to make this custom exception, rather than
using one that is found in the library already.

In this code, you have been successful in setting up your own exceptions and
whenever the user raises one of these exceptions, the message of “Caught:
This is a CustomError!” will be the message that shows up on your screen or
the screen of your user. This is the best way to show your users that you have
added in a customer exception into the program, especially if this is just one
that you personally created for this part of the code, and not one that the
compiler is going to recognize on its own.

Just like with the other examples that we went through, we worked with some
generic wording just to show how exceptions are able to work. You can
easily go through and change this up so that you get a message that is unique
for the code that you are writing and will explain to the user what is going on
when they get the error message to show up.
Working with these exceptions can be a great experience, and it is worth your
time to learn how to handle the ones that will show up within your code, and
even write a few of the ones that you want for your own codes. They take a
bit of time to get used to along the way, but they do make a big difference in
how well your code can work overall.
Chapter 9: The Regular Expressions
We can also spend a little bit of time looking at regular expressions and how
these can fit into the mix of what we are doing here. One thing that a
beginner can even appreciate when it comes to Python is that there is a really
great library, full of lots of functions and more, that you can use. And inside
this library, there are many expressions that will handle the tasks that you
want in your code. These are known as regular expressions.

These are simple enough to work with, and you will catch on to some of the
general ideas as we go through the exercises in this chapter to help us. You
can use the same kind of syntax for this, not only in Python but with some of
the other coding languages that you want to bring out, which can make these
even better to work with along the way. you just need to get some practice in
Python, which we will do here in a moment.

At this point, you may be curious as to what these regular expressions are all
about and how you will be able to use them in your own code. One method
that you can use to explore regular expressions is to do a search for a word
that you spelled differently in your text editor. Maybe you were working on
your code and in one place you wrote out “blue” and in another place, you
may have typed out “bleu”. You may want to go through and change one of
these to make them align up correctly and working with the right regular
expression can make this possible.

Now that we have that little review in mind, it is time for us to dive into a few
more things to help us write our codes, and then it is time to get some
practice with these as well!
Basic Patterns to Remember
Before we jump into some of the exercises that we will do with these regular
expressions, there are a few basic patterns to remember. These will help us to
make some more sense of what we are doing throughout these exercises and
can give you some guidance as you go through and then you will need to do
some writing to make the codes that are best for you to work with. The best
basic patterns that you can enjoy include:

1. Ordinary characters. These are characters that will match


themselves exactly. Be careful with using some of these because
they do have special meanings inside of Python. The characters
that you will need to watch out for include [], *, ^, $
2. The period—this is going to match any single except the new line
symbol of ‘\n’
3. 3. \w—this is the lowercase w that is going to match the “word”
character. This can be a letter, a digit, or an underbar. Keep in
mind that this is the mnemonic and that it is going to match a
single word character rather than the whole word.
4. \b—This is the boundary between a non-word and a word
5. \s—this is going to match a single white space character including
the form, form, tab, return, newline, and even space. If you do \S,
you are talking about any character that is not a white space.
6. ^ = start, $ = end—these are going to match to the end or the start
of your string.
7. \t, \n, \r—these are going to stand for tab, newline, and return
8. \d—this is the decimal digit for all numbers between 0 and 9.
Some of the older regex utilities will not support this so be careful
when using it
9. \ --this is going to inhibit how special the character is. If you use
this if you are uncertain about whether the character has some
special meaning or not to ensure that it is treated just like another
character.

Look these over a bit and review, and then it is time to jump right into some
of the exercises that we can do with regular expressions:

The Exercises
Now it is time for us to dive into some of the exercises that we can do when
we want to handle a few of our regular expressions. These are simple to work
on, though we will spend some time on options that are a bit more
complicated as we go down the line as well. some of the practice that you can
do when it is time to handle these regular expressions include:

Exercise: 1

For our first example, we want to work on writing out a program in Python
that will check that the string we have in front of us will only have a certain
set of characters. You can technically go through and add in any set of
characters that you want, but for our needs, we are going to use a-z, A-Z, and
0-9. Take some time to write out this one before you look at the answer
below:

Answer

import re
def is_allowed_specific_char(string):
charRe = re.compile(r'[^a-zA-Z0-9.]')
string = charRe.search(string)
return not bool(string)

print(is_allowed_specific_char("ABCDEFabcdef123450"))
print(is_allowed_specific_char("*&%@#!}{"))

Exercise 2:

Then it is time for us to move on to another option to help us to go through


and get this done. We want to write out a program where we can look at an IP
address on a computer. But, to make this work, we want to ensure that it will
only write the main part of the IP address while getting rid of all the zeros
that are at the front of the address. This can make it easier to read, though it
may not be technically correct. Give this one a shot and see if you can guess
it right before looking at the answer:

Answer

import re
ip = "216.08.094.196"
string = re.sub('\.[0]*', '.', ip)
print(string)

Exercise 3

For this exercise, we would want to make a program that can search for some
literal strings that will ten show up in the string that we have. This could have
a lot of variations, but we will provide you with the answer that you need to
help out. Let’s say that your sample text says something like “The quick
brown fox jumps over the lazy dog.” You could then do a search for a few
words in order to find this one. The words that you would search for here
would be a fox, dog, and horse. Write out the coding that you need for this
one. Make sure that you add in the right sentence to make this one work the
way that it should as well.
Exercise 4:

In our next example, we want to work with some multiple delimiters. We


want to write out a program in Python that will be able to split up the string at
hand into some of these. If we are uncertain about what this means,
remember that the delimiter will be a sequence of at least one character
though it can be more, that is what we want to use to make sure there are
boundaries about the various regions that are specified out, ones that are
separate, in text that is all plane, or you can use some of the other streams of
data to make this happen. If this is confusing, you can look at the character of
the comma as an example that can work. This will be a type of delimiter in
the field that is separated out well with some of the commans at hand. Take a
moment to write out what you think will be in this code and then come back
and look at the answer:

Answer:

import re
text = 'The quick brown\nfox jumps*over the lazy dog.'
print(re.split('; |, |\*|\n',text))

Exercise 5:

For this final exercise, we are going to work on a program in Python that will
read out the expression that we want and then will do an evaluation on it. The
conditions of this one are that our expression will have some operators,
numerical values, and the parenthesis, and it will end with the equal sign. The
operators will include the addition, subtraction, multiplication, and division
symbols as well. When there are two operators in here that have the same
precedence on them, then we need to work on them going from left to right.

During this, we can make the assumption that we can’t divide by zero since
this is not something that Python allows in the first place. All of the
calculations done here will be done as integers, and after the decimal place,
we would truncate the length of the expression so it doesn’t go past 100. Take
some time to write this one out and then see what the answer is below:

Answer

#https://bit.ly/2lxQysi
import re
print("Input number of data sets:")
class c(int):
def __add__(self,n):
return c(int(self)+int(n))
def __sub__(self,n):
return c(int(self)-int(n))
def __mul__(self,n):
return c(int(self)*int(n))
def __truediv__(self,n):
return c(int(int(self)/int(n)))

for _ in range(int(input())):
print("Input an expression:")
print(eval(re.sub(r'(\d+)',r'c(\1)',input()[:-1])))

Working with these regular expressions can add so much to the codes that we
want to write. Take some time to go through all of these different codes to
see how they work and how we can put them together to get the best results.
They are simple to work with, despite being able to do so much, and you are
likely to find lots of ways to make these work for us.
Chapter 10: File Input and Output
The next topic that we can explore here is the file input and the file output.
When we are doing a variety of things with some of our codes in Python,
there will be instances where we are working on some data and we want to
store it away, making it easier to access that data later when we need it the
most. You can easily choose to save this data in a way that is easy to find it,
and then have it show up in your codes. This is all something that we do with
file input and file output in Python.

The data that we use will be saved on a disk as a type of file. It is also
possible to work with the inheritances that we brought up before and reuse it
over and over again as many times as we want. We just need to make sure
that we do this with the right steps along the way. This is an important thing
to learn about, but we will focus most of our attention on doing some
exercises to help us review it. If you need help remembering the basics of file
input and output, take some time to really look at these topics and how to
make them work.

There are a few things that you can work with when it is time to handle those
file modes in this language. To make this as easy to understand as possible,
you can think about how it is to work on a file in Microsoft Word. At some
point, you will need to stop the work you are doing in this file because you
need to walk away and do something else. You can easily save it, and that is
what we will do with our files in Python.

These files are similar in many ways, but instead of actually saving the pages
of work that we do, you will need to save the code that you want to write
along the way. This will work in a very similar method no matter which one
you use. There are a few operations that we are able to do with these files,
and some of the options include:

1. You can write out some new code of your choosing onto a file,
one that you did in the past.
2. You can close the files you work on
3. You can create a new file to work on
4. You can move or seek some of the files and put them into a new
location if needed.

All of these are important functions when you work with your files and we
need to spend some time exploring them and how we can really make these
work in some of our codes. With this in mind, let’s dive in and learn a bit
more about the exercises we can do with file input and file output in Python.

The Exercises
Now it is time for us to have some fun with the different exercises that we
can do when it comes to the file input and the file output in Python. There are
so many things that we can do when it comes to working on these files, that
we will really want to make sure that we try out a bunch of them to see what
is available. This will take some time to go through, but let’s look at a few of
the options.

Exercise 1:

The first exercise we will take a look at is how to write out a program in
Python to help us read the text that is in a file. This will not matter whether
the file is big or small, the text is set up to read through it all anyway. Try
writing this out a little bit, and then look at the answer below:

Answer

def file_read(fname):
txt = open(fname)
print(txt.read())

file_read('test.txt')

Exercise 2:

When that is done, we can move on to the second exercise that we want to
complete. This is a bit different, but it will bring back in those lists that we
talked about earlier in this guidebook. For this activity, we want to write out a
program in Python and we want it to read through the file, going line by line.
Then, when that is done, we want it to store all of that information as a list.
Try writing out this code and then come back to see how it is done.
Answer

def file_read(fname):
with open(fname) as f:
#Content_list is the list that contains the read lines.
content_list = f.readlines()
print(content_list)

file_read(\'test.txt\')

Exercise 3:

When that one is done, we are going to work on a similar activity again so
you may want to reuse some of the code that we had before. With this one
though, we will avoid the list, and focus more on the variable. For this one,
we will take a look at writing out a program in Python and then we want it to
read through the specific file we have, going line by line, but we want it to
not store it as a list this time, but instead as a variable. Try this one out before
looking at the answer.

Answer

def file_read(fname):
with open (fname, "r") as myfile:
data=myfile.readlines()
print(data)
file_read('test.txt')

Exercise 4:

With this one, we are going to focus more on how long a particular file is, or
how big it is. This means we want to learn how many lines are present in the
document. It can give us the best idea of how much work is there since 100
lines are much smaller than seeing 10,000 lines for example. For this one,
take some time to see if you can write out a program in Python that will count
out how many lines are found in that text file. Try this one out before you
look at the answer below.

Answer
def file_lengthy(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
print("Number of lines in the file: ",file_lengthy("test.txt"))

Exercise 5:

This one is similar to the idea that we did above, but it can be a fun little idea
to just get some more practice with what we are doing here. This one will not
take time to look at how many lines inside of a particular file we will find.
Instead, it will focus more on what the longest word in the file is. Try to see if
you can work with the coding that we did above and write out the code that
you think it would take to make this happen before you look at the answer.

Answer

def longest_word(filename):
with open(filename, 'r') as infile:
words = infile.read().split()
max_len = len(max(words, key=len))
return [word for word in words if len(word) == max_len]

print(longest_word('test.txt'))

Exercise 6:

Now we are going to take a look at some of the steps that we need to take in
order to combine a few files together. Maybe we have two files that are really
similar, with a few updates in one or the other, and we want to combine the
information from one of the files into the next one. It is possible to do this
with a little bit of coding and some practice as well. For this exercise, we
want to take our programming skills and write out a program that can
combine each of the lines that are in the first file we have, with the
corresponding line in our second file to get something new. Try coding this
one out before looking at the answer below:

Answer
with open('abc.txt') as fh1, open('test.txt') as fh2:
for line1, line2 in zip(fh1, fh2):
# line1 from abc.txt, line2 from test.txtg
print(line1+line2)

Exercise 7:

The next one can be a lot of fun to work with as well. This one will require us
to take a file of text and have that as our input. Then we will ask it to go
through and return the number of words that are in that text file that we
chose. This can help us to see just how big the file is, or how many words we
are dealing with at the time. One thing to note before starting this is that it is
possible for some words to be spread out or separated with the use of a
comma and still have no space in them. Try writing this one out before
looking at the answer below:

Answer

with open('abc.txt') as fh1, open('test.txt') as fh2:


for line1, line2 in zip(fh1, fh2):
# line1 from abc.txt, line2 from test.txtg
print(line1+line2)

Exercise 8:

For this one, we are going to spend some time creating our own program in
Python that will help to extract characters from a variety of files that have
text in them. And then we will take those characters and have them all out in
a list so we can look them over a little bit better. The code that you can use
for this one is below:

Answer

import glob
char_list = []
files_list = glob.glob("*.txt")
for file_elem in files_list:
with open(file_elem, "r") as f:
char_list.append(f.read())
print(char_list)

Exercise 9:

This one will be a bit different and will require us to work a bit with naming
and with the alphabet to make it all work. We want to create a program in
Python that is able to generate out text files, 26 of them to be exact. We
would like them to be named in alphabetical order, going from A.txt, B.txt,
and so on down the line. Give this one a try to see what you can do and then
look at the answer below:

Answer

import string, os
if not os.path.exists("letters"):
os.makedirs("letters")
for letter in string.ascii_uppercase:
with open(letter + ".txt", "w") as f:
f.writelines(letter)

Exercise 10:

In this final exercise, we are going to do a bit more work with the English
alphabet to get some practice with the files that we have. For this exercise, we
want to write out a program in Python that will create a new file. This file
will take all of the letters in the alphabet, and then list them out by the
specified number of letters that are on the line. This can help us to keep
things in order and is a good way to get a bit of practice in for the day. Try
writing out the code before looking at the answer below:

Answer

import string
def letters_file_line(n):
with open("words1.txt", "w") as f:
alphabet = string.ascii_uppercase
letters = [alphabet[i:i + n] + "\n" for i in range(0, len(alphabet), n)]
f.writelines(letters)
letters_file_line(3)
These files are going to be an important part of the process as we go through
working on some of our own codes in Python. Being able to put them
together and understand how each part works will be critical to helping you
gain the success that you would like. Try out at least a few of these activities
and exercises to get more familiar with how these files work because it is
likely that you will need to use them in a lot of your programs along the way,
even as a beginner in Python.
Chapter 11: Other Projects to Try
We have spent some time in this guidebook looking at a lot of the different
parts that come along with writing our own codes and some great examples
of how you can make this happen. Even as a beginner, you can put some of
these different types of codes together and see how they work out well. Now
our goal is to take some of the ideas that we have explored throughout it and
combine them to write actual games and programs. Let’s dive in and see how
we can make this happen.

The Magic 8 Ball


There are a lot of projects that you can do when working on Python, and the
first one that we get to explore is the Magic 8 Ball. Remember to some of the
toys that you had as a kid, and one of those being a plastic ball that you
should ask a question, shake around, and then it would provide you with
some kind of answer to that question? We are going to make a program that
has this in mind.

This project will allow you to go through the program, as a question, and then
have it provide you with an answer of some sort. We will use the if and the
elif statements, along with the while statements to see how this will go. Both
the loops and the conditional statements are things that we practiced earlier so
that shouldn’t be a big surprise on how to get it all done. Take a moment to
give this project a try and see how you would write it out, using the operators,
the conditional statements, and the loops, to see if it will work.

The Code
Now that you have had a chance to at least give this code a try, it is time to
look at what the actual code is all about. The code that we need to use in
order to make this program our own includes:

# Import the modules


import sys
import random

ans = True
while ans:
question = raw_input(“Ask the magic 8 ball a question: (press
enter to quit)”)

answers = random.randint(1,8)

if question == “”
sys.exit()

elif answers ==1:


print(“It is certain”)

elif answers == 2:
print(“Outlook good”)

elif answers == 3:
print(“You may rely on it”)

elif answers == 4:
print(“Ask again later”)

elif answers == 5:
print(“Concentrate and ask again”)

elif answers == 6:
print(“Reply hazy, try again.”)

elif answers == 7:
print(“My reply is no”)

elif answers == 8:
print(“My sources say no”)

While there are a few other parts that show up in this code, you should be
able to recognize that there are a lot of important parts that you know how to
do. For example, the main part of this code is just a long elif statement that
we explored before, and we already know how to do this. We separated it out
into eight options for the elif statement since we are working with the Magic
8 ball, but you can change it up to have as many or as few of these options as
you would like as well. this is just a good one to practice with.

There are also a few other parts that show up in this kind of code, which can
make it a really fun one to spend our time on as well. Start out by practicing
this code and getting familiar with how it is supposed to work, and then you
can see what would happen if you tried to change up some of the other parts
as well. you can add in some more of the elif statements or take a few of
them out, change the messages, and more just to get familiar with how this
works.

The Hangman Game


Now we can take a look at the second project that we will spend some time
on in this guidebook, and we will learn how to work with the hangman game.
This is another good one to spend some time on because it will bring up a lot
of the different topics that we spent time discussing through this guidebook.
For example, we will bring in the comments so we know what is going on in
the code at different parts (which can even be useful for you as a beginner
who is trying to figure things out). We will find loops that will allow the user
a certain number of guesses before the game ends; we will allow for ten but
you can make changes to this as needed. Make sure that you don’t forget the
loop here because that condition needs to be in place or else the user will
have endless guesses and the game will get stuck.

That is just the start of what you will see inside this game as well. we will
also explore some of the conditional statements that show up. These are
important here because they are meant to help us figure out whether the user
came up with the right letter that fits into the random word that is chosen or if
not. If the user does guess the right letter, then the conditional statement will
see this, let the user know they are right, and then move it to the right spot. If
the user puts in a letter, on the other hand, that is seen as wrong, then the
conditional statement can see this as well.

The coding that we will use for this one is pretty simple to work with, and
you can really turn it into something of your own pretty quickly. Still, we
need to remember there are a lot of parts that will show up with this one, and
it does take a little bit of time and talent to get done along the way. think
about what needs to happen in this game, and walk through some of the steps
as they are needed. Try to write out at least part of this code ahead of time to
help you out, and then we can look at how to write this code as well.

As you are writing out this code, it is going to take some time to get it all
done. You will notice a bunch of the conditional statements that we were
talking about earlier finding their way in here. You should find some other
options as well, such as the values being assigned to a variable, lot s of the
operators showing up, and even some of the loops.

Once you are able to get the first part done, and figure out how to make sure
the program responds whether the user puts in the right letter or not, then you
are set to work even more on this and really look at a few different options
along the way. You can take that same idea and continue it on down the line,
for however many times you would like to allow your user to guess the word.

We also need to make sure that we can go through and have this come up
with a random word. You are not likely to be the one sitting on the other side
of the program picking out the word that you want the user to work with,
especially if you are the one trying to play the game. Having the random part
show up is important here so look up how to add that into your code as well.

And finally, we need to make sure through this one that the loop does not go
on forever. We want it to loop seven times in this game, or as many times as
you decide is best, and then we want the game to end. We don’t want to
allow the user a chance to guess an infinite number of times. That takes the
fun out of the game and can get it all stuck. So add that to the end of your
loop as well.

You may take a look at this code and notice that it is long, and that can be a
bit worrisome when you first start. Don’t let this fool you though. It is long
but you are working with a lot of different parts in this as well so of course
there is a lot that you have to write out. Even so, take a look at the code and
see how many of the different parts you already recognize along the way. if
you stop and study it a bit, it is likely that you recognize quite a few parts in
there as well!
Python and Email
This one is a bit different than some of the others. We are not going to focus
on creating our own games or working with all that, but we are going to focus
on another cool task that we can do when it comes to Python, and that is
using it to help us with our own emails along the way. this may seem a bit
silly to work with, but you will find that it will bring in a lot of the parts that
we already talked about so you can write some really neat codes along the
way.

There are actually a few modules found in the standard library of Python that
is meant to help you work with emails as well as with some of the servers of
this. The first module that we need to spend our time and attention on is
known as the smtplib. This one is there to make sure we provide the right
definition to the client session object of SMTP and that we are able to use this
to send mail to any machine that is on the internet, as long as it has the SMTP
or the ESMTP attached with it.

In case we are not familiar with how this works, the SMTP is short for
Simple Mail Transfer Protocol. This module will be the one that you want to
use to help you out with communicating with different mail servers. The
actual way that you sue this will vary based on how complex the emails are
and the settings that show up on your email servers.

With that introduction out of the way, we need to take a look at how to create
an object for SMTP. This will be similar to how we would create any object
in Python so that will make it easier, you also need to import the library that
we mentioned before so Python knows how to handle all of this as well. With
this in mind, you can try to write some of this coding on your own, and when
you are done, take a look at this example to see how it can be done with this
particular library:

import smtplib
server = smtplib.SMTP(‘smtp.gmail.com’, 587)

#Next, log in to the server


Server.login(“youremailusername”, “password”)

#Send the mail


msg = “
Hello!” #The /n separates the message from the headers
server.sendmail(you@gmail.com, target@example.com, msg)

If you want to include subject headers, To, and From, you would need to use
the email package to do this because smtplib will not be able to modify the
headers or the content for you at all.

Overviewing the Email Package


While we are here, we need to take a look at the email package we can
choose when we work on Python. This one is unique in that there are a lot of
classes and functions that come together inside of it to help compose and
parse together the messages that you have in your emails, ensuring that those
on the other side are still able to look through them. We will start out by only
importing the classes that are necessary. This is a simple process to use and
will sometimes make it easier so you won’t need to pull out the full name of
that module again and again through this. To make this happen, simply use
the coding below:

from email.MIMEMultipart import MIMEMultipart


from email.MIMEText import MIMEText

Then you can move on and compose some of the headers. The code that you
need for this includes:

fromaddr – you@gmail.com
toaddr = target@example.com
msg = MIMEMultipart()
msg[‘From’] = fromaddr
msg[‘To’] = toaddr
msg[‘Subject’] = “Python email”

After this code is done, you will want to take the time to attach the body, or
the message, of the email to the code that we have been doing. To do this, use
the following code:

body = “Python test mail”


msg.attach(MIMEText(body, ‘plain’))

When you want to send out the mail, it is important to convert your object to
make it into a string. From there, you are able to use the same procedure that
we talked about above in order to send this message with the help of your
SMTP server. The code you need to make this happen includes:

Import smtplib
Server = smtplib.SMTP(‘smtp.gmail.com’, 587)
Server.ehlo()
Server.startts()
server.ehlo()
Server.login(“youremailusername”, “password”)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)

It is possible that you will need to go through this process at some point and
will need to verify one or more of the email addresses that you are working
with at the time. The protocol of SMTP that we use here will have a good
command that will help out. It is responsible for telling the server to check if
you are receiving from a valid address.

Usually, the VRFY is something that will be disabled to prevent spammers


from finding out the addresses that are legitimate or note. But you can easily
enable it for now so that you can get the server to ask about this, and then
send you some code to give you an answer. This is not too hard to create,
despite the sound so take some time here to give it a try and see what happens
with your own coding.

And that is all that you need to do to make your own email server and start
sending your own emails in the process. It is a few steps and takes a bit, but
considering all that happens in them you can definitely see why Python is
such a simple thing to work with and why so many people choose to work
with this language on their projects. Go ahead and try this out and see if you
can create and send your own emails along the way.

As you can see here, there are already a lot of great projects that you can
create with the help of the Python language. These were really simple
projects to work with, and they didn’t include a lot of extras that were hard to
work with at all. And yet, even with just that basic knowledge, you were able
to create your own email server, and a few games, along the way. isn’t that a
really great thing to know when you work as a beginner in Python?
Conclusion
Thank you for making it through to the end of Beginners Python 2, let’s hope
it was informative and able to provide you with all of the tools you need to
achieve your goals whatever they may be.

The next step is to open up your compiler and actually work on a few of the
different types of codes that we spent some time discussing through tis
guidebook. Doing this and learning the codes, while getting some practice, is
one of the best methods to help us make sure that we can learn what is going
on and to help us to gain more experience and confidence in the Python
language. This guidebook has provided you with a lot of the options that you
need to handle this and can be an easy and complete review of the Python
basics along the way as well.

The best way to make sure that you get the most out of this is to take a look at
some of the basic parts that are with this and look at the exercises. And then
take some time to really give them a try, writing out the program the way that
you think is the best, before moving on and looking at the answers that we
provide. This will give you a good learning experience that you can use along
the way, rather than just reading the answers and hoping that will be right.

You may be surprised at the results that you get out of this. It may seem hard
and confusing along the way, but it will really be a good option to ensure that
you can get these codes down as well. Try it out, and move on by comparing
your answers to some of the answers we provide to see what works and what
you need more practice on.

There are a lot of things about the Python language that you are sure to love
in no time. When you are ready to learn more about how to work with the
Python language, and all of the neat things that you can do with it, make sure
to check out this guidebook to get started.

Finally, if you found this book useful in any way, a review on Amazon is
always appreciated!
Description
Have you been hard at work studying Python and learning all of the neat
things that you can do with this language? Are you ready to get into coding
and actually seeing where that knowledge can take you in terms of doing well
with programming? Then this is the guidebook for you!

Inside, we won’t just talk about the topics of Python and hope that you catch
on. We will take some time to look more closely at many exercises, and
games and more that you can create and use some of your previous
knowledge of Python along the way. And it won’t take long before you
surprise even yourself with your new skills and how much you can do.

This guidebook will help you to explore your skills and get practice with
actually writing out the codes that you want along the way. You will get
experience writing codes for operators, classes, inheritances, functions, and
so much more. When you are done, you will be ready to write out some of
your own codes in no time.

There is so much to discuss in this guidebook, and some of the best topics we
will spend our time on include:

What the Python language is all about and why it is such a good
choice.
How to work with functions and do a few great projects with it.
The importance of classes and how these fit into your code.
A look at the regular expressions.
Conditional statements and loops and some examples of how we
can create these, no matter what the situation is.
A look at inheritances and exceptions and how these combine
some of the other topics together to give us the best results in our
codes.
The operators and how these can show up in many of our codes.
File input and output and how we can do different things with our
files in Python.
Some simple exercises we can use to make working on Python so
much easier.
You already know a lot about the basics of Python and some of the parts that
come with that. Now it is time to review that information, and then put it to
good use with some practice. This guidebook will provide you with so many
great exercises and activities that will provide you with all of the practice you
need. When you are ready to take your knowledge of Python to the next level
and learn how to write some amazing codes in Python, make sure to check
out this guidebook to get started.

You might also like