Transcript for:
Essential Python Programming Basics

In this video, I'm going to teach you as much Python as I can in exactly one hour. If you would like a quick introduction, or you're transitioning from another programming language, this video is for you. If you would like to go more in-depth, I do have a full, free, 12-hour course on my channel.

Otherwise, let's get started. I don't like boring introductions, so I say we just jump right in. There's two things we'll need to download. The first is a Python interpreter to convert our written code.

To machine code, we're going to head to python.org, go to downloads, and download the latest version. We will open this executable. If you're on Windows, you'll want to check this checkbox, add Python EXE to path, and we will install now.

The setup was successful, and that's all you need to do to download the Python interpreter. The second download we'll need is an IDE, an integrated development environment, basically a place where we can write code. For IDEs, there's two popular choices when writing Python code, PyCharm and VS Code.

If you already use VS Code, you can stick with that. Just be sure to download the Python extension. I find PyCharm more beginner-friendly if you've never coded before. If you would like to use PyCharm, go to jetbrains.com slash PyCharm. and we will click this green download button.

There's two versions of PyCharm, the professional version and the community version. The professional version is paid for. I would not recommend using it, only because there's plenty of free IDEs on the market.

We'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. Select the correct download for your operating system. I'm running Windows.

I will download PyCharm. We will open this executable. Click next. You could select a destination folder. I'll keep it as is.

Next. I'll create a desktop shortcut, but you don't necessarily need to. Click next.

Install. And we'll just have to give it a moment. Okay, the setup is now complete. I'll check this checkbox to run PyCharm when we close this window. After opening PyCharm, we're going to create a new project.

You can rename your Python project. I'll keep it as is. You can select a location.

Again, I won't change that. You can create a sample welcome script, but for this tutorial, we won't. Let's select the latest Python version and create our new project.

In the menu to the left, we're going to create a new Python file. File, New, Python File. Let's name this file Main.

But really, you can name it anything. And select Python File. Python files end with the pyfile extension.

We should have our main Python file within our Python project folder. Now we're going to print something to the console window. Within our main Python file, we're going to write a print statement.

So type print, add a set of parentheses. Between the set of parentheses, we will add a set of double quotes to print something, or single quotes, either one. My own preference is double quotes.

Normally in a programming tutorial, the instructor would tell you to print something such as, Hello world, but we like to be different here. Instead, think of your favorite food. In this case, I like pizza. I will print, I like pizza.

To run our Python program, we will click this green arrow to run our main Python file. We should have a console window that displays our output. I like pizza.

Or whatever your favorite food is. Let's print another line of code. Let's print...

It's really good. By adding a second print statement, we are printing a second line of code. Now we'll discuss comments. The Python interpreter doesn't output comments.

To write a comment, you use a pound sign. I like to call this a hashtag. My comment will be, This is my first Python program.

Comments are used as notes for yourself, or for other people reading this code. If I were to run this code again, This comment is not displayed to the output. We still have, I like pizza, it's really good.

Alright everybody, so that is your very first Python program. And in the next topic, we'll discuss variables. Alright, we're moving on to variables.

A variable is a reusable container for a value. There's four data types that you need to know as a beginner. Strings, integers, floats, and booleans. The variable behaves... as if it was the value that it contains.

To create a variable, we need a unique name. We'll include our full name. So we will create a variable named full name equals some value. Now a string is a series of characters.

This can be within double quotes or single quotes in Python. Personally, I like to use double quotes. So why don't you type in your full name? I'll use my YouTube channel name. Now with a variable, it will behave as if it was the string of characters.

I could use it within a print statement. To print a variable, the easiest way is to use an F-string. F, then a set of double quotes.

F means format. Using an F-string, we can insert a variable wherever we would like. So I'm going to say, hello. To insert a variable, you need a set of curly braces.

Then we can place that variable name within that set of curly braces. So now, my output is, hello, bro code. That's a string. It's a series of characters. Then we have integers.

Integers are whole numbers. An example of an integer would be an age. Let's say that my age is 25. I wouldn't be, you know, 25.5. Integers are whole numbers.

Then let's output our age. print, I'll use an f-string, u, r, add a placeholder, insert our variable age, years old. Then let's run this.

Hello Bro Code, you are 25 years old. Floats are numbers, but they contain a decimal. A good example of this would be a GPA, a grade point average.

