Learniverse

Learning Python Fast

00:00

Hello everybody and welcome to Python as fast as possible.

00:05

In this video I'm going to be trying to teach you Python as quickly as I possibly can.

00:10

That means I am going to go over things quite quickly, I'm going to talk very fast and

00:14

I will note that this is not for absolute beginners.

00:17

If you are looking for a beginner level tutorial series, I have many on my channel, I will

00:21

link a ton of resources down below and if you feel that I missed anything or made any

00:25

mistakes please do leave a comment.

00:28

That being said, let's go ahead and get started and get into Python as fast as possible.

00:41

To get started we need to download and install Python.

00:44

Please download the most recent version of Python from the Python website which will

00:47

be linked in the description.

00:49

If you are using any version of Python which is below 3.6 that will not be compatible

00:53

with this tutorial.

00:55

When downloading please check the box that says add Python to path, this will make

00:59

your life easier in the future.

01:01

Now Python automatically comes with its own environment to write code in, this is idly.

01:06

You can use this to write your code if you'd like but I prefer to use Visual Studio

01:10

code which is a text editor.

01:11

To download that you can go to the link in the description and just download the stable

01:15

build for whatever operating system you are on.

01:17

Now if you are using VS code there is one little bit of setup we need to do to get started

01:21

here.

01:22

So before we get started I thought it would be a good idea to talk to you about some

01:38

of the things that Python is used for.

01:41

Here I am on my channel in the playlist tab and you can see a bunch of different playlists

01:44

just giving you an idea of some of the things that I've created with Python and taught

01:48

on this channel.

01:50

Python is a general purpose programming language.

01:52

It is very easy to learn, versatile and really easy to write it.

01:56

The core advantage of Python is typically how simple it is, how easy the syntax is to

02:01

read and just how fast development is with it.

02:04

That being said Python is mostly used on the web and machine learning, artificial intelligence

02:09

and data science related tasks.

02:11

Now the sponsor of our video knows a ton about that and that is simply one.

02:15

I need to thank Simply Learn for sponsoring this video and giving you all a discount on

02:19

their data scientist master program that was co-developed with IBM.

02:23

This program is comprised of six unique courses that implement a unique blended learning

02:27

experience and a high engagement online classroom environment.

02:31

During this program you will master 30 plus in-demand skills and work with tools and languages

02:35

like R, SaaS, Python, Tableau, Hadoop and Spark.

02:39

You will master these skills through 15 real-life projects and one capstone project during

02:44

this 12-month comprehensive course.

02:47

You will also be given 1200 USD worth of IBM cloud credits to use during your 24-7 access

02:52

to the IBM Watson platform.

02:55

After completion of this course you will be giving certificates from IBM and Simply

02:58

Learn to testify to your skills as an expert in data science.

03:01

Get started by hitting the link in the description.

03:04

I want to start by covering the core data types in Python.

03:07

These are int, float, string and rule, and otherwise known as integer.

03:12

Now an integer or int is simply any whole number that does not have a decimal point on

03:17

it.

03:18

So 2, 3, 7, 3, 7, that was going to be an int, negative 9, that's an int, negative some random

03:22

large number is an int.

03:24

So long as it does not have a decimal point on it, we consider it an int.

03:29

A float is almost the opposite of that, it is simply any number that has a decimal point

03:33

on it.

03:34

So something like 272.0, this is a float even though you can consider this a whole number.

03:40

The point 0 adds a level of precision to this which makes it a float.

03:44

Of course something like 9.7 would be a float whenever you see a decimal point, that means

03:48

the number is a float.

03:51

Next we have string.

03:52

A string is simply anything surrounded by single or double quotation marks.

03:56

Typically we consider this a sequence of characters or numbers.

04:00

So if I type something like hello and I type something like hello, these two strings are

04:04

actually completely equivalent.

04:06

It does not matter whether you single or double quotation marks, there is no difference.

04:11

I will note that if you want to embed a double quotation mark or single quotation mark

04:15

in the actual string itself, then you would usually wrap the string with the opposite

04:20

type of quotation mark.

04:21

So in this case I want to have this double quotation mark inside the string.

04:24

So I wrap the string with single quotation marks.

04:27

Now just to note here, if I do something like 4.6, even though you might consider this

04:31

a float, this is a string, again because it is surrounded by single or double quotation

04:35

marks, remember the definition anything defined or surrounded by single or double quotation

04:41

marks.

04:42

Next we have Boolean.

04:43

This is one of two values.

04:45

We have true or we have false.

04:47

Some people may consider this one and consider this zero.

04:50

We're going to look at these later on and you'll see why that's important, but that

04:53

is a Boolean value and these are the four core data types in Python.

04:58

The next topic I'd like to cover is output in printing.

05:00

This is very important and we will be doing this a lot in this video.

05:04

In Python there's a simple function called print.

05:06

This is how you output something to the console when you run your program.

05:10

Typically when you print something you'll put it in strings so I want to print say hello

05:13

world.

05:14

So I will print the string hello world like that and if I run my program by pressing

05:18

this run button you can see down here in the console we get hello world.

05:22

Now notice that if I just type hello world like this and I don't put it inside of a string

05:26

and I run my program I will get an error.

05:29

This is because I cannot simply print something like this.

05:32

The program does not know how to interpret hello world if it is not wrapped in strings.

05:37

That is because this is not one of the core data types and we have no variable or anything

05:41

that denotes what hello world is.

05:43

That being said if I want to print a number say like 4.5 Python knows what 4.5 is because

05:49

it is a float you can see when I highlight it it says float.

05:52

If I run this it will print 4.5 out and there will be no problems.

05:56

Now if you want to print multiple things what you can do is separate them by commas.

06:00

So say I would like to print 4.5 and then I would like to print hello.

06:04

When I do this it will automatically add a space between the two things.

06:08

Let's just put another string here just to show you what I mean.

06:10

So 4.5 hello notice it adds that space in between.

06:14

Now we will talk about how you can print things without the space in between later on but

06:18

that is the basics of a print.

06:20

If I want to print many things on the same line I can go hello I can say end I can

06:25

say 87 I can say false and I can run this and this wall where hello and 87 false.

06:33

Now there's a few arguments that you can pass into the print function.

06:37

If you do a comma and then type end equals you can denote what you want the end of this

06:41

print statement to be.

06:43

By default the end here is a character turn which is backslash n.

06:47

What that means is go to the next line after this line is printed.

06:51

Now notice if I don't add this backslash n in fact let me just show you what happens

06:55

by default.

06:56

So let's say have hello and 87 I'm going to get rid of this and I print both these things

07:01

out we get these on two separate lines.

07:04

That is the default behavior of the print statement in prints on a new line.

07:08

Now notice if I add end equals and instead of a backslash n let's say this pipe like that

07:15

and I run this now at the end of the print statement it simply prints a pipe.

07:19

It does not add the carriage return which means we do not move on to the next line.

07:23

That's a little bit more advanced but essentially the carriage return is that backslash

07:26

n and again that means just move down to the next line.

07:29

That is the basics of printing and output and now let's move on.

07:33

Next let's move on to variables.

07:36

Variables are an important part of any program in Python they are quite easy to create.

07:41

When you create a variable in Python you simply type a variable name a space and equal sign

07:46

and then assign it to some value this value is going to be some data type some function

07:51

something else that we'll talk about later on.

07:53

To give you a basic example I'm going to set the variable hello equal to the value 10.

07:58

Think of a variable like a bucket that stores some data or stores some type.

08:03

Now if I want to access the value that is stored in hello what I can do is I can simply

08:08

say print hello what this will do is it will say okay this is a variable I know it's

08:13

a variable because I saw it earlier on the program let me see what its value is and I will

08:18

print that value.

08:20

So now I'm going to run and you can see that we get Tim.

08:23

Let's make another variable let's call this world let's actually set this equal to the

08:27

value world.

08:28

Now let's print hello comma world and let's see what we get we get Tim and then space

08:34

world.

08:35

Variables are somewhat complex you can make them equal to other variables.

08:38

If I decide to change the value of world to actually be equal to the variable hello what's

08:44

going to happen is to say okay hello is a variable what is a low equal to hello is equal

08:48

to Tim so I will print Tim.

08:51

Now watch what we're going to get we get Tim Tim.

08:54

Now what happens if I come here and I change hello to be equal to no.

08:58

If I do this what's going to happen is world will not change because world was set equal

09:03

to the value of hello at this line at this point in timeline three because our program

09:09

reads from top to bottom the value of hello was Tim.

09:13

So world gets set to Tim then hello gets set to no that does not change the value of world

09:19

so what you're going to see is we're going to have Tim and then we're going to have no.

09:23

So let's have a look here and sorry other way around we get no Tim my bad that we printed

09:27

a hello first and then world I have that mixed up.

09:30

As the basics of variables there's many other things that we can do with them but let

09:33

me just tell you about the naming conventions for them.

09:36

So in a variable you are not allowed to have any special characters and you cannot start

09:41

with a number so you are allowed to have an underscore that is the only exception in terms

09:44

of special characters usually you will use that when you're noting a space and in Python

09:49

the convention is typically to separate things using an underscore in other languages you

09:53

may see something like hello world if you wanted to make a variable that has multiple words

09:58

in it but in Python stick with snake case which is what this is known as the other one

10:02

is camel case and use underscores to separate your variables just to give me an example

10:07

of what I mean you cannot start a variable with a number so nine hello notice I'm getting

