Python Programming For Beginners B06XRHH3SN PDF
Python Programming For Beginners B06XRHH3SN PDF
for Beginners
Easy Steps to Learn the
Python Language and Go
from Beginner to Expert
Today!
Table of Contents
Table of Contents
Chapter 1: Starting Out With Python
Chapter 2: How to Work with Your Files in
Python
Chapter 3: What are Exceptions and How Do
They Work in Python?
Chapter 4: What Does Object Oriented
Programming Mean?
Chapter 5: The Importance of Classes and
Objects in the Python Code
Chapter 6: What are Exceptions and How Do
They Work in Python?
Chapter 7: Working with Conditional
Statements in Python
Chapter 8: Learning How Inheritances Work in
Python
Chapter 9: Creating Loops in Python
Chapter 10: The Importance of Those
Operators
Chapter 11: Ready to Work with Some
Projects?
Chapter 1: Starting Out With Python
Readability
Libraries
Community
Basic Patterns
re.findall()
re.match()
re.search()
import re
string = ‘apple, orange, mango,
orange’
match = re.search(r’orange’, string)
print(match.group(0))
When you take some time to place this
code into your interpreter, you will find
that the output is going to be “orange”.
You will be able to find the pattern
whether it is at the beginning of the code
or later on, you need to remember that
with the search() function, it is only
going to find the code for you once. In
this example the search function found
that there was one match of orange
because that is all that is there. But, you
could have ten more oranges in this
code, and it is only going to show up the
word “orange” once because that is the
word that has a match.
Match Method
Now we are going to move on to the
second query option that you can work
with. This option is going to be slightly
different compared to the search function
that we did above, but it can also be
helpful inside of the Python language.
Using the match function is going to
work in that it will find the matches that
show up right at the beginning of the
string. If there is a match that occurs
anywhere except right at the beginning of
the string, it is not going to show up in
the code.
Classes
Objects
Inheritance
Data encapsulation and abstraction
Dynamic binding
Polymorphism
Classes
Objects
Polymorphism
Dynamic binding
It is also possible to work with dynamic
binding when you are working on your
OOP language. Binding refers to the
linking of a procedure call to the code to
be executed in response to the call. But
when you are working with dynamic
binding, this means that the code will be
associated to the given procedure, but
the call is not known until you call this
out at the run time. You are often going to
see this happen with some of the other
features of OOP including with
inheritance and with polymorphism.
The OOP options are one of the best to
use in your programming language
because they are simple, there is a lot
that you are able to do with it, and it is
easier to use without as many
complications like you would get with
some of the older coding languages.
Python as well as some of the other
modern programming languages will use
this option, making it one of the best to
use if you want to have safe
programming that is easy for you to make
and the user to use properly. And as this
chapter shows, there are a lot of great
things that you will be able to do with
your code when you use a programming
language that utilizes OOP.
Chapter 5: The Importance of Classes
and Objects in the Python Code
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 subclass[(superclass0]:
[attributes and methods]
The object instantiation syntax will look
like the following
object = class()
object.attribute
object.method()
class Cat(object):
itsWeight = 0
itsAge = 0
itsName = “”
defMeow(self):
print(“Meow!”)
defDisplayCat(self):
print(“I am a Cat Object, My
name is”, self.itsName)
print(“My age is”, self.itsAge)
print(“My weight is”,
self.itsWeight)
frisky = Cat()
frisky.itsAge = 10
frisky.itsName = “Frisky”
frisky.DisplayCat()
frisky.Meow()
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())
Property
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
>>>
x = 10
y=0
result = 0
try:
result = x/y
print(result)
except ZeroDivisionError:
print(“You are trying to divide by
zero.”)
class CustomException(Exception):
def_init_(self, value):
self.parameter = value
def_str_(self):
return repr(self.parameter)
try:
raise CustomException(“This is a
CustomError!”)
except CustomException as ex:
print(“Caught:”, ex.parameter)
Using this particular syntax is going to
help you to get the message “Caught:
This is a CustomError!” and it is going
to show up whenever the user places in
some of the wrong information, based on
the conditions that you set out. This is a
great way to show that there is a custom
exception that is going on inside the
program, especially when this is an
exception that you set up by yourself
rather than one that is through the
program.
Keeping this in mind, you are able to
make any changes to the wording that
you would like to use inside this as well.
You aren’t stuck with just leaving the
message that we have listed above. You
can use this as a good starting point, or
choose to change it up so that it works
for your particular program. If you are
keeping people out of the program
because it is a gambling site and you
can’t allow anyone under 21 to come
into the game, for example, then you may
want to leave a message about that in
there.
When you are working with these
exceptions, you will find that they are
going to be defined as being able to do
the same things that most of your classes
are able to do, but it is best to keep these
as simple as possible when you are the
programmer, changing a few of the
attributes to make sure that the right
error is extracted out of the exception,
but not adding in much more than that or
it can get confusing.
else:
print(“result is”, result)
finally:
print(“Executing finally clause”
divide(2,0)
>>>
Division by zero!
Executing finally clause
>>>
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
#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)
Overloading
Multiple inheritance
1*1 = 1
1*2 = 2
1*3 = 3
1*4 = 4
All the way up to 1*10 = 2
Then it would move on to do the table by
twos such as this:
2*1 =2
2*2 = 4
And so on until you end up with 10*10 =
100 as your final spot in the sequence.
There are so many reasons why you
would like to use a loop inside of your
code. You will be able to use it as a way
to clean up your code while getting the
compiler to go through the same block of
code over and over again. The compiler
will continue to go through this code
until the condition is no longer
considered through and then it will move
on to either end the program, or move on
to the next part if there is more. This can
open up a lot of things that you are able
to do with your code while keeping
things easy to use.
Chapter 10: The Importance of Those
Operators
Arithmetic operators
Comparison operators
Assignment Operators
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”)
Import random
min = 1
max = 6
roll_again = “yes”
print(“
“
print(“Start guessing…”)
time.sleep(.05)
#here we set the secret
word = “secret”
#print wrong
print(“Wrong”)