Let's say that my GPA is 3.2, 3.2 out of 4. Let's print our GPA using an F string. Your GPA is, add a placeholder, insert our variable GPA. Your GPA is 3.2. Then we have booleans.

Booleans are either true or false. Let's create a boolean variable of isStudent. This is either true or false. Do pay attention to the capitalization.

True and false need to be capitalized. So let's say that I am currently a student. Now with booleans, we typically don't output them directly. But we could.

I'm going to use an F string and let's say, are you a student? And then I will add a placeholder, display our boolean variable. Are you a student? That is true. Or I could say that this is false.

If I am not a student, maybe I've graduated. You can output a Boolean directly, but typically we use them internally within a program, such as when using an if statement. We haven't reached if statements yet, but I just want to show one use of booleans.

We might say if isStudent, if this is true, then we will print you are a student. If isStudent is false, then we would print something else, such as you are not a student. isStudent will be true. This will print, you are a student. If it were false, we would instead print, you are not a student.

We'll cover if statements a little bit later, but that's where booleans are useful. We tend to use them internally within a program. Alright everybody, so those are variables. They're a reusable container for a value.

And well everybody, those are variables in Python. Okay everybody, so now I'm going to show you some basic arithmetic in Python. Here's a few arithmetic operators.

We have plus for addition, minus for subtraction, asterisk for multiplication, a forward slash for division, two forward slashes for integer division, and then the modulus operator. which gives us the remainder of any division. We'll begin with addition. Let's say we have a variable of friends.

I will set that to be 5. Then I will print my variable of friends. Now I'm not using an f string, only because I don't have any additional text to write, I'm just outputting the variable. So friends contains a value of 5. I can increment the amount of friends I have by 1 by using addition. So let's take friends equals friends plus 1. Basically, we're taking 5 plus 1, then reassigning it to our variable of friends, which gives me 6. I could increase it by 2, or even 3. There is a shortcut to this, because this can be a lot to write.

We can use an augmented assignment operator. where we would say friends plus equals 1 to increment by 1. That gives me 6, or 2, or 3. So unlike other programming languages, Python doesn't have an increment operator such as plus plus. Here we have a syntax error.

So to increment by 1, we would have to say plus equals 1. Or some other number of your choosing, depending on how much you want to add. Then we have subtraction. Let's subtract one friend.

Friends equals friends minus 1. That would give me 4. Minus 2 would be 3. Or we could use the augmented assignment option. which would be minus equals, let's say 1, which gives us 4. Minus 2 would be 3. Minus 3 would give us 2. That's subtraction. Then we have multiplication. Let's double the amount of friends that we have. Friends, asterisk, 2. Asterisk is used for multiplication.

So 5 times 2 is 10. Friends times 3 is 15. The augmented assignment version would be times equals some number. So friends times equals 2 would be 10. 3 would be 15. 4 would be 20. Then we have division. Standard division returns a floating point number, a number that contains a decimal.

Let's divide the amount of friends that we have by 2. Friends equals friends divided by 2. That gives me 2.5. And this is a float, a floating point number. Even if our friends divided evenly... 10 divided by 2, we still have that point zero at the end. It's a floating point number and not an integer.

So the augmented assignment version of this would be forward slash equals, what are we dividing by? We will divide friends by 2, which gives us 5, or 3, which gives us 3.33333 repeating. Then we have integer division, which is two forward slashes.

Integer division returns an integer, rounded down. Let's take friends, equals our friends variable, use integer division divided by three. We will round down to the nearest whole integer, which is three. because 10 does not divide by 3 evenly. Then using integer division divided by 2 would give us 5, whole numbers.

Then again the augmented assignment version of this would be two forward slashes equals use integer division to divide by 3. That gives us a whole number of 3. Then we have the modulus operator represented by a percent sign, which would instead give us the remainder. Let's say with all of our friends, we're all in the same classroom, and the teacher says to get into groups of three. I'm going to create a new variable of remaining friends.

equals our group of friends, modulus 3. The modulus operator gives us the remainder of any division. 10 does not divide by 3 evenly. How many friends do we have remaining?

  1. That's John. John knows what he did. He's the one friend remaining that's not paired up.