10:11

the red line popping up here and I could not do something like hello nine that does not

10:15

work but if I want to hello world 32 that is totally fine that is a valid variable name.

10:21

The next topic I want to talk about is getting user input just like printing something

10:25

is very simple getting input is simple as well what we do is we simply use the input function

10:29

we have our open and close parentheses and then we type what's known as a prompt inside

10:33

of these brackets like this now this does need to be in a string unlike the print statement

10:38

we cannot do something like 4.5 in here that will not work we do need a string so what

10:43

I'm going to do is I'm going to add a prompt say I want the user to type their name

10:46

then I will say name I'll do a colon and then I will add a space between the colon

10:51

and the end of the string the reason for this is that the user will be able to start

10:54

typing out on this line right after the prompt so if I don't have a space then what's

10:59

going to happen is the user will start typing right where the colon is which of course

11:02

is not going to be good so let's go ahead and run this and have a look at what happens

11:05

we can say name colon space and now I'm going to type 10 that's great but how do we

11:11

actually get the value how do we store what the user typed in so we can use it later

11:15

well we need to assign this input to a variable so what I'm going to do is I'm going

11:19

to say name is equal to input I just want to note because I forgot to say this previously

11:23

we could have capitals in our variable name if we want that would be fine but I'm not

11:27

going to do that and now what will happen is whatever we type in will be returned to

11:32

us in the statement as a string and it will be assigned to this variable name so if I go

11:37

ahead and print the value of name what's going to happen is whatever we type in here

11:40

will be printed out so let's have a look at this name let's type Tim and we see that

11:45

Tim is printed out below there now we of course can get multiple user input so I can

11:50

say something like age equals input age colon and now let's see what happens when I run

11:56

this so we go name Tim age 20 and we could put print those out if we want but I'm not

12:02

going to actually you know what let me show you one example where I print them out let's

12:05

say hello and then let's go comma name and then you are comma age years old so this is

12:15

a way that we can combine variables and strings so that we print out a meaningful responses

12:19

hello whatever the name is you are whatever the age is years old so in this case I'll

12:23

say name Tim age let's go 12 and says hello Tim you are 12 years old all right so that

12:29

is it for input now we're gonna move on and talk about how we can actually convert say

12:33

numeric input into a integer or float because a default comes back as a string all right

12:39

so the next topic I want to cover is arithmetic operators these essentially like plus minus

12:44

multiplication exponent how you perform mathematical operations in Python now

12:49

they're just something to keep in mind when we're performing and using arithmetic operators

12:52

we have to make sure that the data types on the left and right hand side of these operators

12:57

otherwise known as the operands are the same data type or are both numbers what I mean

13:02

by this is if I have something like hello and I try to add nine to it well that just does

13:07

not make any sense and we're gonna get a error and something's gonna be wrong with our

13:11

program so just keep that in mind I cannot do hello minus nine cannot do hello divided

13:16

by nine you actually can use the multiplication you can take a guess of whether that's

13:19

gonna do but I will show you that later all right so let's go through the basic operators

13:24

the first one is the plus sign very straightforward we can add nine plus three or in this

13:28

case I can add x plus y so x is nine y is three we can add those two values together ignore

13:34

the squiggly when I save that goes away now so I'm gonna keep in mind if I make this

13:37

3.5 this works totally fine even though these are different data types since they're

13:42

both numbers I can add them together of course I can do the subtraction let's run that

13:47

I can do the multiplication which is an asterisk and I can do the division we do the division

13:53

we get some crazy number just keep in mind that the maximum precision and Python I believe

13:57

is 13 decimal points there is ways to get beyond that but that is kind of too advanced

14:02

I'm not gonna cover that right here and when you ever do division or you're dealing

14:05

with rounding and all of that that's always a problem in computers so just keep that in

14:09

mind you're not gonna get the most precise answers with division like this if this is

14:14

a non terminating decimal point anyways hopefully that makes sense now I just want to point

14:19

something out if I actually have say nine over three of course you know that that answers

14:23

three and that is a whole number right three is a whole number but this is actually returning

14:27

to us a float not an end even though neither of these data types are afloat the reason

14:32

for that is because whenever we use the division operator it returns a float because

14:37

it does not know if it's gonna need to represent a floating point or it's gonna have to

14:41

represent some level of precision so just keep that in mind if for some reason you don't

14:45

want this to be a float of course you can convert the result to an end by just doing

14:49

int like that okay so those are some of the basic arithmetic operators next we have

14:54

exponent so that's two asterisks like that this will raise x to the power y so we can

15:00

see that gives us 729 and then next we have floor division so what I'm gonna do here

15:05

is actually make this a 10 have a guess at what you think this is going to give but this

15:10

gives us three the reason it gives us three is because this will give us the integer result

15:14

of whatever the division is imagine this was like nine point three three three or whatever

15:18

it may be it just simply removes all of the decimal points and just give or sorry it

15:23

be three point three three three removes all the decimal points and just gives us whatever

15:27

that integer result is next is mod this is the percent sign this returns to us the remainder

15:32

after a division in this case 10 mod three is gonna be well the answer is nine and then

15:37

remainder one right so that is why we get one from this here I think that's it for those

15:43

operators now I will simply show you how we can do say order of operations so if I want

15:48

to order of operations we can use brackets or if we want to evaluate things in a specific

15:53

order this does follow the simple order of operations which the acronym I know for it

15:57

is bed mass which is B E D and I hate this auto complete it's making it very hard

16:04

to do this M a S okay so what this stands for is brackets exponents division multiplication

16:11

addition subtraction Python does follow those standard order of operations if you just

16:16

write an expression but of course it's always easier to use brackets and spread things

16:19

out so I can say something like x mod y and then I can multiply that by two since I didn't

16:25

say it the integer division and mod operators are in the lowest order of order of operations

16:30

so if you use mod that is gonna be below addition and subtraction pretty sure that's correct

16:34

but someone can correct me in the comments if I'm wrong about that but anyways you get

16:38

the point you can use brackets and that is how you can perform and do arithmetic in a certain

16:42

way alright next thing I need to cover here is simply just an example using input so what

16:47

I'm gonna say is num equals input and we'll just say number calling like that but what

16:55

I want to do is simply print the result of a num minus five so I want to take whatever

17:00

number they type in and I want to subtract five from an imprint that to the screen so

17:03

let's try it num equals five notice that it says unsupported operating types for minus

17:08

are for sorry minus string and int so what that's saying is that num is a string even

17:14

though it looks like an int to us I'll explain why in a second and we cannot subtract

17:18

the number five from it that just doesn't make any sense the program doesn't know

17:21

what to do with that so think about this why is num a string we typed in five this should

17:27

be an int value from what we understand about our data types well this input automatically

17:32

returns to us whatever we type in in a string so just imagine that whatever we do here

17:36

it's gonna give us a string and that means that if we want this to actually be a numeric

17:41

value and be able to perform arithmetic on it we need to convert it to an int so there

17:45

is this fancy function called int in Python this will take some string assuming that there

17:50

is an int inside of that string and it will convert it into its integer representation let's

17:54

have a look now when I type a number let's say 10 of course we get the value five it

17:59

took num which was 10 so the string 10 you know I'll type it out like here we have

18:04

the string 10 and it simply converted that to the actual integer value 10 now you can

18:08

imagine we have other functions in Python that do this as well we have say float that's

18:12

gonna take whatever we have here and convert it into a float now notice what happens when

18:16

I do this if I do float 10 we get five point oh whenever you have at least one float

18:21

in your arithmetic it's going to automatically return to you a float it needs to keep

18:25

the level of precision that was in the original you know operation or any original value

18:30

so just that's something to keep in mind whenever you're doing you know plus minus multiplication

18:34

if there is one float as the operand so on the left or right hand side of the operator

18:38

then you're going to get a float result back alright that's all I want to show for this

18:43

now let's move on so the next thing I want to cover is something called string methods

18:48

before I do that though I need to define what a method is so let's create a variable let's

18:52

make this equal to hello and we can just say hello equals the string hello I want to show

18:56

you this function called type which will tell us the type of variable so if I print

19:00

the type of hello well you can imagine what type this is going to be it simply it's data

19:05

type let's have a look here we get class string you can ignore the angle brackets in the class

19:09

if you're more advanced you probably already know what this means but essentially what this is

19:13

saying is that this string or this variable here hello is an instance of the class string again

19:19

big words you don't have to know what those mean but for anyone more advanced hopefully that

19:22

makes sense to you alright so how do we use a method on a string and what is a method well

19:28

method simply is something with a dot operator so we have dot we have some name whatever the

19:33

method name is in this case it's gonna be upper and then we have an open and close bracket or

19:38

parentheses whatever you want to call them now I just want to show you what these methods do so

19:41

if I say hello dot upper this is a method that we can call only on strings so since this is a

19:48

string I can use this dot upper method on it and imagine what this is gonna do what this actually

19:53

does is it uppercases or puts the entire string in completely uppercase so if I have a look now at

19:59

hello and we printed out we can see we get hello in all capitals now I don't need to call it like

20:05

this I also can do hello dot upper now the reason this is gonna give me the same thing is because

20:11

well this is storing the value of the string which is hello and then it was just making an

20:16

uppercase right here now what happens it says okay well what is hello equal to oh well hello is

20:21

equal to the string hello so you can think of it as a substitution this string just goes where

20:25

that is makes it upper and then prints it out there we go upper now let's have a look at what dot

