Transcripts
1. Course Introduction: Hi, eon, and welcome to the PySonUltimate
Beginners course. So a little bit about me. Hi, my name is Honor, and I will be your
instructor for this course. I'm a software developer and a solutions architect
who has experience in creating and
deploying PySon Jango based applications
to the AWS Cloud. I also hold a BSE and
information technology, and I am an ALBS certified solutions architect
and developer. I'm also very passionate
about IT in terms of all of the technologies available to us
and also teaching. You could say my greatest
calling is combining these two fields and just sharing all that I know about technology and
teaching together. Trust me, you are in good hands. What will you learn this course? You're going to
learn how to master the fundamentals of PySon
programming with ease. You'll also gain insights into object oriented programming to further enhance
your coding skills, and you're also going
to learn how to solve real world problems
efficiently using PySon. Now, you probably wonder, Okay, who is this course for? So this course is designed
for complete beginners who are new to programming and eager to learn
PySon from scratch. So whether you're a student, a professional looking to switch careers or simply someone that is just curious
about coding, then this course is
tailored to help you bring a solid and strong
foundation in PySon. In this course, we will
have a combination of both theoretical and
practical exercises within this course. So we'll first learn the theory and all of
the concepts behind PySon and then we will
also learn how to apply this knowledge in a
practical based setting. Now, each lesson is structured carefully in order to
maximize simplicity and efficiency to make
sure that you are enjoying the lessons and to
make it more fun and enjoyable to
learn in the process. Now you will also have access to the discussions section
of this course. So you have any questions
or anything of the sort, you're more than
welcome to ask ahead, and then I will gladly assist you with whatever questions
or queries that you may have. So enroll today, and let's take your
career to new heights. Thank you, and I hope to
see you in this course.
2. Course overview: Hi, everyone, and welcome to the next lesson
which is going to be focused on the
course overview. Let's go ahead and take a look. A question that's
probably on a lot of your minds is pertaining
to the prerequisites. Are there any prerequisites
for this course? There are no prerequisites for this course and this course is specifically designed
for beginners who have no experience in PySon. Now this course is ideal for those that are
learning programming for the first time or for those switching from another
programming language. I'm just giving you a bit
of a layout here as to the expectations and who this course is more
geared towards. Just keep that in mind here. The bottom line that I
just want to say here is there are no prerequisites
for this course. As long as you have
a computer and access to the Internet,
you are good to go. Now, that's all that you
need to really keep in mind. Right. So the next
big question here is, what will you learn? Now, in this course, you will learn the core concepts of the PySon
programming language. In addition to mastering the
fundamentals of PySon you also gain an introduction to
object oriented programming, which will be covered near
the end of the course since it does tend to lean onto more intermediate concepts,
focusing on PySon. Now, a full description of what we will
cover in this course will be listed under the
about section of this course. So here, you'll see
all of the detail into what we will cover
in this course exactly. So please do keep
that in mind if you want to know the heavy detail. Now, another question
on some of your minds may be pertaining to support. Now, is there support
in this course? So the answer is yes, there is. So if you have any questions or need assistance
in any regard, please feel free to make use of the discussion section
of this course. Now, I will be
glad to assist you and answer whatever
questions you may have, and it may even be the case another fellow student helps you before I'm able to
respond to your question. So please also
keep that in mind. But yes, there is
support for this course, should you have any questions or need assistance
in any regard. A. That's just a bit of
information I wanted to convey to you in terms of the
overview for this course.
3. What is Python?: Hi, Ron, welcome to
the next lesson, which is going to be
focused on answering the question of what is PySon? Let's go ahead and get
started. What is PySon? Now, PySon is one of the most popular programming
languages worldwide. It was also created in 1991
by programmer Hideo Van Rosa. A very key feature
here and advantage of PySon is that it has a simple
and easy to read syntax, making it ideal for beginners. Let's say, for example, you are starting off with
your programming journey. Now, PySon would be one of the best programming
languages to start, not only because it is
very high in demand, but it's also as
we can see here, very simple and easy
to understand syntax. That's why it's very
ideal for beginners. Now it is also a highly
readable language with a very clear structure
that emphasizes simplicity. The whole goal and aim
of PySon is to make programming fun,
clear and efficient. Now, it is also a very diverse
programming languages, since it is widely used in a variety of fields such
as web development, data science, and even AI, of course, standing for
artificial intelligence. I just wanted to give you
a few facts here about PySon and some features,
advantages, et cetera, as to what we are
going to be learning and the benefits of
learning PySon itself. So that's it for this lesson.
4. Course resources: Hi, eon, and welcome
to the next lesson, which is going to be focused
on course resources. So let's go ahead
and take a look. So a very important question that all of you will
have on your minds is, how will we code? So, don't worry. To keep things simple, we'll be using an online
coding editor so we don't have to go
through the hassle of downloading software
and configuring it, such as with an ID, such as visual studio code or
anything of the like here. We want to keep it
simple, straightforward. We want to learn how
to code. We don't want to have to configure
all of those things. Now, I suggest that you either use the online coding
editor available at pitwitle.com or
at onlinpson.com. Now, for this course, I will use onlinpyson.com. So this is the online coding
editor that I will utilize. So keep in mind
here, I just want to give you some options here. So some of you may prefer
one over the other, but for continuity, I'll
be using onlinpyson.com. Now, don't worry. I will show you how
to get started here. So you don't need to
stress here about navigating or heading onto
this website or anything. I'll show you how you can get started and how you can
get organized. All right. So just a little bit
of information here in terms of the
course resources. So that is what we're
going to utilize. So there's nothing else that
you need to worry about. Don't need to download or
install anything, right? So we're going to
keep it very simple. So that's all I wanted to
mention for this lesson.
5. Accessing and using an online coding editor - [Lab]: Avon. Welcome to the next
lesson which is going to be focused on accessing
online coding editors. Now there are many
online coding editors out there for Python. I'm just going to
show you two of them, and I'm going to be using
the one in this course. In this course, I'm
primarily going to be utilizing online dspyson.com. I'm going to be utilizing
this coding editor. Now, to access this
coding editor, you just need to type in
onlinepyson.com in your URL. Now there is an
alternative which you can use as well if you prefer. That is at pitwiddle.com. All you need to do
is you need to just go ahead and type in pitwiddle.com and
then it's going to take you to the
following page here. As you can see here on both of these online coding editor
websites here that we have a space here to type
our code and then of course, to run it and then to see
the output here below. That also goes for online
PySon here as well. Here you'll add in your
code, and then of course, you'll run it and
then you'll see the output down here as well. So it is how you prefer. I'm just going to write
some sample code. I'm not going to be
explaining it right now. I'm just going to give
you a demonstration of how you can decide
to follow along. So let me write a
very simple script. I'm going to say
name here, equals, and I'm going to say my name, I'm going to say no, then
I'm going to say print. And in parentheses,
I'm going to say name. So I'm writing some code
here and we scroll down. I'm going to say run. Of course, that's going to compile
and there's my result. Let's say that you
want to instead use Pi twel instead
and not online Python, of course, you're going to write the same code in
that editor here. Let's say, for example, you say name and here you're
going to say no, and then you're
going to say French. In princes you put name
and you say run here, it's then going to
go ahead and compile that code and there is the
result down here that says no. Right. So here we are, you have two options. All of the code that I'm
going to demonstrate, you can apply this in
whichever editor you prefer. It's really up to you as to
how you want to code along. There are two options
for you. All right guys. That's on this lesson
on how you can access your coding
editor of choice online. It's really up to you as to
how you want to proceed. That's it for this lesson.
6. Thought process: Hi, Eon. And welcome
to the next lesson, which is going to be
focused on sort process. So in this lecture, I'm going to just give you some
words of advice, which you can follow if
you would like to on how to structure
our salt process when undertaking this course. So let's go ahead
and get started. So sort process. Now, before we dive
into the course, we need to structure
our sort process. So I just want to give
you some words of advice. So let's go ahead and get to it. So the first bit of
advice I would like to give you is to take
it one step at a time. So take this course
one step at a time. Please don't brush,
take your time. If a particular
lesson is taking you a lot of time to master,
don't lose hope. You will understand
it with time. Some people understand
various concepts immediately as a first time. Others take a longer time to
understand certain concepts. And as myself, as a learner to certain subjects in my
life, I have, of course, taken lots of time in
certain areas of concepts to understand and in other
particular topics I master almost instantly. For everyone, it's different. Everyone is going
to have a challenge at some point of something, please don't be
discouraged and just take it slow and
easy and steady. Another tip I would like to give you is to set a schedule
and stick to it. Now, of course, you don't need to be extremely rigorous and
detailed in your schedule. What I mean by this, you
don't need to plan it by the minutes and the exact hours and the timing, et cetera. What I mean by that is have a schedule that is not
going to overwhelm you, but it's going to push you
just enough to stay consistent and disciplined and even motivated to
complete the course. Do keep that in mind.
In my experience here, I'd recommend you choose a schedule that is
more flexible to you. I myself don't like to
sit rigory and exactly by how can I say the times and the minutes
spent and the hours spent? I don't like to go
with that approach, or with dates, I like to keep it more
flexible and say, Okay, I want to spend a certain amount per month
working on this course, and I just want to finish
about five to ten lessons. So that would be my
approach and how I do it. So it would depend on how
you prefer to go ahead. I'm just giving you an example. But anyway, it's good to have at least a base schedule
to follow along with. Now, the most important advice that I want to share with
you here is the last one, which is to stay positive and know it's not a race to
complete the course, but rather a slow and
steady journey to acquire long term knowledge
for the best results here. This is what I mean here.
You don't want to rush through this course,
rather take your time, even if you feel that
the concepts are very easy to understand,
rather take your time. Take it steady and work on it on the course
occasionally or regularly, depending on your schedules. That's a very important thing that I want to mention here. Again, this ties
up a lot of things here on the focus of
staying positive. Again, of course,
if certain concepts take some time to understand, work through it, put in the effort, and
you will get there. Stay positive and know it's
going to take some time, but take it slow, take it easy, and relax as you go
through the course. I've done my best
here to make it as simple as stress free
throughout this course, do keep in mind here just to relax, take a deep breath in, and calmly go through the course and enjoy yourselves. Guys. That's all I wanted
to mention here in terms of thought process.
7. Variables: Hi, everyone, and welcome
to the next lesson, which is going to be
focused on variables. So let's take a look
at what a variable is. So a variable is a memory location
that is used to store a value in a
computer program. Now, I know this is going
to sound quite abstract, but let me help you understand a variable further by
the use of an analogy. So let's go ahead and
take a look here. So picture this for yourself. You have three boxes, and you have a tennis ball, you have a PC, so this can be your
monitor and your keyboard, so your PC, and a football. So let's say it's time
for you to move out. You're moving out of your
apartment or out of your flat, out of your house, et cetera, and you need to pack
all of your items away. So here, for
example, in box one, we are going to put our
tennis ball in this box. In our second box, we're going to put in our PC, and in the third box, we're going to put
in our football. So here is a way that we
can define a variable. So try to think to
yourselves of what you think that particular variable would be in this
case. All right. Now, after taking some time to think of where this
analogy comes into play, let's break it down further. So in that particular scenario, we want to refer back to the
definition of a variable, so it is a memory
location that is used to store a value in a
computer program. So if we bring back our previous analogy
that we looked into, in our case here, the
variable would be the box because it is
storing our items. In other words, in terms of
programming terms of PySon, we would be having our values. All right. So let's
bring this in again. So variable is a memory location that is used to store a
value in a computer program. Now, with our analogy, the location or the
memory location, you could say, of course, is going to be our
box as the variable. And what does it use to store? So essentially, we could
see it as in our analogy, the tennis ball, the
football, the PC. But in computer terms, these can indicate the values. So this is how we can better construct what is a
variable. All right. So this was just a lesson
just to give you a bit of a theoretical background
into variables.
8. Variable naming conventions: Everyone, and welcome
to the next lesson, which is going to be focused on variable naming conventions.
Let's get started. Let's take a look at the common variable naming conventions
that you need to follow when you are defining
your variables in pyson. First of all,
variables must follow specific naming rules
and these are known as variable naming conventions. Now, these conventions
dictate what is allowed and what is not allowed when we
are naming our variables. This is a set of rules that we're going to have to follow. Now, don't worry, I will
explain these rules with a few examples to clarify how to go about naming
your variables. Let's start off first
with what is not allowed. For example, here
we have two name. A variable name cannot
begin with a number. Remember when you're
creating your variables, they cannot begin with a number. Another rule here is that a variable name cannot
contain spaces. Here you see my name, then you see a space
in between it and then you see it says
name, my space name. Variables cannot contain spaces. Now the next one
I want to mention isn't strictly focused on
what's allowed or not allowed, but it is just something
to keep in mind. Here, for example, we have
name, name, and name. The first instance here,
we have a capital N, then we have a lower clase N, and then we have
all caps for name. These are seen as three
different variables. Always remember that variable
names are case sensitive. It's very important
that you remember that. These will be treated
in a program if you set up these
variables like such. Going to be seen as three distinct
variables that are different from each other since variables are case sensitive. Remember this is very important
to keep that in mind. All right, so let's
take a look at what is allowed, a few examples. So a variable can only include letters, numbers,
and underscores. So let's take a greater look. So here, for example,
we have my name. So in this case, here,
there are no spaces, and we have letters in
that particular variable. Here, next, we have
M underscore name. So here we can see we have got letters and we have underscores. Here, of course, we can
see another example. We have my name, but of course, we starting off
on an underscore, and after the MPa we add
in another underscore. This is allowed. And here, for example, we have M
underscore name two. So previously in the not
allowed example here, you cannot start a
variable with a number, although you can add numbers
into your variables, but you just cannot
start them with it. So in this case, here we
say M underscore name two. That's fine. As long as the variable doesn't start with the number, you're good to go. And of course, in this case
here, it's also permissible. You can have variables
that are in a CAPS form, so all caps is also
perfectly fine. Just keep this in mind here
and I'll recommend you just rewatch this lesson if you just want to have a refresh
on the rules here, et ce before you start
coding practically. That is some of the
key points to note. Now you probably want okay. What's my preference
and recommendation? How do I go about setting
up my variables and such? What I do is, I want to just
keep a sense of reason here. Or single word variables, start off with a
lowercase letter, such as name, for example. That's how I would go about it. For multi word variables, I'd recommend using
camel casing. And here, for example,
we have first and then for the second word, I have a capital for it. So this is how I go about it. So for single word variables, I just use a lowercase
letter to start off. And if I have multi word
variables such as first name, my name, my age, my date of birth, I'm always
going to use camel casing, so that implies that
the first variable, is going to be in lowercase
and the first word lowercase and the second words going to start off
with a capital letter. But this is a little
bit too abstract. Let's look deeper into
what is camel casing. So what is camel casing? Camel casing is when you join words in a phrase capitalizing the first letter of
each word after the first without using spaces. So do keep that in mind,
that is camel casing. Now here we have a few examples. So my forename, my
surname, and my age. This is how you can
employ camel case. Of course, this is just a recommendation
of what I recommend you utilize if you're dealing with
multi word variables here. Of course, if you have lots
of words in your variable, this is a good
approach to go but it's also very clean
to do this approach because you can clearly
see what variable you are essentially declaring. Guys. So that's it
for this lesson on us focusing on our
variable naming conventions.
9. Data types: Hi, eon, welcome to
the next lesson, which is going to be focused on data type. Let's get started. The question on our minds,
what is a datatype? A data type is essentially a data item or element that
is defined by its value. So let's go on ahead and take a look at the types that we get. So what data types do we get? So first of all, we get SDR, which is commonly
referred to as a string. We have Int, which is, of course, short for integer, we have float, we have Bool, and we also have list,
tuple and range. Now, don't worry.
I'm going to go into a deeper sense of detail for
each of these data types. So that's going to be
in the form of either seri or in a practical
format. So let's continue. So first of all,
let's look at STR, which is commonly referred
to as a string data type. The STR datatype
represents text, which can be letters
or words and it is enclosed in either
single or double quote. Let's take a look at
a few examples here that will denote a
string data type. As we can see here,
first of all, we have ABC in single quotes. We have John Smith in single quotes and we have
Alpha in double quote. Here are a few examples. Next, let's take a look
at Int. All right? So the int datatype
represents whole numbers, including both positive
and negative values. So here we would have,
for example, zero. We can have minus eight, and we can have 91. So you can see here the
variety in place for an integer Int data type and the representation that will
be coupled together with it. We also then have float. A float data type
represents numbers with decipels also known as
floating point numbers. Hence, we have float instead
of decimal because we want to refer to it in Pierson
as floating point numbers. Of course, a few examples
here would be 22.6, for example, 96.1 and 7,342.14. If you are thinking of utilizing the float
datatype sink, decimal. Right, so let's continue. Next, we have Bol Bol is quite interesting one and
also a very simple datatype, probably the most simple
data type to comprehend. The bull data type can
have only two values, either true or false
or one and zero. But generally speaking,
true or false would be the best way to
elaborate on that. We would have true or
we would have false. Usually, true will be denoted in certain cases of programs with the tick and false or
the next or one or zero. We have two states,
true or false. That would denote
the BL datatype. Now, you're probably
wondering, Okay, what about the other data types? Because of course,
we had the list, the tuple and the range. So not to worry, we will dive deeper
into these data types later in the course and
we'll go further into them. However, it's a little bit advanced to dive
in straight away. We want to gradually move
along towards it. All right. So that's guys for the lesson, just to give you some
information on data types and to explain how they are correlating
as a whole, essentially.
10. Declaring a variable: Hi, everyone, and welcome
to the next lesson, which is going to be focused
on declaring a variable. So let's take a look. All right. So first of all,
we want to learn how we can create a variable. So first, we need to give
our variable and name. Then we assign a
value to S variable, and the value we assign
determines the data type. So let's take a look at a few examples that we
have here in place. So here's an example. So try to think to
yourselves of what the variable and what the
value would be in this case. So, the name will
be the variable, and the value here
is John Smith, and the data type is based
of the SDR string data type, because as we can see
here, the value is a string because it's in
quotes and it's based on text. Let's continue Let's
look at another example. The variable in this
case here is going to be num and the value here, it's going to be 564, which is also going to be an int datatype
because remember, an int is focused
on whole numbers. Here we have 564, the datatype here is an int. Let's take a look here again. So as we can see here, the variable is num, and the value is cety 2.8, making it a float data type. Now let's have a look at
another example here. Our variable here is accepted. Our value is, of course, true, and that's based off
the Bol data type. As you can see here, we are beginning to form a
pattern here with variable value and the data type that is associated with s value. Is it a string or STR? Is it an int? Is it a
float? Is it a bool? As you can see,
this is the process for how you can declare a variable also how you can learn to understand the
distinction between a variable, a value and the data type
associated with said value. All right, guys, that's
it for this lesson on how we can
declare a variable.
11. Creating our own variables - [Lab]: Hi, everyone, and welcome to the first lab exercise
which is going to be focused on creating
our own variables. So let's go ahead and dive in. So, of course, like
I mentioned in the accessing and online
coding editor lesson, you have the choice of easy
using the online dpyson.com coding editor or
pwiddle.com editor as well. Okay, so decide which one you want to use Tlins
really up to you. So I'm going to be
using onlinpyson.com. You can use Pytwddle if you desire. It's really up to you. All right, so let's get started. Right. Now, the first thing
that I want to teach you is how you can utilize a
print function, first of all. Now, the print function is
going to be utilized to output our coding
result in the console, which of course is
going to be down here and in PTwittle it's
going to be down here. Okay. So let's go
ahead and do that. So to declare a print
function, we can see print. And in parentheses here, we can add in the text that we want to output
in the console. So you can either
use single quotes or you can use double quotes.
It's really up to you. So I prefer to use
single quotes, and I'm just going to say here, hello world, just like that. That I'm going to scroll
down and I'm going to run this coding script, and here you can see it
outputs hello world. That's how you can output all of the statements that it's based in your
print function here, and we can see here it says hello world, based
on that result. So don't worry, we are going
to go into more detail with outputting our data
into the console. So a print statement
is used if you want to output your data. That's the simplest way
to explain it. All right. Now, of course, you can type
anything here that you wish. You can just say hi, and then you can
just run your code. And then here it's just going
to say in the console Hi. Now, of course, you can have multiple print
functions as well. So here we can say print
and I'm going to say here, my name is honor, and I'm going to add
another print function here and this print function in the parenss I'm going to add in the text that it must output, and that's going to
be my age is 28. Now we can scroll down
and we can just say run and it's going to print
out all of those statements. Hi, my name is Ano, my age is 28 and outputs all of that for us
as we can see here. That is how you can go
on a head and utilize the print function to output data into the
console. All right. Now the reason we are
learning this first of all before creating
our own variables is because we need to know how to output the values that we've
assigned to our variables. And we're going to use the
print function to help us to output those values
into the console. All right. So let's do that. Let's go ahead and delete all of this and let's start
declaring some variables. First of all, I'm going to say first name and notice here I'm using camelcasing
and I'm going to say equals, and here in single words,
I'm going to say no. Then I'm going to say
last name equals, and then I'm going
to say Pretorius. Perfect. I've declared
my two variables, first name and last name. The data type for both,
we can see is a string, and of course, we can see that the values are no and pretorius. Let's say I want to
output the data. I want to output these values onto the console right here. To do so, what I'm
going to need to do is to utilize my print function. And what we can do is we can
add in our variables here directly to output the value
associated with S variable. So here by the print
statement here, I can add in first name, and it needs to match exactly to the variable that
you have set up here. What I'm also going
to do is go ahead and specify another print function
to output the last name. We want to see in
our console now, we want to see Arno
because we are printing the first name and I want to see Pretorius because I'm
printing out the last name. If I scroll down and say run, it's going to show
aro Pretorius. Remember, two lines
because we have two of those print functions
here set and applied. That's so you can simply create your variables and also
utilize the print function to output the values that have been assigned
to S variable. Let's go ahead and go
a bit further now. Let's say we want
to have a bit more. Let's say we want to have age. I'm going to say age here 28. So we can see He, of course, is sent to the value of 26
26 is the int datatype. And here, we're going to say
bank underscore balance. And that is going to be 114.23. So we're using a
float data type. And then we want to also we want to work
with the bull data type. So here we're going
to say post graduate, underscore student equals,
and I'm going to say false. So we can see we're
using the bull data type here by utilizing false. So let's go ahead and
print that out as well. So we can just scroll
down and let's say print age and print, and we're going to say bank
underscore balance and print. We're going to say postdate
underscore student. We want to output the
value of our age, our bank balance, and the
postgraduate student status. I'm going to scroll down and run and here we can see
it's going to print what we had previously Anno Pretoris the values here of
these variables are being output in our console. And we can also
see the age here, the bank balance in per grade graduate student is output here, so 28, 114.23 and false. And that all pertains to
the values assigned to these variables that we have
set up here and into place. Right. Perfect. That is how you can go on a hedge
and you can essentially create your variables
and how you can output them in your console by using
the print function. Essentially, our print function on its own looks like this. I'm going to say print and
opening and closing princes. This is known as
our print function. And you can either
directly insert values into it in a
raw sort of format, or you can add in the variable
within the parentheses of this print function
to output the value associated with said variable. For example, here, I can also
put in a value directly. I can put in 23.4, like a float value and let's go ahead and
remove everything else. What I'm going to do
is I'm just going to delete everything here. Let me move this to the first
line and just run that. And directly, it outputs
23.5. All right. So that's how we can go on
ahead and essentially create our own variables
and output them into the console window
that we have down here. All right, guys. So that's
it for this lab exercise.
12. Arithmetic operators: Ron. Welcome to the next lesson, which is going to be focused
on arithmetic operators. Let's get started. All right. I've decided to
sum everything up for you in a table
here of all of the various arithmetic
operators that we commonly utilize in
PySon and of course, the purpose associated
next to them. As we can see here, according to the operator on
the left hand side and the purpose on
the right hand side, first of all, we have
addition, we have subtraction. We have division, we
have multiplication, we have modulus, we
have exponentiation, and we also have floor
division as well. So we can utilize all
of these operators in our PyCon programs and
we can integrate them as we desire with our own use case and programs that we plan on building itself. Right. That's all I wanted to mention on
arithmetic operators, just give you an overview of the various operators that we get and just to mention their purposes as well,
foresa operators.
13. Working with Arithmetic operators - [Lab]: Hi Iron and welcome
to the next lesson, which is going to be
focused on working with arithmetic operators
in this lab exercise. So in this particular lesson, we're going to be working with our variables
as we did before. We're also going to be
building up on that and also incorporating and
working with specifically, as the lesson states, on
arithmetic operators. So our recently
acquired knowledge on our operators that we
are going to be utilizing. Specifically in this use
case as our example here, we're going to be working with
our addition, subtraction, division and
multiplication operators, and we're going to apply it to a particular use case
which will be dealing with our money in a bank
account and of course, expenses and profits
and all of that jazz. Let's go ahead and get started. So first of all, we want
to create two variables. The first variable here,
I'm going to define as my savings account one, and the total in my
savings account one, let's say that as 500, of course, it's
going to be an inch, so that will be the datatype. Then let's say I have another
savings account as well. They're going to say
my savings account, and of course, the value here, I'm going to set as 400. Perfect. Now, let's say
I want to get the total of my combined savings
accounts, so the total. So let me create
another variable that says total savings. And to obtain the
total savings here, I'm going to utilize
the addition operator. So to do so, I would need to incorporate my two
variables here. So my savings account one. Then I want to say plus
space, and of course, incorporate spaces
before and after, my savings account two. So the value now of total
savings is going to be whatever my savings account one plus my savings
account two is. So we can envision
this as 500 plus 400. So all I need to do
then is I need to go ahead and utilize a
print function here. So I'm going to use
my print function, and it's going to output
the value that has been assigned to total savings. So to do so, I need to
add in the value here, which is the
variable, excuse me, which is going to
be total savings. So we can scroll
down and we can say run that outputs 900 for us. That's how we can
simply go on ahead and print out our value here
from total savings, which utilized addition
here to get the total. We utilize the plus or the
addition operator here, and we assign that value
here of the result of to the total
savings variable. Of course, the
value here was 900, which was output as soon as
we said we wanted to print out the value of the
total savings variable, which in turn turned
out to be 900. Right, so we've got
addition in place. Now, let's go ahead and let's
go a bit further and let's go ahead and set this
up with expenses. For example, now
what I'm going to do is I'm going to
set expenses here. I'm going to create a new
variable called expenses, I'm going to set as 250. Okay. Then what I want to do
is I want to go on a hedge and create another
variable called my total profit or net profit. So let's say my net profit. And I'm going to say equals, and that's going to be
equals to my total savings, which, of course,
as you saw was 900. So here I'm just going
to say total savings was 900 and minus my expenses
now to get my net profit. So I'm going to say
total savings minus, and we're going to say expenses. Okay. And that value is going to be associated
with my net profit. So now what I want to do
is I want to say print, and I'm going to go on ahead
and say my net profit. I want to see what
my profit is at the end after
deducting expenses. So now I'm going to go on
ahead and scroll down, say run, and here I can
see I have 650 now, which of course
is my net profit. The first result here
was 900 and that correlates to this
print statement here to print the savings. Now, of course, I had 900, which encapsulates total
savings minus expenses. 900 minus 250
essentially is 650, which is the value
then of my net profit, which we just output
here in our console. All right. That's how
you can go ahead and work with addition
and subtraction. That's what we have
so far in place. Okay. Now, of course, what we can also do is we can go ahead and add in some
text to make it a bit more clear to actually see what is actually going on here. What you can do to make
this a bit easier is here. We can just go ahead
and add in a space between total savings
and total savings. We can say print and we can add in a value here
and we can just say here, my total savings. Like such. We can also go ahead and
just above mint profit, add that above mint profit here. We can just say print and
we can just put on here my net profit and a colon there. Now when we say run, it's going to be a little bit
clearer and it's going to show my total savings
and underneath it 900, my net profit and
underneath it 650. That's going to make it a
bit clearer and a bit more concise to see everything
in action and in place. So we've got that now in place, and now what we want to do is we want to
go a bit further, and we want to go ahead and
work with multiplication. So what I'm going to do
now is I'm just going to add in some lines here, so you can just press Enter, and that's going to essentially
give you some space here to work with your code now. Now I want to add
in a bonus amount. The bonus amount is
going to be set here. Let me create a variable. Now what I'm going to do is
I'm going to go on ahead and say let's say bonus amount. I'm going to say
equals, and that's going to be equals
to the net profit. I'm going to say my
net profit and I'm going to say times.
I'm going to say two. Here I'm directly going to
be adding in a value itself. I'm not going to set a variable. I'm going to add in
a value this time. I'm going to say my
net profit times two. That's going to pertain
to the bonus amount here. That's what I want to do next. All I need to do now is I need to add in a
print statement, and here I just want
to add in a value that says my bonus amount. And then next line, print, and I'm going to
say my bonus amount. I've got that set, then I
want to scroll down here and run. Here we have it. Here I have my bonus
amount, and of course, I have 1,300 and that pertains
to what we said here. Let me just add in
that colon actually. Just to make it a bit
cleaner. So my bonus amount and a salesman 300, that incorporates what
we just did here. All we did here was essentially
we took the net profit, which was, of course, 650
and we times that by two, so we multiplied it by
two and we printed out that variable assigned
to the bonus amount, which of course is
the salesman 300. That's how we can work with our multiplication operator
in place. All right. Perfect. Now the next
thing that we want to do is we want to go on a head
and work with division. Okay, so let's go
ahead and do that. So in this case here, we could make an example that works with. So let's go ahead and do that. So we can just say, here, M V equals, and we can utilize
the bonus amount again. They're going to
say M bonus amount. Then we're going to say
divide it by and we're going to say 1.25 as an example. What I'm going to do like before is just add in some texts. I'm going to say print.
Here I'm going to say the amount hol on. Here I'm going to say
print and here I want to actually print out the vat. I'm going to say M at. Here we can see my va equals
the bonus amount divided by 1.25 and we're going to output that particular
value of the vat. Let's scroll down. And let's just make sure we added
the print statement, let's say run, and we should
see that new line appear. So here we can see the vat
amount thousand and 40. Okay. So it's just
a few examples here of how we can go ahead and incorporate
everything here into place and how we can
work with addition, how we can work
with subtraction, how we can work with
multiplication, and how we can work
with division. All right. So that's how we can go on ahead and integrate
everything into play, and that set for this lesson on working with
arithmetic operators.
14. Comments: Hi, eon. And welcome
to the next lesson, which is going to be
focused on comments. So let's go on ahead and
get started. All right. So the main purpose
of a comment is to explain the code and
improve its readability. Now there is another
use for a comment, and that is to comment out code. In this case here,
what you can do is if you comment out
a code that's going to prevent specific
lines from being executed in your program
that you have set out. Now, in the Python programming
language, there are, of course, two different types of comments that you can
utilize in your programs. Now, the first type of comment is known as a
single lion comment, and that is denoted
by a hash tag. The second type of comment is known as a multi
line comment, and this is denoted by three starching single quotes and it's ended by three single
quotes as well. We'll as a start, we would have three
single quotes, and then anything
that we want to add a comment about or code that
we want to comment out, and then at the
end, we would have three single quotes at the end. So how can I explain
it in this way? We would have three starching single quotes anything we
want to comment out or anything we want to
comment on and then we'll have three single quotes
ending the comment. Now, this will make more
sense when we work in the practice itself and when I show you examples
in this lesson. Let's take a look
at this lesson for examples, and then of course, in the lab exercise, we will really learn how
this is all coming together. Alright. So the first example
is a single line comment. So of course, as we can see
here in this code snippet, we have a hash tag, and then we can see the
comment that says, This is a single line comment. That's an example of how you can utilize a single
line comment. Now, of course, if you want to declare a multi line comment, it's going to be as follows. So here, as you can see, we'll have the starting
three single quotes. We'll have the comment inside. Of course, you can add
code in here as well, which we'll get to in
the lab exercises, and then the ending three
single quotes right after. That is how you can
utilize, of course, the single line comment and the multi line
comment. All right.
15. Using single and multi-line comments - [Lab]: Hi, Ron. Welcome to
the next lesson, which is going to be
a lab exercise that's focused on working with
comments in Bison. Let's go on ahead
and get started. Now, the first thing
that I want to do is to declare some variables. Let me go ahead and
set it as follows. I'm going to say first name
equals and in single quotes, I'm going to say
honor is the value. Then I'm going to say my age and I'm going to say here
20 H and then I'm going to say my nationality and here
I'm going to put in quotes. I'm going to say South Africa. Right, there we go. I have my variables declared and
the associated values. Let's go ahead and test
out the use of comments. Now, of course, comments, as the first case can help
to improve the readability of our code and to better
explain it as well. What I can do, for example, here is right at the top here, I can just add in some
lines of space and I can add in a single align
common denoted by a hash tag. Here I'm going to say declaring
personal information. That is my comment right here. Hey, and let's say I want to output the values
of these variables. I'm going to use my
print function to do so. And what I can do is I
can go ahead and just go ahead and say
print first name, print my age, print
my nationality. There we go. And what
I can do is I can also add in a comment to explain
what is occurring right here. So with this print function, what I want to do is I actually want to explain
what I'm doing with it. The print function is helping to output personal information. Now, the useful part
of comments here is, especially when you need to
show your code to someone, and they don't want to
have to rebuild the will and have to struggle and see
what your program is about. This is especially important when you have lots and lots
of code in the project and someone wants to
get to understand or learn how you built your project without having
to start from scratch and comments help to make it more readable so
people can be like, Okay, so this part of this
person code does this. This part of someone's
code does that. So that's the
importance of comments. So as we can see here, if someone were to
look at our program, they can see here, Okay, they are declaring
personal information. So first name, my
age, my nationality. And here they outputting that personal information
with the print functions. And of course, that would be available in the console
here and output or rendered, however the code is
set up in interplace. Perfect. Right. So
that is how we can utilize our single
line comments. Now, let's say, for example, we want to use a
multi line comment. So let's say we don't want
to use a single line. So what we can do is you can
just remove this hash tag, and we can just right
here at the top, add in the three
starting single quotes. And then right after that, we want to add in the
ending single quotes. There's three of them. And then you can press
Enter for space, and here is how you can
use a multi line comment. Starting three single quotes and ending three single quotes. Here we having the statement declaring personal information. Let's do the same here
for our second comment. Output personal
information. Let's do that. Starting three single quotes and ending three single quotes. Let's just add in
some space here. That is how you can utilize
multiline comments. And of course, it's
going to serve the same purpose, and of course, if we run our code, it's still going to execute
perfectly fine, but it's going to print out according to the values
of these variables. So that's how we can go ahead
and set that up into place. Now, let's say we
want to go on ahead and ensure that we don't run
a particular line of code. Let's say we're
doing some testing and we don't want a particular
line of code to run. So for example, let's say, I want to run my program and I don't want to output my age. I just wanted to output my
first name and my nationality. Now, you could technically
remove your code, but sometimes it's easier just
to comment it out quickly, just so you can do
some debugging. Let's say I don't
want to show my age, but just the first name
and my nationality. I can add in a single line
quote here, for example, in front of print age, I can add in the hash tag, which means single line
quote. Let's say run. And now it only
outputs the first name and my nationality here
in the console here. Okay. So essentially, that is how you can
stop a program from executing a particular line of code or particular lines of code by adding in a common
in front of said code. So here, for example, you're
adding in the hashtag, and this is suitable
if you just want to prevent one line of code from
executing. You can do so. Let's say you want to comment
out multiple lines of code. Here we've only
commented out one line. Let's say we want to do
multi lines of code. Then it would be appropriate
to use a multi line comment. It will be easier
because you could technically just put
in hash tags here, but that can be tedious. If you run here, you
can see it's going to output nothing because
we have essentially just gone on ahead and just decided to manually add
in the single line comments. Let's say we want to
do this all in one go. So what you can do
is you can just go ahead and remove
that remember, let's just add in some spaces. Before the code that you
want to comment out, you need to put in
three single quotes at the top and at the end, three single quotes at the end. All right. So now,
if you were to run nothing is going
to happen because you've commented out all of
the code here in between these starting and ending
three single quotes. So this is how you can
use a multi line comment to prevent code from running. Now, let's say, for example, I go ahead and only do it for the first two
print statement. So here, I'm going
to remove this, and I'm going to add a line and just comment
out the first two. If I were to say run now, it is going to Prince of Africa because I am outside
of that comment. Okay, so anything that's
in it is not going to be executed because it's going
to be treated as a comment. Alright, so that is it for
this lesson on how you can work with single line comments and multiline comments and
the various uses of them. So like I said, they're very helpful to make
things more readable, to explain code, to
explain what's happening, and also to prevent
certain lines of code from executing in the program that you are setting
up and utilizing. All right, so that's
it for this lesson.
16. Input and output data: Hi, Aon, and welcome
to the next lesson, which is going to be focused
on user input and output. So let's go on ahead
and get started. Right. So first of
all, user input. So with user input, we want to be able to allow our users to
enter information into our program and to
allow our users to do so we would utilize
the input function. Now, the input function
prompts the user for input and captures their
response as a string. And we can then use this input
to perform various actions or calculations in our program. Now, in terms of user output, we already have learned how
to utilize a print function. So to display data in
our console or terminal, we can use the print function. Now, the print function
outputs the data, allowing us to view it during
the program's execution. Now, this can be very
useful for debugging, displaying results
or for providing feedback to the user. All right. So this is the difference
between user input and user output and the functions that we can utilize
to perform them. So remember, user input, we would utilize
the input function, user output, we would
utilize the print function.
17. Utilise the input() and print() functions - [Lab]: Hi, Ron. Welcome to the next
practical lab exercise, which is going to be
focused on working with the input function to input data and the print
function to output data. Let's get started.
Now, we want to ask users to input their name and we want to output their name
to keep it very simple. Now, in such a program, we would want to enter
in some comments just for following
best practices. Let's add in a comment
here and I'm going to say here, input user name. And then I'm going
to add another one here that's output username. Okay. Let's go ahead
and get started. The code that we
technically need to use is the input function,
just like so. However, this isn't going
to be very user friendly, what you usually would do with this input function
is you're going to want to add in a prompt
because the code will run, but it's not going to
display anything as is. For example, if you put input and you leave it as an
empty parentheses here, there was nothing
in between and you were to say run is just
going to keep on running. It's not going to do anything
or anything of the sort. It's very difficult to know
what is going on here. So let's just stop that for now. Let's go back, let's go
ahead and put in a prompt. Typically, here you put in a prompt like a question
or a statement. Here you can say, for example, please enter in your name. Okay, just like that. Now if
we go ahead and scroll down, we can run that again. And here the prompt comes up.
Please enter in your name. So we can just say Ono. And as we can see here, you can then press Enter and the process ends and
the code is done. Okay. So when you
enter something, if you enter in your name or you enter any sort of data that you need to press Enter afterwards. But you can see now
the process ended, and that was all there is to it. Okay. So what we want
to do is we want to use our print function to make this a little
bit more concise. So what I want to do is, of course, just a
tedious detail. I want to add a colon there. And once I've
entered in my name, I want to say, welcome,
no, for example. But before I can do that, I need to assign value of whatever is entered in the
input like I did now with no, whatever is entered
here, I want to store the value that I enter
in here as a variable. To do so, I can say
my name equals, and it's going to be as such. Whatever I enter in here, it's going to be assigned
to my variable, my name. So what I want to
do now is I want to output a message
here with my name. I can just go ahead and say print and let's go back up here. And I'm going to say here, let's say, for example, welcome. And then below that, I'm going to say print and
it's going to be my name. I'm going to grab the name
from what has been input. Then then I'm going to press Enter after I've
entered in my name, then it's going to execute
this line of code that says welcome and then
under it my actual name. Let's do that. We can
scroll down, run it. The input has triggered, the input function
triggers here, please enter in your name. Then as soon as I've entered
it in and press Enter, it's going to store it
in the myname variable. Then it's going to carry on with the next line of code
and print to welcome, and then the name that was stored that I entered
for the input function. So remember, in this case, the input function runs first. That will give the
value for the variable, and then six and seven, print to welcome and
print my name will run. So let's go ahead and
add in the value. So here I'm going to say Arno. And then once I press Enter, it's going to associate that
with the variable, my name. There we go. I then runs
welcome and then no. That's how you can utilize an input function
and how you can also then go ahead and utilize the print function
together as a whole. Now, let's go ahead
and build upon this. Let's go ahead and work
on another example. Let's change this up. Let's
please enter in your name. Let's do another one. Let's
say my age calls input. We say, please
enter in your age. Okay. And we can also
say another one. Let's say my favorite color
equals. We can say input. Please enter in your
favorite color. All right. There we go. So now we want to
input our user's name, age, and favorite color. Then here we can just
change our comment, output our user's name, age, and favorite color. Okay, so we can just adjust our code here now,
so let's remove that. I want to add in a space. To add in a space, we
can just keep it very simple and have an
empty print statement. That helps you to have space in your code because as
you'll notice here, we didn't have much of a space. It just said, please
enter your name, Arno, the value, and then welcome.
We need a bit of a space. You can use a print function, and in quotes, you
can just leave it as an empty string here. Empty single quotes. That
will give us a space. Then here we can just say
print and we can go ahead and put in a generic message saying your details
are as follows. Then we can say print whatever value is
assigned to the name, whatever values
assigned to my age, and whatever value is assigned
to my favorite color. Just want to make it a bit more concise
so there we have it. We've got that all in place now. We're going to have
my name age and favorite color and
we're going to grab the input
value for the name, the input value of the age, and the input value of
the favorite color. Then we want to output these
as name age and color. But first, we want
to create a space. We could technically
move that comment here for clarity and
here we can just say make a space
between our code. That's a simple way
to make space between the code output in your
console in the console. Okay, so we want
to explain a bit. Let's go ahead and do that and then we'll have a space and then we output our user's
name age and favorite color. We have a message here that just says you these is as follows. Then it prints out
the name, age, and favorite color
according to what our inputs are here.
Let's get started. The order in which this
program will run will be from top to bottom.
Keep that in mind. Once you've got
that all in place, let's go ahead and run our
code and see the output. So let's start it and say run. So first, it says, please
enter in your name. That correlates as the
first line here and the value is going
to be assigned to my name of what
we've entered. Here I'm going to say no, Enter. Then it says, Please
enter in your age. Then it goes to the next line of code and prompts
you for your age. Then I'm going to go here
and I'm going to click here again and I'm going
to say 28, Enter. Then it says, Please enter
in your favorite color. It's going to execute
this line of code now. It's going to ask for the input. The value we add
in and press Enter afterwards will then be assigned to the variable my
favorite color. Let me click here
and I'm going to say Brown, and there we go. It added in a space for us, and that was
correlating to here. So make a space
between our code, so I just added in empty line. So whenever this
print function runs, it generates a line of code. And if you enter in
nothing within it, it's just going to
have an empty space. So that's the purpose behind it so you can understand
the principle. Then it outputs the details. You details are as follows. Here we have it and we
can see it says name. It says the age and the favorite color according to what we set up right here. We can see that's
been output and input as we want it to be in place. Right. So as you can see, the, the prompting text that you have within your input
function here is going to automatically appear here in your console
or your terminal, and then you just enter in what you want to
assign to the input, and then you want to save it to set variable here on
the left hand side, and of course, we
then printing out the values based on
what we have input. All right, guys. So that's
it for this lab exercise on inputting data
and outputting data, the input function and
the print function.
18. String concatenation: On, and welcome to
the next lesson, which is going to be focused
on string concatenation. So let's get started. Right, so string
concatenation refers to combining a variable
with a string. Now, this can be achieved
by using the plus operator. Now, you're probably
thinking, Okay, hold on. Wait. Isn't this addition? Technically, yes, it is. However, you can also use the addition operator
or the plus operator, you could say, when you want to perform
string concatenation. So let me give you
a typical example of how you can utilize this. Let's say that you have two variables that
you've declared, your name and your nationality. And what you can do
is you can perform string concatenation
by combining the variable and the
string together. For example, if you have a
print function here in place, you can add in the string, the strings, myname
is and I am from. You can see we have the
two variables included here, name and nationality. You can see that essentially what's going to happen is
it's going to print out my name is Anu and I
am from South Africa. We can see these two plus
operators are being used here with our strings and our variables for
string concatenation. Now, of course, this isn't
the only way in which you can carry out string
concatenation. You don't need to
necessarily put it in a print function itself. There are other
ways in which you can go with the process, but this is just generally the basic concept of how you can combine everything together and perform
string concatenation. But not to worry,
we are going to have a lab exercise on this. I'm going to show you all of the various ways in which
you can perform this.
19. Perform string concatenation - [Lab]: Hi Ron and welcome to the next lab exercise
which is going to be focused on performing string concatenation.
Let's get started. First of all, what we want to
do is define two variables. First of all, I'm going
to say name equals, and here I'm going to say no. Then I'm going to have
nationality and then say equals, and that's going to
be South Africa. Then I'm going to utilize
my print function. Now, of course, the
string conctnation. We are going to combine our strings with our variables together using the
addition or plus operator, which in this particular case, it's going to recognize
that it needs to perform string concatenation
and not addition. This is where the
distinction lies in place. I'm going to add in my string
and it's going to say, my name is then going to add in a space
and say plus space, and then we want to get the
value from the variable, we're going to add in that
variable here, which is name. I'm going to say plus then and then our next
statement and then I'm going to say and I am from, then plus and then we're
going to have nationality. All right. With this
output, it's going to say, my name is Ano and I
am from South Africa. As you can see, we're
combining our strings with our variables using
the addition operator, which essentially is known as performing string
concatenation. So now what we can do
is scroll down and say run and you'll see it's going
to look quite ugly here. It's going to say,
my name is Ono, and I'm from South
Africa, but you can see there's no spacing here. Now, what I always
recommend that you do here is when you're working
with string concatenation, especially in this context is where you see your
strings here before you are adding in or combining with your variable and after
need to add in a space. Here is before I'm performing string concatenation
with my variable. I need to add in a space. And then here is after the string concatenation of
the variable or space again, and then here it's before, and I will need to add
in a space as well. Now if I go ahead
and scroll down, it's going to adjust and it's going to look a bit
cleaner now with a space. We can now see that it says, my name is Ano and I
am from South Africa. We can see it's a lot cleaner
and a lot more concise. Right so that's
how you can go on a head and ensure that you have spacing when you are performing string
concatenation. All right. Perfect. So let's go ahead and set up another example here. So let's go ahead and
set up a variable, and I'm going to set this
as let's set this one as, um I'm going to call
this one about me. And what I'm going to
do here is I'm going to utilize string concatenation to set up a value
for a variable. So here, I'm going to go
ahead and set this as I like, and I'm going to add in a space here, I'm
going to add in plus. And then what I want to do
is I want to define a hobby, which I'm going to
add to this variable. So I'm going to say
here sport equals, and I'm going to say football. And then I'm going to go ahead
and specify another one. I'm going to say here movie. I'm going to say interstellar. And then about me, I'm
going to say I like plus, and then I'm going to
add in a sport plus, and then the space, and
then I'm going to just say and and then to add in plus and then I'm
going to say movie. So this variable now, I've utilized string
concatenation to add in a line here of what will be defined
as this particular variable. So I'm going to now
say print about me. So let's go ahead
and comment out this code for now with
a single line comment, and I just want to see
what it outputs here. Scroll down, say
run and it says, I like football
and Interstellar. Okay, so that is how
you can also perform string concatenation if you want to assign it to a variable. So that is another way in which you can go
ahead and do it. So there's many ways which you can utilize string
concatenation. This isn't a way I'd
recommend you do. It's just to give you
some practice and some understanding with how it works and how it formulates. So essentially, what we
can then do is we can just adjust this so I can just say I like watching and
it's going to say football and I like to watch the movie
and then space there. It's going to be a
bit more concise now, if I run that going to say, I like watching
football and I like to watch the movie Interstellar. That's a bit more coherent. Now what I want to do is I
want to add this about me to our long print
statement form earlier. Let's do that, remove
the print here. And what I'm going
to do is I'm just going to add in the plus
now and I'm going to say, and I'm just going to
say and a bit about me. I'm going to add
in a colon space plus and we're going
to say about me, just like such. A wretch. We should see everything
now in the output. Wha I'm going to
do is scroll down, say run, and there we go. Is my name is Ano and I'm from South Africa
and a bit about me, I like watching football
and I like to watch the movie Interstellar.
There we have it. We can see we've
added in all of that in one single line with
our print function here. That is how you can utilize
string concatenations. Remember, it's when
you are combining your strings with your variables using the addition operator, which can be utilized. Was in string concatenation and not only was the operator. PySon is very intuitive and it can tell whether
it needs to perform addition itself
with numbers adding or if it actually needs to
perform string concatenation. As we can see, it is a very intuitive
programming language.
20. Positional arguments: Hi, everyone, and welcome
to the next lesson, which will be focused on
positional arguments. So let's get started. So positional arguments. We can use placeholders to insert variables
into a string. So let's take a look at
a basic coding snippet. So here, for example, we have a variable, my name with assigned
value of honor, and we can see here in
the print function, we are setting up a string
here that says myName is, and then in curly
braces, we have name. Now, this is essentially
known as a placeholder. We can insert our variable
into this string here. Now dot format is
essentially a method, which inserts values
into placeholders. It allows us to do so. We also then can see
here that we have name. Now, name here is the keyword
argument that it is used to pass the value to the
placeholder here. So notice here, I've used the same name to describe
what we are setting up here. So I've matched the
keyword argument with the placeholder name here for
continuity because what's going to happen is the
value here of the variable which is no is going to be assigned to the
keyword argument, which is name, and
that's going to be placed into the placeholder. And you'll see as the
output here in the console, it will say, my name is no. That's how you can
essentially utilize placeholders to insert your
variables into a string. Now, don't worry,
we are going to have a lab exercise on this, and we're going to work
through a few examples here so that we can cement our knowledge on these
positional arguments that we have in place, right. So that's for the yetical part.
21. Format string by position - [Lab]: Hi on and welcome to
the next lab exercise, which is going to be focused
on positional arguments. Let's go on ahead
and get started. What we want to
do, first of all, is to declare a variables. I'm going to say my name
calls and I wait to see no. Next, what I want to do
is I want to go ahead and define my print function
here. I'm going to say print. W the string here, I'm
going to say my name is and then I'm going to set up my place holder, which
can be anything. I'm just going to
set it as name. Then after the string here, after the quotes, I'm
going to say dot format. Then I'm going to open a new set of parentheses here was in the print function
itself and I'm first going to define
my keyword argument. In this case, I'm going to refer to whatever I name
the placeholder as. I'm going to say name equals and then the variable
that I want to assign, which is my name. Okay. We have our placeholder. We have our keyword argument, which is essentially
you could say is our placeholder without
the curly braces, and we're setting that equal to the value of the variable
my name, which is no. Essentially, what's
going to happen now is the name here is going to substitute the value here
in our curly braces of nu. It's going to print,
my name is no. Let's go ahead and test
it out so we can scroll down and we can say run. And it says, my name
is Arno. Perfect. Right. Let's go ahead and make this a bit
more interesting. Let output our first
and last name. What I can do here is I can
do a bit of reformatting. I'm just going to set this as
my first name and I'm going to switch this to F
underscore name for first name and here,
F underscore name. Here I'm going to
say my first name. Let's just make sure
this works here. All I've done is I've changed my variable name
to my first name. I've changed the
keyword argument here to F underscore name, which matches the place
holder F underscore name. However, the place
older does have curly braces within
the string itself. I can scroll down and run. And it still says,
my name is on it. Perfect. Right. So we can now get everything prepared
for our last name. So I'm going to say
my last name equals, and I'm going to say Victorious. And we can change
this here to say my first name is F name, and then I'm going to
go on ahead and say N, my last name is. And here in the placeholder, I'm going to say L
underscore name. And what we can do now is we can put in multiple keyword
arguments here. What I can do is put in
a comma and then say, L underscore name is going to
be equals to my last name. I want to say my last name. Okay, so as we can see here, it's now going to append
that accordingly here. So now we're going to have name, which is going to
reference position, this placeholder, excuse me, with the value of my last
name which will be Pretorius. At the end here, we're going to see that it's going to
print my first name is Ono, and my last name is Pretorius. Now if we were to scroll down, we can say run then we go, my first name is Ano and
my last name is Petors. Perfect. We've got
that in place. Great. Let's go ahead now and let's create
something from scratch. Let's do another example. Let's say my
favorite car equals. Here I'm just going to
say heslaF the next one, we're going to have
my favorite color and we can say brown. Right, so let's go ahead
and get started here. So we'll start with
our print function, and we're going to set
the checks up for us. So I'm going to say, I like the car. And in my place soldier, I'm just going to set this as F underscore car
for favorite car. And then I'm going to say N, I like the color. And here I'm going to
say F, underscore color. Perfect. So we've got my
place soldiers set up, then I want to say dot format, and now I need to add
in my keyword argument. What I'm going to do
here to make it a bit cleaner is I'm just
going to zoom out a tad bit here and I'm going to
add in my keyword argument, it's going to match whatever's
in the place older. F car first F underscore car. Equals, and that's going to
pertain to my favorite car. So I'm going to say my
favorite R. First, done. Next, we want to have the color. So F underscore color would be the next keyword arguments. I'm going to say F
underscore color, and we say equals, and
that's going to pertain to this variable here,
my favorite color. So we can say MP
color. There we go. So we've got that now
all into place now, and what we can do now is
scroll down and we can say run but I like the card Tesla
and I like the color brown. Perfect. That is how
we can essentially go ahead and ensure that we're able to fully utilize positional arguments and how
to add them all into place. Guys, that's it for
this lab exercise.
22. String methods: Hi, eon, and welcome
to the next lesson which will be focused
on string methods. Right, so string methods. In PySon there are
various string methods available to us that we can use in our program such
as capitalize and upper. There are various other
methods which are focused on setting our data to
lower case letters, uppercase letters,
capitalizing them, and also to change various
positions of letters, to add and remove
letters, et cetera. There's a lot of methods out there that we can
utilize and we're going to go through quite a
few in the lab exercise. Now these methods allow us to modify and manipulate our
strings in different ways. You can decide if you want to modify your strings in
this way or in that way, it really depends
as to how you want to proceed and go
forward with that. Now, as I mentioned and just to elaborate further,
a few examples. Some examples may include
changing the case, trimming white space, or
even replacing characters. This is what you can expect
in the lab exercise. We're going to go through
a wide variety of all these methods and
we're going to focus on them as we work on
manipulating our strings.
23. Utilise various string methods - [Lab]: Hi, Ron, and welcome to
the next lab exercise, which is going to be focused on working with various
string methods. Now, as you can see
here, I've already added in some comments here, and that's just to
structure the process of teaching more efficiently with the message that
we'll be learning. So the methods are going to
pertain to capitalizing, making everything lowercase,
making everything uppercase. Of course, also getting the
lengths of the strings, replace starts
with ends with and a lot of these are actually
methods themselves. Find position is numeric and
of course is Alpha numeric. We're going to be going
through a lot of methods as we work through this
practical lab exercise. Let's go ahead and get started. Now before we
actually do anything, we need to have a
variable that we want to basis of with
associated value. What I'm going to do I'm
going to start off like that, and we're going to
delete each comment for each particular method
that we have mastered. So I'm going to create
a variable called my first name equals, and I'm just going
to say to like so. Perfect. So let's learn about
that capitalize method. As you can see here, it's
probably going to be to capitalize our
string values here. So if I were to go
ahead and test it, so we're going to use
the print function to help us to test all of this. I'm going to say here
print and in the function, I'm going to refer
to the variable, which is my first name. Then to set up the mesod, we want to just say dot caps
with an empty parentheses. So we should see in the
console that it should just capitalize the first
letter here of string. The A here should be a A. Let's go ahead and
test that out. If we were to scroll
down, we can say run. And it says Ono. Notice
here that the A now is the A and it's not just a
lowercase A that it's shown here because this dot capitalize
method went on ahead and ensured that we capitalized
our value here in place. Perfect. Great. That is how you
can use capitalize. We can now remove this next, we want to make
everything lowercase. Now, as of the moment, we can see everything
is in lowercase, so we want to do a
good test for this. What I'm going to do
here is I'm going to set this to honor. I'm going to purposely
make it uppercase, and then here, I want to
go ahead and say print. Let's refer to the variable, my first name, dot,
and then of course, the method here is
just going to be called lower and then we want to open and close a
parenss here in place. Now what's going to happen
is it's going to take the value here of honor
and it's going to apply the dot lower message to it and everything here
will be in lowercase. Now if we were to
run the program, you're going to see here
it just shows all in lower case because we utilized as we can see
here the dot lower mesod. That's how you can go on a hedge and set that up so you
have that in place. Perfect. That's how you can
make everything lowercase. Great. Let's move
on to the next one. Now we want to make
everything uppercase. Now, as you can see, it's
already an uppercase. As a good test, we're going to put this all
in lowercase now, and now we can utilize our
print function like before, you want to refer
to your variable. We can say my first name, you don't want to say dot upper, opening and closing parentheses. Now what's going to happen is
the value here is going to transform into complete
uppercase letters. A, R N and O right. Now we can just go
ahead and scroll down and we can say run, and you'll see now that it's
going to show onom here in all caps due to the message that we just went
ahead and set up. All right, perfect. So we
now know how to use pergas. So we can remove that next, we want to get the links. So this is going to be
a very interesting one. So let's go ahead
and focus on that. So we want to get
the links here. So what I'm going to
do now is I'm going to go ahead and say print. And what you're going to want to do is you're going to want to use the LN function here. So we're going to use
a type of function. So I'm going to say in here within the print function,
we're going to say Lang. And within the parentheses, you want to put in the variable. Here we're going to
say my first name. When I say print length and in parenths, I
put my first name, it's going to print the
lengths of your name here, the total number of characters. We can see here we have one, two, three, four, it
should print out four. Let's go ahead and
check that out. Now if I scroll
down and say run, it's going to print out four. Perfect. That's how you can get the lengths of your strings. You just put embed ng within your print
function with pareness. That's how you can get the
lengths of your string values. We've got that done.
Next, we have replace. This is quite an
interesting one. We want to now go ahead and
set this up for replace. What I'm going to do now is I'm going to use my print function. And you want to just refer
to your variable again, my first name, we're
going to say dot replace, and this is where it
gets interesting. You need to open up your
prenss and you need to put in what you want to replace and what you
want to replace with. The first argument is
what you want to replace. I want to replace the A. A, and I want the A to
be replaced with a B. Though it becomes Brno. The A here is going to be
switched out for the B. What's going to happen is
with this dot replace method, it's going to look
for the As or the A in your string and then it's going to
replace it with a B. We can see here it should now be BRN O with the dot
replace method. Now let's go ahead and
run that and test it out, if I say run, the Bro. Okay, so that's so you
can go ahead and replace, of course, values set
up in your string. Okay. Perfect. So
we've now learned how we can utilize the
replace method. Next, what we want
to do is we want to start off with the
starts with method. So we now know how to replace. Okay, so starts with is next. Right. So again,
we're going to use our print function
for demonstration. We want to start off
with the variable. So we're going to
say My first name, and then we're going to use
the dot starts with message. So starts, excuse me, starts with and opening
and closing pareneses so make sure you put
starts with here. Okay. And what
we're going to want to do is we want to check. So this serves as
sort of a check here. So if I were to say, A, Okay, so we want to check if the
first name starts with A, and it's going to either
say true or false because this method
returns a bull. So BleanRmember, true
or false states. So this should output true because the string here
starts off with an A. So what I can do now is
scroll down and say run. And it shows true because
the string starts with an A. Let me go ahead and change
this to maybe let's say O. So does it start with an O here? The string? It does not, but let's see what the
result is going to be. So if I were to say run, it is false because it starts with an A and not
with an O. All right. So that's how you can utilize
the starts with method. All right. So we've got
that in place. All right. Ends with this one is going
to seem quite obvious. So it is very similar to what
we had with starts with, so we're just going
to check the end. What does the string end with? So we can just simply say print, and we're going to
say first name. This is what we want to
apply this towards dot. We're going to say ends with A. And then what do you want
to check if it ends with? So we're going to first
check if it ends with an O because it should end
with an O because, no, the end of the
string will be an O, and then we can go ahead
and evaluate this. So let's go ahead and say run. And it says True. Perfect.
So that works fine. Now, let's say we
wanted to just say A to actually see if it ends
with an A, which it doesn't, but just for some proof because the dot
ends with method is also evaluated on the
result of a bool. So let's go ahead and check now, so it should be bols and
there we go, it says false. Perfect. So we can see that that method
also works nicely. So we've also learned
now the endswith method. Great. Next, find position, and this is actually
quite an interesting one, which I feel is definitely something that you'll
find interesting and we're going to learn quite a lot about it in the near future. What we want to do is
to find the position. We want to start off
with our print function. Then, of course, the
variable that we want to apply this to and then
you want to say dot find. So we want to find a particular character or
letter within our string. Let's say I want to find N. So I'm going to say
N. I want to find N, the position of N in
my name here on it. You can scroll down
and we can see run. And it shows two. So we can see here that it's
in position two. But you're probably thinking,
Okay, this doesn't make sense because A should be one. R should be two, N
should be three, and O should be four. But it's showing two for us. Now, the reason for this is when you are working,
for example, lists in Pyasin or anything
of the sort, Okay, what's going to happen is
it's going to evaluate positioning starting off on zero for your first character. And this also applies when you are utilizing the
dot find method. So for clarity here, let me go ahead and write
some notes here. So we can say A is
in position zero. R is in position one. And then we're going to
say N is in position two. And we can just say O
is in position three. That's how it works
here in Pison here. It's going to start
off from zero, one, two, and three, especially in case it doesn't
start off with one. Even though you can see here earlier when we
counted the lengths, it showed four
because technically, one, two, three, four, we have four instances. But here, when we are dealing with this and looking for
a particular character, the positioning, we
always start at zero. We can see now that N here is going to be in position two, which we found out here
to be in the output. Let's go ahead and
test this out now. Let's go ahead and say, Oh. That should print
out position three. Now let's do that. Run
and it shows three. Let's go ahead now
and set this up for another position.
Let's say A. That should print out
according to our notes, that should be position zero. Now if we were to scroll down, we can say run and
it shows zero. Perfect. That's how we can
go ahead and set this up. Now, I know this
is a bit foreign and a bit confusing and
out there possibly, but don't worry in
the later lessons, this is going to
make more sense, especially when we're
dealing with lists, Topols and sets, et cetera. I will make more
sense in the process. Don't worry about that if it's a bit overwhelming or
confusing for now. We can just go ahead
and remove this. That is how you can
find a position with the dot find method. Okay, so the last two
messages are going to be quite easy to understand. So let's go ahead and
do that. So is numeric. So we're going to set up our print function
and we're going to pass through our variable, and we're going to
say dot is numeric. And here in parentheses, we just want to leave it empty. So it's going to check if our string here is
essentially a number. So as we know, it's not
going to be the case, so it's going to show out false. So we can scroll down
and we can just say run. Right. So we can see that
the output is showing false. So that means we got it correct and it is
perfectly in place. So we can check that the string value
here is not numeric. Hence, the reason
why we're seeing it false in the console. Great. So we can remove this. Okay, so now Alpha numeric. So again, we're
going to say print. We're going to pass
through my first name. Then we're going to see dot is, and here we're going to say is for opening and closing parenss. So there is Alpha numeric method or there is Alpha method, we are going to be checking if this particular
value has any um, text characters or
numbers are part of it. In this case here, we
have letters and checks, so this is going to
output true because alphanumeric deals with
characters as well. If we were to scroll down now, we can say run and it's
going to output true because alphanumeric deals with not just numbers with numeric, but Alpha, it's also going to be dealing with characters
and texts and so forth. All right guys, that's it for this practical lab exercise. I know it was quite long, but there were quite a few
messages that I wanted to teach you so that you
are aware of them.
24. Casting: Hi Avon, and welcome
to the next lesson, which is going to be
focused on casting. So let's take a look. Right. So the question
on our minds is, of course, what is casting? Now, casting refers
to converting a variable's value from
one data type to another. So for example, you could cast a string to an integer or
an integer to a string. Now remember,
integer, of course, is a longer way of
saying int, okay? So if I were to rephrase that, you can cast a string to an
inch or an inch to a string. Now, this can be
very useful when you need to perform
operations that require specific data types or when
working with user input. So it really depends on how you prefer to go about
setting up your program, what are your requirements and how you want your
data to be received? All right. So in
the lab exercise, we're going to go into a bit
more detail and work with a few examples in performing
casting within our program. So let's get straight to that.
25. Casting to different data types - [Lab]: Hi, everyone, and welcome to the next practical
lab exercise, which is going to be focused
on understanding casting. Now, there is quite a lot to it, so we're going to gradually
build our way up. So let's go ahead and
get started very simply. Right. So first of all, what I want to do is to
create four variables, with the four data
types of string or SDR, Int, float and Bool. So let's get started. So
first, I'll have name, and I'm going to put this
in double quotes now, and it's going to be honor.
That will be my string. Then we'll have Int. So
here I'm just going to say fave favorite number, and I'm going to say
here searchin as my Int. Then I'm going to, of
course, have a float. So here I'm going to say
account savings equals 405.75. And then I want to
essentially have a bull. So here I'm going
to say is active. Going to say true. Okay, so there we have it. So we have our data type settled. So we have SDR string, we have Int, we have
float, and we have bull. Okay. Now, what we want to do, first of all, is we
want to evaluate this. We want to actually check if
this is based on a string, int, a float, and a bull. So we're going to evaluate
these variables to see if the value that we set to them is
indeed based on the correct data type
that we have learned. To do so we can utilize
a function known as a type function which we can embed within our print
function to check that. What we can do
here, for example, is underneath, we can
go ahead and say print, and then we want to add in a type function and
we want to add in the variable which
we want to check for the type of its data type. We want to check
its data type of a particular variable.
Let's check name. Name should be string or SDR, so we can put in the parenses
here of type and name. If I were to scroll down, and run this, it should show
the following class string. Here we can see SDR, which means that this
is based on a string. So the data type here is string that is associated
with the variable name. Let's go ahead and
check this out for favorite number account
savings and is active, so we can go ahead
and write our code. Print type and in
parencs for type. We can put in fave number. Then print type, we can put
in here account savings, and then we can say print, type, and in parentheses of type, we can put in is active. Just like that. So
it should show STR, int, float, and ball. So let's see if we
scroll down and we see run there we go. We have STR for string, int, we have bull,
and we have float. That is in accordance with
what we have defined here. This is how you can get
the current type of your data types
that you have set up and whatever is in place. Let's go ahead and
go a bit further. What we can do is we
can now remove this. Let's make it a bit
more interesting. Let's say, for example,
we have number one, and this is our first
number and we have 43. Then we were to say
print number one. Run. Print out 43. Perfect. It shows
43 in that output. Now, let's say print 43, but it was in strings. Print that out now. We can
see it also prints out 43. Now, we cannot see on the output here what
is the distinction, so we can use our type
function to figure it out. So what I'm going to
do here is I'm going to say type and encapsulate number one within
parentheses here. So now, try to think
to yourselves of what that is going to be because it's encapsulated in quotes. So since it's
encapsulated in quotes, and if I were to
run this program, going to show string
because remember, whatever is in quotes is going to be treated
as a string as text. If I were to remove these quotes now and keep it
as the raw number with anything else attached, and if I were to run this, you're going to see that a
data type is going to be int. Okay. So this is just a short depiction here that I just want
to show to you so that you can understand
that there is a difference in how we are going to figure out what is the difference between what
we are working with. Okay. All right. Perfect. So we
can now understand that. Now, let's say, for example, we want to change
an int to a string, and we want to change a string
to an int or an integer. So let's go ahead and
work with two numbers. So what I'm going to do here is I'm going to keep it simple. I'm going to say here
my first number, and I'm going to set that to 42. Then I'm going to say
my second number, and I'm going to set that
to let's say this to 50. Okay. So we have our
two numbers here. Right. So what I want to
show you, first of all, is what's going to happen if you decided to add
these numbers together. So if I were to say
print and I say my first number plus
my second number, and I were to scroll
down and say run, it's going to add
in these numbers together and display
the result of 92 since 50 plus
42 gives you 92. Hey. Now, just to demonstrate the whole point of working with addition and a bit of
string concatenation here, just to build an
understanding of casting, if I were to change
this to 42, and 50. These two variables
here are going to be assigned based on
the string data type because we put it in text. So now, technically, we're going to perform string
concatenation now because we are
working with strings and we are adding them
to our variables. So now if I were to
scroll down and say run, you're going to
get the following, which is 42 50. So it's just added in 42 and 50 together that it's appended
to string concatenation. So remember, there is a distinction with
integers and strings. Remember that when you
are trying to cast. Okay. All right. Perfect. Okay, so we've got
that in place. All right. Now what we want to
do is, of course, we want to go further and
really get to changing the integers to strings and strings to
integers. All right. So let's go ahead
and get started. The first thing that I
want to do is I'm going to go ahead and keep it
simple and I'm going to say number one for number one. Here I'm going to
set this as 43. Okay. And what I want
to do, essentially, is I want to now change
this integer to a string. So I'm going to cast it now. So to do so you can say print, and you want to add in the data type of
string, which is STR. And here in parenses we want
to refer to that number. So we're going to say num one. So now what's going to
happen is it's going to print out that number
in a string format. So if I were to say
run, it prints at 43. But now what we can
do is we can evaluate this by looking at the type
that we'll be working with. If I were to now go ahead and put in the type function
around all of this, we can go ahead and say run and it's going to
class it as a string. That means whatever
is printed out here is now in the
string format. If I were to leave it as it is and remove this casting
of SDR and just say print type one as is and say run it's going to print out
Int because it is an integer. But as you can see,
what we did was, we cast it to a string. Okay. All right. Perfect. So that is
how you can go ahead and change your
integer to a string. Now, let's do it the
other way around. Let's set this to a string. So let's say we want to change a string to an integer or Int, of course, what we would do is we would just go
ahead and say Int. And we'll put number one, and of course we
can just say run. It's, of course, going to show the same result either way. So 43, but the real difference here is when you put in now the type function right around the Int casting
function here, it's now going to print
the type of an integer. So now if I were to say
runs just going to say Int. So we've casted from a string
to an integer or an into, using the casting of Int. Okay. So that's
how you can change around depending on
what you want to have in place and how you want your integer to change from one side to the other side ultimately and add
that into place. Okay, so that's
very important that you know how you can
change the differences. Right. Perfect. Now, let's go ahead and work with our inputs. So what I'm going to do
here is I'm going to say my first name equals, and I'm going to use
an input function. And here, I'm going
to say, please enter in your first name, adding the colon and a
space here at the end. And what I'm going to do now is I'm going to go ahead
and set my print statement, and it's going to say
type and I'm going to put in my first name. So essentially, what's going
to happen now is we're going to be expecting
a value to be entered, and it's going to output the type of the value
that was entered. Okay, so let's go ahead
and test this out. So I'm going to say run Okay. And it says, Please enter in your first name. I'm
going to say no. Then I want to press Enter. And then it says,
Okay, class string. Okay, so it can track that the data type here
is that of a string. So that's how you can
go ahead and figure out that particular type that
was entered in for you. Okay. So that's how you can
go ahead and figure that out. Now let's go on ahead
and work through this process with numbers and
also work on casting now. We can go ahead and remove this. Let's say we were
to say number one equals and we're going to
use the input function. Here we're going to
say, please enter in a number in the
colon and space. Then we're going to have
number two, to say input, and here we're going
to say please enter in another number space. What I want to do now
is I want to just say print number one
plus number two. So as you can already expect, what the result is going to be, but I'm not going
to say it just yet. What we are going to want
to do is we're going to want to see the
process as it unfolds, so we can see run a number, let's say ten, Enter. Another number, let's
say five enter, and we can see say 105. Remember, we are performing
string concatenation now, so it's adding the ten and
the five together to get 105. Okay, so for this to work, we're going to have
to go on ahead and adding casting
with our integers. For this to actually
work, what you're going to want to do here
is you're going to want to put casting
with your int around your variables for number
one and number two, so that it's effectively going to allow us to
add those numbers. So should be 15, not 105. So we're now casting it, so our type now is going to be in number format
when we add them in. So let's go ahead
and do this now. Now that we've added in casting, we can go ahead and say
run and let's say ten, Enter, five, enter, and there
we get the result of 15. So we added in some casting
here with our integers here in place so that we're able to convert this properly
as it should be. Okay. So that's how
we can go ahead and work with that
process and how we can implement
casting when we are casting our numbers in fact. So that's something
that we want to ensure that we are
working in detail with. Okay, so it's very
important that we do just that. All right. Perfect. So that's how we can
go ahead and add that in. Now, something else that we can actually do is we can change
the process up a bit. So you don't need to implement
casting at this level. You can also implement it at the level here
with our inputs. Meaning you can
just keep it here as number one plus number two. But then what you need to do is you need to make sure
that you implement casting here around
your input function. What you need to do is you're
going to want to ensure you put an int like so
right around it. I automatically knows
that whatever value you enter here is going to be treated as an int
and not as a string. By default was the
input function. Everything will be as a string. Now you just need to say number one and
number two because the casting is already
done here for you. If we were to say run now, and let's say 20 enter, and we were to put in five
Enter, we're going to get 25. So the casting is
already done for us at the following level. So that's how we can do that. Now, a good way to
inspect this as well, just for your own sanity
is what you can do is you can go ahead and you
can actually test this out. So if I were to
remove the following here and just do
this simple chest, I'm just going to look at
the type for number one, I'm not going to focus
on the calculations. I just want to see
the type here and we can actually remove
number two for now. It's automatically going to
be transcribed as an integer. If I were to say run, and I put in 20, it's going
to be classed as an int. We're printing in the type
of whatever numbers entered. But now, if I were to remove the casting to an int and leave it as is
and get the type now, it's going to be as a string. If I say run, and I put
something in such as 50, it's going to be
treated as a string. So remember, by default, the value that is
assigned based on your input function is going
to be a string by default. So please keep that in mind. Very important that
you keep that in mind when you're working with
this. Okay, perfect. So let's go ahead and
do this for floats. So we're going to say
number one equals, and we're going to go ahead
and do this for float. So you can also say
float and input. So you want to automatically
change this to a float. And we're going to say here, please enter in a number. And then number two, we're
going to say float input. Please enter in another number. Now, I know this is a lot
of doing things again, but I just want to work with you through practice
and lots of coding. So I'm doing this
all again myself, so it's just for your benefit. And then we can
say total equals, and we can just say number
one plus number two, and then we can just
say print Total. Okay, so let's go ahead and chess this out
in this format. So we're going to be
expecting a number there to be assigned to
number one, number two, afloat and then add
them up and then print the total. So let's run this. So float. So remember
decimals, 24.2, enter and then another number, let's say 40.1, Enter. And these our results 64.3. There we have it. Great. So
the floats also working. And you can also put in the type function here just to see that you're getting the correct total as well. Just make sure you add in the correct number of penses
when you open and close. So of course, we can run that again and we can just
check the type that we're going to receive 243.1 12.2 we get now that it's
going to be a float. We can see now excuse me,
we've got the float here. We didn't print the total,
we focused on the type. That's how you can
use a float as well.
26. Lists: Hi, everyone, and welcome
to the next lesson, which is going to be focused
on lists. Let's take a look. The question on our
minds is what is a list? A list is an ordered, mutable collection that
allows duplicate values. Now, for those that have programming experience,
let's say, for example, you're coming from
a Java background or C sharp background, et cetera, a list is very
similar to an array. Now a list is also represented
by square brackets. Now I'm going to give you
a coding snippet just to make this a bit more
clearer to understand. So here, for example, I
have a list of names. Now I have my variable
name list, okay. And of course, the data type in this case would be a list. So in my list here, which is
denoted by square brackets, I have various values within
my list such as John, Mark, Sarah, Kim, and Kate. So this is a list and what
we're going to be doing. In the next lesson is we are going to be learning
how we can utilize lists and how we can go about them and query them
effectively with understanding the
process behind them and just learning a bit
more about lists in Pison.
27. Create a list and output it's values - [Lab]: Hi, everyone. And welcome to the next lab exercise
which is going to be focused on working
with lists in Pyson. So let's go ahead
and get started. Now, first of all,
what we want to do is we want to declare a list. So of course, we'll start
off by declaring a variable. So I'm going to go ahead
and call this names equals, and then I want to
define my list. So remember, it is represented
by square brackets, and then you want to
insert the values here. So we'll have multiple
values for our list. So I'm going to
go ahead and say, Jack, and I'm going to say, Jessica, Evan, let's say, Marcus, and we have David. Okay. So here is our list in place, and all of the values or items
you could say in our list. So we can see we have
five in place here, five items or five values. And what we want to
do is we want to output all of those
values in our console. So we can utilize
the print function, so we're going to say print, and you can just say names. So let's go ahead and
scroll down and we can say run and we can now see all
of the items in our list. So we have Jack,
Jessica, Evan, Marcus, and David, and that correlates
with what we have up here. Perfect. Now, let's say we want to get a specific
value from our list. So let's say I want to output
not all of the items here. Let's say I just want to
output, let's say, Jessica. So in terms of Pison
and in the list, okay, the first item or value that you have is going to be
noted by position zero. So Jack is position zero. Jessica will be position one, Evan position two,
Marcus position three, and David position four. Now, if I were to go ahead
and say position one, that's going to output Jessica. But let's correlate in
the console for sure. Let's say run and
it outputs Jessica. Perfect. Remember, again, I will also add this
in a comment here. Let's go ahead and break it down here just for
us to understand. In a list here, of course, we'll have position zero. That's going to be Jack. Then we have the next one, which is position one, that's going to be Jessica. Then next, we're going to have position two, that's
going to be Evan. The next we would
have position three, that's going to be Marcus. And then next, we will
have position four, and that's going to be David. So this is how the process
works in the list. So we'll always start
counting from zero, and then that would
be the order, so one, two, three, four. Okay. And that's also
how you would grab out a particular value or item from your list by referring to
this pattern ordering here. If I were to want
to output Marcus, I can just switch this out for three and you can
also add it here, zero, one, two, three, and that's going to
print out Marcus. So now if I were to
scroll down and say run it prints out marks. That's how you can
get a specific value. That's how we can go
ahead and do just that. Perfect. That's how
we can do that. Now, what we want to do is we want to dive in a
little bit further and let's say that we want to not just get
a specific value, but we want to get
the links here. To do so, we can just go ahead and within
our print function, we can add in the Lang function and then we can
just put in names. Now we're going to get
the lengths of our list. Let's scroll down. So run
and it's going to show five. Now the reason that we have five here is because what's
going to happen is our leng function here is going to count how many
values we have here. So one, two, three, four, five. Okay. So that is how
it's going to count. So it's like a
counter just for us to count on our hands.
How many do we see? How many do we have
here and into place. Right. Now, let's say we want to add an extra
value to our list. So we have Jack,
Jessica, Evan, Marcus, and David, let's say we want to add another
name to the list. So let's say my name no. So what we can do
is we can go ahead and refer to our variable. So we're going to
say names, dot, and we're going to utilize
the append method. And within that,
we're going to add in the value that we want
to add to this list. And this value, of course, it's going to be added
to the end of the list. So we're going to
say dot append, and I'm going to say no. And to check that this
change has been made, afterwards, we can say print. Names. Let's see if
that's been added. So we can run our console. There we go. Jack, Jessica,
Evan, Marcus, David, and now it's appended no at the end of the list,
right at the end. That's you can add in an
extra value to your list. You can utilize the append method and then you would add in parentheses that
particular value that you want to
add to your list. That's how you can go ahead
and add that into place. Okay. Now, let's say you want to remove a
value from your list. Let's say we don't want
to append a value, we want to remove a value. So we can just remove
the following here, and we want to use the
dot remove message. So we can say dot remove. And here in penses quotes. So you want to choose a particular name that
you want to remove. I want to remove,
let's say, Jessica. So I'm going to go ahead
and put in the value as is. Remember, it will
be K sensitive, so you need to be exact. I want to remove Jessica. Then it should output in
my console just Jack, Evan, Marcus and David. Now if I were to go ahead
and scroll down and say run, we should now see
it show Jack, Evan, Marcus and David and it just
skipped ahead where Jessica was because Jessica has been
removed now from our list. That is how you can
essentially remove a value from a list. That is how you can go
ahead and do that in place. Now, there's something
else that you can also do, and that is to insert a value
in a specific position. So first of all,
let's just reiterate on the append
message that we had. So I'm going to go
ahead and then go ahead and say dot append, and I'm going to say ono again, and this will appear at the end. So let me go ahead
and rerun everything. Then we get Jack, Jessica, Evan, Marcus David Arno
Anno is at the end. Let's say I want to insert
Arno into a specific position. So I don't want it
to be at the end. So remember what I taught
you about positioning, okay? So what you're going to
want to do now is you can remove the dot a
pen mesod and we're going to utilize the
dot insert method to insert a value in
a specific position. So we can say dot insert, and we're going to
open and close up parenss then you want
to choose the position. I want Anno to be in front the first value
in front of Jack. So I will say zero, comma, and then I'm going
to put the value that I want to insert,
which is going to be no. So no should now be in front, and you should see now it
should make a change here. If I say run, you can
see now it says Anno, Jack, Jessica, Evan,
Marcus, and David. And you can now
see that was there a Pen mess that automatically will add your value to the end, but with the insert mess it is specifically going to be pushed to the particular location. So since we put zero, that's going to be the
first value in your list. Okay. Now, let's say
we want to add it to just after Jack. So that would mean in
Jessica's place, which is one. So we can just go ahead
and put in one here. And scroll down, say run, and you should now
see, of course, Jack and then no
and then Jessica. So that's how you can
insert particular values in particular
positions. All right. So that's how we can go on ahead and ensure that we're able to have that in place.
Great. All right. Now the next thing that I
want us to do is I want us to learn how we can
reverse our list. So let's say we want to
list the other way around. So we want to showing David, Marcus, Evan, Jessica, and Jack. So we can just say names dot, reverse empty pareneses and then we can just
say print names. So let's go ahead
and scroll down, and this should
reverse the list for us. And there we have it. So we can see we have David, Marcus, Evan, Jessica, and Jack. So our list is now backwards, so we have reversed
our list successfully, and it's now in place
as we can see it. Right. Great. Now the next
thing that I want us to do is I want us
to sort the list. So this is going to be sorted, of course,
alphabetically for us. So let's go ahead and
do that. So we're going to go ahead
and say names dot, and we're going to
use the sort message. So that's going to sort
this alphabetically for us. So, of course, we're
going to definitely have our name list
starting off with, of course, David, and then
it's going to be then set to, of course, Evan, and
then we'll have, of course, the rest in
place such as Jack. Then we'll have, of course, Jessica, and we'll have Marcus. Let's go ahead and do
that. Let's run this. Now it's going to be
in alphabetical order. We can see we have David, Evan, Jack, Jessica, and Marcus. That's if you want
to alphabetically sort your values in
alphabetical order. You can utilize the
dot sort method to go on ahead and do so. You can go ahead and integrate
that as you would prefer. Now let's say for
argument's sake, you want to change
a particular value. Let's say you don't
want it to be Jessica, Jessica is not a part of the list anymore and
Kate is instead. What you can do is you can
utilize the following, which is to directly change a value according to its
position by referencing, of course, the value
and then the position. What we can do is we can say names and with our
square brackets, we can look at the
particular location. Jessica is in position one, so we'll put in one
and we then going to say equals and that's
going to be now Kate. Then we want to see
if this took effect, so you can say print in parenses you can
just put in names. Now it should show the whole
list saying, Jack, Kate, Evan, Marcus, David, we're replacing and
changing the value. Now what we can do is scroll
down and we can say run. And here we go, Jack, Hate, Evan, Marcus, and David. So as we can see now, we have changed, and
we've swapped out. You could say the value
of Jessica for Kate, since we went ahead and modified the value in position one, which is Jessica
because Jack is zero, Jessica would have
been one, which is now Kate, since
we changed it, and we printed the
whole list with that updated change that
we have set up into place. All right guys. That's it for
the lab exercises on lists. Hopefully now you'll have
a better understanding of some comprehension
exercises that you can do with a list and the various
messages that you can incorporate into them and
how you can utilize them. Remember, a list is
one of our data types. It took a while for
us to get to it, but here we finally are with
incorporating it in HSN.
28. Tuples: Hi, von, and welcome
to the next lesson, which is going to be
focused on tuples. So let's take a look.
So what is a tuple? A tuple is an ordered
collection that is immutable and allows
duplicate values. So it is represented
by parentheses. So a distinction between a list, which is with square
brackets and a tuple is that it is represented
by parentheses. Here is an example
or a coding snippet, should I rather say, of a tuple. As you can see here, it is
very much similar to a list. However, two key differences
is to note, of course, that we have
parentheses here and that a tuple is immutable. You're going to see
what I mean by that in the practical activity. We will be working with a tuple and you'll see
what I mean by immutable. That is something that we will
cover in the lab exercise. But at least now you
know what is a tuple.
29. Create a tuple and output it's values - [Lab]: Hi, everyone, and welcome to the next lab exercise
which is going to be focused on utilizing
tuples in pyson. Let's go ahead and get started. This is, of course,
another data type which we are learning,
which is tuple. What we're going to do is
we're going to create a tuple, and of course, it's going to
be assigned with a variable. Let's go ahead and do just that. I'm going to say names,
I'm going to say equals. Here I'm going to create
my tuple because remember, tuples are created
with parentheses and not square
brackets like a list. I'm going to enter
in some names. I'm going to say on a then
I'm going to say pen, and then I'm going to say Sarah. Okay, so there I have the names. Now, to keep it very simple, let's go ahead and output
all of the values of my tuple so I can just
simply say print. Names. Then I can head
on over to my console, say run and it's going to
output my list of names. There I can see my values, Anno Ben and Sarah
all in a tuple. Perfect. That is
how we can go on ahead and output the
values of a tuple. Now, let's say we want to
get a particular value. It's going to be very
similar to a list. What we're going to do is we're
going to say print names, but we're going to also add in a square parens here to
refer to the position. I want to output Sara. I'm going to say zero, one, two. I'm going to put two here
in my square brackets. Chose my print function, and it should now output SRA. So if I scroll down and say run, I can now see it outputs Sara. Perfect. So that is how we
can also get a value as well. So if we wanted to get a value, we can go ahead and do the logic of adding in
our square brackets next to our variable
names and then just inserting the value
as it should be. Now, something that I do want to mention is that tuples
are unchangeable. So if we were to try
and change a value, you're going to see the
result of what's to come. So let me show you
all about that. Let's say I want
to change a value. Let's say I want to
go ahead and change, for example, the name Ben
here to, let's say, Luke. So what we would do or what you'd think we would
do is we would say names. Look at the position.
Ben is in position one. And then what you want to do
is you want to say equals, and we're going to go
on ahead and say Look. Then you want to
say print names, and let's take a look at
what's going to happen. Scroll down, say run. And there we go, we can
see we have an error here. So type error tuple object does not support item
assignment here. So this is the part of Tupols here where you need to understand that
they are unchangeable. So you cannot change
a particular value. For example, as we can see here, they are strict as they are, as they are created.
So you cannot do this. So no matter for what value you have here, you
cannot change them. Unlike with a list, you have the opportunity to change them, but with tupels you cannot
go on ahead and change them. Let's go ahead and get
the lengths of a tube. What we can do is we can of
course remove this line. And of course here
by princh names, what we can do is we can
just encapsulate that in a Lang function. We're
going to get the links. So we have that in place. Now let's go ahead and run our program. And
then we get three. Okay? So we can see we have three items or three
values, one, two, three. So the length
function is going to work perfectly fine with a tube. All right, so that's
how we can go on ahead and integrate
that into place. Right. Now, the last
thing that I want to show you is how you
can delete your tuple. So what we can do is we
can remove the following, and we can just simply go
ahead and add in Dell, and then you want to
refer to your tupel, we're going to say names, and
then you want to see print, and in prenss you want
to type in names. Now this is going to result in an error if it is
correct because that means that the names
tuple no longer exists, so we can scroll down
and we can say run. Now we should see an error that says names is not defined. This is a bit of a short
and sweet indication to us that our names list our names
tuple, no longer exists. Perfect. That's how we can go on ahead and utilize tuples.
30. Sets: Hi, everyone, and welcome
to the next lesson, which is going to
be focused on sets. So let's go on ahead
and get started. So the question on our minds, what is a set? So a set is an unordered, unindex collection that does
not allow duplicate values. So that's a very important
key point here with sets. Unlike tuples and lists, a set does not allow
duplicate values. Now it is also represented
in a different way. A set is represented
by curly braces, unlike a list which
is represented by square brackets and
a tuple by parentheses, a set is represented
by curly braces. Now, let's take a look at a short coding snippet here just to get a bit of an
understanding of the set. Here, for example,
I have a variable defined for name set. As you can see here,
it's very similar to our list and our tuple
in terms of formatting. The only difference
here, of course, is that a set is represented by curly braces as we can see
here in this coding snippet. Now, of course, this is, of course, a data
type as well, a set, we have learned the
three data types, a list, a tuple, and a set. Those were the three
additional datatypes that we were going to reflect on and go into
greater detail later, and of course, that
time has come. All right guys.
That's all on sets. We will dive deeper into a lab exercise just to
solidify our knowledge.
31. Create a set and output it's values - [Lab]: Hi, everyone, and welcome
to the next lab exercise, which is going to be
focused on working with sets. Let's get started. First of all, we need
to define our set. In this case, I want
it to be a sport set. I'm going to say sports as
my variable and I'm going to add in some sporty and curly
braces because of course, remember, a set is
denoted by curly braces. First of all, I'm
going to say football. And then I'm going to say golf. Then I'm going to
also add in rugby. And cricket. So we've got some values
assigned to our sports set. Now, let's say we want to
keep it very simple and just output the
values from our set. So we can simply say print
and with our print function, we can refer to our sports set, and then we can, of course, scroll down, say run, and that's going to
output all of our values. As you can see here,
we have cricket, rugby, football,
and we have a golf. All right. So we can see
it outputs everything. Now, something to
note here is that it printed it out in a different order than
you would expect. So not football, golf,
rugby and cricket. I printed everything
out from cricket, rugby, football, and golf. So it went ahead and set up the order in
a different fashion. Let's go ahead and run
the program again. And you're going to see it
outputs in a different format, again, so football,
rugby, golf, and cricket. So the output here is going
to be in a different order. Okay? So it's not going
to be in the same order, it's going to be in
an unordered fashion. So one of the things
to keep in mind when working with a set. Perfect. That's how
we can go on ahead and output the
values from our set. Now, the next thing that
we want to do is we want to check if a particular
value is in a set or not, and it's going to
return a bull response, either true or false,
if it is or is not. So what I'm going to do is
I'm going to say print, and then I'm going to look for a particular sport
that's in the sport set. So let me look for, let's say, tennis in, and then we want
to say sports, like such. So it's going to
check if the value of tennis is in the sports set. So we can see football, golf, rugby and cricket, but tennis is not
in it, as we know, but let's see the
bull response here. So if we were to scroll down
and run this in our console, we can see false and
rightfully so because tennis is not in our sports set. However, let's switch
this up to Golf. So we know we have golf
in our sports set. Let's see if golf
exists in sports. So let's scroll
down and run this, and we can see true. Now, please keep in mind here, of course, that it
is also K sensitive. So if you were to make it or caps or
anything of the like, it's not going to return the correct result
as you desire. So do keep that in mind, but golf exists in the sports set. So this is how we can check
if a value is in a set. You would need to just refer to the value and then
of course, say in. Right. Now, the next
thing that we want to do is we want to add to a set. So what we need to do is the following remove
this print statement, and we want to refer
to our variable. So sports, we're going to utilize the add message that we're going
to say don't add, and we can utilize
our pareneses. And here, we're going
to add a new sport. So let's say now we want to add in tennis so now
we can say tennis, and then you just want
to say print and sports. So now it's going to add
in the tennis value to our set of values with football, golf, rugby, and cricket,
we'll now have tennis. Now if we were to run our code, we can now see we have rugby, football, golf,
cricket, and tennis. So tennis has in fact
been added to our set. So you need to use
the add method to add a particular
value to your set. Now, you're probably
wrong, Okay, I can add a particular
value to a set. How do I remove a value from
a set? So, good question. So to do so, you would refer again to the
variable sports, and you would say sports Dot, and you're going to want to
utilize the remove messed. So you want to remove
something from your set. So here, I want to remove
football, for example, so that when I print sports, I can only see golf, rugby and cricket was football removed. So let's scroll down and run our code and see
if that does the job. So here we can see we have
rugby, cricket, and golf. So we can see that
the code worked. So it removed the football
value from our set. So to do so, remember to utilize the dot remove
mesod and that's going to remove a particular
value from your set. Okay. Okay, so the next
thing I want to show you is how you can clear all
the values from your set. So how you can just make
it empty. So an empty set. Very similar to
delete, you could say just a little bit
different in how it works. So what I want us to do to observe this is to
do the following. First, I want you to print out all of the
values in your set. So we're going to say print
and then we'll say sports. So with coding, it's going to happen in a linear
approach here, so with line execution. So first, we want
to print the total, not the total, all of
the values in our set. Then what I want us to do is to clear all of the
values in our set. So then I want us to say sports, and we want to use
a clear method to remove all of the
values in our set. And then I want us to
say print again and then to say sports
so we can print everything that is in our set. Okay, so we should see was
the first print statement. It should show all
of our values, then it should clear it, and then it should show an empty set once we've
printed it because the previous statement
cleared all the values that existed in our set for
our sport variable. Let's go ahead and
run this code, so we should see the flowing. There we go, rugby,
football, golf, and cricket, and then we have an empty set, which is the second line here
that prints everything out. The Dot clear method is
going to essentially remove all the values from
a given set that you have gone ahead and
assigned it to. So that is how you can go ahead and integrate that into place. Right. So the last
thing I want to show is how we can delete a set, so it's going to be very
similar to a tuple. So we can remove the following, and we can just say Dow sports. Then you want to say print, and you want to say spots. So we should get
an error message to show that this sport set no longer exists to ensure that
all the values are gone. So let's go ahead and
test this out now. So we can go ahead and say run, and we should see an error here. Name sports is not
defined perfect. So that indicates
to us that it no longer exists since we have
deleted it accordingly. Guys, that is it for this lab exercise on
working with sets in PySon.
32. Dictionaries: Hi, everyone, and welcome
to the next lesson, which will be focused
on dictionaries. So let's get started. So what is a dictionary? Simply put, a dictionary is
an unordered collection of key value pairs where each key is unique and maps
to a specific value. So the important part of note here that I
specifically want you to focus on is key value pairs. So remember that
key value pairs. Now it is represented
by curly braces. And just to give you a bit of a coding snippet example,
as you can see here, we have a dictionary, and we can see we have name on age 26, nationalities
of Africa. So this is seen as
our key value Paris, but we will look deeper
into it to look at the composition of a dictionary. So let's go ahead
and take a look. So the composition
of a dictionary. So here we have our
dictionary maximize that we just saw earlier, and let's take a
deeper look here. So our dictionary is
composed of key value pairs. An example here is name and RO. So the key would be name
and the value would be RO. Next, we would have age, which will be the key, and
the value would be 26. Nationality would be the key, and the value would
be South Africa. So this is how you
can distinguish key value pairs in a dictionary, and this is the big part that
forms a dictionary itself. A. That's it for the
theoretical side. We will then next go
on ahead and dive into the practical side
of the lab exercises on working with
dictionaries in Bison.
33. Working with Dictionaries - [Lab]: Hi, everybody, and welcome
to the next lab exercise, which will be focused on working with
dictionaries in pyson. So let's get started. Now, the first thing
that we want to do is we want to create
our dictionary. So we'll set up our variable, and we're going to set
it up as person equals, and then remember to
set up a dictionary. We need to use curly braces. So here's an opening
curly brace, and a closing one
should be there. If it hasn't automatically
been created for you, you can just add it
there right there. So now we want to create
our key value pairs. I'd recommend you
add in some space and keep it a bit adjusted. What we want to do is we want to define our key value pairs. First of all, I want
to have first name as my first key and then
I'll assign a value to it. The first key is going
to be first name. Then you want to add in
a colon and a space, and then you want to add
the value next to that. I'm going to say Jake. You want to add in a comma
for your next key value pair. This is one key value pair. First name is the key and its corresponding value that
it's mapped to is Jake. We can press Enter and we
can go to the next line. Then we want to add in last name colon here
I'm going to say, let's say, Jake do. You want to add in a comma. That's our second
key value pair done. Another key value pair, we're going to have let's say age you can just click outside and then add in your
comma and then space here, we're just going to
add it as an int. Here I'm going to say 24. Then you want to add in a comma. Press Enter, go
to the next line. Then we'll have nationality. That will be our final key
paraphernal outside of that, add in a colon. Then you want to add
in the value here. Here I'm going to
go ahead and set it as Germany.
Right, there we go. And now, what I want to do is
I just want to remove some of the space and
we are good to go. So this is our first dictionary, and we have one, two, three, four key value
pairs in place. Now, let's say we want to
output this dictionary with all of its key
value pairs for person. So what you want to do is
you just want to underneath, go ahead and say print and then refer to the
name of the dictionary, which is going to be person. So we can just say person. Then we can go ahead and say
run, and in our console, you will then see our our
dictionary keep value pairs. So first name Jake,
last name Doe, age 24, Nationality, Germany. And that is all
incorporated here in this particular dictionary that we have setup for a person. So that's how you can
output all the values. Now, let's say, for example, you want to go on ahead and get a particular value or item. Let's say you want
to get the value of the first name and
the value of age. What you're going
to want to do is you want to keep it
at print person, but then right next to person, you want to add in your
how can I say it your square brackets to refer to the particular key of the
value you want to output. I want to get Jake. What I'll do is
I'll say person and then in square brackets here, we can refer to that
particular key, which is going to be first name. Here we can put in quotes. First name. That will output
Jake in our console. Let's say I also
want to get the age. What I'm going to do
is we can say print, refer to the dictionary, which is going to be person, and then here in
square brackets, you want to refer to age. So it needs to be identical because it is going
to be as sensitive. Now it should output in
our console Jake and 24. Now if I scroll down
and run my console, and I'm going to
see Jake and 24. That is how you can
go on a hedge and get a particular value or item. Now the next thing that we
can do is we can remove a particular value or
an entire key pair, you could say, because
if you go on a head and remove the key pair it
removes the value as well. So what we can do is we
can remove this for now. And you want to go
ahead and utilize Del, the keyword Del,
and then here in parenses you want
to refer to person. That's what we have assigned
as our dictionary name here. And then in square brackets, you want to refer to the key
value pair that you want to delete and you're going to want to set it according
to the key itself. If I just wanted to
show my first name, my age, and my nationality, when I call this dictionary, I need to remove according
to the key of last name, which is going to
remove last name and the value associated
to it, which is do. So here I can just
put in last name. And if I were to go on a hedge and add in my print statement, if I were to say print person, you're then going to see
it's going to output everything except for
the last name now. So we're going to delete the last name and
that will be removed. So if I say run,
you can see here at outputs first name Jake age
24, nationality, Germany. So we have utilized a keyword to remove the last name
out of this dictionary. Since W we printed person, it skipped over last name and printed first name,
age, and nationality. So that's how you
can remove a value from your dictionary or key
value pair, you could say. Let's say we want to get the
links of our dictionary. What you can do
is you can remove this and we can just say print and we want to
incorporate the link function, so in, and then you
can put in person. This is going to go on ahead and give us the
lengths of our dictionary. If we were to say run,
it's going to say four. The reason for that, of
course, is because we have, as we can see here,
four key value pairs. One, two, three, four. That's the reason why
it's showing four. If we were to have
another key value pair, if I were to add one more
here and I'm going to say p and then here I'm
going to put BMW, and I'm going to keep
that as is and say run, it's now going to show Corus five because we have
five key value pairs, one, two, three, four, five. Okay, so that's how we can get the lengths of
our dictionary. Now, there's a lot of
things that we can do. Next thing I want to show you
is how you can get all of the dictionary keys,
all of your keys. In this case, first name, last name, age,
nationality and car. What we can do is we can
go ahead and remove this and we can start with print and the opening parenses and you can refer to
your dictionaries. You can say person dot keys, and then opening
and closing parenes and close it right there. You're going to use the
dot keys method here, and that's going to list out all of the keys in
your dictionary. First name, last name, age, nationality, and car only. Now if I were to scroll
down and run this program, you can see here it's going
to output my dictionary keys. First name, last name, age, nationality, and car, that's all going to be
output by utilizing the dot keys method that
we have incorporated here. Perfect. That's how
you can go on ahead and ensure that you're
able to do just that. Now, what we can also go on
ahead and do is we can also decide to get all of the
dictionary items, so everything. To do so, what you can
do is you can go ahead and replace dot keys
with dot items. We will utilize the
dot items method here to get everything, and then you can say run that's going to get everything for
you in a specific format. First name Jake, last name Doe, age 24, national AD,
Germany, car BMW. That's so you can
get everything in a more structured
form, you could say. Right. We've got that
setup and interplay. That is a few things
that you can go ahead and do with dictionaries. Now, the next thing
that we want to do is we want to
learn how we can add a particular key and value to what we
have set up here. We don't want to
manually add it, we want to automatically
add it with our code. What we can do is
we can of course, remove the following line. What I want to do just to
make it a bit more uh, easy to read, so
easier on their eyes. What I want to do
is I just want to change the style that
I have set up here. I just want to remove some of that extra white
space just to make it a bit more concise and clear. What we're going to want
to do if we want to add a particular key value
pair to what we have here is we're going to have to first of all, refer
to our dictionary. Person, and say person
and here we want to open our square brackets and we want to add in the new
key that we want to add. Here I'm going to say, um, Field of study. Then I'm going to say equals, and the field of study here, I'm going to say law. Then what I want to do is I
want to say print person. I want to see what it's
going to output now. I've added in the
key value pair now that will appear here
in our dictionary. So if I were to say run, you can see now that
it's appendage that new key value parall
to our dictionary. So first name Jake,
last name Doe, H 24, Nationality Germany, CarbMW
and field of study law. Because what we did here
was we went ahead and added in the new key and
assigned the value to law, and then we just output what is part of our
dictionary and it has included that latest
change that we have added into place
that we have there. Now, another thing
that we can actually do is we can go on ahead and copy a
dictionary. Okay. So let me show you how you
can go on ahead and do that. So we can remove what we
have here and we can go ahead and set everything
up as follows. We can go ahead and
copy our dictionary. We can say second person equals, and we're going to
copy everything in the dictionary that
we have set up here. Here we have person. And we can say dodge copy. We'll be using the dot copy
method and we're going to copy our dictionary
to second person. What we're also going to do
is we're going to add in an extra key value pair
to the second person. We can then say second person. And here we want to add
in a square bracket, and we're going to
add in university as a new key, and the value here, I'm going to go
ahead and set up as the university of Capetown, then I want to say
print second person. Now if I were to run this, we can now see what is
happening over here. So now it's printing our
second person variable. So this is our new
dictionary here, and this dictionary,
second person, of course, is encapsulating all of the key value pairs that
we have here from person, and you can see here
it's outputting all those key value pairs. So now it's also
included University, which is the University
of Cape Town. Now you'll notice if I
were to say print person, it's only going to show
the following here. So I below, I say print person, and run that, it's not going to include the university part. Here it only goes up to the BMW. You can see here we of course
have two dictionaries. Now, to confirm that second
person is now a dictionary, you can just simply use the type function
and just encapsulate second person and you can just remove this line for
now and run the code. And we can see here
it is a dictionary. So we declared a variable
known as second person, and it became a dictionary
when we copied over the key value pair structure
and the dictionary of person to second person. So that is how we
can essentially create another dictionary by
copying its contents over and setting up a new
dictionary and then adding in a new key value pair to this new dictionary that we
have essentially created. All right. So that's how we can go ahead and
copy a diction, also just migrate and move some data that
we actually have. Right, so we can just
remove this now. Perfect. Now, let's say we
want to clear our dictionary. We want to empty it. So
that will be very simple. You can just go on
ahead and say person, and you're going to
use a clear method to clear up your dictionary, and you can just
simply say print, and you want to refer to
your dictionary, so person. Now we can just say run, and it should show an empty
dictionary, and there we go. We've got nothing in it.
So the dot clear method will allow you to clear
up your dictionary. Okay, so that's how we
can clear a dictionary. Now let's say we want to
go a little bit further and we want to create a
list of dictionaries. We can create a whole
list of dictionaries. What I'm going to do here is I'm going to remove
everything we have here, and we want to create a
list of dictionaries. You'll have multiple
dictionaries in a list. I'm going to go ahead and
set up a list, first of all, it's going to be people equals, and remember a list you will
create with square brackets. I'm going to add in some
space here and just just it. All right, and I'm going to go ahead and add in some
dictionaries now. So first of all, I'm going
to open my curly brace, and I'm going to actually
close it at the end as well. So let's add in our
key value pair. So this is our first dictionary. So we're going to
have first name, colon, and here we'll say, Kate. And then we'll
have our next key. Then we'll have last name, colon, value, we'll
have Johnson, add in our next key, which is going to be age,
colon, we'll have 28, and then we'll have favorite
sports favor sport, colon and then here
we add in the value, we're going to say golf. There we go. Then you
need to add in a comma. What we're going to do to make
this easier is we're going to go ahead and copy this here. This is going to
be one dictionary. Let's go ahead and base this in that will be
our second dictionary. I'll just change the value. Here I'll just say Jane
and we'll say Jane Linus, age, 24, favorite sport. We'll say tennis, and we'll create one more
dictionary. There we go. And here we will have
Luke Jacobs G six, and we'll say rugby. At the end here, you can remove this last comment,
you don't need that. So these are our
three dictionaries that are part of
our list, people. So that's how you can create
a list of dictionaries. Let's say we want to go ahead
and we actually want to query this list of
dictionaries and to get a particular
value per se. So let's say for argument's
sake that I want to go on ahead and I
want to get the value. The value I want to get is golf. I want to get this
particular value and I want to ensure that I'm
able to output that. Okay. So what I would
want to do is I'd want to go ahead and
do the following, and that is to say print and a moment to refer to my list here,
which is going to be people. So I'm going to say
people, and then I'm going to add in a square bracket
to refer to the position. Okay. So the
position here, okay, I'm going to treat
my dictionaries here as the first in the list, so zero, one, two. So golf with person one, which means zero
in the positioning because remember with a list, the first value or item
is going to be zero. So here we'll have zero. And then what I want
to do is I want to refer to that particular
person that is, of course, playing golf. All right. So now we want to do is we want to
refer to the key. Since we want to
get the value golf, we need to refer to the key, the corresponding key,
which is favorite sport. Here we can go ahead and
add in favorite sport. Now if we were to say
run, it outputs golf. Perfect. Now, let's say
we want to go ahead and obtain the age value of
the third person here. So this will be the person
who has the age of 26. So let's go ahead
and refer to it. So let's get our print function. So the person, so
zero, one, two. So that's going to
be according to the position in the list,
so we can see people. And in square brackets two, and then we need to
refer to the age key, which is going to give us 26
as the value as the output. Then we can say run and there we see we have golf
and 26 output for us. Perfect. That is how you can go on a hedge and work
with a list of dictionaries and
how you can also manipulate it to
obtain the value of particular values here in your dictionary by referring
to that particular key. That's it for this
coding exercise.
34. Functions: Hi, everyone, and welcome to the next lesson which will
be focused on functions. So let's take a
look. The question on our minds is
what is a function? A function is simply
a block of code that executes only
when it is coded. Here is a coding snippet to
just get your head around it. We define a function by using death and then we refer to
our function name which we can customize
and choose such as my function and
opening and closing parentheses and then a
codon at the end and nested within that particular function is where we enter in our code. Now, there's a lot more to it, such as parameters, arguments, et cetera, because technically, here by the parentheses, you can also add in various parameters to work
with your function at hand. Right, so there is a
lot to it, like I said, but we'll get really deeper into it as we work on the
lab exercises together. But I just wanted to quickly
introduce you to a function, just to familiarize yourselves
with it a little bit, and we'll get to it in
the next lab exercise.
35. Define and call a function - [Lab]: Hi, everyone, and welcome to the next practical ab exercise, which will be focused
on working with functions in PySon.
Let's get started. As you can see here,
I've already gone ahead and added
into comments here. I'm going to explain and utilize comments
here quite a lot in this particular lesson because there is quite a lot to
it, so do bear with me. First of all, we want to learn
how to define a function. So to do so, you would need to start
off with the keyword of, DEF, and that's going to be the starting point for defining all of your functions
in Pierson. The next thing you
need to do is you need to name your function. Now, this can be anything. It's really custom and
depending on what you feel. Now, I'd recommend that you
choose a function name that is going to be relevant to the program you're creating
with your function. In this case, I want to create a very simple greeting program. My function name is
going to be my 310. Then I want to add
in parentheses, opening and closing
pareneses and you want to add on a
colon at the end. That's the basis for
defining a function. Then you want to
add in some code. So we can press Enter and
automatically will nest for us. The code you're going
to see here is going to be not on the same line as def, but it's going to be more
in the middle a little bit. That will be automatically
nested code for us. Then what you want to do is
you want to actually add in what you want this
function to actually do. We just want it to
print out good morning, so we can use our
print function and say, good morning, or. Perfect. So we've
defined a function. Now the next thing that
we want to do is we want to actually
call this function. Now, to do so, you
need to refer to the function name and of course, the parentheses right here. So to call it, you would
need to say my greeting. So what I'm going to do is I'm going to call the function, but before we do that, I just
want to show you something. Now, even though you have
your print function in here, if you were to go ahead
and run your code, going to see nothing happens because we haven't
called our function. Now, everything
that is defined to run with this function
has been stated, we want this print
statement to be executed, but it won't run until we've
called the function itself. So to do so, you need to refer to the function name
and you need to say my gaeting and then opening and closing parentheses
as you have set up here. So now we're going to call that function
and it's going to execute this function and all of the code within
it that's been nested. So now if I say run again, it's going to say good morning. As you can see there,
it's done just that, it's going ahead and it's run whatever code was in this
function that you've defined. Perfect. That's how we can go ahead and define and
call a function. We're going to go ahead and
do another example here and I'm going to go down here and I'm just
going to say depth. I want to define my
function and here I'm going to say my sport, opening and closing parencs at the colon and
underneath I want to say print in quotes, I'm going to say I
played football today. Then all you need to do is you need to call your function, it's going to be my sport,
opening and closing pen. You can say my sport, and then it will execute
that line for you as well. It's going to execute
what we'd had before first and then it's going
to execute this function. Now if I say run, it's going
to execute both of them. It's going to say good morning all I played football today. That is how we can
define and how we can call our functions in PySon. We're going to go a little bit deeper and we're
going to look into parameters and we're going
to look at arguments. What I'm going to do
now is we can just remove this extra
function that we created, and we've learned now how to define our function
and how to call it. But the next thing I
want to learn is how we can add in parameters. So to add in a parameter, you need to add
it in here, here, I'm going to go on
a hedge and I'm going to set it as name, and I'm just going to add in a comment here just
to assist you. And what I want you to
visualize is the following. That is to treat
parameters as variables. So try and see your
parameters as variables. So here we have a
parameter called name, and that you could see a
variable as an example. Then what you can do here, when you're calling
your function, you want to add in an argument. So here, for example,
we can put in Ono. And what I would like
you to do here is to treat arguments as values. That's the simplest way
you can go ahead about it. Essentially, what's
going to happen is whatever you put in here
is your argument and that's going to be
sent here where your name is and it's
going to be treated as, of course, the value that's been assigned
to set variable. Everything here
within the paress of your function will
be known as parameters, and anything that
you enter in here within the function that you're calling is known as an argument. Okay, so what we
can do now is we can go ahead and go a bit
further with our code, and we can just go ahead and do some string concatenation. So add in a space here, a plus, and then I'm
going to say name. So now what's going to happen is it's going to
say good morning all, and it's going to say plus no. But to make it a
bit more specific, let's actually go ahead
and say good morning, add in a comma there and just to say good morning to
make it a bit cleaner. So now I want to run my code. You're going to say,
good morning, no. Let's take a look
at the process. We defined our
function my greeting, which takes in a
parameter called name. Name is then passed through here to this
print function that we are using with
string concatenation and we are then
executing, of course, our function called no, which is going to run this program and it's going
to take the value no, throw it in as a
placeholder for name, and then it's going to correlate here as a value of
name which is no. Okay, so essentially you
could also treat name here as a placeholder for your values
here that you're inserting. Try to envision this
now as no and no. This argument here
we are putting in place of name and it's just being taken down into the code here where
we can execute it. That's how you can define
and call a function by utilizing parameters and also how you can also do
it with the process of utilizing arguments here. Let's go a little bit deeper and let's make this a
bit more interesting. So what I'm going to
do is I'm going to remove the following
here that we have set, and let's go ahead and
rebuild the wheel. So we're going to say
death, and let's create another function
called get total. And here we're going to
have two parameters. We'll have number one, and then we'll have number two, and then we have our colon. And then what we want
to do is we want to go ahead and define
a variable here, where we add the two numbers that we're going to say total equals number one
plus number two. Then we want to
return the total. So we're going to
say return, total. So that's going to return the result of what we have here. Then the next thing that
we're going to want to do is we're
going to want to go on ahead and call that particular function
that we have here in place. Now, what I would
recommend you do is you can go ahead and
create a variable even, and we're going to go ahead
and set it as follows. And we're going to say,
let's go for final result. We're going to say equals,
and then we're going to add that function
to our variable. So here's the
function, get total. And remember, we put
in our arguments. So here we can put
in ten and five. This is important
with the place. Ten is going to be
the placeholder for number one and five will be a number two because the place
matches and the position. So ten is going to go here,
five is going to go there. Then we have a variable inside this function that's going
to calculate the total. Number one will then be ten
and number two will be five. The total then will be 15. Then we want to
return the total, which is essentially
going to be 15. That's the result that
we want to return. And what we're doing down here is we're going to be
calling that function. We're going to say final
result is going to be equal to get total where we
pass through our arguments. Then at the end, what we
want to do is we just want to utilize the print
function outside. You'll notice here that we're just saying returning total. We're going to be
returning the total, but we're not going
to be printing it. To output this, we
need to say print. Then we can just simply go
ahead and say final result. Now if we were to say run, we will get 15 there at the end. Okay, that's how we
can go a little bit further and we can work with more parameters, more arguments, and we can return the result of arithmetic operators
and how we can output them right
there in the end. All right. So that's how we
can go ahead and do that. Essentially, what's
happening here, the total is going
to be returned. And what we're going
to be doing then is we're going to
have that value stored here in the
variable final result based on what we
have performed here. So this particular function
is going to be called, that total is going
to be returned and it's going to be stored in
final result where we printed. All right. So that's it on understanding the
basics of functions. We will go a little bit deeper, but that's just a bit
of an introduction into it just to give you
some experience.
36. Local vs Global scope: Hi, everyone, and welcome
to the next lesson, which is going to be focused on local versus global scope. So let's take a look. Right, local versus
global scope. Now, a variable that is
declared locally can only be accessed within
the local scope. In comparison, a variable
that is declared globally can be accessed
from any scope. We're going to be working with local scope and global scope in terms of our variables
that are declared, right. Let's go on ahead and
prepare ourselves for the lab exercise on local
scope and global scope.
37. Declaring at the local and global scopes - [Lab]: Hi, everyone, and welcome to the next lab exercise
which is going to be focused on local
versus global scope. So let's get started. Now, the first thing
that we want to do is just a quick refresher
into functions. Let's define our function.
So we'll say Def. Here again, we're going
to say my greeting. Then we're going to go ahead
and add in our parenths. Then we're going to
add in a parameter, which is name, add in the colon. Then we can just say print, and we can actually
make that higher up, and then we can just go ahead
and say good after noon. Add in the comma and
the space there, then we can add in plus name. Perfect. And then we want
to call our function so we can say M greeting and
opening and closing parencs. And just remember we need
to add in those pareness the actual value here to
be assigned as follows. So we're going to say no. Great. Now if we were
to run our code, we'll see, good
morning, H. Good. We've got that in
place. Perfect. That is how we can define
and call function. Now, let's work on
local and global scope. Let's say for argument's
sake that we don't work with our parameter and we
don't work with our argument. Let me keep it very simple. And we do need name here
to be defined somewhere. So an example of local
scope is as follows. Let's say you define a variable. In your function. Here I'm going to go ahead
and say name equals, and then I'm going to
say Arno just like that. Let's say I want
to run this code, it's also going to say
good afternoon no, but let's go ahead and run that. Here it says good afternoon no. This name here is referring to our variable and we've
assigned that value arno. Let's try and see if we can actually print
this name outside. So by default, it's going
to execute the function, but let's say I want it
to print out my name. So I'm going to say print name. So let's see if we
can print this out. So it's going to run
the function that says, Good afternoon Arno, but let's see if it
prints out the name. Let's run this. There we go. I prints out good
afternoon Arno. But it has an error here saying
that name is not defined. The reason for this is because our variable is declared
at a local scope, which means that it can only be accessible to our function
within our function itself. We're trying to call it from outside of said function.
This is not allowed. We are going past the rule of passing by local scope rules. And this is the reason
why we're getting that error and it
cannot be executed. Now, let's see if we can execute our program at a global scope
level. So let's do that. So what you can do, for example, is you can go ahead and
remove this variable here, and you can define
before your function, you can go ahead and
say name equals no. So now we want to see
if we will be able to grab that name variable here
and if we can also print it. So let's go ahead and say run. And there it works. Good
afternoon no and no. It prints out my
name here and it also runs this function. It calls this function in place. Now, you can see the
difference here. We declared our variable above, so we could see that this is
working on a global scope. We're able to axis
this variable and it's value from anywhere
in our program, and we have defined
it right above it. Remember, there is a difference with local scope
and global scope. Local scope as an example, can be a variable that's defined at the function
level and you'll only be able to utilize axis that variable and its
value in your function. Here we defined a variable
outside of our function right here before any of the following code
was executed and ran, and this is an example
of global scope, meaning we can access
this variable in a function and we can access anywhere else
in our program. So there's the
differences between local scope and a
global scope in PySon.
38. Comparison and logical operators: Hi, Ron, and welcome to
the next lesson which will be focused on comparison
and logical operators. So let's go ahead
and get started. Now, first of all, we want to look at comparison operators. I've compiled this
table here to make it very simple for you to get
the general gist of it. So on the left hand side, we can see our
comparison operators and their purpose on
the right hand side. We have our double equal
sine which means equal to. We have our exclamation mark
symbol and the equals sign, which means not equal
to, greater than symbol, our less than symbol are greater
than or equal to symbol, and our less than equal to symbol as well or operator, you could
refer to them as. And that means less
than or equal to. So these are various
comparison operators that we can utilize in
pyson with our logic. So especially useful in our if conditionals and
also with loops as well, and in many other cases where
we need to use a operator, especially a
comparison operator. Now we don't just have
comparison operators. We also have logical operators as well. So let's
take a look at that. Now, the logical
operators that we have include and or and not. Now let's look at the
purpose of each of them. So the and logical
operator returns true if both
statements are true. We then have an all logical
operator which returns true if one of the
statements are true. So let's say, for example, we have two statements. One of them is false and
the other one is true. That means that we're going to get a true result in the end. Okay? Now, I know this may go on ahead and go
over your head a bit, but don't worry, we will
go into the details of it, and it will make
sense, I promise you. We then have a not
operator as well, and this reverses the result. So it returns true if the
statement is true and returns true if the
statement is false. So it's exactly an operator that deals in opposites
and reversing. Right, so these are two sets
of operators that we get. So comparison and
logical operators, that is one of the two sets that we have here
in place for Pison.
39. IF conditionals: Hi, everyone, and welcome
to the next lesson, which will be focused
on I conditionals. So let's get started. Right, so if conditionals. Now, we get, of
course, IL statements, and I L statements, decide actions to take based
on specific conditions. Now we also get I
Else statements. And these if ELFL
statements allow you to select actions depending on
a variety of conditions. Now, I know this might be
a bit confusing at first, but to help you absorb this information and
to contextualize it, I'm going to give
you a few analogies along with coding snippets
to make this a bit clearer. So let's start off very simply. Right, so let's look at
a Is condition analogy. So traffic lights. So, of course, with
traffic lights, we have our car, and our car is going to stop at a traffic
light when it is red, and when it is green, the car will continue driving. Now, let's take a
look how this can be placed into context
with IL conditions. So we have our car and we have the traffic light above and
now let's put this into code. For example, what you could
do to create a simple Is, you could say program, you can define a variable called traffic light and we can
set that value to green. Then we can set up
an Is conditional. We're going to say
if the traffic light is equals to green, then we want to
print, you may drive. Else, we want to
print, you must wait. Here is a very simple
way in which we can use an Is condition and apply it to a traffic light situation. Take a look at another one.
So analogy to student grades. So let's say we have a
student that, of course, is studying for an
exam and they get their grades back or
anything of the like. Let's go ahead and formulate
this into a program. So let's take a look
at a coding snippet based on this use case. So let's say that we
have a variable which is student grade and that
student's grade is 83. Now we can use an
Ile conditional to check if that
student's grade, if the student grade is
greater than or equal to 50, then it's going to print past, else it's going to print
failed in the console. All right. Let's have a
look at another analogy. So hopefully by now, this is making more sense
as we go through a few use cases
and have a look at some coding snippets for a program you can create
based on that situation. So let's go ahead and
look at another one. So this one now is
going to focus on the I if else conditions. So election votes. Let's say we have someone
voting and we were to put this into a context
of a PySon program. We can go ahead and create a program that takes
in the voters age, the variable will be voter age, and the value will be 17. We can then go ahead and set
up our E if conditionals. We can start off by saying
if the voter age is greater, this is 17, we can
output, you may vote. E, we can check if
the voter age is equals to 18 and then we
can print, you can vote. Else, we can print, you are too young to vote. In this case, the
voter's age is 17. So if voter age
greater than 17 isn't going to run and the print
statement won't be working. The ELIFs statement here, ELIF voter age is equals to
18 will output you can vote. However, the voter's age is 17, not equals to 18 and
not greater than 17. That means it's going to print
out you are too young to vote in the else statement
that we have set up here. So as you can see, for the
statement that is true, it's going to execute that
particular print statement. And in this case, the only um, statement that's true is based
on the statement because the voter isn't
older than 17 or 18, so they're going to get the message you are
too young to vote. All right. Those are a few
analogies just to hopefully make it easier to understand the concept of if conditionals,
but not to worry, we are going to do an exercise
just to become comfortable with utilizing if and
L conditions in Hisen.
40. Creating a simple If-Elif-Else logic - [Lab]: Hi, everyone, and welcome to
the practical lab exercises. So in this particular exercise, we're going to focus
more on conditionals. Specificly with IL statements and if EFL statements as well. Let's go ahead and get started with a simple IL statement. Right. So first of all, we want to define our variable
that we want to evaluate. So we're going to
say student grade, and the value here
that we're going to associate it with will be 45. Then what we want
to do is we want to start off our if statement. So we're going to say,
if the student grade is greater than equal to 50, then we need to
add in our colon, we want to execute
the following code, which in this case is going
to be a print statement, which of course is going
to have asked. Was in it. So we want to print past if that particular student grade
is greater than equal to 50 if for some reason
it isn't, okay? We can then utilize
an else statement. So we can say else,
curl on, and enter, and then we can
then just say print sorry, you have failed. Okay, so we can just go ahead
and add it in like such. Now, do keep in mind when
you press Enter here, it's going to add in a space for you here to nest in
the code that you want to execute if
that particular If statement requirement
has been reached. Okay. Do keep that in mind here. It will go ahead and
nest that space for you. If for some reason it doesn't, you can just go
ahead and press tab, and it's just going
to move out for you and structure your
code accordingly. So according to what
we have set up here, student grade equals 45. If a student grade is
greater than or equal to 50, we're going to print pass else print sorry you have failed. In this case here, the first
conditional here fails, it's going to skip this
and the code, of course, that comes along
with it and skip straight to the
statement where it's going to print, sorry
you have failed. Let's go ahead and run
our code and see it. Sorry, you have failed. So let's go ahead
and change this up. Let's switch this up to 51. Now the first statement,
we're going to evaluate if student grade is
greater than equal to 50, we have 51 as the student grade, and that's going to
then print r. So the thing with our if statements
here are IL statements. So essentially, speaking,
once it reaches the first conditional
that is, of course, true, it's then going to print
out the code that is part of that conditional and then it's going
to stop the program. So it's not going to continue to anything else
because it wants to evaluate to what's true and the first statement that's true. So here we're going
to say print pass. So in this case, it
should say pass, so we can then say run and
there we go. It says past. Perfect. So that
is how we can use a simple if else statement. So what I want us to do now is I want us to go a
little bit further and to take a deeper look
into an if ELF statement. So let's go ahead and start. So first, I want to
evaluate colors. So let's say my favorite color, I'm going to say equals, and
here I'm going to say green. So let's start off
I ELF L statement. So first, if my favorite
color is equal, so we're going to use
the double equal sign. And here, we're going
to see if it is equals to, let's say, red. You can add in our plon. We can then go ahead
and print and say your favorite color is red. Perfect. Then what we can do is we can also add
in another statement. So we're going to
say ELIF statement. ELIF my favorite color equals, double equals here, we need to add in, and we're
going to say green. We're going to go
ahead and say print. Your favorite color is green. And if for some reason, we aren't checking
for any other color. So let's say brown or blue, we can see else print, you have a different
favorite color. Right. What we can
do is execute this. Let's just evaluate it first. The variable here is my favorite color and the value is green. First, we're going to
check if that color is red and it's not red, so I'll skip to the next
line, the Elif line here. It says, My favorite color
is green, and it is green. That means it's going to print your favorite color is green, and it's going to execute this line of code and
end the program at that. I can scroll down and
say run and it says, Your favorite color is green. So it went ahead and ran this statement and it was true because the first
statement here was false, and of course, there's
no need to go to the last one if this passed. Let's, however, go ahead
and set this to brown. So now the first I
statement is going to fail, which means our program
goes to the next statement, which is our Eli statement, and that fails as well
because it is green. And then it's then going
to go ahead and go to the statement and print
what is otherwise the case. And in this case,
it's going to print, you have a different
favorite color. So I'm going to
say run there go, it says you have a
different favorite color. So that's how we
can use our I E, and our se statement. Okay, so let's go ahead
and make it a bit more interesting
in this case here. Let's go ahead and work with
our input functions as well. You can remove
this, and let's go ahead and create one
that is your password. So let's say my password. I'm going to set this
to an input function and here I want to prompt users to enter
in their passwords. I'm going to say, please
enter in your password, colon, and we want to
add in space here. Great. Now we want to do
a simple if statement, so we're going to say if
my password is equals, we're going to use the
comparison operator here and we're going to say password one, two, three, we need
to add in a colon, then enter, and now
we can say print. Welcome. In the event that's not the password
that's entered, we can use an Al statement here. As colon, enter, we can see
print, invalid password. Please try again. All right. So here we can see we're
going to be prompted, first of all, to
enter in a password. The value that we enter will be assigned to the variable
here in my password. Then we're going to use
our I statement here to check the password and if it
is equals to password one, two, three, if it is the case, we're
going to print welcome. If it's not the case, we can use our statement to print
invalid password. Please try again. Let's go ahead and evaluate this process. So I'm going to go
ahead and run this. So we enter a password, I'm going to put something
completely different in going to say pass three, four, three pass and Enter. Then it says invalid
password, please try again. So here I put in pass 342 pass, which means that the first
constraint here failed. So it went to the next line
of code that says else, and it printed invalid
password, please try again. Now, let's go ahead and put in the correct password,
which is password one, two, three, so we can re
run this code as it is. And we can say pass
word one, two, three. Let's double check that
here, there we go. And you just want to
then press Enter. And then it says, welcome. So that first if statement here was ran and it was a
success, it was true. So it went ahead and executed that welcome message
from the print function. So that's how we can set
up our IL statements with an input function
as well just to make it a little bit more credible
into the whole process. Okay. Now let's go ahead and make
this a bit more interesting. Let's add in an I ELF and
L statement this time. What I'm going to do
here is I'm going to set the passwords and assume that
there's a specific user. What I'm going to
do is as follows. I'm going to say if my password is equals
to password 123, I want to say welcome,
I'm going to say John. Then what I want to do
is I want to add in an ELF statement here and I'm
going to say my password. Is equals to pass word 321
colon on to say print, and let's assume
that this is no, so I'm going to say welcome no. And we can do the following. So now we can check if
the password is 123. It's going to say,
welcome, John. If the password is 321,
it'll say welcome no. And if it is neither of these, we're just going to
say invalid password. Please try again. So let's go ahead and
run this code here. First, we're going to check
for one of these passwords. I'm going to say pass
word three to one, Enter. Welcome no. What happened was the password was not
password one to three. The ILS statement caught this correct input of
password three to one and it prompted output welcome
no as we can see here. Let's go ahead and
run this again. Okay. And I'm going to say pass word one, two, three, Enter. And then it outputs
welcome John, and that correlates to what we are checking here is
the above your statement. Let's put something that is not password three to
one or password 123. So let's go ahead and
rerun everything. And we're just going
to say pass pass 321. Invalid password,
please try again. All right, so there we have it. So that is how we can utilize
our I conditionals in a variety of different aspects and add them all into place.
41. Additional: If-Elif-Else practice - [Lab]: Hi, everyone, and welcome
to the next lab exercise. I have decided to add in an additional exercise
on if conditionals, and I'm also going to
delve a little bit more into utilizing logical
operators as well. Let's go ahead and continue. Right. So the first
thing that I want to do is I want to define
my variables as always. So we're going to have
my first name equals, and I'm going to say
on then I'm going to have my last name equals, and it's going to be Pretorious. I've defined that. Next,
what I want to do is I want to go ahead and set up
an if statement here. I'm going to say the following,
which is going to be if. My first name is equals
to n, double equals no. And my last name, double equals is Pretorius
add in my colon. Then what I want to do
is I want to say print, and I'm going to
say, welcome Arno. Now, of course, you can add in spaces here for your
print function as well. And if this is not the case, I'm going to say else, which will be in line
with my statement. I'm going to say print,
and here I'm going to say access, Dno. So let's see what we
are checking here. So we're checking if my
first name is equal to Anu and my last name is equal to Pretorius,
which, of course, it is. It's going to say, welcome Arno. If either one of these
checks here fail, it's going to go straight to the statement and
print access denied. So remember, with what we learned with logical
operators with A. So both of the comparisons
that we make here need to be true for it to
return a true statement, in other words, to execute
the line of code beneath it. If that's not the
case, it's going to fail and print access denied. So let's go ahead
and test it and run this code. It says, welcome no. So that means that
the first name and the last name matched the constraints that
we were checking. Let me go ahead and
change just the one. So I'm going to change
this from nu and I'm going to say Yaku, okay? And I'm going to leave
the check as is. It's going to check
if it's still Arno and the last name Pretoris. Now I've changed
it to Yahoo now. So only this statement here, my last name equals Ano
Pretoris is going to be true, and this is going to fail, which means it should print
out access denied now. So if I were to scroll
down and run this code, it's going to say
access denied because both statements need to be
true in order for it to run. So if I revert this back to no, okay, both of these statements
are going to be correct. So if I were to run it, it's then going to
give me welcome Arno. That's how you can also
see how we can utilize our logical operators with our and statement.
Let's switch this up. Let's do it with or statement. If I were to say
or, in this case, I'm going to switch this
to Yahoo as an example. It's going to go ahead and pass here and I'm just going to say welcome as
something general. It's going to pass because one of the
statements are true, and that is the last name,
which is Pretorious, but the first name is wrong, and it just says Yaku. So if I were to run this, it's going to say, welcome, which means this
line of code was ran because one statement was
true, which is the last name. And that's the reason why this executed because of
this. All right. So that's how you can also
utilize the or statement as well and to have that
into place. All right. Let's go ahead and change our examples here
just to chat a bit. Let's go ahead and do this with something a little
bit more different. Let's go ahead and set this
with a bull, for example. We want to test
the not operator. Here I'm going to keep
it simple and say, let's say,
undergraduate student. Let's say equals true. Then what I can do is
I can set up my if statement to say, if not, undergraduate student
I want to say print, and we're going to see if this is not an undergraduate student, we're going to go ahead and say, welcome, and we're going to say, you are not an
undergraduate student. Please use library
one A, an example. We have that set and then
we can see else if it is the case that they are
a postgraduate student. Here we're going to see print
and we can say, welcome. You are a undergraduate student. So please use Library two A. But let's go ahead and run this. It says, welcome. You are
a undergraduate student, so please use library two A. This is what we set
up here. We set the undergraduate
student status to true, and we're checking with
this if statement if you are not an
undergraduate student. It's going to print, welcome. You are not an
undergraduate student, so please use library one A. S. So if it's the case
that this line fails, it's going to print the following line,
which says, welcome. You are a undergraduate student. So please use Library two A. So this is how we
can also incorporate logical operators
with not as well. So we've learned how we can
utilize and or and not here. So this is how you can go
ahead further and combine a lot more together to get different results depending
on your use case. All right, guys, that's all
for this extra lab exercise, and we're going to continue in the next lesson on
nested if conditionals.
42. Nested IF conditionals: Hi, everyone, and welcome
to the next lesson, which will be focused on
nested if conditionals. So let's get started. Right, so a nested
if conditional. If statements can be
nested within one another, hence prompting the term
nested if conditionals. So if the condition in the first if statement
evaluates to true, the next if statement will be executed and you
can see this in a short coding snippet here
was a nested if conditional. We will have our first if
statement and then under it, we'll have the next if
statement so remember. If the condition in the first if statement evaluates to true, the next if statement will
be executed straight after. Let's take a look at
an analogy to make this a little bit
easier to understand. So let's say for example, we want to become the
president of the USA. You have a person and you want to evaluate their
citizenship status, if they were born in
the United States, and we can go ahead
and have a look here and look at
the requirements. For example, you need to be
over 35 years of age and you need to be a natural born
citizen, a theoretical example. Then of course, we can utilize this example and put this
into a nested if statement. For example, we can
say the voter age here is 37 with
the variable set. Another variable which we can
utilize is the citizenship. We can say they're from
the United States and that was the country of birth. To be more specific, you could
even say it in that way. Then you want to check on
your first if statement. You want to say,
if the voter age is greater than equal to 35, then you want to go ahead
and execute the second if statement since that
first if statement was true, then you want to say
if then in that case, the citizenship or the country
of birth is United States, then you want to print, you are eligible to run for president. This is a typical
way in which we can utilize a nested if conditional, you can nest your if statements under each other
instead of having them line by line and having them evaluate a particular
constraint. That is how we can focus
on nested if conditionals.
43. Creating a nested IF conditional - [Lab]: Hi, everyone, and welcome
to the next hands on lab exercise for
nested I conditionals. So let's go ahead
and get started. So we'll start off
with a very simple and basic example here that will be focused on running for the United
States presidency, which will take in
the person's age and the country of birth. So let's go ahead
and get started. So you can say voters age is a variable in the
snippet I provided in the theoretical lectures
or you can say applicant age. It really depends anything
that you want to do. I'm going to change
it up here a bit. So I'm going to say
here applicant age, and I'm going to set this
equal to city eight. Then I want to create
a variable that says country of burs. Here I'm going to
say as a string USA. I've got my variables defined. Applicant age and
country of birth. Now I can start off my
nested if statement. I'm going to first
of all, check if the applicant age is
greater than equal to 35, then only then I want to execute the following
if statement, which is going to check
the country of birth. Then I want to say if
the country of birth is equals to that of USA and then going to
print out here at the end, you are eligible to run for the US president C. So we can check
everything here in place. So the applicant age is study A. If the applicant age is greater
than equal to study five, we can go to the next I
statement because it's true. And then, of course, we can see here
that is also true. They also from the USA, is then going to say
you are eligible to run for the US presidency. So now we can go ahead and
run this code and it says, You are eligible to run
for the US presidency. Perfect. Now, let's
say, for example, this first if statement
here is false. Let's say we want to catch
that out with an statement, before we go to this line here. For example, what I
mean by that is if, for example, I have Sety four, I already failed this first E statement and
it's not going to even bother the program
won't go and carry on. If I were to run this, the first E statement will fail, but nothing's going to happen. Maybe we want to
add in a message. We can have an else
statement that will be ran if if
statement fails. In the same line,
let's press Enter. You want to backspace and in the same line as this first if statement you want to see else, what you want to do is
you can go ahead and add we can actually
make this a bit. Let's move the space like that. We can see print and
we can say there, you are not old enough that will go
ahead and execute here. If the applicant age is
greater than equal to 35, it's going to continue with the line of code under an set. If not, it's going to run
this statement in line of this if statement that will print you are
not old enough. Let's check that first
so we can say run. It says, you are not old enough. That's what's going
to be executed. Now if for some reason
you are old enough, so let me say Sety six. This line is going to pass
and then it's going to go to this line of code
here that checks if the country of birth is USA, print, you are eligible to
run for the US pregnancy. Let's say, for example,
you are of the age, but your country of
birth is different, you can go ahead
and press Enter and here with this if statement
you're going to check. Remember, the spacing
is very important, so the indentation
you can say print. You are not from the USA, your application is rejected. Quite brutal, but we can
now see how this is ran. Now if we are above
study five with sty six, it's going to go and evaluate
the next if statement. I am not from the USA. I'm going to switch this
to another country, let's say South
Africa, for example. It's going to fail
and it's going to go it's going to go
to the L statement and print you are
not from the USA, so your application is rejected. Let's run this then
it's going to run. You are not from the USA
application is rejected, I purse past the first stage. Then when I went to
the second stage of the if statement here,
the nested if statement, it printed out from the USAC
application is rejected. That's how you can
essentially go ahead and utilize nested if conditionals. You can set up your nested if statements and you can
also use statements to complement it as well and
build something a little bit more how can I say involved
and a bit more structured.
44. Additional: nested IF conditional practice - [Lab]: Hi, everyone, and welcome
to the next lab activity, which is going to continue to focus on nested if conditional. We're going to work on
another exercise just to solidify our
knowledge and to just go a little bit
further. All right. Let's go ahead and set up another nested if
conditional here in place. This next one here is
going to be focusing on a student that's
trying to apply for a postgraduate
program and they are a bachelor student who has recently completed
their bachelor's degree, and they've got their
final year results and they want to apply. So let's create a nested
if conditional for this. So first of all, what
we're going to want to do is create our variables. So first, I'm going to set
the variable for the degree. So here I'm going to
say degree equals, and this is going to
be a bachelor degree. Then I'm going to
say final grade, and here I'm going to say 77. Then what I want to do
is I want to set up my nested if
statement logic here. First, we have our
first if statement. So first, we're going to check
the degree of the student. We're going to say if degree is equals to that of
a bachelor degree. We can continue and add in
the nested if statement, which if that's true, we want to then check
their final grade. We can then go ahead and
say if final grade is, of course, greater
than or equal to 75. Then we can see print. You are eligible to
apply to post grad. There we get. We can see
you've got that in place. The final grade is 77, our bachelor degree
the bachelor degree, we'll pass the first test and
the second test here and it should print out in the console you are eligible
to apply to post grad. If I scroll down and run this, it should
say, there we go. You're eligible to
apply to postgrad. Now, let's go ahead and
add in some extra factors. Let's go ahead and switch
over the statements here. In this case, I'm
not going to add in an L statement for the
bachelor degree part here. I'm just going to add in an statement for
the final grade. Here, I'm just going to say s
and I'm going to say print. You are not eligible to
apply for post grad. Then here I'm going to say, and I'm going to say
here, grade to low. So let me switch, of
course, my grade 272. The degree is still going to be a bachelor degree, so
that's going to pass. But the final grade
here is going to be 72. It's going to be under
the requirements. That means this, of course, print statement won't
be executed further. We're then going to skip
to the next statement, and that's going to say
you're not eligible to apply for postgrad,
grade two low. Let's go ahead and run this. And let it go, you can see it. It says, You are not
eligible to apply for postgrad, grade to low. So that's how we can also
just get more practice in with our nested
if conditionals. All right. So that's it for the activities and lessons on nested if statements
and the like.
45. For loops: Hi, everyone, and welcome
to the next lesson, which is going to be
focused on four loops. So let's go ahead
and get started. So the question on our minds
is what is a four loop? So a four loop is an
iterative statement that is used for repeating or
iterating over a sequence. And this can either be a tuple, a list, a dictionary, a string, or even a set. So you can see there
are multiple use cases for a four loop. Now let's go ahead and
take a look here at a few coding snippets
for examples. So here, for example, we have a list. Okay, so our variable
defined here is people. And in the list, we have a
list of names such as Maria, John, Eon, Kate, and Sarah. Those are the
values in our list. Let's say we want to loop
through the names in a list. We can go ahead and
utilize a four loop and to declare a four loop, we would start off
by saying four, and then we want to decide
on the variable that will take on the values in the
range that we have here. We're going to say four person, person will be your variable, and in want it to be
in the people list. And then as it loops
through this list, we want to print each value in that list by
saying print person. So this four loop
is going to execute until it has printed out all
of the values in the list. So when we start off and
say four person in people, it's first going
to look at Maria, and then it's going to
say we say print person, which essentially is going to
print Maria to the console. Then it's going to go
to the next value, which is John, and then
it's going to print John. Eon, Print Kate,
and Prince Sarah. Now, this will make
a lot more sense. Will we actually go ahead and apply this in the lab exercise? Now, here is another example where you're going to
be utilizing range. Okay. So by utilizing range, this is very helpful when we are working with numbers per se. So again, we need to
add in a variable where these where the assigned
values will take focus here. So we can say four I in
range zero to 11, print I. So it's going to print
all of the values in the range of zero to 11. Now, it's going to be very
specific in this case. So when you are setting up your four loop with range here, you're going to print out from zero all the way to ten in
this example, not 11. Okay? So whatever you specify at the end in the
range here is going to be the limit and not include
that particular number. Now, I know this might be a bit confusing to wrap
your head around, but don't worry, we
are going to work on this in the lab exercises. So this is just a bit
of an example when T's are into four loops.
46. Running for loops - [Lab]: Hi, everyone, and welcome to the next practical
lab exercise, which is going to be
focused on follow. So let's get started. So we're going to work
on a few examples here just to solidify
our understanding. Right. So first of all, I
want to work with a list. So I'm going to create
a list here of sports. So I'm going to say sports
equals, and here in my list. Let's put in golf. Com let's put in football. Put in rugby, put in tennis, and let's put in Bd ten. Okay, so we have a few
sports here in place. Now, we want to loop through all of the values in this list. To do so we can create
a basic four loop. So we start off with
the four keyword here. Then we want to define a variable which is going to be assigned to all
of these sports. I'm going to say for
sport in sports. This is the variable
that we will declare, which is going to take in
all of the values that are in sports here that we've defined in this
list here for sports. Sport is going to be assigned
to golf, football, rugby, tennis, and badminton, and
it's going to be in place. Then to close this floop
we need to add in a colon. Then we want to say print. Port. So it's going
to be in order, and this loop is going
to keep on running until all of the values
have been output here. So we are first
going to have golf. So it's first going
to print golf. Then the four loop is
going to run again. Then it's going
to grab football, assign it, print
football, grab rugby. The four loop is
going to run again. Then it's going to look
for rugby and print that, then tennis, and then badminton. Let's go ahead and see this test here so we can go ahead and see run and there we go. So it prints golf, football, rugby, tennis, and badminton. So this loop is
going to run until all of the values
have been printed out in the list here that we have essentially gone
ahead and declared. That's a basic way in which
you can utilize a four loop. Now, another thing I want
to teach you is how you can add in breaks in your four loop. So if you want your program to stop after a specific
value has been hit, let's say I just want to print
out all of my values here in my list up until
let's say tennis. I don't want to
include Badminton. I want to stop at tennis. So what we can essentially
go on ahead and do is we can go on ahead
and set it up as follows. We can add in an if statement. So we can we here, say I sport is
equals to Badminton. And remember, you need
to spell it exactly. Then I want to say break. This is going to break the loop, not break it as in give it a
stop so it's going to say, stop the fall loop from running. I don't want you to iterate through all the
values in the list. I want you to stop when
you reach Badminton. It's going to print golf,
football, rugby, and tennis. It's going to loop
through everything. And of course, if the sport
is Badminton, it will break. If it's not, it's going to just keep on printing
the sport so golf. Okay, it's going to go straight
to printing it, football, we'll go straight
to printing it, rugby, printed,
tennis, print it. But as soon as it
reaches Badminton, this statement is
going to be true, and it's going to stop the
fall up there and then, and it's going to be stopped
before it has been output. So let's go ahead now
and run this code. And there we go. So we
can see it prints golf, football, rugby and tennis. So that's how we can utilize
the break statement here. Now, let's say I want to only
print golf and football. I don't want to print rugby,
tennis, and Badminton. You can already stop this
already by putting in rugby, and then it's not
going to continue to print the other values because it has stopped here
by the break statement. So it'll only print out
golf and football now. So now if I were
to run this code, we can see it only prints
golf and football. Right, so that's how
you can integrate break statements within
your four loops as well. Okay. Right. Now, the next thing that I want us to do
is to learn how we can utilize the
continue statement. So this is a little bit different from the
break statement. Now, let's say, for example, we want to prevent a specific value from
being output here. Okay? We can go ahead and add in specific code or
anything of the like, but let's say for
argument's sake, I want to go on ahead
and print out golf, football, tennis and Badminton, and I want to actually,
let's do it this way. Let's say I want to
output golf, football, rugby and Badminton, but
I want to omit tennis. Okay, so what you can do, you can say if sport equals, equals, and we can say tennis. Okay, I want to
just say continue. So essentially what's
going to happen now is it's going to print out the sports
like usual golf, football, and rugby. And then when it reaches tennis, we're just going to say continue and keep on running
the four loop. Okay, so it's going
to say continue. And then before going
to print out the sport, it's going to go ahead and start the fall
loop, of course, again, and go to the next value, which is Badminton, and
it's going to omit tennis. Okay? So this is how the
continued statement will work. So the continued
statement allows us to continue as the four loop. Of course, it's going to restart the four
loop you could say, and it's going to go
to the next value and emit what it previously
was set here as tennis. So now if we were to
scroll down and say run, you're going to see here or just play golf, football,
rugby, Badminton, and emitting tennis here, but continuing with the
four loop afterwards and just carrying on with the list here and the values
that we have in place. So that's how we can use
the continue statement. Now, another thing I
want to teach you is how you can utilize range. So we can go ahead
and remove all of this and what I want us to do is to say four I in range, and we want to put in a
start and a stopping range. So the first value will
be where we start, but I want to start at zero, and I want to end at ten, which means I need to put in 11. I want to explain now why
this is adding a colon. Then we can say print, and in parenthes
we can just say I. So again, here I is
going to be a variable, which is going to be
assigned to each value here. In this case, it will be zero, one, two, three, four,
five, six, seven, eight, nine, ten, and the 11 here is going to
be the upper bound. You will never print
out the value here, and you will only print
the value minus one to it. So let me show you here. So this should print out zero to ten, so it scroll down and say run. And we can go ahead and see
here that it prints out zero, one, two, three, four, five, six, seven, eight, 910. Now, if I were to set
this to, for example, 51 and I put in one here, that means it's
going to print from one all the way to 51. So now if I were to
scroll down and say run, it prints all the way
from one straight to 50. That is how we can also
utilize the range in our four loops to print out specific values as we can
see here in a given context. All right guys. That was
working with four loops. A few exercises and examples just to solidify
your understanding. What we're going to be doing in the next lesson is going to be utilizing four
loops with a list of dictionaries. Stay
tuned for that.
47. Loop through a list of dictionaries - [Lab]: Hi, everyone. And welcome
to the next lab exercise, which is going to be focused on utilizing four loops with
a list of dictionaries. So essentially, what
we'll be learning is how we can create a
list of dictionaries and how we can loop through
our list of dictionaries. So let's go ahead
and get started. So of course, what
we want to do is we want to go ahead
and create our list. So let me throw down here. And we're going to
call this people. I'm going to say equals. And remember, this is
going to be a list, so we need to have opening and closing square brackets,
just like such. Then we can define
our dictionary. We're going to have
three dictionaries that we want to define
or three instances. Remember dictionaries, you start and you open them up close
them with curly braces. We're going to have to
define our key value pairs. We're going to
have, first of all, name and then colon afterwards. The first key is
name and then the value here after
adding a space here, we're going to go ahead
and set this as let's say honor then we need to add in a comma for our next
key value pair. Then we're going to
go ahead and set this as we can go for favorite color. I'm going to say fav color
and then curlon space, and then here I'm
going to say brown. Comma. That's another
key value pair. Then here, I'm going to specify
another key value pair, and this is going to be let's
set this as non analogy. And then we're going to
have our colon space, and then here I'm going
to say South Africa. Then we need to add
in a com at the end, and we're going to create
two more dictionaries. Now, I don't want to
reinvent the viel especially with the keys
that we've defined, I'm going to paste
this just like so. Then I just want to
replace the values here. So here I'm going to say mark, favorite color,
red, nationality. We can say Germany. And the last one here,
we're going to set this as Kate, favorite color. We can say green nationality. We can say, Australia. Remember for that last dictionary that you
have in your list, you can just remove
that comma at the end. This is a list of dictionaries. Now you're probably wondering,
how are we going to set up our four loop so that we can loop through
these dictionaries? It's very simple. What we want to do is we want to set up our four loop, so
we're going to say four. Then what we can do is
define our variable. I'm going to go on ahead and set this as
just go ahead and set this as we can
say for I in people. On. I is going to be assigned
as the variable that's going to be assigned
to the values here of our dictionaries in the
list here of people. Then we can just say print
and within our parens here, we can just refer to I and
then here our square brackets, we want to refer to a
particular key, any key here. Let's say I want
to get, of course, all of the values of everyone that has favorite
color, fav color. Here in my square bracket, I'm going to put in fave color. Now if I say print and run, it's going to output
brown, red and green. As we can see there,
what's happening here is we have defined
our variable here, which this can be anything.
I is just an example. You can call this person, you can call this human, or you can call this applicant
or anything that you want. That's going to be assigned to all of the dictionaries
that we have here and it's going to loop
through each and every one that is present
in this listue in people. It's going to go through
all of the data here. Then we're going to print all of the values based on the key, which is favorite color. We can do this for anything.
Let's say we want to get all the names in all
of these dictionaries. You would just refer
to the name key, which is, of course, name, and you can say run that will output
Ano Mark and Kate. Let's say we want
to get nationality. We can do the same thing. You can just put in the
key, which is nationality. And you can run, and that will print out all
the nationalities. So South Africa,
Germany, and Australia. All right. Let's say
you can do just that. Now, let's say for
argument's sake, you want this to be a
little bit more concise. And let's say you want to
get out all of the records, and you want to treat
it as one person, the second person,
and the third person. Okay? So what I'd
recommend you do is to go ahead and put all
of those keys in order. So first, you'll have name. Then you can copy
that and you can go to the next line and
you would then have fav color and paste another line here and that's
going to say nationality. We're going to
print out the name, favorite color and nationality in terms of all the values, and this is going to
be grouped, of course. If I were to say run, you can see here it prints everything or Browns of Africa. Mark red Germany,
green, Australia. It's all linked to each of these dictionaries
values at a time. Now, it doesn't look very clean, what you can do is you
can actually add in some space remember add in the print function
with an empty string. So you can just simply say here, print and add in an
empty string here, and right at the end as well, print and an empty string. So that's going to
give some space. So each dictionary here with its own data is
going to be separate. So Anno's data will be
more clean to read, Marks and Kates, as well. So now if I were to say run, you'll see there'll
be more spacing here. So you can see here,
Okay, this belongs to no, Browns of Africa, Mark red, Germany, and green Australia. So that makes it a bit more
concise and cleaner to read. Right. That is
essentially how we can set up our four loops and utilize it with a
list of dictionaries. This is going to
be very important, especially if you are
planning on working with data and you're going
further with Python. For example, if you decide
to learn jango for example, this is a very important thing
to understand in terms of looping your data with dictionaries and
understanding loops. It's very beneficial if you go further into web
development, for example. Guys, that's it for
this lab exercise.
48. While loops: Hi, Ron, and welcome
to the next lesson, which is going to be
focused on Wil oops. So let's take a look. Right a Wile loop. A Wile loop is very similar
in nature to a for loop, but unlike a for loop, a Wile loop continues to
execute a statement as long as the specified
condition remains true. To help us to solidify our
understanding of Wile loops, let's go on ahead and take
a look at an analogy. Now, this analogy
here is going to be focused on password management. Let's say that you were
to enter in your password to access an online portal or to login on your computer
or anything of the like. Going to base our analogy of understanding
while loops on that. Right. So we have
our user that is attempting to log on to a
site or onto our computer, and programmatically,
we can create a very simple program where
with the WLloop process, we can define a password here. So, for example, we have
a variable core password, and of course, the
value is password 123 here as an example. Then we can have our input
function here and the value of that input function will be assigned to the variable
enter password attempt. So we're going to ask our users
to enter in the password. Then we can utilize a while
loop to keep on running here. This wile loop is going to say while that password attempt, whatever password was entered, that attempt is not equal
to the actual password. We're going to print out in the console to the
user incorrect password, please try again. Then what's going to happen
is we're going to be prompted again to go ahead and enter
in our password again. This Wilops going
to keep on running until this condition here, how can I say until this
condition has not been met. As we can see here, as long as the password is not
equal to the password, that is the operator here, the comparison operator
here, not equal to. So while it's not
equal to the password, it's going to keep on saying incorrect password and
prompting us to try again. And once that
particular statement, once it, of course, has not been met,
in other words, while it is equal
to the password, it's then going to exit the Wile loop and it's
going to print welcome. So that's one way in
which you can utilize a Wile loop in terms of handling a password that
is being entered by the user. Oh, that's it for the
theoretical side. So in the coding exercises, we're going to delve
into while loops.
49. Running while loops - [Lab]: Hi, everyone, and welcome
to the next lab exercise, which will be
focused on utilizing WOW loops in PySon.
Let's get started. Now, first of all,
we want to declare a variable that we
want to utilize. I'm going to say here,
my number equals, and I'm going to
say zero for now. Then I want to set
up my WOW loop. I'm going to say while my
number is less than ten. I wanted to print the following, which is going to say this
number is less than ten. So the condition that this while loop here is evaluating
is constantly going to run and execute this print statement as long as the number here
is less than ten. So in this case, it's
going to be infinity. So let's go ahead and
test this out so we can scroll down and say run. And we can see
here it constantly says this number
is less than ten, this number is less than ten, this number is less than ten. You want to go ahead and
stop this code from running, so it may take a moment
and then it stopped. That is what will happen here if you have an unchecked
while loop, you could say. It's constantly going to execute because what's happening here
is the condition is true, that the number is in
fact less than ten, so it's just going to for the end of time print
out this statement. Let's say, for example, you are working with a number that is equals to ten or
greater than ten. That's going to essentially not run this while loop at all because the first
condition is not met. Now, if you were to do that, let me just say 11 here, for example, and run the code is just going
to output nothing. So you're going to
see here, nothing is going to be output at all. We can just go ahead
and stop this. The reason for that is because there is nothing that
we are outputting. The number, of course, is
clearly greater than ten. So what you would
typically do here is outside of your while loop. In the same line of it, you can say print and here we can say the number is greater
than or equal. To ten. And that's what you
can do to set this up now. So then I'm also going to say the Wile loop was not
executed, and it won't be. So let's go ahead and run
this now, and now you will, of course, see that outputs the number is greater
than or equal to ten. The Wile loop was not executed. So what happened was we evaluated this statement
to see if it was true. It was not true, and it went straight to the
print statement here and it didn't execute the code in it because the
statement was false, and it went out and printed the following
line of code here. So that's how you can
utilize wil loops in Biason. So that's the simplest way to understand their logic with it. Now, let's say we want
to make it a little bit more involved and we want
to execute some code, but don't execute forever, but only to a
certain limitation. What we can do is we can go
ahead and do the following. We can remove what we have here. I'm going to set
the number to zero. While my number
is less than ten, I want to say print my number. Okay. Now remember,
with our Wal loops, we need to ensure that we
are coding underneath it. So if it's on the same line here, that's not how it works. You need to make
sure you press chap and make sure you was
in your wall loop. Same goes for the four loops and your if statements as well. So do keep that in mind here
with nesting our statements. Okay. Now, if we run
this code as it is, again, it's forever
just going to, in this case, print the
number over and over again. But what we want
to do is we want it to essentially print
the number one by one and we're constantly
going to add plus one to each number and
increment it by one. When this while statement
here, this logic here, this statement is not true, it will exit the program at
the end and not run forever. To increment, you're going
to want to say my number. Equals my number plus one. So essentially, what's
going to happen here, we're going to take zero. We're going to evaluate if
the number is less than ten. So zero is less than ten. Then we're going to print zero, and then we're going to add
one to it and assign it to the variable so that it
is going to then be one. So then one is less than
ten, print out one, and then we're going to add one to it, so it's going
to become two, and then two is less than ten, and then we're
going to print two, and we're going to keep
on doing this until the condition here is going to be false and once
it's been false, we can add a print
statement outside saying that we can just say that the program and
counter is done. Just a simple example. This is what we'll see at
the end after we have gone ahead and proven this WAL
statement here to be false. In other words, once
it gets to 11, 12, 13, for example, it's not going to constantly print
out those numbers, it's just going to
end the program here. Let's go ahead and
test this out. We can say run and there we go. It goes all the way 0-9 and it says the program
counter is done, so it exits the program, it doesn't keep on
running forever and ever and constantly after that. All right. That's how you can go ahead and set that
up here in place. Do keep in mind once
the number is nine, it's going to say
nine is less than ten and then it prints out nine. Of course, when it adds the
one to it, it becomes ten. While ten is not less than ten, this
is not going to work. I will just immediately go
and print out the program and counter is done once it
gets to ten in this case. Okay. So that is the basics of how we can utilize
our wild loops. Now, what I want to do now is I also want to give you a bit of a more comprehensive
example here that will solidify your
understanding a bit better. And that's the password. That's the best way to learn
understand wild loops. So I'm going to say
here pass word equals, and I'm going to say pass
word one, two, three. And then I want to
have input function that will be assigned
to my variable. So I'm going to say entered,
underscore password, and I'm going to
say equals input, and I'm going to say, please enter in your password, adding a colon and a space. Then I want to set up my Wile
loop, so I'm going to say, while the entered
Password is not equal, so I'm using my
comparison operators not equals to the
password itself. I want you to
display a message to the user that says
in valid password. And then I want to prompt the user again to enter
in their password, and then I'm going
to go ahead and say entered password equals, and I'm going to use this
input function again. Please enter in your password. Like so. Then if in the
event this statement here, of course, is not true. Okay? So if it is a password, if the entered password is equal to the password,
so the opposite, we can say print here
and say, welcome user. Okay, so let's go through
what's going on here. So we're setting up
our password here and we are then prompting the
user to enter in a password, then our wile loop
is going to check. Okay that entered password if it is not equals
to the password, we want to say invalid
password each time. Until the user enters in a
password, that's correct. What's going to
happen is this loop here is then going
to repeat itself. Anything either side
is not going to be run, just everything here. That's why after we
say invalid password, we need to add in this
input function in this WOW loop so it
keeps on running this program here
and checking as long as that enter password
is not equal to the password, it's going to keep on
saying invalid password and prompt you to keep
on entering a password. Once the password is correct, when this statement is false, when it's equals
to the password, we're going to
print welcome user. Let's go ahead and test it out. Let's say run. So we know our
password is password 123. I'm going to say here
pass 21, two, Enter. Invalid password, please
enter in your password. I say pass four,
three, two, Enter. Invalid password, enter
in your password. So it's going to
keep on running. And what I'm going to do now is put in the correct password, password 123, then it
should output welcome user. So now I'm going to say pass
word one, two, three, Enter. And then it says welcome
user, and the program, as you can see, stops
running as well. Alright. So that is how you can go on ahead
and set this up for a password management
use case and how you can utilize WOW loops in PySon.
50. Modules: Hi, everyone, and welcome
to the next lesson, which will be
focused on modules. Right. So modules. Now, a module is a file that
contains PySon code that can be included in our
PySon based applications. Now, the benefit
of modules is that they help you to
break your code into smaller reusable
pieces so to make it easier for us to work with our own custom
made applications. Now there are a lot of
PySon modules out there, and I mean a lot. There is a list that you can read and take a look
at the following URL. You can have a look
here at the URL, type it in and this website here is
going to list out to you a vast array of PySon modules that you can include
in your application. I'm just going to take you through a very short
exercise here on how you can utilize
a module in PySon. But there are a lot
of other modules out there that you can read
about and experiment with, so just keep that in mind
and take a note of that. Modules.
51. Experiment with modules - [Lab]: Hi, Ron, welcome to
the next lab exercise, which is going to be focused on working with
modules in PySon. Now, that link that I shared
in the previous lesson, which focused on the list
of modules in PySon, you can just head on over to the following website and here
you can see a list of all of the PySon modules that are available to
us that we can utilize. As you can see, there
is a vast array and there is an explanation
given and you can also even read more about all of these modules
that are given to us. We're going to work with a few modules in
this lab exercise. Just wanted to give you
some insight if you want to learn more and
go into more detail, perhaps after this exercise
or after the course. Let's get started in any case. I'm going to keep
it rather simple. When you want to import or
work with a module in Pyson, you need to add in the import
statement first of all. That's the first thing
you need to add in if you want to work with a module.
You want to say import. The module you
want to work with. In this case, I'm going to use the datetime module
because at the end result, I want to get the
value of today's date. I'm going to say, date, time, and that is the module
that I want to utilize. That's going to contain a
whole list of functions and classes for working
with dates and times. Now the next thing that I want
to do is I want to import the date class from
the datetime module. To do so, I can just
simply say date time, Import, and then I want
to import the date class. Perfect. Now, the next thing that I want to do is I
want to set up a variable. I'm going to say today, equals, and I want to reference
that date class. So I'm going to say date, and the date class has a
specific method attached to it, known as dot today. So you can say dot today. That's going to get the date for us today in the
format of year, months, and day and
it's going to assign that value to the
today variable. Then we can say print
in parentheses, we can say today and that
will output it for us so we can see there's a date that has been
printed out for us. That is how you can
go ahead and create a simple program using
a module in pyson. Let's go ahead and make this
a bit more interesting. Let's try and work with the
mass module in Pierson. Let's try and calculate the square root of a
number, for example. We can remove this. Let's say import mass,
inputting the module. Then we want to prompt the
user to input a number. We're going to say
my number equals and we want to cast this to
an integer automatically. We don't want this
to be a float, so I'm going to say int. And then I'm going to add in the input function
within that. Whatever is added
in here will be treated as an
integer, the value. I'm going to say,
please enter in a number to find
its square root, colon, and we leave it as we can add a
space here as well. So we have that set.
Now the next thing you want to do is
calculate the square root. I want to have a variable to
store that value at the end. I'm going to say SRT for square root, underscore
value equals, and then we're going to grab the mass module that
we imported, say dot, and then we want to
apply the SRT function, which comes from
the mass module, and it's denoted as SQRT. And within the parentheses
here is where we want to perform this calculation of the square root was the
number that we want to check the square
root of and in here, we can put in my number. And that will get
the square root of the number that
you've inserted, and that value essentially is
going to be put in here to find the square root
of that number and it's going to be stored
in square root value. Then we can set up our print statements here.
I'm going to say print. And first of all,
I'm going to go ahead and add in my number. So here I'm just going
to say my number. So the number I put
in, then print, I'm going to put a
statement here and it says, the square root is, and then right below here, I'm going to put in SQRT under square value
and then I'll print out the square root
for set number. So once you've got
that in place, we can go ahead and
run our program. And it says, Please enter in a number to find it square root. I'm going to say
64, press Enter, and there we can see
it printed out 64, which of course was Acho to. And then it said
the square root is, and then it said eight
here at the end. Okay. And that printed
the square root value. So that is a very helpful
use case that we can add into place here
if we were to go on a head and essentially work with our modules in
Pyson in this case, a mass module, very helpful. There are a lot of things
that we can do and set up in this example here, but that's just an example
to give you a feeling here. Of the modules that we can use. Of course, you can
explore in further detail all of the PySon modules
here in this index and you can just explore and
see how they can best benefit your
PySon application.
52. Mini-practice project - Introduction: Hi, Ron, and welcome
to the next lesson, which is going to serve
as an introductory lesson into the practice
projects which you will be creating in this course. So let's take a look at the
projects that you will build. Now, the first project is going to be a simple calculator. So you're going to be
expected to create a simple calculator
program in PySon. The second project is going to be focused on a
greeting program. And the third and final
project is to create a student grade checker program. Now, with each of
these projects, I'