All right, everybody, so those are some basic arithmetic operators in Python. We have addition, subtraction, multiplication, division, integer division, and the modulus operator. operator, which gives you the remainder of any division. And well everybody, that is basic arithmetic in Python. All right everybody, so we are moving on to typecasting.

Typecasting is the process of converting a variable from one data type to another. We have various functions to convert a value or variable to a string, an integer, a float, or a boolean. Let's create some variables. We will create a name variable. Type in your full name, an age, make up some age, a GPA for grade point average, let's say minus 3.2, and a boolean of a student.

Are we currently a student? Let's say that's true. Now you actually could get the data type of a variable or a value by using the type function.

Then pass in a value or variable. However, when I run this, there's no output. So I need a print statement.

We will print what is returned by the type function. Get the type of our name variable, then print it. So our name variable...... is a string, str.

Our age variable is an integer, an int. GPA is a float, is student, is a boolean. Using these typecast functions, we can convert from one data type to another.

Here's how. Let's start with something simple. Let's convert our GPA to an integer. Currently it's a float.

I will reassign GPA, use the int function to typecast to an integer, then pass in my GPA. At the end we will print our GPA. If we typecast 3.2 to a whole integer, what would the result be?

A whole integer of 3. We truncate the decimal portion. Let's convert our age to a floating point number. We will reassign our variable of age, use the typecast function of float, then insert our age variable. Let's print our age variable.

and it should be a floating point number, 25.0. Now we'll cover strings. Let's typecast our age to be a string.

Age equals, call the typecast function of string str, pass in our age variable. So the result is still going to appear the same, 25. However, it's a string, not an integer. And to prove that, I will enclose my age variable with the type function.

The type of variable age is a string. It would be the same as if we're taking this number and enclosing it within quotes. So this would make a difference, because let's say that I add 1 to age. Age plus equals 1. Well, we would get a type error. Can only concatenate strings, not integers, to a string.

However, if I were to add a string of 1 to the end, we would be using string concatenation. So let's say it's my birthday, and I add 1 to 25. Well, since we're working with strings now, the result would be 251. I am 251 years old. So, Strings and Numbers behave differently. With Numbers, we can use them within arithmetic expressions. Strings, not so much.

We will take our name variable and typecast it to a Boolean. Name equals, call the typecast function of bool, pass in our name variable. This has an interesting result, so I'm going to print name.

Booleans are either true or false. If I typecast my string of text into a boolean, that gives me true. Now it really doesn't matter what I write here. If I were to change my name to a single character such as b, this would still be true.

If our string variable was empty, there were no characters within it, that would actually give us false. We could use this to check to see if somebody enters in their name or not. If somebody types in their name, then we typecast it to a boolean. If somebody skips entering in their name, that would return false. We could re-prompt the user to enter in their name again.

Alright everybody, so that is typecasting. It is the process of converting a variable from one data type to another. This is especially useful with handling user input, because user input is always a string.

There may be at times where you want to convert it to an integer, a float, or a boolean. And well everybody, that is typecasting in Python. Okay, we're moving on everybody.

How can we accept user input in Python? Here's how. We will use the input function.

We will enter in the input within the console window. However, we need a prompt. What do we want the user to type in?

Let's ask for the user's name. Enter your name. This prompt will appear in the console window. Enter your name.

And then we can type in our name. Although we're not doing anything with it currently, when we accept user input, let's assign it to something. Let's create a name variable. Name equals our user input. Enter your name, type in your name, hit enter.

When we type in our user input, it's going to be assigned to this variable of name. Let's print it to test it. I will print my name to the console window. Enter your name.

I will type in my full name. Hit enter. And it displays what's within my name variable. Let's use an fstring this time. Let's display hello, our name variable.

Enter your name. Type in your full name. Hit enter. Hello, whatever your name is. In my case, bro code.

Now when we accept user input, it's always of the string data type. To test that, I will print the type of our name variable. Type in your name again. So the data type of our name variable is a string. You may want to typecast it to an integer, a float, or a boolean.

This time let's say that we need an age variable. Age equals the input function. We will prompt the user to enter your age. Then we will print the user's age using an F string. You are, insert age, years old.

Let's run this again. Enter your name, type in your full name, enter your age, type in your age. Hit enter. Hello brocode, you are 25 years old. The data type of our age is going to be a string.

Let's test that. I will print the type of our age variable. Again we have to follow the prompts. Our age variable is a string. Let's say that it's my birthday.

