Transcripts
1. Introduction: While most people think that this is what
programming looks like, it's actually much easier and simpler than you could
ever have imagined. Don't believe me, have a look at what the experts have to say. It's a skill that
is very accessible. It's not like a rocky science. It's not like you have
to know everything. You can do a lot
of it by yourself. You don't need anybody's
permission to do anything. You can just break things and it just build back up,
nothing happens. You don't have to be someone who sits in front of a computer 247. Excuse. Hey, everyone. I'm Basil, and in this class, you're going to learn
everything you need to get up and
running with Python. I started programming
several years ago, and I've been teaching it for
more than three years now. It was very rewarding for
me to see how the computer could do all the work with
just a few lines of code. And soon I realized the
massive potential of Python, since we're heading into
completely computerized world where programmers are
in very high demand. If you really want to
get into programming, but don't know where
to start, this course was made with you in mind. It's composed of a series of very clear and
concise videos that will cover all the
fundamental topics of Python. These include
downloading Python, printing texts, using math
and Python, variables, inputting data, if
else statements, while loops, functions, as well as several checkpoints along the course to check
your understanding. So what are we mating for? Let's jump right into it.
2. Python Overview: Stats for Nerds: Now before we get into
the actual programming, I find it very
important that you know what exactly Python is and the key features and properties of this amazing
programming language. There are three main points
that I want you to know. First of all, is that Python is a high level
scripting language that can be used for a wide variety of interests related tasks, system administration,
and texts processing. Second of all, I want
you to know that Python is an
interpreted language. Interpreted languages do not
need to be compiled to run. This means the programmer
could quickly change the code and then
quickly see the results. Third of all, I want
you to know that Python is an open source
programming language. This means that it's
available for everyone, that it is easy to
read and powerful. At the same time.
3. Downloading Python: Okay, So downloading
Python is really easy. You only have to go to
your search engine. In the search box you're
going to type Python download. There you go. Then you're gonna get
a bunch of results. But the safest and
most reliable website to download Python
prompt is SURPRISE SURPRISE! the Python
website itself! You're going to click that. And then here I'm using Windows, so I just have to click
this yellow button. because here it says "you can download
the latest version for Windows" if you're using
another operating system, you can just go
ahead and click on the macOS, any other, Linux. And if you want to see
the previous releases or any other releases,
you can go here. I'll releases and depending
on your operating system, you'll choose Windows, macOS,
other platforms. But it's as simple as
clicking this yellow button. So I'm going to click that and it downloads here in the corner. All right, Once it's done, you can go to click that great and then you click Install. Now. You click Yes. Now I'm not sure if this
appeared on the screencast. But what happened was I got
a notification from Windows that says if I want to allow this to download
and I just clicked yes. So if you see that,
it's a 100% safe. Now this could take a
little bit of time, okay So you just gotta
be patient until the bar reaches the end. Then it will be
on your computer. Still initializing. Okay, there we go. We
have some progress. Now this only usually
takes about a few minutes. More or less of course. Okay, Almost done. Okay, set up was
successful, you're done. You just click Close. And there you go. You type Python here. So we want this one, the IDLE, this is the
one we're working on. I'm gonna double-click
that. And there you go. This is Python. You have Python on your device.
4. Displaying Text : The first and foremost
thing to learn in Python is definitely
how to display text. But we have to stop here
for a moment and ask ourselves what exactly
is text in Python? Well, text is very
simply any character. It could be an uppercase letter, lowercase letter, a number, or symbol that is in-between
two inverted commas, and they have to be in-between
two inverted commas. Texts in Python is called a string and it's
shortened to STR. And it is displayed using
the print function. Now that you know the basics, let's go see this in practice. Okay, so since this is the first ever
program code for you, I'd like to walk you through
the process step-by-step. So when you open Python, you're gonna be in
the IDLE shell, and this is essentially where
the results of your code will appear. And when you want
to write the code, you have to open a new file. So file, new file. And there you go. This is
where you write your code. Our first code will
be displaying text. You want to print a message. And to do that, we're going to use
the print function, which is 'print' in lowercase
and two parentheses. And now whatever you want to print, you have to write between
these two parenthesis. So if I want to print a string, I just have to put
two quotation marks because this is the
structure of a string. And then here I'm going
write any sentence. So let's say one
kilogram of mangoes. costs $3. Okay? So I chose the sentence because it
has numbers like 1 and 3. It has a symbol,
the dollar sign, and it has an uppercase letter, even though it's
grammatically incorrect, and also lowercase letters. Now, this is just to
show you that a string accepts any type of character. And okay, so this is your
first type of program code. If you run this, you have
to go to Run, Run Module. You have to save it. Let's go ahead and
save that, "example". And there you go in the shell, we have displayed the text One kilogram of mango costs $3. Congratulations, this is
your first line of code. Now I want to teach
you a bonus skill, and this skill is
converting text. To do that, I have to
go back to our file. And instead of this string, I'm going to have a one word
string, which is banana. Banana for example. And I'm going to run that. Okay? And as you can see, I have banana in lowercase. But then say, I changed my
mind and I want it to be in uppercase instead
of removing it and then, retyping it in uppercase. But I can do is dot
upper right there. Run, Run module. Okay? We get banana in uppercase. You can do the exact opposite. We can have it in upper case here. banana. Then use the dot lower
function, dot lower. Run, Run module. Okay? There you go. We have
banana in uppercase here and then lowercase in the shell. And one last thing
is you can have, for example, banana, then you can
capitalize just the first letter. So we only want
the B capitalized. We got to do dot capitalize, wonderful, now run, run module. Okay? We have just the B
capitalised and the rest is lowercase. Yeah, that's it. This is printing text.
5. Using Numbers in Python: Math can get repetitive. Those numbers can get confusing. and those decimal points
really got on my nerves. However, what if I told you that Python offers a
solution for this? All you have to do is
write the correct code. And this is exactly what we're
gonna be learning today. But before we do that, I
have to introduce you to two key concepts when
it comes to numbers. So the first key concept is that there are two types
of numbers in Python. The first one is integers, and you may be familiar
with those from Math. Integers or any whole numbers. They could be positive,
negative, or 0. And Python recognizes
integers as 'int'. And if you type that
in, it turns purple, indicating that it
is built in and recognized. The second type of number in Python
it is called a float. A float very simply is
any number that has a decimal point
regardless of what's before or after
the decimal point. And these can be
positive or negative. Now be careful, this
can get really tricky. For example, this
15 and this 15 have the exact same
numerical value. However, since this
one has decimal point, Python deals with it
as it as a float and deals with this
one as an integer. Anyways, floats are built-in and recognized by
Python as 'float'. There is no shortening for us, and of course that turns purple. Now the second important
thing you have to know is that numbers
are not strings. You do not put them into inverted commas because they are completely
different datatypes. To compare, strings are a set of characters that
have no numerical value, while numbers have a value
that Python recognizes. To make things more clear. Let's think of numbers as
figures that represent units. For example, five
represents five units, three represents
three units and 7.8 represents seven units and eight-tenths of a unit
and so on and so forth. So whenever you use numbers in a situation that demands
a numerical value, you do not put them into inverted commas
because if you do, they will lose this
numerical value. And thus they'll be ineligible for
mathematical calculation. Now, don't get me wrong. This does not mean
that you couldn't or shouldn't use
numbers and strings. You can, and it's
perfectly fine, just like I did in
the previous lesson. However, what I do mean
is that if you want this number to hold
a numerical value, if you want to use
it in a calculation, you have to use it as an integer or a float rather
than as a string. Confused? don't worry, you'll understand everything
in practice. Okay, so let's start really simple by printing
some integers. So 'print', lets have seven, for example. Let's print a negative
integer like minus 12. Let's print 0
because 0 is also an integer so you run, run module. Okay? And there we have our integers. Okay, Very nice. Now let's have some floats. So 7.98, minus
12.45, 0.26 for example. Let's run this, run module. Okay, there we have our floats. Now what I want
from you now is to experiment with the numbers,
mess around with them, just like try some
very big numbers, very small numbers,
integers and floats. And I find it very
important that you know how to tell them apart, that you know which is an
integer and which is a float. So go ahead and start
printing some numbers. And this is not really how
we use numbers in Python. We use them in
mathematical calculations, which is what we're gonna
be getting into soon. But for now I have to make sure that you're
familiar with the numbers. So go ahead and
start. And remember, do not put the quotation
marks because we're using them as numbers
and not as streams. Now once you're very
comfortable with using numbers as both
integers and floats, you can go ahead and move on to using them in calculations. Now today we're gonna
be talking about six types of operations. And these are
addition, subtraction, multiplication, division. Powers or indices and modulus. Now let's start with the
easiest one which is addition. So print and two brackets and I'm going to add two numbers
like 5 and 9 for example. You run that. Okay. Awesome. We got 14. Now, remember when I told you how you can't use them as strings, let's see what happens if I put two quotation marks
and turn it into a string. run, run module, okay Yeah, you get five plus nine, just deals with them as characters and sticks
them together. It does not do the actual operation because the number is lost their
mathematical value. Now let's go ahead and have
a look at subtraction. What I have to do is
print two brackets. Then here I'm going to
have like nine minus six. I should get three. The
minus sign is the hyphen. So we run, run module. Okay You get three. Awesome. Let's have another
example where the first number three, the second is six, so that we get a negative
number. We get negative three. Very nice. Subtraction is as
simple as this. Now let's move on
to multiplication. Multiplication is
also very simple. Print. We have nine times six. The multiplication
sign is the asterisk. And run, run module. Okay, we get 54. Let's have another one. For example, 4.8 times six. run, run module, okay
Now as you can see, you can, you can try both
floats and integers. Okay, So now let's
move on to division, go ahead and write print again. And the division sign
is the forward slash. So for example, we
have 72 divided by 9. we should get 8 So we run, run module. Okay? Awesome, You have eight. Let's try another one. We have, for example, 35
divided by seven. Okay We get five. It's very easy,
it's very simple. Now let's move on to powers or indices, for those of you who
don't know what this is. For example, two to
the power of three means two multiplied
by itself three times, so that's two times two times
two, which equals eight. To do that, we're
going to print two. And to raise a number to the
power of another number, you put two asterisks and three. This means two to
the power of three. Run, run module. We should get eight. Awesome. Now let's
try other numbers. So we get like a
crazy large number. Seven to the power 473 run, run module. Okay, awesome. You could just have
fun with this, okay? He could drive very
small numbers, very big numbers, etc, until
we got the hang of it. Now the last mathematical
operation for this lesson is going
to be modulus. And to illustrate this, I'm going to use
an example because this could be new
to some of you. So print, two brackets, five modulus two, so modulus
is the percentage sign. So five modulus two five
percentage sign too. and You read this as five mod two. And what Python does
is it takes the five, divides it by the two, and then it spits
out the remainder. So five divided by two
is two, remainder one. So when I run, I should get one. Let's try another one. Let's try 17 divided by five. For example. This should be three,
remainder two. So I should get 2. Now one very, very important note is that Python uses the
order of operation. What I mean is if I do print
four plus five times seven, it's going to multiply
five by 7 first because it prioritizes
multiplication over addition. So five times seven is 35, and then it's going
to add the four. So we're gonna get 39 run, run module. Okay, Great. Now if you want to add
the four and the 5 first and then multiply
the whole thing by seven, you have to add brackets. This is how we prioritize
certain operations over others. So this way we have
four plus five, which is nine, and
then nine times seven. This gives us 63
run module, okay 63. You see the addition of the brackets change
the number from 39 to 63. It's very simple,
it's very easy. Just pay attention to that and everything will go Amazing.
6. Bonus Skills: Numbers in Python: Once you've mastered the basics, it's always a good idea to step things up a bit and
challenge yourself. So how about you go ahead and
check these bonus skills? Now, nearly all programmers use numbers at some
point in their code. So I'm going to walk you
through the bonus skills and awesome functions that you can use when dealing with numbers, since they form a
huge part of Python. Now, the first one is to classify if a number is
an integer or a float. And do this, I'm gonna use print. The function we're going to
use is the type function. So type in lowercase
and two parentheses. It turns purple because
it's a built-in function. In here. I'm going to have
a float like 4.9 for example, run molecule. You see it's classified
4.9 as a float. So let's try nine for example, which is an integer. Okay? We get to class int. So it tells us that
this is an integer. Now let me show you what
I meant with the example of 15 at 15.0 in the
previous lesson. So 15 without a decimal
point, run, run module. Okay? This is an integer. Now if I do 15.0, the
numerical value is still 15, but it has a decimal point. now so run run module. It is now a float. At some point when you're
writing your program, you may find it useful
to know how to convert numbers from floats to
integers and vice versa. Now to do that
we're going to use the print function first
to visualize the results. Then here I'm going to do
INT float in here like 15.0. Okay? Now what this does is it takes 15.0 and converts it
into an integer, and then it prints the result, it shows us the integer
version of 15.0 There you go, It prints 15. If you want to do the opposite, you want to convert an
integer into a float. you use Float. Let's have
15, for example. Run module, okay? And you get 15.0, which is
float version of 15. And you can find this especially useful when dealing in
physics, for example, with quantity tables
where you have to be consistent with a number
of significant figures. So you can't have both floats and integers in the same table. You can either have all
floats or all integers. So we're going to
have to use this and convert either to either. You can also convert
a number to a string. So print STR, you type in the number like For example, 16. Run, run module. Okay? Now there is no visible
difference here. But what happens is this 16 now does not have
numerical value. It just lost its numerical value and you can no longer
use it in calculations. However, you can use
it in a sentence. So this is especially useful
when you're going to use a number alongside a string
or alongside a sentence. Now to show you that this has lost this numerical
value, i'm gonna try to add that to say 14. Run module. Okay? It prints out an error
message because it's not right, it's not INT. Now there's another very
useful function that gives you the absolute
value of a number. If you don't know what that is, think of it as the distance
of a number from 0. For example, five is
five units away from 0. Minus five is also five
units away from 0. So essentially to find the
absolute value of a number, you just remove
the negative sign. If it was negative and
if it was positive, you just leave it as it is. So to do that, print
the abs function. and let's have. For example, negative 93. Let me run this run module. We get 93 because this is the absolute value
of negative 93. It is the distance away from 0. That negative 93 is, it is very simple, very easy. You might not use this now, but you certainly
will in the future. And we have another function, which is the power function. So pow, to parentheses And this is one of the
functions that allows you to pass it the two
pieces of information. So for example, 2 and 3, okay? And what this does is it raises two to the power of three. It is the exact same. As doing this. So print 2**3, we have the exact same value. Let's see that, they
both give us eight. It's very nice to
know both of them. Now we have two
other functions to which you can pass two
pieces of information. These are the max
and Min functions. Let's start with max. You may simply print then
max, two parentheses. And in here you're going
to have two numbers like 13 and 9 And what Python is going to do is it's going to compare 13 and 9. Then it's going to print
the larger number. So run the module. Okay, we got 13 because
13 is greater than nine. And then there is
the Min function. So min 13 and 9 run, run module. This is going to give
us the smaller number. Gives us nine. Very simple, very easy. Now the last bonus skill
I want to teach you today is going to be how to round numbers using the
round function, print and round, two parentheses. Now what happens here is whichever number you
type here, like 4.9. Now this rounds to 5 them. So if I run, run module, okay, it gives us five. And then let's say I want to
round it down to like 4.2. This should round to four. Run, run module, okay? It rounds to four. So what
this function does is it basically rounds the number
to the nearest whole number.
7. Checkpoint: Numbers in Python: You are doing great so far. Are You Up For a Checkpoint, let's check your understanding with
this very simple exercise. In this exercise, you will write a very simple yet
very useful line of code that will help
us convert between degrees Celsius and
degrees Fahrenheit. So first you have to go to a search engine to find
a formula for that. And I found this one, which is degrees
Celsius times nine over five plus 32 gives us the
degrees in Fahrenheit. So now that you
have the formula, I want you to write a code
that will convert 20 degrees Celsius to degrees Fahrenheit. Then I want you to round that answer to the
nearest whole number. So you're going to have to
use the round function. I want you to try this yourself. So go ahead and pause the video before the answer
appears in 321. Now the key to writing this very simple
line of code is to substitute 20 into
the formula that converts degrees Celsius
to degrees Fahrenheit. Because we want to
convert 20 degrees Celsius to the equivalents
in degrees Fahrenheit. So very simply we put
20 inside the formula. So 20 multiplied by nine divided by five and then plus 32. And notice here how I did
not use brackets because multiplication and division are already prioritized
over addition, so there's no need
to add brackets. Then we want to round our answer because the answer has to be
to the nearest whole number. So round function.
right there. And finally we have to
add the print function. So there are Python
prints the answer. There you go. So what happens here is it
takes 20 degrees Celsius. It puts it inside the
formula right there and calculates the number of
degrees in Fahrenheit, the rounds that answer to
the nearest whole number. And finally, it
displays the result in the shell because we have
the print function. So run, run module. Okay. Yeah, Let's go ahead and check that in
our research engine, you have 20 Celsius equals 68 degrees Fahrenheit.
Awesome job.
8. Variables: Using variables is a very
foundational aspect of programming that you're
really going to want to know and master. And here's why. Because in Python you're going
to be using a lot of data. This data can sometimes be hard to manage and overwhelming. This is where variables come in to make things much easier. Variable is the location
in memory in which you can temporarily
store text or numbers. You can think of it
as an empty box in which you can store
anything you like. Simple, right? Wonderful.
Let's jump see it in practice. So I'm going to start by showing you the structure of a variable. The structure is very simple. It's basically the
name of the variable, the equal sign and
then its value. Let's call a variable
"a", equal sign, followed by lion, which is the value of a. So basically what this means is wherever a occurs
in the program, we're referring to a lion. And the name is a, there is an equal sign and
its values the string lion. Let's have some
more variables like b equals nine. For example. Secret equals I equals I ate the last cookie
00:01:15.710 --> 00:01:16.670
let's have Number equals 36. lets Have my pet. Equals bunny. Finally, let's have
anwer equals true. Now there's a few things
I have to point out here. First of all, the
structure of a variable is name, equal sign, value. And the name could be
one letter like a or b. It could be one word like
secret or number or answer. It could be a
phrase like my pet. However, notice here
that you can't have spaces in the name
of a variable. So my pet was separated
by an underscore and not by a space because you can't have a space in
the name of a variable. It's either an
underscore or just like no spaces, which
I do not recommend. Go ahead and add that underscore. And that's it for the names, the equal sign is
pretty standard. And finally, we have the value. Now, variables can take
three types of values. They can take a
string like lion, I ate the last cookie and bunny. And it could also take
numbers like 9 and 36. And finally, we can
take a Boolean value. Now, a Boolean value is
basically either true or false. It's only these two values, it cannot take any
other two values. And you may not be very
familiar with them in our day-to-day interactions
and our day-to-day life. However, in programming, we
nearly use them all the time. They're very important. Just to illustrate what
exactly they are. For example, let's
say if I asked you, Are you left-handed? Now If you are left-handed,
you say true, Viagra attended, and if you're not, you're gonna go false. I'm right-handed, but there's no other options are either
left-handed or right-handed. It's either true or false. There is no other option. So this is basically
how booleans work. Let's start with a
very basic exercise, which is simply to
print variables. So I'm going to have
w equals popcorn, I'm also going to have m equals 92. So if I want to print
the word popcorn, I can simply do this. Print w. What Python does here
is it comes and it sees that it has to print W.
It revises the code. So W equals popcorn. So it's going to print popcorn. So run module, as you can
see it printed popcorn. Let's print m. For example. Run, run module. You have 92. So now let's have an example to see how we use
variables in Python. Now, I'm going to start
by writing a sentence. For example, I have a pet. bunny. My bunny is called picasso. And I've never seen this cute. Okay, let's try this. And there we have our sentence. Now. Let us say that four years later this morning grew up
and became a rabbit. I'm going to have to
change the sentence and replace bunny, rabbit every time it occurs. And let's try and do that. Okay? Yeah, rabbit. Rabbit. And we also have
a rabbit over there. Now this was not very fun and I could've made
a lot of mistakes. I could have missed
one of the bunnies, I could have misspelled rabbit. And let alone It took a
lot of time and effort. Imagine having to code that's
thousands of lines long. Were we mentioned bunny
several 100 times. And you have to change every single one of
those into rabbit. This is not very doable
because you won't have enough time and you will
make a lot of mistakes. So this is where
variables come in. To show you that, I'm going to create a variable,
let's call that pet, for example, equals bunny, okay? what I can do here is
remove the sentence. I'm going to add a new one. So let's do i have a pet. Then here I'm going to
interrupt the sentence by closing the string, add a new comma, the name of the variable. Another comma, then
reopening the sentence. Now what happens here is when Python starts to read
the print function, it types in, I have a pet. Then it sees this one, it sees pets. So it's going to go
ahead and look for pet in the code and it's going to find that it's equal to bunny. It's going to insert bunny over here. And that is
going to continue. One last thing. There are no spaces
over here because Python automatically
inserts the space. So I have a pet bunny. Um, so my, buny, then we're going to close that. Again. comma, pet, comma reopen
is called picasso. It's called Picasso. I've never seen a Now, we close, comma. add a comma, and reopen. this cute. and close. Okay. I know this was a bit harder
than just typing in bunny, but let's see what happens
on a run, run module. Okay? So I have a pet bunny. My buddy is called Picasso. I've never seen a
bunny of this cute. It just put bunny, right where I put that. Wherever I inserted
this variable, it replaced it with bunny. Now let's say that four
years later it did grow up. What I do is I just replaced bunny over
here with rabbit. Once in the variable. When I run, I have a pet rabbit, my rabbits, I've
never seen a rabbit. It's replaced bunny with rabbits every time
in the sentence. Now this was much easier than looking for bunny every time it occurs in a sentence and just replacing it with rabbits. Look, I didn't make any mistakes because they
only have to write it once. I couldn't have missed one because variables
are already there. And so this makes life much
easier for a programmer. So far we've seen variables
that represent strings, but we haven't seen variables
that represent numbers. So I'm gonna go ahead
and start with that. And to illustrate this, I'm going to use an algorithm
that I completely made up. So let's do print the result of, let's say three plus five minus three times five plus three
to the power of five. Okay, let's try
this module, okay? The answer is 236. Then let's say that I
just made a mistake and it was supposed to be
seven instead of five. So I'm gonna go ahead and change five to seven every
time I see five. And another seven here. Run, run module. Okay? I got to 2176. The answers are very different. And you see, this one is just
like a few characters long. This algorithm is a very
small algorithm compared to the others that you might use in your programming careers
that could be lines long. And so it's not very
doable to just go through all the lines and
check for fives and then replacing them with sevens. And you wouldn't know which
5s should already be there, in which five
should be replaced, the sevens, because
you know, not all of the fives could have to be
replaced with a seven. And this could have
some confusion with it. So we can do is
add some variable. Now I'm going to
create two variables. These are going to be N1. Let me set that to
be equal to three. And N2, which is going
to be equal to five. Then here I'm going
to replace those. Instead of using actual values, I'm going to use the
variables inside. So N1 and two. And one. Oops, I'm sorry. It should be N two to one So here the algorithm used the values of 3,5
and so we got to 236. And let's say I thought that this was wrong and
it has to be seven. I just do this and this. We change it with the
click of a button. Run, run module, okay? And we got the correct answer. and what you can also do is have this algorithm
as a variable. Let's do for example, a equals. Say that we made a mistake
in the algorithm itself. Let's say this shouldn't
be a negative sign, this should be a plus sign. We just changed it here. Then we can say print
a run, run module. Okay, this is the
result when we, when we use a positive
sign over here. And so basically, basically
you can have it as a rule of thumb whenever you're going to repeat a value more than once. And this value could
potentially change. You use a variable,
or for example, wherever you have a very
long sentence or very long string that
you don't want to keep typing again
and again and again. You can just replace it with
a very short variable name. Just add that instead
of repeating and repeating and repeating
and typing it in and typing it when
it's very long. It's gonna be a
pain in the neck. So you can just make things much easier by using variables.
9. Bonus Skills: Variables: Once you've mastered the basics, it's always a good idea to step things up a bit and
challenge yourself. So how about you go ahead and
check these bonus skills. The bonus skills for
this class will revolve around variables that
represent strings. So let's go ahead
and create one. For example, deserts
equals caring. Wonderful. So let's
print dessert first. Desert. I'll do. Okay. Wonderful. We have our
variable right there. Now the first thing
we can do is we can determine how long
this variable is. If I'm going to know how
many characters there are in the phrase carrot cake. It can be very useful sometimes. I can just get the
length of this variable. To do that, all I have to do
is use the length function, which is Len and two brackets. So if we run, run module, okay, we get 110. This is the number of characters there are in the
phrase carrot cake. So let's go ahead and
tell them real quick. So we have 123456. The space is a character, so 7891011, we have 11
characters in this string. Now sometimes the
string can be very, very long that you can count it. So you can just use
the length function. Now sometimes it can be very useful to grab just
one character, just one letter from
this whole phrase. Let's say I want the first
one or the last one, or just any character in there. I can identify each
character by its index. An index is basically
the position that Python gives to a
character inside this phrase. And Python starts
counting from 0. So let me show you this. Starts counting like
this. So the first one, mix of 0, then 1234 and
so on and so forth. If I want to grab
the first letter, I'm going to grab v letter
with index equals 0. What I'm gonna do is
this friend's desert, the two square brackets and then the index of the character I want to print, which is 0. In this case, I want to get C because it's the first
letter over there. Also don't leave just
random numbers hanging in there because they might
ruin your code. We get C. Very nice. Let's do the second
one which has index equals one molecule. Okay? You get a, let's try the eighth
one for example. I'll do another. A lovely coincidence. Now, when I did h over here, I did not want the
eight character. I want the index of eight, which is basically the
seventh character. Now we can do the exact opposite using the index function. Now what this does
is it's going to take the character
that we give it, and it's going to
give us its index. And it's the exact opposite
of what we just did. What we did was we gave it the index and it gave
us back the character. So now we want to
give it the character that's gonna give
us back the index. Let's try this using the
index functions of print. Desert dot, index, two brackets. Then say you want to grab the E. Now, E is the last one. So it's going to
have the tenth index because it's in position 11. So it starts from 0. So we're going to have
ten run, run module. Okay? Awesome, we get
ten. Now finally, we also have a very simple
and very useful function that is called the
Replace function. And I think the name
explains itself. So basically what this
does is it's going to take one word or one
character from the phrase, and it's going to
replace it with another word or
another character. Let's try that. So print. And then we're gonna do
desert Triple P S over there. Okay, so deserts dot replace. And then two brackets in here. And we're going to cost
two pieces of information, that original world
and the new words. So the original word is carrot, and the new word, for example, is chocolate. This one's gonna
do is it's going to find carrots and this drink, and it's going to replace
it with chocolate. Run, run module. Okay? We get chocolate cake. And it also works
with single letters. But it's all very relevant here. So if I want to
change all the ACEs, we can do this run, run module. We get carrot cake, k, whatever that is. So this is just for fun. You might sometimes use it, but you're never going
to have to change like all the A's and two 0s because, you know, you must have
worked with an egg, right?
10. Inputting Data: In Buddhism, integral and
human computer communication. Because if you don't feed
the computer any data, it won't give you
any information, and thus, you won't
get a result. And this is y input is one of the most essential and also easiest skills
learned in Python. Now, what do you do? An input is basically, we're going to ask the
user for some information. We're going to ask them
to give us some data. And then we're going
to take this data and assign it to a variable. So we're going to store this
data inside a variable. Now, what's the difference
between this variable and the variables we use to talk about in the previous lessons. Well, basically this variable has a value that
the user decides. The user can choose anything to swear inside this variable. Meanwhile, the variables
we talked about earlier, how the predefined
value that is set by the programmer and the user
has no control over it. Awesome, Let's go see
you got in practice. Now the basic structure of
inputs is as easy as this, but in two parentheses
or two graphics. However, there isn't much information we
can get from this. So when the user is
using the program, there's no prompt
or no question that asks him to enter information. He has no idea what to ensure. And so we can type in
a prompt over here. So the prompt is string. It says, for example, Enter, hey, you're going to want
to leave a space there so that there's a space between what they
enter your question. And then we already said that we're going to
assign this input. We're going to assign this
data into a variable. So let's go ahead and
add a variable there. Let's call the
inputs name equals. So what this does is
it's going to take the name of the user
to enter their name. Then it's going to store it
inside the variable name. Now let's print this. Let's put a message. I'm sorry. Let's print a message that
it will welcome the user. So print Hello. Better remember this a comma. We're going to
enter the variable. And let's do an
exclamation mark. Prime. Python. Python 3.7.43.4. Nice to meet you. Wonderful. Now let's run
this and see what happens. So here the program asks
us to enter our name, enter your o, nor any
spelling mistakes. It's okay. Okay, so enter your name. So my name is misused and then enter because hello bits
you write Python 3.084. Nice to meet you, nice
to meet you too, 5M. Now in the previous example, we saw input being used as
a strength because we have to enter our name and name
is sets and so it's trained. But we sometimes have to enter numbers that have to be
used in the calculation. And let's illustrate
this with an example. So input, enter your age. Now, age is an integer and convert this input
into an integer. We're going to have to do this. And t in brackets. It's very simple, it's
very easy because input, enter your age as it is. This is a string. But
when we add the INT, it converts the
value that the user enters from a string
into an integer. And of course you have to store this value inside a variable. Let's call that h n equal sign. I'm going to add another
variable very similar to this. I'm going to call
that current here. I just copied and pasted that. So that's good. Change up the names. Print here, enter here. We're going to calculate
the year that the user was born in Northern to do this
by using simple mouth, which is the current year
minus his or her age. And so here, the values that the user input will be
used in the calculation. So we have to be
integers or floats. But since age in years
are most commonly uses integers where we're
going to use the INT function. So let's go ahead and do this. So a year is equal to current year minus the H. And we're going to print
welcome message here. It's gonna say you were born and then comma here, your score. One from this. So run, run module, okay. So enter your age. Let's say that I'm 29. Current year is 2022. I was born in 1993. Let's try another
one, run module. So enter your age, Sam, 55. And the current year is 22. I was born in 1967. Wonderful. And sometimes there are
situations where you have to use an input that is a float, for example, with temperature. Now, the temperature can say, could decimal values like
thirty-seven point five. So I'm gonna go ahead and create a variable called temp out. As we come to trial type, that's going to be
equal to float. You put two brackets and Enter the
temperature outside. I'm going to create a variable
called temperature inside. So this is going to be equal to the temperature
outside minus ten degrees. So this is just my own
sort of calculation. You could use whatever you want, equal to ten minus ten. Now I'm going to print inside. So the temperature inside
is ten minus 1010. Out is an input that the user gives us and it can
take it as small values. Run the module. Okay, Wonderful. So the temperature outside, let's say it's 29.8. We temperature inside is
ten degrees less than that, and it is 19.8. And this is basically
how you use inputs as both integers,
floats, and strings.
11. Checkpoint: Creating Mad Libs: You are doing great so far. For checkpoint. Let's check your
understanding with this very simple exercise. Now for this checkpoint, I want you to write a program
code for the game Mad Libs. Now, here's the general sample that I used. I made that up. So basically I'm going to start to go through
the welcome message. So hey, they're always
playing a fun game. Then I'm going to have, this game is called Mad Libs. Here's how it works.
And then I'm going to explain how the game works. So I'm basically just printing some instructions over here, like welcome message and
then some instructions. So my whole prompt you
to enter REM words, the different
categories, and placed them in a random
sentence of a story. And his work substitutions
have humorous effect when the resulting story is then
read out loud. Let's start. So hope that's clear for you. And when we start to play the
game asks us to enter now. And so I'm going to
enter a sofa example. Enter number. Let's go with 15. Enter a color, pink. Name of a celebrity to Lipa. Now when we enter, you get these very
random sentences. So my best friends is sofa. 15 is my lucky number. And dual EPA likes pink bears. This is what makes
it very funny and has no meaning at all. It's just very random. And you can try all sorts
of different combinations. But for the first time
for this checkpoints, you can just try out
those that are used. And it would be wonderful if
you could try that yourself. So go ahead and pause the video before the solution
appears in 321. Now here's the code that I use
for writing this Madeline. So it's actually very simple
and it looks very long, but it's very simple. So let's have this
side-by-side with the result. So first of all, we have a printf function that displays the welcome
message right there. This is very easy
and very simple. When you have this blank space, you have there's
like empty line. To achieve this, you only
have to do the print and then to empty quotation marks. So there's nothing
between those. And this shows us that we want
a blank space right there. Then we have a new message
which is right there. And then the instructions,
you can just take those from your search engine or any
books that has methods in it. Or you can just not
have them in at all. But I add them just in case some of you didn't know
what amount with this. So you can just type in your own instructions for each and just type
in anything else. These are just simple
print function so that the display the user
sees is more friendly. And then this is the juice here. It asks us to enter now a number of color and
the name of a celebrity. And since we're asking the user to enter
some information, to enter some data, we
have to use the inputs. So then you have a
variable, input, variable here because
this is a number and use the INT I converted
it into an integer. But again, we have variable
input, variable inputs. So every time we took a piece of information from the user, we stored it inside a variable. And then very simply, we
printed some sentences. We've got a new line here,
force printed some sentences, and we entered the variables, the values that
the user gave us. So we entered the input
into our sentences because they are now
assigned to variables and those variables
into sentences. And so very simply, we print some print functions and then some variables wherever
you want to insert them. And this is very simply how you write the
code or a mountain. Now what I want you
to do is to go ahead and experiments with all
the different sorts of categories in words so that
you produce lots and lots of different Mad Libs and then show them to your
friends and family. And I guarantee you, you
will get amazing reactions.
12. If/Else Statements: We all face situations where we have to
make some decisions. And then based on
these decisions, we determine our next steps. Now very similar
situations, right? Some programming where we
have to make some decisions, then based on these decisions, we can run certain
lines of code. So basically, if else
statements decide the direction of flow
of code execution. So if some conditions are true, let's say that these conditions
were met by the program. Then Python will take
a specific course of action and it will
perform a specific task. Meanwhile, these conditions were false or if they were not met, then Python will take
another course of action and it will have
another task before. This is essentially what makes your computer a lot smarter. Now that you know
what it is, Let's go ahead and see
that in practice. Now let's start with a very
basic decision to make so that we can show you how
the if-else statement works. Now, we're going to
have a program that decides whether a
student passes or fails. It's gonna do that by comparing their grade out of 100 points. So let's say that they sit
a test out of a 100 points. And we're going to compare
their agreement that tests to the passing rate and the
passenger is gonna be 15 points. Now first we have to
get the students grade, and we're going to have to
do that by using the input. So grade equals hi In D. Input. Enter your grade. 100. Wonderful. And then we're going
to proceed and type in the if else statement
to make the decision. Now, the if else statement
is composed of two parts. The first one is
the if statement and the second one is
the else statement. Now the if statements
basically saves the condition. So if and then our condition two paths is this grade
is greater than 50. So if grade is greater
than or equal to 50, then we have a colon which indicates the end of
the if statement. Then we enter and we have
an automatic indent. Now this is because we
have to differentiate between the code
that's executed within the if statement and the code that's outside
the if statement. So anything that
is indented below the if statement is going to be a consequence of
this if statement. So what is the consequence of the grade being
graders and 50? The actual we're going to take is we're going to tell
them that they pass. So print Graphs. Now here, if they
don't pass, they fail. And this is because there
are only two options, passing and failing, and
there isn't any other option. And so basically, if the grade is not greater than
or equal to 50, we're left with an option which is the grade is less than 50. And so we can have
the else statement. Remember to get back
to the beginning of the line because we're no
longer inside the if statement. Writing the other part of
the if else statement, which is the else statement,
then colon cancer. And we have another event
for the else statement. Now I want you to notice how I didn't have anything
after the else. So basically no condition,
no comparison, nothing. It's just else and a colon. And this is because else basically wraps up everything
that wasn't in the if. So, anything that
does not satisfy the condition of the if it goes into the else
and that's it. So we have an
indented over there. And let's do else was print
a string so you fail. And this is very basically
an if else statements. Now let's run this. Okay, So here's how
it's going to work. It's going to ask us for the
grade out of 100 points. So let's how I type in 95. Okay, So now what it's gonna do is it's going to take the 95, it's going to compare
it with the 50. Is it greater than
or equal to 15? Yes, it is greater than 50. So it's going to print
congrats, you pass loops. We lost that. No probability. So 95, hang rats, you pass. And then let's say that we have another student that
scored a Thirty-five. Thirty-five. Now what's
going to happen here? This is going to take 35
and compare it with the 50. Is it greater than
or equal to 15? No, it's not. So it's going to move on
to the else statement. Else tells us that if the condition in the if
statement was not met, then you can proceed and do the consequence for
the else statement. It's going to type in you fail. Let's go see that. Show Enter. It says you fail. This is very simply how an
if else statement works. Now there's a few things I
have to comment on over here. Let's start from the beginning. You realized that I use I and t. I converted the
input into an integer. And this is because here we
compared it to a number. And mathematical comparison
is considered an operation. It has to be either an
integer or a float. Now, depending on the
nature of the grid, if it takes decimal values, you can just go ahead and
write float in there. But we took two integers over
here because it also works. So, yeah, Then we're
going to have, if it turns orange, else turns orange because
these are built in. And when you're done
with the condition, you put a colon over here. You have a colon after the else. This is the basic structure. Now, as for the condition, here we use an integer value and we compare it
with another number. Now here are the mathematical
comparison operations. These are greater
than, less than, greater than or equal to, less than or equal to equals. And it's not just
one equal sign, it's two equal times. Remember this? And we
also have not equals. So this just means not equals, greater than, less than, it's just not equals. These are the main operations
we're going to use. So again, greater
than, less than, greater than or equal to, less than or equal
to equals, equals. This is just equal to n. Exclamation mark equals
means not equal to. Finally, you have to pay attention to the
indenting levels. So basically, every
action you are going to take that
follows the if statement. So for example, if this
condition was true and you have to take
a certain action, you have to indent. But then when you're done with those and when you
want to say it's the other part of the
segment which is the else. You can go ahead and go back
to the main line level. Then when you're
done with the else, you're going to
indent again to show that this one follows on
from the else statement. Now in the previous example, we've seen how if else
statements can make decisions by comparing
numerical values. But we can also use
if else statements with Boolean values. Now let's see how this works. So let's have our
first Boolean value. So write equals true. Now to create an if
else statements here, we're going to have to do if, then write the name
of the variable. So if right-handed. And what this basically
means is if right-handed, if this variable has
the value of true, execute the following code, and enter it
automatically indents. Let's have print RH. This means Right-handed, Okay? And else, so this means that if right-handed
was not true too, if this was false, we're
going to print left handed. And print h bar h is right-handed
and LH is left-handed. Let's run and see what we get. We get our h right-handed because initially this
was set to be true. So right-handed was
true initially. And the if statement has a condition that
right-handed is true. And so he got RH. Now, if we change
this to false, false. Run, run module, okay? It prints LH left-handed because this condition
is no longer true. So we have to move on to the
else statement and print out h. Now sometimes you might
have more than one condition. So let's set that to true. Let's create another
condition called male. I'm going to set that
also equal to true. Since here we have two factors. We can make four combinations. I'm going to print
them down below. So these are RH mail
or right-handed male, a left-handed male, a
right-handed female, and going left-handed female. Now, sir, I just want to grab
one of these categories. Can print a message for them. Say I just want to grab
the first category right-handed males. Now, to do this, I need to have right-handed and male
both equal to true. So it has to be both a
male and right-handed. But how do I insert this
to the if statement? Now, if he wants both
conditions to be true, we can use the end. It is built in and
therefore turns orange when we print it in. And then we're going
to have to add the name of the second
factor that we have. So if this guy is
right-handed and a male, we're going to print right
handed male parent molecule. Okay? Always remember to remove
unnecessary pieces of code which weren't
really called. These were just my notes. We've got RH male or
right-handed male. Now here we might face a small problem with
the else statement. And here's why. So as we said, we
have four options. Right kinda male,
left-handed male, right-handed female,
and left-handed female. And all the if
statement will print this message only to
the right-handed males. And so the else
statement will print this message to every
other category, left-handed males,
right-handed females, and left-handed females. However, we can't just
rule out one of those and send a message to specifically just one
of those categories. Because the else is
very general and it simply means anything
that was not in the, if we printed in the US
regardless of what's over here. So how do we fix this? How do I introduce
another condition to the if-else statement so that I can rule out
different categories. I can do this using
the elif statements. This is simply, you live and the liver is
short for else-if. And then you can write
their conditions. So we're going to have
right-handed female as example. So a right-handed female is true for right-handed
but false for male. So let's go ahead and
change that to false. Then what we can do
here is else, if so, if it wasn't for
right-handed or male gender, we're going to move on
to the IO statements and we're going to
have right-handed. And not. Now not male means that male
is false and it is false. So since both of these
conditions are met, right? Tenant is true and
not male is true. Since mill is false, then we can print the message only
to those that are not males, but at the same time are right handed and left
have right-handed. Let's run this. Again. Never leave just
cranium texts inside. We got right-handed female. This is because right-handed
is true, but male is false. So this must be a female
who's right-handed. And now we can add
another eel of seconds. Or you left handed males. Else, if not, write Android, which means
left-handed and male. Let's print left-handed. Now let's change this up. So for this one to be true, we want it to be a left-handed. Right-handed should be false. I'm going to change
this to false. And male is true. So we're going to
change this to true. Let's run, run module. Okay. We got left-handed
male because this is how Python reads
through the code. If right-handed and male, are both of these true? No, they're not. So it skips the if statement and moves on to the first
deal if statement. Now, is it right-handed
and not a male? So israeli tend to
true and false? No, it's the exact opposite. It moves on to the other
gill if statement. Now, is it not right-handed? Right-handed is false. That's true. And male. So males
should be true, which is right over here. So we met this
condition right there. So it's going to print
left handed male. And now the only other option left is going to be a
left-handed female. So we can just continue
with the else statement, then write LH female. Now, let's have both
of these to be false. Can print left-handed female. So now when we run this module, okay, we get a
left-handed female. In the previous example, we've seen the end
and this one was used to make sure that two
conditions are both true, that they are both meant by the program in order
to print a message, were ordered to just
simply take an action. But sometimes you
just want one of the two conditions to be met and this will be sufficient
for us to run the program. For example, if we
have a university that tells us students
that they could graduate, if they either complete a
project or take a test. So either one of them
can make them pass. To do this, we're going to use the board and it turns orange
because it's built-in. So let's see how this works. Now, we're going to start
with two variables. So test true. They will both hold
Boolean values. Projects equals true and I set both of them to be equal
to true at the beginning. Now here we're going
to have to make a condition that the man's either the project or
the test to be true in order to make the student
be able to graduate. That's how project work tests. Well, wonderful. So python reads this as if project is true or test is true, then we can print well done. Else. So else means that if
neither of them is true, so if they're both false, then we can move to the
else statement to print. For example, please submit your. Now, let's set both
of them to be true. I'm going to try all
the combinations. So run, run module, okay? Now since both of them are true, then react well done
because we have submitted at least one of the
projects or the test. Let's set the test to be false. Run, run module. So this student decided to do the project,
but not the test. The student did complete one assignment so he can
graduate and well done. We can switch those two, true. It's student chose be
tested over the project. But he still can graduate because he has completed
the assignment. However, if we have
a student that didn't complete either of them. So if he had the test equal to false and the
project equal to false, meaning you didn't complete
either assignment. It says please submit your
work paintings that he did not graduate or he couldn't
graduate until he does, either as a test for the
project to until these turn true or until one of those turns true, then each congratulate. Now finally, I want
you to know that n and four can be used with non Boolean values like
numbers or strings. So let's have a
quick, quick example. Let's have a equals to 50. Then is greater than 20. Hey, it's less than, let's say 75. Colon, cringe. Like tablets, some
dots right there. Else. Print. Let's print some dashes. So if a was between 2075,
we should get some dots. If it wasn't, we should
get some dashes. Okay? Some dots because it
is within the range. Let's change a to, for example, 500. Run, run module. Okay? You get some dashes. Now this is very
symbolic just to show you the idea
together, the cross. Once you understand
it, you can use this in other useful situations. Now, as for strings,
we can have a equals. Let's say yes. Then if a equals, equals yes. Okay. Else print. Sorry. Now again, this is
just various involved. Just to show you the idea that we can use an if else
statement with a string. So in case you didn't
see that right there. We've got okay,
because he is yes. Let's change that to
know from your module. Okay. We get, sorry, I now finally, finally very important
note is that here a was equal to yes. Just like that, we have
a capital Y over here. Now this is different from
the yes with a lowercase y. So these are considered
two different strings. So it is going to execute
the code under the else. Let's see that real quick. We get started again because
it's not the same thing.
13. While loops: We usually use programming to perform tasks that
are too repetitive, that take too much of our time and so it is very important to be able to execute a block of
code repeatedly and easily. Here's where while loops come in, while loops work
by going through a block of code
repeatedly again and again until a condition that was initially set to be
true becomes false. Very simple, very easy. Now let's go see
that in practice. Now again, if you want
to have a while loop, you must have a condition
initially set to be true. And to do that, I'm
going to have a variable called n That's going
to be equal to 0. Wonderful, and that's going
to have a while loop. The while loop is
very simply a while. It turns orange because it's built-in and
recognized by Python. Then we're gonna have something
called the loop guard. It's basically the condition that was initially
set to be true. So I'm going to have
while n is less than or equal to ten, then a colon. now n is initially 0, so n is less than or equal
to ten, so the condition is true. Now when you press Enter, you're gonna get an
automatic indent. Now this is very
important because whatever is beneath
the while loop and indented is going to
be repeated through again and again until this
condition becomes false. So basically if the block
of code that we keep repeating is anything indented
beneath the wild loop. So the block of code I want to execute repeatedly is
gonna be very simple. It's gonna be a
code that will print numbers from 0 up to ten. And this is why we
have the limits of 010 right over there. So to do that, I'm going to print
n first of all. So this is going to
initially print 0. Then I'm going to have
a new value for n. I'm going to increase
the value of n by one, so n equals n plus one. And there's a shorthand
for this which is n plus equals one. So this means it's going
to add one to the value of n so that this n
is now equal to one. Okay? Now, let's see how this works. We have n initially
set to be equal to 0, and then we're going to have
while n is less than ten, which means as long as n is
less than or equal to ten, we're going to execute
this block of code. So we're going to print n, we're going to print 0,
and then we're going to add one to the value of n. So we're going to increase
the value of n by one. So the n has a new value
which is now equal to one. So that one we're going
to go to the top of the while loop and
read it again. Is one is still less than or
equal to ten. Yes, it is. So we're going to print one. Then, we're going
to add one to one. So we get two, n is
now equal to two. We go back to the top of
the while loop and then we do is to still less
than or equal to ten. It is. So we can print two, then we're going
to add one to two, we get three, and
so on and so forth. Until now eventually
we're going to print all the numbers from 0 to ten. But here's what happens
when we get to ten. Now, is ten less than
or equal to ten? Yes, it is. It's equal to ten. So we're going to print ten and we're going to
add one to ten. So n is now equal to 11. When we go back to the
top of the while loop, we get is 11 less
than or equal to ten, though it's not, the
condition is no longer true. It is false. We don't
execute this line of code. We go ahead and carry on with
the normal code we have. So let's have a message that indicates that we're
done with the while loop. So let's do print. and then Done. Let's see how this works. Let's run, run module. Okay. There we go. We have 012345678910. And then we're done because
the while loop stops over here and we carry on with
normal code over there. Well, let's see the
application of a while loop. So basically we're going to
have the user play a game. And then when they're
done with the game, we're going to ask them if
they want to play again. So first of all,
we're going to ask them if they want to
play game at all. So a equals input. Then do you want to play? Again? Question mark. We're going to guide them
because I can either yes, we're now and now here we're going to
have the while loop. And the loop guard
or the condition will be whether a is
equal to yes or not. So if a is equal to yes, we're gonna go ahead and
continue with the wild. But it becomes no, then
we're going to skip the whole thing and
continue with our code. So let's write our condition
is that a is equal to yes, remember it's a
double equal time. Then a colon. Then here we can write the code for a game so that when
the user types in, yes, they get to play a game. And let's have the game, the Mad Libs because you're
already familiar with it. And I have them outlook
already prepared. So I'm going to
paste it over here. So variables equal to inputs and then we just print
them until the sentences. It's very easy. You can
have it rolled over here. And then we're going to ask the user if they
want to play again. And their response will
be the new value of a. So a equals b. But then, do you
want to play again? Okay, let's have a
yes-no guide them. So yes, no space. Wonderful. Now we're done with the
code that is going to be executed as long as
their answer is yes. But then when they type no, we're going to have to write something outside
the while loop. For example, print, for example. Now here's how this works. We're going to have a equal to either yes or no based
on what the user inputs. Now, if it is equal to yes, we're going to run the
code for the game, so we're going to let
them play this game. And then when they're
done, we're going to ask them if they
want to play again. If they do want to play
again and they type in yes, Then a is still equal to yes. So we're going to
run this code again. And then until the
player decides that they don't want to play the
game anymore when it snow. When it checks if
a is equal to yes, it will be false because
a is equal to know. So it's going to
skip this while loop and go down there
to print goodbye. Let's run this to
see how it works. So do you want to play a game? Let's go with yes,
Andrew, a car. Let's go with white. Plural noun. Let's go with
celebrity, like the rock. So we've got roses or wipe
out your blue while grew up. Do you want to play
the game again? So I'm gonna go with yes, show you how we're going to get the exact same code repeated
over and over again. And we can type in
different colors. For example, we
can go with green. Then for all now
let's go with seats. Celebrity, for
example, Taylor Swift. We got roses are green, seats are blue, and
I'll Taylor Swift. You want to play a game again? Now let's see what happens
if I type in notes. So no, goodbye. And as you can see, we're
done with the code.
14. Functions: As programmers, it is always essential for
us to be able to organize our code and to
break it down into smaller, bite-sized pieces
so that we can deal with and handle large
quantities of code. This is exactly the
purpose of a function. A function is very
simply a collection of code that is used to
perform a specific task. So if you have a bunch of
lines of code that work together to do a specific
task or an operation. Then you can just group them together and put them
inside of a function. And now whenever
you want to carry out this task or operation, you can just call that function. Writing functions is very easy. You first have to tell Python that you're about
to write a function. To do this, we use
the keyword def. When Python sees this, it knows that you're about
to write a function. And then you're going to
have the function name. And let's have ours
called new_line. Now, when we name our function, we use the same rules
when naming a variable. So basically we can't have spaces in their
we have, an underscore. And second of all, we usually prefer this name is descriptive. So as a programmer, when I read new line, I have some sort of indication that we're about to
insert a new line. We're about to write a code that is responsible for
inserting a new line. Now it's not like
this name over here has any effect on
the code inside. You can have any name
you want over here, and Python will be
very fine with it. However, it is very
useful for you as a programmer to have
indicative names. Because when you
have many functions, you're just gonna know
which function you are referring to just by
reading its thing. So then when you're
done with naming, gets you add open and close
brackets and then a colon. When you enter, you have
an automatic indent. Now, anything indented beneath this function belongs
inside the function. So any collection
of code you write here is gonna be part
of this function. And once you decided that you are done with
writing the function, you can just go back
to the main line. So let's start with
writing the function. We're going to have a very
simple collection of code. So first of all,
remember indent and then print an empty space
then the print, just like a line over here. Then finally print
another empty space. so basically this is a
function called new line. And the code inside
it is going to print a space a line,
then another space. Let's run this to
see what happens. And as you can see, we don't get a result. And this is because if we
want to execute a function, we have to call it first. To call a function, you
just write its name. So new line, then open and close brackets.
when I run this. I have my lines and
spaces over here. Now let's see how we
use this function. So for example, let's
say I have a story. So print, oops, print. I love cherries. then Print. But I'm allergic to them. Print so I just ate just eigh. Okay, so this is a very
short and meaningless story, but I'm using it to show you
how we use our functions. So here the story
is right there, but it's not very organized. Say I want to have
it in some sort of paragraphs or something
where each paragraph is separated by a
space a line and another space so that it is
more visually presentable. So what I can do is
beneath each one of those, I can call the function. So new_line. And new_line. Now this is very useful
because I did not have to rewrite this collection of code every time I wanted
to insert a new line. And this is especially
important when you're using very long functions
and you don't want to keep writing
again and again. So instead, you can just call
the functions over here. Second of all, it helps us get a more organized program.
00:04:30.155 --> 00:04:32.975
So as you can see here, this
is very short, very concise. It's easy to read
as a programmer. But if you want to have all this clutter
inside your code, it's gonna be much more
difficult to navigate through. And this is much cleaner and much shorter and
much easier to use. Now finally, let me show you how Python deals with this code. So it's going to start
reading from here. It's going to print,
I love cherries. And then it's going to find this function's name you like. So it's going to go up
there and search for a function called new
line right there. That's going to execute all the code inside
this function. Then when it's done,
it's going to go back to the original code and continue so print, but
I'm allergic to them. It finds the name, prints this. Then it's going to print. So I just ate bananas,
then it terminates the code. Let's run, run module. Okay, let'sSee this last time. We have this very
nice piece of code. This is very simply how
functions work in Python.
15. Class Project: I strongly believe that the
best way to learn a skill is through practical work and
project-based learning. And so I prepared a very quick lightweight
project for you. And by completing get,
you will have tested all of the key concepts in this
class for this project, I want you to write a code for a program that
offers 2 services. The first one is calculating
the body mass index, or BMI, of the user. The second one will be a
currency exchange program. You first want to
welcome the user and introduce them to the
two services you offer. Then we're going to ask
them to choose one of them. And depending
on what they choose, you're going to run
certain lines of code to satisfy their request. Finally, you're
going to ask them if they want to run
the program. Again. If they do, you're going to have to run the program again. And if they don't, you can
just print a goodbye message. And finally, remember
to keep your code clean and visually organized. For more details,
go ahead and check the projects and
resources section down below, there you'll find
the detailed task as well as a project guide and
the sample solution attached. The project guide should give
you hints along the way, and sample solution is my own attempted at
completing the project. Now remember, your code can be very different
from my code, from the code of all the
students taking this class. This is what I
love about Python. We can all be unique and creative while getting
the task done. Once you're done, I
encourage you to upload your project blow to
get live feedback and to allow other students
explore how you think and all the different possibilities of
writing this code. I want you to upload
a screenshot or screenshots of
your program file, which is where you write your code, and the results that
appear in the shell. So be sure to try
your program out and then take a picture of it
to show us how you did. Now should you need
any assistance I'm always available to help. So be sure to leave
your question in the discussion
section down below. You got this. Good luck.
16. Recap: Conclusion : You've made it this
far along this course. Congratulations. You've just moved from being an absolute beginner to someone who is very
knowledgeable in Python, you can write very effective
and useful lines of code. You have learned how to write to the world of simple and complex, comfortably houses
built and effective, smart and interactive
program room. And end up with a very
thoroughly structured, well-organized program file
was an immense pleasure to share my programming skills
and experiences with you. And I hope you enjoyed learning Python just as much
as I enjoy teaching. And now I encourage you to go ahead and attempt
to completing your class project a down
below to get live feedback, which will help you read out all the core concepts you
learned throughout this class. And it will dominate these skills through project-based and
practical learning. If you face any challenges
anywhere along the course, feel free to drop
your questions to the discussion
section down below. You feel like you've walked away with something valuable
from this class. How about you leave a
good review so that other students can navigate
through the same experience. One-off visit my profile and hit the Follow button
while we're at it. Now because this class
comes to an end, I would like to thank each and every one of you for
your dedication. You got to do the
work and unleash your creativity to
the world of program. Remember, you're
burning security.