20:31

lower does you can probably imagine already but it simply makes the entire string lower case

20:36

these are useful when you're getting user input and you want to validate and check if it's

20:39

equal to something which we will do later on so if you're other methods we have dot capitalize

20:44

you can imagine what this does it simply capitalize the first letter in the string and then

20:48

makes the rest of it so it looks good right so I've had a sentence I said hello world like that let's

20:54

have a look at this it's gonna give me hello world properly and if I added a capital W like that

20:59

it removes that and it just kind of formats it as if a sentence would next we have this method

21:05

called count what count does is it will count a specific string within the string so it looks

21:10

very substring in the string and how many times that occurs let's see I wanted to see how many

21:14

double L's were inside of the string well I can say hello dot count double L and then that will count

21:20

and tell me how many are in here now have a look and guess how many you think are in here well there's

21:25

actually zero the reason there's zero is because these are capital L's and this was looking for

21:29

lower case L's so that's why it's very important to make sure you're differentiating between

21:33

capitals and lower cases because two lower cases L's are not the same as two upper case L's

21:39

but if I want to fix this problem I can make the entire string lower case so I could say hello

21:43

dot lower and then I could count all of the double L's in the lower case string so now let's

21:48

look at this in this case we get the value one let's make this a little bit better and let's look

21:52

for all of the is there any other ones that we can look for I guess we can look for how many

21:56

owes there all right in that case we get two owes so those are a few basic string methods there

22:02

are some more but I just want to show you how you use these and the fact that you can chain them

22:06

on each other because what happens is this will return a string you can imagine that the substitution

22:10

of this just looks like you know hello world and then it's going to count all the owes inside

22:16

of hello world and return to us that value which in this case was two all right so now we're going

22:20

to move on to conditions and conditional operators and before I mention that I just want to go

22:24

over something that I forgot in one of the previous sections this is simply string multiplication

22:29

and string addition this is important so I'm just going to cover it quickly but essentially

22:33

let's have a variable and that's equal to hello so x equals to a low have another variable

22:38

and this is equal to actually we can say three well there's this cool thing in python

22:42

but what I can actually do is multiply a string by an integer and that will simply repeat

22:46

the string the integer number times so if I say x multiplied by y even though these are the same

22:52

data type which I said before might cause problems since it's a string and it's an integer and the

22:58

string is on the left hand side we can do this so I can say hello at this gives us hello hello

23:03

because we multiplied the string by the integer so just something to keep in mind that we also

23:08

can add two strings together because that actually makes sense so if I say hello and then I say yes

23:12

well I can add these two together this is known as a concatenation so let's have a look at this

23:17

and I get hello and then yes so you can do that you can add two strings together and you can

23:23

multiply a string by an integer just need to throw that in there because that obviously is something

23:27

that's important to understand all right next we're going to talk about conditions and conditional

23:32

operators so a condition is simply something in python that evaluates or compares to variables or

23:38

data types and returns to us either a true or false value based on the comparison so to you know

23:45

giving example of a comparison is x equal to y is x less than y those are comparisons and the

23:50

answer to that is either yes or no or in our case true or false so we have that true and we have

23:56

these false this is Boolean so the result of a comparison is simply a Boolean value all right so let's

24:02

look at what these operators are we have two equal signs which checks for equality is the left

24:06

hand side equal to the right hand side we have not equal to which checks for inequality so is

24:12

the left hand side not equal to the right hand side we have less than or equal to greater than or

24:16

equal to less than and greater than now as a few other random ones we're not going to go through

24:21

those but these are the core conditional operators and let me show you an example of a condition

24:25

let's say I have x equals hello and y equals hello I want to check for the comparison so I might

24:32

do something like print x equals equals y are these two things the same let's see yes they are we get

24:38

the value true now let's do they're not equal to sign are these things not the same no they're

24:43

not so we get false that is the basics of checking for equality now what if I add a capital L

24:49

inside of this string well we know that lowercase and capital L are different so if I do this

24:54

we get true which means no these two strings are not the same they are different if I do double

24:58

equal sign then we get false now interesting thing with strings we can actually compare them using

25:03

the greater than sign and less than sign now it's not super intuitive so I have to kind of show

25:07

some more primitive examples here to give you an illustration of how this works but if I have

25:11

something like a and I say is a greater than let's actually go with capitals that what do you

25:17

think the answer to that is well that's actually true now the reason for that is every single

25:22

character inside a python is represented by what's known as an ASCII code we actually have a look

25:27

at what this ASCII code is by looking at what's called the ordinal value of a character so if I print

25:32

out the ordinal of z we can actually see that that's 90 if I print out the ordinal of a we can

25:36

actually see that that's 97 so what that's telling us essentially is that this uppercase said is

25:42

actually represented by the number 90 and the uppercase or sorry the lowercase a is represented by

25:49

so what we do is we would compare the two ordinal values of these characters and see that well actually

25:55

a is greater than z now if we do this so I say a greater than b then that's going to be false

26:01

because b is actually represented by 98 there is some logical ordering for these characters

26:06

but I just want you to understand that we can compare strings using greater than less than

26:10

equal to all of those but just understand that we're comparing the ordinal value of the strings now

26:14

let's say we have something like ab what we're going to do is simply just go from left to right

26:19

and compare all of the characters so we would say okay is a greater than b yes it is imagine

26:24

these two are the same so we had a and then we had d like that if these were the same then we

26:29

would go on and compare the next character which would be b in this case we get false because

26:33

obviously the d is greater all right so that is how that works now let's just show a few examples

26:40

let's just say 7.5 equals equals seven is that true obviously that's false let's say 7.5 greater

26:47

than seven obviously that's going to be true and what about 7.0 equals equals seven have a look at

26:54

that we get true all right that is the basics on the conditional operators and conditions of course

27:00

you can store these things in variables I can say result equals six equals equals six and print

27:06

result like that and we get to the value true okay let's move on now to change conditionals

27:14

so a chained conditional is essentially combining multiple conditions together to create one larger

27:20

condition what I have right here is a basic example I have three conditions right I have result one

27:25

result two and result three result one says is x equal to y result two says is y greater than x and

27:30

this one said is z less than x plus two you can imagine that these have brackets around it it

27:36

works the same so all of our conditional operators have a lower precedence than all of the arithmetic

27:42

operators so if I do something like z minus two less than x plus two well that's not going to

27:48

say z and then negative two less than that it will evaluate on the left hand side and it will

27:53

evaluate on the right hand side and then it will combine and use sorry this conditional operator

27:59

so hopefully that makes sense I'll just keep that in mind that you can actually write expressions

28:03

on each side of a conditional operator okay so now that we have that essentially what I want to

28:08

do is say okay how can I make a condition that uses result one result two and result three so

28:13

obviously all of these are equal to true false true false that are equal to something like that

28:17

so what I can actually do is use three separate keywords so and or

28:22

oops and not so these three words are what I can use to chain

28:26

conditionals together and create larger larger conditions from specific conditions

28:32

themselves so what I can do is do something like this I can say result four equals result one

28:38

or result two now what is the or operator do well the left hand side and the right hand side

28:43

of or have to be a true or false value which they are because these evaluate to true or false

28:48

what or will do is it will look at the left hand side and right hand side it will say okay

28:52

if the left hand side is true or the right hand side is true this whole condition is true of

28:58

course if they're both false that's the only situation where this whole condition here is going to

29:03

be false so in three scenarios because there's four possible scenarios here it's going to be true

29:07

and the other scenario it will be false so if both of these are true obviously at least one of

29:11

the most true so that evaluates to true so let's actually just print result four and have a look at it

29:18

great we get true now what we can do is we can add another or if we want and we can say result three

29:24

now what this says is okay if result one or result two or result three is true this whole thing is

29:29

true which of course it is now let's see how we can combine other stuff into this we can actually

29:34

use this not keyword what this does is it takes whatever is on the right hand side of it so in

29:39

this case it's gonna be a true or false value and just flips it so in this case if if result two

29:45

is true which it is it's just automatically gonna become false so if I actually I can show you on a

29:50

more basic level if I just print a set of result four I print not true like that this will print

29:58

the value false which you can see down there and if I print not false then this will print true

30:05

now what I can do is do something like this I can say not false or true so let's have a look at

30:11

that and there we go we get false so it takes whatever this is which is true and then it just

30:15

reverses it and makes it with the not now those are two that we've talked about so or and not now

30:20

what about and so and essentially says okay is what's on the left hand side and the right hand side

30:26

true if both of it is true they are true the whole conditions are used true otherwise it is false

30:32

so only in one scenario out of the four possible scenarios here with two variables or two values

30:37

on the left and right hand side is this gonna be true so let's have a look at this and we get true

30:41

because obviously this is false then we not it and that turns to true now of course we can do

30:45

a combination of and and or now when we use these combination of different things right so not and

30:52

or we have to know which is gonna come before what so kind of in the order of operations now the way

30:57

it actually goes is not is first and is second and or is last so just keep that in mind because

31:03

that will be important based on the way that you write your expression so essentially if I say

31:06

false and true or true then what we're gonna actually do is evaluate the false and true first

31:12

and then we will evaluate the or of whatever this is and then that so in this case this will be false

31:18

so then we get false or true and then this would turn into true then we would have the not

31:24

and that would turn that into the false now we're moving on to the fun stuff which in my opinion is

31:29

the if else if and else statement if this essentially allows us to actually check if something

31:35