I will add 1 to age. Age equals age plus 1, or we could shorten this to age plus equals 1 as a shortcut. Here's the result.

Enter your name, enter your age, and we get a type error. Can only concatenate strings, not int, to string. If we accept user input, and we have to use that input for any sort of arithmetic expressions, we'll have to convert it to either an integer or floating point number. Since I'm working with whole integers, we can typecast our age as an integer.

Age equals, use the int typecast function, then pass in our age. Then I can add 1 to age. Age plus equals 1. So this should technically work.

Type in your name, type in your age, and we will add 1 to age. Hello, Brocode, you are 26 years old. There is a way in which we can condense these steps. Rather than taking up a separate line where we're typecasting our age variable, let's enclose our input function with a typecast function of int. After receiving user input, we'll typecast it to an integer.

Enter your name, enter your age, and now we can add 1 to age, because it's a whole integer. Hello, Bro Code, you are 26 years old. Alright everybody, so that's how to accept user input.

You use the input function. Whatever you type into the console window, you can assign to a variable. With user input, we are given a string value. You may have to typecast it.

As a beginner, you can typecast to an integer, a floating point number, or a boolean value. And well everybody, that's how to accept user input in Python. Okay everybody, we are moving on to if statements.

With if statements, they allow for basic decision making. We can execute some code only if a condition is true. In this demonstration, let's say we have a variable of age.

We will accept some user input. We will ask the user to enter your age. Then we will typecast this input as an integer, because when we accept user input, it's of the string data type. To write an if statement, we will type if, then we will check a condition. Let's check to see if the user's age is greater than or equal to 18, colon, then any block of code underneath an if statement should be indented.

If this condition is true, then we will perform the following. Let's print the following message. You are an adult. Let's perform a test run. And to your age, let's say that I'm 21, hit Enter, you are an adult.

If this condition were false, here's what happens. And to your age, let's say that I'm 12, hit Enter, nothing happens. If this condition is false, we skip over this block of code, like it never happened. Otherwise, we could do something else with an else clause. Again, the block of code underneath an else clause is indented.

We will print the following instead. You are a child. Let's perform a test run again.

And to your age, let's say that I'm 12. We instead print whatever is within the else clause. You are a child. So by using if statements, they allow for basic decision making. Between if and else, we could add else if. E-L-I-F.

So let's check to see else if age is less than zero. Technically, somebody shouldn't have a negative age. Let's instead print the following.

You haven't been born yet. And to your age, let's say that I'm negative one years old. You haven't been born yet. With if statements, else if clauses, and else clauses, we go through these statements one by one. When one of these is true, we skip the rest of the clauses and continue with the rest of the program.

You can add as many else if clauses as you would like. Let's add another. Else if age is equal to zero.

To check to see if a value is equal, you use double equals. This is the equals comparison operator. If you use one equals, Python thinks you're trying to assign something.

Python in this case thinks I'm trying to set age to be zero. So to see if a value is directly equal, you use double equals. If our age variable is zero, then we will print the following.

You were just born. Enter your age. Let's say that I'm zero. You were just born. Let's add one more else if clause.

Else if age is greater than or equal to 65. Then we will print the following. You are a senior citizen. Now check this out.

Let's say that I'm 70 years old. You are an adult. Why do we execute this block of code, where age is greater than or equal to 18, rather than this one, where we're checking if age is greater than or equal to 65?

We're going through these statements one by one, beginning from the top. Technically this statement was true. 70 is greater than or equal to 18. So we execute this block of code, and skip the rest.

The order of your if and else if statements does matter. Let's check to see if our age is greater than or equal to 65 first. So let's put this in the front. Turn it into an if statement, then convert this statement to be an else if clause.

Now it should work just fine. Let's say that I'm 70. You are a senior citizen. The order of your if statements and else if clauses does matter. We also could check boolean values too.

Let's say we're going to a movie theater. We will have a boolean variable of has ticket. Have we pre-ordered a ticket for the movie?

This will be true or false, a boolean. Let's say that we have a ticket. I'll add another if statement to check. So with a boolean, we will check if hasTicket. Now we don't necessarily need to say, is this equal to true?

We could just write that boolean value or variable. If hasTicket. If this contains true, then do the following. Let's print, you may enter.

