Python Tutorial for Beginners [Step By Step] | Learn Python in 2020 | Python Training | Edureka


Change is what describes our world and when it
comes to technology, we all know that these changes are python
Talking about the 80s and earlier the word python would raise Goosebumps and many
of us would run away from it. In order to avoid
being swallowed today. The same word
does raise Goosebumps, but with a different diet python
of the Are swallowed men and python of the present
has swallowed languages. One of the biggest reasons for
this magnetic pythonization is because it can speak English
unlike its Rivals the code that you write in Python can be
up to 10 times shorter than that of other languages
such as C C++ Java Etc. And what is more surprising
is this python can be learnt by just anyone? So hey guys here is
Wajiha from Edureka. with this
magical Python tutorial, which will teach you everything
you need to know about python. Of course, I’m not going
to teach you how to run when See a python [snake], but I can definitely help you
to run your codes in Python. By the way, if you see a python just walk
away and don’t mess with it. Okay, so jokes apart. Let’s take a quick look at
what’s in store for you guys in the session. We shall begin this discussion
with a small introduction to the Python programming
language covering its birth history major projects that use Python reasons for its popularity features Etc. Then we shall move on to see how to install Python
and the Anaconda platform once we’re done with
the installation we shall talk. Talk about the aspects such as
naming conventions variables and data types in Python following which we will take
a look at flow control and python this includes Loops
conditional statements Etc. Then we shall see what our functions data
structures and algorithms in Python following that we will take a look
at Loops in Python. And lastly we shall Learn
Python file handling. So before we move on just
make sure you subscribe to our Channel and hit
the Bell icon to stay updated with all the latest
at Edureka videos. Also if you’re looking
A certification in Python check out the link given
in the description box below. So without any further delays,
let’s move on. So talking about the history of
python python was developed by Guido van Rossum back in 1980s and its implementation
began since 1989. Python was named after the TV series
Monty Python’s Flying Circus. It was a successor of
the ABC language and was capable of exception handling as well as interfacing with
the amoeba operating system. Python was led by
Guido van Rossum for 30. Ears that is until
12th, July 2018. As of now, the leadership of python
is shared by five-member bench namely very War. So Brett Cannon Carol
willing Guido van Rossum and Nick Coughlin talking
about the release of python versions. The release of python to was
done on 16 October 2005 season, 3 was released
on 30 December 2008. However, Python 2.7 support
has ended on 1st Gen this year. The date was actually
scheduled fires earlier, but Both bond
in order to provide. He’s in conversion
of projects built on Python 2.7 to Python
3 the latest version of python. That is Python 3 .8 was released
on 14 October 2019. So moving on let’s
take a look at some of the important projects
that make use of python Google. Yes, the world’s most
famous search engine is driven by python python is one
of the most important languages at Google not just
the Google search engine but Google developers
of YouTube also rely heavily. Emily on python because python is used
in almost every bit of YouTube Dropbox Dropbox as many of us know is
a file hosting service that allows file storage
synchronization Cloud Etc. This award-winning application
runs on python in its back-end Cora. This website has answers
to all your questions and the developers of Korah
actually rely on python. Next comes Instagram there
might hardly be anyone who is unaware
of this application Instagram allows you to post
Your favorite pics and videos on the social
platform with the help of python BitTorrent BitTorrent is a notion
of databases and content but the foundation of
this website is built on python. So those were some
of the major projects that run on python now, let’s move on to see what is the reason
for the popularity of python at number one is
healthy active Community. The backbone of every project
is an active involvement of its members the python
Community provides super cool documentation of python. That allows anyone to learn it. Not just that this community
also is available to help you in case you get stuck with anything from beginners to experts this team is
always there for you. The python libraries python provides a huge
number of libraries that are great for machine
learning data science scientific experimentation
data visualizations Etc. These libraries come
with huge package of function that can be used by
the programmers also become with documentation that are
very convincing Big Data. One of the biggest reasons for
python success is it’s big data and Cloud python is one of
the best data science language that companies look out for it helps you bind
together big data and the web and thereby considered
to be the best by many in today’s world versatile. Python can be used
to develop anything such as web applications
desktop applications Games Etc. It is very much reliable and efficient as well
accessibility python is one of the most accessible languages has thereby helping
any new be to learn and master it Learners
can access and execute python without any difficulties. So those are some of the reasons
for pythons popularity. Now, let’s move on and take
a look at the features of python python has a huge set
of incredible features. It is an interpreted language
meaning that the instructions are executed directly without
being compiled previously. It is very much readable
and understandable plus it’s a cross-platform
programming language which means the code written
once Be executed on any of the platform’s with minimum or no changes python can be treated as a structural
language scripting language object-oriented language
or a modular language. Python can be integrated with languages such as
Java C C++ Etc. It is also a free
and open source language. So moving on towards
something very interesting for python developers that is job Trends
and salary trends for python developers talking
about python job Trends python. Uppers can hold any designation such as
software Engineers python developers research analysts
data analysts data scientists or a software developer. These designations can be in any domain such as
Big Data data science artificial intelligence machine
learning web development Etc. Not just that python developers are also among the highest
paid professionals all over the world. However, the salaries may vary
depending on factors such as experience location skills
Etc on an average and She-Devil python
developer owns about five lakh INR in India and about 88k in the US per year
according to a survey carried out by pay scales
till June 2019. Also salaries of python
developers very geographically in the US the best location for a python developer
seems to be California. Whereas in India. It seems to be Google
not the forget. Let me also mention that having a knowledge
of python along with other skills proves to be
a very crucial factor for influencing python
developers salaries. The most in-demand skills
preferred along with python or AWS and Django. So that was about the job Trends
and the salary Trends now, let’s move on and compare python
with Java C++ and are when compared to Java python
proves better than Java in many cases such as the code readability syntax
lesser Legacy problems Etc. Python is compared
to C++ is better in terms of code readability the syntax
the scope of variables Etc. Fightin leads are in areas such as code readability
job Trends popularity Etc. So now that you’re aware
of python and it’s important. Let’s move on and see how to install this on your systems python
can be obtained from the python software Foundation
website at python.org. Typically that
involves downloading the appropriate installer
for your operating system and running it
on your machine now to do that. All you can do is open up
your browsers and just type in Python Dot o– r– g– Go to downloads
and download python 3.81 for any of the operating system
such as Windows Linux Mac OS or any others once you’ve selected
your operating system, just click on download. And you will see that the installer
gets downloaded on your PC’s. Once the installation
is completed. You can run
the installer to do that. Just click on
the downloaded file. Check the option that says add
Python 3 .8 to path. And then click on install now. I’m not going to install
this right now on my system since I already
have python 3.7 on it. Once the installation is done. Just click finish and you have python ready
to operate on your systems. So now moving on towards
the Anaconda installation. Please make a note guys
that installing Anaconda because I’ll be using
the jupyter notebook for all the codes and examples that I’ll be showing
in the session. So before I
actually install this, let me just brief you guys with
what exactly is an IDE idea. And for integrated
development environment, it is a graphical user interface where programmers write
their code and produce their final products and IDE, basically unifies all essential tools required for
software development and testing which in turn helps
the programmer to maximize his output some IDs are generate meaning
they can support a number of languages such as
Sublime Text Adam Visual Studio, excetera language-specific IDE
support a specific language only they also help you. Understand when you make
syntax errors some examples of these are pycharm
for python J creator for Java Ruby mine for Ruby on Rails talking about some more
important ideas for python. It includes Jupiter
spider Adam Etc. However, in this tutorial, I’ll make use of Jupiter
and therefore to make use of Jupiter. I’ll have to install
the Anaconda platform. So in order to install Anaconda, all you can do is go
to the official website. That is Anaconda. Anaconda.com slash distribution
So as you can see over here, I have an option
called as download when I click on this you will see that the Anaconda
platform is available for Windows Mac OS
and Linux choose the one that will suit
your operating system and then click on download here. I’d like to advise you guys
to download python 3.7 version as Python 2.7 has
no support anymore. So that was all
about the installation now moving on towards
the naming conventions pythons naming conventions outline. Game for naming some of these
are giving lowercase names to methods instance variables Global variables packages modules Etc class name
should have the first letter in uppercase followed by
lower case letters constants should be completely capitalized if a name contains two words, it’s good to separate
those words by an underscore a single underscore before a name means non-public
and the double underscore tells the python interpreter to mangle
those names of a class. So as to avoid The Clash
with the names defined in the other classes
so now moving on towards variables in Python. So what exactly is a variable in any programming language
a variable is a memory location where you can store
a value the value that you have stored may change in future according
to the specifications variables in Python can be created as soon
as a value is assigned to it. It does not need
any additional commands to declare a variable in Python. So now let’s move on
and take a look at the variable. Definition and declaration to understand how we declare
a variable in Python to do that. I’ll jump onto
my jupyter notebook. So as mentioned earlier python
has no additional commands to declare a variable as soon
as a value is assigned to it. The variable is declared
say for example, I have a variable X and I
assign a value say 10 to it. Now the variable X
has been declared since I’ve assigned
a value to it. However, there are certain rules
that we have to keep in mind while declaring a
variable the variable. Name cannot start with a number it can only
start with a character or an underscore variables
in Python are case sensitive. They can only contain
alphanumeric characters and underscores. No special characters are
allowed for naming variables. So as you can see over here, I’ve assigned a value of 10
to this variable X now in case I want to print this all
I can do is just save print. X2 run the code on Jupiter you
can either press shift enter or control + enter when you press shift enter
the code gets executed and the command is given
to the next Colonel when you press control
enter the code gets executed but the command remains
in the same kernel, so here I’ll just
hit shift enter and as you can see over here, I have the value printed as
10 variables can also hold other values such as
float values string values Etc. So taking a few examples of
these Take a new variable to a and I’ll assign a value of 20.3. I’ll take another variable
say be and I’m going to assign string value to this. I’ll just say hello world. So now let me just
print out a and b. So as you can see over
here both the values of A and B have been printed. I’ll be showing you all the
in detail functionality of the print function later on in the session variables can
also be re declared in Python. This means that you
can assign new values to them as in how you proceed
with your program. So as you can see over here, I’ve assigned 10 2 x so in case
I want to modify the value of x, all I have to do
is just declare x with the new value. So this time I just
say X glutes at Eureka and now let me print X. So as you can see
over here previously when I printed X it
return the value 10 now Ivory declared x with a new value and the new value
has been printed. You can either have
Global or local variables Global variables are visible
throughout the program. But local variables are confined
to the class or the function to which they belong so taking
examples of both of these. I’ll just take a variable
over here say C and I’ll assign a value
to this as hello world. No, create a function over here. I’ll just call it as funk. However, I’ll be discussing about functions later
on in the session. So within this function, what I’m going to do is
I’ll take the same variable C. And I’ll assign
the different value to this say for example at Eureka now. I’m going to run the function. I just created as well
as print value of C. So just call the function
and then I’ll say princey. Just a moment guys. I forgot to print the value
of the local variable C. So for that I’ll just say print C and now
I run this again. So as you can see over
here the local variable C has returned at Eureka. Whereas the global variable C
has returned hello world. Also if you can
notice over here, I’ve declared the value
of C right in the beginning and then printed it right
at the end of this program. This is because Global
variables are visible throughout the In case I remove
this print statement from here and just call the function then
the global variable value will not be printed. So as you can see over here
only the local variables value has been printed and this local variable
is not visible outside the function. So I hope you guys are clear
with variables in Python. So now let’s move on
towards the next topic which is data types in Python. So before we actually talk
about data types in Python, let’s discuss about the
difference between data types and data structures. A data type is a type of data element
defined by the value. It holds some examples
of these are NT float double Etc data structures
on the other hand deal with data organization enabling
the user to store access or modify the data, the important data structures
of python are lists tuples sets and dictionaries will
be discussing about the data structures later on, but for now, let’s come back towards
the data types in Python. The data types
available in Python are numbers Boolean values and strings numbers are nothing
but the numerical data types numerical data type
holds numerical values and it can be divided
into integers Floats or complex numbers. So just to show you
guys an example of this. I’ll take a new heading. I’ll just say data types. Taking an example
of the numerical data type. I’ll just take a variable say x and I’ll assign a value
to this as 19. So as we all know
19 is a whole number and this is an integer value. Also, you can assign negative
values say y equal to -4. So what I’m going to do
is print out the type of these variables so
I’ll just say print. type of x and the type of file
so as you can see over here that they belong
to the class integer. So as we all know float values
contained decimal numbers, so in case I want
to declare a float variable, I’ll just take a new variable
say are and I’ll assign some decimal values to this say 45.6 now, I’m going to print
the type of our So as you can see over here, it says that this belongs to class float complex numbers
are used to represent imaginary values imaginary
values are denoted by Jay at the end of the number say, for example, I’ll
take a variable s and I’ll assign a value
to this as 10 plus six J and then I’ll just print
out the type of it. Let me just copy the statement. And instead of our I’ll say yes. So as you can see over here, it has returned
that this is a complex number. So now moving on
towards the Boolean type the Boolean data type is used
for categorical output since the output for a Boolean
is either true or false. Now in order to show you
guys an example of this. Let me just take
a variable say V and over here. I’ll just take two comparison
values set to greater than 6 once I’m done with this. I’ll print out the type of way
as well as the value of V. So first I’ll print out. Type is a type of V. And the value of V. So therefore it belongs
to the class bowl and the value is false, which means do is not greater than 6 so now talking
about strings strings in Python are used to represent
Unicode character values python does not have a character
data type A Single Character is also considered as a string string values in Python are declared using
single or double quotes So for an example of a string,
I’ll just say my string. equals at Eureka and then
I’ll just print out my string. So as you can see over here, it has return the value
of my string. You can also access the values
of a string by making use of the index values. So in case I want
to access any element that is present in Ed Eureka, what I’m going to do is just
type in my string and pause on the index value of the letter that I want to retrieve so
I’ll just say for So as you can see over here
at index number four is e you can also make use
of negative indices. So let’s take an example
of that as well. So I’ll just say
my string of -2. So as you can see over here
at index number minus 2 is a which means starting
from the special character, which is present at minus 1
a is present at -2 here. I want you guys to note that positive indexing starts from zero and increases
by 1 negative indices or the other hand start from minus one and move
on as minus 2 minus 3 Etc. So I hope you guys
are clear still here. So now moving on towards flow control in
Python flow control statements basically decide the order of execution of the program generally accomplished through
Loops conditional statements and function calls. So taking a deep look at each
of these let us first discuss what our Loops in Python Loops
allow us to execute a group of statements several times. Posey on a software developer and you’re required
to provide a software module for the employees
in your office for this you must print the details
of payroll of each employee separately finding the details of all the employees
will be a tiresome task. So instead you can use the logic
for calculating the details and keep on iterating on the same logic statement
by making use of the for Loop. This will not just save you time but also it will make your code
very efficient the illustration that you see
on the screen is a flow. Chart for the for Loop
the execution starts and then it checks
for a condition to be either true or false condition
over here can be any logic that we want to test
in our program. So if it is true, it will execute the body
of the loop and if it is false, it will exit the loop. We shall discuss more about the
for Loop further on but for now, let’s talk about the conditional
statements in Python before talking about
the conditional statements. Let’s talk about
what are conditions conditions in Python are Conditions which we use in if and else statements some
of the conditions in Python are the equals condition
not equals less than less than or equal to greater than greater
than or equal to. These are the conditions we
often use while declaring a test expression for if and
the else statements in Python. So what exactly are the if
and else in Python and if statement is used
to test an expression and execute certain statements accordingly programs
can have one or more if statements in
them the subsequent or the penultimate statement that follows is
an else statement this executes a statement in case
all the tests expressions in the programs are
false to understand the control flow of an if and else statement take a look at the flowchart of
how the execution takes place. So as you can see the execution
starts with the test expression when this is true, the execution will enter
the body of if and execute all the statements That but
when this expression is false, the execution will move to
the body of the else statement and execute the statements
in the else block just like an if statement we
can also add an else. If or an L if statement in Python
to test out more expressions in order to optimize the code and increase the efficiency
of our code, so now I’ll get back
to my jupyter notebook in order to show you guys the syntax
of the if else statements. I just create
a heading over here. I’ll say conditional statements. So like I’ve already
told you all the if statements come
with the test expression. So what I’m going to do over
here is just type in the syntax. So it starts with f So within this you have
the test expression Now we’ve entered
the body of this if statement the body of the if condition is going
to contain all the statements that are going to be executed if the condition is met so this will contain
all the statements. Then the condition is met. In case this is not true
and if I have another if statement to be checked for I can make use of the else if statement so for that
I’ll just write LF and within this will be
test expression, too. So what I’m going to do
here is statements when the elsif condition is met
so in case both these conditions are not met which means if they are false. Okay, I’m sorry guys. I just made a mistake of here. Okay, so in case both these conditions are not met
I’ll have an else block. so else and then I’ll have
the final statement. So I hope you guys
have understood this. So now to show
an example of this. I’ll take another kernel
and over here. I’ll just take
some variable say x and I’ll assign
a value to this as 12 then I’ll check if the value of x of n divided
by 3 is equal to 0 or not. So for that I’ll just type in
if X Mod 3 Is equal to zero? I’ll just print. X is divisible by 3. else I’ll just copy
this print statement and I’ll say X is
not divisible by 3. So now let me just execute this. So as you can see over here, it says X which is
12 is divisible by 3. So in case I want to check
for this condition to be true for other Expressions, what I can do is just add
an else if statement so for that I’ll just say l if X Mod 5 equal
to equal to zero I’ll say print. X is divisible by 5. Let me just change
the value of x to a number that is divisible by 5. So now let me just
execute this program. So as you can see over here, I have a result saying
X is divisible by Phi now. Let me take a number that is
neither divisible by 3 or 5. So say for example 11. So guys the else block
has been executed. And since I have not made any
changes to this print statement, I have the output as
X is not divisible by 3. Let me just add on another part
of this statement. If X is not divisible by 3 or 5
and executed once again, so as you can see over
here the else block has been executed saying X
is not divisible by 3 or 5. Let me just explain this
once again to you all so what exactly is happening
over here is my variable X holds the value of 11 after this my program checks if the resultant of x divided
by 3 is equal to 0 or not. We all know that
if we divide 11 by 3, we’re not going
to get a whole number. Which means the quotient
is not going to be 0 and therefore 11 is
not divisible by 3. Then my program
will enter the else if block here. It will check if 11 divided
by 5 is equal to 0 or not as we all know
11 is not divisible by 5. And therefore this block
is also emitted. Finally it gets into
the else block and over here. It prints the final statement. So I hope you guys
are clear with this. So now let me
take another example. And check whether
a number is prime or not using the if/else block. So what I’m going to do here
is I’ll take a new variable as a and I’ll assign the value
to this as 10 and after this the first thing that I have to do is check if a is
greater than 1 or not. So what I’m going to do is type
in an if statement, so if a is greater than 1 then I’m going
to proceed with the program. So if a is greater than 1
for all the values that are present after one. I’ll have to check. if a mod x is equal to 0 or not, so for X in the range of a comma to which means starting
from do I have to go on till a I will check if a vortex. Is equal to 0
or not in case a mod x which means a divided
by X here the value of x is going to start from 2 and it will go on till 10:00
without including 10. So the values of X will start
from 2 and go on till 9 in case 10 is divisible by
any number present between two and nine then a is
not a prime number. So what I’m going
to do over here is just say print not Prime. This way I’ll be able to check
for all the numbers that are present
between 2 to 10. So once this is done, what I’m going to do is just
break through this Loop. In case this condition
is not met I’ll have to type in else where
in I’ll be printing that it is a prime number. So as you can see by internal if-else Loop is completed now
coming back towards this if statement which checks
if a is greater than 1 or not. So if a is not greater than 1
I’ll just print out. So if the value of a is not greater than 1
I’ll just inform the user that the value of a is not greater than 1 so
I’ll just say value. Of a is less than or equal to 1. So as you can see over
here the value of a that I’ve given over
here is 10 therefore. When I execute
this block of code. I get the output as not prime
which means 10 is not prime. So instead of 10 if I assign the value of a as 1
and execute this program, you see that the output says
value of a is less than or equal to 1 which means the final else block has been executed now
in place of this if I Supply some prime
number save three So as you can see over here when I supplied the value as
3 the output has returned saying that this is a prime number. So I hope you guys
have understood this. So now let’s talk
about the shorthand for if and else in case you just
have one statement to execute or multiple else
statements to check for you can make use
of the short and if and else so to show you guys an example
of the shorthand notation of if what I’m going to do
is take two variables over here say A and B, and I’ll declare a as five. and b is 7 No, I’ll make use
of the if statement. So if a is less than b. I’m going to print a is
less than b so previously if you’ve noticed
the body of the if statement is starting
in the next line with an indentation
in the shorthand notation, you can make use
of the statement to be executed in case the condition is met
with in the same line. So what I’m going to do is
just give a space over here and then I’ll say
print a is less than b. So as you can see over here, my if block has been
executed similarly. You can also have
a shorthand notation for the else statement. Now, this is going to be
a little different. What I’m going to do is first
type in print a is less than b If a is less than b. Else I’ll say print. a is greater than b So as you can see over here
since a is less than b the if block has been executed. So I hope you’ve
understood the else if blocks completely so now moving on towards the next
flow control statements that is the for Loop
and the while loop so what exactly is a for Loop a
for Loop is used to execute statements once for each item
in the sequence. The sequence can be
anything such as a list a dictionary a set or a string
for Loops basically have But the block for the iteration
statement is specified and then there is a body which is executed once
for every iteration in a for Loop. We actually have to specify
the number of iterations that must execute. So let me just get back
to my jupyter notebook to show you guys the syntax
of the for Loop before that. I’ll just create a heading. And I’ll say for Loop. So unlike other programming
languages the for Loop in Python is a for in Loop. So basically for
any iterator variable. in the sequence I will execute
the body of the for Loop that will have
all the statements. So for every iterator
present in this sequence, I can execute a single
or a number of statements. So just to show you guys
an example of the for Loop. What I’m going to do is
take an example list. I’ll just say my list. And I’ll give
some random values to it. We will be seeing
how to create lists and water lists further
on in the session, but for now, let me just create
a list over here. So in case you
guys are wondering what exactly is an iterator
a python iterator is a container having countable number of values values in a container can be traversed
using the iterators. So in my case, I’ve taken the list as
an iterator over here next. I’ll just open up the for
Loop and for every element that is present in my list. I’ll say print that element. So as you can see over here the for Loop Travis’s
each iterable one at a time and then it executes the body for all the iterables
present in my list. So therefore for
every element in my list, I’ll print all elements
one after the other. So now let me take
another example of here. I’ll say my string
equals hello world. So for every element
that is present in my string. I’ll print that element. Sorry guys, I
missed in over here. Okay, so as you can see
all the elements present in my string have been returned in case you want to retrieve
any specific element you can make use
of the index values. So what I’m going to do
is just copy this. And over here, I do not want to return
all the values. I just want to return the value
present at some index number say for example 3 and I’ll execute this. So as you can see it
index number three is L in case I want to selectively
print a section of the values present
in my string I can make use of the for Loop
and then slice the values that I want from my string. So for that I just copy this
and paste it over here and I’ll say print all
the values from You don’t agree. So as you can see
from index number 0 to index number three, I have all the values
that are printed. Please make a note over here that it starts from zero
goes on till 3:00, but it does not print the value
present at index number three similarly in case I
want to print all the values that are present after three. I can slice my string and print all the values
that are present after three. So as you can see, I’ve not given
any value after this: which means I want
to print all the values that are present
after 3 similarly. You can also make use
of negative indices. So I’ll just copy this
I’ll say print the value that is present at -3. So for our coincidence, the element that is present
at -3 also is L. That is because
this special character is present at minus 1 D
is present at minus 2 and L is present at -3. So I hope you guys
are clear with this. So now moving on towards the while loop the
while loop executes the set of statements as
long as the condition is true it consists
of Of condition block and the body with a set
of statements the while loop keeps on executing until the condition
becomes false. So in case you want to make
use of a while loop, you’ll have to keep
three things in mind. The first thing is
to initialize your iterator. The second is to
specify the condition and the third is to increment your iterator in case you
do not increment your iterator. The while loop
will go on forever. So what I’m going to do over
here is take a small example of the while loop
before writing down. Example, let me just write down
the syntax of the while loop. So that is while. some expression and
then the body which contains the statements. So now let’s move on
towards an example of the while loop. So I’ll take an iterator
over here say aye and I’ll initiate
the value of I 2 0 so now my iterator value
has been initiated. So this was the first rule
for my while loop, which is initializing
the iterator. The second is to
specify the condition. So I’ll just take in the while
loop I’ll specify the condition as while the value of I is less than 4 I want
to print the value of I And the last bit of this
is to increment the value of this iterator in case
I do not increment this it will keep on printing
the value of I infinite number of times. So I’ll just say I plus equal to one so
I’m incrementing the value of I by One each time. So now let me just execute this. So as you can see over here, it prints the value of I unless and until it is
less than 4 so therefore the initial value of I is 0
and the first value in In the output is 0 as well, the second value will be taken
by incrementing the value of the iterator
by one therefore. I becomes 1 and since I is
still less than 4 I’ll print I next I’ll increment
the value of I again by 1 which is 1 plus 1
and that will be 2 2 is 2 less than 4 therefore. I’ll print to after that. I’ll add 1 to 2 again, which will give pain
3 the value of 3 is still as then for I’ll print three and
then I’ll increment the value. By this time the value of I will be 4 so
4 is not less than 4 which means I’ll exit
the while loop. So I hope you guys
have understood this. So now moving on
toward something very important, which is the loop
control statements. I’m sure many of you
all would have come across the statement such as
break past continue excetera. These statements are nothing but the loop control statements
the loop control statements are used to control
the flow of the loop or to alter the execution
based on a few specific. If I’d conditions in Python, we have break continue and pass
as the loop control statements. The break statement is used
to terminate the execution of a loop containing it as
soon as the loop comes across the break statement
the loop terminates and the execution is transferred
to the next statement. We here I’ll say
for X in Ed, Eureka. If x is equal to R.
I’ll break the loop. Which means when I encounter are I want
to break through the loop and I do not want to continue
the loop for the rest of the elements then
I’ll just say print X. And finally, I’ll
just say print. Loop has ended. Sorry guys. I just made a mistake
in the indentation over here. So as you can see over here, all the elements that are
present are have been returned which means edu
have been returned and then the loop has ended. So now talking about
the continue statement. The continue statement is used to skip the rest
of the code in the loop for the current situation
the continue statement. However, does not terminate the
loop like the break statement and continues with
the remaining iterations. So basically when a continue
statement is encountered it only skips the remaining
Loop for Iteration, so let me just try
this out of here. So instead of break. I’ll say continue. So when R is encountered
the value is skipped which means when R is encountered the print
statement is not executed. So now talking about
the past statement. The past statement
is a null operation. It basically means that the statement
is required syntactically, but you do not wish to execute
any command of code. So in case if I change this
from continue to pass An add a print statement that is to be printed
when our is encountered. I’ll just say pass executed. So as you can see when R is encountered
this print statement is executed and then the loop
continues so talking about nested Loops python also allows us to use one Loop within another you can use a
for Loop in the while loop and a while loop
within a for loop as well. So to take an example of this. Let me just say for X
in range of 1 comma 10. And then I’ll take
another for Loop within this for Loop and say for y in the range
of x. I’ll say print X. So instead of ending
this print statement without next line character. I’m going to use the end
parameter as a space character. So for all the values
that are present between 1 and 10 each time I take
the value and in that range, I’ll print the value. So for the first time I’ll have
X as 1 so for one in the range of 1 I’ll say print 1 therefore 1 is printed just once
for the second iteration. I’ll have the value of x
as 2 so 4 2 in range of to I’ll print to which
means I’ll print to twice now since my output does
not look to formatted what I’m going to Is add
another print statement? To create some
beautiful-looking output. So as you can see over here,
I’ve created a number Pyramid. So I hope you guys
are clear with this. So now let’s take an example
of a nested while loop. So what I’m going to do
here is take a variable say a and I’ll assign a list
of values to this. So while a is true, what I’m going to do is
just print a DOT pop – 1 and then I’ll take
another variable within this while loop I’ll just say B is
equal to some list of string. Let me just take a symbol that will differentiate
between these two loops. So next I’m going to take
another while loop and I’ll say while B is true print. B dot pop of 0 so as you can see both my
while Loops have been executed. So I hope you guys
have understood this. So next. What I’m going to do is
print the Fibonacci series using both the for Loop
and the while Loops. I already have a code for that. So let me just copy it
and I’ll paste it over here. So as you can see what’s happening over
here is I’ve defined a Fibonacci function
and over here. I’m assigning the value of first
and second as 0 as 1 then while this is true. I’m going to use the value of f in case you want to know
more about the yield function. You can actually check out
generators in Python video. So moving on once the value of f
has been returned, which means if the first value
has been returned, I’ll have to change the value
of first as second and the second will be first. Second so therefore
first become second and second becomes f plus s next I’m taking the
for Loop for every element that is generated by my function
in case the value of x is less than 50. I’m going to print the value and I’m going to end it
with the space character in case the value of x
becomes greater than 50. All I have to do
is break the loop. So I hope you guys
have understood this. Okay. So now moving on to what’s
the next topic of this session which is Functions
in Python before discussing what exactly are functions
let’s figure out why do we need python functions functions
are used to manage inputs and outputs in a computer
program programming languages are designed to work on data and functions are
an effective way to manage and transform this data. The modifications are generally
done to drive outcomes, like performing tasks and finding results
the set of operations or instructions required
to do so comes from logically functional blocks that can be reused in Friendly
from the main program. In fact, the main code
is also a function just a very important one. Every other function
is logically aligned and maintain to functionally
execute your main code. But if the function
has not been defined previously, you’ll just have to Define one
yourself before using it this is because the function definition
lists the steps of operation. So basically functions
are nothing but toss that the user wants
to perform repeatedly by defining it just once
with an impenetrable. Name the user will be able
to use the function anywhere in the program. This not just reduces
the size of the code, but also helps in debugging. So now coming towards what exactly are python
functions the functions in Python are classic example
of such reusability. So to serve a wide range
of applications from GUI and mathematical Computing
to web development and testing python interpreter
already comes equipped with numerous functions that are always available
for the use Also, you can add on libraries
or modules to your program that contain predefined
functions readily available for use. So now talking about the types
of functions in Python. You can either have built-in
functions Lambda functions or user defined functions. So taking a look at each
of these in detail. So python built-in functions built-in functions
are nothing but the functions that are provided by
the python interpreter some examples of these are the print function the sum
function min max functions. Etc now, let’s get back
to our Jupiter notebook and see how you can make use
of these built-in functions. The first function
that I’ll be discussing over here is the print function. I’m sure you all know. The print function print
in Python is a standard function that is used to print
the output to the console. Let me just create
a heading over here. I say functions. The show the syntax
of this function to you guys. I’ll make use
of the help function. So help on the print function
will return all the documentation about
the print function in Python. So as you can see on the screen, the print function takes
a number of parameters value is nothing but a value
or a set of values that has to be printed
out to the console a separator will decide how the values are separated
from each other. The end parameter
is used to specify what is to be printed at the end
of the output default value for the end parameter is / n meaning next line next. Is the file parameter this
is an optional parameter which comes with
the right method the default value of this is Sister STD out flush is
also an optional parameter that is used to specify if the output has to be
flushed or is it to be buffered if the output has to be flushed
the Boolean value will be true and in case if it is buffered
the Boolean value will be false the default value
for this is false. So now let’s take a few examples
of the print function. So the first thing
that I’ll print is hello world. Next I’ll take the same function
and over here. I’ll print it as
two different strings. What I can also do is add
a plus sign in between this. So I’ll just copy this and
I’ll add a plus sign over here. So as you can see over here, the first statement is printing
the hello world string as it is in the second
statement over here. I’m printing hello world
as two different strings in the third example. I’ve made use
of a plus operator, which means I want
to add the first string to the second string. I can also add in
some numerical values say 2020 and then print it so here
it is Hello World 2020 now, what do you mean
by a separator in case you? To use the separator. What do you have to do is just
I’ll just take this example and instead of separating
them by a space. Let me separate them
by something else. So I’ll just say Separator
should be a – okay. So as you can see over here
in place of a space character. I’ve got a – so if I have two
print statements one after the other I can change
the end parameter from next line to something else. So what I’m going to do
is I’ll say hello world. at Eureka I did
the first print statement. I’ll make use of the N parameter
and I’ll specify it to be. A dashed line. So as you can see
over here the outputs of both these print statements
have been printed in the same line, but they are separated
with this dashed line over here. So I hope you guys
are clear with this. So now let’s talk
about the min max functions. The min max functions
are also built in functions that are present in Python. So in case I have
a list of values say a And if I want
to find the minimum and the maximum of this, all I can do is make use
of these functions directly. So I’ll just say
print the minimum of a and the maximum of a so as you can see over
here the minimum value that’s present in a is 1 and the maximum value present
in a is 7 another commonly used function of python
is the sum function. This function will return
the sum of all the iterables that are given as
a parameter to it. So I already have
my list over here as a I’ll just say some of a So
as you can see over here, it has summed up all
the values present in my list and it has returned the value
as 28 one more thing. I want you guys to note over
here is the sum function actually has two parameters now
to show that to you guys. Let me just type in help
for the sum function. So as you can see over here, the sum function
has two parameters, which is the iterables and the start iterables
over here is the sum of iterables whose value
needs to be summed up. The second parameter
is the start parameter, which is by default
0 meaning the sum of all the iterables
should be added to 0 so it will be 0 plus sum
of all the iterables in case I specify some value
to the start parameter. It is going to add
that Value Plus the sum of all the iterables. Find the previous example
if I have some of a comma 10, it’s going to add 10 + 28
and return the amount. So as you can see over here,
it has returned 38, which is 28 plus 10. So I hope you guys
have understood this. So now moving on
towards the next topic which is python
Lambda functions python. Lambda functions are functions
that do not have any name. They are also known as Anonymous or nameless functions the word
Lambda is not a name, but it is actually
a keyword this keyword. Suffice that the function
that follows is anonymous. How do you actually write
Lambda functions in Python? A Lambda function is created
using the Lambda operator. Okay. So now let me just type in
the syntax of a Lambda function. So basically it will be
the Lambda keyword followed by the arguments. And then the expression. Hear arguments are nothing
but the variables that are used with
this Lambda function expression is a mathematical expression that will decide the output
Lambda functions can take any number of arguments, but they can take just
one expression arguments can be anything starting from zero and there is no upper limit just
like any other function. It’s perfectly fine to have
Lambda functions with no inputs. So now let’s just take
an example of a Lambda function. So I’ll just take a variable a And now I make it was
up the Lambda function. I’ll just take one argument
for the first example. I’ll say Lambda X return
X star X and now I’ll say print a of three. So as you can see over here, I’ve got the output for X star X when a is 3 and therefore
the output is 9 let me take another example
with two arguments. I’ll just save be equal
to Lambda X comma y And I’ll say print X Plus y. A posse parameters for X
and Y as one and two. I’m sorry guys instead of taking B of 1 comma
2 I took a of 1 comma 2. So here it is. I found the result for X Plus Y
which is 1 plus 2 and that is 3 so I’ve taken
two examples of here. But please make note that both the functions
have a single expression. So now moving on towards
user-defined functions functions that we Define ourselves
in a program in order to achieve some tasks is referred
to as a user defined function. So the Syntax for user-defined
function will be the d e f keyword followed by
the name of the function. and then the arguments
you can have any number of arguments guys, then comes the body
of this function the body of this function can have
any number of statements. So I’ll just write
statement 1 2 Etc. So as you can see over here, the first thing
that you have to do is make use of the daf keyword. Now this keyword will actually
help you to Define your own function. Next up will be the name
of the function. Then you’ll have to pass in
the arguments of your function which can be any number
of arguments starting from zero. The body of the function
can have any number of statements in them. So now let’s move on and create
some user-defined functions. So what I’m going to do over
here is create a function to add two numbers. So I’ll just say
Define add function. I’ll specify the parameters as X and Y and I’ll just say
the sum is equal to X Plus y. And I’ll return
the value of sum. After this, I’ll just have
to call the add function and pass the values of X and Y. I’ll say 2 comma
4 and I’ll run this. So as you can see over here,
I have the output for 2 plus 4, which is 6 so now
that you’ve seen what our user defined functions
as well as Lambda functions. Let’s take a small example
wherein you can make use of the Lambda function
within a user-defined function. So what I’m going to do
is Define a function over here say new function. And to this I’ll just give
one parameter as X now. This function will just
return one statement. That is the Lambda function. The arguments for this function
will be Y and I’ll specify the expression as X star y. Now
take a new variable safety. And now I’ll have to call
the user-defined function which is no function. What I’m going
to do is just print. the of five so
as you can see over here, my function takes one parameter
as 3 so X over here is 3 and Y is 5 now if you’re wondering
why 3 is assigned to x + 5 is assigned to why this is because I’m specifying
3 as the parameter of my new function then
within this function. I’ve made use
of the Lambda function which takes one parameter. Therefore this parameter
is supplied the value as 5. So I hope you guys
are clear with this. Phenom moving on to what’s the next very
very important topic of the session which is
data structures in Python. So what exactly is
a data structure data structures allow you to organize
your data in such a way that it enables you
to store collections of data relate them
and perform operations or them accordingly
python has implicit support for data structures, which enables you to store and access data
these special data structures of python are nothing
but lists dictionaries tuples and sets python are Allows its users to create their own
data structures as well. The most prominent data
structures of this section are Stacks queues
trees linked lists, and so on. So now talking about
the built-in data structures of python lists lists
are used to store data of different data types
in a sequential manner. There are addresses assigned
to every element of a list which is called as
the index the index value that starts from zero
and goes on until the last element is called
as the positive index negative. I think starts from minus 1
that is from the last element that is present
in your iterable. Therefore positive indexing
starts from the left hand side and moves towards
the right hand side. Whereas negative indexing starts
from the right-hand side and moves towards
the left hand side. So now let’s get back
to our Jupiter notebook and see how to create lists in Python. To create a list in Python. You can make use of the square
brackets and add elements in it accordingly in case
you do not pass any values. The list is going to be empty. So I’ll just take
a new list over here. And I’ll assign
some random values to it. Okay, I’ll take another list. I’ll say my list. And I’ll not assign
any value to this. Then I’ll just print
the values of booties. So as you can see over
here new list contains a list of elements
starting from 1 to 7, and my list is empty not just this you can also make use
of the list function to create a list. So I’ll just take
a new variable say and I’ll say this is equal
to list of I’ll specify an empty list now. Let me just print out a So
as you can see over here, I’ve created an empty list
making use of the list function. Now since lists are mutable. You can add on values or delete values from lists
in order to add elements to a list you can make use
of the append extend or the insert functions. The append function
can be used to add a single element to your list. The extend function
will add a number of elements one after the other
to the list the insert function on the other hand will insert
an element at a specified index In your list. So let’s take an example
of all these three, so I’ll just say a DOT append. And I’ll specify
a value say three. next to new list I’ll add
on a number of values. I’ll send you list of extend. And I’ll give a list of values. Also, I’ll make use
of the insert function. I’ll say new list dot insert. I want to insert the value at position 2 and here
I’ll insert a string. I’ll say Eddie Rekha. Now, let me just
print all these. I’ll print a so as you can see
previously a was empty, but after making use
of the append function, I’ve added 3 to my list. So now in case I want to see
what’s present new list, I’ll just say print. New list so as you can see 9 and 10 have been added
to the end of my list and Ed Eureka has been inserted
at index number 2. So 1 is at index number
zero to at index number one and Erica is present
at index number two. So that was about adding
elements to a list elements can also be deleted from a list in case you want
to delete elements from a list. You can either make use
of the pop function the remove function
or the delete key word. Now, let’s take an example
of each of these. For my new list
what I’m going to do, I’ll say new list Artpop. of two and then I’ll say
prince new list. As you can see
over here the value that’s present at index. Number two has been removed from my list over
here on the other hand. If you make use
of the remove function, you have to specify the value
to be removed instead of the index number. So here I’ll just say
new luster to remove. And I’ll specify
the value to be 10, and then I’ll just print it. So as you can see again is
not present in my list anymore. Similarly if I make use
of the DL keyword, I can delete some element that is present
at some particular index number. I’ll say delete new list of five
and then print new list. So as you can see over here, the element that is present at index number five has
been removed elements of a list can also be accessed
by using the for Loop. So in case I want
to access all the elements that are present in new list, all I have to do
is use the for Loop. So for X in new list, I’ll say print X So
as you can see, all the elements
have been returned one after the other you
can also slice the new list from some particular range. So I’ll just print
all the elements that are present
after index number 2. Sorry guys. I said I want to print
all the elements that are present after two
but here I specified: 2 which means I’ve printed
all the elements starting from the index number 0 to 2. There are a number of other functions
that you can also use which is the Len function
to find the length of the list the index function which finds the index value of
the value passed as a parameter and the first occurrence
of it the count function which finds the count
of the value passed to it. You can also sort the list
by making use of the salted or the Functions Phenom walking
on to what’s the next data type which is a dictionary
dictionaries are used to store key value pairs
to understand it better think of a phone directory where hundreds and
thousands of names and their corresponding numbers
have been saved here names will be the keys and the phone numbers will be
the value pairs for that keys. In Python dictionaries can be created using
the flower brackets. You can also create
them by making use of the dict function. So now let’s get back to
our Jupiter notebook and create some dictionaries over here. I’ll just take a variable say a and then I’ll open
the flower brackets. I’ll take the keys as numbers. So I’ll just say one is
hello and to is world. and then I just print
out a in case you do not specify any parameters and empty dictionary
will be created. So I’ll just take an example
of that over here. I’ll say B is equal to flower
braces and then I’ll print be So as you can see over here and empty dictionary
has been created. You can also create dictionaries
by using the dict function. So I’ll just say
C is equal to ticked off and I’ll copy the same thing. So as you can see over here, I’ve created a dictionary by
making use of the dict function. So now let’s move on and see how you can actually
change and add values to dictionaries values
of a dictionary can be changed by using keys. So the first thing that you’ll have to do
is access the key and then change
the value accordingly. So in case I want to change
the value of to of here, what I’m going to do is see
of to Is equal to Ed, Eureka? And then I’ll print see. So as you can see over here, the value for two has been
changed from world to add Eureka in case you want to delete
items from a dictionary. You can make use
of the pop function the pop item function
or the clear function. The pop function
will return the value that has been deleted. The pop item function
will return a couple of the key and value to clear
the entire dictionary. You can make use
of the clear function. So, let me just show
you an example of this. I’ll say CDOT Bop. The value present
at index number one. So as you can see over
here hello has been removed from my dictionary
in case I make use of the pop item function. It’s going to return
both the key value pair. So as you can see over here, the pop item function has
returned both the key value pair that has been removed. Now. My dictionary is empty. In case I want to delete
this dictionary I can make use of the clear function. So I’ll just say CDOT clear. My dictionary is already empty and I don’t actually have
to make use of this function. I just typed this function
over here to show you guys how to use it the values
of a dictionary can also be accessed now to access the
values you’ll have to make use of the keys only to do this. You can either make use
of the get function or directly specify
the name of the key. I just create a new
dictionary over here. I’ll say d is equal
to 1 is to python. To is to Java. And three is to see shop. Now I want to retrieve
the values that are present for the keys 2 & 3. So what I’m going to do
is use the print function and within this I’ll specify
the name of my dictionary. And then in the first example, I’ll just specify the key value
and at the next example, I’ll make use
of the get function. I’m sorry guys. I’ve not executed
this line over here. So as you can see over here, the value present
for key to is Java and that for Key 3 is C Sharp. So now talking
about stubbles chapels are actually the same as list, but with one exception that the data once entered
into a couple cannot be changed no matter what to create
a double in Python, you can make use
of the regular brackets. You can also create
a double by making use of the double function. I’ll just create my Tuple. And in the first case, I will create
an empty Tuple next. I’ll create another Tuple say Z and I’ll assign
some values to this. I’ll take another variable Y
and then I’ll make use of the Tuple function
in order to create a table. Now, let me just print
out each of these. I’m sorry guys, I’ve missed regular
braces over here. So as you can see an empty Tuple
has been created followed by a tuple using the regular braces and then a tuple making use
of the tubule function. Like I’ve already told y’all before you cannot alter
the values of a tuple. So in case I use any function such as
an append function with a tuple, it’s going to throw an error. So now just to show
this to you all. I’ll say y dot append
I’ll say three So as you can see over here, it has thrown an attribute error
saying pupil object has no attribute append in case you want to access
the elements of a tuple. All you have to do is
make use of a for Loop. So I’ll say for I in why Print I so as you can see all
the elements of the Tuple have been retrieved. One more thing. I want you guys
to note over here is that in case you want to add on some elements you can make
use of the plus operator. So now moving on
towards the next data structure, which is a set sets
are a collection of unordered elements that are
unique meaning that even if the data is repeated
more than one times while specifying the values
the set data pipe is just going to take one entry
of that element. Sets and bitin actually resembles the sets that
you’ve learned in arithmetic. The operations are also the same
such as intersection difference Union Etc to create
sets in Python. You can make use
of the flower braces and instead of adding
the key value pairs. You just have to specify
the values now. Let me just get back
to my jupyter notebook and show you all
how to create sets and bison like I’ve already
told you all sets are created by making use
of the flower braces. I’ll just take a variable
over here as Oh. And to this I
specify some values. And then I’ll just
put in the set. So as you can see over here,
I supplied eight values but only five values
are present in my set. Now this is because I
have three repeated values and those values
are not printed again in my set in case you want
to add some values to a set you can make use
of the add function. So I’ll just say x dot add. And I’ll pass the value as 8. Now let’s just print out X
so as you can see over here, it has been added to my set. You can perform many operations on set such as intersections
unions different Etc. Now, what I’m going to do
is Define another set over here and say why Y is equal
to Some random values. So in order to find
the union of X and Y I can either use
a pipeline character or make use of the Union
function in case I just want to find the union of X and Y I’ll say Z is equal to X pipeline Y
and then I’ll print see. Sorry, I’ve taken
cap see over here, which is small actually. So as you can see, I have the union of X
and Y present in Z similarly. I can also make use of
the Union function to do that. I’ll just make use of the print function
and then I’ll say x Union y equals x dot Union of Y. So as you can see over here, I Found the Union of X and Y similarly you
can also make use of the difference function
the intersection function Etc. So let me just change
this from a union to intersection and
see what happens. So as you can see over here, I change the value
from Union to intersection and therefore I found
the intersection of X and Y. I just have to make
a change over here as well. Okay, so I hope you
guys are here with this. So that was about the built-in
data structures of python. Now moving on
towards the next topic which is user defined
data structures user-defined data structures can either be
arrays Stacks queues trees linked lists graphs or hashmaps and array is
basically a data structure which can hold more
than one value at the same time. It is collection or ordered series
of elements of the same type. We can Loop through the array
easily and fetch the required. By just specifying the index
numbers arrays immutable which means they are changeable. And therefore you can perform
various manipulations operations as required Stacks Stacks
are linear data structures, which are based
on the principle of last in first out here the data which is entered last
will be first to get accessed. It is built using the array structure and has
operations namely pushing or adding elements popping
or deleting elements and accessing elements
only from one point. It in the stack, which is called as the top
this top is the pointer to the current position
of the stack stack. So prominently used in applications such as recursive
programming recursive word under mechanisms in
word editors under mechanisms inverter tutors and so forth Q’s a q is also
a linear data structure, which is based on the principle
of first in first out here the data entered first
will be accessed first. It is built using the array
structure and has operations which can be performed
from both ends of the queue. That is the head. Or the front back
operations such as adding and deleting elements
are called as enqueue and dequeue cues are used as Network buffers for traffic
congestion management used in operating system
for job scheduling and many more trees trees are
nonlinear data structures, which have roots and nodes. The root is a note from
where the data originates and the notes are
the other data points that are available to us the note that precedes
the parent and the note after is called as a child. Levels of a tree shows
the depth of information. The last notes are called as the leaves trees
create a hierarchy which can be used in a lot
of real-world applications such as HTML Pages which use trees
to distinguish tags that come under the same block. It is also efficient
in searching purposes and much more linked lists linked lists
are linear data structures, which are not
stored consequently, but are linked with
each other using pointers. The node of a linked list
is composed of data and a pointer called. These structures are most widely used in imaging applications
music player applications. And so forth graphs graphs are
used to store data collection of points called as vertices and edges graphs
can be called as the most accurate representation
of real world map. They are used to find
the various cost to distance between various data points
called as the notes and hence find the least path
many applications such as Google Maps Uber and many more make use of graphs
to find the least distance and increase Profits
in the best ways hashmaps. Hashmaps are the same as
what dictionaries are in Python. They can be used to implement
applications such as phone books populate data according to lists
and much more. So now let’s move on and discuss
what our algorithms in Python algorithms in general
or rules or instructions that are formulated in
finite sequential order to solve problems and get
the required results. They give a pseudo
code for problems and can be implemented
in several languages as they are not language. Pacific so how do you
write algorithms algorithms are generally written
as a combination of user understandable language and some common
programming languages. They are commonly
written in steps. However, it is not always
necessary to do so, there are no distinct rules
to formulate algorithms, but you will need to keep
the following points in mind figure out what exactly
is the problem that the Mind where you need
to start and determine where you need to stop formulate
the intermediate steps and finally review your steps. Let’s say for example, if you have to formulate
an algorithm to check if a student has passed
in an exam or not. So after you start you’re going to declare
two variables say X & Y and then you’re going
to store the marks obtained by the student in X and store
the minimum passing score in y then check if x is greater
than or equal to Y if yes, then return pass
else return fail, and finally you have
to stop so this was an example of an algorithm to check if a student has
Our field however, you can manipulate the steps
according to your preference. For example, you can assign
values to both variables X and Y in a single step
rather than two steps. So this way a single problem
can have multiple Solutions and it depends on the problem and the programmer
to choose the most feasible and the reliable
solutions for now. Let’s understand what are the elements of a good
algorithm good algorithms need to have finite clear
and understandable steps. There should be a clear. And precise description
of the inputs and the outputs each step
needs to have a defined output that depends only
on the inputs in that step or the preceding steps. The algorithm should be flexible
enough to mold it and allow a number of solutions the steps should make use of
General programming fundamentals and should not
be language-specific. So what are the classes
of algorithms available for programming? The three major algorithm
classes are divide and conquer class
dynamic programming and greedy algorithm class
the divide-and-conquer class divide the problem into sub path and solves each of them separately talking
about dynamic programming. It divides the problems in two subparts remember
the result of supports and then applies
to the similar ones this way there will be
lesser number of repetitions when compared to divide and conquer method greedy
algorithms involve taking the easiest step file. Being a problem without worrying about the complexity
of the future steps. So now let’s move on and take a look
at the important algorithms such as tree traversal algorithms searching
algorithms and sorting algorithms in Python. The first that
I’ll be discussing in this session is the tree
traversal algorithm trees in Python are non linear data structures
having a root and note as mentioned earlier
tree traversal refers to visiting each node present
in a tree exactly once in order to update or check them. So based on the order
in which the nodes are visited there can be
three types of traversals that is the pre-order traversal
in order traversal and the post order traversal
before moving on in detail about each of these algorithms. What I’m going to do is jump
onto my jupyter notebook and create the tree
that you see on your screens. I’ll create a heading over here. and I’ll just call
it as tree traversal. So like I’ve already told
you all I’ll be writing down the code to create
a tree first so for that, let me just create a heading. This would be an H2 so
I’ll have to have two hashes and then I’ll just
say creating the tree. So the first thing
that I’m going to do over here is defined a class
called node so class. Note in case you have
any issues understanding classes and objects. Don’t worry guys, because I’ll be discussing each
of this in detail later on in the session. So for now and just defining a class within which
I’ll Define a function that is the init function. And then the
mandatory parameter, which is self followed
by the value parameter. Now this self parameter
will allow me to access the variables of this class
all the details about classes and the self parameter. I’ll be discussing
later on in this session when we go on to oops in Python. So for now I’ll just
write self dot left child or I’ll just say L C equals none
now specified none over here because right now there
is no leftfielder. Child the first value should
be assigned to the root note and therefore the left child and the right child
will contain nothing and therefore I’m
assigning none to it. Similarly. I’ll write self dot
right child equals none. finally self dot node data
will be equal to Value. So now let’s move on and create
an object of this class. So I’ll just call
the object as root. And you guys have seen already that in my tree the first root
or the root node is 1 so therefore I’ll
assign a value as 1 so once this is done, I’ll have a root note
that is created. So once the route
is created I’ll have to create the left child
and the right child for it. So now what I’m going to do
is Route dot left child will be equal to note of to and router try child
Will be node of 3, so in might read the note
2 has another left child and the right child. So to create that what I’m going
to do is write down Route dot left child dot left child
is equal to node 4. And finally the right child
of node 2 will be Note 5. So I hope you guys
are clear with this. Let me just draw this
and explain this to you guys. So at the first step
I’ll have my root note that is created and
that will be node one. Following that I’ll have
a left child for this node, so that will be 2. And then a right child
will be created for one. That is 3. Not again not to
will have to child’s that is left child will be
Note 4 and right child will be node 5. So I hope you guys are clear
with this concept. So now I’ll just
erase this drawing. So now let’s move on and see how you actually do
the inorder traversal in order traversal refers
to traversing the tree in such a way that your first visit the left
notes followed by the root. And then the right notes you
begin your traversal from the all the nodes
in the left sub tree then move towards the root and finally the right subtree. So if I have to implement
the inorder traversal over here, what I’m going to do
is I just create a heading I’ll say in order. Since I have my base tree
already created over here. All I’m going to do is just
Define the in order function. So the first step
of inorder traversal will be to Traverse through the nodes
present in the left subtree. Then at step two. I’ll have to visit the root
and finally the right subtree. So now I’m going
to define a function. I’ll call it as in order and then I’ll pass
the parameter as root. So if a value for
the root is present, what I’m going to do is call the
in order function and over here. I’ll first visit the left child. So roots of left child and then I’ll print
the value for the root. Okay. Sorry guys, if route
I just miss that. So once I visit the left
child and the route, I’ll have to visit
the right child. So I’ll say in order. of root right child After this, I’ll have to call the
in order function. So in order of route. So as you can see over here, the inorder traversal
starts from for moves on to node 2 then 5 1 and 3. So in this tree over here
for the inorder traversal, it will start from
the left most node. The left most node
in my tree is node 4 so it will start from for then
it moves towards the root and finally the right node. Once I’m done with
for two and five. My left subtree is completed. Once the left
subtree is completed. I’ll have to apply
the same formula again. Left root, right. So therefore I will visit Node 1 which is the root node
and finally visit root 3, which is the right node. So I hope you guys
are clear with this. Okay. So now moving on
towards the next reversal, which is the
pre-order traversal. In a pre-order traversal
the root node is visited first followed by the left subtree
and then the right subtree. So in this example over here, I’m going to visit
the root node first, which is 1 then come
towards the left subtree following which I’ll move
towards the right subtree. Therefore. The order will be
1 2 4 5 & 3 now, let’s get back
to our Jupiter notebook and implement this so
I’m going to define a function that is pre order function. And I bought fruit
as a parameter. So if root is present. I’ll have to first print
the value of the root note. So root dot Note data. And then I’ll have
to do the pre-order. For the left child first,
so root dot left child. And then pre-order
for the right child. So once this is done, I’ll just have to call
the pre-order function. So as you can see over here, the first note to be visited
is the root Node 1 followed by the left sub child to and then
the left sub child of 2, which is 4 and then
I’m going to visit root 5, which is the right sub child
of do and finally when the root and the left subtree is done. I move on towards
the right subtree which contains three. Okay. So I hope you guys
are clear with this. Okay. So now let’s move on towards the next topic which is
the post order traversal. The post order traversal begins
from left then to the right and finally the root. So in this example of here, what I’m going to do
is start from for move on towards 5:00 to 1:00 and 3:00. Now, this is because 4
is the leftmost child and at this stage. I applied the Left
Right route formula. So my program is going to check if I have a right node towards the right of 4
is 5 I Traverse five or I visit five and then when Completed four and five
I go to the root node, which is 2 once the left
subtree is completed. I have to move
towards the right subtree and then Traverse it
in my right subtree. I just have one node, which is three and after I
complete the left subtree and the right subtree. I apply the Left Right
route logic again, and then I visit the root
which is 1 okay. So I hope you guys
are clear with that. So I’ll get back
to my jupyter notebook in order to implement this so what I’m going to do
is just Define a function. That is pre-order. Sorry post order. And I’ll pass route
as the parameter. The first thing that I’ll have to do is visit
the leftmost child so for that I’ll just do a post order. For the left child, so root dot left child
and then post order of the right child. Finally, I’ll have to print
the value of the root note. Once this is done. I’ll call the
Post order function. And I’ll pass route
as its parameter. Seems like I’ve made a mistake. Sorry guys. I just miss the
if statement to check if root is present or not. If the value for root is present
only then I’m going to do this. So, let me just type in a tab for all this
and then I’ll execute it. So here it is. My left subtree is visited
first Then followed by the right subtree and then one
which is the root node. So I hope you guys
have understood all of these three traversals. So now moving on
towards the next algorithm. That is the Sorting. Guard them’s sorting algorithms
are used to sort data in some given order sorting algorithms can be classified as
mod sort bubble sort insertion, sort selection sort
or shellsort. Please make a note
guys that in this session. I’ll be sorting everything
in the ascending order. So the first algorithm
I’ll be discussing in the session is the merge sort algorithm the merge
sort algorithm follows. The divide-and-conquer role
here a given list of items is first divided
into smaller lists. Until it reaches a point where each list consists
exactly of one item by default a list consisting
of one item will be sorted and the merge sort algorithm
then Compares adjacent lists and reorders them
in the desired sequence. This process is done recursively
until it reaches a point where there exists
only one sorted list. So I’ll just create
a heading over here and I’ll call it
as sorting algorithms. I’ll just say something. The first algorithm
I’m discussing as much thought so I’ll just
call it as much salt. I already have
the code ready for this. I’ll just copy that code
and explain this to y’all. So as you can see on the screen, the first thing that I’ve done
over here is defined a merge sort function. Now this function will check if the length of the list
is greater than 1 or not. And if this is true, it’s going to divide
the list by to following that I’m going to sort both the left half and the right
half recursively finally. I’ll create a new list that
contains the sorted elements. So over here, I’ve called an M list function
but have not defined it. The next thing I’ve done over
here is defined an MLs function. The first thing that I’ve done
over here is defined. What is the left list? And what is the right list left
list contains all the elements from the left to the middle and the right list
contains all the elements from middle to right? Following that I’ve taken
three variables Ki and J where K is the temporary
variable that I’m using to fill on my final list. I and J over here help me
to iterate over the left list and the right list. So while my left list and my right lists
contain elements, that is when they’re not empty. What I’m going to do is if the value of the item
present in the left list, if the value of the item
present in the left list is less than or equal
to the value of element that is in the right list. I’ll place the element present
in the left list into my sorted list else. I place the item
present in the right list in my sorted list. So if the value of left list
of I is less than the value of right list of J.
I’ll place the value of left list of I into K else. I’ll place the value
of right list of J into my list of K. If I place the value
of left list of I into my list of care. I’ll have to increment
the value of I or else if I place the value
of right list of J into my list of K. I’ll have to increment
the value of J. Finally when I’ve placed some value for K. I’ll have
to increment the value. Get as well. So once I have
all sorted elements, what I have to do is start
merging my list the final part of my program will help me
to merge the sorted sub lists. So if there’s
any element present in the left list and it is less
than the right list. What I’m going to do is place
that element into my new list, then I’ll increment the value
of I and I’ll increment the value of K when the left
lists are completed. I’ll have to place
the values present in the right list into k. Once all the elements
of right are placed into care. My list is completed. So in the final part
of this program, all I’m doing is assigning
some value to my list here in this program. I’ve made use
of the input function. Now this function will allow
the user to specify some list since the user will provide
a sequence of elements. I’ll have to split them and for that I’ve made use
of the split function and for all the elements
present in my list. I’ve converted them
to the integer. Finally, I called the merge sort function to which
I pass my list as a parameter and the value of left is 0 which is the first index value and the value of right
is the length of my list. Finally. I’m just printing
the sorted list. Finally, I’ll have to enter
the list of values. So I’ll just pass in
some random values. So as you can see over here
by sequence is for split and then it’s converted
to integers and then finally by calling
the merge sort function. I have created a list
of sorted elements. So now moving on towards
the next sorting algorithm, which is the bubble sort
algorithm the bubble sort. Algorithm is a comparison
algorithm that first Compares and then sorts adjacent elements
and if they are not in order, it’s swaps them. This process is repeated
n minus 1 number of times where n is the length of the Just say I already have
a program for this. I’ll just copy it. So the first thing that I’ve done over here is defined
a bubble sort function. So like I’ve already told
you all for every list. I will have n minus 1 iterations where n is the length
of the list in this program. I’ve named my list as a and the number of iterations
will be length of a minus 1 and the value of that is stored
in the variable B. So for every element
that comes in the range of be I need to take
that element and check if that element is greater
than the element. Let us present towards the right
of it in case that is true. All I have to do is
swap the elements. So a of Y will become a of Y plus 1 and a of Y
plus 1 will become a of Y. So as you can see I’ve given
some random list over here. In this case. The length of this list is for therefore the value
of B will be three. So for every element
present in the range of three, I’m going to take
that element and check if that element is greater than the element that is present
towards the right of it. So First I’ll take 3 and check if it is greater than 6
since 3 is less than six. I’m not going to swap
these values next. I’m going to check
if 6 is greater than 1 or not. So as we all know 6 is greater than 1 I’m going
to place the value of 1 if place of six and I’ll Place six
in place of one there for six and one will be swapped and my list will contain
three one six and then 8 for the next iteration. I’ll check if 6 is
greater than eight. So as we all know 6 is
less than 8 therefore, they will not be swept. So for the second iteration, my list is going
to look like tree. 1 6 and 8 next I’ll have
to perform bubble sort on this list again. So what my algorithm
will do is check if 3 is greater than 1 or not since 3 is greater
than one the position of one and three will be swapped. And my list is going
to look like one 3. six and eight In
the next iteration, my program will check if 3 is greater than 6
or not as we all know that these are already sorted. They will not be swapped. This process is repeated until the complete
list is sorted. So I hope you
guys have understood the bubble sort program. Okay. So now moving on
towards the next that is the insertion sort. Insertion sort picks up
one element of given list at a time and places it
at the exact place where it’s supposed to be. Since I already have
a program for this I’ll just copy it over here
and explain it to you guys. So what I’m doing over
here is first I’m going to place my pointer
at the second element. So whatever input list I have I’m going to place
the pointer at the value that’s present at index number
one now to do that. I’ve made use of this for Loop. So for X in the range
of 1 comma the length of a which is the element present
at to and throughout the length of a I’m going to set
the element present at a of X as the key and then I’ll store
the value of J as x minus. So in the first case
k will be 56. So 56 is going to be care. And Jay will be 1 minus 1
so that is zero. So in the first case
k is 56 and J is 24. The next thing
what I’m going to do is check if J is greater than or equal to 0 here in the first case
J is equal to 0. So therefore the first half
of the while loop is true. The next thing to be checked
if K is less than a of J. So if the value present at K, which is 56 is less than
the value present at a of J, which is 24. I’ll have to
swap these elements. Since these elements
are already sorted I’m not going to do that. Now. Once that is done. I have to decrement
the value of J. Now this step is done because I’ll have
to check the value of the key with all the elements that are present in the left
sub list in the first case. I just have one element
present towards the left side, but for the second iteration,
I’ll have two elements present. So therefore if I
do not discriminate the value of J. I’ll not be able to check
this value with all the values that are present towards. The left of it. So finally, I’ll place the value
of a of J plus 1 into K. And then I’ll move on
towards the next value of x which will be index number two. So I’ll just Erase this
after the first iteration of my program. The list is not going to change since the value of 56
is greater than that of 24 for the next iteration. I’m going to make one
as the key element since its present
at index number 2, so 1 will become the key. And the value of J
will be 56 and 24. Now. This is because 56 is present
at a of 1 and 24 is present at a of 0 and for all the values that are greater than or equal
to 0 I’ll have to check if the value of the key
is less than that or not. So I know that 1 is
less than 56 and 24. So what I’m going to do
is just remove one from here. Repeat and then I’ll Place
56 in place of one. I’ll Place 24 in place
of 56 and finally, I’ll Place one in place of 24. So for this iteration, I’ll have one followed
by 24 and then 56 which is my sorted list. And for the unsorted list. I have 50 and 17. So once this is done, what I’m going to do is take
the next element as the key which is 50 when 50
becomes the key. I’ll have to check this key
with all the values that are present
in the left side. So I know that 50 is less than 56
but greater than 24. So therefore I’m going
to shift 56 to the place of 50 and I’ll Place
50 in place of 56. After this, I’ll have
a list looking like one followed by 24 then 50. And 56 and then my unsorted list
I’ll have just one element which is 17. Finally. I’ll take 17 as the key and then
I’ll check this key value with all the elements
present towards the left of it since I know
that 17 is less than 56. It’s less than 50
and less than 24. What I’m going to do is Place
56 in place of 1750 will come to the position of 5624
will come to the place of 50 and finally 17. BPS in the place of 24 So I hope you
guys are clear with this even though there’s a lot
of mess on the screen. I hope you guys
have understood this. Okay, so let me just erase that for now and
execute this problem. So as you can see on the screen, I’ve sorted my list
in the ascending order. So now moving on
towards the next which is the selection sort
algorithm the selection sort algorithm divide
the given list into two halves, but the first half
will be sorted list and the second half
will be an unsorted list. At first. The sorted list is empty and all the elements are present
in the unsorted list the selection sort
algorithm will look at all the elements
present in the unsorted list pick up the element that is supposed to come first and then it Is it in the sorted list the step
is then repeated by searching the least element present
in the unsorted list and it places it
next to the first element of the sorted list. So in my examples since I’m arranging
all the elements in the ascending order
if I have a list of numbers, I’m going to select
the smallest element and then place it
as the first element of the sorted list. Then I start to search
for the next smallest element and place it towards the right
of my first element. So I’ll get back
to my jupyter notebook in order to implement this I
have the code for this already. So just copy it. So initially what we
have to do is assume that the first element is
the minimum element then what I’m going to do is check if the first element is less than any element present
towards the right of it. So in my list, I’m going to first assign
34 as the minimum value. So what I’m going to do
next is I’m going to check if the value of this minimum
is less than any value that is present
towards the right. So for every element that is present towards
the right of minimum that is X if that value is less
than the minimum value. I’m going to place the value
present at my array of Y as the minimum value. So for the first iteration, I’m going to check
if 23 is less than 34 or not since 23 is
less than 3423 becomes minimum. So after the first iteration 23 becomes the minimum value
next I’m going to check if one is less than 23 since 1 is less than 23 one becomes
the minimum value. So after the third iteration, I’ll have one as
the minimum value. Finally, I’m going to check if one is smaller
than 67 or not. Since one is smaller
than 67 the minimum value will remain one finally. I’ll check if one is
smaller than 4 or not. Since one is still smaller
than for the minimum value is 1 so the first
element to be placed in the sorted list
will be won by sorted list will have one and unsorted
list will have all the elements which is 34. 2367 and fork So this
is my sorted list. And this is my unsorted list. So once this is done, I’m going to repeat
the same thing for the unsorted list again
at this time 44 will be minimum and after the first iteration
23 will become minimum and for the third iteration
23 is going to remain the minimum following which I’m going to check if 4 is less than 23 or not since 4 is less than 23 4
is going to be removed from the unsorted list and it will be placed
towards the right of 1 which will become
the second element present in the sorted list. So I hope you guys
are clear with this. I just erase all these drawings
and I’ll run my code. So as you can see over here, I have a sorted ascending
order list now moving on towards the next algorithm, which is the shell sort
algorithm the shell sort algorithm allows
you to sort elements that are apart from each other
the original sequence of the salt elements
follows the N by 2 and by for sequence where n is the number of elements present
in the unsorted list, for example, if you have a list
of eight elements, the length of that list
will be divided by 2 for the first iteration as we all know 8 divided by 2 is 4 Now the first element
will be compared to the element that is present at index number
4 and then the Gap will be produced by dividing 8
by 4 this time the Gap will be 2 and the elements present
at these intervals will be compared. Finally. I divide 8 by 8
which gives me the Gap as one so when I have the Gap as one I
compare adjacent elements and the final list
will be sorted. So what I’m going to do over
here is just copy a code that I already have written. So my program over here. The first divide the length
of my array by 2 then if the value of Gap is greater
than 0 for every element that comes in the range of Gap. If the elements present after the Gap
elements are greater than y will have to follow
the insertion sort algorithm and swap the elements. Finally. The Gap is divided
by 2 again since in my case n is 7 This is my list
contains Seven Elements. I’m going to divide 7 by 2. To find the Gap so 7 by 2 which is equal to G
or the Gap will be nothing but 3 now I’m taking 3 because it’s the
whole part of 3.5. So the Gap over here will be 3. So what I’m going to do
is first take this element 23 and then I’m going
to check with the element that is present
at index number three. So at index number 3 is 17
since 17 is less than 23. These values will be swept. So after the first iteration
17 will be at the first place. Okay, let me just clear
these drawings and then write down the file list. And then after
the first iteration, I’ll have 17 followed by 12. 123 which was swept with 1745. two and 13 So
for the next iteration, I’m going to check 23 with 13. Now. This is because I’ll have
to check after the gap of 3. So since 13 is less than 23. I’m going to
swap these elements. So after that, I’m going to have
13 in place of 23 and 23 will be placed at last. In case of 13. So once this is done, I’m going to divide
the Gap by two again. So my Gap in
this case was three. So therefore if I
divide this by 2, I’ll get the Gap
to be 1.5 wearin. I’m going to take
the whole number which is 1 so Gap
in this case will become one. So next up. I’m going to check
all the elements that are present at Gap of one
and then replace them. I’m going to repeat this
until my list is sorted. So let me just clear
all these drawings. And I’ll execute this problem. So as you can see, I have all the elements sorted
in the ascending order. So that was all
about the Sorting algorithms. Now moving on towards the searching
algorithms searching algorithms are used to search for or fetch some elements
present in some given data set. There are many types of search algorithms
such as linear search binary search exponential
search interpolation search Etc in this session. I’m going to be discussing
about the linear search and the binary search
problems the LIE. Linear search algorithm
is used to successively search for a given element by comparing it with each element
of the given array. It is one of the simplest
searching algorithms, but very important to understand
other searching algorithms. So what I’m going to do
over here is get back to my jupyter notebook
and over here. Let’s create a heading again. I’ll call this
as searching algorithms. The first one over here
is linear search. And I’ll just paste the code
that I have for it. So the first thing that
I’ve done over here is created a function that is going to accept
the iterables the length of the list and the key element. So if any element
present in the list matches the key element, I’ll have to return
that element in case the element is not found. I just have to return -1. So for every element
present in the range of 0 comma n that is starting from the 0th index number
and for all the elements present in the list. I’m going to check if any element present
in my list is Filter the key or not in case
the element is present. I will return X else. I’ll return -1. Okay, the key that I’ve supplied
over here is 16 and my array over here contains
some random numbers next thing that I’ve done over here
is assigned the value of n as the length
of my array following that I’ve take another variable
named match to which I’m going to assign the value
of the linear search function if the value of match is equal to minus 1 then K is Is
not present in my array else the key is present
at so-and-so index. So, let me just
erase these drawings and I’ll execute this problem. So as you can see
over here Mikey is 16 which is not present
in my array in case I change the value of this to 12
and then I run this program. You see that the key is present
in the given list at index number 0 so I hope
you guys have understood this. So now moving on
towards the next algorithm, which is the binary search
algorithm binary search is used to search
for some given element in the sorted array
by making use of the decrease and Conquer algorithm here. The key is looked or by
first comparing it with the middle element and then dividing
the array into half. The left half is searched if the element to be searched is
smaller than the middle element and vice versa. The appropriate sub arrays
are again divided into half and the process
is repeated again, say for example, if you have a sorted list
of Seven Elements, the key will be compared
with an element that is present at the middle or
the element present at 7 by 2, which is 3.5. So in this case, the middle element
will be assigned as the element present at in Number three. So in case the key element
is smaller than that of the element present
at index number three. I’m going to check
the left sub list if the value of Chi is greater than that of the value
present at index number three. I’m going to search
in the right sub list for I already have
a program for this. I’ll just copy paste it
so it might program. I’ve created a function called as the binary search function to which I’m supplying the list
as well as the key element left variable is going to point
at the first element which is present
at index number 0 Errol and the right element is going
to be length of my list – 1 which is the last element
present in my list. Then I’ve taken a variable to which I’ve assigned
a Boolean value as false initially the value
of this is going to be false. So until my list is not empty. I’m going to divide
my list by 2. So if the value of the middle element is going
to be equal to the key. I’ll change the value of match
from false to True else if the p-value is less than that of the value
present at the middle. Going to make the value
of r as middle – 1 which means I’m going
to search only towards the left list. And if the value of T is greater
than that of middle. I’m going to change the value
of left as middle plus 1 which means I’m going to check
only towards the right of the middle element. Finally. I’ll return the value
of the Matched variable. So in this problem, I’ve taken two examples
in the first case. I’ve taken an element that is present in my list
and the second I’ve no I’ve taken an element
that is not present in my list. Okay, so as you can see over
here for the first case, I have true and for
the second case I have faults. Okay, so I hope you guys
have understood this. So now moving on
towards something that all of you need to know which is object oriented
programming in Python object oriented programming is a way
of computer programming that makes use of the idea
of objects to represent data and methods it is
also an approach used for creating neat and reusable code instead
of a redundant one here. The program is
divided into cells. Objects are several
mini programs. Every individual object
represents a different part of the application
having its own logic and data to communicate
within themselves. So what exactly is the difference between
object-oriented programming and procedure
oriented programming object-oriented programming has
a bottom-up approach whereas procedural
oriented programming has a top-down approach? In opes the program
is divided into objects. Whereas in procedural
programming program is divided into functions. Oops makes use of access modifiers such as
public private protected whereas procedural programming does not have
any access modifiers. Oops languages are much
more secured compared to those which are procedural languages
the objects created and an object oriented
programming language can move freely
within the member functions. Whereas in procedural
programming language data can move freely from function to another function object oriented programming
supports inheritance. Whereas procedural
oriented programming does not support inheritance. So now moving on
towards classes objects and the pillar
of oops in detail. So first let us
discuss about objects and object is a real
world entities such as a pen book person
animal Etc objects have States and behaviors. In Python, everything
such as variables functions Etc are considered
to be an object a class on the other hand is
a collection of objects, or you can say
that it is a blueprint of objects defining the common
attributes and behaviors. Now the question arises,
how do you do that? Well, the class logically
groups the data in such a way that the code reuse
ability becomes easy. So for example, consider an office going
employee as a class and all the attributes related to it
like employee named employee. Salary ID as
the object in Python. So all the details of the employees will be placed
in the class employee this way. You’ll not have to write
the code repeatedly for all the employees instead a single class will be enough
to determine all the details of each employee. So now I’ll just jump
onto my jupyter notebook and show you guys how to write down
a class in Python. I’ll just create a heading so
here I’m going to write down the Syntax for a class. The first thing
that I’ll have to specify is the class keyword followed
by the name of the class. Like I’ve already told you all in the naming
conventions class names begin with the capital letter
followed by small letters. So in case the name
of my class is my class my is going to be with a capital
M followed by a small Y and capped see followed
by small l a SS. So this is my class name
within this class. I can have
n number of statements. So I’ll just say statement one. one to statement n So this is basically
the syntax of a class. So now talking about methods methods
are nothing but functions that describe the attributes
and behaviors of an object. Now, let us take an example
to demonstrate a class an object and a method. Please make a note of your guys that it’s mandatory for python class functions
to have a self parameter unlike other languages
in Java self is nothing but a reference to the current
instance of the class and it is used to access
the class variables. So I’ll just create a cloth over
here I’ll see class new class. And within this I’ll Define
a function and I’ll call it as new function. So like I’ve already told you
all the first parameter to this by default should be
the self parameter. So when this function
is invoked, I just want to return hello world. Now once this is done,
I’ll come out of my class, and I’ll create
an object of my class. So I’ll just say
object is equal to class. So this is how you create
an object in Python. You just have to give
a name to your object and then make use
of the class name. Sorry the name of my classes
new class not just class. Next I’m going to make use of this object of new class
to call the new function. So I’ll just say print. obj dot new function So as you can see over here
with the help of this object. I just created
I’ve called the function which is present
in my new class. And therefore it has returned. Hello world. Similarly. You can have multiple objects
and functions in a single class. So now let’s take
another example in which I’m going to have more
than one functions and objects. I’ll just copy this and paste it over here
and I’ll take another function. named my function I’ll specify
the self parameter as usual and for this function, I’ll just have
one return statement that says welcome to Ed, Eureka. So now I have two functions
and one object. I’ll create another object. I’ll say object to and this
is a new class object. And through this object. I’ll call my function. So as you can see over here, I have two functions to objects
and with the help of both these objects. I’m calling both the functions
present in new class. Therefore. I have an output saying hello
world and welcome to edu Rekha. So now let us take a more
complex example of this. What I’m going to do is create
a class named calculating here. I’m going to find the sum
difference product and quotient for different values using
more than one object. So I’ll just say
class calculating. And within this I’ll
Define a function say at the first parameter should be the self parameter
followed by the variables that is a and b now
this function is going to return the sum of A and B. So I’ll see return a plus b. Okay, so I’ll create
a similar subtract function. I’ll just copy this and in case of a doll
right subtract and in case of a plus b LC a minus B. Now, let’s create a function
to find the product so ad will become product. And from plus I’ll
change it to stop. Finally a function
to find the quotient. So this one Advil
change to divide and it will be a divided by B. So now I’m done with this. I’ll create an object
called as or one. And this will be an object
for the calculating class. Now using this object, I’m going to find the result
of each of these so I’ll just save print or one dot add and I’ll specify some random
parameters say 4 comma 5 I’ll copy this Mr. Taylor
paste it here paste it here and I’ll change the names
of these functions accordingly. so subtract product and divide let me change
the values as well. Just say 12 comma five
product of 32 and 55 and divide 44 by 5 or 15. So I have one object that is calling
all the functions that are present
in the calculating class in case I want to create another object. I can do it in a similar manner. So what I’m going to do
is just copy this. I’ve pasted over here and I’ll change the name
of this object from o1 202 wherever I have
over one in this I’m going to change it to O2. So I have two objects
and four methods now. I want to execute this seems like I’ve made some error
has made a mistake in the spelling over here. It’s not CLA it cael. So as you can see over here, I’ve got the output in case
I want to differentiate between the results
of both these objects. I’ll just write down
a print statement of here and I’ll just print
a dashed line. So as you can see the first
half contains the result obtained by object one and the second half
contains the result obtained by objective. So I hope you are
clear with this. So now moving on towards
something very important, which is the init
method in Python. The init method is
a very important method that is always executed whenever
a class is It so whenever you create an object of a class
this method will be invoked. The init method is always used
in the real world applications. Unlike the previous examples
that we just saw. So now to show you an example
of the init method. I’ll get back to my jupyter
notebook and over here. I’ll define a class and I just call it
as Python programming. I’ll just use a shortcut So within this I’m going
to call the init function or the init method
the init method has two preceding underscores prefix
and suffix to it. So I’ll just say
double underscore in it double underscore and the first parameter
should be the self parameter. So here I’m going to pass
into other parameters, which is named
and the age of python. Once that is done. I’ll just find the self
parameter with the name. and the H attribute I’ll just
say self-taught name is name and self dot age. Is age I’m going to define
a new function of here. So I’ll just say
Define new function. And again, I should specify
the self parameter and within this I’ll just
have one print statement that is going to
print both the name and the age so I’ll just say hi. My name is python. So that is self-taught name. And my age is self-taught age. So you guys have to first
specify a plus sign over here. So once this is done, I’m going to create
an object of this class. So I’ll just say
P1 equals python. Programming and then
I’ll use this object called the new function
so P1 dot new function. I’m sorry guys, I forgot to pass
the parameters itself. So I’ll have to
pass the parameters. I’ll just say name is
Python and age is 31. So as you can see,
I have an output saying hi. My name is Python
and my age is 31. So now moving on towards object-oriented
programming methodologies object-oriented programming
methodologies are nothing but the pillars of oops which
our inheritance polymorphism encapsulation and abstraction. So now let’s move on and discuss each
of these in detail. The first thing that I’m going to be discussing
over here is inheritance. So what exactly is inheritance the method
of inheriting the at ease of a parent class into a child class
is known as inheritance. It has a number of benefits
such as code reuse ability where we do not have to write
the same code again and again and we can just inherit the properties we need
in the child class. It also represents
a real world relationship between the parent class
and the child class inheritance is transitive in nature. If a child class inherits
properties from parent class, then all other subclasses
of the child class will also inherit the properties
of the parent class. So now let me just show
you all an Beloved this I’ll get back
to my jupyter notebook. So I’ll just take
two classes over here. The first will be
the parent class and second will be
the child class. So just say class parent and then I’ll Define a function
and I’ll call this function as first the self parameter as usual should be specified
and within this function, I’ll just have
one print statement that informs first function. So next I’m going
to declare another class. I’ll just call
this class as child. And this child is going to be
a subclass of the parent class. So inherits the parent class. I’ll have to specify
the parent class within the braces. So I’ll just type in the name
of the parent class within this I’m going to define
a function named second and the self parameter to it. Now. This function will also
have one statement that is the print statement
saying second function. So once this is done, I will create an object
of the child class. I’ll just say obj. equals child And then
using this object. I’m going to call the first
and the second function. So I’ll just say obj dot first. And obj Dot second. So as you can see over here, this object is able
to access both the functions that is first and second. The first function
is not present within the child class. But since the child class
is inheriting the parent class this function is visible
to the child class and therefore it
is able to execute it. So what we are actually doing
over here is subclassing the child class
calling a Constructor of the parent class by
mentioning the parent class name in the Declaration of the child class
is known as sub classing a child class identifies
the parent class by subclass. So now moving on towards types of inheritance depending
upon the number of child and parents classes involved. There are four
types of inheritance. The first is single inheritance. Second is multiple
third is multilevel, and finally hierarchical
single inheritance is when a child class inherits
only a single parent class. So in the previous example, my child class is inheriting
just one parent. This is an example of single inheritance in case
of multiple inheritance. The child class will inherit
more than one parent. So what I’m going
to do is just copy this I’ll paste it over here and then I’ll just call this
as parent won. I’ll declare another class
as parents, too. Even if you do not
specify these bases, it’s going to work
properly within this. I’ll just Define the function. I’ll call this as third. and the self parameter and then I’ll just
print multiple inheritance. So here my child class is going to inherit parent
one as well as parent, too. Then my object is going
to invoke both these classes. So as you can see over here
in the first example, I just had one parent
as the base class. But in this example, I have two parent classes
and a single child class. The object of this child class
is able to invoke the functions present
in both the parent classes. So I hope
you’ve understood this. The next is multi-level inheritance
multi-level inheritance is when a child becomes a parent
class for some other class. I just paste the previous
code and over here. I’ll create another child class. I’ll say Us child’s do and I’ll inherit
the child class. I’ll Define a new function. I’ll just call this as function and the self parameter and I’ll
print multi-level inheritance. So here instead of creating
an instance of the child class, I’ll create an instead of child
to and then I’m going to invoke all the functions
using this object. I’ll say obj dot function
and I’ll run this so as you can see over here, all the functions
are accessed by this object. So now talking about
the hierarchical inheritance hierarchical inheritance
involves multiple inheritance from the same base or parent class. So in this case, I’ll have Do
different child classes that are inherited
from the same parent class? So what I’m going to do here
is just paste the code and then I’ll have another child. I’ll call this as child
to itself and this is also going to inherit the bend class. This is the
hierarchical inheritance. And then I’ll create two objects that is for the child class
and the child to class. So obj dot one will be an object
for the child Class 2. Let me just change the name
of this function. just copy this paste it over
here and I’ll change the name of the function
from second to function. Okay. Sorry guys. I just forgot to change the name
of the object over here. As you can see over here
both the child classes have executed and return
the results accordingly. Similarly. You can also have Hybrid inheritance Hybrid
inheritance involves multiple inheritance taking
place in a single program. So just to show an example
of this I take this code and paste it over here. And I’ll create another class is
a child for or else a Child 3. And this is going to inherit the child class as well
as the parent class as you can see over here. I have more than a single type
of inheritance has the first type you see
is a single inheritance and now I have multiple
inheritance as well. Similarly. You can have any other type
of inheritance also, so I just Define a function say
function to the self parameter. And then I’ll print hybrid. Okay, so now let me create
an instance of this class. just copy this paste
it over here. I’ll remove these object
since we’ve seen their output. So as you can see, I can have multiple
inheritance as well. So now talking about the super
function the super function in Python allows you
to call the method from the parent class. So to show that to you guys, what I’m going to do is take
the original two classes, which was the parent
of the child class. I just paste it over here. And within this function, I’m going to type
in something called as super. . The name of the function that’s
present in the parent class. So that is first so now let
me create an object of this. I’ll just say obj equals child. And I’ll execute the function
present in the child class. So I’ll just say obj dot second. Sorry guys. I just made a mistake. I specified capital s
in case of smallest. So let me just
execute that again. So as you can see over here
in the previous example, I had to explicitly specify my object to call
both the functions present in the child class
and the parent class but overhear my object is accessing the function
present in the parent class by making use
of the super function. So now let’s move on
towards the next pillar of opes which is polymorphism
polymorphism defines the ability to take different forms. By making use of polymorphism. You can redefine methods in the child class
with the same name as that of the Panel
class polymorphism is taken from the Greek word poly which means many and morphism
which means forms. It means that the same function
can be used for different types. This makes programming more
intuitive and easier in Python. We have different ways to define
polymorphism a child class inherits all the methods
from the parent class. However, in some situations the
Inherited from the parent class doesn’t quite fit
into the child class. So in such cases, you will have to re-implement
the better in the child class. This can be done
in many ways such as using different functions
class methods are objects to Define polymorphism. So to understand this clearly, I’ll get back
to my jupyter notebook and show you all
an example of this. So I’m going to have
different classes which will have functions
with the same names. So, let me just define a class. Call this apple. And this class will have
a function called type. Within this I’ll just
have a print statement that is going to tell me
if this is a vegetable or a fruit. So as we all know
apple is a fruit. I’ll just type in fruit and then
I’ll create another class. Called as potato. Here, I’ll create a function
with the same name, but with a different implementation
and instead of fruit. I’m going to type in vegetable. Let me just change
the name from type since type is a very
general keep word in Python. So just say type off. So now I’ll create two objects
and say obj1 and obj2. And by using these objects, I’m going to involve
both these classes. So I’ll say obj1 Dutch type
of and then obj to the type of now is just run this code. So as you can see over here, even though I have two functions
with the same name, my objects are able
to access the right function and return the output. You can also have
polymorphism along with inheritance polymorphism. Like I’ve already told
you all defines method in a child class that have the same names as
the method in the parent class so as discussed
in inheritance the child class inherits the methods
from the parent class. Also it is possible
to redefine a method present in the parent class
in the child class. This process of read
lamenting a method and child class is called
as method overriding. So now let’s take
an example of that. I just defined a class called
this bird and within this I’ll Define the function
called as information. Now within this I’ll just keep
a general print statement. That is to say that birds. Birds are very pretty. So now I’ll create
a subclass of this class. I’ll just say class. And I’ll specify
the book as padded. I’ll inherit the
bird class of here. And I’ll Define the function and I will redefine
the function information. Here, I’ll just print. Parrots can fly. So now I just create an object
of the Paris class. And I’ll say
obj dot information. So as you can see over here when I’ve called
the function information, which is present
in both the base class and the inherited class. The function present
in the inherited class has been executed. So that means I have redefined or overwritten my method
that is present in the base class and
that method has been executed. So that was all
about polymorphism. Now, let’s move on towards encapsulation
encapsulation deals with binding up the data
together with the code that is used to work on that data this Not just
makes our code clear but it also helps in preventing
data from being modified as well in opes a classic example
of encapsulation is a class A Class
encapsulates all the data and the code such as
variables methods Etc data that is encapsulated
can have three types of access specifiers in opes. These access specifiers
are protected private and public members having Public
Access specifiers are visible throughout the code
protected members are visible. Just Within The class
and the subclasses whereas private members are
visible only inside the class in which they are present. Even the inherited classes
will not be able to access them. So one thing I want you
all to know over here is that python in real has
nothing such as privacy module. It does not have
exclusive access to modifiers such as C++ C sharp or Java. Therefore there are no truly private or protected
attributes in Python. Now, I’m sure you would be thinking then what is
the use of proceeding single and a double underscore
and Ten members that have names with
the preceding double underscore our mangled in order
to protect them from clashes with the inherited
classes subclasses are also allowed to Define
their own private members, which will not interfere
with those of the parent class. This is why they
are considered to be private. They are accessible
outside the class but less is the chance
of a clash mangling in Python is achieved by
prefixing any such name with an extra underscore and the class name this in turn
provides an effective namespace. So in case I have
a class named parent, so I’ll just say hash. Parent and if this parent
contains a member with double underscore, so I’ll just say
underscore double underscore private member. At the compile time this private identifier is going
to convert to underscore parent. double underscore private member
So as you can see over here, the name has been mangled. So therefore in case I try
to access this private member anywhere outside the class
by just making use of a double underscore followed
by the private member named I’ll get an attribute error. Similarly if I
have a child class. I’ll just say child. And if this child
has a private member. Then at compile time
it’s going to be something like underscore child. Double underscore
private member. So I hope you guys
are getting this the names of members proceeding with a double underscore
our mangled such that you have an underscore
followed by the name of the class. And then the name of the member
that has a double underscore. So what I’m going to do here
is I’ll define a class say a and within this I’ll call
the init method. Through this init method. I’m going to invoke
the so-called private method. So therefore I will have
self dot double underscore. Hi, the name
of my method is high. I’ll declare another method. Call this hello world. So this method is just
going to print hello world. So here I’ve declared
a method named high, but I’ve not defined it. So I’ll just Define underscore. Hi, so I double underscore hi. To which I’ll specify
the self parameter again, and I’ll save print
welcome to edu Rico. Now I’ll create an object
of this class. And first by making
use of this object. Let me try to call
the hello world function. So as we all know this object will be able to access
the hello world function. So just execute this. So as you can see over here,
I have both the outputs, which is welcome to edu
Erica and hello world as you’ve seen I’ve not called
this function which contains a double underscore but I
still have the output to it. Now if you guys are
wondering why the output is seen now this is because I’m calling the double
underscore High method through the init method as I’ve already told
you all the init method is always executed when But I create
an object of this class. So in case I do not
write this method, so I’ll just delete
this from here. And then if I
execute this problem, you’ll not be able
to see the output for the high function in case
I want to invoke this function. I cannot directly use obj dot. Hi. I’ll have to type
in obj dot underscore a double underscore high and then execute this so I hope
you guys are clear with this. The name of high has changed from double underscore height to
underscore a double underscore. Hi. So what happens when you
have a single underscore, let me take another example
to explain this to you all so I’ll just say class base. And then I’ll Define
a single underscore class. I’ll just stay single. And I’ll just print saying
single on the scope. Next I’ll Define another class
called as double underscore or just double. And then I’ll print a message
saying double underscore. Now we’ll create another class
which is the child class of Base Class. Enter this class. I’ll try to access
both the functions present in the base class. So here I’ll just type in self
dot the name of this function which is single. Sorry. I forgot to define
a function of here. So I’ll just say Define function
one with the self parameter. I’ll Define another function. with the self parameter And then
I’ll say self dot double. So now let’s create
an object of child class. So I’ll say obj equals child. And I’ll call obj Dot
function 1 and function 2. So now let’s see what happens. So as you can see over
here the function having a single underscore
has been inherited and executed by the child function. Whereas the function that contains a double
underscore throws an error saying child object has no attribute named underscore
child double underscore double. So in case you want
to execute this, all you have to do is just type in the name of the Base Class
preceded by an underscore. So I’ll say underscore base
and then I’ll hit run. So as you can see now both the
functions have been executed. You did using the object
of the child class. So I hope you guys are clear
with this now moving on towards the final topic
of this session, which is data
abstraction in Python. So abstraction basically means
to show the necessary data and hide the implementation
details this not just increases the efficiency of the code, but it also
reduces its complexity. For example, consider
your washing machines. You know, what happens
when you press certain buttons such as on off save water set
water level change program Etc, but The actual implementation of these programs
is hidden from you. You do not know how it’s being turned off
or turned on you do not know how it knows to take
less water or more. This is called as data abstraction abstraction
can be achieved by creating abstract classes any class that contains an abstract method
is called as an abstract class. So what exactly are abstract
methods abstract methods are those methods
with just have names but do not actually contain
any function definition. So that means that you An abstract class
but you did not Define it. These functions are also
called as unimplemented methods therefore an abstract method
does not have anybody in Python. This can be achieved by making
use of the pass key word. However, an abstract method
needs to be redefined in a subclass where it’s being used to achieve
abstraction in Python, you can make use
of the ABC module. That is the abstract
Base Class module. So to show an example of this I’ll get back
to my jupyter notebook. I just type in a heading. So the first thing that I’m going to do
is import everything that’s present in the ABC module
first safe from ABC import Star. So this is going to import
all the necessary functions that are present
in the ABC module. I’ll create a class
of here called as Bank within which I’ll have
a function that describes the name of the bank. I’ll have another function that is going to find
the location of the bank but this function
is going to be abstract or I’m not going to have
any function definition next. I’m To the final child class
called as location through which I’m going
to redefine the abstract method. I’ll just define a class. Sorry. Class bank. So the bank class is going
to inherit the ABC class in case you want to know
what this ABC class does. I just add in a kernel over here
and I’ll say help on ABC. So as you can see,
this is a helper class that provides a standard way to
create an ABC using inheritance. So now moving on I’ll Define
two functions having Bank name. and the South part
of Mito I just put in the name of the bank. I’ll say HDFC. And then I’ll Define
another function called as Bank location. I’m not going to provide
the body of this. So I’ll just try to pass. Now I’ll create a child class. I’ll say a bank. underscore location This is
going to inherit the bank class and then I’ll redefine
the bank location function. I print a location as Bangla. Finally, I’m going
to create an object of the bank location class. I’ll just say obj equals. Bank location and I’ll call
both the functions. So as you can see both, the functions have
been executed successfully. So this brings us
to the end of the session. I hope you guys have enjoyed
and learned something new in case you have
any doubts or queries. Please do let me know
in the comment section and I will revert
to you at the earliest. We’ll be back with more
and more interesting videos and sessions till then good. Bye and take care. I hope you have enjoyed
listening to this video. Please be kind enough to like it and you can comment any
of your doubts and queries. And we will reply them at the earliest do look out
for more videos in our playlist And subscribe to Edureka
channel to learn more. Happy learning.

Leave a Reply

Your email address will not be published. Required fields are marked *