occurs or if a condition is true and if that is true we can do something specific otherwise we might

31:42

not do anything or we might do something differently so for the example I'm gonna do here I want

31:46

someone to type in their name and I want to say if their name is equal to in this case Tim I will

31:51

tell them they are good at Python I don't know something random so in this case to do that what I

31:56

will start doing is writing an if statement so if syntax and if statement is if condition

32:02

like that I know that's spelled wrong but that's fine condition colon indented block whatever I want

32:08

to happen in the indented block and then that is the basic syntax of the if statement so for

32:13

here if I want to check if x is equal to Tim I will say if x equal to and then the string Tim with

32:19

the capital T I will simply print you are great so now what will happen is if and only if x is

32:29

equal to Tim will I print you are great now in any situation so regardless of if the name is equal

32:35

to Tim or not I will always do this so I will print that statement hopefully that self explanatory

32:39

so let's go ahead and do Tim we can see you are great always do this now let's do another name let's

32:44

go with Joe and it says always do this it did not print this statement now the addition to the if

32:50

statement is of course I can add more stuff underneath this indented block so you do another print

32:55

statement and I would add a space so let me actually just show you how this works if I do Tim now

33:00

you see we get that space by just printing an empty print statement essentially okay now what we

33:06

can do those we can say okay how about I only want to print you are great or I want to print some

33:12

other message I don't want to print two things or what I can do is I can print I can add this

33:16

else this else must come after an if statement and essentially what allows me to do is say okay

33:22

if this isn't true do this so it's an if else right if true do this otherwise do this never

33:29

will I do both so in this case I'll just print no you know not very intuitive or it doesn't

33:34

really make my sense but that's fine so now I'll go ahead and I'll say name Tim says you are

33:40

great and let's actually just go with no and then it prints out no all right so that's the basics

33:47

on the if else now let's say we actually want to check multiple things so maybe we want to check

33:53

a bunch of different names and if the name is Joe maybe we want to say you know thanks Joe I don't

33:58

something random right well what we can do is use this elif statement the elif statement can be used

34:03

as many times as we want but it must come after an if and it must be before any elses so I can't

34:10

just go ahead and start writing these random elifs everywhere only is gonna come after an if statement

34:15

so there's only ever one if only ever one else but as many elifs as we want and you'll see

34:19

how this works in a second so the elif syntax is the exact same except it starts with the el so I'm

34:25

gonna say elif x equals equals Joe and I'm gonna print by Joe why not so now let's run this

34:34

let me just show you what happens when I go with Joe it says by Joe and let's just print some

34:39

random it gives us no now of course I don't need this el statement I can use an elif without the

34:44

el statement that's totally valid in this situation if I do something random just nothing will happen

34:49

but if I put Tim or Joe then it will give me the valid response now let's add another elif let's say

34:54

if elif x equals equals I don't know let's go with Sarah we can print random okay and now let's

35:02

try this out and that works now of course we could add the elis at the end but that is the basic

35:08

for the if elif and else now of course you can chain and add stuff inside of another if statement

35:12

I could put another if statement inside of this but just make sure you keep the indentation correct

35:17

because that how it that is how it determines what we're going to be printing and what we are doing

35:22

if this statement is true so in these next few sections I'm gonna be talking about collections how

35:27

we deal with collections looping over collections and all of that fun stuff but what is a collection

35:33

well collection is simply an unordered or ordered group of elements now the two collections I'm

35:38

gonna discuss here in this section is a list and tuples so if we have a list a list looks something

35:42

like this it is simply square brackets now inside of the square brackets we can have a series of

35:47

elements elements are simply some data type so I could have the integer four I could have the value

35:53

true I could have some string high and these elements do not need to be the same type like in

35:59

many other languages so this is a list and the way that the list works is that it can store

36:03

a bunch of different elements and a list is an ordered collection which means the order in which we

36:08

enter things into the collection matters and is maintained in fact it's very important in the list so

36:15

how do I actually access elements in the list how do I deal with the list what are some things

36:18

we can do with it well of course I can define a list by simply opening up some square brackets

36:23

and then defining some elements inside I also can just leave square brackets like this empty that

36:28

would give me an empty list the first thing to look at on the list is this function called

36:32

Len Len will tell us the length of the list this also works on strings and a few other things as

36:37

well but if I look at the Len of x that tells me three now just to kind of show you here if I say

36:42

y equals high and I were to look at the Len of y after I look at the Len of x we obviously

36:49

get two so Len is a function in Python that we can use to look at the length of stuff

36:54

all right so that is one of the things we can do on a list what we can also do is we can

36:57

append things to it what append does is add to the end of the list so if I want to add to the

37:02

element hello I can say x dot append now I can print out x and if we have a look we get four true

37:07

high hello of course we added this element to the end of the list I can also extend the list by

37:13

another list so let's say I have a list here and inside of this list I have four five and just a

37:19

bunch of other stuff well when I extend it what that will do is take all of the elements from

37:24

this list and simply append each of them to the end of the list so you can imagine extend

37:28

its internal implementation just loops are all this and appends all those elements to this list

37:34

now what about removing something or popping something off of the list well I kind of already

37:38

gave away what one of these things is but what pop does is remove and return the last element

37:44

in the list so if I actually go ahead and print x dot pop what this will do is it will print

37:51

and it will remove high from the list and you can see that when we print x again so we get high

37:56

and now our list has high removed because we popped it off of the end now another argument for pop

38:02

is actually the index of what we want to remove when we look at a list every single element in the

38:07

list is identified by a number that number is known as its index otherwise known as its position

38:12

in the list when we start looking at index is in the list we start counting at zero so the first

38:18

position in the list is zero the next is one and the last is two if we want to know the last index

38:23

in a list we look at the length of the list and subtract one in this case we have three elements

38:28

the last index would be to the first is zero so if I go ahead and say x dot pop zero what that's

38:33

going to do is remove the first element from the list which is four so now we remove that and

38:37

all of a sudden if we look at this new list the first element at position zero index zero is

38:41

going to be true now if we simply want to access elements of the list maybe don't want to remove

38:46

them we just want to look at them we can say x put the square brackets which means we're going to

38:50

access something and we can put the index of the element that we want to access in this case will

38:56

access true there we go we print true and that is the basics now what I can also do is say

39:02

let's do something like x zero equals and change its value to say hello now when I print x we'll get

39:08

hello true hi let's have a look and there we go the reason I can do this is because lists are

39:13

what's known as mutable this means this x right here does not actually store a copy to the list

39:20

it stores what's known as a reference to the list and the actual items itself are stored somewhere

39:25

else so for people that are more advanced and understand that I'm hoping if you guys have come from

39:29

another programming language that makes sense but lists are mutable which means they can be changed

39:34

I can change all of these elements and if I do something like y equals x I go ahead and I make

39:40

that change to x but I don't make it to why you're going to notice that x and y will be the exact

39:45

same so I have hello true hi hello true hi making a modification to x made a modification to y

39:51

because x and y are storing a reference to this list not a copy of this list if I wanted to copy

39:57

the list I would have to do this I would have to do two square brackets a colon inside we'll talk

40:01

about how this works later but now notice if I made this copy that change does not apply to

40:06

the other list there's a lot of other stuff that you can do with lists but those are the basics

40:10

and hopefully that makes sense next I'm gonna talk about tuples so tuples are similar to lists except

40:15

they are immutable so a tuple uses round brackets instead of square brackets and it works pretty

40:21

much the exact same except we cannot append we cannot remove and we cannot change elements so a

40:27

tuple is really an immutable list you can think of it like that it means it cannot be changed once

40:32

it has been defined we want to change it we actually have to redefine it so what I can do is I can

40:37

print out say x zero and that's fine that will give me the correct answer so that'll give me zero

40:41

but if I go ahead and do something like x zero equals five and I try to do that you're gonna notice

40:48

we get an error because the tuple object is not support item assignment reason for that is because

40:53

again it is immutable I also cannot do something like x dot append I can try but I'm gonna notice that

40:59

again we'll get another error tuple object has no attribute append so this is a tuple they're not

41:05

used a ton although they are used sometimes and just to note here you can have lists inside of

41:10

lists so I can say something like you know I could have a tuple inside of a list I could have a

41:15

list inside of a list inside of a list and I could have elements inside of this they don't need to

41:19

be uniform size sometimes in other programming languages when you're looking at arrays if you have

41:24

embedded lists every element has to be another list stuff like that just want to point out that

41:29

you can do something like that and I think that's all I'm gonna cover right now for list and

41:32

tons all right the next topic I have for you is for loops now for loop if you've learned another

41:38

programming language just allow us allow us to iterate a set number of times so we'll be talking

41:43

about a while loop the main difference between a while loop and a for loop is a while loop is running

41:48

and in defined amount of times we don't know we're going based off of a condition which we'll

41:52

see shortly a for loop we know how many times we're gonna loop at least relatively so what I'm gonna

41:57

gonna do is I'm actually gonna show you how we can just create a basic for loop to print the numbers

42:00

at from one to ten say for I in range and then inside of here we simply put ten I'll talk about

42:05

this range function doesn't a second but let me just show you how the for loop actually operates

42:09

so now I can show you that this prints zero one two three four five six seven eight nine does not

42:15

include ten so the way we set up a basic for loop if we're just gonna be iterating through

42:19

account is we say for I or for some variable this is gonna be our iterator or a counter variable

42:25

whatever you'd like to call it in this is a keyword we always have for I in and then some word here