You have a ticket. Else, let's print something else. You need to buy a ticket.

So again, our variable of hasTicket is true. So first we have to enter our age. Let's say that I'm 25. You're an adult.

You may enter. You have a ticket. I set the variable of hasTicket to be true. Let's set this to be false. Enter your age, 25. You are an adult.

You need to buy a ticket because we do not currently have a ticket. Let's add a few more lines to these if statements. Let's say that if we're a senior citizen, we get a discount on the price of a ticket. But first we should set the initial price.

Let's say that the price of a ticket is $10. If we're considered an adult, then let's print the following. I'll use an F-string. The ticket price for an adult is, we'll add a placeholder, and insert the price.

I will precede this price variable with a unit of currency. I'll pick American dollars. So if we're a child, let's say the price is 50% off.

We get a discount. The ticket price for a child is price times 0.5. If we're a senior citizen, we get a 25% discount.

The price for a senior is price times 0.75. These other else if statements are kind of distracting, so let's delete them. We'll just stick with these three statements. Enter your age. Let's say that I'm 25. Hit enter.

You are an adult. The ticket price for an adult is $10. You need to buy a ticket because we do not currently have a ticket. Let's say that I'm 70. You are a senior citizen.

The ticket price for a senior is $7.50. You need to buy a ticket. Let's say that we do have a ticket, and we are a child.

I am 13 years old. You are a child. The ticket price for a child is $5. You may enter. You have a ticket.

Alright everybody, so those are if statements. We can execute some code, only if a condition is true. If statements allow for basic decision making. We have if statements, else if clauses, and else clauses. And well everybody, those are if statements in Python.

Alright people, we're talking about logical operators today. Logical operators allow us to evaluate multiple conditions. We can link them together. There's three we'll discuss. OR and NOT.

We'll begin with OR. With OR, we can check more than one condition. If at least one of those conditions is true, then the entire statement is true. Here's an example. Let's say we have an outdoor event.

And I will create two variables. One, temp, meaning temperature. Let's say that this is in Celsius, 25 degrees Celsius.

Pick Fahrenheit if you would like. And I will create a boolean variable of isRaining. I will set that to be false.

It is currently not raining. If the temperature is too hot, too cold, or it's raining, then I will cancel this outdoor event. We'll write an if statement to check that.

If our temp, short for temperature, is greater than, let's say, 35, 35 degrees Celsius, then I'll use the or logical operator, or if our temp is less than zero, or if is raining, is true. If one of these conditions is true, we're going to cancel our outdoor event. So let's print the following.

The outdoor event is canceled. Else, we will print something else. The outdoor event is still scheduled.

The temperature is reasonable, and is raining is false. It's not raining. So we print the else clause.

The outdoor event is still scheduled. What if the temperature was really hot, like 36 degrees Celsius? Well, the outdoor event is canceled. What if it's cold?

Negative 5 degrees Celsius. The outdoor event is cancelled. This condition was true, therefore we execute the if statement. Or what if the temperature is reasonable, but it's raining? Is raining is true.

Well then the outdoor event is still cancelled. So with the OR logical operator, at least one of these conditions needs to be true. If one of these conditions is true, you could consider the entire statement true. Now let's cover AND.

With AND, we can link two conditions together. Both conditions must be true in order for that entire statement to be true. So again, let's say we have temp short for temperature and we have a boolean variable of is sunny. I will set that to be true.

We will check if our temp is greater than or equal to 28 degrees Celsius. And is it sunny? Is sunny.

If it's hot and if it's sunny, if this is true, let's print the following. It is hot outside. For fun, I'm going to add an emoji, but you don't have to. I just think it's more entertaining that way.

But you do you. And I will print, it is sunny. Sometimes these emojis are formatted differently, I'm just going to copy it from somewhere else.

That's better. Currently the temperature is 25, 25 degrees Celsius, and it's sunny. This condition was false, but this one is true. With the and logical operator, both conditions must be true in order for us to execute this block of code.

If our temperature was 30, 30 degrees Celsius, well then, both conditions are true. It is hot outside, and it is sunny. Let's write a few more.

Let's add else if. Else if the temp is less than or equal to zero, and is sunny, we will print something else. It is cold outside.

I'll change the emoji. And it is sunny. Let's set the temperature to be negative 5 degrees Celsius.

