Your programs can also have a lot of lines due to all the variables you have. If you have a bunch of variables you want to print or whatever at once, you can put them in a list. Lists are variables that hold more than one value. Here's an example:
family=['Jack', 'Tim', 'Mom', 'Dad', 'Willy', 'Betty']
This grouped your family names together. It can be a quick way of printing things. Here's what you should get if you say:
print(family)
['Jack', 'Tim', 'Mom', 'Dad', 'Willy', 'Betty']
So, just plain printing lists might not be a wise idea for your programs. But you can print them out one by one, like this:
print(family[0:2])
['Jack', 'Tim']
Sometimes that just happens. But, instead of starting at 1, lists start at 0. If you tell Python to print family from 1 to 2, you would get this:
['Tim']
So, Jack is 0, Tim is 1, Mom is 2, Dad is 3, Willy is 4, and Betty is 5. However, you didn't get Mom in the list! Remember, in lists, you don't get the last one when you print it. You would only get 4 of them if you typed "print(family[0:5])."
If you don't want the square brackets or apostrophes (or quotes, if you used those), type this:
print(family[1])
Tim
You can only do one at a time, though, if you don't want those. When you put more than one number there, it gives you that. Look at the difference here:
print(family[1:2])
['Tim']
When you put that other number there, it thinks you want it to display more than 1 item from the list. Only putting one number, though, tells Python that you only want 1 thing displayed there.
The first time, we got back an item or something from the list. The second time, we got back the list with that item. A single index (Index means the position of something. The plural of index is indices, but some people also use indexes as the plural. EX: If you're 7th in line, your index in line is 7. But if you're the 7th in a Python list, your index is 6, because Python list indices start at 0.) was used the first time to get 1 thing out of the list. The second time, we used something called slice notation (slicing a list is when you get only part of it, like when you typed "print(family[0:2])." A slice notation is the square brackets.) to get a 1-item slice of the list. More will be told about lists in the next article.
Another thing to make programming easier is making your lines shorter. If you get a really long line and need to scroll to the side to read it or fix it, you can make it smaller with the back slash character, which is \. In a program with a long line, just put a \ wherever then press Enter to make that line shorter. Keep making those back slashes until you think that line is divided up enough. However, that doesn't work too well with the print statement. If you want to make one of those shorter, you will have to put end="" where you think it is appropriate to make the line shorter. That end="" lets the program know the print statement isn't finished yet. However, you can't put that around the quotes. Something you should have figured out from the last article is that you can put variables and other things in between the print statement. After the quotes, put a comma, then your variable or math problem or whatever, then put a comma after that, then you can continue the string.
Now, there's a quick function I must name: type(). This is a function that tells you the class of things. Type() is a simple and sort of useless function. Let me show you how to use it:
type("Bob")
<class 'str'>
So, when we put "Bob" in the parenthesis of this function, it told us that it is a string. Let's try some others:
type(3)
<class 'int'>
type("3")
<class 'str'>
What happened here? Well, anything that is put in quotes or apostrophes, Python takes as a string. Now, let's try this function with our list:
type(family)
<class 'list'>
Well, we already new that was a list, but type will tell you the class of anything. Now, what about the items in the list?
type(family[4])
<class 'str'>
Like I said, everything in quotes or apostrophes are strings. Well, I guess that wraps up the type() function.
So, in this article, you learned about comments, lists, and shorter lines. Try these to test what you know:
Create comments in the Multiplier program from the last article. Make the first print statement shorter. You don't have to try anything with lists if you don't want to, since you've experimented a lot in this article.
Sometimes that just happens. But, instead of starting at 1, lists start at 0. If you tell Python to print family from 1 to 2, you would get this:
['Tim']
So, Jack is 0, Tim is 1, Mom is 2, Dad is 3, Willy is 4, and Betty is 5. However, you didn't get Mom in the list! Remember, in lists, you don't get the last one when you print it. You would only get 4 of them if you typed "print(family[0:5])."
If you don't want the square brackets or apostrophes (or quotes, if you used those), type this:
print(family[1])
Tim
You can only do one at a time, though, if you don't want those. When you put more than one number there, it gives you that. Look at the difference here:
print(family[1:2])
['Tim']
When you put that other number there, it thinks you want it to display more than 1 item from the list. Only putting one number, though, tells Python that you only want 1 thing displayed there.
The first time, we got back an item or something from the list. The second time, we got back the list with that item. A single index (Index means the position of something. The plural of index is indices, but some people also use indexes as the plural. EX: If you're 7th in line, your index in line is 7. But if you're the 7th in a Python list, your index is 6, because Python list indices start at 0.) was used the first time to get 1 thing out of the list. The second time, we used something called slice notation (slicing a list is when you get only part of it, like when you typed "print(family[0:2])." A slice notation is the square brackets.) to get a 1-item slice of the list. More will be told about lists in the next article.
Another thing to make programming easier is making your lines shorter. If you get a really long line and need to scroll to the side to read it or fix it, you can make it smaller with the back slash character, which is \. In a program with a long line, just put a \ wherever then press Enter to make that line shorter. Keep making those back slashes until you think that line is divided up enough. However, that doesn't work too well with the print statement. If you want to make one of those shorter, you will have to put end="" where you think it is appropriate to make the line shorter. That end="" lets the program know the print statement isn't finished yet. However, you can't put that around the quotes. Something you should have figured out from the last article is that you can put variables and other things in between the print statement. After the quotes, put a comma, then your variable or math problem or whatever, then put a comma after that, then you can continue the string.
Now, there's a quick function I must name: type(). This is a function that tells you the class of things. Type() is a simple and sort of useless function. Let me show you how to use it:
type("Bob")
<class 'str'>
So, when we put "Bob" in the parenthesis of this function, it told us that it is a string. Let's try some others:
type(3)
<class 'int'>
type("3")
<class 'str'>
What happened here? Well, anything that is put in quotes or apostrophes, Python takes as a string. Now, let's try this function with our list:
type(family)
<class 'list'>
Well, we already new that was a list, but type will tell you the class of anything. Now, what about the items in the list?
type(family[4])
<class 'str'>
Like I said, everything in quotes or apostrophes are strings. Well, I guess that wraps up the type() function.
So, in this article, you learned about comments, lists, and shorter lines. Try these to test what you know:
Create comments in the Multiplier program from the last article. Make the first print statement shorter. You don't have to try anything with lists if you don't want to, since you've experimented a lot in this article.