- 7 weeks ago
Python Full Course in 1 Hour
Category
๐
LearningTranscript
00:00In this video, I'm going to teach you as much Python as I can in exactly one hour.
00:04If you would like a quick introduction, or you're transitioning from another programming language, this video is for you.
00:10I don't like boring introductions, so I say we just jump right in.
00:14There's two things we'll need to download.
00:16The first is a Python interpreter to convert our written code to machine code.
00:21We're going to head to python.org, go to downloads, and download the latest version.
00:30We will open this executable.
00:33If you're on Windows, you'll want to check this checkbox, add python exe to path, and we will install now.
00:46The setup was successful, and that's all you need to do to download the Python interpreter.
00:52The second download we'll need is an IDE, an integrated development environment.
00:58Basically a place where we can write code.
01:01For IDEs, there's two popular choices when writing Python code.
01:05PyCharm, and VS Code.
01:07If you already use VS Code, you can stick with that.
01:11Just be sure to download the Python extension.
01:13I find PyCharm more beginner-friendly, if you've never coded before.
01:17If you would like to use PyCharm, go to jetbrains.com slash PyCharm, and we will click this green download button.
01:27There's two versions of PyCharm, the professional version and the community version.
01:33The professional version is paid for, I would not recommend using it, only because there's plenty of free IDEs on the market.
01:40We'll use the community edition, the free one, because I don't like to pay for things, and I'm sure you don't either.
01:46Select the correct download for your operating system.
01:49I'm running Windows, I will download PyCharm.
01:52We will open this executable.
01:56Click Next.
01:59You could select a destination folder.
02:02I'll keep it as is.
02:04Next.
02:05I'll create a desktop shortcut.
02:07But you don't necessarily need to.
02:10Click Next.
02:12Install.
02:13And we'll just have to give it a moment.
02:15Okay, the setup is now complete.
02:20I'll check this checkbox to run PyCharm when we close this window.
02:26After opening PyCharm, we're going to create a new project.
02:31You can rename your Python project.
02:33I'll keep it as is.
02:35You can select your location.
02:36Again, I won't change that.
02:38You can create a sample welcome script, but for this tutorial, we won't.
02:43Let's select the latest Python version.
02:47And create our new project.
02:49In the menu to the left, we're going to create a new Python file.
02:54File.
02:55New.
02:56Python file.
02:58Let's name this file main.
03:01But really, you can name it anything.
03:04And select Python file.
03:07Python files end with the py file extension.
03:12We should have our main Python file within our Python project folder.
03:18Now we're going to print something to the console window.
03:21Within our main Python file, we're going to write a print statement.
03:25So type print.
03:26Add a set of parentheses.
03:28Between the set of parentheses, we will add a set of double quotes to print something.
03:34Or single quotes.
03:36Either one.
03:37My own preference is double quotes.
03:40Normally in a programming tutorial, the instructor would tell you to print something such as, hello world.
03:46But we like to be different here.
03:49Instead, think of your favorite food.
03:51In this case, I like pizza.
03:53I will print.
03:55I.
03:56Like.
03:57Pizza.
03:58To run our Python program, we will click the screen arrow.
04:03To run our main Python file.
04:07We should have a console window that displays our output.
04:10I like pizza.
04:12Or whatever your favorite food is.
04:14Let's print another line of code.
04:17Let's print.
04:20It's.
04:22Really.
04:23Good.
04:24By adding a second print statement, we are printing a second line of code.
04:31Now we'll discuss comments.
04:34The Python interpreter doesn't output comments.
04:38To write a comment, you use a pound sign.
04:41I like to call this a hashtag.
04:43My comment will be, this is my first Python program.
04:50Comments are used as notes for yourself or for other people reading this code.
04:55If I were to run this code again, this comment is not displayed to the output.
05:00We still have, I like pizza.
05:03It's really good.
05:04Alright everybody, so that is your very first Python program.
05:08And in the next topic, we'll discuss variables.
05:11Alright, we're moving on to variables.
05:14A variable is a reusable container for a value.
05:18There's four data types that you need to know as a beginner.
05:21Strings, integers, floats, and booleans.
05:24The variable behaves as if it was the value that it contains.
05:28To create a variable, we need a unique name.
05:31We'll include our full name.
05:33So we will create a variable named full name equals some value.
05:39Now a string is a series of characters.
05:42This can be within double quotes or single quotes in Python.
05:46Personally, I like to use double quotes.
05:49So why don't you type in your full name?
05:52I'll use my YouTube channel name.
05:55Now with a variable, it will behave as if it was the string of characters.
06:00I could use it within a print statement.
06:04To print a variable, the easiest way is to use an F string.
06:09F, then a set of double quotes.
06:12F means format.
06:13Using an F string, we can insert a variable wherever we would like.
06:18So I'm going to say, hello.
06:21To insert a variable, you need a set of curly braces.
06:25Then we can place that variable name within that set of curly braces.
06:29So now, my output is, hello, bro code.
06:33That's a string.
06:35It's a series of characters.
06:37Then we have integers.
06:39Integers are whole numbers.
06:41An example of an integer would be an age.
06:44Let's say that my age is 25.
06:48I wouldn't be, you know, 25.5.
06:51Integers are whole numbers.
06:53Then let's output our age.
06:56Print.
06:57I'll use an F string.
06:59You.
07:00Are.
07:01Add a placeholder.
07:02Insert our variable age.
07:04Years old.
07:07Then let's run this.
07:09Hello, bro code.
07:10You are 25 years old.
07:12Floats are numbers, but they contain a decimal.
07:16A good example of this would be a GPA.
07:19A grade point average.
07:21Let's say that my GPA is 3.2.
07:243.2 out of 4.
07:27Let's print our GPA using an F string.
07:31Your GPA is, add a placeholder.
07:35Insert our variable GPA.
07:39Your GPA is 3.2.
07:42Then we have booleans.
07:44Booleans are either true or false.
07:47Do pay attention to the capitalization.
07:48True and false need to be capitalized.
07:50So let's say that I am currently a student.
07:53Now with booleans, we typically don't output them directly.
07:56But we could.
07:57I'm going to use an F string.
07:58I'm going to use an F string.
07:59And let's say, are you a student?
08:02And then I will add a placeholder.
08:04Display our boolean variable.
08:06Are you a student?
08:07Do pay attention to the capitalization.
08:09True and false need to be capitalized.
08:10So let's say that I am currently a student.
08:12Now with booleans, we typically don't output them directly.
08:14But we could.
08:15I'm going to use an F string.
08:16And let's say, are you a student?
08:18And then I will add a placeholder.
08:23Display our boolean variable.
08:25Are you a student?
08:27That is true.
08:28Or I could say that this is false.
08:31If I'm not a student, maybe I've graduated.
08:34You can output a boolean directly, but typically we use them internally within a program.
08:41Such as when using an if statement.
08:43We haven't reached if statements yet, but I just want to show one use of booleans.
08:49We might say, if is student, if this is true, then we will print, you are a student.
09:00If is student is false, then we would print something else.
09:05Such as, you are not a student.
09:10If is student will be true, this will print, you are a student.
09:17If it were false, we would instead print, you are not a student.
09:23We'll cover if statements a little bit later, but that's where booleans are useful.
09:28We tend to use them internally within a program.
09:31Alright everybody, so those are variables.
09:34They're a reusable container for a value.
09:37And well everybody, those are variables in Python.
09:40Okay everybody, so now I'm going to show you some basic arithmetic in Python.
09:46Here's a few arithmetic operators.
09:48We have plus for addition, minus for subtraction, asterisk for multiplication,
09:53a forward slash for division, two forward slashes for integer division,
09:58and then the modulus operator, which gives us the remainder of any division.
10:03We'll begin with addition.
10:05Let's say we have a variable of friends.
10:08I will set that to be five.
10:10Then I will print my variable of friends.
10:14Now I'm not using an f-string, only because I don't have any additional text to write.
10:19I'm just outputting the variable.
10:21So friends contains a value of five.
10:24I can increment the amount of friends I have by one by using addition.
10:29So let's take friends equals friends plus one.
10:34Basically we're taking five plus one, then reassigning it to our variable of friends,
10:41which gives me six.
10:43I could increase it by two, or even three.
10:47There is a shortcut to this, because this can be a lot to write.
10:53We can use an augmented assignment operator, where we would say friends plus equals one to increment by one.
11:02That gives me six, or two, or three.
11:08So unlike other programming languages, Python doesn't have an increment operator such as plus plus.
11:16Here we have a syntax error.
11:18So to increment by one, we would have to say plus equals one.
11:23Or some other number of your choosing, depending on how much you want to add.
11:28Then we have subtraction.
11:30Let's subtract one friend.
11:32Friends equals friends, minus one.
11:36That would give me four.
11:38Minus two would be three.
11:41Or we could use the augmented assignment operator, which would be minus equals, let's say one,
11:47which gives us four.
11:49Minus two would be three.
11:51Minus three would give us two.
11:53That's subtraction.
11:55Then we have multiplication.
11:57Let's double the amount of friends that we have.
11:59Friends, asterisk, two.
12:01Asterisk is used for multiplication.
12:04So five times two is ten.
12:08Friends times three is fifteen.
12:11The augmented assignment version would be times equals some number.
12:16So friends times equals two would be ten.
12:20Three would be fifteen.
12:23Four would be twenty.
12:26Then we have division.
12:27Standard division returns a floating point number, a number that contains a decimal.
12:33Let's divide the amount of friends that we have by two.
12:37Friends equals friends divided by two.
12:41That gives me two point five.
12:43And this is a float, a floating point number.
12:46Even if our friends divided evenly, ten divided by two, we still have that point zero at the end.
12:53It's a floating point number and not an integer.
12:56So the augmented assignment version of this would be forward slash equals, what are we dividing by?
13:03We will divide friends by two, which gives us five.
13:08Or three, which gives us three point three, three, three, three, three, three, three, three, three, three, three, repeating.
13:14Then we have integer division, which is two forward slashes.
13:18Integer division returns an integer rounded down.
13:21Let's take friends equals our friends variable.
13:25Use integer division divided by three.
13:29Three.
13:30We will round down to the nearest whole integer, which is three.
13:35Because ten does not divide by three evenly.
13:38Then using integer division divided by two would give us five whole numbers.
13:44Then again, the augmented assignment version of this would be two forward slashes equals, use integer division to divide by three.
13:54That gives us a whole number of three.
13:57Then we have the modulus operator, represented by a percent sign, which would instead give us the remainder.
14:04Let's say with all of our friends, we're all in the same classroom, and the teacher says to get into groups of three.
14:10I'm going to create a new variable of remaining friends equals our group of friends, modulus three.
14:21The modulus operator gives us the remainder of any division.
14:24Ten does not divide by three evenly.
14:27How many friends do we have remaining?
14:30One.
14:31That's John.
14:32John knows what he did.
14:33He's the one friend remaining that's not paired up.
14:36Alright everybody, so those are some basic arithmetic operators in Python.
14:39We have addition, subtraction, multiplication, division, integer division, and the modulus operator, which gives you the remainder of any division.
14:49And well everybody, that is basic arithmetic in Python.
14:52Alright everybody, so we are moving on to typecasting.
14:56Typecasting is the process of converting a variable from one data type to another.
15:02We have various functions to convert a value or variable to a string, an integer, a float, or a boolean.
15:11Let's create some variables.
15:13We will create a name variable.
15:15Type in your full name.
15:17An age.
15:19Make up some age.
15:21A GPA for grade point average.
15:24Let's say minus three point two.
15:27And a boolean of is student.
15:30Are we currently a student?
15:32Let's say that's true.
15:34Now you actually could get the data type of a variable or a value by using the type function.
15:40Then pass in a value or variable.
15:43However, when I run this, there's no output.
15:47So I need a print statement.
15:49We will print what is returned by the type function.
15:54Get the type of our name variable, then print it.
15:58So our name variable is a string, str.
16:02Our age variable is an integer, an int.
16:08GPA is a float, is student, is a boolean.
16:16Using these typecast functions, we can convert from one data type to another.
16:21Here's how.
16:22Let's start with something simple.
16:24Let's convert our GPA to an integer.
16:27Currently it's a float.
16:28I will reassign GPA.
16:31Use the int function to typecast to an integer.
16:35Then pass in my GPA.
16:37At the end, we will print our GPA.
16:41If we typecast 3.2 to a whole integer, what would the result be?
16:47A whole integer of 3.
16:50We truncate the decimal portion.
16:53Let's convert our age to a floating point number.
16:56We will reassign our variable of age.
16:59Use the typecast function afloat.
17:02Then insert our age variable.
17:04Let's print our age variable.
17:11And it should be a floating point number, 25.0.
17:16Now we'll cover strings.
17:18Let's typecast our age to be a string.
17:21Age equals, call the typecast function of string, str, pass in our age variable.
17:31So the result is still going to appear the same.
17:3525.
17:36However, it's a string, not an integer.
17:39And to prove that, I will enclose my age variable with the type function.
17:46The type of variable age is a string.
17:49It would be the same as if we're taking this number and enclosing it within quotes.
17:56So this would make a difference, because let's say that I add 1 to age.
18:00Age plus equals 1.
18:04Well, we would get a type error.
18:06Can only concatenate strings, not integers, to a string.
18:11However, if I were to add a string of 1 to the end, we would be using string concatenation.
18:17So let's say it's my birthday, and I add 1 to 25.
18:21Well, since we're working with strings now, the result would be 251.
18:26I am 251 years old.
18:30So, strings and numbers behave differently.
18:33With numbers, we can use them within arithmetic expressions.
18:37Strings, not so much.
18:39We will take our name variable and typecast it to a boolean.
18:45Name equals, call the typecast function of bool, pass in our name variable.
18:52This has an interesting result.
18:54So I am going to print name.
18:56Booleans are either true or false.
18:59If I typecast my string of text into a boolean, that gives me true.
19:06Now, it really doesn't matter what I write here.
19:09If I were to change my name to a single character, such as b, this would still be true.
19:15If our string variable was empty, there were no characters within it, that would actually give us false.
19:22We could use this to check to see if somebody enters in their name or not.
19:27If somebody types in their name, then we typecast it to a boolean.
19:31If somebody skips entering in their name, that would return false.
19:35We could re-prompt the user to enter in their name again.
19:39Alright everybody, so that is typecasting.
19:41It is the process of converting a variable from one data type to another.
19:46This is especially useful with handling user input, because user input is always a string.
19:52There may be at times where you want to convert it to an integer, a float, or a boolean.
19:57And well everybody, that is typecasting in Python.
20:00Okay, we're moving on everybody.
20:03How can we accept user input in Python?
20:06Here's how.
20:07We will use the input function.
20:09We will enter in the input within the console window.
20:13However, we need a prompt.
20:15What do we want the user to type in?
20:17Let's ask for the user's name.
20:20Enter.
20:21Your.
20:22Name.
20:23This prompt will appear in the console window.
20:27Enter your name.
20:29And then we can type in our name.
20:31Although we're not doing anything with it currently.
20:34When we accept user input, let's assign it to something.
20:38Name.
20:39Let's create a name variable.
20:41Name equals our user input.
20:47Enter your name.
20:48Type in your name.
20:49Hit enter.
20:50When we type in our user input, it's going to be assigned to this variable of name.
20:55Let's print it to test it.
20:57I will print my name to the console window.
21:01Enter your name.
21:02I will type in my full name.
21:04Hit enter.
21:05And it displays what's within my name variable.
21:09Let's use an f-string this time.
21:11Let's display.
21:13Hello.
21:14Our name variable.
21:16Enter your name.
21:19Type in your full name.
21:21Hit enter.
21:22Hello.
21:23Whatever your name is.
21:24In my case, bro code.
21:26Now when we accept user input, it's always of the string data type.
21:31To test that.
21:33I will print the type of our name variable.
21:38Type in your name again.
21:41So the data type of our name variable is a string.
21:45You may want to typecast it to an integer, a float, or a boolean.
21:50This time, let's say that we need an age variable.
21:54Age equals the input function.
21:57We will prompt the user to enter your age.
22:03Then we will print the user's age using an f-string.
22:07You are, insert age, years old.
22:14Let's run this again.
22:15Enter your name.
22:16Type in your full name.
22:17Enter your age.
22:19Type in your age.
22:20Hit enter.
22:21Hello, bro code.
22:23You are 25 years old.
22:25The data type of our age is going to be a string.
22:29Let's test that.
22:30I will print the type of our age variable.
22:34Again, we have to follow the prompts.
22:38Our age variable is a string.
22:41Let's say that it's my birthday.
22:43I will add one to age.
22:45Age equals age plus one.
22:47Or we could shorten this to age plus equals one as a shortcut.
22:52Here's the result.
22:54Enter your name.
22:56Enter your age.
22:58And we get a type error.
23:00Can only concatenate strings, not int, to string.
23:04If we accept user input, and we have to use that input for any sort of arithmetic expressions,
23:10we'll have to convert it to either an integer or a floating point number.
23:14Since I'm working with whole integers, we can typecast our age as an integer.
23:19Age equals, use the int typecast function, then pass in our age.
23:26Then I can add one to age.
23:28Age plus equals one.
23:31So this should technically work.
23:34Type in your name.
23:35Type in your age.
23:36And we will add one to age.
23:38Hello BroCode, you are 26 years old.
23:42There is a way in which we can condense these steps.
23:45Rather than taking up a separate line where we're typecasting our age variable,
23:49let's enclose our input function with a typecast function of int.
23:55After receiving user input, we'll typecast it to an integer.
24:00Enter your name.
24:02Enter your age.
24:04And now we can add one to age.
24:07Because it's a whole integer.
24:09Hello BroCode, you are 26 years old.
24:12Alright everybody, so that's how to accept user input.
24:15You use the input function.
24:17Whatever you type into the console window, you can assign to a variable.
24:21With user input, we are given a string value.
24:24You may have to typecast it.
24:26As a beginner, you can typecast to an integer, a floating point number, or a boolean value.
24:31And well everybody, that's how to accept user input in Python.
24:35Okay everybody, we are moving on to if statements.
24:38With if statements, they allow for basic decision making.
24:42We can execute some code, only if a condition is true.
24:47In this demonstration, let's say we have a variable of age.
24:50We will accept some user input.
24:52We will ask the user to enter your age.
24:57Then we will typecast this input as an integer.
25:01Because when we accept user input, it's of the string data type.
25:05To write an if statement, we will type if, then we will check a condition.
25:10Let's check to see if the user's age is greater than or equal to 18.
25:16Colon, then any block of code underneath an if statement should be indented.
25:22If this condition is true, then we will perform the following.
25:26Let's print the following message.
25:28You are an adult.
25:33Let's perform a test run.
25:35Enter your age.
25:36Let's say that I'm 21.
25:38Hit enter.
25:39You are an adult.
25:42If this condition were false, here's what happens.
25:45Enter your age.
25:46Let's say that I'm 12.
25:48Hit enter.
25:49Nothing happens.
25:50If this condition is false, we skip over this block of code, like it never happened.
25:56Otherwise, we could do something else, with an else clause.
26:00Again, the block of code underneath an else clause is indented.
26:04We will print the following instead.
26:07You are a child.
26:10Let's perform a test run again.
26:12Enter your age.
26:13Let's say that I'm 12.
26:15We instead print whatever is within the else clause.
26:18You are a child.
26:20So by using if statements, they allow for basic decision making.
26:24Between if and else, we could add else if, e-l-i-f.
26:31So let's check to see else if age is less than zero.
26:37Technically, somebody shouldn't have a negative age.
26:41Let's instead print the following.
26:43You.
26:44Haven't.
26:45Been.
26:46Born.
26:47Born.
26:48Yet.
26:49And to your age, let's say that I'm negative one years old.
26:56You haven't been born yet.
26:58With if statements, else if clauses, and else clauses, we go through these statements one
27:04by one.
27:05When one of these is true, we skip the rest of the clauses, and continue with the rest
27:10of the program.
27:11You can add as many else if clauses as you would like.
27:14Let's add another.
27:16Else if age is equal to zero.
27:21To check to see if a value is equal, you use double equals.
27:26This is the equals comparison operator.
27:28If you use one equals, Python thinks you're trying to assign something.
27:33Python, in this case, thinks I'm trying to set age to be zero.
27:37So to see if a value is directly equal, you use double equals.
27:42If our age variable is zero, then we will print the following.
27:46You were just born.
27:51And to your age, let's say that I'm zero.
27:54You were just born.
27:57Let's add one more else if clause.
28:00Else if age is greater than or equal to 65.
28:06Then we will print the following.
28:09You are a senior citizen.
28:13Now check this out.
28:16Let's say that I'm 70 years old.
28:19You are an adult.
28:21Why do we execute this block of code, where age is greater than or equal to 18?
28:26Rather than this one, where we're checking if age is greater than or equal to 65.
28:31We're going through these statements one by one, beginning from the top.
28:35Technically, this statement was true.
28:3770 is greater than or equal to 18.
28:40So we execute this block of code, and skip the rest.
28:43The order of your if and else if statements does matter.
28:48Let's check to see if our age is greater than or equal to 65 first.
28:53So let's put this in the front.
28:56Turn it into an if statement.
28:58Then convert this statement to be an else if clause.
29:02Now it should work just fine.
29:04Let's say that I'm 70.
29:06You are a senior citizen.
29:09The order of your if statements and else if clauses does matter.
29:13We also could check Boolean values, too.
29:16Let's say we're going to a movie theater.
29:18We will have a Boolean variable of has ticket.
29:22Have we pre-ordered a ticket for the movie?
29:25This will be true or false.
29:28A Boolean.
29:29Let's say that we have a ticket.
29:31I'll add another if statement to check.
29:35So with a Boolean, we will check if has ticket.
29:40Now we don't necessarily need to say, is this equal to true?
29:45We could just write that Boolean value or variable.
29:48If has ticket.
29:50If this contains true, then do the following.
29:53Let's print, you may enter.
29:57You have a ticket.
30:01You need to buy a ticket.
30:14So again, our variable of has ticket is true.
30:18So first we have to enter our age.
30:20Let's say that I'm 25.
30:22You're an adult.
30:23You may enter.
30:24You have a ticket.
30:25I set the variable of has ticket to be true.
30:29Let's set this to be false.
30:31Enter your age, 25.
30:33You are an adult.
30:35You need to buy a ticket.
30:36Because we do not currently have a ticket.
30:39Let's add a few more lines to these if statements.
30:41Let's say that if we're a senior citizen, we get a discount on the price of a ticket.
30:46But first we should set the initial price.
30:49Let's say that the price of a ticket is $10.
30:54If we're considered an adult, then let's print the following.
30:58I'll use an F string.
31:00The ticket price for an adult is, we'll add a placeholder, and insert the price.
31:10I will precede this price variable with a unit of currency.
31:14I'll pick American dollars.
31:16So if we're a child, let's say the price is 50% off.
31:20We get a discount.
31:22The ticket price for a child is price times 0.5.
31:29And for a senior citizen, we get a 25% discount.
31:33The price for a senior is price times 0.75.
31:41These other else if statements are kind of distracting, so let's delete them.
31:45We'll just stick with these three statements.
31:49Enter your age.
31:50Let's say that I'm 25.
31:52Hit enter.
31:53You are an adult.
31:54The ticket price for an adult is $10.
31:57You need to buy a ticket because we do not currently have a ticket.
32:02Let's say that I'm 70.
32:05You are a senior citizen.
32:06The ticket price for a senior is $7.50.
32:10You need to buy a ticket.
32:13Let's say that we do have a ticket, and we are a child.
32:17I am 13 years old.
32:20You are a child.
32:21The ticket price for a child is $5.
32:24You may enter.
32:25You have a ticket.
32:26Alright everybody, so those are if statements.
32:29We can execute some code only if a condition is true.
32:33If statements allow for basic decision making.
32:36We have if statements, else if clauses, and else clauses.
32:41And well everybody, those are if statements in Python.
32:45Alright people, we're talking about logical operators today.
32:49Logical operators allow us to evaluate multiple conditions.
32:53We can link them together.
32:55There's three we'll discuss.
32:56Or.
32:57And.
32:58Not.
32:59We'll begin with or.
33:00With or, we can check more than one condition.
33:03If at least one of those conditions is true, then the entire statement is true.
33:08Here's an example.
33:09Let's say we have an outdoor event.
33:11And I will create two variables.
33:13One, temp, meaning temperature.
33:16Let's say that this is in Celsius.
33:1925 degrees Celsius.
33:20Pick Fahrenheit if you would like.
33:23And I will create a boolean variable of is raining.
33:27I will set that to be false.
33:29It is currently not raining.
33:31If the temperature is too hot, too cold, or it's raining, then I will cancel this outdoor event.
33:38We'll write an if statement to check that.
33:40If our temp, short for temperature, is greater than, let's say, 35.
33:4635 degrees Celsius.
33:48Then I'll use the or logical operator, or if our temp is less than zero, or if is raining, is true.
34:01If one of these conditions is true, we're going to cancel our outdoor event.
34:07So let's print the following.
34:09The outdoor event is cancelled.
34:14Else, we will print something else.
34:20The outdoor event is still scheduled.
34:26The temperature is reasonable, and is raining is false.
34:33It's not raining.
34:35So we print the else clause.
34:37The outdoor event is still scheduled.
34:40What if the temperature was really hot, like 36 degrees Celsius?
34:45Well, the outdoor event is cancelled.
34:49What if it's cold?
34:51Negative 5 degrees Celsius.
34:54The outdoor event is cancelled.
34:56This condition was true.
34:58Therefore, we execute the if statement.
35:00Or what if the temperature is reasonable, but it's raining?
35:04Is raining, is true.
35:06Well, then the outdoor event is still cancelled.
35:09So with the or logical operator, at least one of these conditions needs to be true.
35:15If one of these conditions is true, you could consider the entire statement true.
35:20Now let's cover and.
35:22With and, we can link two conditions together.
35:25Both conditions must be true, in order for that entire statement to be true.
35:30So again, let's say we have temp short for temperature, and we have a boolean variable of is sunny.
35:37I will set that to be true.
35:40We will check if our temp is greater than or equal to 28 degrees Celsius.
35:47If it's hot, and if it's sunny, if this is true, let's print the following.
36:00It is hot outside.
36:04For fun, I'm going to add an emoji, but you don't have to.
36:08I just think it's more entertaining that way.
36:10But you do you.
36:12And I will print.
36:15It is sunny.
36:20Sometimes these emojis are formatted differently, I'm just going to copy it from somewhere else.
36:24That's better.
36:26Currently the temperature is 25, 25 degrees Celsius, and it's sunny.
36:30This condition was false, but this one is true.
36:33With the and logical operator, both conditions must be true, in order for us to execute this block of code.
36:40If our temperature was 30, 30 degrees Celsius, well then, both conditions are true.
36:47It is hot outside, and it is sunny.
36:50Let's write a few more.
36:52Let's add else if.
36:53Else if the temp is less than or equal to zero, and is sunny.
37:03We will print something else.
37:05It is cold outside.
37:09I'll change the emoji.
37:11And it is sunny.
37:14Let's set the temperature to be negative 5 degrees Celsius.
37:20It is cold outside, and it is sunny.
37:23Both these conditions are true, so we do this instead.
37:27You can link as many conditions together as you would like.
37:30Let's see if our temperature is within a certain range.
37:33Else if temp is less than 28, and our temp is greater than zero, and is sunny.
37:47To check to see if something is within a certain range, there is a shortcut too.
37:51PyCharm is recommending this.
37:53We can simplify change comparisons.
37:56So this effectively does the same thing.
37:59If 28 is greater than our temp, and our temp is greater than zero, and it's sunny, then
38:06we will print it is warm outside rather than hot.
38:16And it's still sunny.
38:18So let's say our temperature is 20 degrees Celsius, and it's sunny.
38:23It is warm outside, and it is sunny.
38:27Now we have the not logical operator.
38:30It inverts the condition.
38:32We are checking to see if something is either not false, or not true.
38:37So let's check to see if it's not sunny.
38:40Really, I'll just copy what we have and paste it.
38:46Else if not is sunny.
38:52Then that means it's cloudy.
38:54And let's use a cloud emoji.
39:07So basically, not does the opposite of what you're looking for.
39:11We are checking if not is sunny.
39:14Is sunny is false, then this condition is true.
39:17Okay, let's say our temp is 28.
39:22Is sunny is now false.
39:26It is hot outside.
39:28It is cloudy.
39:29What if our temperature was zero?
39:32It is cold outside.
39:35It is cloudy.
39:36What if the temperature was reasonable like 20 degrees Celsius?
39:40It is warm outside.
39:42It is cloudy.
39:43So not, it inverts the condition.
39:46If it's true, it's now false.
39:48If it's false, it's now true.
39:50Alright everybody, so those are logical operators.
39:53They allow us to evaluate multiple conditions.
39:56With or, at least one condition must be true.
40:00With and, both conditions must be true.
40:03And not, not does the opposite.
40:05It inverts the condition.
40:07We check if something is not false, or not true.
40:10And well everybody, those are logical operators in Python.
40:15Now we're covering while loops.
40:17A while loop is used to repeat a block of code as long as a condition remains true.
40:23We recheck the condition at the end of the loop.
40:26Here's an example.
40:27I have a condition.
40:29If one is equal to one, this would be true.
40:34Then we will print the following.
40:37I am stuck in a loop.
40:41If statements only execute once.
40:44I am stuck in a loop.
40:47Let's replace if with while and see what happens.
40:51I wouldn't recommend doing this, but you can watch me.
40:54We're going to execute this code forever.
40:58At the end of our while loop, we recheck the condition again.
41:01There's no way for us to escape this loop.
41:04Hence, I am stuck in a loop.
41:06We're repeating it forever.
41:08That's how a while loop and an if statement are different.
41:11If statements are executed once, while loops could execute forever.
41:16You should put in some way to escape the while loop.
41:20Let's go over another example.
41:22Let's say we accept some user input.
41:24Name equals input.
41:27We will ask a user for their name.
41:30Enter your name.
41:34Now let's say that somebody doesn't type in their name.
41:38I'm just going to hit enter at this prompt.
41:41Our name is an empty string, an empty set of quotes.
41:45If somebody skips entering in their name, let's write a while loop.
41:50While our name is equal to an empty string, an empty set of quotes.
41:57If a user doesn't enter in their name, let's re-prompt them again.
42:02So in order to escape this while loop, the name that the user enters in can't be empty.
42:08And then once they do escape the while loop, let's print the following.
42:12I'll use an F string.
42:13Hello.
42:14Insert a placeholder.
42:16Name.
42:18If I'm supposed to enter my name, then hit enter, we get that prompt again.
42:24We are now stuck within the while loop.
42:27And I can't escape until I type in something.
42:31So let's type in our full name.
42:33Hit enter.
42:34Hit enter.
42:35And now we escape the while loop.
42:37Hello.
42:38Whatever your name is.
42:39If we type in our name right away, we actually don't enter the while loop at all.
42:47We check the while loop once, and enter it if this condition is true.
42:51Since our name was not empty, since it wasn't an empty string, we skip it entirely.
42:57So after our while loop, let's ask for a user's age.
43:01Age equals input.
43:05Enter your age.
43:07And then I'll typecast this input as an integer, since we're working with whole numbers.
43:14While our age variable is less than zero, then we'll do the following.
43:21Let's perform two actions.
43:22Let's print, age can't be less than zero.
43:29And then we will ask for the user input again.
43:33Then when we escape the while loop, we will print, hello, the user's name, and the user's age.
43:40You are, at a placeholder, age, years old.
43:47Alright, so in this program, we have two while loops.
43:50I'm just gonna change one thing. After age, I'm gonna add a colon, then a space.
43:55Just to make it look better.
43:57So let's enter our first name.
44:01Enter your age.
44:03If I type in an age that's less than zero, we'll be stuck with in this while loop.
44:08Let's say I'm negative one years old.
44:10Age can't be less than zero. Enter your age.
44:14Negative two.
44:16Age can't be less than zero.
44:18Negative kajillion.
44:20Age can't be less than zero.
44:22Okay, let's say that I'm 25.
44:24Hello, bro, you are 25 years old.
44:27While loops are really good for accepting user input.
44:31If somebody enters in a value that's not valid, you can reprompt them again.
44:35That's a great benefit of while loops.
44:38Alright everybody, so those are while loops.
44:39Those are while loops.
44:40We can repeat a block of code as long as a condition remains true.
44:45We recheck the condition at the end of the loop.
44:48If that condition is still true, we perform another iteration of that while loop.
44:53And well everybody, those are while loops in Python.
44:56Alright everybody, so we are moving on to for loops.
45:00A for loop is used to iterate over a sequence, such as a string, list, tuple, or set, but we still need to talk about these three topics.
45:10But we will soon.
45:11Or, we could use a for loop to repeat a block of code an exact amount of times.
45:17With a while loop, we will execute some code, possibly infinitely.
45:21With a for loop, it's limited.
45:24So let's say we would like to count to ten.
45:26We can do that with a for loop, fairly easily.
45:29Here's the formula.
45:31For, we need an index.
45:32But developers usually shorten this to i.
45:36i meaning index.
45:38in.
45:39Now what number do we want to count up to?
45:41We will say range.
45:44Range is a function.
45:45Then pass in ten.
45:47We will do something ten times.
45:50So let's print i.
45:52i is our index, our counter.
45:56We will iterate this loop ten times.
45:58So we begin at zero, actually, and go all the way to nine.
46:03But technically, it's a total of ten.
46:05To set the first number to be something other than zero, we can specify that.
46:10Let's say one.
46:12Then where would we like to end?
46:14The second value is going to be comma-separated.
46:17So let's say eleven.
46:19The second number is exclusive.
46:21The first number is inclusive.
46:23Print the value of i, our index,
46:25from the range between one and eleven.
46:28Exclusive.
46:30That would give us one through ten.
46:33I could change these around too.
46:35Let's say...
46:37Ninety-one through...
46:39one hundred-one.
46:41That would be ninety-one through one hundred.
46:44So again, the first number is inclusive.
46:47The second number is exclusive.
46:49Let's count from one to ten again.
46:52So one comma eleven.
46:53We could count by twos.
46:56That would be the third value.
46:58Comma two.
47:00We will count by twos.
47:02One, three, five, seven, nine.
47:05Basically, we're incrementing our index of i by two during each iteration.
47:11Or three.
47:13We'll count by threes.
47:15One, four, seven, ten.
47:17And then four would be...
47:20Let's find out.
47:21One, five, nine.
47:23We even could iterate over strings as well.
47:26Anything that is considered iterable.
47:28So let's say we have a string of name.
47:31Type in your full name.
47:33Let's change this for loop around.
47:36Rather than saying for i, our index,
47:38we could rename this to something.
47:41Let's say letter.
47:43For every letter in our iterable of name, print each letter.
47:50Then let's see what happens.
47:53We get each letter on a new line.
47:56At the end of each print statement, we print a new line character.
47:58Rather than a new line character, let's make one following change.
48:04Let's set the ending character to be something else, like a space.
48:08We will print each letter, but separate each letter with a space.
48:13Or we could even do a dash.
48:16It's your choice.
48:18Now here's a mini project that we'll do.
48:21We'll count from ten, down to zero, then display Happy New Year.
48:24So we need a for loop to iterate backwards.
48:28Let's say for i, our counter, our index, in range.
48:35Now we'll begin at ten, and end at zero.
48:39Zero is exclusive.
48:41To count backwards, we'll set the step, the third value, to be negative one.
48:46Then let's print i, our index.
48:50So then we should count backwards.
48:52Starting at ten, ending at one.
48:56Now let's do this.
48:57When we escape the for loop, let's print Happy New Year.
49:03So we start at ten, count down to one, then display Happy New Year.
49:09To actually simulate a countdown, we can import the time module.
49:14This will be a mini project.
49:15Let's import the time module at the top.
49:19After printing i, let's access the time module, call the sleep method, then pass in one, to sleep for one second.
49:29Now we'll have a countdown timer, starting at ten.
49:32Once we reach zero, we will display Happy New Year.
49:42Alright everybody, so those are for loops.
49:44For loops, they're used to iterate over a sequence, such as a string, list, tuple, and set.
49:51These three we're going to talk about next.
49:54Or, we can repeat a block of code an exact, or certain, amount of times, such as counting down from ten.
50:01And well everybody, those are for loops in Python.
50:05Alright people, we have an important topic to discuss today, and that is lists, tuples, and sets.
50:12If you come from a different programming language, these three collections are all similar to arrays.
50:17But in Python, we have three different varieties.
50:20Each has their own benefits.
50:22Let's start with the list.
50:24I have a variable of fruit.
50:26Let's say fruit is an apple.
50:28Well, with a variable, a variable can only contain a single value.
50:33With a list, we can contain multiple values.
50:37To create a list, we will enclose all values within a set of square brackets.
50:42Each additional element within this list should be comma separated.
50:46So let's include an orange, a banana, and a coconut.
50:52That's good enough for this example.
50:54Let's rename fruit as fruits, so people reading this may recognize that this contains more than one fruit.
51:03If I was to print my list of fruits, here's what's returned to us.
51:08We print out the entire list, including the set of straight brackets at the beginning and the end.
51:13To get one of the elements within this list, you'll use the index of operator, which is a set of straight brackets.
51:22For the first element, that's going to have an index of zero.
51:26The element at index zero would be the first item within my list, of apple.
51:30One would be orange, two, banana, three, coconut.
51:39If I attempt to access an element that doesn't exist, we get an index error.
51:44List index is out of range.
51:46You could use a for loop to iterate and print each element within the list.
51:52Let's say for every fruit in my collection of fruits, I will print each fruit.
52:00That will give me apple, orange, banana, coconut.
52:07Now you don't have to do this, but I'm going to end each character with a space, rather than a new line.
52:14Just so it's easier for me to read.
52:17Lists are mutable, and they're the most flexible out of the three.
52:22I can change one of these elements at a given index.
52:24Let's say fruits at index of zero is now a pineapple rather than an apple.
52:32So that should change the element at the first index to be a pineapple.
52:38Or let's say fruits at index three is a mango.
52:43Then we have apple, orange, banana, mango.
52:47We can append elements using the append method.
52:51Take our list of fruits, use the append method.
52:56What would we like to append to the end?
52:58Let's append a mango.
53:00Then we have apple, orange, banana, coconut, mango.
53:04We can remove elements.
53:07Take our list of fruits, called the remove method.
53:11Let's remove our banana.
53:16Apple, orange, coconut.
53:17We can pop, meaning we can remove an element at a given index.
53:24So take fruits, call the pop method.
53:28Let's pop the element at index zero.
53:30That's the first.
53:32Now we have orange banana, coconut.
53:35Let's pop at index one, which gets rid of the orange.
53:39Two would be the banana.
53:41Three would be the coconut.
53:42And then if we go out of bounds, we get an index error.
53:47Pop index out of range.
53:49So that's the pop method.
53:51Then if you ever need to clear your list, you'll take the list of fruits, call the clear method.
53:58That will remove all the elements from your list.
54:00So lists are immutable, meaning we can change the elements, and they're the most flexible.
54:08We can change the element at a given index, append elements, remove elements, pop elements, and we can clear the elements.
54:17Our next type of collection is a tuple.
54:19They behave similarly to lists.
54:22However, rather than enclosing all of your elements within a set of straight brackets,
54:27you'll enclose them within a set of parentheses.
54:30Where tuples differ is that they're immutable, meaning we can't change the elements once the tuple is created.
54:38But this makes them faster.
54:40It's faster to access the elements of a tuple rather than a list.
54:45So PyCharm has even given me a warning.
54:46Tuples don't support item assignment.
54:50If I attempt to change the element at index 0 to be...
54:55Uh, let's keep that as a mango.
54:57Well, we get a type error.
54:59Tuple does not support item assignment.
55:02Once we create a tuple, we can't change the elements.
55:06They're immutable.
55:08Even if I was to append a new element, fruits.append, I will append a mango.
55:14Again, we get that attribute error.
55:18Tuple has no attribute append.
55:21I can't remove either.
55:24I will attempt to remove an apple.
55:27Again, tuple object has no attribute remove.
55:31Tuples are good if you need a collection that can't be changed.
55:35The benefit of this is that they're faster to access.
55:40Then we have sets, which are probably the most complicated.
55:44All your values will be enclosed within a set of curly braces.
55:48We can add and remove elements, but we can't replace any of the elements within it.
55:54That's because sets are unordered.
55:56So, to demonstrate this, I'm going to print my set of fruits.
56:02The order is going to change each time.
56:05During this first run, we have coconut, then apple, orange, then banana.
56:09If I run this again, we get apple, coconut, banana, orange, then coconut, banana, apple, orange.
56:15Sets are unordered.
56:18We can't access them by index.
56:20So, let's say fruits at index 0 is now a pineapple.
56:30Well, sets do not support item assignment.
56:33That's because they're unordered.
56:35Whatever's at index 0 within our set is going to change each time.
56:39They're unordered.
56:40However, we can add and remove elements, and we would use the add method to do that, rather than append.
56:47So, within our set, let's append a mango.
56:53So, we have a mango within our set, but the position changes each time.
57:00We can remove.
57:02Fruits.remove.
57:05Let's remove the coconut.
57:07So, the coconut's gone.
57:12Let's attempt to pop.
57:14Fruits.pop
57:16At index 0.
57:18Again, we can't work with indices.
57:21Type error. Pop takes no arguments.
57:25And we can still clear.
57:27Fruits.clear.
57:31Sets also don't allow for any duplicates.
57:34What if I were to add just a bunch of coconuts to the end?
57:38Well, we still only have one coconut.
57:41Where sets are really good is for membership testing, checking to see if there's a given value within your set.
57:49Here's how we can do that.
57:50I'll write an if statement.
57:53If the word apple is in our set of fruits.
57:59If this statement is true, then let's print.
58:04Apple was found.
58:07Else, we will print something else.
58:10Print.
58:12Apple was not found.
58:16So, apple is currently within our set.
58:19Apple was found.
58:21But what about a pineapple?
58:23Let's change apple to pineapple.
58:27Currently, there's no pineapple.
58:29Pineapple was not found.
58:31We even could accept user input.
58:34Let's say, fruit equals input.
58:38Enter a fruit to search for.
58:45If our fruit variable is in fruits, let's convert these print statements to an f-string.
58:52Add a placeholder, fruit.
58:54Our fruit was found, if this is true.
58:57Else, our fruit was not found.
59:01Enter a fruit to search for.
59:03Is there an orange?
59:05Orange was found.
59:09Is there a mango?
59:11Mango was not found.
59:14Sets are efficient for membership testing, meaning checking to see if there's a given element within that set.
59:20Alright everybody, so those are lists, tuples, and sets.
59:24Lists are mutable, meaning we can change the elements, and they're the most flexible.
59:29Tuples are mutable, but they're faster than lists.
59:33Sets are mutable only when adding and removing elements.
59:38They're unordered, we can't access a set by an index.
59:41They don't allow for duplicates, and sets are the best for membership testing.
59:45And well everybody, those are lists, tuples, and sets in Python.
59:49And then we'll see you next time.
59:50See you next time.
Recommended
3:22
10:40
12:42
1:38:32
1:20
1:59:04
1:57:34
1:10:17
2:08
0:53
1:05
1:14
Be the first to comment