42:31

in this case we're gonna put range range is a function that actually creates a collection of numbers

42:37

based on the input that we give it the input to range is the following start stop step so we're

42:46

allowed to put up to three and we can have less than three arguments inside of this range function

42:51

essentially start stop step is saying okay what number do we want to start at what number do we

42:55

stop at one number do we step at now if we only have one argument so we only pass one number

43:01

by default that is the stop argument we usually start at zero so by default our range will start at

43:07

zero and we will go to whatever number we put here most of the times you're just gonna put one

43:11

number and this defines the ending range so in this case it says okay we're gonna start at zero

43:16

and we're gonna go up to ten but not include ten notice we didn't print ten now if we had

43:21

two arguments that's gonna be default start stop so I will say okay we will start here at one

43:26

and we will stop at ten but not include ten if we had three we'll include step and that tells us

43:31

how much we should increment every single time by default we increment by one but we can't increment

43:36

by two and we can increment by negative number as well let's actually say that we want to start

43:41

at ten and we wanted to go to negative one and we want to step by negative one well let's have a

43:47

look at what this looks like and just test it out okay so running you see we start at ten right there

43:53

and then we went all the way down to zero now of course we could start at negative ten and once

43:58

we'll happen here we do nothing the reason for that is we start at negative ten we were going to

44:03

negative one and we're stepping by negative one but since we were already passed negative one we

44:08

aren't gonna go there obviously and we just immediately stopped so that is kind of how the range

44:13

function works so for i in range again start stop step by default if you just put one argument

44:18

that's gonna be the stop if you put two that's start stop you can mess around with that and see

44:23

how it works but I think that's enough of an explanation the next thing we can do is actually loop

44:26

through a list so let's say we have four i in and now instead of range we'll simply just put a

44:31

list we'll say four i in range and we can do three four and just a bunch of numbers here now if

44:36

we print i it will simply print all of the numbers in that collection or in that list it will step

44:40

through one at a time and print all of them now let's say that maybe we didn't want to do that

44:45

maybe we actually wanted to keep track of what index we were at we could say x equals the list we

44:50

could say four i in x and then we'll sorry that actually would not be a great way to do it we can

44:55

say four i in range the length of x and then instead of just printing i we could print x square

45:01

brackets i let's have a look at this here you see how that works the way this works is we're

45:05

gonna go up to but not including the last index of x or the length of x sorry so in this case

45:10

one two three four five six so this right here we'll say six so four i in range six that means

45:16

the maximum value of i is going to be five so we say x zero x one x two three four five and print

45:23

all of the values out now there is another fancy thing that we can do if we want to avoid

45:27

using that it's called enumerate what enumerate will do is create indexes and values for all of

45:33

the elements in our list we can say four i comma element in enumerate x and then what I can do is

45:40

comma element and you'll see how this works what we do is you get zero one two three four five and

45:44

then all of the elements so this works kind of like a combination of the two four loop styles I

45:49

just showed you what we actually have a range so we go from zero up to five those are all the indexes

45:54

in the list and then we print all of the elements for each of those indexes in the list and we just

46:00

have two variables here that denote that so the next topic to discuss is while loops now while

46:05

loops are pretty straightforward essentially what they are is while condition equals equals true

46:11

go ahead and do something so we're gonna have some condition here before this colon and then after

46:16

the colon there's gonna be an indented block and whatever's in that indented block will run inside

46:20

of the loop while that condition is true to illustrate this I will say i equals zero so while i

46:26

is less than ten we'll say print run and then we'll say i plus equals one this is another way

46:35

that you can add one two variables so you could say i equals i plus one or you can do i plus equals

46:40

one you can also do something like i multiplied equal by one or divided equal by one so

46:46

multiplied by equal two what that's gonna do is say okay multiply i by two we can do something like

46:51

that i if we want it as well just showing you some more syntax for some interesting things that

46:55

we can do in python but as the basics of a while loop if I go ahead and run this we can see that

47:00

this is gonna run ten times and there isn't really much more to talk about for the while

47:05

if other than the fact that we can implement a break statement so we could also write this same code

47:09

like this while true i plus equals one and then if i equals equals ten break and i will simply

47:17

break out of whatever the closest loop is to us imagine we had another while loop like this and we

47:22

said while true inside of here then this would break this while loop not the other while loop

47:30

hopefully that's making sense anyways let's move on to the slice operator so the slice operator

47:36

is personally my favorite part of python there is implementations in other languages but none of them

47:41

are as nice as python in my opinion and what this allows us to do essentially is to take a slice

47:46

of a collection like a string or a list or a tuple i and do something with it so let me just

47:52

show you what i mean the slice operator is simply the square brackets like this with a sequence of

47:58

coins and numbers within it way that it works is we have a start a stop and a step just like

48:04

the range function that we looked at before so if i put something like x here and then i go start

48:09

stop step what i can do is say okay i want to start at zero i want to stop at index these are all

48:15

indexes that we're putting in here three and i want to step by two now actually let me just stop at

48:20

index four just so we get some more values in here but let me show you what this looks like we

48:24

get the value zero two when i have zero four two into my slice what this is saying again is start

48:29

at the first index which is zero i go to the fourth index but do not include it which means stop

48:35

as soon as we hit this and step by two so we're gonna start at zero then we're gonna go to two

48:39

and then we're gonna go to four but since four is the stop here we're not gonna include that in our

48:43

slice now when we do the slice operator we don't have to include all of these different things as

48:48

arguments in fact what we can do is just include a stop just include a start and all of that so

48:53

just like the range function if we don't include anything so we just sorry we just include a

48:58

colon like that and then we put something on the right hand side that is saying stop at whatever

49:03

index we put here so if we do this it says start at the beginning and stop at the end now if we

49:08

do something like two colon that says start at two and stop at the end so whenever we leave a blank

49:14

we're assuming it's either the end or the beginning or one in the case of a step so if i did

49:18

two colon colon maybe i did two colon four colon this is saying okay start at two stop at four and

49:25

then step by one but i don't even need to include that i could just do two colon four but now

49:30

let's say i wanted to go four and then colon two colon negative one what this is saying is okay start

49:36

at four go to two and step by negative one so now what this is gonna do is start at four which is

49:40

actually this and it's gonna give us four and then three and then it's not gonna include two

49:45

or any of the other one so that's what we'll get in fact let's look at this we see we get four three

49:50

now one of my favorite things with a slice operators to reverse a list all we have to do is

49:54

colon colon negative one this means start at the beginning stop at the end and then step by negative

49:58

one so this will actually start stepping backwards which is kind of interesting so when we look at

50:02

that we get eight seven six five four three two one zero that is an easy way to reverse a list in

50:07

Python now of course all this works on a string as well let me show you that with s you see we can

50:12

reverse the string like that and we can also just do the standard arguments so we would typically

50:17

do let maybe we'll just step by two so start at the beginning stop at the end and step by two

50:23

we get hlo so as the basics of the slice operator there's a bit more stuff that we can do with

50:28

this but that is kind of how it works and i believe this works on tuples as well we can just try it

50:34

out to make sure that i'm not lying to you guys but if i do that you can see that that does indeed

50:39

work and we can use the slice on kind of any collection of elements all right now we are moving on

50:44

to sets which are an underrated but extremely useful data type in python now set is simply an

50:50

unordered unique collection of elements what that means is there is no duplicate elements we do not

50:55

keep track of order or frequency of elements all we care about is if something is there or if

50:59

something is not there the reason we do this is because a set is extremely fast to do what's

51:04

called lookups removals or additions when we're talking about a list let's say we have something

51:09

that's you know has a bunch of elements in it if we want to say remove the first element from the

51:13

list that actually involve shifting the position of all of the other elements because we're

51:16

keeping track of the positions of these elements with the set since there is no notion of position

51:21

adding and removing something is as simple as just adding or removing that element there's no

51:25

other operations that need to be done which means it's very fast to use so think about a set to be

51:30

used only in a situation where you care if something exists or doesn't exist not about the

51:34

frequency or order in which that item exists so to create a set is pretty straightforward we can

51:39

say x equals set and we use oh the open bracket and close bracket now the reason we don't actually

51:45

write something like say s equals and then this which is really the representation for a set

51:50

is because what this will do is create a dictionary now technically if we want to create a set we

51:55

can do something like this and this will create a set for us that initially has the elements

51:59

for 32 and two inside of it this is what's known as a set literal if you're not going to be

52:04

creating an empty set it's totally fine to do it like this so long as you add an element inside

52:08

but if you are creating an empty set then you need to use this constructor otherwise you're

52:12

going to end up creating a dictionary which is not what you want to prove the simple point you

52:16

have print out the type of just an empty you know set representation and you'll see that it

52:21

gives me dict like that of course that's not what we want anyways let's have a look at this set

52:25

and I'll just show you what I mean by unique unordered collection of elements so 432 to 2

52:30

if we print that out we get 32 to 4 so obviously a different order than how we added in there

52:35

and notice that the duplicate twos were removed now the set operations are pretty straightforward we

52:39

can add something to a set with s dot add our set dot add I'll put five and inside of here and

52:46

let's just keep printing out s there we go we can see we added in there we can also do s dot remove

52:53

and that will well since there's nothing in there give us a key error now if there is the element in

52:58

there so we can do it like this it will remove that element for us and then there's a few other

53:02

things that we can do as well for a set one of the good ones is actually checking if something is

53:05

in a set what I can do is say for in s what this will do is simply check if the element for