It is cold outside and it is sunny. Both these conditions are true, so we do this instead. You can link as many conditions together as you would like.

Let's see if our temperature is within a certain range. else if temp is less than 28 and our temp is greater than zero and is sunny. To check to see if something is within a certain range, there is a shortcut too.

PyCharm is recommending this. We can simplify chain comparisons. So this effectively does the same thing.

If 28 is greater than our temp, and our temp is greater than zero, and it's sunny, then we will print it is warm outside rather than hot. And it's still sunny. So let's say our temperature is 20 degrees Celsius and it's sunny.

It is warm outside, and it is sunny. Now we have the not logical operator. It inverts the condition.

We are checking to see if something is either not false or not true. So let's check to see if it's not sunny. Really I'll just copy what we have and paste it. Else if not is sunny. Then that means it's cloudy.

And let's use a cloud emoji. So basically not does the opposite of what you're looking for. We are checking if not is sunny.

Is sunny is false. Then this condition is true. Okay, let's say our temp is 28. Is sunny is now false.

It is hot outside, it is cloudy. What if our temperature was zero? It is cold outside, it is cloudy.

What if the temperature was reasonable, like 20 degrees Celsius? It is warm outside, it is cloudy. So not, it inverts the condition.

If it's true, it's now false. If it's false, it's now true. Alright everybody, so those are logical operators. They allow us to evaluate multiple conditions. With OR, at least one condition must be true.

With AND, both conditions must be true. And NOT, NOT does the opposite. It inverts the condition.

We check if something is NOT false or NOT true. And well everybody, those are logical operators in Python. Now we're covering while loops. A while loop is used to repeat a block of code as long as a condition remains true.

We recheck the condition at the end of the loop. Here's an example. I have a condition.

If 1 is equal to 1, this would be true. Then we will print the following. I am stuck in a loop. If statements only execute once.

I am stuck in a loop. Let's replace if with while and see what happens. I wouldn't recommend doing this, but you can watch me. We're going to execute this code forever. At the end of our while loop, we recheck the condition again.

There's no way for us to escape this loop. Hence, I am stuck in a loop. We're repeating it forever.

That's how a while loop and an if statement work. are different. If statements are executed once, while loops could execute forever, you should put in some way to escape the while loop. Let's go over another example. Let's say we accept some user input name equals input, we will ask a user for their name, enter your name.

Now let's say that somebody doesn't type in their name. I'm just going to hit enter at this prompt. Our name is an empty string, an empty set of quotes. If somebody skips entering in their name, let's write a while loop.

While our name is equal to an empty string, an empty set of quotes. If a user doesn't enter in their name, let's reprompt them again. So in order to escape this while loop, the name that the user enters in can't be empty. And then once they do escape the while loop, let's print the following. I'll use an f string.

Hello, insert a placeholder, name. If I'm supposed to enter my name, then hit enter, we get that prompt again. We are now stuck within the while loop.

And I can't escape until I type in something. So let's type in our full name, hit enter, and now we escape the while loop. Hello, whatever your name is. If we type in our name right away... We actually don't enter the while loop at all.

We check the while loop once and enter it if this condition is true. Since our name was not empty, since it wasn't an empty string, we skip it entirely. So after our while loop, let's ask for a user's age.

age equals input Enter your age and then I'll typecast this input as an integer, since we're working with whole numbers. While our age variable is less than zero, then we'll do the following. Let's perform two actions. Let's print age can't be less than zero, and then we will ask for the user input again. Then when we escape the while loop, we will print hello, the user's name, and the user's age.

You are, add a placeholder, age, years old. Alright, so in this program, we have two while loops. I'm just going to change one thing.

After age, I'm going to add a colon, then a space, just to make it look better. So let's enter our first name. Enter your age. If I type in an age that's less than 0, we'll be stuck within this while loop.

Let's say I'm negative 1 years old. Age can't be less than 0. Enter your age. Negative 2. Age can't be less than 0. Negative kajillion.

Age can't be less than 0. Okay, let's say that I'm 25. Hello, bro, you are 25 years old. While loops are really good for accepting user input. If somebody enters in a value that's not valid, you can reprompt them again. That's a great benefit of while loops. All right, everybody, so those are while loops.

