yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Python | Lesson 2


5m read
·Nov 3, 2024

Hey guys! It's Makinson on their second Python tutorial. In this Python tutorial, I'm going to be teaching you something called lists, which are variables that can contain multiple variables inside of them, and loops, which is something that allows you to run code more than one time.

So let's get started by editing our script. I'm going to call it lists.py, and this is going to be a script that demonstrates lists in Python. Before you start using lists, you have to understand what they're important for.

Let's say we're going to keep on getting people to type in things to this thing, and I'll compose a list of different things like these people typed in. That's where you would want to use a list. A better example of that is a script I wrote that we're going to be able to make by the end of this tutorial, that will do just that.

It'll say, "Answer name, enter another name, and enter another name." Then, when you hit enter without typing a name, it prints out the list of names. So this is basically a list right there. To make a list, you do list_name = list().

Lists are just like other variables, so declaring them is the same. Let's say you want to add something to this list. Now you say list1.append("first_object"). Obviously, this will add "first_object" to list1. You can think of it as a big stack of items, basically, and we can append to it again with "second_object".

You can go as far as "third_object". You can really put as many objects in a list as you want to, but I'm going to be mostly using small amounts of things in lists. So here's our list; it's going to have "creative".

Now, if we want to print it out, just do print(list1). So now turn this to "first_object" like "object", "third_object". Notice how it's in brackets, and all the objects are separated by commas. This is useful for what we just observed.

Instead of doing list(), let's declare it a different way: [item1, item2, item3]. Then I can add on to "first_object", "second_object", "third_object", etc., and this will say them. So basically, when you're declaring a list that you don't want to start out empty, you can do it this way.

Also, another thing you might want to do with the list is see what's at a place in the list. Let's say you want to see the "first_object" in a list. You can say print(list[0]). "0" means one; you have to think of things that way in Python.

The first object in the list is "item1". We want to get the second object, we can do list[1], so just think of subtraction; you're subtracting one basically. Here's the problem with doing it this way: your list has seven objects or six objects.

So if you want to get the thing at the seventh place in the list, it won't work at all. If we try to get the sixth thing, because six means seven, this will not work at all. You'll see "list index out of range." That's because the list doesn't have a sixth object; it only has zero to five.

If we want to make this safe, we can get the length of a list by doing this line of code: print(len(list1)), and this will get the length of the list in a variable that's an integer. At this point, we see the length is six, and then we're still getting the error because we're trying to access the sixth index of the list.

So at this point, you guys know how to do an if statement to check if the place I'm trying to get is greater than len(list), or something like that. You could print out your own error.

So that is lists. Now let's do a loop. A loop is essentially like an if statement, except slightly different, and I'm talking about while loops here.

So here's the while loop we're going to be doing. We start out making a variable i that's 0, and we do while i < 10. We're printing out "less than 10." If this were to be an if statement, it would print out "less than 10" because 0 was definitely less than 10.

Now, let's note the difference between if statements and while loops: a while loop runs the code inside of it, then it goes back up if this is still true, and does it again and again and again. So right now, if you run this, it will just continue running because we aren't changing i, so it's always going to be true.

We're going to change it. We're going to say i = i + 1. This will add 1 to i. Here we got it printed out exactly ten times because we're doing it while i is less than 10, and it starts out at 0 and keeps on adding every time.

This is an example of the loop. From here, we should be able to make the program I demonstrated. So you're going to say name = "" and list1 = [].

Alright, we're going to now change this to while name != "". What an empty string is is a piece of text that has nothing in it. So if you do a raw input and they don't type in anything, they just hit enter, then it will be "" essentially.

While their name isn't "", it will continue running this code. Now we're going to say, "Enter your name." Now we're going to say name = raw_input(), and I'm going to say list1.append(name).

So what this is going to do right now is going to start name out as nothing. Then it's going to ask them to enter their name. They're going to enter either their name or just hit enter, and no matter what they do, it'll add what they just did to the list.

What we're going to do just to be safe is we're going to say if name != "" then we'll run this code. This way, it doesn't add something that's nothing to this list.

Once they do type something in, after it's done with this, it'll exit the loop because name will be "". Now we can print out list1. If you run this now, I want your names.

I'll say "Alex", "Joe", "Bob." Alright, we got "Alex", "Joe", "Bob." So this prints out "People are ..." but to do this, we need to quote people = "People are " + str(list1).

You think this would work, but list isn't a string, so you can't add it to a string. So to convert it to a string, you do str(list1). This will convert a list to a string.

I have your names: Alex, Joe, Bob. So this brings it out, "People are blah..." So, this is our first program using loops and lists.

If you have any feedback, any things you didn't get, or anything you want to try using this information or similar information, just comment on the video and leave your responses.

Thanks for watching my tutorial. Subscribe and goodbye!

More Articles

View All
You Won't Get Rich Renting Out Your Time
Next, you go into more specific details on how you can actually get rich and how you can’t get rich. The first point was about how you’re not going to get rich. You’re not going to get rich renting off your time. You must own equity, a piece of a business…
How the Kushites Took Over Egypt | Flooded Tombs of the Nile
[tense music] Nuri is one of the most intensive concentrations of pyramids anywhere in the world, across any culture and civilization. [upbeat music] In Sudan, in fact, there are more pyramids than in Egypt. And this is something that people don’t think …
Introducing Khanmigo for teachers
This is Conmigo, an AI-powered guide designed to help all students learn when subjects are giving them trouble. Conmigo can help. Kamika was fun and can transform learning into an adventure. Kanmigo is not just for students; teachers can use it too by tog…
The 3 Pillars Of Stoicism Explained
If we look at Stoicism as a philosophical system, we can divide it in three pillars: Ethics, Logic and Physics. Some scholars arrange these aspects of Stoicism in the form of an egg. The yolk represents the Physics; the white, the Ethics; and the shell th…
How to buy a private jet
This is 100% scale of the G stream 550. That’s the three sections I’m talking about. You got the kitchen door here, the galley door, and most airplanes the front section has these four single seats. This is really the normal setup. Again, the normal setu…
Absurdism: Life is Meaningless
Sisyphus was a great king of Greek mythology. So clever, he was able to outwit the gods themselves. Twice he cheated death; first by capturing Thanatos, the god of death, then by tricking the goddess of the underworld, Persephone, into releasing him back …