Transcripts
1. Introduction: Hello, I am showing
comment and welcome to my Python programming
language course. Now, in this course, I will tell you all
you need to know about Python
programming language. So you will be able to
go from knowing nothing about this programming language
to being able to use it. So you don't need to know anything about
programming or Python. I'm going to teach you
Python from scratch. Let me tell you something about what you can do with Python. You can use Python for
machine learning and AI. And Python is a number one in machine learning and also
popular in web development. Using Python and
framework called Django. You can build amazing websites. You can use Python
for automation. Using Python can save
you time and increase productivity by automating
repetitive tasks. Here are some powerful websites built on Python and Django, YouTube, instagram, Spotify,
Dropbox, and Pinterest. Please make sure to leave a good review and rating,
which helps me a lot. And now let's get started.
2. Installation and Setup: The first thing I want
you to do is to head onto python.org to download the
latest version of Python. Click on the first
link and go to the download section and select the latest version of Python. Select the desired folder
you want to save python. And once downloaded, click
on the executable file. So this is the Python installer. If you are on Windows, check this box because
it's really important. Otherwise you are not gonna be able to follow this tutorial. Simply click on Continue and
wait for the installation. There are several other
options to Python. Ideally, the most and
best suited is PyCharm, which is widely used to combine, compile your code much effectively in exchange for
more resources offer system. Since the Python
ideally makes it easier and much
lighter to write and compile code with
minimal resources based on the code
and the libraries. So we will be using Python
ideally for this tutorial, for very basic tutorial. So now we will wait for the
installation to complete. Once this is done, click on the Agree with terms and install the
latest version of Python. And there we go. We have
installed a Python ideally
3. First Python Program: For creating and executing
our first Python program, we are going to write, print all in lowercase, then add parenthesis,
then add quotes, either single quotes
or double quotes. And inside this course, we're going to
write hello world. So this is what
we call a string. A string means a sequence of
characters in simple words, that means textual data. So in Python and in many
other programming languages, whenever we are dealing
with textual data, we should always surround
our texts with codes. In Python, we can use
single or double quotes. Now, let's print. You see here this is a
function built into Python. So we can use it to print a message on our
application window. So let me show you
how to run this code. On top of the section, we go to the Run menu
and then we select Run. So a new window shows the
output of our program. So here's hello world message printed in the terminal window. As you learn more Python, you will learn how to build applications that have a
graphical user interface. That's an advanced topic. So now, let's not worry
about it right now
4. Variables: Now let's talk about variables. We use variables to temporarily store data in a
computer's memory, e.g. we can store the price of a
product or someone's name, their email, their
age, and so on. Let me show you. So to declare a variable, we start by typing a
name for that variable, let's say h. Then we
add an equal sign, and then we type of
value, let's say 20. So with this, we are
storing the number 20 somewhere in our
computer's memory. And we're attaching this age as a label for that
memory location. Now we can read the value at this memory location and
printed on the terminal. So instead of
printing Hello World, we want to print the value
of the age variable. So I'm going to
delete what we have inside the parentheses
and type H. Note that I'm not
adding H quotes. Because if I'm, if
I run this program, we will see the text
H on the terminal. We don't want that. We want the value of
the age variable. So let's remove the code and print the value of
the age variable. Now you can use the shortcut that I showed you
in the last video. So the shortcut is f phi. So there you go. You can see the value of
the age variable. Now we can also change the
value of the variable e.g. on line two, we can
set eight to 30. Now we run the program, so we see 30. So as you can see, our program gets executed
from top to bottom. So this is how we can
declare and use a variable. Now let's look at a
few more examples. So I'm going to declare another variable called
price and set it to 19.95. So in Python, we can use numbers with a decimal
point or whole numbers. We can also declare a variable and assign it to a string value. So let's say first
underlined name. So if you want to use multiple words in the
name of a variable, we should separate them
using an underscore. This makes our code more
readable and see what would happen if I didn't use this
underlying underscore. Now we set this to a string so we can use single quotes
or double quotes. Let's say March. We also have a special type of value called a Boolean value, which can be true or false. That is like yes
or no in English. Let me show you. So I'm going to declare
another variable called it's online
and set it to true. We could also set
it to false that we have here is called
a Boolean value. Now note that Python is a
case-sensitive language, so it's sensitive to look lowercase and uppercase letters. In this case, if I
use lowercase f, We can see an edit over here because this is not an
not recognized in Python. So False with a capital F is a special keyword in Python that represents
the Boolean value. So, alright, now here's a
little exercise for you. Imagine we want to write a
program for the hospital. So we are going to check in
a patient named John Smith, is 20 years old and
is a new patient. I want you to declare a few variables to
store these values
5. Receiving Input: In this tutorial, I'm
going to show you how to receive input
from the user. So in Python, we have another built-in
function called input. Use this to read a value from the terminal window.
Let me show you. So we add parenthesis. Then we type in a string. Here we can type a
message like what is your name and give a question
mark followed by a space. You will see why we need
this space in a second. So let's save this file
and run this program. So we get a message. Now, we have to enter a value. So we click over here. And now you can see that it is separated from
the question mark. This is because
of the whitespace that we added over here. So now we have to type a value. Let's say Chavan. When we press enter, this function will return the value that we entered
in the terminal window. So we can get that value
and store it in a variable. So let's declare
a variable called name and set it to the return value of
the input function. Now, we can print a greeting
message for this user. So we use the print function. We say hello, we add a space. And after the string, we want to add the value
of the name variable. So we use a plus sign
and then type name. What we're doing here is
called string concatenation. So we are combining this
string with another string. Now, let's run our program
and see what happens. So what is your name? Chavan. Now we get this
message, Hello Chavan. So this is how you use the
input function in Python
6. Type Conversion: You will learn about the three
types of data in Python. We have numbers,
strings, and Booleans. Now, there are times
you want to convert the value of a variable
from one type to another. Let me show you. So we are going to use our input function to
read the user's birth here. So enter your birth year here. Now this input function is
going to return a value. So we can store it in a variable called Bird underline ear. Okay, now let's write our code to calculate
the age of this user. Soviet write an
expression like this. Currently we are
in the year 2022. So 2022 minus birth
year tastes expression or a piece of code is
going to produce a value. So once again, we can store
that value in a variable. Let's call that variable age. Now, let's print age
on the terminal. Let's run our program
and see what happens. So my birth year is 2001. Enter, oops, our program crash. So whenever you see this red message that
indicates an error, this error occurred
in this file. On line two, right below that you can see the piece of code
that generated this error. So that is this expression, 2022 minus birth here. Now below that you can
see the type of error. So here we have unsupported
types of subtraction. We have int and a string. What are these? Well, this int is
short for integer, and that represents a whole
number in programming. So 2022 is an example
of an integer. Now birth here is an
example of a string. Because whenever we
call an input function, this function would return
a value as a string, even if we enter a number. In other words, when
I entered 2001, this input function returns a string with these
characters 2001. So this string is the in is different from
the number 2005. They are currently
different types. So in this case, let me delete these lines. The reason we got this
error is that we tried to subtract string
from an integer. So our code looks
like this, 2001. Now Python doesn't know how to subtract a
string from an integer. So to solve this problem, we need to convert this
string to an integer. Now in Python, we
have a bunch of built-in functions
for converting the types of our variables. So we have this end function. We can pass our bus here. So this will return the new numeric representation
of the birth year. So to solve this problem, we need to replace the
string with the n function. So let's see what
is going on here. On the first-line be
called the input function. Returns a string. On the second line, we pass the string to our end function. The function will return the numerical representation
of the first-year. Then we subtract it from the
two, subtracted from 2022. We get the age and store
it in the age variable. Now let's run our program. So 2000s. And there you go.
I'm 21 years old. So this is how the
function works. Now, we also have another
built-in function called float, that is for converting a value to a
floating-point number of floating point
number in Python and other programming languages is a number with a decimal point. So n is an integer
and 10.1 is a float. So we have n, we have float, and we also have bull for converting of value
to a Boolean. And finally, we have string for STR for converting a
value to a string. So these are the
built-in functions for converting the type
of our variables. Now here's a little
exercise for you. I want you to write a
basic calculator program. So here we have to
enter two numbers. We can type a whole number or a number with a decimal point. And then our program will print the number of
these two numbers, the sum of these two numbers. So pause the video, spend 2 min on this exercise
and then see my solution. Alright, first we're going to call our input function to read the first term where
we get the result and store it in a variable
called first. Now let's declare
another variable called second and read
the second number. Now we calculate the sum, so that is the
first plus second. Now, let's see what happens When we print the
sum on the terminal. So I entered 10.20, but instead of 30, we get 1020. This is because we are combining or concatenating two strings. So as I told you before, the input function
returns a string. So this line will be equivalent
to first equals ten. We are dealing with a
string, not an integer. Similarly, second is going
to be 20 as a string. So when we combine two
strings, ten plus 20, we will get to 1020 because we're dealing
with textual data. Okay, so to solve this problem, we need to convert the values we read to their numeric,
numeric representation. So over here, we are going to pass first to our end function. And here as well. Now let's run our program. So we enter 10.20, we get 30. What if we enter a
floating point number? So 10.1 and 20, We got an error. So to solve this problem, we need to treat both
these values as floats. So instead of the
inner function, we're going to use
the float function. Now, let's run our
program one more time. We enter the whole number
and floating point number. So the result is correct. Now, let's add a
label over here. So sum is plus sum. Let's run our program
one more time, 10.20. Once again, we got an error. The error is saying
that Python can only concatenate strings,
floats to strings. So on line four, we
have our string. We're concatenating
this with a float because the result of this expression is a
floating point number. We are adding two fluids. So the result is
a float as well. So Python doesn't know how
to evaluate code like this. It doesn't know how to
concatenate a float to a string. To solve this problem, we need to convert some
two-year-old street. So this is where we
use the STR function. Now, let's run the
program again. So ten plus 20.1, and here's the result. And one last thing
in this example, I am calling the float
function at the time. We want to calculate the
sum of the two numbers, but this is not the requirement. This is not the requirement. We can call the float
function over here. So this input function
returns a string. We can pass that string to our float function. Take a look. So float parenthesis like this. So the value that
we are passing to the float function is the value that is returned
from the input function. Similarly be called
the float function. Over here. We can change this expression
to first plus second. That is another way to
write this piece of code. So type conversion is important in Python and other
programming languages, there are times you
need to convert the type of variable
to a different type.
7. Strings: In this tutorial, I'm
going to show you a bunch of cool things you can do
with strings in Python. So let's start by declaring
a variable called cores and set it to
Python for beginners. Now, this string
that we have over here is technically an object. An object in Python is an
object in the real world. As a metaphor, think of a
remote control of a TV. This remote control is an object and it has a
bunch of capabilities. It has a bunch of buttons
for turning your TV on, turning it off, changing
the volume and so on. In this program,
this course variable is storing a string object. This string object has a
bunch of capabilities. So if we type course, you can see all the capabilities
available in a string. These are basically
functions that you can call it just like the
print or input functions. The difference is that the
print and input functions are general purpose functions. They don't belong to
a particular object, but the functions you see over here are specific to strings. Now, more accurately we
refer to these as methods. So when a function is
part of an object, we refer to that
function as a method. So let's look at a few examples. Here we have a function
or a method called upper, and we use that to convert
a string to uppercase. So if we print cores dot
upper and run this program, they can see your, our course in uppercase
pretty useful. Now, what you need to
understand here is that this upper method does not change our original string. It will return a new string. So right after this, if we print cores, you can see that our cause
variable is not affected. So the soda upper method
returns a new string. Now similarly, we have
another method called lower for converting a
string to lowercase. We have a method called
find to see if our string contains a character or a
sequence of characters, e.g. here we can pass y
and this will return the index of the first
occurrence of y in our string. So in Python, the index of first character in
a string is zero. So here we have 01234 and so on. So when we run this program, you are going to see one on the terminal because
the index of y is one. Take a look first-time
going to delete this line. We don't need it anymore. Also, let's do this line. Let's run the program. Here we, here you go. Now, as I told you before, by ten is sensitive to lowercase
and uppercase letters. So if I pass an uppercase
Y here, this methyl is, this fine method, returns negative one because we don't have an uppercase
Y in this string. We also can pass a sequence
of characters, e.g. four. So this will return the
index of the word for. Take a look, so it's seven. Now, there are times
we want to replace something in a string
with something else. To do that, we use the
replace method three plus, so we can replace for with a string containing
the number four. Take a look. So Python,
for beginners, obviously, if you look for a
character or a sequence of characters that don't
exist in our string, nothing is going to happen. E.g. if we tried to
replace x with four, obviously we don't have x here, so nothing is going to happen. Also, just like
the upper method, the replace method
is not going to modify our original string, so it's going to
return a new string. This is because
strings in Python and many other programming
languages are immutable. We cannot change them
once we create them. Whenever we want. Change your string, we will end up with a new string
object in memory. Now, one last thing I want
to cover in this tutorial, there are times you
want to see if a string contains a character or a
sequence of characters. One way to do that is using the find method that
we talked about. So let's see if our
string contains Python. Now, when we run this program, that is the index of
the first occurrence of the word Python
in our string. Now in Python, we can
also use the in operator. So we can write an
expression like this. We type a string Python, then we type in, this
is a special keyword. In Python. This is what we call
the inoperative. So after we type the
name of our variable. So with this expression, we are checking to see
if we have Python. In course. As you can see, Python
code is very readable. It's like plain English. So when we run this program, instead of seeing the index
of the first occurrence of Python Vc, a Boolean value. This is more desirable
in a lot of cases. Next, we are going to look
at arithmetic operations
8. Arithmetic Operators: In this tutorial, I'm
going to show you the arithmetic operators
that we have in Python. These are the same
arithmetic operators that we have in math, e.g. we can add numbers, we can subtract them, multiply them, and so on. So let's print ten plus three. Let me run this program. We have 13. So this is the
addition operator. We also have subtraction, we have multiplication
and division. Now technically we have two different types of
division operators. We have our division with one slash and another
with two slashes. Let's look at the differences. If you use a single slash, we get a floating point number. That is a number with
a decimal point. But if you use double slashes, me get an integer,
a whole number. We also have the
modulus operator that is indicated
by a percent sign. And this returns
the remainder of the division of n by three, so that is one. And finally we have exponent operator that is
indicated by two Asterix. So this is the, this is
ten to the power of three. So when we run
this, we get 1,000. Now for all of these
operators that you have seen, we have an augmented
assignment operator. Let me explain what it means. So let's say we haven't variable called x and we set it to ten. Now, we want to increment
the value of x by three. So we have to write
a code like this, x equals x plus three. When Python executes this code, it is going to evaluate this expression or
this piece of code. The result of this expression is ten plus three, which is 13. Then it will store 13 in the. Now, there is another way to achieve the same result
using less coal. We can type x plus
equal to three. That what we have
on line three is exactly identical to what
we have on line two. So what we have here is called the augmented
assignment operator. So we have this
assignment operator, but we have augmented
or enhanced it. Now here we can also
use subtraction or to decrease the
value of x by three. We can use multiplication
and so on. So these are here, the arithmetic
operators in Python
9. Operator Precedence: Alright, let me ask
you a question. I'm going to declare
a variable called x and set it to ten
plus three times two. What do you think is
the result of this? This is a basic math question that unfortunately a lot
of people fail to answer. The answer. The answer is 16 years. The reason in math, we have this concept called
operator precedence, and that determines the order in which these operators
are applied. So multiplication and
division have a higher-order. So this part of the expression
gets evaluated first. So two times three is six, and then the result
is added to ten. That is why the result of
this expression is 16. Now in Python,
operator precedence, it is exactly like math, but we can always change
it using parenthesis, e.g. in this expression, if you want ten plus three to
be evaluated first, we can wrap it in
parenthesis. So like this. Now, when we execute this code, we are going to see 26
because ten plus three is 13 and that divided
by two is two. And then that multiplied
by two is 26. Let's verify this. So print x and we get 26. So there, there we go. That's the arithmetic operators IN operator precedence in Python
10. Comparison Operators: In Python, we have
another set of operators called
comparison operators. We use these operators
to compare values. Let me show you. So I'm going to declare a variable called x and set it to an
expression like this. Three is greater than two. So what do we have here
is this piece of code. This expression is
called a Boolean expression because it
produces a Boolean value. So in this case, three is greater than two. The result of this
expression is boolean true. So if we print x, we get true on the terminal. So here is the greater
than operator. We also have greater than
or equal to, less than. We have less than or equal to. Here it is the equality
operator which is indicated by two
equals signs to not, do not confuse this with
the assignment operator. So here we're comparing
the end to 40 quality. So if we run our program, we see faults because three
does not equal to two. So here's the equality operator. We also have the not
equality operator that is indicated by an
exclamation mark followed by an equal sign. So let's quickly recap here. These are the
comparison operators we have in Python greater than, greater than or
equal to, less than, less than or equal to, equal to, not equal to. These operators are extremely important in real
Python programs because quite often
we have to compare values to evaluate
certain conditions. You are going to see that soon
11. Logical Operators: In Python, we have
another set of operators called
logical operators. We use these operators to build complex rules and conditions. Let me show you. So I'm going to
declare a variable called price and set it to 25. Now, let's print a Boolean
expression like this. Price is greater than n. Now, let's say we
want to check to see if the price is 10-30. This is where we use the
logical and operator. So V-type and, and
right after that we'd be type another Boolean
expression less than 30. So winters AND operator, if both these Boolean
expressions returned true, the result of this entire
expression will be true. And take a look. So in this case we get true
because the price is $10-30. We also have the or operator. With the OR operator, if at least one of these Boolean expressions
stones true returns true, then the result of this entire
expression will be true. To demonstrate this, I'm
going to change price to phi. Let's see how Python is
going to execute this code. So first, it's going to look
at this Boolean expression. It's price is greater than ten. No, it's not. So it will keep going. Then it will look at the
second Boolean expression is price less than 30. It sure is. The result of this entire
expression will be true. Take a look. There you go. We also have the not operator, which basically
inverses any values that you give it.
Let me show you. So we're going to have
one Boolean expression, price greater than ten. The result of this
expression is false. Now, if you apply
the NOT operator, this will inverse
false, true, true. So when we run the
program, we get true. So let's quickly recap. In Python, we have three
logical operators. We have logical and which returns true if both
expressions and returns true, we have logical or
which returns true. If at least one
expression returns true. And we have not which inverses
any value that we give it
12. IF Statements: In this tutorial,
we're going to talk about if statements in Python. We use statements to make
decisions in our programs, e.g. we can declare a variable
called temperature. And depending on the
value of this variable, we can print different
messages on the terminal. Let me show you. So here's
our temperature variable. We send it, set it to 35. Now, let's say if temperature
is greater than 30, we want to print a message
saying it's a hot day. So we type, if then
we type of condition, this is where we use our
comparison operators. Soviet-type temperature
greater than 30. Then Soviet add a colon and see what happens
when I press Enter. Now the character is indented and this
represents a block of code. So the code that
we write over here will be executed if
this condition is true. Otherwise it's not
going to be executed. Let me show you. We're going to print
it's a hot day. And by the way, note that here are surrounded the
string with double quotes, because here we have a single
quote as an apostrophe. So I couldn't declare a string like this
with single quotes. If I typed a single code, s, a hot day, look, Python gets confused
because it thinks this single quote D presents
the end of our string, so it doesn't recognize
the subsequent characters. Okay? So that's why we use
double-quotes here. So we can have an
apostrophe in our string. So it's a hot day. Now, if I press Enter
again, it's indented. So the code that right
here will be our part of our if block and it will be executed if this
condition is true. So here we print
a second message. Drink plenty of water. Now to terminate this block, we press Enter and then
press Shift and Tab. It is no longer indented. So the code that we write here will always be executed
no matter what, whether this condition
is true or not. Now in C based
programming languages like C plus C-sharp JavaScript, we present a block of
code using curly braces. So you start a block of
code using a left brace, then ended using a right brace. In Python, we don't
have curly braces, so we use indentation to
represent a block of code. In this case, these
two lines are indented and that means they are
part of this block of code. Now, let's run the program
and see what happens. So we see these two messages because the temperature
is greater than 30. Now, if I change the temperature to 25 and run the program again, we don't see anything. Okay? Now after this block,
let's print done. Because this code
is not indented, it will always get executed. It's not part of our if block. Okay, so take a look. Here's the down message. Now, let's add a second
message. Second condition. So if temperature is
not greater than 30, that means it's less
than or equal to 30. So I am going to add
a second condition. So if the temperature is 20-30, I want to print,
it's a nice day. So here we type I. If, that is short
for if else-if. Here we type of
second condition, so temperature greater than 20, we add a colon, press Enter. Now we have a new block. So here we print,
it's a nice day. So if this condition is true, that means that temperature
is greater than 20 and less than or equal to 30. Now what we have here
is called a comment. That is why it is grayed out. It's not real code. It's just some node
that we add to our program by ten is not
going to execute this. So whenever we type a hash sign, what we have after is
treated as a comment. Okay? If this condition is true, then we're going to see this
message on the terminal. Let's run our program
and verify this. There you go. The
temperature is 25. That's why we see this message. Now we can have as many
condition as we want. There are no limitations. Okay? So let's add another condition. Elif, temperature is
greater than ten. Then we are going to print
it's a bit cold now, in this case, if this
condition is true, that means the temperature
is greater than ten and less than greater than ten. Then we're going to
print, it's a bit cold. Now in this case, if this condition is true, that means the temperature
is greater than ten and less and less than
or equal to 20. Now finally, if the
temperature is less than ten, let's print a message
saying it's a cold day. Here we type ends, then we add a colon, and now we have a new block. So this code will get executed if none of the
above conditions are true. So here we can
print it's called. So this is how we use if statements to make
decisions in our programs.
13. Exercise: A great exercise for you to practice what you've
learned so far. I want you to write a weight
converter program like this. So this program is
asking me my weight. I entered 170. Next, it's asking me if the weight is in
kilograms or pounds. So I can type K 4 kg and pounds. I can type lowercase or an uppercase L,
it doesn't matter. So let's go with a lowercase now it tells
me weight in kilograms. It is 76.5. So go ahead and
spend 5 min on this exercise. You can use the discussions, but you can tell me if you have done it correctly
in the reviews below. And then when you're done, come back and see
for my solution. So first mic Oliver
input function, to ask the first question, wait, we get the result
and store it in a variable called VT next week
called the input function. One more time to ask
the second question. Is this in kilogram or pumps, we get the result and store
it in a variable called unit. This is where we're going
to use an if statement. So we want to check
unit equals k. Then we should
convert the weight two pumps and printed
in on a terminal. However, with this code, if I type a lowercase k, This condition is not
going to be true because earlier I told you that Python is a
case-sensitive language. So we need to convert
this string to uppercase. Earlier, we talked
about string methods. So if we type dot, we can see all the functions or methods available
in a string object. So we use the a parameter and returns a new
string in case. Now, if this condition is true, first we need to
convert the weight. Two pumps, Soviet declare a new variable called converted, get the weight and
divide it by 0.45. And then we put
this on a terminal. So we save weight
in pounds. Is this. Then we append converted. Now to terminate this block, we press Shift and
Tab else colon. So if this condition
is not true, that means the weight
was entered in pounds. So we need to convert
it to kilograms. Once again, we
declare a variable converted and set it
to wait times 0.45. And then we print
weight in kilograms. And here we concatenate the string with
converter already. Now we need to
terminate this block. So we press Enter, then Shift and Tab. Good. Now if we
run this program, we're going to see an edit. Let me show you. So let's run it here. I'm going to enter one sovereignty and
typer, lowercase l. Okay? Years and error can't
multiply sequence by non end of a float. So this is where we got this error when we try to
multiply the weight by 0.45. So that is line
seven in our code. Now the reason this
is happening is because the variable is
storing the string object. Because earlier I told you the input function
always returns a string. So here we need to convert
the way to a number. We can entered. We can either use the
end or float function. Let's run the program
one more time. So 170 in pounds. Here is the second error. It can only concatenate string, not float to string. And that error
occurred over here when we tried to print
the weight in kilograms. So look at line seven. In this case, weight
as an integer. We are multiplying an
integer by a float, and the result of this operation
is going to be a float. Now on line eight, we, we are trying to concatenate or combine a string with a float. And Python doesn't know
how to execute this code. To solve this problem, we need to convert
this flow to a string. So here we use the
built-in STR function. Let's run the program
and see what happens. So 117 pounds, and here's
my weight in kilograms. Now, let's try entering
a weight in kilograms. So run it one more time. Let's say 76 kilos. Here we get a familiar error. On line phi value. We try to concatenate
a string to a float. So once again, we need to convert this to a string object. Now, let's run the
program one more time. 76 kilos is equal to 168 pounds
14. While Loops: In this tutorial, we're going
to talk about while loops. In Python. We use while loops to repeat a block of code multiple times. E.g. let's say we want to
print the numbers one to five. A poor way of doing this
is writing code like this. Print one, then print to
print 34 and print phi. Now, why this is a bad approach? Well, what if we wanted to print the numbers one to 1 million? We don't want to write 1
million lines of codes, each line printing a number. This is why this is where we
will be using while loops. So we will start off by
declaring a variable like i and set it to our initial
number, let's say one. Then we type. Here,
we type of condition. So once again, we can use
our comparison operators. Let's say I less than phi, less than or equal to five. As long as this
condition is true, then the code that we write inside of the file block
will get executed. So here we type a colon
and then press Enter. Now we have a block of code. In this block, we can print i. Then we need to increment i one. So we set i2 I plus one. If you don't do this, I will always be one. And this block will block, this block of code will
get executed indefinitely. It will never terminate. Basically, our
program will continue running until it
runs out of memory. In the first iteration, I is one is less than phi. So Python is going to
execute this block of code. It will print one on the
terminal and then I becomes two. Then the control
moves back over here. So Python evaluates
this condition. Two is less than
or equal to phi. The condition is true. And once again,
Python is going to execute this block
one more time. In the second iteration, we're going to see
two on the terminal, and then I will become three. So this will continue until this condition is
no longer true. Let me show you. So
let's run this program. Now we see the
numbers one to five. So this is the beauty
of while loops. I can easily change
phi two for 1,000. And by the way, I'm
separating these. I'm separating these three
digits using an underscore. This makes my number
more readable. We don't have to add this, but it just makes our
code more readable. So let's run the code. Now we see the
numbers one to 1,000. It's much easier than
thousand lines of code, each renting and number. Okay, now, let me show you
something really cool. So I'm going to
change this to ten. Now, instead of printing, I, I want to print
an expression. So I'm going to multiply
by a string, an asterix. Now, you might be confused here, because earlier I told
you that in Python we cannot concatenate
a number to a string. But here I'm using the
multiplication operator. When this is different, we can multiply a
number by a string. And this will repeat that string based on the value
of that number. So if I is one, we are going to see, we're
going to see one asterix. If I is five, we're going to see fire sticks. Let me show you. So when we run this program, we see this triangle shape because in the first
iteration I is one, and so we see one asterix in every iteration I is
incremented by one. So we see one extra Asterix. And finally in the
last titration is ten. And VC tell asterix
on the terminal
15. Lists: So we have learned about
three types of data. In Python. We have numbers, which can be integers or floats. We have Booleans and strings. These are what we prefer, referred to as primitive
or basic types in Python. Now in Python we have a
bunch of complex types, as well as these
complex types are very useful in building
real applications. In this tutorial, we're
going to talk about lists. We use lists wherever we
want to represent a list of objects like lists of
numbers or list of names. Let me show you. So I'm going to declare
a variable called names and set it to
a list of names. Here we add square brackets
to represent a list. And now we can add one or more
objects inside this list. In this list we add
a bunch of names like Chevron, John Rena. So this list, so we
separate this element, these items using a comma. Okay, Now let's print our list. So print names. Take a look. It comes out exactly
like how we wrote it. Now we can also get individual elements
in this list, e.g. if we want to get the first
element in this list, here, we type a pair
of square brackets. And inside of the
square brackets, we type an index. The index of the first
element in this list is zero. So now when we run this program, we're going to see SRO
weren't on the terminal. There you go. Now in Python, we can also
use a negative index. This is a feature that
I personally have not seen in other
programming languages. So if zero represents the
first element in this list, what do you think
negative one represents? It represents the last element in this list. Let's verify it. So let's run the program
and here Selena, what about negative two? Well, that represents
the second element from the end of the list. So let's run the program
and here's John. Then we can also change an
object at a given index, e.g. let's say here. Let's say here, we made a mistake and this
John should not be spelled with an H.
So we need to reset it. We type names of zero. Now we treat this like
a regular variable. So we set it to a new value. We set it to Jon without an H. Now, let's print our list
and here's our updated list. Beautiful. We can also select a
range of values, e.g. let's say we are only interested in the
first three names. So here we type square brackets. Here we need to
type two indexes, a start index and end index. Our start index is a zero because we want
to start from here. And our end to end index is
going to be 012 plus one. That's going to be three. So we add a colon three. So Python is going to written
all the elements from the start index up
to the end index, but excluding the end index. So it's going to return the
elements at index 01.2. Take a look. Here are
the first three names. And by the way, this expression does not modify
our original list. It returns a new list. So right after this
print statement, if we print our original list, you can see that
it's not changed. So this is how we
use lists in Python
16. List Methods: Earlier I told you that
strengths in Python, strings in Python are objects. Objects in Python
programming are kind of like objects
in the real world, like a mobile phone,
your bicycle, the remote control
of a TV and so on. They have certain capabilities. So if we type string
here and then press dot, we can see all the functions or methods available in a string. Objects in Python. Now, lists are also objects, so they have a bunch
of methods for adding items or removing
them, and so on. Let me show you. So I'm going to
declare a list of numbers, let's say 1,234.5. Now, to add a new element
at the end of this list, we can use the append method. So we type numbers that are pen, and here we type six. Now let's print our lists. So here's our updated list. Beautiful. Now, what if you want
to insert a number somewhere in the middle or
at the beginning for that, we use the insert method. So the first value that
this method expects is an index value and the type
of this value is an integer. So if I want to insert a value at the
beginning of this list, I should pass zero as the index of the
first element, right? So let's pass zero. Now the second value
is highlighted. So the second value
is an object. And the type of this is that basically means
this can be any type. We can pass a number, we can pass a Boolean, we can also pass a string. We can pass a list or any
type of objects in Python. So I'm going to
pass negative one. Now, let's run our program. You can see negative one up here at the beginning of our list. We also have a method
for removing items. So let's call removed three. Let's run the program. Three is gone. We only
have one to four and phi. Now, if you want if you want to remove all
the items in the list, we call the clear method. So clear this method
doesn't expect any values, so let's run our program. Our list is empty. Now sometimes you
want to know if a given item exists
in our list or not. To do that, we use
the in operator. So let's remove this line. Instead of printing
our numbers list, I'm going to print an
expression, one in numbers. So here we are using
the IN operator. We are checking to see if
one is in the numbers list. This is the Boolean expression. It returns a Boolean value. Take a look, so we get a true. Obviously we search for a value that doesn't
exist in this list, like ten, we get a false. Okay? Now finally, there are times you want to know how many times, how many items you have in
the list could do that. You can use the
built-in Len function. So let's print Len of numbers. So Len is a built-in function just like the print function. That is why it's
highlighted as purple. It returns the number of
elements in a list. Take a look. So we have five
elements in this list
17. For Loops: When writing Python programs, there are times you
want to iterate over a list and access each
item individually. Let me show you. So I'm going to declare a
list of numbers, 1,234.5. Now, if we print this list, it comes out exactly
like how we wrote it using the square
bracket notation. But what if we wanted to print each item on a separate line? That is where we
use the for loop. Let me show you. So we're not going to
print the entire list. Instead, we're
going to type for. Now we declare a variable which is called a loop variable. Let's call it an item that
anyway type in numbers. Next, we add a colon to
start a block of code. So this is what we
call a for loop. With this for loop, we can iterate over all
the items in this list. In each iteration, this item variable is
going to hold one value. So in the first iteration item, it is going to be equal to one, and the second
iteration is going to be equal to two, and so on. Now, if we print item
and run our program, we see each item on new line. So this is how we
use a for-loop. Now we can also achieve the same thing
using a while loop, but our core would be
a little bit longer. Let me show you. So we would have to
start by declaring a loop variable outside
of our while loop. Let's say I said, let's
say I set it to zero. Now we say while I
is less than here, we need to find out how many
items we have in this list. So we use the length function
len off numbers as long as I is less than the
length of the list print. Now, we see we can use the square bracket notation to get the element
at this index. So numbers of i. Now we need to
increment I by one, so I equals I plus one. Let's run the program
and see what we get. So we get the
numbers one to five. These are coming out
from our for loop. And then we get the numbers
one to 51 more time. These are coming out
from our while loop. Now, if we compare
these two approaches, you can definitely see
that the implementation using the for loop is shorter
and easier to understand. We don't have to use the
square bracket notation. We don't have to call
the Len function. We don't have to
declare a variable, loop variable and
then increment it explicitly with the for loop. In each iteration, the
item variable will automatically hold one
value in this list
18. Range Function: In this tutorial, we're going to talk about the
range function. In Python. We use the range function to generate a sequence of numbers. Let me show you.
So you type range. This is a built-in
function just like the print and input
functions here. Or you can pass a
value like phi, and this will return
a range object. A range object is an object that can store a sequence of numbers. Let me show you. So let's call that numbers. So this is a range object. In this object we are
going to have the number zero to five, but excluding phi. Now if we print numbers, we're going to see this
message range of zero to five, not the actual numbers, because this is the
default representation of a range object. To see the actual numbers
we need to iterate over this range object
using a for-loop. In the last video, you learned how to iterate
over a list using a for loop. But we can also iterate over a range object using a for loop. Basically, we can use
the for loop for, with any object that represents
a sequence of numbers. In numbers I colon. We're going to print number. Take a look. Now, we see the
number zero to four. So range of phi
generates a sequence of numbers starting from zero
up to the number four. We specify here. Now, if we supplied two values, the first value is going to be considered the starting value. The second value is going to be considered the ending value, and it is going to be excluded. So range of five to ten is going to generate the
numbers five to nine. Take a look. There you go. Now we can also
supply a third value, and that will be used as a step. So let's say instead of having a sequence of
numbers like 56789, we want to jump with
two numbers at a time. So 57.9 and so on. So here we pass to as the step. Take a look. So we get this odd
numbers, 57.9. So this is the range
function in Python. Like quite often, you see the range function used
as a part of for loop. Because we don't really need to store the result
in a separate variable. We can call the range
function right here, where we are using the
numbers variables. So we can type range
of phi and this will return a range object or link
the numbers zero to four. Take a look. There you go. So we don't really need to store the numbers that result
in a separate variable. Like there you go.
19. Tuples: In this tutorial,
we are going to talk about tuples in Python. Tuples or lists. We use them to store a
sequence of objects, but tuples are immutable, which means we cannot change
them once we create them. Let me show you. So I'm going to start by
defining a list of numbers 123. Now, we use square
brackets to define a list and parenthesis
to define a tuple. So now these numbers with numbers variable
is storing a tuple. If we try to reassign, let's say the first element
we are going to get an error. Tuple object does not
support item assignment. So this is what I meant
by tuples are immutable. They are unchangeable. Also, if we type numbers dot, you don't see any
methods like append, insert, remove, and so on. We only have content
index count. Index count, count returns the number of occurrences
of an element, e.g. if we have, let's say, two threes in this tuple
and call count of three, this will return to. And the other method we
have here is indexed and this returns the index of the first occurrence
of the given element. Now, these other methods you see here that start
with an underscore, they are called magic methods. Tuples are immutable. We cannot change them
once we create them. And now practically speaking, most of the time you
will be using lists. So there are times that once you create a list of
objects, you want, you want to make sure that
somewhere in your program, you or someone else is not going to accidentally
modify that list. If that's the case, then you should be using a tuple