We can repeat a block of code as long as a condition remains true. We recheck the condition at the end of the loop. If that condition is still true, we perform another iteration of that while loop. And well, everybody, those are while loops in Python. Alright everybody, so we are moving on to for loops.

A 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, but we will soon. Or we could use a for loop to repeat a block of code an exact amount of times. With a while loop, we will execute some code, possibly infinitely. With a for loop, it's limited.

So let's say we would like to count to 10. We can do that with the for loop fairly easily. Here's the formula. For, we need an index. But developers usually shorten this to i, i meaning index. In, Now what number do we want to count up to?

We will say range, range is a function, then pass in 10. We will do something 10 times. So let's print i. i is our index, our counter. We will iterate this loop 10 times. So we begin at 0 actually, and go all the way to 9. But technically it's a total of 10. To set the first number to be something other than 0, we can specify that.

Let's say 1. Then where would we like to end? The second value is going to be comma separated, so let's say 11. The second number is exclusive. The first number is inclusive.

Print the value of i, our index, from the range between 1 and 11, exclusive. That would give us 1 through 10. I could change these around too, let's say 91 through 101. That would be 91 through 100. So again, the first number is inclusive, the second number is exclusive. Let's count from 1 to 10 again, so 1, 11. We could count by twos. That would be the third value, comma 2. We will count by twos, 1, 3, 5, 7, 9. Basically, we're incrementing our index of i by 2 during each iteration.

Or 3. We'll count by 3s. 1, 4, 7, 10. And then 4 would be... Let's find out. 1, 5, 9. We even could iterate over strings as well.

Anything that is considered iterable. So let's say we have a string of name. Type in your full name.

Let's change this for loop around. Rather than saying for i, our index, we could rename this to something. Let's say letter for every letter in our iterable of name print each letter. Then let's see what happens. We get each letter on a new line.

At the end of each print statement, we print a new line character. Rather than a newline character, let's make one following change. Let's set the ending character to be something else, like a space. We will print each letter, but separate each letter with a space.

Or we could even do a dash. It's your choice. Now here's a mini project that we'll do.

We'll count from 10 down to 0, then display Happy New Year. So we need a for loop to iterate backwards. Let's say for i, our count our index in range.

Now we'll begin at 10 and end at 0. 0 is exclusive. To count backwards, we'll set the step, the third value, to be negative 1. Then let's print i, our index. So then we should count backwards, starting at 10 ending at 1. Now let's do this. When we escape the for loop, let's print Happy New Year.

So we start at 10, count down to 1, then display Happy New Year. To actually simulate a countdown, we can import the time module. This will be a mini project.

Let's import the time module at the top. After printing i, let's access the time module, call the sleep method, then pass in 1, to sleep for 1 second. Now we'll have a countdown timer, starting at 10. Once we reach 0, we will display... Happy New Year! Alright everybody, so those are for loops.

They're used to iterate over a sequence, such as a string, list, tuple, and set. These three we're going to talk about next. Or we can repeat a block of code an exact or certain amount of times, such as counting down from 10. And well everybody, those are for loops in Python.

Alright people, we have an important topic to discuss today, and that is lists, tuples, and sets. If you come from a different programming language, these three collections are all similar to arrays. But in Python, we have three different varieties. Each has their own benefits. Let's start with the list.

I have a variable of fruit. Let's say fruit is an apple. Well, with a variable, a variable can only contain a single value. With a list, we can contain multiple values.

To create a list, we will enclose all values within a set of square brackets. Each additional element within this list should be comma-separated. So let's include an orange, a banana, and a coconut. That's good enough for this example.

Let's rename fruit as fruits, so people reading this may recognize that this contains more than one fruit. If I was to print my list of fruits, here's what's returned to us. We print out the entire list, including the set of straight brackets at the beginning and the end. To get one of the elements within this list, you'll use the indexOf operator, which is a set of straight brackets. For the first element, that's going to have an index of 0. The element at index 0 would be the first item within my list of apple.

1 would be orange. 2, banana. 3, coconut.

If I attempt to access an element that doesn't exist, we get an index error. List index is out of range. You could use a for loop to iterate and print each element within the list. Let's say for every fruit in my collection of fruits, I will print each fruit. That will give me apple, orange, banana, coconut.

Now you don't have to do this, but I'm going to end each character with a space rather than a new line, just so it's easier for me to read. Lists are mutable, and they're the most flexible out of the three. I can change one of these elements at a given index.