53:12

is in the set and give me a true or false in this case it is true and remember this happens and

53:16

what's known as constant time which means it's just extremely fast to do this as opposed to a list

53:21

so we can say thirty three in s that's gonna give me false now the uh parallel I want to make

53:26

here is if I had s equals to four thirty two two two like that sorry actually let's go s two

53:34

and I said print and let's just go two in s two this operation the one that's actually in the set

53:42

is going to happen much much much much much much faster than this one of course this isn't a big

53:47

deal right now because these collections are quite small but as these get uh extremely large

53:51

just understand that using a set is very beneficial when it comes to looking things up checking

53:56

if something's in there removing and deleting there's a few other operations we can do on sets

54:00

let me just define s two and I'll just quickly show you a few of them uh we can do for example

54:05

the intersection or the union of these sets so I can say s dot union s two and we'll go ahead

54:10

and have a look here and we can see we get the union of these two sets essentially adding them

54:13

together we can do the difference of the two sets if we want we can do the intersection and we

54:19

can do the symmetric difference and all that other fun stuff that you can do with sets now I'm not

54:24

gonna get into all that stuff just know that these are sets and that's some of the stuff that

54:27

you can do with them so now I'm gonna talk about dictionaries now if you're from another

54:32

language what may be familiar to you here is a hash table or a map something like that they're

54:36

very similar to dictionaries but a dictionary is essentially a key value pair you have some key

54:41

you have colon and then you have some value that it corresponds to now of course that value has

54:45

to actually be a valid data type but it could be something like a list it could be a single value

54:50

like that and the way that you access and create new key value pairs is something like this I can print

54:56

out say x and then the key inside of here in this case it's actually called key and what that will

55:02

give me if you look down here is simply the value for because well that's what corresponds uh

55:06

to this key so you have keys and you have values now to add a new key is pretty straightforward

55:11

what you can do is you can say x inside of square brackets you can define whatever key you like

55:16

it can already exist if you want we can say something like key two and we can make that equal to five

55:21

now note that these keys do not need to be the same uh sorry same data type so I don't have to

55:26

string keys I can have integer keys and whatever keys I want new something like x to equals eight

55:33

and there we go you see that works perfectly fine in fact I can make this a list

55:37

just to prove to you that the values don't have to be the same either and there you go now just

55:42

like a set this actually works using a hash which means this is very fast to retrieve add and modify

55:48

values within the dictionary in fact it's so fast you can kind of assume that it's in constant time

55:53

although there is a chance there will be something known as hash collisions although I'm not

55:57

gonna talk about that because that's probably a little bit more advanced than what most of you know

56:01

anyways what we can do here in terms of operations on the dictionaries we can check if something

56:05

is in the dictionary so I can say okay is some key in the dictionary key in x there we go print that out

56:11

and that says true a few other useful things that we can do is we can get all of the values from

56:16

the dictionary so we can say x dot values print that out and it says dict values and it gives us a

56:21

list of four usually when you do this you're gonna want to actually create a list out of this

56:26

because that is a different data type that's returning to us and there you go we get the value

56:30

for now similarly we can do dick dot keys so x dot keys and there we go we get a list that has

56:37

all of the keys in terms of iterating over or deleting values I'll first show you how you delete

56:42

something to delete something we can say x and then the key that we want to remove in this case

56:47

it will remove this key so let's go ahead and do that and well I guess it didn't really show you

56:52

that it was deleted let's just print it out to prove that there you go and we get an empty dictionary

56:57

of course like I showed you before we can use that in operator and to loop over it there's actually

57:01

a few interesting ways that we can do this so I can say for a key value in x dot items

57:09

and then what this will do is simply return to me all the keys and values so I can print key value

57:14

and you'll see we'll get a key for so if I want to loop through all the keys and all the values

57:20

I can use dot items if I don't want to do that I can just say for key and then I can say in x

57:25

what this will do is simply just give me all of the keys in x and if I want to access the value

57:29

of course I can say x key like that and that will give me the value and we get the same thing

57:33

popping up there that's kind of the basics for dictionaries there's a few other methods as well

57:37

but I'm not going to talk about those that is a dictionary and this next one is a fun one this is

57:43

comprehension now as far as I know Python is one of the only programming languages that actually

57:48

has this a lot of people absolutely hate it but I personally love it and if you learn to love it

57:53

you can use it quite well now to do a comprehension essentially what that is is a one the line

57:58

initialization of a list of a tuple of a dictionary of many other things so let me just show you what

58:03

I mean I can do something like x equals a list and then I can say x for x in range 5 now if I go ahead

58:10

and I print the value of x you're going to see what we get here let's run it like that you

58:17

get zero one two three four the way this works is essentially we can actually define a for loop

58:21

inside of this list and what it will do is say okay we're going to loop through as if we normally

58:26

would and whatever we have on the left hand side here is actually the element that we're going to

58:30

add into the list so I could do something like x plus five for x in range five and there we go we

58:36

get five six seven eight nine ten I can also do something like you know x mod two and or x mod five

58:43

there we go I could also you know grab something else I could say zero for x in range five that

58:48

would give me a list of all zeros you can see how this might actually be useful now if we want to

58:52

go into a more complex example I could do a list and then inside this list I could say zero for x

58:59

in range 100 for x in range five now what that's going to give us is five of these lists that

59:08

each have 100 zeros inside of them you can see that's quite large but that is kind of how

59:13

that works now we can also go ahead and add some more complex stuff to this too we can say zero four

59:19

or I can say I for I in range 100 if I mod five equals zero so essentially if I is divisible

59:31

by five then what we can do is actually add it into the list let's have a look at that and there

59:35

we go we get all the increments of five so you can play around with that and see how that works

59:39

but this also works for dictionaries in fact let's just do the same thing here let's make it

59:43

it a dictionary and now we'll just go I colon zero so we have all these keys that are initialized

59:48

at zero we can go there and have a look we can see that dictionary works and of course we could

59:53

do the same thing for a set we can just say I for I in range blah blah blah and there we go now

59:57

we get a set so we can actually do this for tuples as well except we're not going to do it in the

01:00:01

way that you might assume if you just go ahead and do this this actually returns to us what's

01:00:05

known as a generator object it's a bit more advanced for this kind of video so I'm not going to talk

01:00:10

about that but what we can do if we want to make this work for tuple is we actually type tuple we can

01:00:14

use the tuple constructor which is simply this and then do the comprehension inside of there

01:00:18

now that will return to us the tuple you can see I tried to do it the other way before and that

01:00:21

returned to me the generator object anyways that is comprehension these can be used in a

01:00:26

whack of different things they're definitely interesting and good to know and one of the best

01:00:29

features of Python that is not in many other languages so now I'm going to start talking about

01:00:34

functions so to define a function and Python you simply use the def keyword this means define

01:00:38

then you pick your function name in this case I'm going to pick funk the function names of the

01:00:41

same as variable names in terms of how you name them you can put some positional parameters inside

01:00:46

of here if you want or you can leave it empty you add a colon indented block then anything inside

01:00:50

the indent will run when you run the function so let's go ahead and actually just run this

01:00:54

and show you the basics of this so that is pretty much how you run and define a function of

01:00:59

course you can define multiple functions you can actually define functions inside of functions if

01:01:03

you wanted to this is totally valid if I wanted to do something like this and then I could call

01:01:10

funk like that now of course this is a more advanced use case but just showing that you can do

01:01:14

that because of the way that python works in fact functions are actually objects which means you

01:01:18

can technically return them I'll show that in a second but let me talk about arguments first so

01:01:22

let's say I want to add the argument x y inside of here of course that means now when I call the

01:01:27

function I need to add some value for x and for y so let's go five six and now if I after run

01:01:33

want to print those I can print x and I can print y let's have a quick look we get run five six

01:01:38

of course we can return something from function as well maybe I'll return x multiplied by y now if we

01:01:43

actually go ahead and print the value of this function what will happen is it will evaluate what is

01:01:48

here return that to us and print that out we get the value 30 now note that when you return multiple

01:01:53

things from a function which you can do I can return x multiply by y and maybe x divided by y as

01:01:59

well this will return the value to us in a tuple so if you look here we get actually two values

01:02:04

we have I guess the x y value here and that's inside of a tuple now if we want to unpack this

01:02:09

tuple and say separate this out into different variables we could of course index at zero and index

01:02:15

at one but a much cleaner and faster way to do this is to do something like r1 comma r2 equals

01:02:21

funk five six what this is gonna do is take these two values here and simply separate them into

01:02:25

the r1 and r2 variable now if I go ahead and I print r1 r2 and I have a look here you can see that

01:02:31

we get 30 and then zero point eight three three three four that is the basics for a function if you

01:02:36

want to do a optional parameter you can do something like z equals you can set a default value I

01:02:41

usually set it as none and what this means is that you do not have to pass that in but if you

01:02:46

want to you may and it will override the optional value that you have here so let's just start by

01:02:50

actually printing out z I'm not gonna call it with z to start you can see that oops let's run that

01:02:57

for a second we get run five six none and now if I add a z so let's just add seven there we see

01:03:03

then seven none we are going to get seven printing out right there that is the basics of a function

01:03:08

and now we're gonna move on to some other more advanced aspects of it all right so I'm gonna talk

01:03:13

about something that's really cool called the unpack operator and then I'm gonna talk about star

01:03:17

arcs and star star quarks but I first want to show you an advanced example of a function

