Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
hi i'm rona and together we will learn
python this course is structured in 17
lessons in each lesson we will learn
some new concept and build a project
based on that and previous knowledge
that means along the way it gets more
and more advanced we will start simple
by learning what are variables what are
lists and dictionaries how can you use
them in your programming how can you
make program flow with if statements and
loops we will use functions to structure
your code also we will use randomness to
create small awesome games we will learn
how to read and process csv files if you
don't know what any of these things are
don't worry we'll get there we will
learn about object oriented programming
and start by creating a card game that
we can play we will learn how to
visualize data and we will use numpy and
pandas to work with data these are some
of the key libraries you need to master
we will learn about recursive functions
and how to use that
we will learn about list comprehension
and finally in the capstone project we
will build our own machine learning
model we will build it from scratch yeah
that's right we're not gonna use some
library to build it we're gonna build
every single piece of the machine
learning model and we will test it to
see how it performs comparing to a naive
classical way to solve the problem for
me this is exciting and i want to share
this passion with you i'm owner i have a
master degree and a phd in computer
science i have more than 10 years of
professional programming experience i
love python and i want to share that
passion with you and show you why it's
so popular
see you
if you're new to programming or new to
python in general and you wonder what
people are using python for luckily
they're made surveys for that so let's
dive into that the main things that
people use python for are data analysis
web development machine learning devops
and programming web parses scrapers and
crawlers and so on you can see the full
list here this course will mainly focus
on how you can get started with data
analysis and machine learning why are
people so excited by python i mean i
know why i am but why are people well
some of the key features that people
love are simple syntax syntactic sugar
easy to learn right that sounds like the
perfect beginner programming language
and it is
also it's easy to write and read code
it's a high-level language basically
list comprehensions people love that
kind of stuff and we will cover all that
in this course so don't worry about it
and the list goes on why people love it
if you're looking for a job well is
python the right language and how long
does it take right how long do you need
to have experience with python in order
to get the job so let's first dive into
the job roles and see what are people
what label do they put on themselves
well the main one is developer and
programmer and i must say i'm in that
group as well i'm a freelance developer
at current stage and
yeah most people say that but they're
also architects and data analysts i
wouldn't go too much into the because
most people they call themselves
developers or programmers so how much
experience do people have actually well
they have three to five years of python
and that sounds like a long time but
there's a catch this is actually not
professional experience if you look at
that professional coding experience most
have less than a year unfortunately we
cannot conclude it will take less than a
year for you to get a job the trick
question is how long does it take from a
starting python to get a job
well you can see here that the python
experience in general on this survey
well it seems like around three years
most more than half have less than three
years experience also professionally
most have less than a year does that
mean it takes less than a year to get a
job no we cannot conclude that i'm sorry
to say that but i do personally know
people that started learning python and
got their first job in within half a
year
but if you have commitment you can do
exactly the same are you excited i'm
excited let's get started
this course is structured in 17 lessons
and in each lesson there will be two
jupyter notebooks the jupyter notebooks
contain the coding environment that we
work in and these are available for you
so you can follow along and also there's
a project at each lessons they're
starting quite easy and there's
description of what you should do
after you've done that you can see my
solution how i would do it in the videos
as well along with that there's a e-book
which actually follows you through all
you need to do so if you download this
ebook here you can actually go step by
step through the lessons as well and it
will show you everything you need to do
right how to install anaconda how to
download the zip files containing all
the jupyter notebooks so you're already
in your environment if you like to work
with an ebook where you can see all the
things we learned well this is a great
resource the projects we make along the
way become more and more advanced in the
end we like to make this reinforcement
learning a machine learning model where
we actually
do all the coding from scratch we will
use no libraries actually to implement
it
if you just want to get started and you
don't want to install anything well you
can do that too go to my github here and
actually down here you can see you can
actually access
all the jupyter notebooks directly in
colab and if you don't know what that is
well don't worry about it i'll just show
you here that is actually owned by
google and
when you
you can load the jupyter notebooks in
there and then you can actually execute
the code in there
directly so you can get the experience
that you're looking for so are you ready
i'm ready let's get started
so before we get started you should get
anaconda anaconda gives you everything
you need it gives you the jupyter
notebooks and the python environment so
how do you get all that well you go to
anacondas page and you download it here
you just click here
you should choose the operating system
that you're using i'm using mac here so
you should use a graphical installer and
then it just downloads and it's
available in a moment and after you
download it just install it well after
you installed anaconda you need to get
the resources from this course and the
resources are available in my github
repository there's a link for that and
what you do is actually just click here
and then you download the zip file here
the zip file contains everything you
need in order to complete this course
let's try to get started with anaconda
so after you installed it you should be
able to open the anaconda navigator and
in that one you should just launch
jupiter notebook when you press it a
terminal will jump up then you should
come to this picture
and you probably downloaded the zip file
if you did like that well you just go
here and click on that and here are all
the files available and in the first
session we're gonna work with hello
world so that is the one you should
click and then it opens here now you're
ready and i'm ready let's get started
so in this first lecture we will get
started with python we will start using
our first jupyter notebook and learn
along the way and in the end of this
first lesson here you'll get ready to
make your first project so are you
excited i am let's get started
so we're inside this jupyter notebook
and if you're unfamiliar with it well
jupiter notebook is an environment where
you can write text and execute code i'll
show you in a moment don't worry about
it
so the first thing here is called hello
world so there's a story behind that
the hello world thing why is it that
every programmer does this hello world
as a first program well there's a link
there it's a funny story and you should
know it that everybody does that as
their first program that's just how you
are that's how it is to be a programmer
make the hello world and be like us
right
awesome
so
should we do that
yeah i think so because one of the
advantages of python is actually the
hello world thing it is so simple if
you've seen other programming languages
you'll see wow why do you need all these
things well in python you only need to
use a print statement
print which will print to the screen and
you just write hello
world
executed so how did i do that how did i
do that what happened here right so i
wrote print hello world in quotes
and then it printed hello world down
here without the quotes that's the
output of the print statement and how
did i execute this one well you can
execute it by either pressing this one
up here the run here or you can execute
it by pressing shift enter
so if you download the e-book it's all
explained there too if you forget about
it so don't worry about that but you
always know you can always just use the
bottom-ups here well that was it the
story behind
the storybound hello world read it it's
funny
the sin of python and what is that it's
kind of a joke in some way but it's also
good guidelines and i included it here
because it's kind of like computer
science humor and on the other hand it's
also good guidelines when you do
programming
and the code to get it don't worry about
it it's just called import this and
this is not the
this is actually the way you import the
libraries but this library only prints
this the sin of python so if you execute
that cell here we'll get the sent of
python by tim peters
so i'm not going to go through all these
here together with you because
i just wanted to emphasize that these
are just guidelines and this kind of
sets you into the mindset how does the
programmer think right so it's just
beautiful it's better than ugly what
why is that right everybody agrees with
that but in computer science maybe it's
like you need to understand that you
need to make things in a nice and easy
to understand way and there's also a
saying called simple is better than
complex
and
actually the third one here simple is
better than complex you need to simplify
things you need to understand when you
program things often very easily becomes
very complex and nobody understands them
and this is saying actually that you
write the code once but you read it
multiple times right so when you add new
features you need to read the other code
in order to understand it when you debug
that is when the program doesn't work
you need to read the code again but you
only write it once right so we as
programmers we are actually responsible
to make it readable and actually look at
that readable readability counts right
you're an author you need to make a
journey you may need to make it
beautiful you need to make it simple you
need to make it readable
wow
that were just a few things from the sin
of python
read about it
have fun with it learn about computer
science humor i would say but it is kind
of on the edge of humor or serious
that's up for you to judge
so let's get started what is the goal of
this
lesson here right so the main goal is
obviously to get started with python and
we did that with the print hello world
we need to get a bit more started right
yeah okay i agree
okay and then get comfortable with
jupiter notebook this is the jupiter
notebook you can use it quite simple by
running and executing and yeah running
is executing i'm talking crazier
and
the key thing we want to know is how to
input
user input and how to print it back
right and we will learn a few string
methods capitalize lower is decimal and
replace in this one and this will
actually get you started so you can
solve your first project on your own and
when you solve it on your own i'll show
you how i would solve it
good let's get started down here
so the print is an amazing tool you can
print out
things and i say things you can print
whatever let's just try to take a
look at that right so print it can print
for instance a string a string is these
quoted here
double quoted it can also be single
quoted so you can print a string and
then you can comma separate it if you
comma separated you can put something
else in it it can be 10 and it can be
2.2 for instance
and what it does it prints this line
here with the string hey without the
quotes then it prints 10 and then prints
2.2 with a space between you'll see this
way of printing is actually a great way
to use it it can have any number of
arguments in it and
well i love to do it like that the other
way i would love to use print is with
formatted strings which we will learn
about later in the course okay
so
first thing first now we need to know
how to print things and uh well that's
not funny right you can just sit and
print a novel or you can print the send
of python here but how fun is that to
run that multiple times not really so
you want to interact with the user and
the user is the person in front of the
computer so how do you do that well
let's try input right
[Music]
input
well let's execute shift enter remember
execute whoa a line came there let's
write something on that line
i write my name
i press enter boom
and it returned
it just kept
on there look here i also made an
assignment here if you see here right so
the input assigns the value to s so how
it works in programming is that the
right side that is input here
is assigned to the left side here and
the left side is a variable we'll learn
more about variables in the next lesson
here so for now just know that you can
you put the input from
to typed in over to s so if we look at s
this is how you can look at s you can
either print it or just execute it here
if you execute s here you'll see it's a
string single quoted string here it
doesn't matter it's the same thing in
python single or double quoted and it's
called it has a value
awesome so far so good so another thing
you can do is actually you can in this
input here it is actually you can put a
string in that
so
just to learn teach you a few things
with jupiter notebook you can actually
press shift tab here and as you see here
then the
the dock string as it's called that is
the documentation of it
pops up
so
input it says prompt none by default it
can be a bit confusing to read
in the beginning but
what is this is read a string from
standard input the
trailing new line is stripped that is it
doesn't take the new line when you press
enter right uh the prompt string is
given if print
is printed to standard output without a
trailing new line before reading input
so let's try to put a prompt here so
what
what is your name right and question
mark many people like to put a space
between behind here and i'll show you
why
so what the space does is it actually
adds a space here
between
my prompting message here right so what
is your name space just put some space
there it's up to you what is your name
it is
and then i have it here and again i
assigned it to s so there is no question
about it
we print s here and it prints
here and you see the difference when i
use print and i just use a variable is
here it prints it without the quotes
here it shows it's a string with the
quotes okay
so far so good so now we can print
something and we can input something
and let's try to make it a bit more
advanced okay
so
let's say we have a name here we put a
variable and we can call variables
everything and we'll get back to that in
the next lesson and we put input here
and we say what is your name right
that's what we learned so far
and
look i pressed enter without executing
now because it was just enter and then
you have more lines you can multiple
lines of code right
so what we can do is print
hello
and then name here
and what that does is actually prints
the hello
space name as we know right name is the
one that's inputted there right now it's
going to be funny right so now we put h
here
and then we do input here and what
should we ask now
how old are you right
awesome
and then we can say print
you are
h
comma
o or years old right years old
awesome so now we've structured a few
lines of code together right so what
will happen now is first we'll prompt
what is your name then we'll print
afterwards when we get the input right
it will wait until the input is here and
put that value into name and then we'll
print hello and then the name input it
there and then we'll prompt you again
how old are you and
when you press enter it will put that in
h and then it will print you are and
then the h you enter and years old right
so i cross my fingers this will work are
you ready i'm ready
how what is your name okay it's still
luna right
hello
how old are you well i'm 30.
maybe
enter you are 30 years old at least the
computer believes me
i mean that yeah imagine we ran this
program again and we wanted to make it a
bit more advanced actually
this name here what if he enters that in
lower case all the way what do i mean
right so if i execute this one again i
just enter in lower case well what
happens is that it says hello only in
lower case can we make our program a bit
smarter well let's see
this is where functions come into the
picture and a great way to find
functions or methods as they are called
on objects and a great way to find the
methods uh is to write
dot because that will call a method on f
on a function and then you can see the
functions available and the first one is
capitalize so let's use that and then
you call capitalize on name and then you
need the
brackets here in order to
that's how you call methods okay so
let's try to execute again and as you
saw here you can execute the same cell
again and again so we do that shift
enter and it will ask you for your name
and we do that in lower case again here
but you see what happened now is
actually hello honey became capital
because capitalize what it does is it
capitalizes the word there amazing right
and again if you enter 30 it keeps
continuing believing i'm 30 years old
nice so what happened right said
differently you can say that only here
we have the string here una
and we can actually we can actually
assign that string and capitalize it
and you can see you get the
documentation here it returns a
capitalized version
of the string so again this is something
you need to understand sometimes things
return things sometimes they modify
things right so returning means you need
to assign it to a new variable right
here it returns a capitalized version it
doesn't matter because we use the
returning value of this thing here but
if you wrote
let's just show you here if you wrote
this here
and then you
print s here well
i should actually put it with lowercase
thank you for helping me
well
it does capitalize here but when you
print it out it's lowercase again
because it returns it so you need to
keep the returned value and let's just
put it in t here and then you see the
return value is capitalized right
so it's important to understand that
some methods they modify things and some
methods they return new things so you
should be aware of that
just a few more things right
if you you can apply it directly on a
string as i do here or you can do it on
a variable containing a string right so
let's look at some more methods here so
just pressing tab
and you can see a lot of methods
available here right so let's try one
here let's try upper what does it do
well obviously he puts all the
characters to upper right
and
we could do something it's probably
something called lower
and there is
and it will lower case everything right
and another thing is actually sometimes
when you work with strings you want to
know if it's a number or not right so
is decimal
it's a true here right well
so
similarly we could do on this one and
ask if is decimal
and you see that's false obviously right
and this comes in handy when we learn a
bit a bit more about programming because
sometimes we need to convert things that
are inside a string to an integer so
when we learn about variables in the
next lesson you'll learn about different
types of
variables
exciting another great thing is kind of
fun is like actually you can replace
things so let's say we had the name here
and we just put it to be
here that's my name and then you can
actually replace in the name here so
let's try to replace here and we take
the r and we replace that with an s
what's going to happen should we look at
the dock string or should you just guess
well you guessed it right
okay so what replace does it replaces
the capital r with a capital s so my
name is chained from une to soon and
if you're unfamiliar with my region of
the world well these two are both names
the first one is mine the other one is
not so a final thing i just want to show
before we end this lesson here is
actually
well the print here sometimes you want
if you noticed when you print something
here right if you print my name here
again here you see here it actually
makes a new line here and sometimes
let's make it a bit more
you see here it prints it on a new lines
sometimes you want to print on the same
line and you don't want to use the same
print statement so what you can do is
put this end here and then you can
actually say what should the string end
with by default it ends with a new line
but if you put nothing in here as you
see here and we execute that it will
actually not put a new line it will just
put nothing here right so now it says
una and sooner but the problem now is
right okay you wanted the space well you
can actually just say it should end with
a space and it adds a space but
the end here is actually overriding the
default value of a new line right okay
amazing so let's just walk very fast
through so let's just check very fast
what we did we did we learned a bit
about python we learned about jupiter
notebooks how to execute cells and we
can execute cells again we learned about
input and print statements we learned
about how to capitalize lower actually
also upper and is decimal and
replace and finally with the print here
we also learned how to avoid the new
line which is actually quite handy often
when we program
so the next thing is well you need to
get started with your first first
project so i'll just cue you in a moment
i'll help you to get started and after
you solve it yourself well i'll solve it
together with you as well so see you in
a moment
so here i'm going to introduce you to
the first project
and don't worry i'll not show any code
in this one i'll show you how to solve
it or how i would solve it in the next
one so first let's get into jupiter
notebook and see what it says so you
should be in the jupyter notebook called
zero zero projects so 0 projects
identifies this first project and it's
called input and output strings right
here as it says here input and output
strings
so the goal of this one here is
explained here the goal is to get to
know jupiter notebook and execute your
first program
right it's not really programs but it's
small
code snippets so just to get started
right
so description here of what to do well
this project is in three steps and you
should
do them step by step and the purpose is
still just to get comfortable with
jupiter notebook okay are you ready i am
okay so the first one is
just to make sure that we're on the same
page and you try to do your first print
hello world program do that and execute
the cell right so you know how to use a
jupiter notebook and get comfortable
about that
then in the next step here when you've
done that the first step well the next
step here you should use input to get
the name of the user and print hello and
then the name of the user right again
it's very easy you've seen me doing it
but often there's a difference between
seeing people doing it and actually
doing it yourself sometimes there's
small details you don't notice trust me
i've been there i just like seen people
doing stuff and it's like that's easy i
can do that myself but when i'm sitting
at the computer myself then suddenly
it's difficult and there's some details
you didn't see
you first learn programming when you do
it yourself right
step three
use the input to get the name of the
user print i like capital letter and the
name capitalize by using upper right
remember upper
returned everything in capital right
so
are you ready i think this is what you
should do and if you're
if you need help in the next one i'll
show you how i would solve it and
hopefully that will get you started with
jupiter notebook trying to execute some
code yourself and see it's actually not
that difficult
i'm ready
i'm ready i hope you're ready take some
time and finish this off and i'll see
you in the next one
so in this one we're gonna solve the
project together i know it seems easy
and you might think well this is too
easy i know what he's doing but i still
advise you to do it especially if you
haven't done any programming before it
is crucial to get some experience okay
are you ready i'm ready let's get
started
so
again we jump straight down to step one
here and make your first print hello
world program right so what was that
remember the lesson it is basically to
tell the computer to print hello world
and if you read the docu if you read the
story you will also probably know that
it's not as simple as in python as
you've seen here so let's try to do it
right so you need the print statement
here
and
the print statement
will print every string you want so i
used double quotes before i'll just show
you you can also use single quotes and
it will do the same right hello world
comma world capitalized and exclamation
mark and how you execute it you can
either press run up here or you can
execute it by shift enter here you go
you see that wasn't that difficult was
it
okay step two use input to get the name
from the user and print hello and the
name from the user right
it seems doable let's try to do that
together so help me here the input from
the user right you use input but let's
just try to do it right input
what is your name
it's always good to ask and we can use
the comma here so
what we're doing here right now is we
want the name now prompt the name right
and now we need to print it so let's
hello comma
but there's a problem right we cannot
print the name now because we don't know
what they typed in so what you needed to
remember is to have a variable here we
can call it name
and then we say hello name here right
and now it should work right because the
key thing is the input here is stored in
the name right remember it executes
executes the right size right side first
and then it stores the value in the left
side of an equation here right so now we
have the value of name
and we can print it let's try it
together right are you excited let's try
shift enter boom what is your name
what is your name
i can't hear sorry i'm
i'll use my name right
hello
oh it works and i'm pretty sure it
worked for your name as well and i'm
pretty sure your name is not owner
because you're not from denmark are you
i'm in denmark actually this name is
also used in some other
countries but
mainly in denmark
step three use the input to get the name
from the user that was actually the same
thing we did up here print i like
capital letter and the name
of the user
so let's try to do that
so again we know we take name input
what is your
your name
question mark
and then we can print and i'm kind of
lazy here right so i just copy this one
here shift copy
insert it here and then comma name
so
if i just do like that it will just
output it as i type it in and we don't
want that we want it to be an upper so
we use this upper function here right so
now we're ready let's try it
name what is your name i'll put it in
all lowercase now because i'm lazy
and i say i like capital letters
okay
so far so good and that was actually it
right that wasn't that scary now we
actually solved the first simple project
together this is the most simple project
it will get more and more advanced along
the way so don't worry let's get to the
next lesson where we're going to learn
about variables
interesting what are variables and
different data types right
maybe you kind of
i i was kind of hinting at their
different data types so let's get into
that are you excited i am let's get
started
in the last lesson we learned how to
input an output and interact with the
user of the program in this one we're
gonna learn about variables and that
actually means we need to learn a bit
about data types okay so let's jump into
jupiter notebook and get started
variables what are variables actually
well a variable is a reserved memory
location what
well said differently if you think of
the
computer has a storage in memory right
and like your brain and it's kind of
like a variable that points as a
specific memory in your brain right ha
that's how it does and a computer is
similar right it just points a specific
area and it says i remember this piece
of information that's what a variable
does
a variable in python program gives a
data to the computer for processing
right as we already saw when we put the
input right we executed the input
statement well it put a value
the user input into a variable called
for instance a name right and this is
the location it points at and stores it
and this is what we can use to process
data inside a program without variables
programs would be pretty boring i would
say
so we need to cover some basic data
types and the basic ones that we are
going to cover are integers floats
strings and booleans there are actually
also complex numbers in python but other
than that these are the basic data types
complex numbers
if you don't know about them don't worry
about them you'll probably never use
them then
before we get started actually with
variables there's something called the
python variable name convention so why
is that so first of all if you just go
crazy and call your
variable names whatever you want
capitalize lower cases up cases and
how you like it
well the code becomes less readable for
other people that is kind of a
convention that we do our names of
variables are like this because then it
makes it easier for other people to
understand what is this this is a
variable because it's this structure
right so what are the rules right uh use
lowercase only
separate words with underscore these are
the main rules right a variable name in
python can be a single lowercase letter
that is like x and i here a lowercase
word word like cars here or a lowercase
words or lowercase words separated by
underscore like my cars right example of
variables like here so finally i just
want to mention that
other programming languages can have
other naming conver conventions right if
you're probably
familiar with something like
java for instance you know that they
love the first word is lowercase then
you separate the words with uppercase
words don't worry about it just keep to
this these are also described in the
ebook that i highly encourage you to
download
good so how do you declare variables
well basically we already know that
right so let's just try it here so let's
try a few ones here right so s
this is a string right
a
assigned to 2 and b assigned to
[Music]
2.3 or 4 it became okay so these are
variables right
so these are all storing things but as
you probably know here right these are
different things we're storing right
we're storing 2 here and 2.4 here and
this is a string so these are some of
the basic data types that we're going to
work with so how can you get a data type
well there's a built-in function called
type as you see here when i type
type it becomes green it signals that
this is a built-in function and you
shouldn't use it for anything else so if
you take the type of
s it will say well i'm a string right
if you take the type of a it will say
well i'm an int an int is an integer
right and if you take the type of b it
will say well i'm a float right let's
just leave the boolean out for a moment
and just start with these and the
boolean is a bit different right
i think booleans are often something
that people have a hard time to
understand the first time they see it
but and why they're useful
these booleans is a key thing actually
and you will know why later
so let's get started so integers right
so what are integers an integer is a
number which is a fraction a whole
number
examples of integers minus 23 minus 5 0
seventy eight one million nine hundred
eighty thousand three hundred fifties
are integers
examples of non-integer integers right
these are not integers three point one
four zero point zero zero one nine
thousand nine hundred ninety nine dot
nine
these are not integers right
so
let's just play around with that for a
moment right so imagine we have two
integers a
and
b
two and three doesn't matter what they
are these are integers right so let's
just ensure that they are type a
and you can actually comma separate here
sometimes you do that
and it will return that they are both
ins right
so a isn't it and b and z i would just
put it on the same line here to get them
both here right
don't worry about this details here
we're not really going to use it
so don't worry about it okay so what i
want to show you is actually you can add
two integers right so you take a plus
a plus b here you'll see that becomes
five obviously but what is a plus b well
we can actually try that by type a plus
e b
and you say that's an integer right so
put it more strictly you could say you
can assign a new variable a now c which
is equals a plus b and what is the type
of c well it is an integer right so what
we learned here is actually that when we
add two integers it adds like we know it
should add why do i say that well we'll
see later right not always add is adding
when it comes to strings it's something
different
and when we add two integers it becomes
an integer
so far so good let's move on to the next
data type
floats
what
a float is a number
with digits on both sides of a decimal
point this is in contrast to the integer
data type which houses an integer or a
whole number
example of floats 3.13 9.99 0.0001
note that float data in python can
represent a whole number like zero point
now one point zero or three point zero
right
so let's try the same thing here a
one point one
b
2.3 okay
and just to ensure that they are both
floats a
type b
float float right and
we expect the same here type a plus b
it's a float should we also just look at
what
a plus b is equal to well it's equal to
3.4 is that surprising 1.1
plus 2.3 no it's not surprising that's
correct
but i want to
i want to show a few things more here
and you can actually add in more cells
by adding the plus sign here so let's do
that here so for instance
imagine actually we have a variable
which is an integer called i
equals one right
type of i is integer right so now we
know
but what happens if we take type of i
plus b
or a
so we add here an integer and a float
let's see what happens well it becomes a
float
interesting right
so
i plus a
becomes 2.1 it calculates correct but in
the calculation it actually converts the
integer to a float so this is nice to
know right awesome so far so good
those were floats
now let's try to look at the math
functions here right so i just wanted to
share a few ones here because there are
lots of them but these are like built-in
and they're nice to know absolute the
power
and rounding right so we can return the
absolute value the absolute value is
just making positive to negatives and so
on
so not positive to negatives what i'm
talking about so abs
here if we take minus 2 here for
instance it returns 2. and then we have
the power here power here we can take 2
to the power 3 for instance that's 8
here as you see so 2 times 2 is 4 times
2 is 8 right so that's what the power
does it's pretty nice to have that in
one here i can actually reveal that you
can actually do that also by multiply
multiply 3 right so multiply multiply
means the power 2 as well right but
again what should you choose well it
depends also on you if you're familiar
with this one and you're comfortable
about that well use this one if you
don't understand this one well this one
is more readable what happens here right
and again readable readability counts
finally round i find that one very
interesting right so for instance if you
took round here and you take
two point or just a 3.14
and we round it by one what happens it
rounds the value right this is this is
really cool so it rounds a floating
point so let's take another one here so
let's take a float here and say 1.2
oh i can't have multiple
here we go
and uh
let's round this one here around here f
and what it does is write how many
digits should we keep i mean let's in
this case keep
four
and we see here it rounds
two three four five so two three four
four but it rounds the four to a five
because we have a five afterwards right
you know the rounding rules you do
awesome okay good
so far so good
so let's go to strings here and there
are just a few things i want to show
here as it already emphasized that we
talked about you can have a string with
double quotes and single quotes it
doesn't really matter right so let's
have two strings now here
i am a string and we have s2 here
double quotes i am another
string right
good so what happens if we add them
together s1 plus s2
well it concatenates them right so you
see here i am a string i am another
string right and
notice here there is no space between
here because there is no space between
here so that's what addition does i told
you addition does different things well
i'm
curious now right
aren't you i'm curious what happens with
we have an integer now a 1 here let's
execute that one and we take s1 and plus
a
well
we get something called type error
and this is kind of scary in the
beginning you see this red thing here
and something goes wrong and you don't
know what to do don't worry about it we
as a program you see this so many times
per day it will become
bread and butter for you to read these
things but in the beginning you don't
know what you to look for right so it
says type error so that means probably
something with the types there can only
concatenate string not int to string
right so we take a string and try to
concatenate it to an end right so it
says we can only concatenate string to
string so
what if i wanted to print out something
like that well this is actually where i
want to show you something else called
the formatted string right so i'll just
shortly introduce it there and a
formatted string is actually a string
where you put an f in front for format
so i am
a
string
with an
int here and how you can put it in is
use curly brackets and put the variable
in here which is called a here and then
you actually have that a is put in here
and you can have string something
afterwards here
and more text
right
so as you see here it outputs the one
here so now we can actually add an
integer inside the string here by using
this formatted here and formatted is
really really nice to use and you can
have multiple variables in here and you
can actually also make calculations in
here you can actually add 2 here and
then we'll execute this expression in
here and return that as a value here as
you see because a is 1 remember and
added 2 it's 3 1 plus two is three yeah
it is
[Laughter]
awesome right so here we have it right
this is amazing okay so these were just
a few things i wanted to show you and
don't worry about not remembering
everything here there's an e-book
did you download it you should
good
boolean
so booleans are kind of funny right it's
like
true and false booleans are used to
actually make program flows to decide
either do this or that right
but
often people say well the world is not
that black and white and that might be
true and computer programs are often
black and white and i'll say yeah but in
the end you are the same right for
instance does it rain and you say ah
it's not real rain it's like in between
i can't really answer that but in the
end either you take your umbrella or you
don't
right that's the programming so you make
some kind of decision based on that and
you can model that logic maybe you don't
model it as easy as you do in your brain
with a computer program but in the end
the outcome of it is black and white
either you took the umbrella or not
there's nothing in between you cannot
it's not quantum physics this year where
you are in between states and i'm not
into quantum physics i don't understand
that kind of stuff right we are still in
the old type of world without quantum
physics states where everything just
splatters around and you don't know
what's happening in this world good
so
when people criticize this black and
white thinking
false and true thinking well in the end
the outcome is always like that in your
life as well so what are the booleans
well it's true and it's
and it's false
so you can execute them right and they
return it and notice please note there's
a capital here it's important that you
use capital true and capital capital s t
and capital f in true and false
so
what can this do for you well it's like
you can assign that to a variable as
well right so you have a true variable
here you can execute that variable here
and it returns true but you can also
execute something and we'll get
back to more of that later so don't
worry but you can have an expression
like here right so 10 is greater than 5
right and we execute that and then b is
true
so as you see here what's happening here
again with an equal sign we execute this
right side of the equation 10 is larger
than 5 and that's true right so we
execute that and assign that to b so b
becomes true
if you want to play more around with
that you can use this bool function
again it becomes green here because it's
a built in function and you can try to
do your statements right so you can say
2 is larger than
4
that's false right and then you can
still play around with that right
i know right now we covered so many
things so i'm not going to put more on
that and again don't worry if you don't
remember it you're not supposed to
remember
all these things by heart
you will when you use it on a daily
basis but in the beginning don't worry
we've all been there we'll you need to
look up everything we're on as a
programmer with more experience it's
just different things you look ups but
you always be looking things up because
you don't remember everything by heart
okay
so don't worry about it so what did we
cover well basically we covered what
variables are
the basic data types integer floats
strings and booleans and
now you should be ready to make the
project i'll introduce the project in
the next one and are you excited i am so
let's get started
making projects is the best way to learn
you need to get your fingers dirty you
need to type in code even though you
think you remember stuff often it turns
out it's a bit more complex than you
think because programmers no programs
need to be 100 accurate because a
computer doesn't have any guesswork it
cannot guess what you're thinking you
need to type it in 100 accurate
so that's why it's very important to try
these things because it will ensure you
have all the details correct so
are you excited let's introduce this
project here and in the next one i'm
going to show you how i would solve it
so in this one
we need to do some basic calculations we
need to be in a stage where we do stuff
we know
so we should have learned anything so
so we don't use anything we don't know
right
good
so the goal is to
use jupiter notebook as a massive
calculator basically and understand how
cells keep track of calculations
so the description is create some
formulas and have some fun with basic
math okay
are you ready so the first one here so
again we have some steps in this one
here and step one is making a conversion
from kilograms to pounds
so we use this formula here for pounds
that it is kilograms multiplied by
2.2046 that's very accurate right
so
as we already know this right this was a
float here at least and then multiplying
with a
and if you put an integer in the
kilograms with a float it will become a
float as you can check
so make the cell and use some variables
right so you need to use variables in
this one this is also a goal right so
that one are you comfortable try it out
step two
make calculations of circumference of a
circle
the cell should be defined
should define the radius right so we
need to
make a radius and then we use the
following formula that the circumference
is 2 times pi times r and we use
pi to be 3.14
and use a variable name pi right
it's difficult to use this character the
pi character here in programming you
will know that
and
then let the cell return the
circumference right so are you
comfortable about that so you need to
make a variable you need to make this
calculation you need to have another
variable called pi probably create it
before you
create this formula here and then you
should return the circumference okay
ready good
step three
assign a variable to any number
this is kind of a fun thing actually any
number your choice and my advice hmm
take your favorite number okay it should
be an integer though okay
multiply the number by three
so you do that on another line
add six to the result from bullet two
another line
divide the number by three another line
subtract the number from bullet one the
one you put here right
and from the answer
from bullet four right so you just take
this number and subtract one
and then the answer is two what the
answer is two what yeah i know this is
kind of fun right so try it out and try
to see if you can figure out why this is
the case right why does it become two
it's just fun right just having fun with
math okay that's it i'm excited are you
excited you should be let's get started
let's let's do this okay first you do it
on your own and when you get stuck let's
continue in the next lesson or the next
lesson the next part where i'm going to
show you how i would solve these three
steps okay are you ready i'm ready
so
did you try it yourself you should don't
cheat yourself this is an opportunity
where you work with it first and then
you see how i would solve it even though
you get stuck and you get frustrated we
all been there trust me i've been
sitting with problems i couldn't solve
and you're just like how do you do that
when then you see the solution just like
wow is it that simple and he's like how
couldn't i figure that one out but trust
me over time you will in the beginning
everything is new and everything is
complex but it will become easier i
promise you that the only thing you have
to do is to keep on working and actually
the more time you spend trying yourself
even though you don't succeed
the more you learn i actually realized
that the more time i spent trying to
solve something and then seeing the
solution the more i learned from that
that means if i got the solution
immediately without trying myself i
would forget it the next day it will be
all gone but if i spent hours maybe
sometimes days trying to solve something
and then i got the solution i would
remember it for i would say almost
forever
wow
now that's an incentive for trying out
things yourself let's get started though
i hope you tried it i hope you spent
some time and now let's get started
so remember
we just jumped down to step one and
let's get started with that one so make
a conversion from kilograms to pounds
right we have the formula here and we
should use variables right so let's make
a kilogram variable here and let's just
start with the fun kilograms of four
right so
now we actually have the formula here so
basically what we need to do is to type
that in actually so lbs equals kilograms
actually there's a misspelling here i
can see here it says kb it should be k
k g here i apologize for that it will
probably be corrected when you do it
okay
times 2.2046
so again remember multiplication is by
this asterisk here right and not by nx
and then we should return it lbs right
so here we have it right so this is
actually how you do it and you then you
can change actually we put one kilo here
how much should that be well one kilo is
by the definition 2.2046
pounds right and then you can add up
here and you see here the variable is a
float here even though you put an
integer here right
that's how it converts okay good step
one done
step two are you ready
make calculations
to find the circumference of a circle
the cell should define the radius let's
just start doing that right so for short
we just use r so the radius of our
circle is 2.5
how do i know that i just chose that
value myself you can choose your value
if you want
good that was one
calculate the circumference by the
formula
c equals 2 times pi times r
we don't have pi but it says actually to
define pi air to be
3.14 so let's do that first
3.14 and then we take the formula c
times
2
times
pi times r and let's return the
circumference as it says in the last
bullet execute
wow
15.7001
so
often you will see these small
discrepancies with a long
row of zeros and one or a long row of
nines and this is due to
programming that actually sometimes
it does some rounding along the way and
it becomes a bit
inaccurate and this is a common problem
and that's not a common problem it's
that's how programming is it's not 100
accurate as you know it but most of the
time you won't notice like this one here
it looks fine but as you saw before you
can get something like that don't worry
about it it's
how it is
so
that was
circumvents step three let's get started
here
so let's try this one assign a variable
to any number
i'll take one number
number
23
it's not gonna guess my 23. okay
so now we have assigned a variable 223
multiply the number by three
okay
so number
and then we need to store that number
and multiply that by three right so we
take the number here we take the number
and multiply by three right so we take
this number which it's trying to guess
and then
are trying to do something
and
multiply that by 3 right so we have a
new
level here
so add 6
add 6 to the result from bullet two
okay number
num
number plus six okay perfect
divide this number by three okay
number
equals num
number divided by three
okay
subtract the number from bullet one
from the answer and pull it to four so
okay
number
number minus 23 right
and then it says the answer is two so
let's get that out here
number two
interesting and uh
i'll leave the fun for you to figure out
why this is the case
but uh i'll just try to change this
number to something else
it's 56 it's
oh then i also have to change it down
here 56.
i just got confused there a moment right
and you see it becomes two again right
so for the curious one it says it says
2.0 while everything here seems to be
integers right why is that
well actually the division here
it converts
it to a float so when you divide
something right
uh even you you even though you divide
by an integer it will return a float so
that's why it's a float down here so
everything up until here are integers
but here it becomes float and then it
continues as a float
so now you know okay
wasn't this crazy so
this is amazing right
are you ready for the next step i am so
let's continue our journey together see
you in a moment
in the last lesson we learned about data
types integers floats strings booleans
right but sometimes you need to convert
a data types
for instance say you have a string right
but you know there's an integer inside
that string how do you convert that to a
integer right that string with the
integer inside to an integer how do you
do that well don't worry obviously other
people have the same problem let's try
to dive into a specific problem and try
to solve that so let's jump to jupyter
notebook and get started in this lesson
here we'll look at type conventions
we'll learn a bit more about formatted
strings to become more comfortable about
that and
we know that types are not the same we
already know that but we'll re-look at
that again
and then we'll learn how to convert them
within float and string these are
built-in functions that we are familiar
with
we're already familiar with the concept
of
built-in functions and these are also
good to have in your repository
so
let's get started with a specific
problem as you all we already know we
know how to query the person for the
name
what is your name
question mark and we put a space here
and we want to assign that to a variable
let's call it name
and now also to get more comfortable
about formatted string let's use that
immediately so print
format string with this f in front here
right so we have a formatted string and
then you have the quotes afterwards and
just say hello
hello
and we'll put the variable name here
name and exclamation mark what that does
is it prints hello and whatever you
typed in an exclamation mark right after
it without no space okay so let's assume
we want to
figure out what year the person was born
right so let's say we input
what
e
what is your
birth year right
and to do that we probably want some
birth
year variable here and then we'll print
in the first case here we'll just print
formatted again formatted here
u are born in or you were born
we're born in and then
here we put the birth year in here
dot here so let's just try this one
first and then i'll try to modify it and
then we'll get a problem later this one
should work out fine so what is your
name
i'll just put my name here hello roona
and what is your birth year well 1991
right you were born in 1991 i love that
this machine makes me so happy good
perfect well what is the problem here
nothing but let's try to tweak our
program a tiny bit let's actually copy
this one here and put it down in the
cell here
and then instead we want to
print the age right so we make a formula
string is
so let's say u
r
let me take the
current year
it's 21 when i'm making these recordings
maybe you're watching this later
but and then we subtract that you are
years old
right let's try to execute that so
as we already saw you can make a
calculation inside these
curly brackets here so it will take 2021
and minus in my case 1991 right and
print how many years i am or i suppose
it will
so what is your name it is owner
what is your birth year 1991 and boom
something happened
it didn't go well did it
so again as i say when we see these
things don't get don't go into panic i'm
here i'll guide you don't worry about it
but let's read it sometimes
right read the first one right type
error so that's the exception that's
happening here right so right to bottom
here
unsupported operand type
for in and string
so it points actually on the line the
line four we don't have line numbers
here but it says print you are this one
here old right so it's this line here
the last line here we change right and
it says something with int and string ah
so this one is an into right
what is birth year
type
birth year
it's a string
because what input does here
what input does here it actually reads
the string from standard input read as
string from standard input right
so that basically means it returns a
string
and we want to convert that so how can
we do that well there were some hints in
the top of this something convert with
int float and string
my best guess would be int what does int
does
do
well
it says convert
a number or string
to an integer
perfect
so let's take birth here
convert it to an integer okay let's
actually just keep this code up here and
copy it down here
and then add that to the line here that
birth year
equals end of birth year right so what
we do here we add an additional line
here which takes the input here the
string birth year and convert it to
birth year let's see if that works
rooney
1991 enter you are 30 years old i told
you i was 30 right you didn't believe me
but even the computer says i'm 30 now
perfect i love it
perfect so
this is the simple way of converting a
string to an integer so let's just play
around with that right so int
we had already here that we could
convert
integers like that right so
what happens if it's not an integer like
if this is r 4 for instance well it will
say
you cannot convert here value error blah
blah blah right so obviously if it's not
a clean integer if there's something
else in it well it doesn't know what to
do and even with a space no it can it
actually manages space that's pretty
nice actually
actually i didn't know that hahaha i
learn along with you nice
perfect right
good
and obviously you can have negatives as
well right so now we know how to convert
convert a integer
from a string to an integer
and
now you're probably curious there's
something called float here well we can
probably do the same here right so if we
take a float here and we have a string
here
we can have 2 here and it returns 2.0 as
you see here
and
you can also have a float here
3.14 and it will return 3.14 just
similar to string right and you can
actually
take a float and have an integer here
and it will return it as a float so it
takes the input just like integer it
didn't say it has to be a string but it
takes whatever it takes and tries to
convert it to a float just like integer
right
perfect
what about the last one the string here
why is that so awesome right
okay let's
dive into it let's have a string here
for instance
see the problem here string we know the
problem this is a string
let's have i for integer and this is an
integer 7
and we know if we take s plus
i we have a problem right
but what does string do what do you
think string does right
well it says down here
it will convert
object into a string create a new string
object from the given object right so
let's just try to actually say s
plus
i here right so we take this the
original here so we have the i as an
integer and convert it to a string let's
try that boom
this is a string seven right so it adds
seven immediately after and remember
again we didn't have any space after the
string so there's no space between here
you need to take care of all these
things right
so this is actually basically what you
can do with conversion right and now you
know
how to convert anything
or try to int
and if it doesn't you get a failure
try to convert something to float
if it doesn't work a failure
try to convert it to a string if it
doesn't work failure right
so these are really really handy so
let's try to use that in the next
project of hours are you excited i am
let's get started see you in a moment
in this one i'm going to introduce you
to the project and we're going to make
an interactive calculator remember the
last project we made some calculations
but now we want input from the user
because now we know how to convert them
right um
exciting let's get started so again we
are in steps later the projects will be
more complex and we'll have like one
thing to do but in this one we do three
smaller things right
so the goal is to multiply two numbers
it seems easy but it's going to be input
from the user of course and then we'll
try to calculate
how many days it were since you were
born and then make a tip calculator so
let's get started the first one
input a value from the user and keep it
in variable a
input another value for user
and keep it in variable b and print the
product of the two values a times b
right
awesome
and
step two
input how many years the user is right
convert the years to an integer
calculate how many days hours minutes
and seconds since the user is born right
perfect
straightforward right and step three
here we will make a tip calculator input
the price convert the price to an
integer
input the percentage the user wants to
tip right
and convert the percentage to another
integer
calculate the tip by the following
formula tip
is equal to price times percentage tip
divided by two round the tip to two
digits so we also use the round function
right print the tip bonus adjust the
code
to
take float input off percentage tip
right so instead of using this as an
integer you see here you can also do it
with a float instead for instance if you
want to tip not 13 but 13.3 percentage
you can do that awesome right
uh it's just if you want to play along
with it so that's a bonus step if you
want to do that awesome are you ready
i'm ready let's get started now
you should get started first and then
when you have done your solution let's
try to do it together to see how you can
do it differently and don't worry if you
do it different than i do it's normal
it's not just one way to do it if you
come with the rec correct solution it
doesn't matter okay
as long as you keep the python sent
remember readability simple and all that
kind of stuff see you in a moment
are you ready i think you are so let's
get started i'm sure i'm all confident
that you tried it yourself and you want
to see how i would do it okay so are you
ready i'm ready so let's jump
immediately down to step one and see
what we can do well we need to input a
value from the user and keep it in
variable a
let's do that
a equals
input
input i need to spellcorrect here
input a
that's it
input another value from the user and
keep it in variable b okay input
[Music]
input b
okay
print the product of the two
values a times b let's try to do that
print should we do it and
let's do it formatted here and we'll say
a
times b
is
[Music]
and let's do oh let's do here a times b
let's see what happens
are you ready i am
boom a
a number a number a number
five
b
three
no
we failed don't worry about it it
happens all the time
it says
tie bearer right these are these types
they're so annoying right but you'll
love them in a moment
can't multiply sequence by non-end of
type string oh yeah remember input
returns string so a is a string b is a
string right and we cannot multiply that
so we need to convert it so you can do
it in steps or directly let's do it in
steps here a equals int a
b equals int
b
let's try to run again are you ready
i am
a
five
b
three
fifteen so
five times three is fifteen perfect i
like it i feel comfortable again do you
i know we need to learn along the way
making mistakes is the best way to learn
if you don't make mistakes you will
never realize if you fully understood it
if you make mistakes there will be
something you think why is it that that
mistake happened okay
step two
how many years the user is input right
so
years let's just call it years
or age let's call it h
input
how many
years are you question mark
convert the years to an integer
h equals int age now it actually helps
you right
calculate how many days hours minutes
and seconds the user
since the user is born right
uh u
u r
let's do a float here an h times
if we just assume we're there 365 days
then we have you are
days old right let's try to execute that
actually and let's put the my age 30 is
still 30 years old
and
what happened here right so what we did
here it's good to make mistakes right it
says age times here and you think it
doesn't complain about anything so
what's wrong here why doesn't it print
number of days right because we forgot
the format string here right
awesome lesson learned 30 and it'll say
10 950 days
whoa i passed the 10 000 mark
okay and we can continue this journey
now right so
i'll do something ugly and copy and
paste you should always worry about that
and let's copy one more isn't that what
we need to do so if we multiply days by
24 we have hours right
hours
and we multiply
24 by 60 we have minutes
[Music]
and if we multiply
2460 by 60 we have
seconds
okay right you see here right so first
we got the number of days there are 365
days per year or we just i know no leap
years or anything
and then in this one when we get hours
we multiply that number by 24 then we
have the number of hours because there
are 24 hours per day right and then we
take that number and multiply that by 60
because there's 60 minutes per hour and
then we take that number and multiply by
60 because there are 60 seconds per
minute right
let's try it
boom
so in my case i'm 10 000 over 10 000
days i'm more than 250 000 hours old oh
i don't wanna how much is this 15 over
15 million minutes
and how much is this it's almost one
billion seconds
whoa
whoa
did you cross the one billion mark did
you
whoa that's crazy did that count right i
think so don't do this at home kids this
is dangerous
so in step three here we need to make a
tip calculator and let's just dive into
it and follow along here right so input
the tip price uh
price not the surprise input the price
input
yeah let's just write input
input
price
colon what to do convert the price
convert the price to an integer let's do
that
that's always good
int
price
[Music]
and uh
input the percentages they use want to
tip
percentage
tip
and input
how much
do you
want to
tip
percentage question mark
and we need to it says here convert
percentage to an integer
percentage tip equals int
hope
percentage tip
and calculate the tip by the four
following formula let's do that
tip equals
price
times percentage tip divided by 100 and
then it says
round the tip to two digits
tip equals round
tip comma two
and then it says printer tip
tip let's just make format the string
here tip so we need the f in front of
here are you ready let's just dive into
it
input price let's say i tipped one
hundred dollars
how much do you want to tip let's say 15
percent
your chip 15 that's pretty nice
let's make something a bit more advanced
230 dollars i want to tip
17 16
17
[Music]
39.1
wow right
okay so the bonus adjust the code to
take a float input in percentage tip so
the problem right now if i added a float
here you see here if it's at 10.3
percent here it will get a value error
because this is not a
integer anymore right because we convert
it to
an integer on this line is this this
line here right
and how do you change that well you just
put it to a float here
and execute again and then say 110.3
for the game and your tip is 14 10.4 now
right
you should do the math
awesome isn't that crazy i think so i
like it
and this is amazing right and actually
the next thing is going to be amazing
we're going to learn about flows in
programs how can you make flows right
now we always have the same flow in the
program you do the same thing and no
matter what it will always do the same
thing in the same order this is like a
basic recipe right in the kitchen you do
this things in the same order now
can we do something in one case and do
something in another case well we can
and we will learn how to do that so are
you excited i'm excited let's get
started
so far we covered how you can take input
how you can convert that input and make
awesome calculations based on that but
there's a piece missing right you don't
know
how to make a flow in a program what do
i mean well so far you're following the
same recipe no matter how the input from
the user is
but in reality you know you do things
based on input right for instance if
it's raining you might be bringing your
umbrella outside right
so if it's not raining you don't right
you want to be able to express that kind
of logic in your programming and this is
where program flows come into the
picture
in this lesson we will learn about if
statements yes and they are awesome they
are connected to the booleans remember
those those are a bit awkward true or
false right
we're going to dive into those and see
how they can help you making flows and
programs so are you excited i am let's
get started so inside jupiter notebook
we have about program flows it says here
the goal is to learn about if statements
use if statements to make program
perform based on variables right
so
that's the goal of this listener so
let's look at the boolean expressions
again and we need to master a lot of
them down there or some of them at least
so
if you have variables a and b you can
make equality test as a boolean and you
notice that there are two equal signs
where a single equal signs would assign
something 2 will evaluate if a equals b
not equals it is with an exclamation
mark and an equal sign
and a less than it's a less than sign
less than or equal less than equal
a greater than greater mean sign greater
than or equal greater or equal sign that
all makes sense kind of i think so
at least i think so
so let's try some examples to see how
this
gibberish here becomes code down here
let's turn it into some coding examples
before i
so we can get ready for the project
right that's all we want to do we want
some programming projects so let's get
started
so let's say 8 10
and b 12.
okay
so
we need to know these if statements up
here that we talked about how can we use
those so the syntax is as follow if
then we put a boolean expression let's
say a equal equals b
and then a colon
so that's the syntax and when i type
enter something happened here you see
here my
computer didn't put it all the way out
here it didn't indented the code as it's
called because what's intended
afterwards will be executed if this sign
is true so let's just say here let's
a
a
equals
equals b here right
so let's execute it and we see here
nothing happened here because a is not
equal to b and it will only execute this
if this is true right so let's try to
change this value actually and say boom
a equals b now right you see because
this is the case right
awesome
so this way here you can have all kinds
of expressions here we can have
something
let's just take a and b equals 12 again
here and say if a is
less than b then print
a is less than b right
and it will execute that right but if we
change obviously b to b8 here it will
not print anything right it makes sense
right so now we can
make model programs that do something in
one case if that case is true
that leads us to the next thing
sometimes you don't want to do one thing
in one case and another thing in another
one right okay let's try to do that
so let's say a equals
10
b equals 12.
so if a is
greater than
b in print
a is
greater than b
but
then let's do an else what does else do
let's say
a is not greater than b okay let's try
to execute this
so what we say here is
a is not greater than b that means we
execute the
else part here why is that because a is
10 and 10 is not greater than b so this
is why this
command line here was executed
if we changed a to let's say 15 then it
would actually say a is greater than b
right
just a warning here right so this is
computers are very pedantic so if a is
equals to 12 as b does then it will say
a is not greater than b right
because in this case we don't have this
case here it's not equal it's
you know again up here
it distinguishes between less and less
than equal and greater and greater than
equal right
both areas so these are two specific
cases right
awesome so if i change this one to equal
sign as well right it will say a is
greater than b and this is not true
actually right so let's just remove that
again
awesome isn't that great i think so
uh
perfect we can also have multiple flows
like this one here so let's say a again
10 b equals 12. so if a
greater than b we'll say print
a is greater than b
then we have something called alif
it's called else if but it's called alif
in the python and let's say a equal
equals b and then say print
a is
equal to
b
not b b
and then we're going to have the else in
the end here and then we'll print
a is
less
than b right
perfect so a is less than b here and we
can try equals
and we can try
13 here so a is greater than b right so
you see how you can model flow now right
based on this the only one we didn't
really try where this not equals let's
just try to do that
a equals 10 b equals 12.
if a not equals b then
a does not equal
b right and this is the case here and if
they were equal
obviously they were equal right so see
what kind of power this gives us right
now we can actually do a lot of things
and in the project we'll do a car
insurance
company and we'll do calculations based
on that it will be amazing so if you're
ready i think you should dive into that
because that's where we really learned
something is doing something and it's
it's going to be amazing actually so our
first real
more real life big project so let's get
started with that see you in a moment
are you ready for this project it's
gonna be amazing it's our first kind of
real project we'll see some diagrams and
try to turn that diagram into code are
you ready i think so let's get started
this is a car insurance it's very
simplified and to be honest i don't know
if that's how they calculate it it's
just to simplify have an example that's
more real life
so the goal is to in to car insurance to
calculate a car insurance based on the
following decision tree right what is a
decision tree well it will make sense in
a moment so the decision tree takes kind
of input from the top first you'll
figure out what the gender is if it's
male or female and based on that you'll
go down let's say for instance you're
male and you're
you're are 30 years old are male and 30
years old right
then i'll go this way and my age is
is
greater than 26 right you need to say
that the age here is greater than 26 and
then i have to pay 99 of something right
if i was less than 26 or equal and it
would be 23 and if i was female then
it's depending on what type of car you
have if it's a sports car or not then if
it's a sports car you'll pay 21 and if
not you'll be
paying 10 so you can see there are
different decisions based on if you're
male or female if you're male it depends
on your age if you're female it depends
of
depends on if you
ride a sports car or not right so
description this is a fictional car
insurance company called car crash
they calculate their yearly insurance
price based on the current market price
of the car so given the market price of
the car market price
then the year the insurance price is
nine percent of the market price if the
male is above 26 years old right
as we see in the decision tree we have
four possible price calculations create
a program which takes input based on
that calculate the correct price for all
four cases right so this looks scary try
it out yourself then i'll help you in a
moment okay are you ready i am let's get
started
now this is exciting did you try it out
yourself please let me know in the
comments how it went for you and what
was the problems i love to hear
what are you struggling with and what's
difficult to understand but now let's
see how you
one way to solve it how i would solve it
on this level where we at now right
remember we will learn more maybe later
we would solve it in a different way so
let's get started
so this is the decision tree that we are
working with right so in the beginning
we need to figure out if it's male or
female and based on that if it's a male
if it's less or equal 26 or not if it's
a female whether she
drives a sports car or not right
so how do you structure this right we
have if statements right so how do you
do that right
again i assume we
know all the text here we
should have a market price but let's
leave the market price out for now and
try to figure out how we model this
decision tree here so a good advice is
not to over complicate things and try it
along the way right
so let's just look at this first one and
ensure we are doing this correct right
so the first one is figure out if we
have a male or female that we are
interacting with right so how do you do
that well
we should ask them for their gender
right so input
what is your gender
male slash female right so they know
they should answer in that kind right
perfect so when they enter that it can
be either
mrf right that's what we're looking for
and
a good thing is always to
convert the input to a specific type so
we don't have to take all the cases
lowercase and uppercase and so forth
so for this one we will just immediately
put it to uppercase so gender upper
right
so now for the if statement so if
gender equals equals male right so we
know it's uppercase so we can just do it
as male right
then let's just see if it works print
male
[Music]
elif
gender
equals equals female so why would i want
not just make else here well you could
do that but sometimes people put wrong
formatted input there right so
female
and then we can say else
unknown
or wrong format let's just say wrong
format wrong format
okay
so this is basically trying to capture
the first step here if it's male or
female so let's just focus on that and
let's try to evaluate that
so what is your gender well male male it
works what is your gender capital m it
also works obviously
what is your gender female it works
what is your
gender okay let's say g
well wrong format right
so
this is
one way because then we don't falsely
assume it's a female if we write
something wrong right so
just
for that so let's go to the next one
right so now we have a place in the code
where we actually know we are have a
male then we need to figure out if it's
he's less or equal to 26 or not right
okay let's do that
so
let's take h here and then set input
input
what is your age
and we convert that
to an integer immediately and then we
say if age less equals 26
and then we say print
less
less equal
e equal
to
26
and else
more than 26 okay so let's try to
execute that we take male
how old
what's your age 20 less than equal to 26
let's try again male
30
you're more than 26 right so now we
actually have that part of the decision
tree so let's do the final part here
with the female and so the female we
needed to prompt if it's a sports car or
not right
okay so let's say
a car
type for instance and let's say input
[Music]
what is your car type right and we are
interested in sport
sports or non-sports
car right
perfect so if let's also just convert
that type
to
we don't have to put upper but we did
that
card type
upper
upper
and then say if
car
type equals equals
sports now we need to put in a number
then we say print
sport car
sports car
else
non-sport
sport
sports car it goes well right perfect so
let's try this one
we are female
what the type of car let's just take
sports
sports car right let me try again
uh female and we say false we say
none
okay non-sports car right
so here you have it so now we actually
have all the cases here and so we have
built this decision tree now it's time
to build the logic or the calculations
based on that so if you remember here we
need to find the market price and based
on the market price we should calculate
the
yearly payment of the percentage on
these areas so what we can do is
actually we can have a variable called
percentage and
let's do that
so let's say percentage
of 26 here that was then it was 23
and uh this one here
percentage it is 9
and if it was oops
a female sports car percentage
it was what was
21
and non-sports car female
percentage
10.
and this one here
percentage
let's just put it
to none okay
so the second thing is we need to know
the market price so
you can do that in different ways you
can ask before or after for instance in
our scenario here let's just keep the
structure of program first to figure out
our male female what age and then we ask
for the market price of the car right
market
price
input
what is the market
price of your
car
okay
and based on that we can actually
calculate the insurance insurance
insurance offer which is the market
price
market
okay it was a g their market price
times percentage
divided by 100 right then you get the
market price
and the formatted prank print you
okay
let's just say insurance ensure
insure
rents offer
[Music]
here an insurance offer here okay let's
try that so let's do the story here mail
25
and the price is let's just say 10 000.
and then we have an uh
wrong here because what did why did i
forget actually i did forget that it is
to convert this one to an integer right
so it says here type error i'm really
fast to read this maybe it wouldn't be
so fast right but it says unsupported
operand type division string and it
right so i know something went wrong
with a string here and that's the one
here right
so what we needed to do is
convert market
price to an int
int
market price and again i mean
making mistakes like this is common
every programmer knows it
and uh
it happens all the time
so here's the offer right it's a pretty
expensive offer 2 300 for insurance per
year but you know he's in the wrong
group so let's just take me
i don't like this insurance company
right male 1
30 i'm still 30 and if i had a car for
10 000 it's like 900 per year i still
find it quite expensive but you know
it's just an example here right and then
you can play around with that right but
the key thing is now you learn how to
turn this
decision tree into some code isn't that
amazing i mean come on you can make
the most advanced decision trees now but
based on that you can mix the logic
behind that you can make a program flow
behind that
wow that's powerful and that's amazing i
hope you enjoyed that because that's
amazing so that was the end of this
lesson here and then in the next one
we're gonna dive into randomness to
create use randomness to create actually
awesome games it's going to be awesome
so see you in a moment bye-bye
so far we covered a lot we can actually
turn decision diagrams into code now
isn't that amazing i mean now you can
make their most crazy structures into
programs right but they're still missing
something right
imagine you want to make a game right
and
well what is it you need in the game
what is it that makes it exciting that
is something unexpected right something
you cannot predict and that's where
randomness coming into the picture right
imagine you need to
to model a die right you roll a die
right it can be a number between one and
six not between including one and six
right
and uh
how would you model that with all you
know right now well you couldn't you
could make a function called no you
couldn't actually but that's where
randomness comes into the picture giving
some you know unexpected something you
cannot predict this makes games funny
right it makes it possible to
[Music]
make it interactive right
good let's get started so in jupyter
notebook the only thing we need to learn
in this lesson here is random it's a
library and we'll just import it and
that will teach you how to import a
library it's kind of like we did with a
sin of python we it right but it's not
the sin of
python is not really a library because
it's no functionality just prints the
sin of python right so what we want to
do here is import the random library and
use it and see how it what it is and
what it does so let's do that so a
library is like a collection of a lot of
information and if you're more curious
about that please check the e-book it's
also described what the library is in
that one for this lecture here okay
so let's get started here so import
random so this will give access to the
random library which does a lot of
random stuff
so we imported it so nothing happens
when you import it but it just makes
available that you can access a random
thing here how can you see that you can
say random dot and then you can
put
space
space after here now space tab what's
meaning and then you see a lot of
functionality we're not going to cover
all that we're just going to cover a few
of them to see what it does right
so
a great one is randint and what does it
do let's look at the documentation
randint
well
quite simple returns random integer in
range one to b no not one a to b
including both end points right these
are often important right does it
include or doesn't it include the
endpoints these include right so for
instance if we wanted to model a die
it can be one to six because it will
return a number between one and six both
inclusive right so roll it
three roll it six roll it six again roll
it one right so here you have it right
now we can
model a die just with this one here
right so
basically if we didn't import this
random we wouldn't be able to write a
random dot random that will not work
right that should be clear to you right
so that's
that's the purpose of this import stand
statement it imports
the library
two others that are useful it's like the
random uniform
and what the uniform does is as you see
here
it gets a random number in the range a
to b
or a to b
i mean it's it's if it includes b or not
it depending on the rounding right so
it's it might do it might not do right
so often you need the uniform random
number it can be between zero and one
right so as you see here it will get you
some crazy
number here right and it changes and
it's between zero and one zero is
inclusive and one might be inclusive
right
but as you see here it might not really
matter it will be so unlikely you hit
zero or one anyhow and if you're into
statistics there's also the possibility
to get a norm
gaussian or normal distribution and you
get that by
random
cause
and you see here
it goes young distribution right
and yeah i'll just take 0 and 1 here and
then you can use that and as you see
here you get
random numbers based on that okay
so basically this is all we need to get
started with the project
later actually we're gonna add
use more functionality from this random
library but uh as we don't have a list
learned about lists yet it doesn't make
sense to introduce it now right so when
later we learn about lists i'm gonna
show you some more tricks with this
random library but for now we can
actually start creating some awesome
games it's gonna be really fun so let's
do that so see you in a moment
so are you ready for another awesome
project so let's get started right so
we're gonna actually make two small
games here and the first one is a math
game and the other one is rock scissor
or rock paper scissor game if you don't
know them don't worry they are kind of
described here so the first
simple game will be
create a math game right
so the first thing you need to do is
import random because you need the
randomness right
so
what you want to do here is actually to
get a random integer in the range one to
ten and then you get another random
integer from one to ten and then you
prompt the user to what is integers
multiply right so you ask them if for
instance the first one was three and the
second one was seven then you ask what
is three times seven
and then you print correct if it's
correct and
not correct if it is
and print the correct answer in that
case okay that makes sense right so this
is kind of like a math game it's good
for training your math skills i mean at
least
my math
skills can be corrected
at least my math skills could be a bit
better i guess it's been some years
since i was calculating my head so i'm
gonna be excited to see in the next one
if i can answer this fast i'll
i assume you challenge me right
good and then for this one here rock
paper scissor game right so i don't know
if you know it right so it's like this
rock scissor paper and then it's like
you go for one of them it's like scissor
paper and brock right and you don't
stand two guys or how many you are and
it's like rock scissor paper oh i win
rock scissor paper i win this one is
rock all the time right
apparently it's not so smart that's my
left hand it's not so smart okay good so
you need to implement that right and uh
actually there's no description because
it's gonna be you know a bit tricky
right
obviously
there's some randomness here right so
how do you do that right so
at least the computer
should enter something right rocks is a
paper and so should user user do right
so how do you model that how do you do
that it might be a bit tricky and i left
it without hints here for you to think
about it because one of the best way to
learn programming is to think about it
and see how would i model this how would
i do that right and remember you can
only use what you learned so far so it's
nothing advanced here and maybe later
if you had to redo this you would do it
in a different way but i encourage you
to sit down and think about it maybe you
don't come up with a solution but that's
why i'm here to help you to show how i
would solve it with the current
competencies we have at this stage
and
yeah but think about it and it actually
this will help you learn a lot so sit
down maybe you start with paper in your
hand and write down some thoughts how
would i do this and see what you come up
with and then try to type in some code
if it doesn't work don't worry about it
this is a learning journey
you're not expected to know all these
things at first stage but
sitting thinking having problems that
are difficult will teach you the most
okay
so are you excited i am so let's get
started
so here in the first one we're actually
building upon most of the things we know
except the randomness right
but let's get started with this one and
this is like a step by step guide here
right so first import random
let's do that
import random
executed
okay
get a random integer from the range one
to ten both inclusive remember how that
was so let's just call it a
random
randint
one to ten right and if you're forget if
it's inclusive or not the documentation
here tells it to you right so you get
the documentation by shift tab inside
here and then you say return random
integer and the range a to b including
both endpoints right that's all that was
the first one and then b you should get
another integer right
random
randint 1 to 10. perfect
and now
prompt the user for
for what the two integers multiplied is
right that was
so
answer
input
and we can make a formatted string here
and what is
i'm going to take
a
times
b
okay and what what should we do with the
answer
yes that's right you're good we should
convert it right answer
hint
answer right
and
then we need to check right this is
where the if statement comes into the
picture if
answer equals equals a times b and then
print what should we do print correct
correct
let's just do a formatted string here
a
times
b is
answer right
else
format
in correct
a
[Music]
times
b
is
a times b right
perfect
here we go
let's try to run it
are you ready to humiliate me
are you
okay let's let's see how it goes eight
times same sixty-four
yes correct
two times four it's eight perfect
two times ten is twenty perfect
oh
uh 21
yes
4 times 6 times 4 is 24
awesome
7 times 9 is
63 holy smokes it's difficult
seven times nine is 63 second times it's
easier
and uh 40 okay so you get the you get
the picture so at least i managed
at some stage i mean if you're really
really into this game you can also put a
timing element to see how long people
are
uh we will not cover that in this one
but there's another library called time
which you can include and then you can
actually time how fast people are and
put a score based on that
it's just a
way to make it more advanced
so for the next one here
we have rock
rock scissor paper rock paper scissors
it's called but here's the rock but this
is what it beats right so
we have that scissor beats paper and
paper beats rock and rock beats
scissor right this is how it is right so
what we need to do is actually we need
to prompt the user first for an input
right so how would you model this right
so
so again when you're modeling you it's
it's nice to say okay what will the
computer do the computer is can return
an integer one two three for instance so
what if we prompt the user to say one
two or three somehow right instead of
saying rock scissors paper we could also
just ask them to write rock scissor
paper but on the other hand we know we
need to convert something and what we
convert yeah that's up to you so
my suggestion is let's convert the user
input right
so let's just make a print statement
here and it says
enter
choice
[Music]
one
rock
two
paper
three
scissor right
perfect
no we just keep it here right and then
we'll have choice here
to be input
and let's just take choice
choice
one
two three
i don't know if this is nice to write
like that but we did i did
okay
perfect so far so good so now we enter
something and we need probably to
convert it to an integer right
perfect so this is us this is a user
right so now for the computer right so
let's just call it computer
choice
what can we do yes we can use random
rand int
and we can take one two three right both
inclusive right so now we have two
integers and that's nice right
so let's model this so we could actually
draw out a decision tree but let's see
if we can figure this one out
if
my choice is the same as computer choice
it's a draw right
so
if
choice
equals computer choice
[Music]
okay we should have double equal sign
thank you for correcting me here
then print
draw right that's the first case here
right
and
then we can actually just take
my choice right so if choice is equal to
one right so we have the rock here
that's one case here i have rock then we
can make an if statement inside here
right
we should actually have else if here so
we don't take two of them oh
it makes
sense right so if i have the rock then
if computer
if computer
choice is equals to we know it's not one
because that will be covered by the
first case up here right but if the
computer i'm rock computer is scissor
alright let's just start with paper
then actually computer wins
computer wins
we can actually just say else because in
the other case we know that we win
right because in the other case it is a
computer has scissor
you
win okay
else if
choice is equal to two that is a case
where
i have paper
paper so if computer is rock that's
number one
if computer
computer choice is equal to one
if computer
is one and i'm scissor right
then computer wins
and in the other case else
print human right
you win in the other case computer has
let's say i have two that's three so
computer has scissor no computer has
paper and i have scissor right um oh
i need to say it correctly right so else
if
choice equals equals three we could also
just have an else
if computer choice equally equals one
what happens then
well if
i am three that's a scissor and computer
is one
it's rock
then
so
did i do it correct and up one of the
two two it's like
paper
and computer was one here right so i'm
making a mistake here right so this is
this is this is difficult right so a
good idea is actually to add comments
here right so let's do that here so
choice one it is
rock
and choice two is
paper right so in this case he wins
right computer here
is here i am paper and computer is
rock right so
[Music]
so here actually i win
or computer win no you win
and then here in computer wins
[Music]
and wins
and in this case here i have
scissor
and computer has
rock
and here computer wins
computer wins right
else
you in
okay perfect okay so
just to make it a bit more fair maybe we
should have
computer choice
[Music]
let's just have it as
[Music]
so we see what the computer got right so
we can see everything is correct so
let's try this out it's going to be
awesome right so i'll just take rock and
computer took three that sister so i win
right awesome
and i'll just take paper and computer
takes three so paper and the scissor
that seems correct as well right and i
take
scissor and computer took scissors so
there's a draw perfect and i take
one
that's a rock computer takes one as a
draw right
so i take two it's a paper and computer
text paper draw well this is this is fun
and uh draw again okay so you see here
now you can play around with it and you
also saw how easily it becomes confusing
we didn't make a drawing of it and
and i was actually making a mistake here
and that happens all the time
if you don't make your
diligence and do the correct work right
so it's a good way as you also introduce
something here comments here which are
these
hash values here it's like the hashtag
here
then afterwards that it's common so it's
not executed so that's a really nice
feature to use as well here right so
this was actually amazing now we
actually learned about
randomness and we made an awesome
projects here with a math game and a
rock paper scissor game isn't that
exciting so are you ready for the next
one i think you are and it's gonna be
amazing i promise you that see you in a
moment
so this is going to be exciting i
promised you that right so we're going
to learn about lists so
lists what a list well you know this you
know a list on a paper right your list
of things right in python list is
basically actually the same thing it's a
list of
values or
list of strings list of whatever you can
put inside a list and you can put
everything inside a list right
so let's get started with them let's
jump to jupiter notebook get familiar
with them and see what we're gonna learn
in this lecture here good so inside
jupiter notebook it just says lists and
the goal is to learn about list how to
get a random element from a list and how
to make a randomization of a string
actually which we actually in the
process is going to use a list and then
a few nice functions as well we're also
going to learn a bit of the methods on a
list inside here so
what is a list well let's get started
we don't need to import anything because
list is a built-in data structure inside
python and a data structure is just a
way to have data in a nice way and again
the list is just a sequence of data
that's how you can say it
so let's create a list here so my list
let's just say
you created by square brackets and let's
just put something in here apple
orange
and
banana
okay so this is my list perfect executed
my list is here so what is the list well
let's just take a look at that list
here execute it
well it looks like this you see here
this list here has elements apple orange
and banana so how do we access elements
you might wonder
yes i wondered about that too myself you
use square brackets actually and then
you index it by number right so it
starts by zero so when you count as a
programmer in almost all programming
languages you start with zero
so let's try to execute that
so zero gets one the first element right
perfect
so that's the syntax of that let's try
to take the second element right so the
second element is indexed by one
and you see you get orange here right
and you can continue like that until you
get over the end of the list right
and the nice thing with python is
actually you can come from the opposite
direction
so you can do that with negative
indexing so
obviously you cannot start with 0 to
access this one because since 0 wouldn't
know which end to start one from so
that's why you index by -1 minus 2-3
right so let's take -1 here and you see
you get banana
and then you can continue like that with
-2 right
so you can have positive indexing
starting from zero here
or negative starting with minus one
minus two minus three and so forth right
so that was a bit about lists
in general and you can take an element
from the list right you can we will use
something like call pop later and pop
what does pop do well returns
uh return
remove and return item at index and
default index is -1 so that means the
default is the last element so let's try
to do that
so
you see here
it returned banana
was banana the last one
it was the last one here right and how
does my list look like now
[Music]
it only has apple and orange we've
removed it right
awesome
so this is what i was talking about
earlier that sometimes you need to be
sure
if the call you're doing is returning
something new or it does it on the
object you're working with right here it
does it on the object the list
so if we take my list we also need to
know append something so what should we
append
append banana back
okay
and let's say see what append does here
appends object to the end of the list
perfect
my list
my list
now we have banana back
amazing right
so one thing that we also often use is
we have multiple lists right so let's
just have another list here
and let's take some countries here
france
denmark i'm
living there so let's just take that and
sweden i can see sweden from my
not from my window exactly but almost
so
let's get started so we have another
list here so how can we concatenate the
list so often we want to do that right
so my list plus another list
[Music]
you see how easy that is
so again here it returns a new list in
this case here right so if you wanted to
save that one here you can just say
new list
to be this piece here right and then new
list will be the list of all the
elements right so so far so good so now
we have a new list with all the elements
from the my list and another list right
so these are
amazing functionality that you need to
master right so again just a pop return
the last element and the append
move put an element in the end and
concatenate strings by just a simple
plus sign here right
and indexing is quite easy so this
doesn't cover everything you need
everything about list but it covers what
you need to get started with them and
that's enough for this course actually
obviously you probably figured out there
are a lot of things you can do and
if i should go through everything it
will be an endless
mass and we will never finish
let's import random right
import random remember i promised you
when we came to lists i will teach you
something about a random well let's do
that
random
so let's see what random choice does
chooses a random element from a
non-empty sequence and
luckily a list is a sequence
so let's
take new list
and return it returns orange right so it
returns a random element right so did it
remove it
remember
no it didn't so it just takes a random
element right so
if we run this code again here
it will return a different element or
most likely a different element in this
case france here right so you see now
you can if you have a list of
whatever
then it can return
whatever another thing we will get
need to use is actually how do we mix it
up right so random
[Music]
i think it's called shuffle
what does shuffle do
it
shuffle list x in place and returns not
right so shuffle
takes s
x right here right perfect
so new list
[Music]
perfect
so what happened to new list
[Music]
it shuffled it right so it shuffled in
place as it says said here right so
again here this does it on the list
right so it has banana france
orange
apple
sweden and denmark right so it's
shuffled just randomly so if you run
this one again it will be different
order of the list as you see there right
awesome another thing that's often used
is actually on a string method right and
this is a bit awkward first time you see
it right so if you take a string for
instance this one with the space and
then you use join right so what does
join do
oh it doesn't do it directly on here but
what join does is
it takes the list as you see here
denmark apple orange sweden banana
france and puts it together but it joins
it with this
space we put here so actually if you put
the space away it will join them without
the space right so that's what it does
and this comes in really really handy
when you have a list and you want to
present it in some way then it's not
looking like this here right then you
can actually do something like this
and it looks
just as you please we will use that
later in the course if you don't
remember don't worry there's a ebook and
it will be written in the ebook so you
can always recall it like that so you
don't need to remember everything we
don't do that unless we use it on a
daily basis then we don't remember
everything so what can this be used for
well let's just make something a string
here
let's call this awesome
so i promise you i wanted to show how to
shuffle something right so
random
sample is another function and let's see
what that does right
and uh
what we wanted to do is actually stick
the length of the string here and add it
as well
so if you look at this function here
let's see what it does
chooses k unique random elements from
the population of sequence or set right
so that's why i have the length of the
string here right so i use the length
here
we haven't really been familiar with the
length function so far but length
returns a length of a string here or as
as of the list or whatever it is right
so
returns the number of items in a
container it says right and string is a
container list is in container and so
forth right so this is it takes
seven
random samples uniquely random samples
that means it only takes each one of
them once
because they have to be unique
unique in this sample here so they can
be duplicates because there are two e's
for instance here so that is okay
perfect so now we have the random sample
here so what can we do now remember join
[Music]
what if we join this right
then we have something here right look
it's different now right but
it is awesome right awesome
and if i run it again it can be
something different again here run
so now we actually take this string here
and scramble it this will be really
really handy in our project so just a
hint there remember these things here
when you do the project right perfect
awesome next thing right now we just
used
strings inside our list but it can be
anything right so
my numbers
so let's say we have a list of numbers
here 3 5 2 six eight
two
then we have my numbers here
you see here that's a list of numbers
so there are a lot of useful functions
right so let's take max you probably
figured that one out
it will return the maximal element in
this case it's 8 as you see here
and there's something called minimum
my numbers
and it returns 2 obviously
and
you can have the sum
[Music]
26
3 plus 5 is 8 plus 2 is 10 plus 6 is 16
plus
8
is
24 plus 2 is 26 so that's correct wow my
math skills i told you they can be
improved
perfect awesome right
so also just to remember i just told you
that uh it can return the length of the
list here so now we have that there one
two three four five six elements that's
correct right so
the way you can calculate the average
for instance is take the sum of my
my numbers and divided by the length of
my numbers right so these are all
awesome things to master right
good so far so good so the average
number in this sequence here is
4.333 so that should be correct because
we have 26 elements and we have no
the sum is 26 and there are six elements
right
so this is actually what i wanted to
show you about lists it's a long thing
right and we learned about how to index
the list here right and we can do it
positive and negatively we saw how we
could add
take an element the pop element and
and we saw how we could append an
element
and we saw how we couldn't concatenate
elements with a plus sign or not
elements lists
and uh
yeah that's nice and then we saw how we
could choose a random element it comes
in handy in the next one here
and we saw how that worked and uh
we saw how we can mix it shuffle it
it'll come in handy later
and then we saw how we could do this one
here actually join them together and
actually what we need in the next one is
this one here right
awesome and later we also need i don't
think in the next one no but later we
will need some some calculations like at
some point we would love to calculate
the average of something and that's how
you can do it
perfect awesome yeah so actually this is
what you need to know about list to
complete
the rest of the course there are a lot
of more things you can learn along the
way my advice is always to keep it
to the least amount in the beginning
because if you learn too many things
you'll just forget them and remember you
don't need to remember them there's an
ebook where you can follow along if you
didn't get that ebook go get it straight
away it's awesome
and
well we are ready for the next thing
right
let's get started
are you excited you should be because
the projects are where you learn things
but enough talking let's go to the
project and see what it is you should do
so the project is a game right it's
called the jumplet game and what is that
well
honestly there's a link to a description
there but the things are you need are
described here
and remember
it becomes more and more difficult along
the way and the description becomes less
and less or less detailed in many things
so let's look at that a word jumble is a
word puzzle game that presents the
player with a bunch of mixed up letters
and requires them
to unscramble the letters to find the
hidden word right remember what we did
in the last lesson we scrambled the word
interesting
the computer will take a word and jumble
it mix up the letters that's what we did
right
then the player will guess what the word
is
an initial word list could be father
enterprise science programming
resistance fiction condition reverse
computer python
nice list i like it already
okay so this should actually get you
started with that one and again how to
do that
well sit down think about it
i advise often to take a piece of paper
and try to figure out how you would
sequence this flow here and see how what
you come up with in the next one i'm
gonna show you
how i would solve it at our current
stage of python skills okay so you're
ready
go dive in on a piece of paper maybe and
or directly into jupiter notebook and
try to solve it on yourself okay
ready i am let's get started
so how did it go please let me know in
the comments before we dive into it
don't worry about it if it seemed to be
very complex right remember in the last
project we made we took a picture of the
rock paper scissor game and turned that
into code that was difficult right here
we kind of have a wake description of
something and we need to turn that into
code a game as well right
how do you do that well let's try to
jump into it together let's see how it
is and let's get started okay
so let's look at the
i assume you know what the game is we
already went through that but let's look
at these bullets again so the computer
will take uh take a random word and
jumble it right so mix up the letters
and then the player will guess what the
word is and then so this is basically
the game and then you say initial list
could be this one here so you might be
thinking wow do i really have to type in
this long list here well actually not
this is actually listing itself you just
copy it and then you say the words are
this list here so now you have it here
so again back to the first step here
right the computer will take a word
right so a random word
how do you do that
that's right thank you we should import
random import
random so why do i
import random on a line by itself well
if you write random here and you want to
find the methods available by pressing
tab it doesn't know them because you
haven't imported yet so that's why i
have it so if i import here
and then i go down on my line here and
then i write random then you can see you
can actually get the methods by pushing
tab here and remember words would return
a random
as you see a random word so this is the
word we are going to use here right
random words
so
now we need to jumble it right mix it up
how would how did we do that remember in
the last lecture again there was
something called what was it let's break
it down right so we need a method and
that method was called
sample right
so let's try to use that one sample and
see what it says here right the
documentation says
chooses k unique random elements from
the population sequence or set right so
this is the population will be the word
here and then we need
the length of that because we need all
of them right so word
length word
word
and there you see it and then it
randomized it but we also need to join
it together in one string and how was
that
well you remember correct there was
something called join right
and now it has it
you see
now we have it
so this is our jumble
and we assign that to a variable right
so now we actually did all the mix up
and i would have the jumper word right
okay so now let's prompt the user right
let's see how we can do that
we can do it with the print statement
and the formatted print statement let's
just do that
and the
jumbo
word is
jumble
[Music]
and then we have
guess
guess
input
right your
guess
[Music]
and what do we need to know we need to
know if the guess is correct
so if
guess equal equals word
then we print
[Music]
correct
else
and then we need probably to write what
the correct word is formatted
here
in
correct
[Music]
the word is
word right because we have it in word
here right remember we keep the word
here perfect
awesome okay let's try to run that now
boom right you guys the jumble word is
what
i don't even know what that is
don't know
wrong the word is condition okay
cute cute
more cute up more
what am i just all messed up here
cute up more what is that don't know
computer of course okay i have to get
into this game here
okay so
this is computer again
perfect right and let's try it once more
oh this is difficult i have no idea what
is this shriv
river
severe
severe
no severe reverse of course
okay you can see here i'm not very good
at that game obviously
these words are way too difficult for me
i hope you have more luck with that than
i do i'll not humiliate myself anymore
but it's kind of funny though i was kind
of surprised how difficult it actually
was
i mean
i think you're better than me i hope you
are
uh if not then you can see the standard
is not high here i mean wow i'm
embarrassed wow i should have done
better than that
never mind
let's
continue to the next one
the next one is going to be amazing
because then we're going to jump into
loops and that's another way to make
flows in the programs because you want
to do the same thing again and again and
it's going to be amazing you're going to
love it see you in a moment and please
just a note forget about how bad i was
at this jumble game here please don't
tell anyone it's embarrassing see you in
a moment
wow we need to learn a new flow right
now remember the flow with if statements
but sometimes at least now we have these
lists maybe we want to iterate and
that's always the same thing again and
again and actually many programs use
that all the time you probably don't
realize it but actually when a computer
runs in the background it must have some
kind of loop where you're just waiting
for the next task to do right so that's
kind of a loop right you're waiting
waiting waiting should i do something
shouldn't i do something and so on the
same with lists right you want to
iterate over a list for instance and do
something for each element well this is
where loops come into the pictures and
in python you have two main types of
loops you have for loops and while loops
so let's jump to jupyter notebook and
get started
so again the lesson is about loop here
and the goal is to learn how to iterate
over lists
iterate over chars or characters in a
string create a for loop over a range
and create a while loop so these are the
things we want to do right so again it's
about learning some syntax and get
started so for the
purpose of this let's just make a list
here as you could see we need a list
here we need to learn how to iterate
over a list right
so let's just call it first
second and
third
that's my list that's my list and you
know how a list is right
so it's just like this one here so
now we need to iterate over a list here
right so how do you do that well
for let's just call it item in my
my list
colon
this is basically the loop construction
here again you see the colon here like
we also had with the if statement
because now it will be indented
afterwards
so
what's intended afterwards will be
executed for each item in my list so
let's try to do that print item
just a note here it doesn't have to be
item let's change it in a moment but
it's just you know a name for it so what
it will do now is print each item as you
see here that's what it does first
second third right so it takes and
assigns the value of the list item by
item to item
and as i promised you you can just call
it whatever you want here and it will be
the same here so there's nothing in the
naming here right
perfect awesome right
so that was the first thing we needed to
do
so now we know how to i iterate over a
list so what about a string right so
let's say we have a string and it has a
name in it unknown name but we want to
inspect all the characters how can you
do that for c for instance for char
in
s
let's try to print c out and see what
happens well it does actually assign a
character
to c one by one right so it takes
assigns r to c and prints it
assigns u to c and prints it assigns n
to c and prints it and assigns e to c
and prints it right amazing
again if this is confusing don't worry
about it try it again right try to play
around with it remember everything that
is indented afterwards is executed in
the loop right so here we could say
print let's say we print
inside
loop
and then when you're
stopped the indentation
it's only executed after the loop so
this is
outside the loop
[Music]
so you see here what it does is it
assigns r to the variable here it prints
r and it says inside loop inside the
loop and then it assigns c or u to our
variable c and it prints c or u here and
inside the loop and you see inside the
loop inside the loop interloop and then
afterwards when it's iterated all over r
u and e r u and e
it is outside the loop so it is
executing the indented code
for the loop and
that's how it does right another thing
you often do is you use a range
so what is the range we'll see in a
moment
so let's try to do it for i in range so
let's see what range actually says in
the dock string
well it says uh
range
self
uh it doesn't actually says that much
but it has a stop and start a start and
stop right potentially it doesn't need
to start it doesn't need to it only
needs to stop often you only use it with
a stop but sometimes you need to start
on a stop value we'll just try to do
both here
so
in range let's just say 4 not to make it
too much print i so what it does it
assigns
accounts all the way up to four for
exclusive and
zero one two three right
and as i said you can have a start value
let's say we start at two and then we
iterate up to 4 right so instead here we
have 2 and 3 because we start from 2.
uh when do we use these for loops all
the time and just to
let's say we have remember the my list
here it's still here right so 4i in
range sometimes you would say you take
the length length of my list here why
would you do that well in this case here
it's because you want to access the
items in the classical way here not
classically this way here right for
instance if you also want the index here
so i comma i here right
so in this way you actually get the
index of the items there is zero one two
right zero one two of the list first
second third right zero one two zero one
two this is the way you access them
right so sometimes this makes sense uh
there are other ways to play around with
these things don't worry about it this
is enough for you
finally i want to show you a
while loop so let's
say we have a value here
while y is a bit different from four if
you notice four it does something
specific number of times so in the first
case here it does it
the number of times as long as a list
has items right
so it's fixed
and this one here with a string it does
it as many as characters are here right
fixed
and the same with range it takes it from
one number to another number right it's
fixed
what if we want to simulate something
different and the same with the last
example for instance in the program
sometimes it keeps running until the
user quits right so how do you do that
well in this case here we could do value
value is
is uh greater than zero for instance
right so we have some kind of value and
inside here we will actually update it
value
uh we take an input from the user
value
and what do we need to remember we need
to convert it to an end let's just do it
immediately together here right
so
what i'm saying here is now it will
prompt me for a value and if i put nine
here it will continue if you put four
here it will continue if i put zero here
it will end right so you see this while
loop here is different from a for loop
because this can continue forever
basically
and
and will never stop if we continue
entering numbers that are above zero
greater than zero right
so in this sense the while loop is very
different from the for loop right
awesome
before we finalize this lesson here i
want to show you a few new things
actually because we will need them in
the next one and they're not fitted
right into loops so i just put them here
in the next one here right so let's get
started what is it that we need well we
learned a lot of things right but are we
done
no we need a few more things and this is
just small things right so
one thing we need to do is our if
statements we want to see if a character
has been guessed or not we'll use that
in the next lecture right so let's do
that right so if we have like a string
here let's say we have abc here
and let's call it just
here then we can actually use an if
statement like this one here if
let's just put a character here and we
put it to a here and say if c in
s
c is in s right
let's just keep it like that so in this
case c is in s because we have a is in
there right the same for b
and the same for
c but if you took another character like
d then it will not be in it right so
this is quite a handy way to do some
programming because you see this is
often you want to just check if this
character is part of the string here
it's pretty easy to do it like this so
this is a really nice thing and you'll
need it in the project in order to
finalize that
another thing is often
sometimes you need to check that
something is not the case and we did
that with variable not equals right but
you can also do it like this so if
c
not in s
c
is not in
s right so if you execute that that is
the case because we have that be it as
assigned it to here right so this is not
negates the expression right so c not in
s right that's what it means right so
not so seen it's just about reading it
right so this is quite straightforward
and finally you can also have two
statements in one right
what do i mean well
if
let's put some variable here let a to be
10
b to be
12 c to be 14 in this case here so if a
is greater than or less than b
and here it comes
b is less than c then we print awesome i
don't want to
right because this is the case right
because we both have that a is a lot
less than b and we also have that b is
less than c right so in this case here
this is true right but if i change this
one here well it's not the case anymore
because now a is greater than b right
and in the same way you could have or
instead here instead of and or then it
just means one of them is correct and
one of them is correct because b is a
less than c here right but if you
changed this one here
or c here to 10 here this is not the
case anymore because none of them
neither aim is less than b or b is less
than c right so you can combine them
with and and ors like that so
this is amazing you have not and you
have in with a string and this is
amazing right this is just a few things
that you need to master for the next one
so see in a moment
if this looks advanced don't worry about
it we are here together to figure it out
but first let's try to look what the
hangman game is and there's actually i
think there's quite a lot of help in
this one here so i hope you'll figure it
out it will be a great success story for
you right
and uh don't worry if you don't figure
it out there's nothing wrong with that
we all have our own journey and me i've
been sitting often with problems that
took me days to solve
and again
the more time you spend trying yourself
uh the more you appreciate the more you
understand you will get from the
solution when you see it so let's try to
understand the game right i also have a
link here for the hangman game but we'll
use the description below here right
so the hangman game basically uh
it is about you should guess a word
right so
first of all you will just get blanks
and
then you will answer
you will guess a character
and it will display characters which are
correct and
in the end my you might guess a full
word if not
then you'll be you'll lose you have 10
guesses wrong guesses right so this is a
game right the game is as follows
computer has a list of words okay
computer chooses a random word from the
list hmm we know how to do that the
player gets 10 wrong guesses or 10 turns
the following the game follows this loop
computer prints the word character by
character placing with underscore those
not guessed yet
initial initial
no characters has been guessed right it
starts with all blanks or underscores
a player guesses a character if
character is not in word a turn is
withdrawn
if no turns left computer wins if player
has guessed all characters player wins
right an initial word list could be
father enterprise science
programming resistance fiction condition
reverse computer python okay
so
are you ready
i hope so it's gonna be amazing and uh
see you
in some time you should take some time
and try this out and uh then if you get
stuck don't worry i'll help you
see you in the next one
isn't this exciting are you ready for
this one i hope so i hope you did well
and let me know in the comments below
what's difficult for you and
i really love to hear about that because
it also teach me what is difficult and
what's not so let's dive into jupiter
notebook and try to solve this one right
so we are at the hangman game here right
so the game was as follows right
remember you had wrong guesses and write
guesses and so on there but let's try to
get the structure first right so
we know we need
something done right we need to choose a
word so let's start by that well first
we need to import random right remember
good and then we can take this list here
for our list of words right so words
this one there and then we can take a
random word and we know how to do that
right so it's random
choice
and words right so it's always a good
way just to check along the way that
things are going as we plan right so
here we have science
enterprise fiction and so on python and
so on python again and fiction
so it seems to work so we can choose a
random word now right and
now we need a loop right
i mean
we need a loop in order to iterate
multiple times how do we do that
does it sound like a for loop do we know
how many times no it depends on we know
we have 10 turns but if we guess right
it's not taking a turn away from us and
if it gets wrong a turn is taking away
from us so we don't know how many times
so it will be a while loop right so
while what well we have these turns here
so let's try actually to add them to b10
here
so while turns is greater than zero we
continue right so
this is exactly what we want so what is
the first thing we want to do right well
it says computer prints the word
character by character replacing
underscore for those not guessed yet
so we need somehow to keep track of the
characters we have been guessing right
so let's add a
guesses here and let's have it as a
string so these are the guesses we had
so far right and now we need to print
the word character by character
so how do you do that well it's a for
loop right 4c in word
what do we do well if c if we have
guessed it we should print it right so
how do we do that remember if c in
uh guesses because that will be the
string where we add all our guesses to
right
then we print
c and else we print underscore right
[Music]
perfect
well this will print each character or
underscore on a line by itself so what
we can do is use this n trick here if
you remember it and
so it doesn't add a new line so in this
case here we'll just add new
a space between them instead right and
then we have a print statement down here
and let's try this out so let's just
have an input here so right now it's
just the input so it doesn't go in an
infinite loop really really fast so
let's try to do it
just want to see that yeah okay it does
print underscore for all of them because
we haven't guessed anything yet right
um perfect so that's the first stage
now we actually print it out and we
assume it will print the characters we
know so how do we do that right well
let's first stop it you stop it by
pressing this square bracket up here
right and then it stops it
perfect
so we need to add guesses to our list
right so
guess here is input
guess
a char
hr here
for character and then we take our
guesses and add it to our guesses so you
can do that by just adding that right
so
let's see if actually that works now you
can guess a
char
okay
it seems to work i don't know what this
word is
uh
reverse of course re
verse right so actually it continues
after we've guessed everything because
right now we don't terminate right we
terminate f 10 times
uh and actually we don't because we
don't
we don't decrease this one here so we
need to terminate it so far okay
perfect perfect perfect perfect so what
is the next step right
so
if we guess correct or incorrect then we
should be penaltized
so if
guess
not in word
[Music]
right so if the guess the guest
character is not in the word well we
take turn
and say turns -1 right so this is
actually
making it possible to
terminate the program okay so so far so
good
so far so good
no
c
o
m
not computer
houses
uh co
condition all right con
so we still don't terminate after we're
done but if i continued like this
let's see
[Music]
uh let's see g right now we don't know
how many turns we have so i got a bit
confused there so let's let's also print
that out here so
you have
turns
left
or actually we should write turns left
and then we have format string here so
let's do it here you have 10 turns left
okay so when you just put nothing
all right so now we should see if we
terminate down there right so we
terminate at the end here right after we
have no turns left okay perfect now that
works but still we cannot win right so
how do we win
well
basically we can make a variable
so let's try to do that and we say
guest all to be false here
oh what why do i want to do that right
actually not here actually it would be
nicer to have it down here
[Music]
guessed all false we put it in front of
the
for loop here because if we reach inside
here we know we had a wrong guess so
guessed all
or it's opposite right false it should
be true here right so we assume first
that we guessed everything
and
if we come into this one here we know
that we didn't guess everything right so
we don't have i guess here
so how can we get out of that well if
if not
guessed
all we need to do the following down
here right otherwise we need to
terminate right and how can we terminate
well there are two ways to do that let's
just keep it simple right now and we'll
say else turns b0 right because then we
don't care about anything else then we
dig
we put it to zero right so this should
actually do it right so guess char
i'm not good at this game
see here
what can it be i'll just cheat again uh
fiction of course
fig
because we need to check that it works
now boom and it ends right you win right
perfect and actually good right here you
win in this case here right but those
are details you can play around with but
you see how
actually nice this is right so i just
again here we have the list of all the
words and then we have choose a random
one we keep track of all the guesses we
have we have the turns here and while
the turns is greater than zero we
continue right and then it prints out
how many turns we have
we
assume first that we have guessed
everything and then we go through it and
if we didn't we put it to false right
and afterwards we check if we guessed
all if we guessed all we just say turns
zero it'll be nice to have say you win
it down here and then otherwise we'll
take a character and our next guess and
add that to our guesses right
makes sense and it's amazing
perfect if you find this exciting the
next thing is going to be even more
exciting because then we're going to
learn about functions yeah that's right
functions it's a great way to structure
your code and it'll make it more
readable and you can actually make
abstractions so you work on a higher
abstraction level it's gonna be amazing
see you in a moment
are you excited i hope so because in
this one we're gonna learn about
functions
functions what are functions well
basically it's just to make code
reusable and there are many reasons why
you want to master functions but one of
the most basic thing is well you don't
want to write the same code twice right
no you don't
in the ebook that you can download
there's a link in the description
there you can actually get a more
detailed description about why why use
functions an introduction to that in
this one we're going to dive into
learning about functions and what they
are and how to use them so let's jump to
jupyter notebook are you ready here we
are so again the goal of this section
here or this lecture here is to learn
about functions use functions to
structure code
function makes the code more readable
remember about the sin of python well
it's all about readability right and
functions actually can make your code
more readable more structured more easy
to comprehend
it might not be obvious in the beginning
but it will do magic for you so
understand functions if you are into
math you probably know what a
mathematical function
is well they're basically the same thing
just bit more broad okay so let's try
just to make our first function i'll
just describe what it does right my func
and it does
[Music]
all it does is print hello world
hello
world here okay
so
if i execute this one nothing happens no
output comes here because this is a
definition of a function so you define a
function with the word dev def and then
you have a space and then the name of
the function you have there in this case
i called it my func you can call it
whatever and then you have these
brackets here and inside these brackets
you can have arguments as you will see
in a moment right but don't worry about
that and then again we have this colon
here and then it indicates that
everything behind there that's intended
is part of this function so you have
multiple lines just like if statements
and while loops and for loops and so on
right here so how do you utilize this
function how do you use it right well
you call it like
we called a lot of other things right
here so if you call it you get the hello
world
right
so this means that this piece of code
here that you have in this one here can
be accessed from
wherever in your program
this is kind of nice in many cases in
this case here it doesn't really make
that big a difference but imagine have a
big block of code and then you want to
have that multiple places in your code
then you can have it without retyping
the same code again as i mentioned you
can also take arguments so let's try to
do that right so let's make a function
called print name
still very useful functions here right
so it will take the name as an argument
here as you'll see in a moment how it
works print and let's make a formatted
string here
hello
and let's just say
name here
and we need to end it there
here we go
down again when we execute it you can
see it's executed by the number three
here uh nothing happens right it's just
a piece of reusable code so now we have
name here and let's just put my name
here
and you can see hello
so that's what it does and if i put
another name in there let's just try
that
let's be funny right so what's your name
jens
okay your name is jens you don't look
like a chance i must say never mind
but your name is jens and you don't look
like one that's how it is okay it says
hello chance okay good enough fun for me
and uh obviously you can also have
multiple lines so let's just make it a
bit more fun now
print h and just say name and h here
and this will print
hello
oh i forgot
name
and just to have emphasize we can
multiple lines here
uh
you are
age
years old right and then we take here
print age
i'll just take mine first
and comma
30 here right so as you see here
you have two variables here or two
parameters here right and use parser
arguments honor and third here right so
honey becomes assigned to name and third
it becomes assigned to age here right so
that's how the function structure is
right and there's often a bit confusing
about what our parameters or what our
arguments basically said the parameters
are the ones where you define the
function right and the arguments are the
one you parse to the function in the
case where you call it right don't worry
about that it's not gonna do much
difference if you know all the
terminology and use it correctly it
doesn't matter many computer programmers
can be inclusive
mess it up all the time because we talk
but the key thing is you understand that
what happens right
and
another thing you can do is actually
return things from a function right and
that can be actually pretty handy so
let's see this one here
calculate
h right so let's do this one so let's
take birth year
right
and it returns
2021 minus birth year right
let's see one why do we care about this
one so let's see here so let's calculate
age and i say
i'm still born in 1991 i hope so at
least because then i'm 30 years old i
like that
and then
why is this useful you might say well
imagine that actually this calculation
here is done multiple times
in your program right and suddenly
something crazy happens
you know what
new years right and
then it actually turns into 2022
instead right
and uh
then this is crazy right then you need
to update this everywhere in your code
but if you have a function like this you
only need to update it one place and
that place is right here right so if we
update this one then everywhere you use
this calculate age it updates to a next
to the next to the correct value right
so this is also another useful thing
about functions right so
these are some of the things that was
so far
we covered simple functions and
arguments multiple arguments and
returning something and you can have
multiple return statements inside here
it depends on you can have it wherever
you want but when it returns it returns
and doesn't execute after that line so
if i had a line with here print
hello
it will not execute that line because it
returns before that we'll need to learn
a few things in order to do the next
exercise so
let's try to do that in this one here
so the project we're going to make is
going to be a cesar cipher and we need a
new few things in order to do that it's
not complex it's just i didn't see any
reason to introduce them before you need
them here right so that's what we're
going to do in this one here we're going
to introduce a few functions that you
need and try to play a bit around with
it just to see that we mastered them
before we get started with the project
the caesar cipher so the caesar cipher
probably as you know it takes characters
and does something with them right it
scrambles them up somehow we'll see in a
moment how but in order to do that we
need to calculate with characters so
let's do that so let's jump to the
jubilee notebook so one function we need
is the order of the of a character and
what does it do it returns a unicode
of a
unicode point for a one character string
so let's try to do that actually let's
put the capital a in here
and say well capital a returns 65 and
let's just for the fun of that take b
here and see it gets 66. so these are
unicodes for those characters right so
we will need this one here and why we
need that it's because making
calculations on characters is difficult
we need a numeric expression of them so
then we can actually make calculations
based on them and that's what we're
going to need so we also need to get
them back and there we have this one
here which will return unicode string of
the number right so if we put 65 here it
will return a and here it will be 66 it
will return b and so forth here right so
this is really really nice
so just to show it here so four
see in range here we take 65
and 65
plus 26 here i think there are 26
characters in the alphabet and let's
just print it here format it there
so
c
is
let's see here right
let's do that
and you see here we get all the alphabet
all the way down to set here so we have
that alphabet is from
65 all the way down here to 90 air force
set
okay
so so far so good okay
another thing we need to do is remainder
calculations and this seems a bit
confusing at first but actually it's
quite straightforward
so
there are many ways to describe it and
in the e-book i go i actually make a
quite good description of how it works
but let's just show it here right so
imagine actually you have
it's new year's eve again right and you
say i will count the hours from new
year's eve right so after one hour it's
one am after two hours it's two a.m and
so forth right but let's say
hours right so let's say they went
25 hours so what time is it right well
if you do the remainder calculations
here with 24 that means that
if we remove
if we have a clock with 24 hours
it will actually do it in a circle as
you see because then it'll back at 1
right then it's one am again right and
this is actually what it does it
can actually circulate like that so if
we take one up
one is one but
25 is one again
and uh if we add 24 to that again it
will be 49.
it's still one as you see and it
continues like that and if you take 50
it will be two right so in 50 hours it
will be 2 am right and this is how you
can actually think of it it's just
taking a value and
putting it down like a clock it resets
every time once in a while this is 24
hour clock right it could also be 12
o'clock 12 hour clock right and resets
it all the time similar
so this is how you do it right so you
can actually do additions to it right so
let's say here 50 it was two but you
could also add to this one here add
three air and it will be 53 because it
addition is done afterwards but here
would be 5 right
so
this is often handy when you do things
because you do need to do it modulus
something right or remainder something
so
we will need to use this in the next one
and uh it's gonna be
if you never worked with that it's a bit
tricky but let's make an example here so
let's uh def
is let's make a function called is even
and take a number here
so how do we check if something is even
right hmm
well if we had a clock with only zero
and one so it could be zero or one zero
one zero one right that'll make sense
because
if it is zero o'clock and then it's even
and if it's one o'clock it's odd right
so if
n modulus two
equals equals zero
then we print
even
else
odd right
so
let's try that
is
even
zero
yes it's even
is evens oh
here yes it is so let's try to do that
if 4i in range
10 let's just take 10 and then just
is
even
even here right i and actually for the
fun of it let's just print
i and we take end
here right so here we have it even odd
even odd even odd even odd even odd as
we expected right so
just when you think of this it is like
you come to the end of the number here
right the number you take the remainder
calculation with it is the clock with so
many hours in all right and it just
returns to the same thing again right
it's like a clock right it goes around
around around even though 50 hours is
gone it's still
just 3 a.m right
or 2 a.m it was 2 a.m yeah perfect okay
so far so good and that's actually all
you need to get started with the project
so are you excited i am let's get
started
this is amazing this is a great project
and it's really really fun and you will
learn a lot of things along the way
so let's get started right so
cesar cipher if you're not familiar with
it there's also a link here you can
enjoy if you want to read a bit more
about it but the goal of this project is
to make encryption and decryption right
so let's just introduce it here
so the caesar cipher is a simple
substitution cipher that basically means
it changes characters which is limited
to only shift the character by fixed
number i'll show in the diagram down in
a moment so let's take an example right
imagine you got the message brx d u h
d said h v r p
what we're going to make out of it no
sense right so what if we get help with
this diagram here right so let's look at
this diagram here right
so
c here is a bit difficult to see here
right a goes to d maybe b goes to e and
so forth so we shifted the alphabet down
here by three characters right so the
three a b and c are over in the end here
while so a maps to d and so forth right
so if you had b here and you actually
find b somewhere in the list here b it's
was mapped y was mapped to b you see
that y is above b here because this is
the encryption algorithm and the
decryption is opposite right so b maps
to opposite
y
so b maps to y and r
let's find r it maps to o
x maps to
u
and d you probably figured this one out
now right d
a and so forth you can figure this one
out and try it yourself right so this is
actually what you want to do right so
implement so the project you should do
is implement a helper function encrypt
char which takes a char and a key to
encrypt a single character chart with
key it can be helpful to use this
function ord and chr and the remainder
calculations right
and calculation can be a bit tricky so
don't worry if you don't figure it out
but this is fun to play around with to
figure it out and see if you find out
right and uh yeah and then you implement
the message encrypt message message key
that should be used use a helper
function we just created up there
implement similar functions for decrypt
right so again
i know that i mean i think
the first helper function up here is
quite complex and that's also why it's a
good idea to make it as a helper
function because then you can see uh try
to encrypt one character right if you
use key three you know key three that it
should map a to d and b to e and so
forth and then you can try around with
it because if you don't nail this one
here the rest will be wrong right
obviously
so are you ready to try and you know you
should use this one here and this one
here and this one here and still if you
don't figure it out
it is difficult i would say it's not
easy but it is fun to try to play and
then see how it can be done and remember
often there's not just only one solution
there are multiple are you ready i think
you are let's get started
and don't be afraid to get some help
this is a difficult one see you
did you how far did you come let me know
in the comments down below there i would
love to hear
where you ended up or if you figured out
all by yourself don't worry if you
didn't it's pretty difficult right
so this is the mapping we want to
achieve with our helper function right
remember
you need to create this helper function
encrypt char that takes a char and a key
right so to keep it simple let's try to
implement it for the value three or not
we will call it with value three and
validate it with it to see how it works
right
so so far so good let's create a
function here dev encrypt
char and it takes jar and key here right
and it should return something so right
now let's just return the char and see
that it doesn't do anything right so
so far so good encrypt
char and it will take let's say a here
and a key here three here so this is a
purpose here so right now it fails
because it maps a to a because we don't
do anything right
so
it says to use these helper functions
here right there or
cr
chr and then remainder right
so how can we do that right
it is to be honest it is or one
calculations but you can do it in
multiple steps and that's what we're
gonna do right so remember if we mapped
if we mapped uh
it
this one here we get the unicode of char
and then we can actually add the key to
it and return that and
then we have a key value let's just see
that here right so now we get 68
remember
that the order of
a
or 65 right so we added three to it so
this looks pretty pretty nice so how do
we turn that into a character and we do
it like this here right
so
is this it
it seems pretty good right but there's a
catch right if we come to the end of the
alphabet
so if we map x for instance let's just
take w first just to assume that it goes
well here
[Music]
it goes fine well but after w we get x
here and it gets a square brackets and
that's where the a should come in right
so this is where the
this is the strange thing of uh
a strange thing of the house called the
modulus or the remainder calculation
comes into the picture right
so
how can we do that now
this is difficult because actually we
have a problem here because this value
here should not index to
to zero after it comes up to this value
which is 90 actually so what should it
map to right hmm
well
first of all let's just try to add this
together here
and then we have here and then we put
modulus
well what is the modulus looks like it's
26 right because we know they're 26
characters but the problem is
then we need to add more again
how do you do that so let's say we take
the a character
and subtract that
from our value right
[Music]
what if we subtract that a character
this which is 65 right
so we subtract now
a from it
we add the key to it so what do we have
here right so we
take the character if it was a we
subtract a so it's zero and then we add
the key and then we take it modulus 26
right because that's 26 characters and
then if we just
take
and add a outside here right
i actually think we're pretty close now
let's try this one boom a you saw that
but is everything correct now
well there's one way to find it out
let's try just a few ones here a becomes
d set becomes c
uh it looks pretty good right so if this
was a bit confusing don't worry about it
but look what we do here it's because we
need to do this remainder calculations
here
and
in order to do that we actually subtract
the
65 from the character value and add the
key and then do the modulus calculation
with the 26 here and then we add that to
the 65 character again right so it looks
a bit confusing i know don't worry about
it
it is the tricky part so
next thing next up here right what do we
need to know we need to make this
function encrypt
message now and it takes the message and
key here right
so what we need to do now is actually we
we focused on uppercase words so we need
to convert our message to upper right
just to make sure that the user didn't
mess something up i'm a user and i would
know how to mess it up so i just ensure
that all the characters are in uppercase
and then let's just have a cipher here
and
that's a cipher text okay and then we
take four char in message
uh
in message right
and then what do we do we call the
encrypt but there's a cache right there
might be spaces and stuff like that so
if
so we do if c
in
not in we could say not in or maybe we
just say in
the characters let's say space dot and
comma
if it is this case then we take cipher
is
equal to cipher plus a character right
else we call cipher
and we take cipher and plus
encrypt
char right
where we call it with c right this
should basically do it and then we
should return cipher okay let's try that
one okay confused not really i'll walk
you through in a moment
encrypt message
and we take
you are awesome
and we have an
oh we need i forgot the key here right
i'm so excited about it and i actually
forgot the key down here
and got so excited right perfect here
right so now we actually have this
message here which is actually the same
one as we have up here right
that's pretty
cool that's pretty cool actually
okay so let's just check this code here
again so encrypt message right so first
step here we just put everything in
uppercase in order to ensure that then
we have an empty string called cipher
which we'll use to add character by
character because then we take the
message the character from the measured
one by one
if the character is a space a special
character basically then we just add it
we just ignore it right otherwise we
encrypt it with the key here and
if you like me all excited you just
forget about putting the key in there
never mind right and actually now you
can use other keys but let's just leave
that for you to play around because the
next step is we need to make decrypt
right
hmm
how did you do that
if it was me at first i would rewrite
everything up here
in a decryption function let's do that
decrypt
char
char
key well it's probably pretty
the same here right but we need to do it
opposite order now right so let's just
actually start with the same thing as we
have up here
and let's
change something here right
so
if
encryption and decryption are actually
the opposite of each other right
decryption is actually the opposite way
so that's actually the
26 minus the key right
instead of plus a key here so that
should actually do it right so let's try
that one here so let's decrypt
a char let's decrypt d here with key 3
here and we see we get a here so this is
opposite right so all we need to do is
this one here that's pretty nice right
so now we have that one there so let's
try to make a decrypt def
the
crypt
message
cipher now and key here
and
do we assume the cipher is an upper case
i think so we do so we just take message
and we put it to the empty message oops
ah
message and put it to the empty message
here
and then for c in cypher
we'd say if c in
this space
and dot it was with opposite order here
it doesn't matter then cipher equals
cipher plus
c
else
cipher or message
message
thank you for helping me here message
message
message plus
decrypt
char
c key
oops and we need still we need to
return message so let's try that one
right
so the
crypt message
what message do you want to decrypt
this one here
three
you are awesome right
if you're really really smart you
probably realize this can be done a bit
smarter right so let's try to do that a
bit smarter right
so
let's make a new function here def de
crypt
message
two
and called cipher and key
because you'll probably realize that
this is the same
sequence here and it's just the key
minus here so what if we just return
encrypt message with a
cipher
and we take
26 minus key right
what happens then let's try that
so decrypt
message 2 and we take our favorite
message here
and we take three here and you see it
also works like that
so basically because the encryption
decryption is the same algorithm you
don't need to implement it all again but
it's just fun to do it right it's just
fun and often when you see like i say oh
why didn't i think of that well i had
multiple experiences like that so don't
worry if you had the same experience i
think maybe even i did this myself a
long long time ago i don't remember
but it is quite fun right so again this
one seems quite complex and this is
probably i mean
if it doesn't make sense at first try to
write it down try to make some simple
examples calculate it yourself with
specific values it will help you a lot
you can actually take this one and print
them out one by one and figure out what
are the values of each of them and
calculate it by yourself the tricky part
is to understand this remainder think of
it like a clock that has 26 hours and
then it resets every time right start
from zero again every time it reaches 26
it's zero instead
zero instead zero instead right
uh
good and uh that's basically it i mean
this is amazing isn't it fun i think so
amazing see you in a moment
so this one is exciting because we're
gonna learn a new data structure so this
is one of the built-in data structures
from python and it is used all the time
it's called the dictionary
whoa so what is a dictionary well in the
one hand it actually is similar to a
dictionary you know the one on the
bookshelf those physical books i don't
know you remember those
back in the times when people had these
big shells with books on right one of
them might be called a dictionary so how
would you use a dictionary well you
would have a word that you wanted to
look up and find the explanation of the
word what type of word and so on so
the key thing is that the word you're
looking for is the one you find the
content with right so you have a key and
a value right the key is a word and the
value is the description of the word
right
a python dictionary is similar
you have a key
this is the one you want to look up and
then you have a value which is a content
the description like in a dictionary
right
so key and value pairs what am i talking
about let's jump to jupiter notebook and
figure it out right you ready i am let's
get started
so here we are in our jupyter notebook
and we have the dictionaries here the
goal of this one is to learn about
dictionaries and uh yeah what do they do
they have these key value pairs i also
write dict here and this is because that
in python they often refer to the mastic
okay let's get started so the best way
to learn about things is often just to
see them and play a bit around with them
so let's try to do that right are you
ready yeah you are
so my dick
my dick
uh so the way you create python
dictionaries is with curly brackets like
this right
so if you do like this you just create
an empty dictionaries and you could do
that and then you can add items to them
just like you had a list where you can
append elements to them we're not going
to do that here we're just going to
create one here and just describe how it
works so i said
a dictionary keeps key and value spares
so let's try to put a key in here and
the key could be anything now we just
keep it as a string key one and then use
colon here to
put the value behind there and the value
could also be anything now we have the
key as a string and the value is a
string
and let's just add another one here key
2
and
value 2 right so now we have actually
created a dictionary here
and
again we have the key and the value so
you might think how do i access things
right so again like a dictionary
the physical book i was talking about
right you would look up at word right
and this is the key to our
thing right so if i wanted to look at
the value of key one then i just write
it like that and then i get it right and
you probably already guessed it what
would happen if i put key two instead
here well i would get the value of key
two so this is really really nice so
this is how a dictionary works and a
great thing about dictionaries is you
can just add
items or key value pairs along the way
so i created it here so i can put a key
for
key 3 and add value 3 for instance right
and now my dictionary
looks like this here right
so please note that we have key 1 value
1 key 2 value 2 key 3 value 3. and the
key 3 was assigned here so it was not
originally assigned in the definition
here so you can just add key values
pairs along the way
you can also update the values
my dick here for instance if you take
key 2 here you can update it and you can
actually use a similar
the values do not need to be unique
so let's just say we have put a value 3
here
so let's see what happens here then you
can actually see here that the values
can be the same
but the keys obviously need to be unique
you cannot assign the same
key multiple times then you wouldn't
know which key we're looking up right so
the keys are unique that means there can
only be one key of each type of each
value not bad it's a it's a bit awkward
to use the word value there right
because the values are behind the keys
okay
good makes sense
so
often people struggle a little bit with
well
when do i use these and why are they so
useful
well
recall a database for instance or an
excel sheet or something like that
so often what you have are rows of data
with similar
the keys in them actually and this is
called a record so let's do one for a
car for instance let's make a record for
a car so imagine we had a long
row of cars where you had the brand the
model and the year right so you had a
big sheet of that then one row would be
a record in it that's what you call it
right
so let's try to make one like that so
let's say here we have the brand
and the brand here what car should we
take lamborghini i like you lambo
lamborghini
was almost not possible
someone was spelling it wrong so
lamborghini so what model should we take
here
modeler yeah i know we are in these
times when we need green energy green
green driving so we need an electric one
and luckily lamborghini has an electric
car and it's called xi'an
right so lamborghini
i know in the us you often call it a
lambo lambo lambo yeah i know that i
think it's because
you have a hard time to pronounce
lamborghini
i probably don't do it perfectly because
it's like it's italian
the car model names are actually most of
the time spanish names
named after spanish bulls right is that
the case i think so
good and then we have the year of this
one here and that was a trick right
because now we can put for instance an
integer because a year is an integer
right so here you also see how they can
have different types here so this is a
string string and an integer right
so how would this record look like like
this
so
in the next lecture we're going to learn
about csv files
and csv files are rows of similar data
like i talked about like an excel sheet
right where you have the columns and you
have the same rows or a database if you
can relate to that instead right so rows
of the same type of data right and a
great way to read that data is to read
it into a dictionary and we're going to
do that it's going to be awesome right
this is also why we need to learn about
dictionaries and in the game
not in the game in the project we're
going to you and you're also going to
see some awesome use cases of
dictionaries right
so here we have it
a record like this one and we could have
multiple cars but we took this car just
to show it as an example
one thing that you often also use these
uh
dictionaries to is actually counting
occurrences of something right
and
me personally i use it all the time for
that so for instance if we had a list
now
let's just have a list of items here
and these items could be strings right
so we have a pen here we can have a
scissor
i'm gonna have another pen here
we have a
pen here again and then let's just we
have a scissor and let's just say we
have these
five items there so
our goal now is to count the occurrences
of each type
uh in there right so we have pen and
sisters
and we also assume we actually don't
know we have pen and scissors we don't
know how many different types we have we
just want to count them right so you see
that
the code below here that i'm going to
make doesn't assume that we know we have
pen and scissor and
that's kind of the neat nice thing about
these dictionaries how flexible they are
right
so let's just make a dictionary called
count
and have it empty so this dictionary is
just empty right
and then we take four item in items
right
so remember this for loop here will
will iterate over the items in our list
and so far we have made no assumption of
how many different types or items we
have in our
items how many different items we have
in our items we don't know we only have
pen and scissors right
so what can we do so we count
and now we actually put the item as a
key here because we want to increase and
count the number of them each one of
them right
good so far so good so how can we do
that so we actually look at count and
then it has a method here
i thought so at least and that is called
get
so what does get do
and what does get do
it doesn't want to help me the automatic
help here
well for some reason
it's not active right now
i actually don't know why it doesn't get
the help
book up here never mind i can't help
help you then but what you can do is
look up the item that we're having and
then we can put a default value so that
means actually what this get does is it
looks up item i
item in a key in this count here and if
it doesn't exist it will return zero
this is this is great news because then
we can just add one to that right and
then we execute it because what do we
have now look at now
we have count three for pens and count
two for scissors right so we have one
two three pens and two scissors right
this is what this one does right it goes
from item and items right so first pen
and it puts pen in here and looks up
does pen exist
no not the first time so it puts zero
and then it adds one right
then put scissor and look scissor up
here
scissor do you exist not so far then you
add one then you come to the pen right
you look up pen you pan exist there's
one pen we add one to then now pen two
right and take this one
it looks pen up pen is there pen is two
we add one two pen is three
then we have scissor
scissors there
scissors one we had one scissors two
right you see how it goes this is really
really powerful this one here and i love
that kind of stuff
and we're going to use something similar
to it
in our project a question you might have
is like but how do you iterate over a
dictionary right
when you do that very similar to a list
you just can't do it the same way
because you have key value pairs so
let's do that for key
key value in
count here you can take items
instead so items returns the key value
pairs in it so let's just show you here
key value here right so you see it has
pen and scissor key and value a similar
we could have done it for this car here
we could print that out so let's do that
for key value in car items
i forgot the colon here
key value right so we have the
here we have the brand lamborghini
models
and year 2020 right you see how it works
i think so
a final thing you often need is actually
the keys to get the keys out of the
of
a dictionary right so here we have the
brand model here as a keys and yeah the
other one here in account we had the pen
and scissors keys so how can you access
those well
well if you just say keys on it
what do you get you actually get a
keys right
so this is you see
obviously this is very close to a list
already but if you want it as a list
then you can actually just write list
outside and have car keys car keys
that's nice and you get it as a list
here now this is a list you can actually
just if you're a bit worried about it
you can see
type here and you can take this list
here inside here and see it is a list
what list does is you know it it
converts it to a list if possible
and uh similarly you could take the
the count up here and convert that to a
list right so if you wanted that list
count here keys right
so here you have this pen and scissor
asset list and you would need that in
the project so
but often you also need that you won't
just want the keys right
in a similar way you could iterate over
them but we're not going to do that in
this one so now you're actually ready
for making the project are you excited i
am so let's get started
so now we're going to use our
dictionaries in our project are you
ready i hope so
so the project here is actually two
smaller projects and one is called
capitals that's a small game and the
other one is frequency count so it's
more a functional way to use the
dictionaries which i find really nice so
the goal is to make a game who's called
a guess a capital game and the other one
is
frequency count
so
description of the guess a capital game
well the thing is you should use a
dictionary obviously this is a key thing
we are learning in this session here
right
to keep country capital pairs as key
value pairs right
and i just put an example down here you
can use whatever you want these are the
capitals i managed to figure out the
rest of them i probably wouldn't
no
just kidding maybe i would know one more
right
and then prompt the users three times
for a capital in a country it should be
a random one obviously or that's up to
you of course
print the number of the correct capitals
after the game right feel free to print
whether answer is correct and what it is
right that was the first one right
so do as far as you can and i hope you
manage this one you should be
doable it takes some time obviously to
figure it out
and then go to the next one description
of uh frequency count right
prompt the user for a line of text right
you just need some text there right use
a dictionary to make a frequency count
off the letters print the frequencies
yeah
i would print the frequency with a loop
you don't have to but that's just makes
it looks a bit nicer i think so
are you ready i think so
i'm ready and i i'll be waiting for you
in the next lecture and uh hoping that
you spend some time trying to solve this
it's for your own benefit right the more
time you use trying to solve projects
the more you learn so see you in a
moment
not a short moment
a few moments see you bye
so are you ready to see some solution
here and uh did you try it out i hope so
because it's good for you to practice
and even if you tried and you came to
the solution and you see i do it
differently it doesn't mean that your
solution is wrong often people get a bit
confused especially in the beginning
it's like wow there's so many different
ways you can solve problems in does it
mean that your solution is wrong no it
doesn't maybe yours is actually better
than mine i don't know and uh
you always just have to keep in mind
that yeah as you learn along the way and
the more tools you get the better
solutions you can make right but it
takes practice and experience what is a
good solution and what is a bad solution
you have to look at the context right so
let's get started so
strictly speaking it doesn't say that
you need to take a random capital of
here but you just need to take
three of them right but i'd like to take
them random when you do it randomly of
course and when your list is so short of
this one you're not guaranteed you'll
get three different capitals and that's
just and compromise i'm willing to take
in my solution so i import random
because
i would like to take a random city
random country to quote question what is
the capital right
so let's try to do that so first of all
let's call it capitals here and take the
dictionary down there and then we have
the dictionary right let's just add
another cell there
and just to see their capitals and then
we have capitals here and you can see
how it is here right
perfect
perfect perfect perfect
good
so that was the first step right
and this next step is we want to take
keep track of the score of the person
right we already
know that that we are going to do three
questions from three times and
so we want to keep track of how many
answers were correct or non-correct
right and then we make a for loop and we
can make like four i in range three
that's three times loop
i can reveal that we're not going to use
this variable i here and often when
people are not using a variable but they
need to define a variable they use
underscore like that
it can look a bit confusing at first so
just leave it out there just keep it
simple okay so now you want to prompt
the user for a random
capital right or a random country right
so let's just say country here and then
we need a random one how did he do that
right
well we needed the keys right the keys
are the countries right
so how did they get the keys right well
capitals
keys right that would turn a
list right
we want this as a list
okay so far so good now this is a list
right remember list of the keys so this
list will contain of france finland
sweden and denmark right
and then we need a random one and that
was a random choice right
so now
let's just try to run this
country
now this one will
print three random capitals right so we
run it again it's three different
not three different but a different
order at least sweden finland denmark
right
okay so far so good
now we want to prompt the user what is
the capital of this country right so
the user will get
prompted
so we make a formatted string
what is the
capital of
country oh
country
question mark
space right
good so now we are prompted and now what
we want to do is check
if the answer is the same as the capital
in our game here right so how do we do
that right
so one way to do it is also remember
maybe there are differences in lower
upper case so often what i like to do is
just to say answer lower so we put it in
lowercase right you could also
do it differently but this is fine for
our purpose and now we have the country
we're looking for and that's a key in
this
dictionary right so capitals
capitals let me put the country in there
and then we obviously need to put that
to lower again right so if this is the
case we are correct right so print
correct
correct right
you can also say
correct the capital
capital of
country
is
we can just say answer
or we put
country
uh
that was not correct
capitals
country here right
exclamation mark no exclamation mark
here good and then we need one more
thing here right the score
equals score plus one right
and then we have the else statement and
then it's when it's incorrect right
in correct
the we can actually just copy the same
thing here and write the
this one here
right
then we have it again here right
and we needed to remember these should
be formatted strings
obviously thank you for reminding me
good
so this is the three times we're asking
we have the
for loop here and we ask take a random
country we ask for the country
we check if the answer is the correct
one if it's correct we write it's
correct and we add one to the score if
it's incorrect we write it's incorrect
and write the r write the right answer
and in the end let's just say print
your
score
we can just add it here format the
string score
and then we need to remember formatted
here right so are you ready do you want
to try i want to try oops
i did something wrong
capitals is not defined
did i write a wrong cap p
tolls here right so it didn't matter i
was typing enter twice that's why also
so fast right ko
ko ben hagen
yes i got this one stockholm
yes i got this wrong
paris
boom
so i got to score three can you do
better than me i think so because these
are also easy these are the three four
capitals i'm know by heart the rest of
the world i have no idea
perfect maybe a few more capitals i
don't know but as you see this seems to
work let's also just ensure if you
answer wrong that it works sweden
say copenhagen
that's incorrect
oh sweden
it's stockholm so then that's one is
correct and
denmark let's just call it helsinki and
it's incorrect right so you could score
one right you had one right answer there
right
because uh
the capital of sweden here was stockholm
i wrote copenhagen the capital of sweden
was stockholm i wrote stockholm one
correct capital denmark is copenhagen
and i wrote
helsinki right
perfect so it seems to work so far so
good are you ready for the next one so
now for the frequency count let's try to
look at this one here right so let's
recall it frequency count prompt the
user for a line of text use a dictionary
to make frequency count of the letters
right so how many times does each letter
occur right so printer frequencies let's
get started right so
we need to input the line of text so
let's just call it text
and we use input here and uh just write
a line of text
right
and uh remember how it was right you
created the dictionary let's just call
it frequency here right or frick
and
then we need to iterate over it right
and remember the iteration here we need
to iterate over text which is a string
but you can do that easily with a for
loop right so that's 4c in text and what
c does is it's assigned to character by
character or letter by letter and this
is actually what we want right
so what we can do now is actually
add this c here to it and then we take
the freak and get right remember this
get method there it will look up c and
return 0 if it's not there already and
then we add 1 to that
so that's actually it
but we also want to print it out so
one way to do that is actually to print
over the frequencies just print the
frequencies out like this print frag
let's just do that with start and then
we make it a bit prettier afterwards so
this is my text
and you see here this is okay well this
is a bad solution but i will like to
print it line by line right
and maybe also another thing that i'm
looking at is i have t and capital t
right maybe i just want to make them
lower
actually i could do that immediately
right to set c is equal to lower right
and why do i want to do it here instead
of just adding here because if i add it
here i also have to add it here right so
i just change c to be in lower case
right so if i do it now this is my text
then i get the three t's here instead of
having capital and
lowercase frequencies
but now to the last part so how did i
make a for loop over a dictionary right
remember
for key value
in
freck
and then we just needed to take the
items right remember
then we can print and let's just print
it formatted here
and we take
key
colon
value right
perfect so this is my text and then you
see it you have it here i think it's a
bit nicer like this right
right
and
yeah you might see that this is not
ordered and
this one is because of its space but you
can actually order them afterwards if
you want but we're not going to go into
that in this one here this is exactly
fine for our purpose right so you see
now you actually can count things and
again i want to emphasize that the
dictionary here
before we start i didn't know what are
the letters in the string we wrote this
is my text right it doesn't know
anything about it you just add them
dynamically along the way and that's
really nice this frequency get is really
nice as well
so you enjoyed it i hope so because this
is really really amazing and powerful
stuff
so
see you in the next one i think yes the
next one we're going to learn about csv
files which are ideal to write read into
dictionaries like this one it's going to
be amazing see you in a moment
in this one it's going to be amazing
because we are going to learn about csv
files you might say c is what
comma separated values files right and
what are they well it's basically a
great way to store data in a structured
way so let's just dive into jupiter
notebook and actually try to find one
and read one and learn your magic about
them right so let's get started
so we're here inside jupiter notebook
here right and the goal is actually just
to learn to read and process csv files
because we're going to use that in our
project and again the best way to learn
is to make projects so let's get started
so
i just want to jump over to here your
file structure here because here you can
see there are some files and the first
one we're going to dive into is this a
name csv right you see all of them here
are called csv files we're going to use
them along the
projects along the way here in this
course there but the first one is this
one here it's quite a simple one right
so what do we have on the first line is
a bit different from the rest of them
right so the first line contains the
names of the columns so they're comma
separated that means that for every
comma it encounters it's a new column of
data right so here we have first name
last name and age so this is a simple
one that just keeps track of people's
first name last name and age right so
the lines below are those with the
actual data on them that correspond to
them right so if you take the first line
airline two we have connor so connor is
the first name right ward is the last
name and the age is 15 right
and this
third line here we have another
record in it like remember we called
record when we talked about dictionaries
uh then the first name is rose the last
name is peterson and the age is 18. then
we have paul
cox
12 hana higgs 10 right
so this is a very short and very simple
csv file but it's just to get you
started and familiar with them right
so how do you do that right how do you
read these csv files how do you process
them right
well you import a library that does
basically most of the things for you
that's why we love python because it has
all these powerful libraries that does
everything for us
in
port csv it's called csv isn't that
beautiful it reads csv files and the
library is called csv so let's load that
immediately
so
now we have to work with files and
there's something called file pointers
and so on like that but
honestly you don't really have to figure
that much out just you need some simple
syntax there are different ways to do it
uh
i do it in two different ways i'm just
going to show you i think this is the
most preferred way because then it keeps
a less mess behind you
so it's called with first and then we
open and this open here what it does is
it opens the
file pointer right so this opens a file
and returns a stream here right
so this is what i talked about
the file
pointer right so here we have to write
the file name so remember it's on the
files and it was called
name it was called name records
csv csv right this is a file and another
thing we often need to tell these file
pointers it's whether we want to read or
write to them in this case we only want
to read
and
now
here's the syntax because we need to
simp
we need somehow to get this file pointer
this is like a pointer pointing at the
file that file system and let's just
call it f here right so
in the code which is indented after this
with there it is everywhere we have
access to this file pointer because
after it's not indented anymore it's we
don't have access it will close a file
again and while with this mess of
opening and closing files it becomes to
keep track you don't want to have
multiple people adding as the same file
because it gets a mess so you need to
take
access to a file and make sure you're
the only one writing to it or reading to
it currently right that's why you have
this open and close with a file pointer
to make sure it's only one people person
doing it at the time
if you are in a file system with
multiple users
good now we want to have the csv reader
what is that csv
well
csv here has a lot of
a lot of things here right you see here
a lot of things here what we are going
to do is called the dict reader right
it's probably as you can guess it
let's see what it says here
it doesn't say much here actually but it
is a dictionary reader obviously and we
just learned about dictionaries and i
just told you that records are awesome
for dictionaries so let's try to read
the data here
and all it takes is this file pointer
here
perfect
and what you now need is actually the
name records
because this is returning a csv writer
reader and now we need to convert that
to a list of
list of dictionaries you'll see in a
moment
name
records
and all it does is actually we just
convert this csv reader
to a list
so let's do that
now we have name records down here and
you see here this is a list
where normally you notice but this is a
list right the square brackets here and
the square brackets here means it's a
list and each item of the list is a
dictionary each dictionary has the
column name for each of them right first
name connor last name ward age 15.
just like in this one here what i said
first name connor last name word age 15
right you see
and so it goes on for each of the lines
right
so we already know in this we can access
the first one like this here and so on
so forth right
but
for our case here
yeah and
just one more step here
name records you could obviously also
access the dictionary directly here
because the first one here it says we
have the first item of the list and now
we want to access something in the
dictionary you can do that directly as
well like this this is again really
really nice syntax right then you get
corner obviously right so this is how
you can use it and obviously you could
take
another
list
item like the third one here and you'll
get hana the last one right because the
third one is the last one right
zero one two three
right
perfect right isn't that amazing i think
so
good so
how do you do that right so how do you
print out
all these things here
well let's try to make a for loop here
now we know the syntax of this one here
and we in this case we just play that we
know all the key value
keys in it and we're all only interested
in showing the key
uh using the keys to print something out
so let's do that so for
record in name
records
records
what do we do this is a list again
remember now record what is record
record is a dictionary right because an
item in the list is a dictionary right
so what can we do we can print format it
here let's just do that
and
let's just say we take record and then
we take first
first
name
[Music]
and then we take
also record
oops square ones
last
name
and let's just say we just put it here
the h here
record
age right this is all we want to do
right so see here what it does right so
it prints first name connor
last name ward
colon age 15 right so we have connor
ward 15 rose peterson 18 paul cox 12
hannah hicks 10 right so this is exactly
the data we had here and you see how
nice and easy it is to access it through
dictionaries right
amazing
this is actually all you need to get
started using csv files and csv files is
an amazing way to work with data
so are you ready for the project it's
going to be
actually quite awesome i think so yeah
we're going to read some nice files csv
files
and work with the data in a bit more
advanced way in this simple way here and
it's going to be amazing see you in a
moment
are you ready i hope you are because i'm
really excited about this one
so in this one we're gonna read csv
files and process data right and we're
gonna process a massive one in this one
or at least for our
our stage of our learning journey it's a
massive csv file okay
good so let's jump into jupyter notebook
notebook here and see what it says so
the csv file we want to process is
having car data right we love cars
so the goal is to learn how to read and
process csv files so the
goal of the project is also to process
files cars csv we'll just look at in a
moment and calculate the average
horsepower for each car brand okay
let's just jump into our
file structure here and you see here we
have the files here we have this files
here all the
library files here and we click it and
we have cars here and then we open it
inside here
and in this case it just
takes a few seconds because it's quite a
large one and sometimes but the notebook
is a bit slow here it is right
so here we have the first line with all
the columns here right and then we have
cars for each aligned below here and as
you see here it's quite a big one here
actually
we actually have more than 5 000 cars
here right you see there are lines
5077 is the last line number i mean this
one is empty down there okay
so for each line we have a car
so what do we have about the car
well it says on the first line here we
have height and we also just notice that
each line is broken down
in approximately three lines in my case
if you open it it might be a different
breaking up because the size of the
screen are pretty large font here
so it's height length width engine
driveline engine type engine hybrid
number of forward gears
engine transmission fuel city
fuel type fuel highway classification id
make i just want to say that this make
here
is a car brand so i guess it's short for
maker or whatever
model year year horsepower this is one
last second one right we only have
torque afterwards so horsepower is one
we're looking for so if we take the
example of the first one here we have
141 141.4 blah blah blah all the way
down here and we see actually this is an
audi as it says here this is a make you
don't really see easily this is a make
but
i promise you it is
and the horsepower is the second last is
this one here right so this is the horse
power we are looking for so for audi and
you can see there are multiple audi's
down here
we want to collect all of these here
these second last values here and
calculate the average of that in the
prop project right
so let's go back to the project
so it's also broken down in in this case
four steps here to make it a bit easier
to figure out what to do right but the
goal should be clear we need to get for
each car brand so they're more than audi
in these 5 000 cars we collected
you want to collect the horsepower and
collect them and calculate the average
of them so how can you do that so one
way to do that is like this here so
first import c3 because we want csv to
do all the hard work for us right
and then we read the csv file into a
list of dictionaries and why we do that
well it makes it easy to access right
and then now we need to collect all the
horse powers for each car right so one
way to do that is remember we did this
frequency count right
so
at the frequency count we had a integer
but what if we had a list instead of
that integer well you can do that in a
python dictionary as well so instead the
value is a dictionary so we can actually
do the same trick here right
so use the dictionary to have a list of
horsepower for each brand and then we do
this for loop again right and uh
just a hint here
the car
the car brand is called make as i said
in the data set
okay
so that's how you access it so imagine
you have a for loop or a car
in the cars list you have
then you could use the get method again
right and this get method you should
take it for car make for karmic because
we are it's raining over the car makers
right we are trying to look for for
instance first case for audi we want to
look it up if nothing is there we
we return an empty list if there is and
then we add you know plus two list is
concatenate them then you concatenate
the horsepower to it right so that's the
second step
also remember
the horsepower here as you saw it is in
a
string
even though it would not be quoted in
here it would still read it as a string
because
that's how csv reader does it so no
matter what you should always remember
this is a string so you need to convert
it and in our case we you just need to
convert it somewhere and i'll
i'll do an sf4 you can do it
here if you wanted to
so use a new dictionary to keep the
calculated average horsepower we
calculated for the for loop over the
dictionary from step three right so
basically we do kind of the same thing
we just have a new dictionary and for
each one of them we actually calculate
the sum of that list and then we
divide it by the length of the list then
we have the average right
the sum of the list divided by how many
items are in the list the length of the
list is the average of the list right
yeah good
so
this can be done by a nested loop inside
this is what we this is how we're going
to do it in this one if you do it a bit
different it's fine you could do a bit
smarter than this but this is also
easier to understand maybe
good are you ready i am so let's get
started
are you ready for this i hope you try to
yourself and again don't get confused if
you didn't manage to do it all because
it's it is a bit tricky i think this
exercise here or this project here is a
bit difficult
and uh but still you know
trying to solve things where you're
maybe not 100 comfortable this is where
you learn right and this is what we're
doing in this project here right
learning along the way right
so so far so good so let's just do it
step by step right
so
import csv that should be
straightforward right import csv
boom there you have it
you read the csv file into a list of
dictionaries and again if you don't
remember the syntax by heart it was
covered in the last one you just needed
to change uh change the file you're
reading right
so
if you don't remember the syntax nobody
does that actually most of the time you
just look things up again and
unless you use it on a daily basis it
takes time to get it into your
into your household into your
into your flow so you just know it by
hard right
so this one was called cars right
perfect like how did i type it in so
fast i use tab
it will auto complete it for you and
then we do read and then as far right
and then we had the csv csv reader here
and then we take the csv and we took the
dict
reader remember and it just takes the
file right remember that good
and uh then we just need the cars let's
just call it cars here
because it's a list of cars you could
also the car list but let's just call it
cars here for simplicity list of the csv
reader right so now you actually have
them all here but again if i just print
a knob there it's going to be a long
list so let's just
use
just look at the first one here
and you see here here we have the first
car here
and it is the first line of the csv
files if you wanted to check it right we
have a maker audi and it says horsepower
250 right so you can just check here
this is the first one
it's an audi and it has horsepower 250
right so this is actually nice right and
again you see the advantage of reading
the dict reader it is it puts it really
easy and nice to get
and also as mentioned
make is a car brand
okay
so the third one use a dictionaries have
a list of horsepowers for each car brand
and hint
the car brand is called make in the data
set and use get car make
here and concatenate the horsepower to
it right so let's try to do that right
so
let's call it car
brands
and have a
dictionary for that and then we do the
for loop for
remember the cars is a list
of cars so for car in cars
that is we have a car here and this car
would be similar to have a data set
similar to this one here right the first
one is actually this one here
so what is it that we want to do well we
have the car brands here
and then we want to put the car
make here that's the car brand
and
what do we want to do well we take the
car
brands and we do the get method here
right and
then we are looking up if we have
something already
a car if i have something already on car
make right the car brand if not we
return a empty list right so instead of
returning 0 here we return the empty
list this is a nice trick and then we
concatenate the horsepower of this one
here so car
here we take the horse
power right
that's basically it
that's basically it yeah so let's try to
execute that
and uh if you look at car brands now
let's just look at that one and we just
take audi we know audi exists here we
don't want to read it all we have a list
here right so lots of audi is actually
here we have a list here right
and uh
this list here
is
containing the horsepower for each one
of
the cars of audi brand right
perfect
i actually want to delete this one here
again it's too large to be there okay
good that was second step
or third step right and then we have the
step four
use a new dictionary to keep the
calculated average horsepowers we will
calculate in a for loop over the
dictionary from step three
so the hint is to use a nested for loop
inside the for loop to sum up the
horsepowers okay and i i do agree you
can do this smarter than i'm gonna do it
here but
it's also to keep it simple
good so
car
brand
horsepower right let's let's call the
dictionary that i'm very
very good right
so
now we have the car brands and this is
this one here this one has the list of
them right
so what do you want you want to have
four
car and
powers right this is a list of
horsepowers
in
car
brands
items right remember this is how you
iterate over a dictionary right and
don't worry i call it here this is a key
and this is a value right so the key is
a car brand actually
so maybe we should call this one car
brand to be more
specific and this is a list of
horsepowers right horsepower list let's
call it that
right
so
this
is a key this is a value right so what
do we do now right so
we want to sum up the list here this
list here and divided by the length of
it afterwards this will be the
average horsepower we are looking for so
let's try to do that so
horsepower sum
we start by zero and then four
horsepower in horsepower list right
then we take the hp sum
and we
add
here the end of hp remember hp the
horsepower in the list is a string right
remember we are keeping everything as a
string here inside
the list here that's what i said you
need to convert it somehow somewhere
we're just going to do it down there you
could also have done it here right i
didn't
and then we have the sum of all the
horsepowers now we need the average of
it right so car
brand hp
and then we have the car here
and we add yeah we need the carb no we
don't we just need this
hp sum here divided by the length of
of the hp list
right
so what i'm doing here right so here we
summed it all up all the items and then
they divided by the length of the list
which gives the average right so let's
execute that and
unhashable type dict
oh yeah i didn't call it car call the
car brand here right
thank you for reminding me right so i
was forgetting i didn't i changed this
car to car brand right
so you needed to put the correct one
here right perfect awesome so now you
actually have the car
brand hp here and now we're just gonna
list it all we have not that extr
dream long list now right so we have all
audi here so we see the average of the
horsepower is around 300
and uh bmv here is a 347 a volvo is 247
and just to take one volkswagen is 120
i mean of these specific brands here
right this is not scientific or anything
right
is there anything interesting on this
list here
aston martin that's pretty nice car too
british car 470 ferrari 540.
is there anything else did i miss
something
i often miss them oh there's my favorite
lamborghini i know i know i'm a
lamborghini fan
nice
perfect okay so this actually does all
we wanted to do right
we have done what we wanted to do we
wanted to have calculate the average
horsepower of each car brand right and
again this this number doesn't really
make any scientific sense or anything
it's just you know to learn to do some
tools and uh i love cars so why not do
something with a car list you can do it
with your own project with something
else now because now you have the tools
awesome huh
so
unless anything else no let's see you in
the next one bye bye
recursion and this is amazing because
this is one of these things that a lot
of programmers
don't really understand at the beginning
and i understand it's a bit tricky but
recursion actually doesn't give you more
power as a programmer but it makes you
solve problems that are complex in an
easier way
right remember again the sin of
python readability is everything simple
is everything right you need to keep it
simple
so often why you would use recursion is
to make things simpler and easier to
understand the problem with recursion is
in the beginning
often people find well this doesn't make
it simpler because it's difficult to
understand but that's
just until you get
so that's what we're going to try to
capture in this one here we're going to
capture it so we're going to do it
in this lecture where we're going to
start with looking at a simple recursive
function
and then we're going to solve one of the
classical problems uh fibonacci sequence
i don't know what it is don't worry
about it we'll get there but this is
something which is actually defined
recursive um maybe get you started
understanding recursion in the project
it's gonna be amazing we're gonna solve
the tower of hanois
the tower of hanoi is a crazy problem to
solve and when you think about it in the
beginning it just seems like this is
impossible to solve how can i program
this but when you get recursion it's
going to be simple
yeah i know often
i remember myself when i got the problem
in college when i was a student i didn't
really get it in the beginning but
suddenly just fell down and just wow now
i get it and it's just like how could i
spend so much time figuring this one out
maybe because it wasn't explained well
but uh
yeah recursion just flew over my head in
the beginning but when you get recursion
it's going to be amazing it's going to
make things easy and make some problems
easier to solve so are you ready i hope
so because this is so exciting and let's
jump to jupyter notebook and get started
so in our jupyter notebook here we have
the title recursion
and the purpose is to learn about
recursion and recursive functions so
recursive recursive functions are using
recursion right
so just a little quote here in computer
science recursion is a method of solving
a problem
where the solution depends on solutions
to a smaller instance of the same
problem right so basically you have a
big problem and you break it down to a
smaller problem so how can we exemplify
this symbol right so let's try to make a
function right so this one should count
this function should count
and the point is that this should count
the values
uh if you say we call it let's just
assume here we call it count six here
right what this should result in is the
sum of six plus five plus
four plus
three
plus two plus one right
this is what it should return right
so this is a sum right this is a count
it's actually not the account it's just
maybe count was the wrong name for this
one right but don't worry about that we
can live with that
how can we solve this right
well
we can solve it with an uh with a loop
right a for loop but we don't want that
in this one here we want to solve it
recursively right
so the key thing remember when you call
it with six you should
depend on a solution of a smaller
instance of the problem that's what it
says right
so if we return here now instead we say
six for instance and we add count 5.
what will happen now right
well
first of all
we should probably not use 6 here
because it's n that we are calling with
but then we call with n minus 1 here
right
and in this case we have broken it down
to a smaller problem while this
definition sounds pretty nice this is
one problem if you think about it right
so let's actually just try to run this
one and
run it down here and what happens is
well actually nothing happens until it
says recursion error right
and what does it say it says maximum
recursion depth exceeded right so what
happens it keeps calling itself right so
remember remember here we call this six
it's a six here and then it calls with
six minus one is five then it calls
itself right it calls itself here and
then it's called with five and then five
is here and then it calls itself with
four calls itself the three two
one
zero
minus one minus two minus three and so
on and continues right and this is bad
why is this bad because it never ends so
we need a base case right so if n is
let's say less equal to zero
less equal to zero then we return n
just keep it simple right
because what this will do is when we
reach n in this case it will return
reach zero in this case it will return
zero
and then it'll return all the way back
right let's try to do this now right did
i execute this one above no here we go
and then we get 21. let's try to see
that right let's try to write it out
there so
count
six
well it is this one here right and then
we change
six to
the n to six and n minus one to 5.
count 5 here well this is this one again
right
and we put it in here and then we put 5
here and then we add 4 here right
but 4 here it's again a call to itself
so we get it again
and then we have three
four here and
three here but three here again it's a
call to itself so we do it again three
two
and again the call here is call again
and we have two
and
one
and one here is also a call so we call
again one and then we call this zero
right
so what happens when we call with zero
remember then we come into this one we
just return zero
so we return zero here so this is
actually what happens right and then we
have six
plus five
eleven plus ten four four fifteen plus 3
is 18 plus 2 is 20 plus 1 is 21 plus 0
is 21 right so we get the 21 here right
so this is actually correct right so you
see how the recursion is working right
we call down and down and down and then
we return the value value the key thing
to understand we need the base case here
otherwise it goes crazy as we saw
previously right let's just remove this
good
now that was a simple
count example as i showed here
now we want to go to the fibonacci
numbers right and fibonacci numbers are
defined like this
and you can read about more about them
down here if you want i mean don't don't
bother because it's all defined here
right
so the fibonacci numbers is a sequence
of f0 f1 f2 f3 and all the way up right
so f0 is defined as 0 and f1 is defined
to 1.
f2 is defined by this formula right so
if 2 equals
so n is 2 in this case then we have f
n minus 1 that's f1 and fn minus 2 is f0
right so f2 equals f1 minus plus f2
right
so the sequence begins as follows 0 1
yes 1 again 2 3 5 8 13 21 and so forth
let's just try to play around with this
so if we put f0 here to be 0
and f1 to b1
now we have that right
so let's try to make f2 here right so f2
was equal to f1 plus f0 right so let's
just also just get the value here it's 1
again right so remember the sequence is
0 1 1
0 1 1 right
then we have f 3 here which is then f
2 plus f 1 right because we're using
this formula here f 3 is if 2 plus f 1
right
and let's just
get it out here it is 2 right as it
should be right remember
zero one one two so the next one should
be three five eight let's just try to
make those
f
four is x f three plus f two that's f
four
and those three f five is equal to f4
plus f3 so we have f5 here and i can
continue like this i think you get the
point and you also see a problem here
this is this is not nice programming
right this is this
i need f 100 right how do you get that
right hmm well i continue like this
forever right you don't want that you
want a function that can calculate this
for you so how do you do that
yeah you're ready okay so let's try
def
uh
not def def def fibonacci fibonacci here
we just call it fibrin and call n here
so the base case
is actually
f0 and f1 right
so if
n is less or equal to 1 we just return
n right
that's it
get it
perfect
then we return and this is where it
becomes tricky right we return this one
this formula here right
and this basically means that we take
fib
off
n
minus one
plus
fib
and minus two
i'm
mr minus there right and that's
basically it
let's try it
fib
we calculated three up there or four
there
it's three right
as we see it should be f4 is three so
fib four is three right and
fib
five
is five right and fib
six
is eight
right
thirteen twenty one thirty three right
she will just try
fib
seven
thirteen fib
eight
twenty one right and you can continue
like that but you see here the
simplicity of this
formula here now
most people don't appreciate it just
seeing it like this but just take
yourself a moment and look at the
formula up here
how would you calculate this
if
you couldn't use recursion
it is possible yeah it's not a problem
it just becomes more complex and
if you're curious about that there are
many you can find that online as well
but this is just you know when you just
figure out recursion how easy it is wow
it's amazing right
so
now we understand what recursion is and
how it calls sub problems
like we did in this case here and how
you need a base case here to return
otherwise it will continue forever if i
took this if statement out here it will
get the same recursion error we did
before
okay so now we're ready for the tower of
hanoi it's going to be exciting i'm
telling you it's going to be amazing so
see you
in a moment huh yeah good
tower of hanoi this project is going to
be amazing it's going to be a big one
and
i don't know if it's big one but it
takes time to understand it let's let's
say it like that right
so the project here and the goal is to
solve tower of hanoi
recursively
you are free to do it non-recursively i
just say recursively is easier when you
get it right
so what is the tower of hanoi right
the tower of hanai is a mathematical
game which was which has three rules
before we set the rules let's see how
our universe looks like right so the
universe is we have the three towers
here or rods
and
then there's some discs and the base
simple case people solve first is this
case with three disks you can have many
disks on top here but there's something
you should notice that there are
different sizes so we have disk of size
one two three and if you had more disk
it would be four five six and so forth
uh
perfect right so this is it so this is
the universe and the goal is to get all
these discs over on this rod or this
tower over here right
perfect
so as i said here the disks have
different sizes and the goal is to move
the disk from
one tower the first one to the one with
the following three rules right
so we have three rules so one other
thing you can only move one disk at the
time
only one disk like here i take one disk
here right you can only take the top
disk and place it on top of another rod
right so you take this one one on top of
this one if there's nothing it's on the
top right
you cannot place a bigger disc on top of
a smaller disc that means you cannot
place this one on this one right
so the first two rules combined means
you can only take one top disc and move
it right so you can only take top moves
you can never take number three here
before the other ones are removed right
the third rule says that you cannot take
disk number two and put it on here
because it's bigger than this one here
right
you get it okay
now
how do you get from here right
to here following the three rules
i encourage you to sit and think about
it for a moment
and uh maybe you get an idea but it's
often difficult to just you know what
should i do just experiment right and
that's what most people do experiment i
think i did that too myself the first
time i saw it right
so it's just experimenting with back and
forth but that's not a good strategy
right and this is where recursion will
come into the picture right
uh
because it's not easy and
before we continue also just take a
moment also
then imagine that you need to program
this
and we're gonna do that in this project
right but imagine you have to do that
right now with without any more thinking
and i'm pretty sure that you're gonna
get stuck in this one because it's it's
difficult
very difficult
before you know it and then it's easy
so here's the trick you ready
how to solve the tower of hanoi
assume you can solve the smaller problem
of two disks right
so basically this is a recursion right
you solve the
smaller problem right
so now we just assume you can solve that
that's a recursion call remember you
call down to something that can solve
the smaller problem and then you solve
the problem you're in right now so now
you just make a recursive call and say
remove the two disks
remove or move to smaller disk
then you can move to disk at the same
time right so that's what you do how
would you solve it now right so you take
the two disk and put them here
and then you move the third disk from
here to here
and then you take the smaller problem
again and move them here
right
that's easy because you can
in recursion you just assume you're
moving solving the smaller problem right
just like that with a call and that's
what recursion does for you
right did you see that now you actually
have the recipe of solving it and i know
at first here it might be a bit complex
right
how did he do that but again just
imagine this is a recursive don't don't
worry about this one right you take the
two discs and put them here
then you take the third disc and put it
over here
then you take the two small discs that
are here and put them here and you have
solved the problem right
with that recursive call the recursive
call does the exact same thing
so this is the formula and all you need
to know right move the smaller problem
of two discs in this case and it could
be one disc if it's even smaller when
you call again down there and move them
from the first rod to the second rod
move the big disc from the
first tower to the last tower right and
then move the smaller problem to this to
the second
to the last right from the second to the
last this is exactly what we did here
right move the two to here
move the big one here move the two from
here right perfect
now we have the recipe so now we just
need to use some hints here to do it
right
so step one
represent the towers as a list of lists
right so you see here we have a list of
lists and the second list has uh
disks with numbers representing the
sizes right and if it's empty there's
nothing on the rod so this is the
beginning situation right
that's step one this is how we represent
them right
[Music]
step two create a move function so we
create a helper function right a move
function which takes the towers and move
a disk from one tower to another tower
right
and how do you do that well you can use
pop and append on these ones here right
so there'll be a from disk to t from
tower to a two tower right or something
like that from tower to tower right and
then you do a pop and then bend
then step three make a helper function
to print the towers and the reason why
you want to do that is if something goes
wrong it's also really nice to see how
it works but also
if you want to see how it actually moves
it is really really fun to have and this
one doesn't have to be fancy it's just
you know just print it out i'll show you
how i would do it and it's just
simple because we're going to assume we
have three towers and three disks
because obviously you could have
multiple disks here and then you have to
modify this printer function and the
functionality we do would will work
whether we have three disk or 10 000
disks
okay and then the recursive function
right solve tower of hanoi we call it
that it will have the towers these are
the towers here then they have n i'll
come back to n then that'll start tower
destination tower and auxiliary tower
right so what do that
what does that mean right
so if we are here in the beginning
here right
then we have a
what you want is we want this is the
from tower and this is a two tower right
and then we have an auxiliary tower here
right
and the end is
how many disks we are having in this
recursive call right
perfect
perfect right
so n is the number of disks we move
starting with three and then the cursor
called down to one and zero right
the base case is n0 then just return to
that case and we don't do anything right
because in this case we don't need to
have a start tower destination tower or
auxiliary tower we don't do anything
because there are no disks to move right
if we had one disk we needed to move
from the start tower to destination
tower right
good
and uh
what we do then in our recursive call is
remove the sub problem for n minus one
disk right in this case we take two
disks
uh from the start tower to the axillary
tower right and then you move the disk n
to destination tower right
and if you want you can do the calling a
print function right after this one it's
going to be look nice
and then finally we move the sub problem
of n minus one disk from auxiliary tower
to destination tower wow this sounds too
easy and it is this is exactly what you
need to do but it takes time
to figure it out
i encourage you to spend some time
solving this it's it's amazing problem
and you're going to feel awesome when
you solve it and don't worry if you
don't really
get it finally well
jump along to my video the next one here
and then we'll solve it together okay
see you in a moment
are you ready for this i hope so and i
hope you try to play around with it
because it's gonna pay off remember
again your learning is the mo you get
the most out of this if you try to play
with play around with it before you see
the solution and don't worry you will
learn
even though you didn't solve it yourself
because now you see how to do it and
then i still encourage you to do it
afterwards again actually because
sometimes you need to have multiple
times and it doesn't mean that
you will never get good at programming
me personally i've done things that i
didn't get i had to do it redo it again
and again and again and it took long
time before i got it but the key thing
is to continue until you get it right
perfect are you ready let's jump to
jupiter notebook so we are right down
here at the tower of hanoi here right
starting of that and we're at the end
here where we just need to implement it
in the last part we went through how to
describe
understand it so we jump straight right
right into solving the problem
good so at step one here we need to
represent the towers as follows right so
let's just do that let's just copy it
down here
towers we just call it and then we have
the towers like that and uh as you see
you can just see the towers like that
it's just list of lists right so
in theory if you wanted the first
rod or tower you can just access it like
this and the second one like this is
empty and the third one is here right so
the goal is to move them all from one to
the other right so this is step one
that's pretty easy right
then we need to create a move function
which takes the towers and
moves a disk from one tower to another
tower so let's try to do that right
so def
move we just call it and it takes the
towers and then just call it from tower
to destination tower right
let's just call it like that
and this function here
it just assumes we're not doing anything
illegal because if we are
yeah we are
either we do a move it's not possible
which is uh making an exception if it's
an empty list we take from or not or we
make a put a bigger one or a smaller one
but we can make a print function so we
can verify we don't do that okay
okay let's start so
how can we do that right so
we need to
take a disk from the from tower right so
let's do that so
towers
and then we take
from
tower
and we do a pop on that one right so it
takes the last element and then we need
the towers and then we do
destination
tower and then we just appended right
the disk here right so this is basically
it and and
it is a list but i really often like to
return it as well
strictly speaking it's not necessary but
uh then you have like uh
then you can just like
i think the syntax is easier to
understand so you don't get confused
whether
you do something different with towers
right
so let's let's just try there right so
towers
move and we take it from uh
and take the towers there and we take it
from zero to two right boom and now we
can look at the towers we have the one
the smallest one so we took the smallest
one and added over there right
so
again we can just move it back towers
and we take two to zero right and then
we have towers here and it's back to
normal right perfect so far so good
right that was step two
step three make a helper function to
print the towers hint assume we have
three towers and three disks right so
don't try to over complicate this
function here it's just to help you
right def so let's call it print towers
right and then take towers here right
perfect
perfect perfect perfect
so for i in
range and
what do we want here we want to
the three
the three towers
and we can actually do it we want to
print it from the top right so at the
top we have the top one so let's
actually do a
a
how is it called
a negative count here and you probably
didn't see that
here but you can actually start from
three here and count down to zero
exclusive and then the step is minus
one right so if we actually print i here
you see here that if we called
print towers
and we need something here towers here
we'll go three one
three two one right
i don't actually know if this is the
best thing we're doing right now but
let's just do it like this okay
so for
so for tower in towers right so what
we're going to do is now we take each of
the towers right first this one this one
this one what we want to know is if the
length of tower
is larger than this i value because in
this case we know that in the first case
it's three we know we have three items
in it right what do we need to print
then we need to print that item right
and how do we print that item that's
tower but we want to print it from the
reverse
order right
or do we no we don't we don't we want to
print it from so we actually want to
print from i minus one right so if we
have that number of elements we want to
print the element before right that's
how it is and this should also be equal
to right
and then we want an end here and we just
use space here to have that right and
else what do you want to print else we
print just something here i don't know
should we print a pipe
and end here and
also need a spacer and after this one we
need to print a new line here right so i
think this should actually do it right
and we can also just for the fun of it
print a
base here
and i think they should do it right so
we kind of have like the base here
and uh
yeah i think maybe this is one too much
i think this is fine
i mean maybe we actually want a space in
front of this i don't do we want that
i think this is fine i think we just do
it like this right so
is there a space coming too much here
one two
one two three i think so so where does
this extra space come from oh i added
these spaces here that's why
okay now it's like this here
and um
yeah
good question
perfect
perfect here we have a visualization of
the towers it's not nice it's not pretty
but uh yeah maybe we just add an
additional space here right boom boom
boom yeah i don't know you can play
around with it
honestly it's just to show you how it
looks like it doesn't really matter that
much okay that was step
step
three right so step four
the recursive function right so this is
actually all we need to do right so what
i'm going to do now is just to copy this
step here down actually we just move all
the text down there let's just do that
because what i want to do is actually
have it when i write the code because
because
here we have it right so let's do this
function here and we just copy that
right so def here right
perfect this is what we want to do so n
is the number of disks we move right so
the base case is this one here that
means actually so if n
is equal equals zero we can also
sometimes just to make sure it doesn't
matter here equal to zero we just return
as it says right
and then we have this case here right
and remember we can put comments in the
code like this if you want is this
hash symbol here
this will not be executed right
then actually we
do the same we call the same function
here but now we need to be strict right
so we call it with towers again but we
call this with n minus
one obviously because it's a smaller
problem and then we take the disk from
start tower to auxiliary tower right so
this is actually what we do
here right
perfect right so we switch those two
around right
and then we do
this one here
you see
then we move the disk right so this is
where we actually do the move here right
so we do move the towers
and then we take
from
start tower
to destination
tower right so this is basically what we
have been looking for right so we move
the smaller problem so this one here it
moves a smaller problem from start tower
to auxiliary tower right this is what we
do
in
this one here we take the smaller
problem put the auxiliary tower and then
the next one is we take number three
here the big one and moves in over here
right
these are these two steps right
and then
let me do this one here
let's see if we can do it correctly now
and again we have a smaller problem and
then we take it from auxiliary tower to
auxiliary tower
and we take
start tower
no this is destination tower we need it
too and then we use start tower as an
auxiliary right
start
tower here right
perfect right because we can use we can
put something on top of that right
awesome and then it actually said here
we could have print
towers here right
towers
are you ready to try this
i'm not
okay so
towers
let's just put them here so we know we
have them correctly
nothing more annoying than not having
them correctly
and then we call this function here
and
what we're going to do is
yeah let's just print
towers here
towers
and then we take the towers we take
number three here start tower zero
destination tower is two auxiliary
towers one right so let's try to do that
so first we start by
printing the tower and let's look at the
end we have the correct solution here in
the end right we have all the three uh
disks here right so what it does along
the way right it just takes here
so the first call it does is it calls
recursively down on this one here so it
calls with two right and when calls with
two it starts and this start here again
and then it calls with
one here and when calls one here calls
with zero here and zero returns so then
it returns with one right and then we do
the actual move so the first move we do
is that move here right
so we move
this one here then we move this two here
then we remove move disc one back right
so this is the recursive method right
and then we're back to the next layer
recursive move the three here and then
we are back in the recursive move one
here and then two here and then one in
the end over here right
you see
i highly encourage you to check it out
again here it is
i know it's a bit confusing at first
when you see it but it's amazing
and
it's magic to be honest it's magic but
again you see
the mindset you need to have is just
thinking how can i solve it if i can
solve a smaller instance can i then
solve it and yes i can and then you just
call down and then you need a base case
right and then it's salted right this
recipe is quite simple just sit and
think how you would solve it without
recursion now right it'll be complex it
is possible but complex
so are you excited i hope so this was
amazing see you in the next one
list comprehension right people love
this aspect about python and i do
understand why because it makes your
program more readable
if you understand them right and again
if you're on a level where you don't
really understand this comprehension it
doesn't make your code more readable but
your goal is to be able to comprehend
this comprehension right enough talking
let's let's jump to jupiter notebook and
see what these comprehensions are both
list and dictionary
here we are
comprehension the goal is what is a
comprehension learn list comprehension
and learn dictionary comprehension both
are awesome so
get ready for this
okay
the best way just to see it is just to
see an example and see how you do it
with a normal code right so let's make
my list here
and like make a list comprehension so
this comprehension is basically
you do
write some code inside here that does
things for you right so i for i in range
let's just take 10 right
so what does this do right
well if you let's just try to read it
and guess
i for i in range 10 right so i in for i
enraged 10 that means that i is
incremented from 0 all the way up to
10 exclusive right
so
maybe that's what i is i for i n right
let's try to see it
execute yeah that's what it did right
so
this is nice but let's just think about
it how would we do this if we didn't
have this comprehension we can do that
right so you can make empty list here
and then you would say for i in range 10
and then you would say my list
my list
append
i right this is how you would do it
right because now you have my list here
is
the same here right
so basically what you have done here in
this comprehension is taking this one
here and folded it inside here so you
avoid creating an empty list out here
and appending to it it appends
automatically inside the list right so
this is one way to use it another way is
actually you could also make
calculations inside there so actually if
you take the same thing here let's just
use mice list again here then you say i
times i for instance for i in range
10 again right
so in this case here
what you've done here is just
multiplying i zero is zero zero times
zero is zero one times one is one two
times two is four three times three is
nine
four times four is sixteen and so forth
up all the way to nine times nine is
eighty nine so you see you can also make
something in here and you can make it
even more advanced actually
and similar you could do the same here
just multiply by multiply here right so
this is basically it so i want to
emphasize here that here you could also
have a formula if you wanted to but it's
not needed
good
but
what if you probably say
what if i only wanted every
all the even numbers right
you wanted to do something if statement
right well my list can do that or this
comprehension can do that as well right
so here we have i for i in range
10
if
i
percentage two equals e
equals equals zero right so what does
this do let's just see it first
it takes all the even numbers right 2
0 2 4 8 6 8.
so what this statement here does it
actually says we do the same here thing
but only if this is true right
so it's kind of
i don't know if it's logical syntax this
one here but if you read it i
for i in range if i
is zero right remember this one here the
remainder calculation right this is
taking all the
even numbers right remember that right
good so this is perfect right hmm
interesting what if i wanted an else
statement and this is where i think the
syntax is kind of i don't know
it's beautiful let's just say that
if i
uh i
if
now we actually change the syntax you
see here i else
what could you do at
minus i
uh let's just do that for i in range 10
right
so
so here we have it right so if i
still if i is here
then we add this one here else we put
minus 1 or minus i here right you see
now they come negative here right
before we just excluded them but here we
just add them as negatives you could do
whatever you wanted here right again you
could have functions you could have
whatever in these two ones here right a
4i in range of the same thing here right
so you see you can have the else
statement there yeah if you have an if
statement it's only behind if you have
if and else it's in front
yeah i don't know
maybe it makes sense maybe it doesn't
make sense i don't know this is a syntax
so we love it because python decided we
have to love it right
perfect right
we can do similar thing with a
list inside it right so let's just put
my list here again to be i in
for i in
range 10 here then we have that one then
we have my
another list here
which is comprehended by
what should we do here
i
times i for i in
my list right so you probably guessed
that that this is
obviously the same thing is happening
here it takes i from the list here right
so i just want to emphasize that it
doesn't have to be range here it could
also be a list here
or yeah
at least there right so we can do the
same for dictionaries this is pretty
nice actually so let's put a dictionary
here and let's say we actually want to
do the same thing here but now we want
to have a key value pair right for i in
in let's just take my list here just to
emphasize it right
so what it does here as you see here it
has index or key here
zero
as the first one here the first one of
uh where's
my list here we put it here
and then it multiplies multiplies as a
value right so key value key value key
value key value key value key value key
value right so see how nice this is so
you can make a dictionary in this sense
here
so this is a lot of new things and i
think the best way to learn about these
things is actually to look back on on
some of the projects we made and
try to use this
comprehension and see how we could have
solved those problems differently if we
knew about this comprehension right are
you excited i hope so because this is
this is going to be amazing see you in a
moment
are you ready for this project it's
going to be amazing because now you're
going to see how you can make
make some of your old codes in a this
conventional way right are you excited
about what well there's probably more
than this but this was the things these
were the things i thought you should do
because these are just you know it's
obvious to make this comprehension or
dick
dick dictionary comprehension dict
comprehension right good so here in
jupiter notebook we're with uh dick list
and dick comprehension the goal is
obviously to learn how to use a list and
dict comprehension right
and
what we need to do is redo some of our
old code here right that's that's all we
need to do
so remember csr cipher i don't know if
do you remember that i think so good
because back then we used use this uh
function encrypt char
before we use the encrypt message
function right but in this one we're
just going to use this encrypt chart
function and this comprehension to
encrypt the string you are awesome right
and hint you can use join right
so
i left the function here for you to use
it because this one is a bit complex to
remember and i didn't want you to go
through that again right so you have the
character here and so on the ordinary
the yeah never mind about all that we
covered that so far so you have that for
free to use so all you need to do is use
this function here this comprehension
and you probably need to use join
remember join is a
method on strings
so if you join something on the empty
string it joins a list right remember
that okay good
perfect and then we want to decrypt it
afterwards right and uh we use the
decrypt char here and you see this just
called encrypt char with
26 minus key here right
remember that okay and again you use
list comprehension to decrypt the cipher
it's just to see the circle
works
good
then for the last one here we need to
use addict comprehension for frequency
count remember when we added a text
we could calculate the occurrences of
each character in that text
so prompt the user for a line of text
right use addict comprehension to
conduct count the number of currents of
each character right for example a a b b
c c c cccc should give two a's right two
a's two b's and five c's right
and hint here you can use count on a
string and it will return the number of
currencies of a right
so does that sound exciting i hope so
because it is really exciting to use
these list and dictionary comprehensions
and uh don't worry if you don't get it
in the immediately that's why in the
next one i'm gonna show you how i would
solve it and uh
yeah
use some time and leave some comments on
what you find is difficult do you like
this comprehension this is one of the
people's favorites about python remember
in the beginning of the course i had a
list of what people love this is the
biggest those are the biggest surveys
done on python so this is what people
love about it okay so you're ready see
you in the next one
are you ready i hope so because this is
really really exciting uh we need to do
list and dict comprehension did you
manage yourself and you just want to
sure make sure that i'm not doing
something fancy well let me know in the
comment down below there because this is
really exciting right
okay let's jump right into it right so
remember the encrypt char
originally in the csu cipher had another
function encrypt message and this is
actually the function we want to
implement with list comprehension and
join actually it says here right
so
let's just remember this encrypt chart
let's just execute it right so remember
it just takes
encrypt charge just take a char here for
instance let's take the y from you are
awesome right and it needs a key right
and the key here let's just say three
right so it would encrypt y to b right
this is what it says here right
and i'm pretty sure that is correct
right so we need to use this one here in
order to
encrypt it right so let's jump
immediately down to have the message
here
and
we want to
encrypt a string it doesn't say we have
to encrypt this one but let's just take
this one but we need to notice here that
this is lowercase and we have uppercase
here but we are getting comfortable
about all that so one way to do it is to
take the message and immediately put it
in uppercase or whatever we want
we will do it
in one way or another right but first
let's look at this comprehension right
so here we have it right so if we take c
for c in message
we actually have it here right so we
have each character in a list now right
you are awesome right and one way to
make sure it's upper is just to call
upper on this one for instance you are
awesome right
so if we call directly encrypt message
on this one we will also encrypt the
spaces and remember we didn't want to
encrypt spaces in the old ones we need
to use an if statement but let's just
for now just call it here and let's just
add a key here and
say the key is equal three right
uh
it didn't like that actually it was it
was not called encrypt encrypted char
thank you for reminding me right
good so now we have it here except that
we have these
here are encrypted to something
the spaces are encrypted to something we
don't want right so
we only want to encrypt it if
c
not in
the sequence of space we also use comma
and dot
else we just need to leave c as it is
right this is a syntax right so we do
this if this is the case else we just
leave the character right so now we
actually have the encrypted message here
right the problem is that as is here
our cipher list let's call it cipher
list here this list here is
is character by character right so in
order to make that to a
string we can use join as it says here
the empty join here on cipher list right
so then you actually have it as a string
here right and that's actually what we
wanted right
perfect
good
that was actually it
thank you for helping me so the next one
here is going to be easy right so
basically we have this one here
which you can take the cipher here and
this one here and immediately we should
remember to execute this one here
and then we take key again here to be
three right and then we take a message
list again here
make it in two steps here
and then we have
decrypt
char
c
comma key
if c
not in
one of these
else c for
c in
uh cipher
upper we don't need upper in this case
but we just do it anyway right
so here okay so now i'll forget to join
it right we need to join it here
bum join
message
list right
here you have it you are awesome so this
went pretty pretty easy right and again
it's the same syntax we did up here
uh
ifc not in this one here then else blah
blah blah run you see it right
isn't that amazing so
basically
basically this is like two lines of code
you could combine this in one line
and uh
except have instead of having this uh
decrypt message and encrypt message
methods or functions we made
in the csr cipher do you feel cheated
now i hope not because this is just you
learn more concepts and uh
if you see this the first time you see
it i don't think it's more clear what's
happening i think actually the
for loops we did did in the caesar
cipher exercise it's more
explaining what it is but now we
understand that concept we can take it
here to the next level where we
understand the list and dictionary
comprehension right
good
now ready for the
frequency count there where we need dict
use addict comprehension right so let's
try to do what it says here prompt the
user for a line of text text
input
input line of text
thank you user
use a dick comprehension to count the
number of currents of each character
okay
let's try to do that
okay so we have a
dictionary here
and
how do we do it so
we need to
take
we have the text here right so we have
the c here and then we can have the
count right the hint here and the count
here on the string then we're gonna take
the text here and count on it the c here
for c in text right i should actually do
it so input the line of text
a a b b c c c c
and you see it you actually have it here
right
so
did this go a bit too fast
so what happens here right because you
might see some
small details here right that's
maybe a little bit fast right so what
happens here right so for c in text it
takes each character in the text right
so first it takes a
and then it says
a
key a should have text of count a that
means it counts a number of currents of
eight that puts it two perfect a maps to
two
then it takes the next character but the
next character is also a
so it puts the key a and maps it to
[Music]
two right because the count of them is
two so you actually overwrite the same
one again and it doesn't matter actually
and then it takes b and does the same
and it overrides again and then c it
overwrites actually five times the same
key of c right
so you might think oh isn't that a bit
overkill to do the same again and again
yeah it is might be so alternatively you
could actually
you could actually have what's called a
set here this is like a bonus
if you are comfortable i mean this is
this is fine up here
this is just a set right so what does a
set do right
well let's look at that right
so
text here we have it here
and then we
set of text here so let's do that right
so aa b b c c c c c boom
and you see here
the text here is this one here right
this is obviously the text we entered
here but the set here it actually looks
a bit like a dictionary except it
doesn't have the colons here right it
says we have a b and c so this is a set
is like the unique
unique
values in an iterable right like a
string right or it could be a list then
it takes all the unique elements of it
and puts it in there right so in this
case here it only takes a
it takes b and c so it doesn't just do
it multiple times right
and the count does the same thing right
so this is basically
just
ensuring for instance if this string was
extremely long and extremely many
duplicates right then this might be
faster right you put a step here where
you remove all the duplicates and only
have unique values and then you iterate
over that instead right
so this is just optional
and if you like this it's fine it's
maybe better
otherwise it's fine to do it as this one
up here right because basically you
didn't know the set thing here i just
wanted to show you so you know it for
another time right so how do you like
list comprehension and dict
comprehension please let me know in the
comments because this is one of the
things that people love about python if
you don't get them yet please let me
know too because maybe it needs more
explanation uh some more practice and
then you'll get them and you will love
them monday i think so please let me
know
see you in the next one
object-oriented programming this is
amazing because this is one of those
concepts you just want to master
why you might ask because
object-oriented programming will make
your programming
easier to understand because it wants to
try to resemble reality that means the
things you program
looks more like reality you know so you
don't need to do so many transformation
on how the computer understands things
so in order to understand this concept
we're going to create a game together a
card game where we're going to introduce
objects of card deck and hand so three
different types of objects that we're
going to use to simulate or play or
write our game right so
enough talking i think the best way to
learn is just to see it and
start doing it are you ready i hope so
because this is going to be exciting so
let's jump to jupiter notebook so in
jupiter notebook here we have the
object-order programming so the goal is
to learn the basics behind object
oriented programming okay so first
question first what is this thing and
what is a hype about
at its core object-oriented programming
helps you with structuring your programs
to resemble reality that's what i said
right
so you kind of create these objects
from definitions of classes i'll get
back to that in a moment
but these objects are resembling reality
in our case we're gonna start by using a
card game and we're gonna have the
objects types of card
deck and hand okay
so
more
said differently right that is you
declare objects with parameters and
methods
you need on them right so again here on
the card we need a suit and a rank and
we need something called sdr and lsn
that's a string representation of the
object and the less than so these have
double underscores in front and
and back of the
methods
it means it's kind of the s built-in
python things that are used by all the
all the python functionalities you might
say
the deck here on the other hand it has
cards which is a list of cards and then
it has a length
method and then add card pop a card and
shuffle the deck
the hand here is actually said to be a
subclass of the deck and what that means
is actually that
the hand resembles the deck in many
senses right it has cards you can add a
card you can
pop a card remove a card right or
shuffle the cards in your hand right but
on top of that it has it has a label it
might be your
your hand so you put your name on it and
it might in this game here we're going
to have a win count how many games you
want right so
the hand is going to remember that so
it's also kind of hand off the person of
the player right
and for this hand here we also want to
be able to print it so that's a string
here again this is makes it easier easy
for us to get the
string representation of the object and
this is a standard python method so
again so we can when we call print a
print will automatically call string
here to get the string representation
then we have the get label which is the
label of the hand and the round winner
that increments the win count and then
they could get a win count which is
the current count of the hand right
so this is kind of a diagram and
you see multiple ways of drawing these
diagrams i'm not going to go into them
but these each one of them kind of
describe the classes that we are going
to implement in python and the classes
is kind of the recipe of the objects so
the objects are the one that we create
and use inside so the class defines them
objects are instances often right so in
a card right we can have in a decker
card we usually have 52 cards if there
are no special cards right so there will
be 52 different cards with different
suit and different rank in our game
right so these are objects in the deck
here the class is defining it the deck
is for instance having 52 cards in the
beginning and then you play along you
maybe you take some cards out or you add
some cards on top of them right and you
can shuffle it
also the hand it's similar to the deck
in that sense
it has the same functionality or methods
down here and some additional ones here
right
so this concept of
of a super class and a class here this
relationship is commonly used in object
order programming because you say for
instance
you categorize things in reality too
right so for instance take fruits right
a banana is a fruit an apple isn't a
fruit right so they both have the same
characteristic of being a fruit so the
fruit is a class superclass and the
banana and
banana class and apple class are
instances of fruits right
so all fruits can you can say are
eatable right so
one thing you can do with the fruit is
to eat it right well you could do that
with all fruits so you put that in a
super class when it makes sense you
don't want to over complicate things but
you know just to get it there so are you
ready to implement this
again you don't know how to do that so
that's why we do that in the session
lesson here and in the next one you when
we implemented the three classes here
you're gonna implement the game it's
gonna be awesome right a card game right
a real card game amazing
okay
so how do you define a class
do it by class
class card and a colon and a colon again
represent that the code internet after
that belongs to this class right
so in python as i already told you you
have these double underscores here these
are the methods that are
belonging to python syntax so we're
going to create a
init function
which is like the constructor which is
called when you construct a card object
and the syntax of classes is it has
itself along with all the methods the
self is referring to the self so
everything you assign to self can be
accessed anywhere inside the object okay
so let's just try this one here and say
self and we put a suit here and we add
this suit to be let's say
let's say
zero right
and self rank here we put that to zero
here in this case here so now we can
create an object of
class but right now all of them will
have suit zero and rank zero so let's
just exemplify this so we create a card
here
and
this card here we'll call this init
function here when we create the
instance of this one here right so here
we have card we can see suit here is
zero and if we try rank as well it's
zero as well right as you see here right
if you created another card it will be
another instance of it
and it will have the same student rank
because we hard coded them here so what
you want to do is actually be able to
put suit and rank in here suit rank here
as arguments and assign them here so now
you'll see what happens right now we
execute this one and when we execute
this one here we need to add the suit
and rank so let's for the first one just
add zero
zero and zero and let's just call it c1
for card one
and uh let's make a card two immediately
here
and have card here
one by one for instance right
and now we can see c1 here
suit here is zero still but
c2 here suit here is one right because
they have been assigned to different
values
inside here in the init function here
you see
so that's how it works this one is
assigned specifically to these
variables here which point at the
objects of the card and each card object
has its own set of suit and rank
variables we have inside here right
now there's a lot of things to
comprehend right but try to play around
with this so you understand the concept
here of these two things here so if it's
all new to you i would advise you to
stop the video and try to play around
with this code here and create some
instances so you really understand the
differences on objects and classes
good
so this was it actually but i actually
want to do something a bit more advanced
here in a moment but let's just keep it
at this
let's do the string here because when we
print it right now
if we print
c1 here you see it's
something that doesn't really make sense
here because
python doesn't know how this object
should be represented right so when you
call print on it it will just say well
this is main card object yada yada yada
right it doesn't make sense to us right
it makes sense to python because it's
object at this address here
which you can identify with it but when
we want to print an object we want it to
be represented in the suit and rank but
we can tell python that again by using
overwriting one of the
built-in functions which this one is
called string which is a string
representation
and as you see immediately i need to
put the self here as an argument which
is never sent to the object but it does
automatically by python and all the
thing we are assigned to the self can be
accessed in any methods here right so
here we will return the string
representation of the object so let's
try to do that
so it's excel
is it ranked first
yeah i think rank first and then we have
the self
suit here right
so this is a string representation of
our objects and you see now we changed
the class of that but we didn't change
these objects down here so if i was
printing again here
nothing changed but if i execute this
one here where i create new objects here
then it'll actually change and you see
here we have zero zero
and we can represent it as we want we
could say this is rank and this is
suit right
and now we have it again here and i can
actually print both of them inside here
right rank suit ranks you right you see
now
i want to represent it a bit nicer so
actually i throw in these two here
and what is that right so inside the
card class i sign some
lists here to represent the suit
by
unicode characters and you'll see in a
moment what they are they are the
hearts and clubs and so on spades and so
on and the ranks right i've done
something with the ranks i started by
two and end by a's right so these are
ordered in the row of uh value in our
card game right
but this is quite of nice but what it
also can do for us is actually make it
easier
prettier to print it so this is just to
make it pretty print
so how can i access this well i can
access those by
immediately just
access them
as a list of cards
on the object card right so that's
card here
uh ranks here
i think i did the opposite here it
should be ranks here i was talking to
you and
then
and then i messed it up
so here it should be suits right i think
i did it right now right
and then you see here then you get them
like this
right
three of hearts right
uh
perfect right so this just makes it look
prettier so this is strictly not needed
but it's just
yeah these are the unicodes right
so apparently this is hearts
hearts of three y is three when it was
one one right you see is because the one
index is this one and the one index is
this one right so this is unicode for
hearts and this is the value of 1 which
is 3 now right but that's because we
want ace to be the highest value
when we do our game
you don't know that yet but
that's how we rank it right so this was
a string
but we want to continue
we want to make something called a less
than and this is comparator because if
we compare the two right now if we say
c1
is let's say c1 is less than c2
it doesn't know what to do it because it
says well type error not supported
between instance of card and card
because we haven't defined
the function yet so let's do that lesson
self it takes itself and then the other
perfect
and what we want to do here is actually
if the rank
is the same
of the card that means we have
for instance we have three
of the rank and the other card is three
then we go to the suit and ordered by
that so let's do that so if
self rank equals equals other rank
this is the other card
in that case we return self suit
lessons
other suit right
and else
else we return
self rank because now they're not the
same suit
rank other rank here right so
what we can do now is compare them right
so
this less than comparator again this is
python doing all the
things for us it will take and take
another card and call it other and then
they will call it on this and then they
can order them based on that
and we just say how it should be ordered
it says if we have the same rank well
then we use a suit in this order up here
right
and if the rank are different then it's
a rank counting so let's try to execute
that
so in this case here we have true that
obviously these are smaller than this
one if you put it opposite it will be
false right so you see that
so that's how you can implement that and
now we have now we can order the card so
we in our card game can say which card
is higher
higher rank than the other card right
isn't that nice i think so
awesome
perfect that well that was actually the
first class here we did here so now we
have card class here and we can create
instances of this card now the next one
here is deck
so deck has a list of cards and it also
has a length
method and then they have add card
pop card and shuffle let's try and do
that
now we're actually getting experience
right so class deck
we need to do this init function because
what we want to do in that in the
function is
have the cards right the list of cards
there
so def
init
self
here
and then we do self-deck so so far it's
just an empty list let's just do it like
that and the first
method we implement is the length method
this one here right
and this is quite simple this is
actually so when you have a deck of
cards what makes sense to be the length
of it right it's the number of cards we
have in it right
so that will be basically return the
cell
or the length of self
self
attack here right
perfect so let's try to make a deck here
take here
deck
and then we can take the length of that
deck here and it will be zero right
makes sense right in a moment we will we
will create uh we will add some cards to
it right
def
add
card
self again we have to add self and then
we have a card here
and what should we do we should add the
card to the deck so we take it self
append
card right so let's actually try to do
that right and again we need to execute
this one here but let's just do it all
in this cell here so we took take deck
add
card and we just we had the c1 here and
then we take length of deck
and you see something went wrong here
and it says here add card
add card
deck object has no attributes self
oh it says self self that was my mistake
thank you for correcting that one
perfect oh it says again takeover is no
no self it was self no not sell self
itself tech i should
append here right thank you for helping
me out on that one right so now you see
here that
the length of
this is one now right because we added
one card when we create a deck here
similarly we could add another one here
deck add card c2 here right and then it
will be length of two now it should be
obvious right
good and we can also have the
pop card
pop card
itself it doesn't take anything but it
returns itself
and it takes a deck now i have to
remember right and pop from it because
this list we can append and pop right
and if we added these two we can have uh
deck and we pop again here
not poop pop
and uh it should be pop card
and there's one back here right perfect
you understand it now right
good so far so good so this is add card
and pop card
let's just jump back to the init method
here because in our game what we want to
do is have these 52 cards and why not
let the deck itself add these cards to
it and we kind of made that nice
remember how the card is defined right
it takes a suit and a rank right and the
suit is from index from zero one two
three and the rank is from zero one of
all the way to twelve
okay so let's do a four loop and a four
loop so four
suit in range
four right that's one two three four no
zero one two three thank you for
reminding me
and for rank in range
thirteen it's from zero to twelve now
you get it right
i get it at least uh self
deck
deck
append
and then we take a card and let's exude
and rank right so you see what's
happening now now we added all of them
to there so let's try to do that
actually dunk
and let's not do this one anymore let's
just create a deck and see there are 52
cards in it now right so you see
now we added all the cards and they're
only unique cards in it right you see
because we take it suit by suit and rank
by rank and add them one by one
and as you see here the final thing we
want to do is shuffle so let's try to
make a shuffle and to do that we need
some randomness right randomness we love
randomness
so let's just import random here import
random boom and random has what we need
right so let's just say def
shuffle
self
and then we take
random
[Music]
shuffle
and then we take
then we take
self deck on it right boom so what i
want to show now is that if we take the
deck here and we
print let's say we take the deck and
pop
card
what happens if if we play it again
again is the same card right
but what happens if we take deck now and
shuffle
what do you think
well we get a different card from game
to game
right you see
awesome right
and maybe we actually
just use the detail that actually we
just every time we
make a deck of cards let's just shuffle
them you could do that by calling self
shuffle right then you call shuffle on
yourself right so if you
create that then we actually don't need
to call shuffle here
then we get a new card every time right
so that's actually the deck so now we
are ready for the
hand over here
so what is the hand the hand is of type
deck and i'll show you how to do that
it has cards again it has a label and
win count and then it has a string
representation it has get label which is
the label we set here and a round winner
and get win count so let's try to do
that it's going to be exciting huh
so class
hand and because it's a type deck we
just add the deck here
so if we didn't create an init function
in this one it will call this init
function here right because it's of type
it's a bit confusing at first right but
hand we declare that of type deck that
means it inherits all these methods we
defined here
so but we also have the option of
overriding and we're going to do that
because in the hand we don't want
we don't want to shuffle a
total deck of cards so what we're gonna
do is init
here
and we need self and we need a label
also so it's also already different
right but we need to remember to have
the deck here because we don't
otherwise we don't have it right so this
is it
and then we have the self label
and we set the label and then we
remember we had the
win count was the win count i think so
and we add the win count there
so these are the
variables
or the parameters that our objects will
have right
and the next thing we wanted to do was
um
yeah what was the thing we weighed a bit
with string actually or should we no
let's just add string immediately def
string
and we have the self here
so string should represent the the
object that we're creating right so
return
i actually think we should take the
label here and we can just concatenate
here and then we concatenate a colon
so we have the
label of the hand the colon and then we
want to
actually we want to
take the deck we have and print it out
right so we do that by a join here
but this is catch right because we don't
need to call string on each object in
the deck otherwise it will not look nice
so string
card
for
card in self
deck right you see what's happening here
so we take card by card in the deck and
then we call string on then and then we
have the list here this is list
comprehension right
and then we join that on this one here
actually we should have a space here and
then here right i think this is actually
nice so
now we can have a hand here and this
hand here it will take a label and let's
just call it player here and this hand
because we are of type deck here we can
call the methods up here so let's add
card
add
card and we can actually take our deck
and pop card right
let's do that and then we actually print
hand
boom
you see here that's it and uh actually
we can do
a few here just three just for fun and
you see we get three cards in there
okay you see how that worked
awesome right
perfect
uh
good that was the first step of hand and
then we needed to do get label round
winner and get win count that's
straightforward
def
get label
self and then we just have that return
self
let's just call label it just returns a
label we just need that
and if you get
win count
myself and uh return self
win
count
perfect and then death
uh
round winner wasn't that what was called
round winner itself and what that does
is just take self
win
count equals self
win
count plus one right so you win one
round right so
yeah basically what this does is
actually right let's just
play around with that
i executed that one above there right so
i can get for instance i can get
a label here then i get the label
i can get the
hand get
wind count i get the win count it's zero
but if i add it
and
round winner
then here in the case i would have won
one round right in if i added two rounds
two round winners here i will have two
rounds here right so this is basically
it this is amazing right so we created
this class over here as well and we'll
learn
what this deck
means here that it
inherits all the methods of this deck
here
and so we can access the same method on
the same object that's that's amazing
right so we have card deck and hand now
we are ready to create our first card
game i am are you i hope so because it's
going to be amazing
so
if you find this a bit confusing don't
worry i advise you before you start
play around with these classes
until you get the aspect of it the key
thing is to remember is that these
objects are resembling reality right
this is why you create them they
resemble reality so a card is a car that
deck is a second hand in his hand so it
comes in handy
when you do your programming because it
becomes easier for you to
to
to model your program inside there and
it can be easier to understand okay so
see you in a moment it's going to be
exciting see you
i hope you are excited because this is
going to be exciting you're going to
create your first card game using
object-oriented programming so let's
jump to jupyter notebook and look at
that right project object-oriented
programming create a card game goal
learn basic object-oriented programming
create a simple game right so in the
last one we created these three classes
here which we are going to use the card
the deck and the hand
and
the card game is simple as this one here
we start simple right
so the card game create a deck of cards
create four players p1 p2 p3 p4
okay
divide all cards to the four players
okay
assume you are p1 print the hand of p1
then the game has 13 rounds right this
sounds like a four low each player plays
one card
the player with the highest card wins
update the score for the winning hand
print the cards played in round in the
round and the winner
with the winning card
after the 13 rounds print the score of
all players p1 p2 with p3 p4 so this is
actually the recipe of what you should
do and
while just stepping through these might
be a bit
more complex than you think right but i
hope you manage and the classes are
available here so you just import them
and then you just try to follow along uh
the implementation of the game here and
see how far you come and if you get
stuck
in the next one i'm gonna help you so
don't worry
it is a bit confusing at first but every
time you try something and the more time
you longer you try then the more you
learn right it's also okay to get stuck
see until then and then try it yourself
again afterwards right remember
the more time you spend programming the
more time you start learn the more you
learn right
for me i've been spending hours for
coming nowhere sometimes feeling like
that but in the end you will succeed
you'll learn something all the time so
see you
enjoy it and then i will help you
if needed see you in the next one
this is cool right a card game how far
did you come please let me know in the
comments and don't worry if you didn't
manage all it's
it is complex to make these things so
let's jump right
down to it so
remember we have all the classes there
and they are implemented and obviously
you should use them right so
i want to use this recipe so i'm just
going to move it a bit down here and to
have it where i implement the
game here okay
so the way i'm going to do it is
i'm just gonna implement it straight
forward here right so
the first step is uh create a deck of
cards right
hmm let's do that deck it's a deck right
so now we actually have the deck of
cards here right so this is the first
step of the description so now we need
to create four players p1 p2 p3 p4
how do you want to do that right they
are hands right
these are the hand object right
never created up here right so a player
has a hand and a label right it's going
to be called p1 people p2 and so forth
right so let's make a list of hands
right so we
can reference them again right
hands so four
let's just call i in range
four so we have the players p1 p2 p3
before
to be strictly we want the column p1 p2
up to p4 so we need range from one to
five right that will index i for one two
three four right
good and then we take hands and we
append to that a hand and a hand takes a
label
and
let's make a format label here and put p
here and then we add the i right
so what do we have now right we have a
list of hands
perfect right
perfect so we have a deck we have the
list of hands so
divide
all cards to the four players it says
here let's do that
so how can we do that right so the deck
remember
the deck here has a length method here
so that means that
we can we know how many cards are in it
at any time and then we can pop a card
that is taking one card off right
so
easily said right so as long as we have
cards left we can pop cards so that's
how it's done right
so let's try to do that right so so
while
deck
while the length of
deck
is greater than zero right then we can
for each hand
forehand
in hands
we can take the hand and
add
a card
from deck where we pop
card right
so we pop a card and add it to the hand
this is amazing right so
let's actually just loop over our
hands and see how it looks like right so
forehand in
print hand right
and uh we didn't like that this deck is
not defined oh i forgot to execute these
things up here
boom boom right
and then we can execute this one here
right
perfect right
so now we actually took the deck of
cards we created a list of hands we
appended the player name and then we
took all the cards one by one and added
to the hands right
and then we have for instance this
distribution here if you played it again
it will be different
hands right you see here the cards
change every time i execute it right
perfect awesome
awesome perfect
good so far so good so we created the
deck we have the four players we divided
the cards assume your player one and
print the hands of p1 so now we can just
delete this
and we print here
hand off player one which is zero here
right so now
we have it it should be hands here right
so now we have p1 here perfect right
okay
the game has 13 rounds so how do we do
that right 4i in
range
13
perfect right so now we iterate 13 times
perfect
each player plays one card but here's a
catch right we need to be able to
remember
remember
uh
how many
uh the cards they play right so let's do
call it uh
played
cards
and make a list of that right
and uh just to make it simulation easier
no i don't know actually we want to do
that we just yeah let's do input here so
we take round by round and take input we
have to press enter every time right
and
then for hand
in hands
because each player needs to play a card
play one card
then we have
played cards
append
hand
pop
card right
then everybody played a card right so
the next thing we need to do is uh
we need to find the winner card right so
if we
just look at the cards
so for
card
in
played cards we can just print a card
here right so this is basically it so we
can press enter here and then we have
the cards which were played you see here
the first player played this one
obviously and then the other players
played these cards and then you see the
second card i played was this one here
and so forth down there right
but we need to find the highest card and
this is where the card
class comes in handy because we
implemented this one here so we can
actually take the maximum of that list
right
so basically what i can do here is
actually print just to show it print max
off played cards right
if i do that
uh what happened there
i need to stop first and then play again
then we see here
i printed afterwards so seven of hearts
was the greatest one here that's true
right and then we can see here again the
winner card was the ace which is true
right
and then we have the nine
is the highest card and here we actually
go by the suit instead right so you see
that it works perfectly and let's just
stop it so i don't make the same mistake
again right good so
winner card we have it right here
actually and we don't need to print this
out anymore we just need to say
winner
card is equal to maximum right and
maximum remember that takes the maximum
of the list right and we define the
order of them so that's why we can take
the maximum of the list here right
okay good so the next thing we need to
know is uh
who played the winning card right who
was a player right
and
one way to do that is actually to
iterate over it and find which one has
this card right
uh another thing is to just use some
methods
directly on the list and use it directly
yeah the player that wins the hand needs
to be a rewarded one point right so
that's the next step here so we need to
figure out what index is the player of
the winning card and we actually have
the order of the cards in here so the
one which has winner card is the index
of that one is actually the player which
one right so
winner card is here and
then we can take winner hand for
instance it is this one here which we
say
hands
[Music]
played cards and we just take the index
of the
the winner card
right
index here
index on this one here it is uh
returns the front index of value right
so this is basically what we want we
could also make a for loop where we find
the index uncounted but here we just use
the index function which returns the
index of that and that will index it
into the hands and we'll return the
winner hand right because on the winner
hand we need to say
we need to house a call
we need to how is that method called i
forgot again actually the winner hand is
called round winner right
round winner
so now we added one point to that right
so that was this one the player with the
highest card wins
update the score of the winning
hand print the cards played in round and
the winner okay let's do that
so
now we need to print out a full load of
things here right so the winner
now how what should we use to print the
cards played in round okay so we need to
print all the cards so maybe we do that
by a list comprehension so let's say
cards
played
we call the played cards and cards
played actually we have the cards played
there already
in that one
so can we just say
[Music]
let's just
let's let's let's let's try some things
here right so here we have
the round of it it is called round
i here
and then
we
use a list comprehension
and a join here
on this list comprehension on this list
comprehension
we will use the played cards
and we call str
on the card
for card in played cards right
that's correct and then we are going to
use
format again
[Music]
winner
and we have here
winner hand
get label
this is going to be a long one i
apologize for that
and uh
then we actually take the winner card
here right
we need to take the
string off winner card i think here
okay this is a long one right so let's
try that one
and then we just press enter
[Music]
winner
bound method hand okay i i did something
wrong here this one should have a space
here
and then we have winner
winner card get
a label oh i should have method call
here right
here we have it
and then we execute this one again
and then we have it here right
perfect right
so here we have round one
hearts of two two of hearts and then we
they play here and the winner is p4
which played this one here right and
then you can see forward like it goes
here and you go to 13 rounds and then it
stops
actually we count round zero two yeah
that's okay
and
in the end we want to play
we want to yeah what is it it says here
after the 13 rounds print the score of
all players p1 to p4 right so again here
we two four
hand in hands
you see
and then we do print
what do we do
score
we do a formatted string here
score for
for
then we have hand here get a label
and then we do
colon here and then we do
hand here we have
get
win count
would get win count there right
good perfect so let's try that again
here we have it right
so having get the scores at the end
there
so p3 you want this one here and you can
check that this is correct right
so you see here it's not that complex
actually
can i get everything here yes here
so in the beginning here we create oh
actually i don't get the deck here we
get the deck here in the beginning
so let's get it here we create a deck
here we take all the hands here and then
we
divide the cards we print the first hand
and then we do the 13 rounds
and then we play
a card from all hands we
remember them all here then we find the
maximal card then we find the hand that
one then we add a win around then we do
this crazy print statement here and then
after the round we print out the winner
okay isn't that crazy i think so and
this is amazing actually this is
actually object owner programming and
now you kind of get the idea of how it
works in the next one it's gonna be
exciting too we're gonna learn about
visualization how to visualize data it's
gonna be amazing i promise you that so
see you
in the next one bye
when it comes to data visualization in
python you need to master matplotlib
and remember in the beginning of the
course we had the list of favorite
libraries well matplotlib is up amongst
them
and
i do know that some people they are not
that excited about matt bloodlip but i
think also some of the confusion is that
actually matplotlib can be used in two
different ways and it's not obvious
first which way you're using it so we're
gonna explore that so there's a
functional way and an object-oriented
way of using matplotlib
and i think the functional way makes
more confusion than it actually should
do
so that's why i advise people to use it
in the object-oriented way and the
overhead is basically only one line of
code so i don't see any excuses of not
using it in an object or the way and it
will save you for
making mistakes later in your code
so are you ready to learn about
matplotlib i hope so because i'm really
excited about it so and so should you be
see you in a moment so we are here
inside our
juvenile notebook about matplotlib and
the goal of this one is to learn to
visualize data charts so
this introduction this lesson here is
only about showing you a few
methods of using matplotlib and then
we're going to make a project where you
use it on real data so here we're just
going to make some fake data and
in the project we're going to do it on
actually the cars we know we're going to
make a scatter chart which is really
amazing with colors and all and it's
gonna be amazing so you're gonna love it
okay so let's get started
so matplotlib is a library like so many
other things so let's import it first
but there's a catch when you import
matplotlib in jupiter notebook because
jupiter notebook interacts well with
matplotlib but can visualize the charts
immediately for you so you need to
communicate that so first of all let's
import it import
plot lip pipelot is the one using as plt
and again as plt is just to short name
it so now to the part where you
communicate to jupiter notebook how
should it visualize it so we're going to
use a matte
plot lip inline so the inline tells it
in which way you should do it so there's
also an inline is static and if you want
it to be interactive where you can zoom
in and do lots of stuff you can use the
notebook for that we're gonna we're not
gonna explore that one in here but i
just want to notice that this one one is
also a great choice
inline okay imported
so the first thing we're going to show
is
the functional way where you actually
call and create a
plot or a chart a figure
immediately from the functional way and
i'm just going to show it and then i'm
going to show the object order way and
explain what the advantage is okay
so
when you call it directly
you just call it on plt and then you
could call plot immediately so this is a
functional way right
and what we're gonna visualize here is
just like a line of code here a line of
a list here so this
shows it immediately right and you see
this is easy why would you use anything
else on this right but that's because if
you later call on some other data and
then sometimes it will update on the
same
figure here as it's called this figure
here so you just keep drawing on the
same figure because you haven't reset it
and get a new figure and so on so it
becomes very confusing so in the
object-only way you create a new figure
all the time every time you use it right
okay so let's just do that immediately
so i write figure and axis and then i
call plt again to create the subplots
as it's called so subplots is i think
the best way to create
create your own figure and access as is
called to create to draw on right so
subplots here as you see it creates a
figure this is a figure and a set of
subplots and the setup subplots are
called axis and we're only going to use
one axis so we don't put any arguments
here so so it's all only one x and then
one x is like you can think of them like
like the chart right in the figure you
can put multiple charts on it if you
want to but we're only going to use one
but creating this figure here it makes
it
transparent to you that where are you
drawing on
well in this one here you actually don't
know which one you're drawing on because
you use it in a functional way so it's
hidden all behind the
the thing
hidden behind the method right
so so far so good so how to draw the
same thing as up there is just call the
axis and you call plot on it and then
you
do the same thing right so you see the
only difference is you have one more
line of code above here
and then you call axis on it and you
call it an axis instead of the plc right
because you want to make sure you're
drawing on this figure here this figure
and this axis are connected together
because they came from the same call
here right so they are connected so when
you
call axes and you plot on it you plot on
this figure here and the figure is the
canvas down here you could see it right
the canvas here perfect
so far so good
but what we're more interested in is not
making simple plots like this uh simple
line we are interested in making
scatter plots and histograms that's what
we're going to use in our project
and scatter plots i find it's a very
nice way to visualize data when you have
a data set because it can show you if
there's some connection between the
points you're looking at but in there
in here we're just going to demonstrate
it in the project we're going to use
real data
perfect
so
what kind of data could we have right it
doesn't really matter one two three four
five six so let's just have six points
here and then the y-axis can vary a bit
more so we can have 2 3 2 1
6 and maybe just 10 here right so we
have the same number of items there and
then we have the figure the axis and
then we take plt
sub plots
okay there are a lot of methods there
subplots here and then we take the axis
and then we call scatter instead scatter
and we take the x and y axis on this one
here boom and then you put it here right
so here you see we scatter it right and
you can actually have multiple points on
the same
on top of each other here i just put
them on a nice line here but we can
actually have the same one here right so
we could put five i get him or four here
and on four we put three here right so
we plot it and we get it again we see we
have two points here instead so scatter
plots plots it just the points
uh point by point right so you see here
we have one point
one by two one by two it's this one here
and then we have two by three two by
three right here and then three point
two three by two
four by one
five buys
six
and uh
six by ten and then we have this last
one four by
three this one here right
so far so good right uh another thing i
like with the scatter plot is actually
you can put colors on it so let's just
actually add another line here and just
take one one two two three
three
four right
and then you can actually add the colors
here as
as the argument c and it the argument c
is the colors and it gets the list here
of the color so each of the
scatter plots
each of the dots will get a color right
and you see it figure out it's a way to
map these numbers to a color
so the same numbers obviously have the
same color and based on the data set
here you'll just figure it out by itself
it's really really nice so you don't
have to think about it unless you want
specific colors but other than that you
will find a color mapping for that is
unique
another thing you probably wonder about
what about titles and labels well you
can do that
you can say it's called set title and
you have title
title here
and you have x
set
x label and you have x label
and you have obviously said x y label
[Music]
and you have y
label on that one here right so here you
have it right you have the title i set
the title the x label and the y label
here and you still have the same scatter
plot here right so this is how you make
a scatter plot just a lists right and
the coloring like that
perfect
now to the last one we want to show
before we head for the project and
that's how do you make a histogram
so histogram is basically also like
remember the frequency count we had
right so if you have a list like let's
call it data and
let's just take some points
and let's just have three
points
here i don't know how much of each of
them
perfect let's just say that and you want
to
map that out on a histogram right so how
do you do that well again figure axis
and you take plt sub
plots
and then you take the axis and then you
take hist and then you take the data but
you want to communicate to the histogram
how many pins should be there and bins
are the number of
bars you're gonna put there so bins
and you put three in this case because
we have three unique values here and if
you don't know the data set it will just
adjust the data set in the number of
bins out there actually you can put less
bins here right so let's see that in a
moment actually so here we have it right
actually it's quite even out this one
here right
i'm actually actually not that good at
making
or maybe either i'm good at making
random things or i'm not good at it so
three bins
i promise you you can also have two pins
and then you see here you get two of
them instead it shouldn't make sense to
make four bins because
and then it just leaves one empty here
right you see
but
if you had more data
it makes sense to have more bins and so
on right
this is actually it and again you could
put the title
set
title here
to whatever you want
and uh
x label set
x label
x label and obviously at 6 x set y label
y
label right that should be obvious right
so now you're actually ready for making
your project now so are you excited i am
because it's going to be about the cars
and we're going to see if there's
connection between or visually see if we
think there is a connection between the
the horsepower and the torque
of the cars
yeah
it sounds exciting right we're gonna
read all the csv files with all the data
and figure that one out so see you in a
moment
so we are ready for our project here so
the project is named horsepower versus
torque and roll dies so we're going to
do two projects the first one is
horsepower versus torque and then it's
going to be roll dice so
the goal of this is obviously to learn
to make a scatter plot and a histogram
right
so the first one is horsepower power
versus torque so it's described here
what to do so
we start by importing matplotlib
pipeline as plt remember that line and
then we use matplotlib inline matplotlib
inline and then we import the csv
because we need to read csv files and
then read the files data
cards csv right remember this long one
with 5 000 cars yeah we're going to use
that again exciting
and then we list we
will read that into a list of
dictionaries like we used to
and then we use a list comprehension to
get horsepower torque and if you want it
fuel city
miles per gallon right because if you
want it you can use this one for
coloring i'm going to show you how to do
it if if you don't want to use this do
this bonus just do the horsepower and
torque and remember there's a twist
right the horsepower and torque are
are stored inside the dictionaries in
the beginning when you read it from the
csv as strings so you need to convert
them to integers
and then you use a scatter plot and i
say optional you can use alpha 0.25 and
color with a fuel city miles per gallon
right
the alpha argument here it is optional
again
it's option right you can add that to
the scatter plot and it will make the
the plotting a bit transparent
i actually didn't show you that in the
lesson but if you don't want to use this
don't use it or just add it and see for
yourself what it does
perfect so that was horsepower versus
torque now to roll dice
so
in order to simulate
simulate a dice roll
well we need randomness to do that and
then we create a function roll dice
which simulates the role of two dies
right
i think we should have done that right
and then we use a list comprehension to
create a list of 500 calls to roll dice
and then we're going to use a histogram
with 11 pins of the list
why 11 pins
well there are 11 different
scores you can have right 2 3 4 5 6 7 8
9 10 11 12 right that's 11.
good so this should be it are you ready
for that i hope so because i'm really
excited about doing it but remember try
to do it yourself on your own before you
see how i do it when you get stuck watch
me do it try it yourself again okay it's
how you learn the best okay so i can't
wait until to see you again so see you
in a moment
so how did it go let me know in the
comments down below there i would love
to hear from you
uh what do you find challenging or is it
too easy for you please let me know i
mean i just love to hear get some
feedback from you guys i love to read
that okay so let's get started so i
assume you already know the project
because i just read it to you or
maybe you forgot it so should go back to
the previous lesson right
okay so first of all of all import
matplotlib pi plot as plt and use
matpotlib inline let's do that import
matte
plot lip pie
plot as plt
matte plot lip
inline perfect
import csv okay
import csv this is pretty easy right and
then read the file cars
into a list of dictionary
perfect let's let's do that
so
how do we do that right so remember
with
open
and then we have the files and cars
and then we have to have in a read mode
so this is a file pointer and we do s f
colon and then we do all our data here c
s v
reader and then we take the csv and we
take the dict reader
and it takes a file and then we have the
cars we just call them cars here
and
we return
the csv or reconvert the csv reader to a
list right
perfect i just add a few more lines here
perfect
so so far so good so actually in cars
now we should have a long list of them
let's just look at the first one here so
we remember our audi here
this is uh audi and the make here
and
we have a lot of data but all we are
interested in is uh
horsepower and torque right this is what
they ask for
use a list comprehension to get
horsepower and torque bonus fuel city
mpg miles per gallon okay so let's call
it horse
[Music]
power
let me do this comprehension
so we have all the cars and car right so
what do we need to do we need to take
car
horse power and here it's important of
course that you spell it correctly here
if you don't do that you'll get a
mistake right
and for car in cars right so there's a
catch right now let's just try to do it
actually
what's the cat catch it is
well you see actually here the catch
yourself right
we have
quotes around it means it's string so we
need to convert this one to integers
right
so how do you do that well int remember
int so here we have it so let's just
look at that again
now it's it's
perfect
and clip that and a few more let's just
do the same here with uh torque
torque
like that
and we took int
car
torque
for
car in cars and let's just take the
fuel right so
miles per gallon i think we just call it
and we do the same here int
car
and this one i need to
spell out correctly here
just copy it there
for car in cars right so now we have all
three of them right
as integer lists so now we can actually
plot them as scatter plots i think
that's what we need right
make a scatter plot use alpha and c for
the color of mileage okay
so figure axis
plt
sub plots right now we have the plot and
then just take the axis and scatter
and we can just first take the
horsepower
and torque right so
now we'll just see how it looks like
right so this is how it looks like as it
is right
perfect let's just add some labels ax
set
on label
and this one has horse
power
and ax
set
y label
label and this is the torque
right so now we have the
uh what did i do wrong there
oh i've
labeled
here we go perfect right let's just miss
miss tybo a typo mistake
okay so this is what we want and then i
said i could put optional alpha
0.25 and why would i do that maybe i
would actually put even more because
what you see here is when they're all on
top of each other right it becomes
really dark but when you put alpha then
you can actually see where it's mostly
on top of each other and where it's not
actually if i put even less here
it becomes less and less right we can
experiment a bit with that and you can
see most of them are just on top of each
other here and you really really need to
put alpha low in order to see it
perfectly
good and the final thing we could do
here is actually to add color on it
right so c equals
not c equals c but mpg
here and here you see it right then you
see actually is there also connection
between now we have three
dimensions right
now you can see if there's a connection
between the miles per
gallon in city driving and how much
torque
and horsepower is it's obviously this
scenes that horsepower and torque go
almost hand in hand right it's pretty
pretty much close to a line the more out
here you come the more scattered it is
but it looks pretty much like a line
another thing is that the more
horsepower you have
then
yeah you don't see that on the coloring
here because you don't have the color
scale here but
the less
miles per gallon it drives a car right
it should be obvious right so down here
you have the best economy
fuel economy and up here you have worse
and worse and it seems you can see the
color becomes darker and darker so
there's a connection between all three
of them the more horsepower the more
torque and the more or the less miles
per gallon right so there's a clear
connection between everything right and
of course there will be some outliers
that are a bit outside
but that's how it is okay so that was
actually this one here right we did
everything right we imported we imported
we read the file we took the horse
horsepower torque and fuel and we made a
scatter plot with alpha and color with
this perfect
for the next one roll dies
so import random let's do that
i like that import random it's nothing
more fun than random
create a function roll dies which
simulates a roll of two dies
interesting dev
roll
dies
what should we return well you return
random
randint remember rendon returns
returns a random integer in the range a
to b including both endpoints so include
both so one two six
and then we add another
[Music]
die
rendent
1.6
[Music]
so roll dice
we get here right so we get a different
from time to time and we know the lowest
is 2 and the highest is 12
perfect
use list comprehension to create a list
of 500 calls to roll dice right now we
love this right so we just call it data
now
data
and how do you do this right
well
we call
roll
dice in this one
for
i in range 500 right
you might say okay so why do we need
this i here we don't you can also use
underscore but if you're uncomfortable
about that just use i it doesn't matter
boom now we have data
and data should contain 500 rolls here
we can actually take the length of
data and just to make sure that they're
500 right in 500 rolls
awesome and we can also take a minimum
of data it should be two
and maximum
of data should be 12 right but now we
want to see how it's distributed is it
as likely to hit 12 as it is to hit
7
i don't know do you know that
let's figure it out this is why we have
the histogram right so fig
axis
plt
sub
plots and then we do axis
hist
and we take data and we take bins and we
take 11 because we have 11 different
bins here and we can put different
number but 11 makes sense here right so
there's one for each one of them
and
let's just look at that simple like that
so what we see here is actually it is
not perfectly but you can see here that
it's less likely to hit 2 row 2 and 12
than it is to hit something around 7
which seems to be most likely and if you
actually calculate on it it should
actually be totally
triangular here it's not right but this
is because it's random so we have too
many of these we have probably too many
of these right so it should be exactly
triangular but it's not
so this is actually uh what we should do
in this exercise here and uh we came to
an end wasn't this exciting i think so
it's always funny to play around with
things and learn something new
and uh yeah i hope you enjoyed that one
in the next one it's gonna be amazing
now we're gonna the next stage we're
gonna
use some machine learning we're going to
learn a bit about numpy another favorite
library of python users and
we're going to use that to create our
first
machine learning algorithm isn't that
exciting i hope so you think so because
it is
so
until then see you it's going to be
amazing see you
machine learning are you excited you
should be that because machine learning
is the thing everybody's talking about
right and you want to master that i know
you do okay
so what is it we're going to cover in
this lecture well we're going to build
our first machine learning model and use
it right
so in this lecture we're going to learn
something about how what is machine
learning and how does it differ from
classical computing what power is it
that it actually gives you what kind of
problems can it solve right and
specifically we're going to make a
linear regression model which is going
to help us predict something right so
given some input it will predict some
expected output or expected decision
based on that so if you're ready well
let's get started inside the jupyter
notebook it's called linear regression
which is a machine learning model so the
goal of this lecture is to learn how
machine learning is different from
classical computing right
then understand machine learning on a
high level
we need to learn some numpy basics if
you don't know what numpy is we'll cover
that don't worry but numpy is basically
just a different way to represent the
numbers in a data structure than from
the
list we know and the dictionaries we
know so far right
so it makes it more efficient because
our machine learning on libraries wanted
data in a more efficient way
so this is basically what numpy does
it's a scientific computing library
then we're going to learn some
foundation with a linear regression
which is a specific model we're going to
implement
and then we're going to get ready to
apply it on real data because our
project it's going to be on real data
right we're going to create our own
machine learning model which is going to
predict something
good so
what is machine learning and why are we
all so excited about it right
well
first of all remember what is it that
machine learning does for us right it
can kind of make decision or prediction
of something right so given some input
right it will try to predict something
expected output or classify something
so it can make
more related decision based on like we
humans we see in an environment we
interpret it and make a decision based
on that imagine you you would do this in
a classical computing environment well
what do we have at our disposal we have
some if statements we have some loops as
well but how is that helping well the
loop can make us iterate over the
environment right the if statements can
kind of say if this and do that right so
you kind of have to program all the
logic inside and you need to predict all
the logic this logical decisions we need
along the way right so you see
if you have
very complex decisions to make it
becomes very complex to write all these
if statements you see
so
basically in classical computing it's
like very specific instructions when we
see this specifically here do this when
we see this specifically here do that
right
modern computing or with machine
learning as we say we have a more like
an algorithm we feed data and based on
this data it creates a model so it's
more like indirect computing and it can
predict
based on that data so it creates a model
on that data and makes prediction based
on that so later when we have more data
we can feed it with more data and make
better
prediction in the future right
so we keep improving
so set differently in classical
computing model everything is programmed
into the algorithm this has a limitation
that all decision logic need to be
understood before usage right again we
need to compute we need to program all
the if statements basically and if we if
things change we need to modify the
program right so imagine you make this
insanely big decision tree remember the
decision tree we made with the
with a
car insurance company well in this case
here you have a lot of if statements
together but if you change that decision
tree model
dramatically you need to change a lot of
code you can probably realize
with modern computing or machine
learning right this paradigm is chained
changed
we feed the algorithm with data and
based on that data we do the decision in
the program so we just re-run the
program with new data and the algorithm
improves by itself
huh
that that's nice
so a bit about how machine learning
works and this is a high level right so
on a high level you can divide machine
learning into two phases there's a
learning phase right that's the phase i
talk about where you feed the algorithm
you train it you make it better right
and then there's the prediction phase
right where we use the model and say
what is it that you think now what do
you predict from this based on this
input here right this environment here
so
a lot of things often focus on the first
phase because this is where you make the
magic right are you good at creating a
model it will predict really well
because the prediction in itself it's
not that difficult right so when you
have a model you just give it a new
environment or a new context and it will
make a decision right this there's not
much programming to it the real key is
to create a good
machine learning model so this is this
process is divided into steps on a high
level
so in the first step here we need some
training data again if you don't have
any training data how are you going to
make the model work so basically what
you need is some data set that
represents the future data you will base
your decision or predictions on right so
this is the data we will build the model
upon
then we have the pre-processing
pre-processing we need to clean the data
data and format the data that basically
mean we need to put it in a specific
format so when you work with a lot of
data often you will find there will be
discrepancies in the data there will be
data we cannot use and so on so and and
finally we need to format it in correct
way so the algorithm understands it
basically said we're going to use this
not so much on this part in our project
but we're going to format it in the
correct way for the algorithm because
the algorithm needs to be formatted
correctly
so the learning phase well
you actually divided in more categories
and supervised and unsupervised but
these are like the high level so you can
be supervised or unsupervised and
supervised learning this is where you
tell the algorithm where you have some
input where you actually know the exact
output right so you know that if you see
this input you need to predict this
output so you categorize this input is
predicting this output
so we can see this input and so forth so
you have a lot of data like that in
supervised learning right you supervise
it when you see this do that see this
predict that see this predict that right
you have a long list of all these
connections together and then in
unsupervised learning it's different you
don't know anything so you just say this
is input this is input figure out some
kind of
intelligent thing to say about it right
so that's unsupervised learning
and the final stage is where you test
your algorithm right so now you created
your model and you need to test how good
this model is how does it perform right
so this is where you measure it right so
often what you do is actually from your
training data
you divide it up in training set and
validation set right and the validation
set is then used in the far end over
here to check and validate that it is
predicting as it's supposed to be or
often you have a measure since this
predicts this kind of accuracy okay
so this is it
and
i have a bit more description here and
here also
here we have a description of the
supervised and the unsupervised learning
i also added actually the reinforcement
learning here it teaches us a machine to
think for itself based on past action
rewards and this one is actually going
to be in the capstone project where
we're going to make a reinforcement
model from scratch that means we're not
going to use any libraries when we
create it we're gonna
programming from
nothing it's gonna be amazing that one
this is kind of the key of this why i
love this reinforcement learning first
of all it is easy to understand and
secondly because you can program it from
scratch you don't need any fancy library
to do it so it will give you a deep
understanding on how it actually works
this reinforcement but don't worry about
that right now because we're going to
cover that in the capstone project in
the final project in this course
perfect
so
we are ready to get started numpy
so
remember in the beginning here we needed
to look into the numpy
library and we need it because numpy
library is efficient way to work with
numbers specifically in these scientific
libraries that we're going to use for
machine learning here they love numpy's
because it's more efficient way to
represent the data so let's just start
importing numpy
import it as np that's just a standard
you do that and let's create our first
numpy array so you'll see it's very
similar to a list but with limitations
so let's just make a array here we just
call it a1 because we're going to create
another one and
you just access this array with np the
library numpy and then you say you want
an array and this array just takes a
list
here good so this looks basically like a
list except you have this array outside
here so this has some differences right
so first of all let's inspect actually
it has something called d type which is
a data type
and when you look at that it only has
one data type what does that mean so the
entire array here with all the integers
and it could be longer if you wanted to
it only has one data type that basically
mean that all of the entries need to
have the same data type in this case
it's an integer and it is 64 because
it's represented by 64 bits
and that means that
the only entries
that are
available here has to be integer 64.
this is a limitation right except in a
python list you could had have any data
type in any entry you wanted right so it
could change but for a numpy array it's
the same one all along the way
this is a restriction on it and you if
you're new to programming you want to
why make restrictions well it's because
you can make other things more
effectively now right imagine you are a
program and you don't know how the input
is right and you need to program
something that takes some kind of input
you don't know what kind of input well
you make you need to make a lot of
special cases because if it's a string
you do this if it's a float do this if
it's an integer to this and so forth
right
when you know for sure it all the
entries have the same type of and
all the entries have the same type well
you don't need as many if branches right
it's easier to program it becomes more
effective as well right because you have
less
less special cases because it's just
like one case
another thing that is different is that
if you look at the shape here it can be
a bit difficult to see here i understand
here but it says it has four entries and
there's a comma because you can multi
multi-dimensional we're not really going
to dive into that aspect of it but the
aspect we want to dive into is that the
shape of a array is fixed
so
once you create it you cannot change the
size the number of entries in it anymore
and again this is a restriction but
again a restriction which makes it more
effective
good so let's try to make another one a
2 here
np array
and then we take 5 6 7 8 and we execute
that now we have an a1 and an a2
right so they are similar as you see
so what i want to show you is you can
make computations on it right so
a1 for instance you can
multiply it if you multiply it you
multiply each entry by two but you can
also multiply two
numpy arrays and what it does
now i should have both here yes
what it does here you see one two three
four five six seven eight so what it
does is one times five is 5 as you see
here 2 times 6 is 12 3 times 7 is 21 and
4 times 8 is 38 right so you see it
multiplies entry by entry so this is
some earth
this is just some basic computation you
can do and similarly you can do
play around with it and see also in
addition and so on
these things are very handy when you
work with them and it's nice to know how
it works
good but for our purpose we are going to
use this numpy array which you have an
idea about what is now it's just a
different way of having a list of
numbers
and it's more effective we want to use
it with a library called sklearn which
has the linear regression model we want
to use so the next step of this part
here is to figure out how to use it and
in the project we're going to use it on
real data okay so let's try to import it
from sklearn we want a linear model
model
import
linear regression right so this is a
model we want to do and i actually want
to import matplotlib
as well pi plot as plt and we just do as
normally matte oh matt matt plot
lip
really used inline as well
and
i forgot a t here import thank you for
noticing that small mistake we all do
typos all the time perfect good so what
we're going to do now is just to feed it
some data so you can see how it works
right so what i import here is the
linear regression model here this is a
model we want to work with right so
this is going to do all the magic and
the reason why we needed the numpy
arrays is because this model here uses
input from a numpy array because it's
more effect efficient so it makes the
calculations faster so let's try to make
some data here so how would
the model work is like it will have some
input data and some output data and the
input data will be the x axis data so in
this case here we're going to make some
just some data for i in
for
i for i in range let's just take 10
numbers it's okay and then we have some
data of the output here so for these
cases here
it's not the data itself
it is just to have some data right so
here
we would have some data which we
spatially basically says well
let's just look at it
x and y
x and y it's just you know numbers so if
it was a data set it will say for
input
0 we have output 0 for input 1 we have
output 1 right and so forth and these
could be anything right but these are
connected together these input output
things right x input y output
so this is the input for the algorithm
the next thing we want to do is actually
have to in numpy arrays and
this is where it becomes a bit tricky
because the
library needs in specific format as well
but first let's just convert them to
numpy array so array
x here
and y here equals to np array y here and
notice that i use capital letters here
this is just a standard i don't really i
mean i like python way of doing things
and apparently
this is how it's done done
now in statistic you use these capital
letters here right and uh yeah so here
you have it but we need to format it in
a different way because what it takes
here is here's like a one-dimensional
row here and it wants to take it in
you'll see in a moment reshape
minus one comma
comma one here and you reshape a bit
here in
minus one comma one here
and here we have it on a different axis
right so you see before it was on one
row here and now you have one column of
data instead so it's like an array with
uh
single entries of
of lists
python arrays down here so it it kind of
changes the dimension right and you see
here this is like the minus one and one
so minus one here means that uh just
like a negative indexing so if we have
like 10
10 entries one two three four 10 right
it means
it says a 10 here we need 10 rows and
one
one
element on each row right so we have ten
rows here and one element on means so
the minus one is like negative indexing
as we know it
perfect
oh now this one i want to delete perfect
so now we're ready for our algorithm
so let's just call linear regressor
regressor
and we take the linear regression model
here and
then we take our linear regression
grasser and we fit it
to x and y so this is basically the
magic it does
and after we have fitted it we can
actually get a prediction on it
so
why
prediction
so
we take the linear regressor and we say
predict all the values for x so in this
case let's just
look at this one here so the linear
regressor here it creates a
a class of this or an object of this
linear regression model
on this line here linear regressor we
make the fit that is it fits the data as
best as it can
and then this one here it takes all the
x inputs and says well what do you
predict the
y
output to be right so let's try to
visualize this
so we do that by figure axis
on plt sub
plots
that's
matplotlib remember
and
x
we make a scatter plot
scatter
with
x and y so this first just scatter the
points right so these are the points
we're trying to fit and this looks like
a perfect line right we know that
because 0 0 1 1 2 2 and so on so it's a
perfect line
the interesting thing is if our
prediction line here will be perfect as
well so we can actually add that
so we make a plot x and y print and
let's just make the color
red here
and you see here this is a prediction
line so given any input along this line
here
this is what the model would predict
right so it predicts perfectly as you
see
you can also set a title here
set
title and often what you're interested
in from this model here is actually the
alpha and beta value so the alpha is a
intersection with a
intersection with a y axis here and the
beta is the slope of the line so let's
try to get that
alpha so the alpha is uh
given from the
linear regression model and it's called
here
this is
and beta is linear regressor model
here
and uh
i think it's
double here let's just try to execute
this and then see if we have the values
here alpha
we have a value here right you see a lot
of it's actually not 100 accurate
between zero
and beta should be close to one right it
is perfect it's not
100 accurate okay so in order we want to
make a title here we just put the alpha
and beta on there so let's just do that
so you could do that by putting a string
on here but let's just round it a bit
here and let's take five digits so on
here
convert to a string here and convert
this one to a string here because we
want it in the title
and how many digits do we take here five
as well
let's try that
and uh
then we have alpha here
alpha is not defined
oh alpha
alpha here's zero zero right and beta
is one now because we rounded it so
let's add it to here
should we make a formatted string alpha
and
comma beta
perfect so here we have it right so
alpha and beta
so this is basically what we need to
master in order to do the project right
so again it looks a bit scary but
actually it's not right so we have to
understand this is input data and this
is output data x and y and we need to
reshape it in this format here for the
model down here and then we fit it and
then we have the predictions based on
the input here and then we just take the
alpha and beta value and put it on the
scatter plot the scatter plot are the
actual points and the
plot here with the prediction is the
prediction line of the plot so this is
it basically it's amazing right so let's
get started with the project it's going
to be awesome so see you in a moment
so
are you ready for the project and
remember you don't need to recall all
the details from the previous lecture
this is an e-book if you didn't download
it already and please remember there's
an e-book there's a link below in the
description and then you can use that
ebook to look up how was the syntax and
so on because honestly especially if you
don't use it on a daily basis you will
never remember the syntax but that's
okay and then you look it up and in the
e-book it's everything you need to do
this project so
project machine learning our first
machine learning project that's exciting
so the goal is to implement a linear
regression model as we were introduced
in the last one
so let's look what we have to do well
it's described in steps down here so it
should be kind of straightforward but
you know always
don't underestimate how difficult it is
when you do it yourself so what you want
to do is import numpy as np and import
csv then you need to import the sklearn
with a linear regression here this long
line here and then you want to import
the matplotlib pi plot as plt and with
matplot inline okay that was a lot of
things right and then we read the csv
file of the data we want to work with
and we're going to work with the files
cars again here with the list of
dictionaries right so you need to import
it as a list of dictionaries
then we use a list comprehension to get
the horsepower and torque from the csv
data right remember how we did that we
do it again because practice makes you
better
and then we create a list as a numpy
arrays of
and reshape them right
so the data we have here we need to
transfer that to numpy arrays and then
we need to reshape it okay
then we create the linear regression
model and then we fit it and then we
predict right
perfect and then we create a scatter
plot with the points horsepower and
torque write the data points from here
and then we create the line read with
the prediction right and then we see how
the prediction looks like and so forth
so this project will also make it more
understandable for you what this linear
regression model can do for you
so are you ready i hope so because this
is really exciting and if you find it
too difficult please let me know also in
the comments i'd love to get some
feedback from you guys so let's get
started with this one see in a moment or
first work
then if you get stuck
see the next one then redo it yourself
it's highly highly
encourage that you try to do it yourself
and even though you don't get it try
again and try again until you kind of
get it
well see you in the next one
are you ready for this project i hope so
i hope you managed really far if not let
me know in the comments maybe there's
something you find a bit difficult to
understand don't worry we all been there
so it's not easy the first time you do
it
and let's try to walk through how i
would try to solve this so basically
what we need to do is to follow the
steps all the way down here one by one
so let's try to do that together
so the first step here you see we need
to import numpy as np and import csv
let's do that
in
import numpy snp
import csv
that's the first one here then we need
the sksk learn here and the linear model
here we need the linear regression
that's our model right sk learn
learn
actually that's missing an
r there i'll correct that for you
uh linear model
and uh we need uh import
linear regression
it
doesn't doesn't recognize it or that
because it said import here it should be
from here and import linear regression
perfect
and then
we need the matplotlib pi plot as plt
import
mat
plot lib
pi plot as plt and then we need the
matte
plot lip
inline right
and that's basically the library
so we need that's a lot of things right
now it looks pretty professional right
numpy csv sklearn mat.lip and we have
this line here for
telling
jupiter notebook how to
show it
then we're in this line areas we need to
read the csv file
this one here files cards here with a
list of dictionaries
you remember that syntax don't worry if
you don't you can always look up in the
ebook i would do the same
so how is it help me here with
open so we open the file pointer
files
cars
then we have
read mode
as f and then we need the colon there
right
then we have the csv reader
and we take the csv here and we do the
dict
reader here and it takes just a file
pointer and then we make the cars here
cars list we have called it cars
previously so just keep that syntax as
well and uh what we do here is take the
list or convert the csv reader to a list
which converts it to a list so let's
actually just try to execute that and
look at the cars now so let's just look
at the first one here
first one here and we remember the first
one is an audi and it has horsepower
here and torque here right
perfect so far so good
so then it says use a list comprehension
to get horsepower and torque from the
csv data right
and
basically there's a catch right remember
remember when we look at the horsepower
the horsepower and torque here they are
strings right they have the quotes here
so they are actually represented as
strings so we need to convert that as
well let's also do that in this process
here right so horse
power
we have a list comparison here so how do
we get it out well cars or car
and we take horsepower
and we take
four car in cars right
so this is basically what we need right
this is a horsepower so if you executed
this one here you will have a
list of it but as i told you these are
strings right so how can you convert
that well you take an int on that
here
execute that and when you see here then
the type is int which is what we want
right
perfect
and we need to do that for torque
torque as well so now we need the syntax
int
car
torque
for
car in cars right so this is the data we
are going to work with right and uh just
remember how much data is there
horsepower they're like
5076 right the same with torque that
should be right so what we're going to
try to do in this model here is try to
see that
uh how to fit the
dependency between horsepower and torque
so what you can predict now
or what's supposed to predict is giving
a horsepower what is the torque of the
car right
interesting
good
perfect
so that was actually
this step here use a list comprehension
to get horsepower and torque from the
csv data
then we need to create a list as a numpy
arrays and reshape them remember and
we're going to use the same syntax here
so we're going to use x here and
np
array
array
here and we take the horse power and
then we reshape it and remember this
syntax there minus one it's negative
indexing and one here and then we take y
here np array
and we take
torque and reshape
minus one comma one here so now we have
the data
and
yeah
perfect
so that was it
and then we need to create a linear
regression model
let's do that
linear
regressor
and that's just a linear regression
model here right
perfect and
then we need to
fit
with the two reshaped lists right
perfect
linear regressor fit
x comma y so this is where the magic is
done right
and then we need to have a prediction
here predict perfect
and we predict with x
so y
is linear regressor
and we do predict here
and we do it on the x data because
that's the horsepower we're going to
predict
the torque
of each of them right so this is where
the magic is done perfect it's already
done awesome right
so now for the final
step or final two steps create a scatter
plot with all the points horsepower and
torque
awesome
so
thick axis plt sub
plots here perfect
and now we need an axis and scatter
plotted
x and y so this one we actually seen
before right so this is all the points
that we have here right
pointed over here
and
we already kind of saw that there is
some kind of connection here right
and we will want this model to predict
that
and let's plot that as well plot
x y print
here and we take color
red
so this is the line it's going to
predict with right
so given any horsepower for instance 500
this is a prediction it would make
so we can actually get the
get the coefficient of it it doesn't
require that in this project but we did
that in the other one right yes it
doesn't require it but let's do that
alpha
how was it it was a linear regressor
interceptor
0 right
and
we just want it
immediately as a string to put it there
and we used round in the last one
and we take five and
end here and here
and then the beta
string round
linear regressor
coefficient
zero
zero right this will sit
and uh then we can add the title here ax
set
title
let me use the formatted string alpha
let's just have a colon here oh
here
alpha
beta
[Music]
then we can actually see here so
actually the beta is exactly one that's
kind of interesting right
that means basically that uh
it actually looks like that too right so
for 100 it's about 100 and for 600 is
about 600 right so the beta is one and
the interception is like around six so
it's like the torque is actually six
on average six above right so with 100
it's around 106 and with 600 around 606
and so forth here so this is a line it
would predict along right and you see
that's pretty nice right uh
with data like this there's always
points scattered all over right but this
is the best fit you can find to predict
so given anything
any horsepower this is the prediction of
the torque it would make
wow that's amazing you did your first
machine learning model and this one is
quite simple one and uh but it uses the
principles of machine learning in order
to do that prediction right before we
make the capstone project where we
actually built our own machine learning
model from scratch the reinforcement
model i love that one because in this
one here we kind of use a library
already here but in the capstone project
we're gonna use a
we're not gonna use any libraries we are
gonna build it from scratch wow that's
amazing so we built our own model from
scratch in this reinforcement model and
the difference between a reinforcement
model and this model here is actually
the reinforcement model actually
learns
along the way and keeps continues
learning along the way it's amazing it's
like
uh
it's like it gets feedback for every
step it does so it has immediate
feedback was this a good decision or a
bad decision right it's going to be
amazing but before that the capstone
project we're also going to use learn a
bit about pandas so in the next lecture
we're going to learn about pandas and
we're going to use pandas to represent
data and export it to excel sheets it's
it's really fun as well so so i hope
you're excited i am so see you in the
next one
in this lesson we're gonna learn about
pandas and pandas is an amazing tool for
working with data which is similar to
excel sheets and you can do similar
things with it
for me it's just easier because you do
it from python so in this lesson here
we're going to learn how to read data
from csv files into a pandas data
structure the data frame and we're going
to work a bit with the data inside there
we're going to group it in different
ways and then we're going to export it
to excel and create amazing charts of
that data so in the project that's what
we're going gonna do we're gonna read
data from csv files and work around with
it and export it to excel and create
charts in it so this is a subset of what
pandas can do but i want to demonstrate
that to you so you do have some advanced
thing where you can automate some of the
work you actually do at work
or i don't know if you do work at work
but i do and
then you can automate generating amazing
excel sheets so are you ready
let's get started
so inside here we have the title of our
lesson to be panned as and
that's because pandas is so extremely
powerful so the goal of this lesson here
is to learn basics of pandas to read csv
files from pandas to explore the data
frame and data frame is the data
structure the main data structure of the
pandas library and then we're going to
learn about group by and group by is
kind of how you if you have similar data
inside there it's like group by and
excel actually and then we're going to
use date time and group by and
because you can group things in time
period based on the date so we group the
data for month for instance group it in
month and then finally we're going to
show you how to export it to excel and
actually create a chart with excel and i
have a link here because
when it comes to syntax for
creating these charts in excel here you
will see down here well we need to
master a few things in order to do it
and basically it's not difficult and we
will do it in the easy way here like
this one just writing it out and but
then you can insert charts like this one
we're going to do here and on the page
here you can find multiple examples of
how to do it and honestly you would
never remember these by hard you just
look them up if i say i want a chart
like this one how do i do that so that's
what we're going to do in this one
good are you ready i hope so because now
we're gonna import our pandas library
let's do that import
pandas
as pd so
we did it our first pandas import
amazing right
so what is pandas
well
remember the numpy library right it was
more like a list of
a list with some restrictions the pandas
library is more like a dictionary with
restrictions i'll see why in a moment so
let's make a dictionary here
a dictionary here
let's just call it d for short here and
we have the brackets here so
imagine here we have a scenario where we
actually
register cars and the speed they're
having right so let's just make a list
here to have a car so this is our key
the car
or should it be cars because we have
multiple and then we have some
a list of cars here okay so for this
case here we have a
porsche we have
another portion
and then we have a
ferrari
and let's just take another ferrari
okay
so we have four cars in this case here
and let's just say they were registered
with speed
with a speed and it doesn't really
matter what the speeds were
200
215 this is kilometers per hour so not
miles per hours
and uh ferrari here was crazy 305 and
195 right
so
what we have here is a dictionary as you
see here and for
for each
car we registered we registered the
speed also so these are
depending on each other so there's a key
thing here in this dictionary we have
when we convert it to a pandas data
frame we need to have that the list
representing each key value
need to be the same length so again we
have a restriction on it another
restriction is just like numpy arrays we
need the data type of the list to be the
same all the way down there or it will
generalize it to the same one okay
so let's do our data frame so let's just
call it data frame here and pd
data frame
here and just takes the dictionary here
boom
and then we can actually have print out
the dictionary here right so you see
here
this is actually similar right
to the data up here and you see why we
need the same number of entries it's
because well porsche 200 right porsche
200 porsche 215 ferrari 305 and ferrari
195 here right so
as you also see it kind of looks like a
spreadsheet as well right so you have a
column names here
and then you have the row numbering here
right
perfect
and
what we can do on these data frames we
can do a lot of things and we're not
going to demonstrate everything here but
as you see here the data frame we can
actually
let's for instance take the mean value
on it and what it does actually it will
take the columns which makes sense to
take the mean value on and it's only the
speed column because you cannot take the
mean value of the names porsche and
ferrari but here we actually see that
the mean value of the speed here the
average speed here was
228.75 kilometer kilometers per hour
right
so you can do a lot of things directly
on and it's really really nice another
thing you can do is for instance if you
have the data frame here and you want to
say well we have the cars here and i'm
only interested in cars which are
porsche right
so this one here will only show the
which one has
the porsche in it right so true true
false false true true false fault but if
you want to show them you can actually
put it inside the data frame here so the
syntax here is like this here right
so now you get all the porsche right so
you immediately see you can
mangle around with the data in many ways
to get the data you want and again i'm
not going to go through everything here
i'll recommend that you look into a
cheat sheet which is available from the
pandas library and i'll put a link down
there to it and it's also
a link in the ebook so you can find it
from there
because all these things here is just
you know you don't need to learn them by
heart in the beginning you will look
them up in a cheat sheet okay
yeah so don't worry about all the
details about these things so in this
project we're going to do some specific
things but one of the things we want to
master is actually grouping things right
so imagine we want to group the the cars
right so how can you do that well
there's a function called group by and
what it does it takes the column name
here and then groups the data into this
but this
by itself just represent a group by our
data frame group by object right so this
is not useful for us we need to tell it
what should it do with the data right
because when it has grouped in porsche
and ferrari as it will be doing here
what should it do with the data well one
thing you could do is say what is the
average speed by the cars right so you
can actually just call mean on it and
you see here then you get the mean value
of the porsche or the ferrari here which
is 250
which is 305 plus 195 divided by two
it's 250 as you see and then this one
here the average of this one is uh
207.5 right so you see then you get the
average speed of the cars you could also
add some or whatever right then you get
the sum of
each of the cars so you can apply
functions based on a group so this is
what we're going to do in the project as
well right we're going to use these
features okay
but
so far so good let's try to
read some other data
in this case we're going to read a new
data set
i'll just find it so you can see it
and if you go to files here you'll see
here there'll be a speed check here this
is a small data set for us to play with
first right
so this data set becomes uh has a date
on it and it has a car and a speed just
like we did in the above there right so
we have a date time when the porsche
here was registered to 315 kilometers
per hour and then we have bmv ferrari
bmv porsche for ibm bmv right so you
have the date the car and the speed as
you see here and there's another
difference in this comma separated
value file we use a semi
colon here instead of the comma right so
this is just to show you that sometimes
the data is structured a bit different
and how to read that
so you don't get worried about that all
right so you see how to do that right
okay good so this csv file is called
speed check so let's just call
the data frame we create here speed
check
and uh then we need to read that and
with pandas it's amazingly easy right
pd
read csv
what does read csv do well we could look
in the manual here or the signature
docs string as it called
read a comma separated value file into a
data frame this is what we need right so
how do we do that we just put the files
here
and
we call the speed checks so if you read
it immediately we need to set some
things as we will see here
because right now it by default it
expects a comma to be there right so
if it doesn't find a comma it'll just
say well i just read one line right so
this line has column name date car speed
right obviously because there was no
comma so it's just one column and then
you see it down here so this is not very
good for us right this is bad
so
what can we do right so let's look at
this
long list of things here right so it has
a delimiter right
and let's put that one
to be
semicolon right because if we read now
we see now it actually structures the
data correct here right perfectly
another thing we might be doing is
actually we want the date here to be the
index here right so right now we have an
index here
as you also remember here we have this
index here
and
we want actually the date to be the
index because often we'll just say we'll
look up this date and be the index okay
how do you see the index is actually
just put index here
and execute it then you can see the
index is a range index from start and
stop it's just numbering here right and
that's step one and another thing you
can look at is like the data type
speed check d type s with an s remember
with the numpy without s
and you see here it has object object
and integers so you know that actually
the the date here is
read as an object and the car is an
object and that means basically it's a
string in these cases most of the times
on this object it's a string
and then you have the interior which is
the speed so this one
is an integer and this is a string and
this is a string right
so this is
saying as something else it says here
that actually that each column has one
data type which i also mentioned before
right
and
another thing is that this date here is
not represented as a date time object so
the computer doesn't know it's a date
and we need to tell that to the
program here but luckily
luckily there's actually a
argument we can set there
and you see here par states is by
default false if we set parse
parse dates to true let's try that
that's actually not the one we wanted to
look at then we have this one here
well date is still an object but now
it's actually we cannot see that here
but it's a date time object so the next
the next thing we're going to do is
actually we're going to change this date
to be the index
index call
to be
the date column right so now we set that
the index column this one here out here
should be the date column so let's try
to do that
so now you see here
and that the date
became
the column here
or the index here and then we actually
can see that the index now is a date
time index right you see that
it is changed to daytime index here
really really nice
and the date that types now is
we have one less column now right
because now we only have
car and speed
perfect so this is actually the data
that we are going to work with so first
of all we can obviously group by car
again here right so
if we wanted to look at the cars and the
speeding right so we take this speed
check and we take group by and then we
can take
car
i'll call it car here
and then we could take let's just take
the mean value
then we can see that on average the
fur and porsche are just the sinners
right the bmv is not as sinful as
as the other one here on average right
and uh
the thing i actually want to show you is
actually now we have the monthly data
here or the data out here we could also
group by the monthly
view right
so let's try to do that
so
speed check
group by and then we actually use a
pandas again and we'll have a grouper
and this grouper here
it basically says grouper
allows a user to specify group by
instruction for an object right
and
if you read down here you can read a lot
of things you can have something called
frequency and this frequency here is how
often uh you know what kind of group you
should put into so we're gonna use that
freak i will
say it's a monthly basis and you need to
look these things up to know that's by
hard right so this is grouping by
monthly and then we can again
apply mean on it and then you see here
that we have two months
we have
we have march and april in 21 and you
can see actually on the data here that
this is correct
we only have data from
march and april in this data set here
so this is actually true
so
this is nice right so let's just put
this monthly here
monthly data here and have it here
because
if you look at the data again
well it indexes by the last day of the
month here right maybe we just want the
month to be written like that to write
the actual month and you can actually do
that to make it look a little bit nicer
so remember the index here is this kind
here right so you can override that by
just assigning it to something so
monthly index and
then you have something called month
name here
let me do that
and then you actually override it and
you see here it will write
march and april right so what you take
is you you overwrite the index here and
you say we want the month name to be
written instead right so you all
reassign the index to be the current
index but you take the month name and
put it there you can do that on date
time
in objects like that okay
amazing perfect
so
next thing next right now we need to
export it to excel sheets and this is
where it becomes interesting right
so this is actually quite easy again
with
pandas so it's just to write to
excel
basically
and then you write the
[Music]
excel name that you want to export to so
let's try to export it naively like this
and then we need to open the excel sheet
and you we have it here actually the
first one is a date here and you see
here this doesn't look nice but we're
not going to go into that this is
because it's a date object and it writes
like that but in our case we don't care
but you could actually
resize this one from python as well so
when you open it it will be resized or
you could reformat it so you don't have
this 0 here but we're not going to play
around with that in this one here
because we're not going to use the date
export the data like that
but here we have the data right the car
and the speed
all done by one line of code right so
what is the next step here right what we
actually want to do is write multiple
sheets here so let's try to do that one
where we export
this data here and one where we export
this data here so let's try to do that
but
when you read the excel documentation
here
you actually see here
that
to write a single object to excel a file
it's only necessary to specify the
target file name to write
to multiple sheets it's necessary to
create an excel writer object with a
target file name and specify a sheet in
which the file to write to
so that's what we're going to do down
here actually
so
let's just call it workbook
pd
and then we create this excel writer
and
we're going to use the same
name here
perfect
and then we take the
speed check and we do group
by
car
and let's just take the mean value here
and then we do to excel
oh
to excel
and then we take the workbook here
and then we define the sheet name as
as it said and we call it
cars
right so this is our first sheet and
then we're gonna do the
monthly here
and we're gonna do this is just written
directly because we saved it to excel
workbook and um
sheet name
we call it um let's just call it
monthly
oh monthly here
perfect
when we work with a workbook we need to
close it as well
because it doesn't know if something
more is coming right so what do we do we
create an excel writer as it tells us to
do so we have a workbook and
the reason we need that is because when
we write multiple times to excel
uh we need to specify that we're working
with the same excel sheet but then we
say which sheet name we are creating in
it right so first here we take the speed
check group by car and mean that was
what we did up here
so we will get this one here
and uh we write that to the exp
spreadsheet here and we create the sheet
name cars and then we take the monthly
data this one here just directly and
write it to excel in the workbook
monthly and then we close it which will
write it so let's try to execute this
piece here and
let's open our spreadsheet here
and then you see immediately here right
we have cars and monthly and here in
cars we have the
cars listed here with the average speed
here
perfect and on the monthly sheet here we
have the monthly data error wow amazing
right
perfect
so the final thing we
we want to do here is actually we wanted
to create a chart right and this is
where this link comes into the picture
so let's look at the code here
so
so far so good right we did this
this part here and we what it's doing
here is
kind of formatting and
writing some data we did it a bit
different here but what we're looking
for is actually this down here we need
to create a chart so how do you do that
right so
let's try to do that in our case here so
let's just take the same
code here and put it down here so i just
save that one here and then we put some
code in between here okay
and this code here is actually going to
be
this
we have here let's just copy this
and we're going to modify it so it fits
our things but there's one more line we
need before
we need actually to insert the chart
and they work with
worksheet here right so we need to
modify that because we didn't create a
worksheet here right so we had the
workbook but we don't have the worksheet
here
so so the worksheet is accessed by
workbook
dot
sheets
and then we take this sheet we want to
put in this case we're just gonna
put one sheet in here and it's gonna be
the car
sheet or cars it's called and we're
actually gonna remove this part here
and
so what is going to happen here right so
we create a chart one here and we added
two
of type
column we want the same type as they do
and then they add the series but we
don't have
the same name here but let's just have
the cars here and then we have the
categories it's not on sheet1 it's on
cars
and the categories are from a2 that is
correct
as far as i remember let's open the
excel sheet again
so from a2 to a4 right and the speed is
from b2 to b4 right this is the one
so a2 to
a4 and here we have the values cars from
b2 to b4 right so this is basically what
we do we're specifying the area of the
column chart as they call it and then we
insert it in our sheet cars
in d2 okay let's try to do that boom
so i did one mistake here so
add chart so it was workbook
actually we need to have the book here
because we do it a bit different than
they do because if you look at the code
here i just forgot about that
so when they create a
work or they use the xl
x writer that's why
uh so we need to add that one there
in the book here
and it's a bit confusing you know i know
but it's it's a bit different syntax on
this one here but other than that it's
totally the same right so now we execute
it so let's look at the
spreadsheet here
and you see here we got the chart here
right so you can see from bnb
it is
average speed here and ferrari and the
porsche here right so this is how it was
entered and again it is a live data so
if you change it it changes here as well
right
perfect right
so see how you can change the values
there so isn't that amazing so
that's basically what
we need for
our project
and uh if you find this down here a bit
confusing don't worry about it you're
gonna copy paste it and modify it to
your need as we did here together and
that's it are you excited i hope so
because it's going to be amazing see you
in a moment
are you ready for this project it's
going to be amazing
so let's jump to jupiter notebook and
read about it so the project is called
pandas and excel export so the goal is
to learn how to use pandas data frames
right the data structure of pandas and
how to export to an excel sheet okay
so the description here is what you need
to do so first we need to import pandas
as pd
that should be straightforward then we
read the csv file
file sales csv with pandas and let's
just take a look at that one right so we
have that under files here and then we
have the sales data here
and you see here this data is
actually pretty long actually
and it's structured similar to the one
we looked at right so again here we have
a delimiter which is different from
comma it's semicolon right and then we
have the date we have the sales
representative the item the price the
quantity and the sale right so here we
have the date of the sale and then we
have the sales rep
and what did they sell the markers and
you can see different types here and
then the quantity and know the price the
quantity and the sale
yeah the quant oh yeah so that makes
sense right so the price the quantity
and the sale right so you have double
quantity as here right then the sale is
the sale the total sale of that perfect
good
awesome
so it actually here remember to set
index call to date
delimiter to semicolon and parse states
to true right otherwise you cannot do
all the magic below
so first we need to group by sales rep
and sum it up right so this is basically
saying we have these sales
representatives and we need to figure
out how much they sell right so this is
a
normal way you want normal thing you
want to do an excel sheet to see well at
the end of the month who sold the most
right or the end of the year or whatever
right you want to see which of the sales
reps are actually performing well okay
and uh
that's the first one and then
we need to group by month right and in
this case here
remember this grouper the frequency here
right remember that one we can use the
month here you use that inside the group
by method right remember okay
and
then we also want to change the naming
of
the index to use month name and not just
using
using the
end of the month date in there right and
then we need to export it to excel
perfect
we create an excel writer remember when
you have multiple sheets you need to
create an excel writer and then you made
a sheet with a sales rep and
with the data above here right the sales
rep data and then you of course need to
remember to set the sheet name and then
we create one for the monthly data and
also set the sheet name and remember
after this you need to remember the
close-up workbook
and if you're up for it i think you
should create charts uh column charts
for each sheet right just use the simple
way we did it and you can always do more
advanced where i actually figure out how
many rows and columns are there don't
worry about that you can do that in more
advanced later okay so this is a project
i think it's a great
resource to learn because
if you like me been working in an
industry where people
actually any industry they use excel
sheet all the time so if you want see
somebody doing some manual things every
month well you say well i can
automatically automate that for you
and honestly i've been doing some stuff
and it's just like
i helped one guy and it took me like a
few hours to do and he was just like
well i do this every single month and it
takes me
so long to do it and it's just like now
you just saved me all that time thank
you
you can be that guy the guy that saves
that guy okay so see you in a moment
bye-bye
perfect are you ready did you try it out
did you figure it out let me know in the
comments below and again if you don't
remember all the syntax remember to
download the ebook because that will
make you
that will have all the things you need
to learn right
and let's get started so
let's just jump into it and start at the
first point here and import pandas
how do you do that
import pandas as pd thank you for
helping me
then we need to read the csv files and
remember how it looks like here this is
this file here
and we have something to take care of
but let's just first import it naively
and see how it looks like so let's just
call it data here
pd read csv this is how you read the csv
file and then you just put the file
destination here
sales rep or sales data it's called
but if you look at this one here right
we have the same problem we had in the
lecture right so here
so here we have everything in one column
name because it doesn't understand that
this is a separator or delimiter so
let's add that there delimiter
and
semicolon so if you do that immediately
it puts it in columns by itself right
and
also remember the data types each column
has a data type here
date
sales rep and items are objects and
price quantity sales are integers so
it's fine right another thing we wanted
to look at was uh
we needed to remember to parse states
par states to be true and then we need
to set
our column index know how is it called
i actually forgot it for a moment here
we need to set index call that's how it
called index
call
to be
date okay
so now actually we have
data here and we see it looks like this
here right
it's a small trick here so you see here
now you get a lot of data here but it
summarizes there often what you're just
looking for are just the first few lines
to see if the data is correct you do
that by hit right it's just a little tip
for you free free tip for you okay so
then you can see how the data looks like
so you don't make this long
long list of data okay perfect first
step done
or actually that was second step so here
group data by sales rep and summit how
do you do that
well we have the data here and then we
need the group
by and we just write sales rep and again
remember you need to spell it correctly
if you make a mistapping down here it
won't work
and then you need the sum of that right
so here we have it right so this is the
data so now you can see all the sales
rep that you see for instance emma here
c has
sold this is the one we are actually
interested in sales down here she has
sold for
[Music]
50 no 65 000
so actually we have the sofia down here
is a
star of this company
or i mean it depends on how much revenue
there is in the sales but we don't have
that kind of data in this data set here
so we are just assuming naively that the
revenue is
equal to the sales here
or equal connection between it perfect
awesome
awesome awesome awesome so we want to
save this data here so
so let's just say sales
and
actually we want more than that we don't
want the full data set here because the
price and quantity doesn't really make
sense maybe the quantity makes sense but
we are
actually only interested in sales here
right so how can you get that well you
actually just write sale here and then
you actually only have it the sale from
that you also notice that it becomes a
different data structure here right so
this is actually not a
not a what's called a data frame anymore
it's a series but the series is just a
one column data frame you could you
could think of it like that and here it
also has one data type and then it has
the still the same index out here okay
so what should we call
this one we call it
rep
refresh rep cells
here we have it right so now we save
that data for our excel sheet that will
export later
then the next step here we need to group
the data by month how was it
how was it right we had this grouper
frequency month and we needed to
remember to
change the index name so let's try to do
that
data
group
group by
and then pd
grouper
and frequency
month
and if you look at this one it doesn't
make sense because it's a data frame
group by object again we need to do
something so the month here what is it
we want to do on the monthly daily and
the basis here we need to sum it again
and here again as you see here what is
it we need to sum we only need to we're
only interested in the sale here so
again we can just
isolate it with writing sale here okay
so now we have the sale by month but
again you see here now we have the end
of month date here from 2020
so if we put it here let's call it
monthly
monthly
sale
put it here
then we can actually change the name of
the index
monthly sale here index here and then we
set it to monthly sale index and how is
it called
month
name there we were
then we change it because now it looks
nicer as we see here
then we have for we know it's 2020 so
it's january until december right so you
can see which months
sold the most so as you see here when
you have it like this
it's not so easy to interpret or see
visually we're going to export it to
excel now here in the next one and in
the final part we're gonna actually
insert excel sheets excel charts in it
so you can see it more visually
good perfect
because here's the next one export the
data to excel right and we need to
export the sales rep and the monthly
let's do that
and we needed to use a excel writer
right so we called it workbook
and how do you do that
you had the pd
excel writer and did we have a name for
it i don't know
sales sales report we call it
perfect
works fine for me
and the first thing we need to do was to
export the wrap
reps sales to
excel
and we need the workbook and there was
one thing we needed to remember what was
that yes that's right the
sheet name
and we just call it the
sales
per rep
[Music]
as a long name
sales per rep
sales
per rep representative
we call it rep
i don't know we call it sales per rep
and then we need the monthly sale and we
do that to excel again
[Music]
and we have the workbook and the sheet
name
and
monthly
monthly here
perfect and then we take a workbook here
and we
close it
perfect so let's jump to
see here
the excel sheet how it looks like
so here we have it actually
we have all the sales rep here and we
can see the sales down there and here we
have the monthly data
and we can see down there so again as i
told you it's often easier to interpret
these results you may have a chart it
will easy to find the months that we're
doing well and the month they're doing
less good so actually let's do that and
we just do it in an extra one here
so
let me do that between here and remember
all the syntax
actually we wanted to
do something similar to this so let's
just try to copy that again
and we needed to
insert
chart
so let's do that and remember we needed
the
book here
[Music]
and
then
we need
to add a chart here
and we need to modify so in the first
one here we need to take this long name
here that's why i was
okay this one we didn't want to change
queen change this one here and this one
here here we just write it exactly it's
just
sales
per rep
right and then we need to look at what
is the
first and last i actually don't know how
many were there
so we have down here it is a 10
right a10 and then this one here we have
down in 13 obviously
so that's the first one and we also need
to insert it
insert it
we're not using these offsets thing here
so the work
book
sheets
and we're just inserting the chart here
and
we insert it in this one here
and uh we're going to do the same once
more here but we're going to do it on
this
different
one here so instead of sales rep we're
going to take the monthly year
monthly sales
and it's going to be 13 instead
and 13 instead
and we're going to insert it in monthly
instead right
so so far so good so this is actually it
let's try to execute it and it says
error
and it says sheet name
contains spaces but isn't quoted
so you cannot have spaces and sheet
names without having oh we need to quote
that
okay
[Music]
we learn something along the way all the
time
do how many places do we need to quote
this
i don't know
it seems to be fine just to quote it
there let's look if it went fine it
actually looks so right
so now you see it's more way more faster
to
see how people are performing like ethan
here well is that good enough i'm not
the one to tell if it's good enough or
not but you see you get a more visual
faster visual here and this one here you
see again how the month going so you see
january is nice february is low and then
may is not good but actually in the
summer it does pretty well here and so
forth here so you see it's
you have some good monsters and bad
months
so far so good so this is basically what
i wanted to show you with this sheet
here
there was a small detail here right but
as you see that
it writes back to you what you should
change and
you should quote
these things here
well i didn't remember that by hard
could i see it here in the manual here
they have examples where it's not done
right so if you encounter a problem like
that and you don't know what it says to
you you know sometimes
return messages error messages are
difficult to understand so what should
you do well try to make that exact same
example like they're doing here and then
change it step by step until you come to
your example right and then you'll see
which step does something wrong and then
you figure out which
you know where the error is because
sometimes i know it can be difficult to
read oh this is where the error was this
is what it's saying to you right
perfect so are you excited about this i
think so this is a great project so uh
in the next one we're gonna start with
the capstone project where we're gonna
create our own
our own
machine learning model and we're gonna
programming program it from scratch it's
gonna be amazing i'll promise you that
see you in a moment
the capstone project of this course are
you ready i hope so because this is
exciting so i promised you we are going
to create or build our own machine
learning model from scratch and you know
what i'm going to keep that promise to
you and so in this lesson here we're
going to learn about reinforcement
learning which is a machine learning
model we're going to use or build
so to break it a bit down the structure
will be first we'll understand what is
reinforcement learning how does it work
and the second step is well what is the
problem we want to solve simply said
it's a problem of
if you were let's say you're a cab
driver you drive around in your cab
and you need to pick something up and
you need to drop it off
you don't know where to pick it up and
you don't know where to drop it off but
you will learn along the way right so
you just do random things and with this
random behavior you learn
by reward and punishment it's called but
basically you do something you get a
kind of reward it can be positive or
negative and based on that you learn how
to behave more smart and we will measure
that so that's a problem you want to
solve so how do you model that so we're
gonna in the second step there we're
gonna implement the field source a where
we do the processing so that's the
second step then on the
third step we're going to implement the
naive solution right so if we only did
random things how would that perform so
we have some base cases so if you do
random behavior how does it perform
right you need that so after we
implemented the naive solution we need
to figure out how can we map the world
we're living in into the model we're
living in right the model we need to
create so that's going to be how do you
map from the world
to the model so that's kind of like the
cue table it's called how do you map
from the world into the queue table the
states of the queue table don't worry
about what it is we'll get there and
then we'll look at how do you do this
learning process it's called q learning
the model we're going to implement how
do you do that so that's just
another step so finally when we
implemented that we train the model and
then we
will evaluate how it works right so we
train it and then we evaluate and the
evaluation is going to compare it to
the
the naive solution it will be surprised
how good it is so it's going to be
amazing so are you ready for all this i
hope so so in the next one we're gonna
learn about what is
reinforcement learning okay learn about
reinforcement learning sounds awesome
see you in a moment
so here we are in our jupyter notebook
and you see here there's a lot of
descriptions so let's walk through that
and see
how where we end that okay so the goal
of this one is to understand what is
reinforcement learning right
so
in the project in general we're going to
use object-oriented programming but
we're not going to talk about this quite
yet what we want to do is to build our
own machine learning model from scratch
which is the reinforcement learning
here
so
they will teach how to pick up and
deliver items just like the cab driver i
told about but the goal of this part
here is to learn what is reinforcement
learning
so
i'm a bit
a bit mixed about this picture actually
because this kind of uh
it kind of shows a world where you get
reward and a punishment but this is
basically the easiest way to understand
it right so
what we want to do is create a reality
where
where we have some data learning and
we'll do an action
and that action will either give you a
reward or a punishment and there will be
the feedback all the time right so how
they often explain this one is uh
think of a dog right how do you train a
dog
so a dog
you
if the dog is making behavior and that
you appreciate you will reward it right
but if the dog doesn't do what you want
you might
not reward it right so this is where
like classically you say you will punish
it right but i don't encourage
punishment of anything so
don't don't don't
don't hear like that but just find it
easy to understand that yes you can have
if you do good behavior you get reward
if you have bad behavior that is not
appreciated well you don't get a reward
or you get punishment so this is easy to
understand right reward good so you get
a feedback and you will by nature do
things where you get a reward right it's
just like in a job market if you get a
salary for what you're doing you like
doing it because you like the money
right you get more money if you do
something really good you get a bonus so
you get more money and you
you would in the future do something
similar so it reinforces reinforces this
behavior right
so this is reinforcement learning right
it's just a principle of understanding
if you do something good you get a
reward if you do something which is not
accepted or not wanted well you get a
punishment so say right so this is the
feedback and often actually they don't
use the terminology punishment anymore
just call reward it will just be a
negative reward right but it's easy to
understand that something good something
bad and the reward can also be measured
in a level so it could be
just be a number so it's a the feedback
is just a number
if it's positive it's rewarded it's
negative it's a punishment right so this
is how it works right data learning
right so you understand the concept now
so
if we talk as a dog here right
it's like training a dog right so
the dog doesn't talk the same language
as you just like the
machine learning model
so
how you're training a dog is based on
this reward and punishment right
and uh
this is the same action as i talked
about in our terminology here we use the
agent is a dog right so this is the one
we want to train we want to train the
agent and it exposed to an environment
and this agent encounters a state in
this environment and performs an action
based on that state to come into a new
state if you want if you want to go from
this state to the new state you will
make a reward if you want don't want to
go into this new state you'll make a
penalty or a punishment right
so this forms a policy and
to create random to choose action for a
given state in the future right so this
is learning right so
think again like the dog it is exposed
in an environment the dog is in some
kind of state so the dog has to make a
choice should it
bark or should not bark if it barks you
don't get happy if it not barks you get
happy right so you reward it or you
[Music]
make a negative reward
and uh
based on that right
perfect so that's that's the idea behind
it
so what is a re there are different
algorithms used for reinforcement
learning i'm not going to go into all
these here but these are the three main
ones and we are only going to focus on q
learning and it's a model free and
reinforcement learning model to learn a
policy telling an agent what action to
take under what circumstances so this is
the one we're going to focus on and you
can read a bit more about the other ones
if you want to
so
this is it
so
we have a q learning or a queue table
here
and this is basically just a table
showing you if you're in this state what
to do right
so
what do you do
what do you do right
so
you have two choices in the queue
learning
model paradigm and dime either you
exploit that is you choose the best
action you know so far and then you
update the queue table based on the
feedback right because every time you
make an action make a choice and then
you get a feedback then you get
something back positive or negative
right that's a feedback then you update
it based on that action
so let's exploit but
as you probably know if you don't do
anything new once in a while you'll
you'll get patterns that might not be
optimal so there's something called
explore and then you make a random
action and based on that random action
you also get feedback and you update the
queue table right
so imagine you always only done one type
of action in a specific
state for instance let's say that
so let's let's assume you're a dog right
and in the case you
when a visitor comes to your home you do
either you only try to do two things
right either you bark then your owner
doesn't get happy so you don't get a
positive reward or then you learn that
you jump up on people and lick them in
their face and then the dog owner did
nothing right it was just a
neutral reward right so it's better than
the other reward right so what you'll
learn from this is well you should jump
up of people and dick them in their head
all right it's kind of awkward but
that's what dog deals right
and
if you never did anything random that
will be the conclusion you had because
you only tried these two things so by
doing something random it could be not
jump up just stay passive and in this
case maybe the owner was like wow that
was really nice i really appreciate you
don't jump up of people
when
people come into my house and then you
would reward it even more right so that
action actually creates a more positive
feedback loop than
jumping up of people licking them even
though
that was better than barking when people
came right so in this sense if you do
something random you find new patterns
that are even more appreciated in the
circumstances right so this is why you
need this random action do something
random right
okay
so let's just
see what it says here q algorithm is
basically a q table so it's a big table
it's a matrix if you don't know what it
is it's just a big table of entries
and don't worry if you don't know what
it is and the agent the q learning
algorithm will be in a state right and
then in each iteration agent needs to
take an action right so that means you
need to do something right you come in a
state you need to take an action and the
agent will continuously update the
reward in the queue table right the
learning can come from either exploring
exploiting or exploring that's what we
talked about this translated into the
following pseudo algorithm and eq
learning the agent is giving in the
state and needs to choose an action
right
so this is actually the algorithm and it
can be a bit tricky to see here but this
is what we're going to implement a bit
later don't worry about this now because
we're going to do a lot of things that
are way easier than this first
but we initialize the q table to all
zeros
and
then we iterate in each
this is a description of the algorithm
above right then we iterate
so this q table here means just
a entry for each state
and uh for each possible action right
so the agent is in the state with
probability epsilon we will explore
else exploit right so we take some
probability we explore
that means we take if we explore we take
random if we exploit we take best action
right
and that's what it says down here and uh
if you take explore we take a random
action of the action possible and then
we exploit we take the best action based
on the current queue table
then afterwards we update the queue
table based on the new reward from the
previous state and this is the formula
for that right so the queue state we
took this action then we calculate a new
value based on this formula don't worry
about it we'll implement it and you'll
see how it works
so you see here in the above here
there's some new variables right we have
epsilon which is the probability of the
random action
to explore new territory as we talked
about then the alpha is a learning rate
of the algorithm this is this alpha here
and this is how much we learned from
previous action so this is a parameter
you can turn right and then the gamma is
this discount factor of used to balance
the intermediate and future reward right
and then the reward is a feedback action
of
any number
it can be negative as penalty or
positive as reward right
perfect so far so good so that was
actually the formula for this cycle here
right so we have the q table here with
all zeros if we take exploit we take the
best possible action from this state
and then we update the table based on
the formula below if we take explore we
take a random action and we update it
again
based on
this formula down here
okay so now to the description of the
task we need to solve right so now we
have the algorithm
now we need to describe what is we need
to solve so this is our universe right
we want to keep it simple so we create a
field of 10 times 10 positions right so
we have this board here it's like a ch
chess
board except we have
10 by 10 and not 8x8 right
in this field there is an item that
needs to be picked up and moved to the
drop-off point so
you are the carrier in this picture
right so you start here
in 0.9 right 0.9
then there's an item to be picked up
this is this item and you need to drop
it off down here right so
if you were
intelligent and you could see this field
here you would go over here pick this up
and drive down here
so the moves you can do is you can go
south
you can go north okay so south is down
north is up
you can go east
and you can go west and then you can
pick up item
and you can drop off item right so this
is a
this is like six
moves right
and uh of course there are some rules
about it you cannot go over the field
and if you pick up where this
where the item is not for instance if
you say here i want to pick up well you
can't pick it up because there's nothing
or if you drop it you picked it up
correctly and you drop it off
incorrectly you you can do everything in
every state right so you have these
six
actions here action zero go
south north east west pick up and drop
off
but we want to make some rules based on
that right and this is what we're going
to implement down below right so if the
agent tries to go off the field punished
by -10 in reward right so if for
instance we are here and we go
north that's up we're going outside the
field we get a punishment and we're not
going to let it go outside but we will
punish it by -10 right that's what it
says here
if the agent made a legal
move punished with -1 and reward as we
do not want to encourage endless walking
around right so this is it so even
though we move let's say we move
east here
in this direction it is punished by one
and the reason why we want to do that is
we want to minimize the steps we do
because if we
may positively reward it would find out
just walking around and makes a lot of
sense so we'll just walk around doing
nothing right so we want to minimize the
steps we're doing and we do that by -1
if the agent tries to pick up the item
and it's not there
or it already has the item it is
punished by -10 right so again you can
pick up you can try to pick up whenever
you want you can do
either of these
six
actions here so you can try to pick up
in the first year but the item's not
there it's really punished right so
in every
encounter we have the items will be
picked up the same place and dropped up
to the same place right so you will
learn
that when i am in this state this is
where i want to pick up and you will
learn
this is where it would drop off so you
want to
reward if you try to pick up when you
are here and punish if you try to pick
up anywhere
else and
if the agent picks up the item correct
place reward with 20 right if the agent
tries to drop off the item wrong place
or does not have the item if tries to
drop it off and doesn't have the item
obviously it should be punished
and if the agent drops off the item
correct place reward with 20 right and
then the game is over
that translates into the following code
or that's the code we need to implement
right so
in the next one we're going to try to
implement this field here so we can
implement it and then we're going to try
to make a naive solution where we just
do random actions and see how that
performs because it because it will
succeed if you just do random things
that's kind of funny but it will okay
are you ready to try to implement this
we're going to use object order
programming obviously are you excited i
hope so because it is
perfect see you in a moment
so are you ready to implement the field
that we are playing on let's get started
right
so we are in this reinforcement learning
here and we learned about the
reinforcement but the step we're looking
at right now is implement this field and
be able to make these actions down here
and
based on these actions to return the
reward
back to the user of that so let's get
started and as i said we're going to use
object-oriented programming so we're
going to make a class here called the
field which is the field we're playing
in
and remember we need to make this init
function if you have some in is in
initialize if you need to
initialize something right and we do
because what is it that our program
needs to know well first of all it needs
to know how big is a field because we
need that you can either hard code that
or you can implement it by
size setting a size so we're always
going to assume it's square so we put
the size here and then we just say
self.size is equal to size
size here
and in this way we will remember how big
it is right
and then there's an item to be picked up
item to be drop off and a carrier start
position right
so we can have that in those positions
[Music]
item
pick oh item
pickup
item
drop off
and start position right
and we need to save those self
all right then
pick up
item
pickup
self
item
drop off
i turn
drop off
self
and then we have position we just add
this as a position and we add the start
position for that one so this is the
position we are in right
and then there's one more thing we need
to know right so when we are driving
around here we need to know if we have
the item in the carrier or not right
because remember we need to reward if
you try to pick up
uh
and you don't have the item but if you
pick up if you do have the item you
should be punished and if you drop off
and you don't have the item you should
be punished as well with negative right
so self we can call it
item in car to be false by default right
so
this is
initialization of our class field but
right now we cannot do anything and the
next step we want to do is actually
based on that we want to
make an action right
make an action
so we call it make action
and this action here should take uh the
action right
and this action can be one of these here
zero one two three four five right
so on the first place here let's just
say if action equals equals zero right
then we just write pass and pass just
means that we haven't implemented yet so
do nothing right else
alif action
one we just write what it is so we
remember it here actually
we can do that by comment right so
action one
go south
action two
go
north pass
lf
action
2
[Music]
go was it east
i think it's capital actually in
america in
english right you have to write like
that i think so
i didn't do that up there and go vest
pass
alif
action
three
go
west
pass
alif
action
4
pick up item
pass
alif
action
equals
five
drop-off
item
pass right so this is the five actions
that we have right
and then we need to implement the reward
and
and
the reward
yeah the positive or negative reward
right
so what we need to know is in the
beginning here we need to get the
position of where we are so we start in
start position and position is a
tuple of two items right
so based on that
we just put x
and y here and we take self
position
so let's try to do that
so the first one here if the agent tries
to go with the field punish with -10
right so if we go south that's down
right so that means basically if we're
down so we go south here we go down here
we are outside the field so that means
that
if we are in position nine here that
it's a size minus one and we go down we
get punished right so if
y
equals equals self
size minus one
and what do we do we
return -10 but we also want to return if
we are done or not so we return actually
comma false we'll come back to that
later
so basically when we succeed in the end
here we want to
say true right
good
else
is there anything else we need to do
here well else we need to update the
position right so self position
and we take
x
comma
y
[Music]
plus one right because we go down and we
have created our field like this that we
go down it will go
adding to the y-axis we keep this y-axis
and this x-axis right perfect
good
and then we return -1 because we don't
want to encourage that and we still keep
false here okay
north is quite similar so if y equals
equals zero
then we are returned minus 10 and false
right and else what we do is actually we
want to move the position of our self
and it is
y
minus 1 now
and then we take return -1 false right
so in this case we can move up
north with one that means the y axis the
y number is going one up and
and
we're still not done perfect
action
east right
so east is
left so if we are here we go this way so
if we are in zero we need to punish
right
so if x equals equals zero
return
minus
comma
false still
else
we do
x no
self
position
actually i did this wrong here we needed
to have equal signs here
equal signs
pull
session
let me take x
minus 1 comma y
return minus one
false
and then we'll go west here
and if
x equals
equals
self
size minus one and then we do
return
-10 comma false
else
return
oh we need to update first self position
equals to x
plus one comma y
and we
are still punishing by one right so this
is actually the movement we can do right
so again
again this
these are coordinates right so we have
that the position here is x comma y this
is a start position we start with so in
the beginning it will be
0.9
0.19 or
9.0 actually
and then we update it accordingly into
that position right so we overwrite if
we go south we go the y-axis down
no up one and
north down one right so this is how it's
updated okay so
now this one is a bit more tricky right
remember if we try to pick up somewhere
where it's not
if we pick up ice and if we can't uh
if the agent
picks up the item
and
it says here if the agent picks tries to
pick up the item but it's not there or
it has it already punched by 10 right
so if
if
self
item
in car
so that is true
let's just do that return
-10
false
l if
item is not there so this is this case
is
we know it's not in the car but the item
is not there right so how do you check
that well you say that
self item position
item pickup actually
yeah
it is equal to x comma y or not equals
2.
and then we have
return minus 10 false
and else we're actually in the good
place right so then we have self
item in car
to be true now
and then we return
20 and false right we want to reward
this
by 20 right
if the agent tries to pick up the item
correct place reward with 20.
good
and then the final here
so again if
you want to drop off and if
not
self item in car
right if it's not in the car return -10
false
if
we try to then it is in the car and uh
self
item
drop off
is not equal to x comma y so it's not
equal to the position we are
and then return minus
10 comma false what it is there so we
need to drop it off right
so here we need to self
item
we use the pickup place so now we
overwrite the pickup place here to be x
comma y because we need to
even though it's
here right and then self item in car
is false now right
so we try to drop it off we drop it off
here then we have to pick it up again
here later right so this is a bit
confusing right and uh
finally else here we do this is a
correct place now we are in the correct
place so return
20 and true right
because now we're done okay so so far so
good is this all correct it's it's a lot
of code right remember
[Laughter]
so how can we test this well we're going
to test it in the next one we're going
to make a naive solution and see how it
works right but let's just for the fun
of it let's just hard code a trip in our
field
so
field
here we make a field and what does it
take it takes the size of it
and
pickup item
item pickup it is in
0 by 0
and then we have
item drop-off
9x9
and we start in
nine by zero right that's where we start
right nine by zero yeah perfect this is
our field
perfect
and now we can make actions right but we
actually know what how to get there
should we try to pick it up
and uh
so what we want to do is go east right
so go east is 2.
oh
make action
two right
then we get one minus here right and
then we can actually see in the field
position where we are right so now we're
in eight right and we can again just
for the fun of it let's just
let's just keep it in the same here so
we create a new one and then we can see
how we're moving around right
and let's just delete this and this
because we need to go
how many times one two three four five
six
seven eight did i correct here now we're
in zero zero right
so one two three four five six seven
eight nine so we moved
we moved one two three four five six
seven eight nine times and then we need
to pick up item and pick up item was
was
action four
action four
and then we need to walk
so here we have it
we're still in the same position here
and then we need to
basically we can
we can walk
left right left right left right so it's
like
south down it's zero and let's just go
south of the way and then we go
west afterwards so west is
three
so let's just take one like this
zero
now i need one more
run no it's right
is it
no i need one more yeah that's right
[Music]
and then we're down to nine and then we
need to
then we need to go
three
here now we're 99 and then we need to
drop it off right so this is our
superior
intelligence or overview of the
situation right that would make the
perfect match but we're not going to do
that when we do randomness so in the
next one we're actually
it seems like it's working right
we are traveling so what we did here
kind of testing if we can work around
walk around we didn't test the feedback
exactly but
but uh you you you know what's happening
here good so are you excited to make the
naive solution so get ready for that in
the next one see you in a moment
so are you ready to make the naive
solution so in here we are created a
field that represents this field here
and this game here where we actually
start by being the carrier here and we
need to move over here and drop it off
here so
we don't know anything so we are
basically
no we have no knowledge so what we would
do is make a random action and walk
around and until we succeed so let's try
to make an implementation of that
how that would work right
so
in order to do that we need to import
random because we need to
we need to do random actions right and
the next thing is let's make a naive
solution so naive
solution to it right and this one
will
we'll just
make a field here
field here and
we'll make the field actually the same
as this one here
we can actually make it a bit more
readable so one way to do that is
actually to have the size here to be 10
and then you change this by size because
then you know what it is and then it is
item
start
you put that to 0.0
item start
item
drop off
item
drop off
and then we have here we have a
start position right
position
start
position right
so here we have it right so it's a bit
more readable like that right so in that
way we
create the field right so now for the
naive solution right
and uh
so so far we actually need
somehow to
say if we're done or not because we're
going to make a loop and just iterate
and then we'll count how many steps
we're doing so steps is zero so
that's it while done we continue until
we are done right so we make an action
and we know we can make one out of six
actions so random
rand int
and it is between zero and
five right because they are not they're
including both right so it creates a
random action and then we take a reward
and we take a done we don't care about
the reward right now actually and then
we take the field and
make action
action right
and
then we take steps
and we take steps
plus one right and then we just continue
like that and then in the end we return
steps
so this is actually our naive solution
right
this is what we do right we just take a
random action we create it and we just
continue until we're done so how does
that work naive solution
zero that was not good
so
what happened here is
something not good
i actually put the drop off and start
position the same place here you see
that
but that shouldn't make a
zero oh well not done
thank you
so here you see it so you make an
enormous amount of steps here right so
here
you could just continue like that right
so it's different number of steps but
you see here
this one here is uh 204 209
229 steps and
that's a lot of steps and we're going to
try to improve that with our smart
algorithm right so how can we just uh do
a bit test here so let's just say
to get some kind of average base case
how many steps does it take well let's
just make a list here so
runs and we have that naive solution we
run that and we do that for i in range
let's just i don't know it's pretty slow
this one so let's just take 100 runs
and let's see how that goes and then we
get an average number of that
or we get it runs on that and then we
calculate the average
average of that in a moment when it's
done
okay so it's done
and so what you do is just take the sum
of the runs and then you divide it by
divided by the length of runs right it's
100 but here we have it so on average is
158 000
runs or steps to complete it in naive
solution so this is the naive and then
in the next one we're going to create
this q table or the states that is what
we need to do in the next one is how do
we map
from
this table here
a position is stated in this one
to a queue table so we can do updates in
our queue table that we're going to do
the computations on remember we need to
do this q table here with the
computations but we need somehow to know
what is the state and the state is
basically a
position of this one here so we need one
state for each of them and then we need
a
a action
probability for each action in it okay
so are you ready for that i hope so it's
going to be exciting see you in a moment
so what we want to do now is to figure
out a way to map from our world our
universe here to a state right
so we need a unique way to represent
a state in this one here so we need to
figure out what is a state right so what
changes in this one well one thing that
changes is the carrier it can move
around right and how can it move around
it can move around this 10 by 10
universe right so there's at least 10 by
10 states in it right so that's a one
variable that can change the another
variable is where is the item right so
the item can be in any place also except
the last one everybody ignore that so it
can also be in 10 by 10 states so
when we move around
in this carrier
our action should depend upon where this
one is right so if we move this one down
here
our action should be different based on
that as you can see right
so
they're actually 10 by 10 for this one
and then 10 by 10 by this one but these
two have to be multiplied right because
based on the position of this one we
have different actions different
behavior we want right so it's actually
10 by 10 by 10 by 10
this state of this but there's one more
dimension that's whether
we have the item in the car or the
carrier or not right so it's actually 10
by 10 by 10 by 10 by 2 right
so that's the size of our universe then
afterwards when we have the size of that
we can create the table because there
should be one
entry for each
state there is and then we need to
calculate uh based
the
probability base for each action
possible from there right so first of
all we need the number of states we'll
make a function or a method that returns
the number of states and then one that
maps from the actual position we're in
to the specific state so let's try to do
that
so we'll do that in our field here
so let's get started
okay so
what do we want to do here is get a
number of
states
okay it's a self here and what it does
it's we already realized that it is
self.size
times
self.size cell the size of that size
right so that's basically to the power
of
four
and then times two right because we
needed the two extra there right but
let's for the sake of it just write it
out here so it's a bit more clear
self
dot size
times self
dot
size times two right so this is our
the size of the
q table we need to create right so now
we can uniquely
define
what is the
state we're in here right so that is now
we can map the state here to a specific
number which is in the range from
this size here so now we need to kind of
get the state right and get the state
so how do we calculate that right
so
this
is
done like this let's just just assume we
have 10 states right
so let's start with the position right
so self dot
position we take the first coordinate
and this one should be multiplied by
self
size
self
size
self
size three times and then by two right
because
this one fills out the last one of this
formula here right so
this one is the first one of this one
here so this one can be in the range
from zero to nine right and we multiply
zero by nine by the rest of the factor
here right so that's the first
part of the state and then we
just i haven't yeah let's just do it
like this and then we need to add the
next one right and that's a self
position again plus self position of
one here
and then we multiply by
this part here right you see
oh here
because and now that's that is what
that's corresponding to right and then
we take state again equals state
plus
if you're familiar with the plus equals
sign you can use that but if you don't
know what it is don't worry about it
and now we need the item
pickup position right so
item pickup
let me take this zero
and then we multiply by
in this here
right
and uh
that's it
and state
equals state
plus
and then we take the
not this one we take this one here
and we take the
second coordinate and then we multiply
by two right and then the final one is
if it is in the car but this is a
boolean up here so you cannot just
multiply it like this so we say if
self
item
in car if it is then we take state
equals
state plus one
and that's basically it so now we
actually can map to a specific unique
state so the key thing to understand
here is actually
giving a number here we actually could
map it backwards to a setup in this one
here and for each possible setup or each
possible state here we can map it to a
number and from each number we can map
that to a state
it doesn't really matter how the mapping
is done it just has to be unique that
means you cannot have two states that
map to the same position
or a position here that doesn't have a
state right but we have a one-to-one
mapping right so each state has
each state up here has a number and each
number has a state
one's one mapping right
perfect so now we can just execute this
one here and it doesn't really change
anything but it will change when we do
the next step where we're gonna teach
and update our q
learning table okay are you ready for
that this was just
the mapping okay see you in a moment
so now we're getting there right now we
have created a mapping from this state
here to a number inside our code so
remember we did that by these two
functions here or methods there the
first one here is to create uh return
the size of it you can see there's 20
there should be a two here
and
uh and the second one down here it is to
get to state right this is doing the
mapping and remember there's a
one-to-one mapping between them right so
let's just execute that one again
that being said right
for each state here for each position of
this one position of this one and
whether we have the item or not there is
a number and for each number there is a
state one to one right
the next step we need to do is implement
this algorithm here right so what we
need to do is initialize a q table with
all zeros we're going to use numpy for
that numpy is really good for that it's
efficient to calculate with
and then we need to iterate like this
here right so the agent is in a state so
we get the state and then with
probability epsilon we choose to explore
or exploit if explore then we choose a
random action if exploit then we choose
the best action based on the current
queue table
then we update the queue table with a
new reward to the previous state and the
formula is like this here right this
long
i would i was about to say beast here
okay so let's try to do that and
actually i copied this down here so we
have it right
at our disposal but first let's import
numpy
numpy as np because we're going to use
that for a queue table
and then we're going to continue our
journey here and actually we need to
initialize things just like we did here
so let's actually do that here
copy these things down here
and
let's just
put them here right so we need the same
field here and so on so
first off we need to make this q table
so we create a field to get the size of
the q table right q
table or number of states we call it a
number of
states
we put that to be the field and we have
get
get number of states here right and then
we have number of
actions and we put that to
six
and then we create our q table
like this
we do numpy and zeros and we'll just
create a numpy full of zeros and uh what
do we do with that we have the number of
states and number of
no
number of
actions okay so all this does is if you
look at the q table here is create a
long table like that so for each state
we have
six possible actions and they're
initialized to zero all of them right
perfect so the idea is when
we use this q table we will update it
along the way according to this
formula here right we have a state an
action and then we update it according
to this as we learn and either we
explore or exploit right
so for that purpose
let's delete this we also need
for epsilon
epsilon and we just use 0.10 you can
play around with these values how much
you want to explore and so on
and then you have the alpha and alpha
it's i don't know 0.6
and
then we need a nose alpha is 0.1 and
gamma to be 0.6 let's do that
perfect
so in the first step here we're actually
going to iterate
because we want to teach the algorithm
so we're going to do a lot of gaming so
that means we're going to create a field
use our queue table and learn along the
way because in beginning we're not going
to be that smart because we don't know
anything because our q table is all zero
so for underscore in range first of all
we just do one iteration because we just
want to test our code right
so what we want to do is actually we
want to recreate these two here because
we create a new field for every pass
actually we don't need that one up here
so we have a new field to start with and
what we're gonna do first
it is uh
we're gonna go inside here while not
done we're gonna continue the same thing
here and then we're gonna get the state
which state are we in well we have the
field here get
state we have the state
next thing
next thing we need to do is well we need
either to explore or exploit right so we
do explore with probability epsilon how
do you do that we need random did we
import random
i don't remember if we imported random
oh we did the naive solution here so we
have random already so if random we say
uniform
uniform zero point
zero point
zero comma one so that's a uniform
number in the interval from zero to one
if that is uh less less than epsilon
epsilon then we do random action right
so action will be
random
rent int
zero two five right a random integer
from zero to five
else
we do action and then we do the best
action we know and that's
we take the arc max which is a maximum
of
our q table
or
q table here
of the state right so what this one
returns here
i can just show you an example
this one will turn an entry
of
possible actions we start by all zeros
remember so the arc max here in this
case here
we'll just return i guess it just
returns the first one in this case
zero right but if we were in another
case where we had different values
it will return the maximum of that right
okay so now we have an action so
let's do the action and we have a reward
and done right if we're done right so we
do the field
action
oh
i think it was called make action
action right so now we are done the
action
and after that we need to make
update our table here and this table
here is this beast formula here
and either we can do the formula
immediately like this let's just see we
can
have it here
well this is a long line
but let's
let's let's try should we try to write
it directly
i don't know q
table let's try
uh
state
action
one minus alpha
here times
q state action
or
q
table
state action
plus alpha
times
reward
plus
gamma
times
and then we have the maximum
and the maximum of the new state right
okay so this is this is this is we need
we need the new state here right so
new state
is equal to
field get state
here right
and the maximum of that state here let's
just have haven't calculated here so
new
state max is equal to
q table
q table
new state and we just take the np max of
that right so i have the maximum value
of the new state right because we're
looking forward here right
new
state
max
and then we actually subtract the old
state q
table
state
action right and this should actually be
it
always a bit scary with these long
formulas but it looks pretty
accurate okay
so
so far so good so actually now we should
have run it once here so let's just try
to run it once
and already done so we actually solved
the solution one here and let's just
look at the queue table to see that it's
not all zero anymore right so now it's
updated not always all values are
updated and it's probably states that we
will never encounter it even though we
run it again and again okay
perfect so now we need to run it
multiple times and i'm a bit worried
about how long it takes actually
okay it's pretty fast so let's let's run
it
uh 1000 times here
let's see how it goes and it goes pretty
fast and
the more you run it the more it learns
right so
what i'm gonna do now here is going to
run it let's say 10 000 times and
then in the next one we're going to
evaluate how good our algorithm or our
queue table is based on that and i'll
show you in the last one how to do that
it's going to be amazing i promise you
that so see you in a moment
so actually it was faster than i thought
so actually ten thousand
ten thousand times is
probably nothing you can do it more but
let's just see how it performs with ten
thousand iterations it was just like a
few seconds so
it was like wow faster than i thought
right so now we want to evaluate this
one just like we evaluated the naive
solution up here remember we just did
the naive so we're going to make a
similar one
down here a function here which is
called re
in
force
meant
learning
and do the similar thing here right so
what we need here is uh the same things
but we don't want to overwrite what's
called overwrite our q table as it is
but we are learning along the way
actually so actually we
we want these variables and we're going
to do the same thing as down there
but
first thing first right so
we need the field here and then we need
to
see how many steps we're using now steps
equal to zero right and then we are
actually gonna do the same thing here
we're gonna do while not done
we're actually gonna do exactly
this
and this thing here
perfect
but we're going to do one more thing in
the end here we're going to add
steps
equals to steps plus one and then after
we're done here right we're going to
return
return
steps
so
this should basically be it because
we're going to do the exact same thing
this one was not intended correct we're
going to do the except exact same thing
here
and we're going to continue learning
because that's how the algorithm is it
automatically becomes smarter all the
time and
then we're going to see how many steps
we are using
after we have trained it 10 000 times
and you could probably train it more but
let's just see how it performs i have no
idea how it performs so let's execute
this one and then we have this
reinforcement learning and let's just
recall how many steps we used up in this
one here on average 150 000 so let's see
just a random run here 32
32 steps wow let's do again was that it
wow that's good so let's see how we do
so let's make runs again here
runs reinforcement learning
runs rl
so we don't overwrite the things up
there then we do reinforcement learning
and how many
runs did we do up there we did 100
so for underscore in range 100.
we have this one here and then we do
just a sum
of runs
pl
divided by length of runs
pl
and you see on average okay it's a bit
more than these were a bit lucky but 50
steps comparing to
[Music]
comparing to
150 000 steps on average okay so this is
what this algorithm does right isn't
that amazing
how amazing it is and let's see
do we remember the optimal we actually
did the optimal here how many steps is
the optimal uh so we have one two three
four five six seven eight nine
ten plus nine
plus nine
so that's 19 plus uh
28 plus
one here it is
twenty
nine right optimus 29 so actually these
ones were pretty good actually but on
average there sometimes it goes a bit
off track apparently and maybe these
outliers are way out so the optimal is
29 and you cannot do better than that so
this is pretty good this is pretty good
i mean i mean
that's nice
so here you have it right
amazing little journey we had together
and i really hope you enjoyed this one
and you see how amazing this is and we
actually built this from scratch
the only library we're using is a random
library and then numpy and we use just
the data structure from numpy we don't
use any advanced things and random is
just part of the algorithm so i hope you
enjoyed this one it has been amazing to
create your own reinforcement learning
model this is this is this is so crazy
right now you can say you can say i
built my own machine learning model from
scratch congratulations that's awesome
perfect so
thank you so much for this one and now
i'm just going to finish it off in the
next one see you in a moment
thank you so much for joining me on this
journey remember you went from the start
creating a simple hello world program
and then you learned about variables you
learned about program flows you learned
about dictionaries and lists and you
learned about numpy pandas you learned
about machine learning you created your
own machine learning model you should be
proud of yourself
how many people can go and say well i
built my own machine learning model from
scratch that's huge
and
many people say then what next right i
still don't feel i can sit down at the
keyboard and just type away well it
takes time right start with small
projects and build upon that and then
continue every single day the more you
do it the better we all been there and
we all still
even i i google things all the time to
figure out how to solve it so don't
worry about you don't remember it slowly
more and more will be on place but i'll
reveal to you in the end
there is no end there will never be a
day where you just remember everything
and knows everything
programming is about learning new things
all the time new libraries come new ways
to do things
and it continues and that's why it is so
awesome you learn new things all the
time and you can solve problems in front
of your computer it's so amazing right
i love that and i hope you kind of got
the energy from that because this is
what i want to give to you to how
awesome it is to program and how much
you can
build on your own in front of your
computer
so
what's next
i do have more free resources you can
have i have some free courses here on my
youtube channel you can go to my web
page as well there's a link below and
you can look at the free video courses
there you can scroll down there and you
can see
and then take them as well please also
if you didn't get the ebook get the
ebook because it will help you to
remember how to do stuff
finally you also have some paid courses
if you're really serious about this then
a good way is also to have some more
longer courses for instance i have my
python for finance course if you're into
finance and you want to learn how to
invest well this is a course for you and
it is like i think it's 21.5 hours long
and it is full of code similar to this
just in the aspect of finites and you'll
learn a lot with pandas and numpy and
actually also matplotlib these are the
main libraries we use in that course and
you'll learn how to find the right
investments okay so you're excited about
that thank you
finally and just say i hope you
subscribe to this channel because i'll
be posting more free tutorials like this
one on courses like this one and
yeah like subscribe and all that and
spread the word i
really the way i
the way i get encouraged to do this is
helping people so the more people that
know about this the better so without
said please leave a comment below how
you like this course and how you want
what you what your ambitions are what is
that you want to be do you want to be a
data scientist do you want to be a web
developer or what is it that you want to
be please let me know i like to hear
that so i know more about what troubles
or what challenges you have in front of
you so unless anything else let me know
in the comments so see you in the future
bye bye
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.