Let's say fruits at index of 0 is now a pineapple rather than an apple. So that should change the element at the first index to be a pineapple. Or let's say fruits at index 3 is a mango. Then we have apple, orange, banana, mango.

We can append elements using the append method. Take our list of fruits. Use the append method.

What would we like to append to the end? Let's append a mango. Then we have apple, orange, banana, coconut, mango. We can remove elements.

Take our list of fruits, call the remove method. Let's remove our banana. Apple, orange, coconut. We can pop, meaning we can remove an element at a given index. So take fruits, call the pop method.

Let's pop the element at index 0. That's the first. Now we have orange banana coconut. Let's pop at index 1, which gets rid of the orange.

2 would be the banana. 3 would be the coconut. And then if we go out of bounds, we get an index error. Pop index out of range. So that's the pop method.

Then if you ever need to clear your list, you'll take the list of fruits, call the clear method. That will remove all the elements from your list. So lists are immutable, meaning we can change the elements, and they're the most flexible. We can change the element at a given index, append elements, remove elements, pop elements, and we can clear the elements. Our next type of collection is a tuple.

They behave similarly to lists. However, rather than enclosing all of your elements within a set of straight brackets, you'll enclose them within a set of parentheses. Where tuples differ is that they're immutable, meaning we can't change the elements once the tuple is created, but this makes them faster. It's faster to access the elements of a tuple rather than a list. So PyCharm has even given me a warning.

Tuples don't support item assignment. If I attempt to change the element at index 0 to be, let's keep that as a mango, well, we get a type error. tuple does not support item assignment. Once we create a tuple, We can't change the elements. They're immutable.

Even if I was to append a new element, fruits.append, I will append a mango. Again, we get that attribute error. tuple has no attribute append.

I can't remove either. I will attempt to remove an apple. Again, tuple object has no attribute remove.

Tuples are good if you need a collection that can't be changed. The benefit of this is that they're faster to access. Then we have sets, which are probably the most complicated.

All your values will be enclosed within a set of curly braces. We can add and remove elements, but we can't replace any of the elements within it. That's because sets are unordered.

So to demonstrate this, I'm going to print my set of fruits. The order is going to change each time. During this first run, we have Coconut, then Apple, Orange, then Banana.

If I run this again, we get Apple, Coconut, Banana, Orange, then Coconut, Banana, Apple, Orange. Sets are unordered. We can't access them by index.

So let's say fruit set index 0 is now a pineapple. Well, sets do not support item assignment. That's because they're unordered.

Whatever's at index 0 within our set is going to change each time. They're unordered. However, we can add and remove elements, and we would use the add method to do that rather than append.

So within our set, let's append a mango. So we have a mango within our set, but the position changes each time. We can remove.

Fruits.remove. Let's remove the coconut. So the coconut's gone. Let's attempt to pop. Fruits.pop at index 0. Again we can't work with indices.

Type error. Pop takes no arguments. And we can still clear.

Fruits.clear. Sets also don't allow for any duplicates. What if I were to add just a bunch of coconuts to the end?

Well, we still only have one coconut. Where sets are really good is for membership testing, checking to see if there's a given value within your set. Here's how we can do that. I'll write an if statement.

If the word apple is in our set of fruits, If this statement is true, then let's print, apple was found. Else, we will print something else. print apple was not found.

So apple is currently within our set, apple was found. But what about a pineapple? Let's change apple to pineapple. Currently there's no pineapple.

Pineapple was not found. We even could accept user input. Let's say fruit equals input. Enter a fruit to search for.

If our fruit variable is in fruits, let's convert these print statements to an f-string. Add a placeholder, fruit. Our fruit was found if this is true, else our fruit was not found.

Enter a fruit to search for. Is there an orange? Orange was found.

Is there a mango? Mango was not found. Sets are efficient for membership testing, meaning checking to see if there's a given element within that set.

Alright everybody, so those are lists, tuples, and sets. Lists are mutable, meaning we can change the elements, and they're the most flexible. Tuples are immutable, but they're faster than lists.

Sets are mutable only when adding and removing elements. They're unordered. We can't access a set by an index.

They don't allow for duplicates. And sets are the best for membership testing. And well everybody, those are lists, tuples, and sets in Python.