01:03:21

so if I say define funk like this and then maybe I'll say take x and I define funk two inside of

01:03:26

here this is totally valid by the way you can put a function inside of another function and then

01:03:30

inside of here what I'm actually gonna do say this function is going to print x and I'm going to

01:03:34

return funk two but notice I don't call it now this is just to prove to you that functions are

01:03:39

objects hence they can be passed around just like variables so what I can actually do here is I can

01:03:43

go ahead and I can say print or sorry not print but I can call funk four or funk three and then I

01:03:49

can actually call it like this what this is doing essentially saying okay I'm gonna call this function

01:03:53

here with the value three what this function does is it returns to me another function so it gives

01:03:59

me funk two notice it's not calling the function it's simply returning it now I'll actually just

01:04:03

show you what I mean first before confusing anyone let's print this out before I do the call

01:04:07

statement on it but you can see that this actually tells me this is a function object it's a funk

01:04:11

dot locals funk two so it's pretty much saying that funk two is defined within funk now if I

01:04:16

actually call this because that's just the function reference or object is not actually you know

01:04:21

being evaluated if I call it like that we can go ahead and we get three and then we get none of

01:04:26

course the print statement's gonna print none because there is no there's nothing to print from the

01:04:31

return of funk two but just have a look here we get three now this is equivalent if I did something

01:04:36

like this I said you know x equals funk and then on the next line I just called x since x is equal

01:04:42

to a function funk two I can call it just like I showed you before and there we go we get three

01:04:47

anyways that's the advanced aspect I want to show now what I want to do is show you about star

01:04:50

arcs and star star quarks so I'm gonna put in like this all right so before I define what this does

01:04:56

what I'm gonna do is just show you what the unpack operators and Python so let's say have a list

01:05:00

maybe we'll call it x and we just have a bunch of random numbers in this list so what the unpack

01:05:08

operator does is actually separate all of the elements from a list or from a collection into individual

01:05:13

elements the best example of this is the print statement if I print asterisk x which means unpack

01:05:17

x what this is gonna do is take all of these elements out so separate them by individual elements

01:05:22

and pass them into the print statement as arguments so instead of just printing you know say the

01:05:27

entire list what it's actually gonna look like is print one comma two three comma two three six

01:05:33

both while you get the point that is what this is gonna do so just have a look at what happens when

01:05:37

I print that I get all of these things separated by spaces like what happened if I just pass them

01:05:41

an individually not being in the list now look what happens when I just print x let's have a look

01:05:47

see it actually prints the list out so what this does is unpack whatever we have in a

01:05:52

tuple or a list or some kind of collection and sends it through as arguments to a function so that's

01:05:58

where star arcs and star star quarks come in although you'll see that in one second so let's say I

01:06:02

have you know x y that we need inside of function we got to pass x y what this will do is simply print

01:06:10

x and then print y now let's say I actually have maybe a bunch of pairs that I want to pass

01:06:15

this function so maybe I say pairs equals and then I have like one two three four what I'm gonna

01:06:22

do is do a four looping say four pair in pairs and what I want to do is call this function with

01:06:26

those pairs now then I avoid to do this would be to go funk okay pair zero and then pair one great

01:06:34

that would work but that is not to be Pythonic way to do this what we'd actually do is do

01:06:38

asterisk pair and what this will do is take one two and take three four unpack them separate them

01:06:43

and pass them as arguments to funk so if you have a look at that that actually works and that is

01:06:47

why the unpack operators really useful you can also use this actually on dictionaries as well

01:06:52

now I'll show you how it works with dictionaries a little bit more complicated involves two asterisks

01:06:56

what this does is if I have keys I can say x representing obviously the argument x and then

01:07:01

make that equal to two and then I can say why and this could represent the argument y and I'll

01:07:05

make that equal to five now if I run this this works fine I needed two asterisks because this was

01:07:10

dictionary and that is kind of how this works yeah I don't I guess really need the four loop anymore

01:07:16

to illustrate this but the double asterisks is used for dictionaries the single asterisks is used

01:07:21

for a tuple or for a list now I just want to make the point here that let's say these are not

01:07:25

in the correct order so it's why and then x this will still work and that's actually why this is

01:07:30

really useful because you don't have to have it in the correct order so long as you name the

01:07:34

arguments as the keys so now hopefully that will explain to us how star arcs and star quarks work

01:07:40

so star arcs and star star quarks essentially imagine you have a function and you don't know how

01:07:45

many arguments positional or keyword arguments you want to accept this what quarks stands for keyword

01:07:49

arguments well what you can do is you can use star arcs and star star quarks and what that will do is

01:07:54

allow you to pass in an unlimited amount of regular arguments and keyword arguments so if I

01:08:00

actually go ahead and go funk and then I pass like one two three four five and maybe I'll pass

01:08:05

some keyword arguments like you know one equal zero two equals one I know this doesn't really make

01:08:11

any sense but you get the idea now if I actually run this imprinted out you can see that when I

01:08:15

print arcs and print quarks we're getting a tuple that has all of the positional arguments which

01:08:20

are these and all of the keyword arguments which are one and two now if I wanted to actually use

01:08:26

any of these what I can do is I can unpack them so first of all I can unpack arcs and what this does

01:08:31

if we have a look here is it will print out one two three four five I could try to unpack quarks but

01:08:37

you're gonna see what happens when I do this and hopefully if you remember the last example you'll

01:08:40

realize why this is not gonna work it says one is an invalid keyword argument for print so what

01:08:45

this is gonna do when I unpack quarks is it's gonna take all my keyword arguments and it's gonna

01:08:49

say one equals zero and it's gonna say two equals one and pass that to the print statement now

01:08:55

obviously one and two well those are not valid arguments for the print statement so we're gonna get

01:08:59

an error anyways that is arcs and that is quarks you're gonna have to look into this on your own

01:09:04

and kind of learn about how it works but that is the basics and I just wanted to show that to you

01:09:08

so that you're aware that you can do these cool fancy things in Python next one is gonna be very

01:09:12

fast but this is just defining scope and global so when we look at a function like this there's a

01:09:18

notion of a scope so we have x equals Tim we have defined funk and then we have name as a parameter

01:09:23

and what we're doing is we're saying okay we're gonna change x to be equal to name so essentially

01:09:27

if I was naive and I didn't know any better I would assume that that what's gonna happen as well

01:09:30

this variable up here would change when I pass this name to this function any of you that program

01:09:35

before probably know that's not gonna work but if you have a look here you can see that x does

01:09:38

not change before or after when I call funk with the string changed the reason for that is that

01:09:43

this x variable is local it is within the scope of this function which means it cannot be used

01:09:49

access change from with from outside the same thing here with this x this one is actually global

01:09:54

this is on you know not not defined within any scope other than this file which means well I can't

01:10:00

just directly change it from this function I could however access it from this function I could

01:10:04

print it here but I can't actually change its value because if I do that what's gonna happen is

01:10:08

I'm gonna create a new x inside of here which is equal to name and it will be local to this

01:10:12

function it will not modify this however there is one interesting thing we can do in Python which

01:10:16

is the global keyword if I global x now you're gonna notice that the expected behavior will occur

01:10:22

now we get him and we get change what that is saying is okay I want to use x as a global variable so

01:10:27

I want to reference x in the global scope never use this this is never good to use and if I ever see

01:10:33

a Python program that has it I always get upset but this is something that you can use and I feel

01:10:37

obligated to show you because there is some very very rare situations where you may need to use that

01:10:42

all right so now I'm gonna spend about 10 seconds showing you how to actually raise an exception in

01:10:46

Python there's a keyword called raise maybe in Java you've seen something like throw and raise can

01:10:51

raise an exception if you want to do that so I could raise exception and then inside of here I could

01:10:55

just say bad you know whatever I want now if I run the program as soon as we hit that line immediately

01:11:00

I'm gonna get an exception and it says raised exception bad exception bad of course that is the

01:11:05

basics there is more arguments and things you can do with this this is a base class which means you

01:11:09

can extend it when you get to object or into programming make your own exceptions and make all that

01:11:13

fancy stuff for our cases that's all we really need to know I could raise like a file exists error

01:11:19

any other kind of error that I want and then again add some description for it by just putting a

01:11:23

string inside of the brackets that's enough for that now let's show how we can actually handle

01:11:27

exceptions all right so now that we know how to raise exceptions it makes sense to learn how to

01:11:32

handle them so in Python rather than having like a tried catch we actually have a try except

01:11:37

finally block so what I can do is do something like try that means I'm going to try to perform

01:11:41

whatever code is inside of here maybe I'm gonna get an integer division by zero error so I can use

01:11:45

you know seven over zero that's gonna raise an error then I can create an except lock I can accept

01:11:50

an exception as e what this is gonna do is mean whatever the exception is will be stored in the

01:11:55

variable e and then second print out that exception and we can continue running the program this is the

01:11:59

basic try except block you don't actually need to define something here I can just say try except

01:12:03

like that if I want and also accept a specific exception so I don't have to just put exception

01:12:08

this is general this means any exception that occurs I will actually catch you can only catch

01:12:13

specific exceptions if you want but again that's up to you so in this case you see we get division

01:12:17

by zero it just printing out but it didn't actually raise or you know show us that error so did

01:12:21

raise it but we didn't actually see it now if I go ahead and just do this what we can do is we can

01:12:27

say oh well that is the actual problem and now our program is gonna crash and not work but if we

01:12:32

