Russel J. Python Programming. A 7-Day Crash Course To Learn Python Language 2020
Russel J. Python Programming. A 7-Day Crash Course To Learn Python Language 2020
John Russel
© Copyright 2019 by John Russel- All rights reserved.
This content is provided with the sole purpose of providing relevant information
on a specific topic for which every reasonable effort has been made to ensure
that it is both accurate and reasonable. Nevertheless, by purchasing this content
you consent to the fact that the author, as well as the publisher, are in no way
experts on the topics contained herein, regardless of any claims as such that
may be made within. As such, any suggestions or recommendations that are
made within are done so purely for entertainment value. It is recommended that
you always consult a professional prior to undertaking any of the advice or
techniques discussed within.
This is a legally binding declaration that is considered both valid and fair by both
the Committee of Publishers Association and the American Bar Association and
should be considered as legally binding within the United States.
Furthermore, the information that can be found within the pages described
forthwith shall be considered both accurate and truthful when it comes to the
recounting of facts. As such, any use, correct or incorrect, of the provided
information will render the Publisher free of responsibility as to the actions taken
outside of their direct purview. Regardless, there are zero scenarios where the
original author or the Publisher can be deemed liable in any fashion for any
damages or hardships that may result from any of the information discussed
herein.
Introduction
Day 1: The Basics
What is Python
Your Choices in Python Versions
The Benefits of Python
How to Install Python On Your Computer
Day 2: The Variables and the Operators
The Dictionary
Lists
How to Create a Class
The Class Objects
Inheritances
Encapsulation
Polymorphism
Day 7: Getting Started; Python Tips and Tricks
Conclusion
Introduction
Congratulations on purchasing Python Programming and
thank you for doing so.
For our first day, we are going to start out nice and
easy and learn a bit about the Python language. We are
going to learn what some of the benefits are, a bit
about the different versions that come with this
language, and why you would want to choose to work
with this language compared to some of the other
options out there. We will also take a brief time to look
at some of the steps that you need to take in order to
download this language onto any operating system
that you want, ensuring that you can jump right into
coding when you are ready.
On the second day, we are going to look at some of the
basics that you need to know when it is time to start
coding. We will spend time looking at strings, numbers,
how to handle variables, statements, classes,
comments and more. And then on day 3, we will move
on to looking a bit at the Python functions and how we
are able to make these work for our needs. These
functions are important to many of the codes that you
want to write, and we will spend our time looking at
how to define them, and how to make some of our own
as well.
And that is all that it takes! Even if you have never worked
with a coding language in the past, and this is the very first
time that you have even worked with it at all, you will find
that this guidebook has some of the tips and tricks that you
need to get this done. It only takes seven days for you to
get started with a new coding language, especially one as
simple as Python, and soon you will be able to write out
some of your own codes and see some great results in the
process.
There are many things that you are going to love about
working with the Python language. Many people may be
wary of starting on a new coding language because they
may think it is too hard for them to focus on or learn. They
may not think they have the time to learn. Or they may
worry that each time they want to create a new project,
they will have to go through and start from scratch.
And all of this can be done with the ease of the Python
language. You will find that when we spend some time
focusing on this language and some of the basics that we
are able to do will help to prepare us for some of the more
complicated things that we are able to do with this code as
well.
Your Choices in Python Versions
As a type of interpreted language, Python does have a lot of
advantages over the other programming languages. One of
these advantages is that it has the ability to grow and make
changes as your computing needs change. Like your
desktop applications, the fact that Python is continually
developed will allow for new features to be added to the
language, and you will see a lot of refinements added to
Python to make it easier to use.
If you do decide that this version is the best one, you should
still take a look at the 3.X version. There are some
differences in the best programming practices of each and
you may even be able to make slight modifications to your
code on the 2.X version and get it to work on the newer
version.
It will not take long for you to work with the Python
language before you are able to see some of the benefits
that are available when it comes to working with this kind of
language. In fact, you may already know some of the
benefits that are present, and you may already have a list of
your own reasons as to why you would like to choose this
language as the one that you choose to work with. Some of
the different benefits that come with working on the Python
language will include:
There are a lot of resources out there that will help you to
take charge of your programs. This is due to the fact that
there is a large community of programmers out there who
are willing to answer your questions, show you new ways to
do some of the codings, and so much more. Because Python
has so many benefits and is such a good coding language to
work with, there are a ton of developers out there who know
how to use this language, and they are found all throughout
the world.
The standard library that comes with Python will help you to
get a ton of things done no matter what your level of coding
may be. When you download the Python language like we
will talk about later, you will find that it has a lot of the
capabilities that you need to get some coding done. We will
take a look at some of the different coding things that we
are able to do when it comes to working with Python, but
there is so much more than the Python language is able to
do to help us see results.
As we can see here, there are a lot of parts that come with
the Python language and being able to use this properly and
getting them to work the way that we want, and there is just
so much that we are able to do with this language. Now that
we have some of the basics down of what works with
Python, let’s explore a little bit more about some of the
different things that we are able to do with this language to
make it work the best for us.
Day 2: The Variables and the
Operators
Of course, for right now those are in the future, and we are
going to spend our time looking at some of the basics that
come with coding. Still, some of the topics that we are going
to spend our time on in this chapter will be things that you
can see in all sorts of programs that you work with and can
be reused over and over again. These include things like the
Python keywords, the comments, functions, operators, and
more. Let’s dive into some of these topics and see what
they are all about so we can learn how to make them work
for some of our needs as well.
Python and Its Keywords
The first thing that we are going to take a look at when it
comes to doing some of the codings that we want to
complete in Python is to take a look at some of the
keywords that come with this language. These keywords are
so important because they are going to help us to really
give the right commands to the compiler. When these
keywords are used in the proper manner, you will find that
they are going to provide you with the right execution when
it is all said and done.
You have to make sure that you are using the keywords in
the right spots of your code. If you use them outside of the
prescribed areas, you will end up confusing the compiler
that you are using because it will not know what you would
like to have done. Make sure that you are learning what
some of these keywords are all about, and where to put
them properly in your code, to ensure that they are going to
do the right work for you.
The Comments
Another part of the code that is pretty simple to work with,
but can make a world of difference in some of the parts of
coding that you do here will be the comments. Sometimes,
you may go through the code and need to leave a little note
or an explanation about what is going on inside of the code.
But you don’t want to have the compiler read these at all
and ruin the program. This is where you will need to bring in
the comments.
The next basic on our lists that we need to spend some time
on here is the importance of functions and how we can use
these to our advantage as well. These functions are
basically just going to be sets of expressions, which we can
also call statements in some cases. They are going to fall as
some of the first-class objects that show up in the code,
which helps to take off some of the restrictions and other
issues that can come upon how you will work with these.
The nice thing here is that you are able to use these
functions in a manner that is similar to some of the other
values out there, including strings or numbers. Plus, they
are going to have some nice attributes that we are able to
bring out, simply with the use of the dir function.
It will not take long for us to work with these functions
before we notice that they are really diverse. Plus they
come with a lot of attributes that the programmer is able to
use when they would like to create these functions. Some of
the different attributes and types of functions that we are
able to work with here will include:
Before we get too familiar with some of the work that can be
done with these Python functions, we need to take some
time to understand the rules of indentation when we are
declaring these functions in Python. The same kinds of rules
are going to be applicable to some of the other elements of
Python as well, such as declaring conditions, variables, and
loops, so learning how this work can be important here.
Then we can take this a little bit further as well. Here we are
going to see how we are able to retrieve the output with the
use of the “return” command. When you work with this
function, and then execute out the code that we are going
to have, and we used the command of “print square (4) “
from above, our output is going to be 16.
Then we are able to work with the list. The list is going to be
one of the four collection data types that we have in Python.
When we are working with a collection type and picking out
which one we want to work with, it is important for us to
understand the limitations as well as the functionality of the
collection. Dictionaries, sets, and tuples are going to be
some of the other collection data types that come with
Python. A list is ordered and changeable, unlike the string.
We are then going to be able to add some duplicate values
to this as well. And we can do the declaration of these with
some square brackets as well.
The nice thing about these lists is that you are able to work
with this process and then make some changes to the items
that are on the list. Some of the different methods that we
need to know about when it comes to working with these
lists include:
for x in myset:
print(x)
#this will get all the values.
20 in myset
#this will return true if the value is in the set.
#to add value in a set
myset.add('edureka')
#to add multiple values in a list
myset.update([ 10, 20, 30, 40, 50])
#to remove an item from a set
myset.remove('edureka')
#we can use the discard or pop method to remove an item
from a set as well.
myset = {10, 20, 30}
myset1 = {10,30,50}
myset.issubset(myset1)
#this will return false
myset.union(myset1)
#this will return a set with the union of the two sets.
Since we are working with the keys to help access the items
that are there, we have to remember that we can’t duplicate
them at all. If we do, then the compiler is going to get
confused about the key value pairs that you want to use,
and it just makes a mess. The values can have duplicate
items though. Some of the other operations that are needed
when we are working with this dictionary is going to include
some of the following:
The first thing that we are going to take a look at here when
we get started with files in Python is how to create a brand
new one to work for our needs. Any time that you would like
to make a few changes to the file that is already opened on
your computer though, we would work with the function of
write(w) to help get this done because it is the easiest and
can allow us time to start writing right away.
Any time that you would like to open up a new file though
and then write out a brand new string inside of there, we
will work with a binary file, which we will discuss a bit more
in a moment, and then work with the write() method again.
This is going to work out well for us because it will make it
easier to get all of the characters that you want to be
returned to you right away.
The write() function is really easy to use and allows you to
make as many changes to the file as you would like. You can
add some new information to the file or you can make some
changes to one that you opened up. If you are interested in
doing the writing in the code, you can use this example to
make things easier:
Before you jump on this one, the number one thing that we
need to consider here is that we have to be able to supply
the data inside of our object. This will make it easier for us
to expose the byte that is there when we execute the code.
The syntax that we are able to use to make this happen will
include:
This will help you to get the data changed over to a binary
file simply by turning it over to the image or sound file.
How to Open Up Your Files
Now that we know how to create and save one of our files, it
is time to move on and take a look at the steps that you
would need to take in order to open up one of your files.
Once you open up that file, you are then able to overwrite
what is on it, or just write things on it for the first time. the
good news with this one is that opening up the files that you
would like to use through Python is going to be relatively
easy to accomplish. Some of the codes that we are able to
use to make this happen to include the following:
the output that you would get from putting this into the
system would be like the following:
Hello, world!
This is a demo using
This file contains three lines
Hello world
This is a demo using
You will find that when it comes to some of the Python codes
that you want to write, working with these files will be a
great option to choose from. We already took some time to
look at how to write out a new file, how to overwrite some of
the data that we were able to write into the file, and we
even looked at how to open up the files. Now we are going
to take a look at how to move your file from one directory to
a new directory.
Maybe you are working on a file and you then find out that
things are not matching up in the manner that you would
like, such as misspelling the name of your identifier the
wrong way, or you ended up placing it into the wrong
directory. When this happens, you would like to use the seek
option in order to help you find that file, move it where you
want or change the name of the file you are working with.
It can help you to write out the code, make some changes to
what you wrote in the first place, and more. Try out some of
the different codes that we talked about above and you will
find that you can do anything you would like with some of
your Python codes.
Day 6: Data Structures
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
You are also able to take this a bit further and delete all of
the items that are in your dictionary at once. You just need
to use the clear() function. Items that show up in a nested
dictionary can also be deleted using that del keyword that
we had before, as long as we make sure to provide the
specific nested key and a particular key to be deleted from
that nested dictionary.
# Initial Dictionary
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks',
'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
'B' : {1 : 'Geeks', 2 : 'Life'}}
print("Initial Dictionary: ")
print(Dict)
# Deleting a Key
# using pop()
Dict.pop(5)
print("\nPopping specific element: ")
print(Dict)
Lists
We can also take some time to look at what the Python lists
are all about. There are going to be four types of collection
data in the Python language. We already talked about one of
them with dictionaries. But let’s spend some time looking at
all four of them below:
One thing that you will quickly find is really important when
it comes to working on your Python code is that the classes
are going to have a big place. In fact, this is one of the
things that is going to make your codes so strong and easy
to use in Python. These classes are meant to help you get
the most out of what you are doing in this process, and will
ensure that you are able to really organize things and get
them to show up at the right times for you.
class Vehicle(object):
#constructor
def_init_(self, steering, wheels, clutch, breaks, gears):
self._steering = steering
self._wheels = wheels
self._clutch = clutch
self._breaks =breaks
self._gears = gears
#destructor
def_del_(self):
print(“This is destructor….”)
myGenericVehicle.Display_Vehicle()
class Cat(object)
itsAge = None
itsWeight = None
itsName = None
#set accessor function use to assign values to the
fields or member vars
def setItsAge(self, itsAge):
self.itsAge = itsAge
def getItsName(self):
return self.itsName
objFrisky = Cat()
objFrisky.setItsAge(5)
objFrisky.setItsWeight(10)
objFrisky.setItsName(“Frisky”)
print(“Cats Name is:”, objFrisky.getItsname())
print(“Its age is:”, objFrisky.getItsAge())
print(“Its weight is:”, objFrisky.getItsName())
Before we move on, type this into your compiler. If you have
your compiler run this, you are going to get some results
that show up on the screen right away. This will include that
the cat’s name is Frisky (or you can change the name to
something else if you want), that the age is 5 and that the
weight is 10. This is the information that was put into the
code, so the compiler is going to pull them up to give you
the results that you want. You can take some time to add
different options into the code and see how it changes over
time.
Working with these classes can be a great way to see some
of the different things that the Python language is able to do
overall, and it is likely that with any code you would like to
write, you will need to create a few of these to get the best
results. With that said, we need to see that these classes
are not really all that difficult to work with and they can be
just the thing that you need to organize your code and keep
everything in place. Take some time to explore the codes
that we have above to ensure that you are able to create
some of your own Python classes in no time.
The Class Objects
Now that we have explored a bit about the classes above,
we also need to take some time to look at the objects of
these classes. A good way to think about this is that the
class is going to be like a container that is able to hold onto
the objects. The objects will be real-life items that we add to
the code, and they need to be stored somewhere so that the
code is able to find them at the right time. This is where
they will fit into the classes that you plan to create.
With this in mind, there are a few rules that we need to use
to make sure that the objects work with the classes that we
create along the way. You are able to place as many objects
as you would like into a class, and you can have any type of
class that you would like. But when you or another person
takes a look into one of the classes that you created and
you look at some of the objects, it needs to make sense why
those objects are together in the same class.
This doesn’t mean that the objects have to be identical to
one another. It simply means that we can’t just throw
random objects into the same class and hope it works out
for us. You could have a class that is about vehicles or fruits
for example, but putting things like a horse, the color blue,
peas, and pepper into the same class would not make sense
and should not be done.
Inheritances
Another cool option that we are able to work with when it
comes to the Python language is the idea of inheritances.
This is something that is pretty unique when it comes to
OOP languages like Python and when you learn how to use
them properly, you will find that it is a great way to save
time by reusing parts of your code, and can make the codes
look nicer and cleaner in the long run. These are going to
help us to reuse parts of code that we have used in the past
on our program, without having to go through the process of
rewriting all of that same code again and again.
#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)
def DisplayPostGraduateStudent(self):
print(“Student Name:”, self.name)
print(“Student Rollno:”, self.rollno)
print(“Study Group:”, self.postgrad)
When you type this into your interpreter, you are going to
get the results:
(‘Student Name:’, ‘Mainu’)
(‘Student Rollno:’, 1)
(‘Student Group:’, ‘MSC-Mathematics’)
(‘Student Name:’, ‘Shainu’)
(‘Student Rollno:’, 2)
(‘Student Group:’, ‘MSC-CS’)
1. Find the URL that you would like to scrape in the first
place.
4. Write out the code that you would like to use with the
help of Python in order to complete this process.
5. Run the code that you just wrote and then extract out
the data that you would like to use.
There are also a few options that you are able to use when it
is time to work on the process of web scraping. As we know,
Python is already able to be used for a lot of different types
of applications, and there are going to be a ton of libraries
with Python that is going to be used for different purposes.
There are a few libraries that work the best when it comes
to working with the process of data web scraping will
include:
def nested_loop():
for number in num_list:
print(number)
for letter in alpha_list:
print(letter)
if __name__ == ‘__main__’:
nested_loop()
Then we are able to move onto the second line. this one is
going to show us the current line of a source code that is
going to be executed here, as a pdb, and will provide us
with an interactive console that is going to help us to do the
debugging that we want. You are able to work with the help
command to help us to learn the commands that are here,
and then the help command to help us out with the specific
command that we want. Note that the console for pdb is
different than what we will find with the interactive shell in
Python.
if __name__ == ‘__main__’:
nested_loop()
The current line is going to be seen with the ->, which in our
case is going to be the first line of the file that we are
working with, but it can definitely be found in some of the
other parts as well.
(Pdb) list 3, 7
3
4
5 def nested_loop():
6 for number in num_list:
7 print(number)
(Pdb)
Now we need to take a quick look at the step and the next
function. The step is going to stop within a called function.
But then the next one is going to execute all of the called
functions, and it is only going to stop at the next line of the
current function. We are going to see the difference of this
when we work with the functions, even though it may seem
like it is not telling us much right now.
(Pdb) step
> /Users/sammy/looping.py(5)<module>()
-> def nested_loop():
(Pdb) step
> /Users/sammy/looping.py(11)<module>()
-> if __name__ == ‘__main__’:
(Pdb) step
> /Users/sammy/looping.py(12)<module>()
-> nested_loop()
(Pdb) step
--Call--
> /Users/sammy/looping.py(5)nested_loop()
-> def nested_loop():
(Pdb) step
> /Users/sammy/looping.py(6)nested_loop()
-> for number in num_list:
(Pdb) step
> /Users/sammy/looping.py(7)nested_loop()
-> print(number)
(Pdb) step
500
> /Users/sammy/looping.py(8)nested_loop()
-> for letter in alpha_list:
(Pdb) step
> /Users/sammy/looping.py(9)nested_loop()
-> print(letter)
(Pdb) step
x
> /Users/sammy/looping.py(8)nested_loop()
-> for letter in alpha_list:
(Pdb) step
> /Users/sammy/looping.py(9)nested_loop()
-> print(letter)
(Pdb) step
y
> /Users/sammy/looping.py(8)nested_loop()
-> for letter in alpha_list:
(Pdb)
And maybe you have gone through and tried out a few other
options and they are not working out for you either. This can
be frustrating when you are a beginner, but sometimes the
best thing that we are able to do for this is to ask someone
who has some more experience with coding to help us out.
If you have gone through all of these steps trying to
troubleshoot your program and you still can’t seem to find
the error or you worry that you are just making things
worse, then it is fine to ask someone else for help. But
before you ask someone to help you go through the code
and figure out what is wrong, you need to make sure that
you have all of the following parts in place to help them
understand what is going on, what you have tried, and what
they can do to help you out.
Instead, every few minutes, you should run the updates and
test the code to see if something comes up. That way, when
a bug does show up in the code, you only have a few lines
or so of the code to check for the issue rather than a whole
bunch of code. It really isn’t possible for you to test out your
code too often so do it as much as you can.
Always remember that the more code that you write before
you test it again, the more places where errors can occur
and the more code that you will need to go search through
again. And each time that you do go through and run the
code, you will get some more feedback on your work so you
can learn as you go.
Along the same lines, you may find that printing out a lot of
the things that you work with can help you to really see
what is going on as well.
As you look through your code, you should be able to look at
each line and have a sense of what values are associated
with each variable. If you are unsure, then stop and print
them out. Then, when it is time to run that program, you
can look at the console and see how the values are going to
change or how they get changed to null values in a way that
you wouldn’t expect.
The next step is to set up one week where you would like to
start learning Python and go through each day of this
guidebook to help you get that done. Many people are
worried about starting out with a new coding language
because it may seem complicated and like something that
they would never be able to catch up on. But with the help
of this guidebook, you will be able to master some of the
basics, and even some of the more advanced, parts that
come with the Python language.