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
The Stock Market's Valuation is Getting Ridiculous...
It’s no secret that the stock market is currently overvalued, but what should we as investors do about it? I have a look at this chart, which is tracking a metric called the Shiller PE. This metric was created by the American economist Robert Shiller, who…
Ray Dalio & Bill Belichick on Going from Nothing to Something Big: Part 2
And then communicating well, because when I went from the 67 who knew me and knew where I was coming from, and then I had to go to another level, how was I going to keep that same communication, that same idea of meritocracy? That’s why I started to write…
Are Guitars Worth Investing In? | Walt Grace PT III
People come from all over the world to come here. It’s a destination. You’re not a typical guitar retail store. There’s nothing like this I’ve ever seen. What is this thing in this case? That’s also a Martin. This one is twenty thousand dollars. Uh, yeah,…
Angela Bassett on the Water Problem | Breakthrough
A beautiful Earth is covered roughly 70% with water, but only 1% of that is usable by humans for consuming. Water is one of those elements that we need to exist, like oxygen. Coming to this project, one of the things that I’ve learned is that there’s no o…
Vitalik: Ethereum, Part 1
All right, welcome everybody back to the podcast. We have with us Haseeb Qureshi, who’s our partner at Dragonfly. Haseeb and I used to work together back when I was more active in crypto land. Vitalik is, of course, a polymath ingenue, although he may bri…
How to Make a Friction Fire | Live Free or Die: DIY
[Music] I want to talk to you a little bit about friction fire. The tools that you need are: you need your hearth board, you need a spindle, and you also need a nest. With the nest, I like to start out with my longer fibers, and I’ll just twist those arou…