we have this except block like this of course we're good to go now there also is a notion of a

01:12:37

finally block the finally block will run it no matter what usually you put cleanup type operation

01:12:42

inside of here maybe you're trying to write to a file some exception occurs and the no matter if

01:12:46

this was successful or not you want to close the file after you would put that inside of finally so I

01:12:50

could say print finally like that have a look here and you can see we get finally I'm not gonna

01:12:56

go through this too much I assume if you're gonna be using this you understand how try except

01:13:00

and finally work but that is the implementation in python and that is the basics we're raising and

01:13:05

catching an end and now we're gonna discuss the lambda so lambda is actually a really cool aspect of

01:13:10

python I'm pretty sure they have them in many other programming languages as well but it looks

01:13:13

something like this essentially a lambda is a one line anonymous function what that means is not

01:13:20

really a named function you don't define a lambda using the def keyword what you can do is something

01:13:24

like x equals lambda x and then x plus five what this means is this lambda is gonna take one

01:13:31

argument which is x and then it's going to return x plus five not multiplied by x didn't mean to do

01:13:36

that that means if I call x like this and I don't know x2 and we print this let's just go ahead

01:13:42

and have a look at this here oops again did not mean to do that we see that we get the value seven

01:13:49

now I will note that this is not the advised way to use a lambda we're gonna see how to use it in

01:13:53

just a second in the next section but this is the basics of how it works it's just a one line

01:13:58

anonymous function so I can do something like xy as well and then I can do something like x plus

01:14:02

y not capital y and if I go ahead and call x with I don't know two three or two 32 sure that's

01:14:09

fine several look here we get to the value 34 so that makes sense on how the lambda works it looks

01:14:14

intimidating but it's really not that complicated and in the next one you'll see why these are

01:14:19

and now I'm going to talk to you about map and filter which are two useful functions and Python

01:14:23

which make use of the lambda functions now you don't necessarily need to use lambda function but

01:14:28

a lot of times it comes in handy so let me show you what I mean let's make a list let's add

01:14:33

oops I wanted to do a few more commas than that let's add a bunch of elements inside of here to

01:14:38

make it kind of nice and long so we get some interesting output and now what I'm going to do

01:14:42

is I'm simply going to show you how we use map so what map we'll do is we'll take all of the elements

01:14:46

of a list and use a function to map them into a new list so if I say something like MP standing

01:14:51

for you know map I'd say map which is the actual function now what I do is I put a function inside

01:14:57

of here that I want to use for the map so this will make sense in a second let me say lambda x

01:15:03

x plus two so this essentially saying add two to every single one of these elements or add two

01:15:07

to x in this case and then what I do is simply pass x now I know x is kind of confusing so maybe

01:15:12

we will just change this to i so that's easier to differentiate but what this is going to do is say

01:15:17

okay we're going to map all of the elements inside of x so in this list x to this lambda function

01:15:23

which means take this lambda function and apply it on every single element of x and then put that

01:15:27

into a new list now what I'm going to do is I'm going to print the list representation of MP

01:15:32

map actually returns to us a map object we can use that if we're going to iterate over it but it's

01:15:37

usually more useful just to convert it right into a list so let's have a look at what this does

01:15:41

and there we go now we get three four six and you can see all these elements have been added by two

01:15:46

now we knew more complicated one maybe we can multiply by two it'll seem more of a difference

01:15:50

and there we go now the next function I want to show you is filter so hopefully you have the idea

01:15:54

of how map works but filter I don't want to say the opposite but it does something a little bit

01:15:59

different so this function here which is a lambda what it's going to do is instead of actually

01:16:02

returning some value it's going to return true or false and it's going to tell us whether or not

01:16:08

we should include the item in our final filter list or filtered object so essentially this lambda

01:16:14

function or whatever function we put inside of here has to return true or false based on the value

01:16:18

of an item so what I could do is say something like I it I mod 2 equals equals zero so this means

01:16:24

you know only return it if it's even so this is the function take some value I tells us if it's

01:16:29

equal to or sorry divisible by zero and if it is we'll include it so let's have a look now

01:16:33

and you can see that we only get all of the even elements in our list now of course what we can do

01:16:38

is also define a more complicated function so we can say define funk take some value let's go with i

01:16:43

and we can do the same thing here we can say return i mod 2 equals equals zero but we can also say

01:16:48

you know i equals i multiplied by three so we can do that and then we can say i mod 2 equals

01:16:55

equals zero then instead of using a lambda here we could simply just write funk that's totally

01:17:00

fine so we just put the name of the function that we want to use and then we run this and it

01:17:03

works fine but this is why lambda's useful because a lot of times you don't want to define your

01:17:07

own function up here you just want to define it right inside of the map statement or the filter

01:17:11

statement so that's kind of where you would use this there's another few use cases for it but

01:17:16

this is kind of the most common and where you'll often see it so anyways that is map and and for

01:17:21

our last and final topic I'm gonna be covering something called f strings now f strings are new

01:17:27

in python 3.6 if you have any version lower than that you are not going to be able to use f strings

01:17:32

but they're a really cool way to actually just manipulate and create strings so what i can do is

01:17:36

do something like x equals i can do an lowercase f or a capital f it doesn't matter and then a string

01:17:42

notice that this will highlight or maybe it'll be italicized depending on what editor you're using

01:17:46

and well what you can do is type a string as you normally would except now if you want to embed

01:17:50

an expression what you can do is do that inside of curly brackets so i can do hello and then i could

01:17:55

say like you know six plus eight and this will actually be evaluated and now when i print this

01:18:00

out it will give me the answer 14 so this is great because if you have some variables say like i have

01:18:05

you know i don't know tim equals 89 or something and i want to embed that in the string without

01:18:09

having to concatenate it and convert it into a string with str i can simply just use the f string

01:18:14

and just embed it with all the formatting by putting it inside of these curly braces so that is

01:18:19

kind of how that works you can go ahead and mess around with this if you want to really see how

01:18:23

it works but essentially you just put an expression inside of these curly braces it will be

01:18:26

evaluated and then it will turn into a string really great easy way to work with strings of course

01:18:31

you can print an f string as well i could print hello and then we can do that and in fact let's

01:18:37

run this and just see what we get and there we go we see we get hello 89 so that is f strings

01:18:43

so that being said i'm going to conclude the video here i am officially out of breath i went

01:18:48

very fast do this tutorial with the purpose of making it as quick as possible and not wasting

01:18:53

any of your time if you guys appreciate this effort please do leave a like subscribe to the channel

01:18:58

and of course let me know what you want to see next i will quickly mention that i did miss a few

01:19:02

things in this video uh specifically object oriented programming in advanced language features

01:19:07

if you would like to know about those two things i have a ton of resources related to them i will

01:19:11

leave them in the description the first is a about an hour long video on object oriented programming

01:19:16

and python that covers kind of all the fundamentals and everything you need to know then the next one

01:19:20

is a whole series on expert level features in python i believe that's about six videos and cover

01:19:25

things like meta classes context managers generators all of that anyways as i said i hope you enjoy

01:19:30

if you like subscribe i will see you in the next one

00:00

Introduction to Python

00:41

Downloading Python

03:04

Core Data Types Overview

04:57

Understanding Print Function

07:33

Working with Variables

07:57

Creating a Virtual Environment

10:21

Getting User Input

11:15

Using IDLE for Python Development

12:39

Using Arithmetic Operators

14:21

Introduction to Visual Studio Code

14:54

Understanding Order of Operations

17:21

Extending Python Functionality with Libraries

20:30

Basic Code Execution in Python

22:15

Navigating the IDLE Environment

23:54

Writing Basic Python Scripts

27:12

Executing Python Scripts from IDLE

30:30

Understanding Errors and Debugging

33:48

Exploring Visual Studio Code Extensions

36:00

Creating a Python Project

39:19

Managing Python Packages

40:03

Introduction to Lists

42:37

Using Git for Version Control

45:55

Debugging with Visual Studio Code

49:13

Best Practices in Python Development

01:49

What is a general purpose programming language?

00:54

How do you add Python to the system path?

04:41

What are the four core data types in Python?

04:57

How to use the print function effectively?

07:57

What are the benefits of using a virtual environment in Python?

11:15

How does IDLE simplify your Python coding experience?

14:21

Why is Visual Studio Code popular among developers?

17:21

How can libraries enhance your Python projects?

22:15

How can you enhance your coding experience with IDLE's navigation features?

23:54

What steps do you need to follow to write a basic Python script?

27:12

How do you execute a Python script within IDLE and what should you watch out for?

36:00

What are the essential steps to create a Python project?

39:19

How do you manage Python packages effectively in your projects?

42:37

What role does Git play in managing your Python codebase?

11:19

How can user input be stored in a Python variable?

13:23

What are the basic arithmetic operators in Python?

16:11

How does Python handle order of operations?


ArithmeticTupleList (abstract data type)Function (computer programming)AlgorithmPython (programming language)Data structureException handlingProgram optimizationData validationSet theoryData typeDynamic arrayDictionary

Description

In this tutorial, we will learn how to download and install Python, create an environment to write code, and use a text editor to write and run programs. We will start by explaining how to download the most recent version of Python from the official website and how to check the box that adds Python to the system path for easy future use. Then, we will move on to explain the different environments provided by Python, including idly, which is an interactive environment where you can write and run code, and Visual Studio Code, a popular text editor for writing and running programs. Finally, we will provide tips on how to make your life easier when working with Python.