0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Python has built-in lists, so the language provides a special syntax for creating lists called<strong>list literals</strong>. For example, [1, 2] is a list literal.</p>
1
<p>Python has built-in lists, so the language provides a special syntax for creating lists called<strong>list literals</strong>. For example, [1, 2] is a list literal.</p>
2
<p>The tuples described earlier are also built into the language and created with their literals. This expression in parentheses, ("foo", 42), is a tuple literal.</p>
2
<p>The tuples described earlier are also built into the language and created with their literals. This expression in parentheses, ("foo", 42), is a tuple literal.</p>
3
<p>We can create multiple lists, including an empty one:</p>
3
<p>We can create multiple lists, including an empty one:</p>
4
<p>So far, everything looks like tuples, except that the parentheses are square instead of round.</p>
4
<p>So far, everything looks like tuples, except that the parentheses are square instead of round.</p>
5
<p>But in the last lesson, we mentioned that lists are modifiable. They can change over time. Let's learn how to modify lists by adding elements to the end:</p>
5
<p>But in the last lesson, we mentioned that lists are modifiable. They can change over time. Let's learn how to modify lists by adding elements to the end:</p>
6
<h2>Lists as objects</h2>
6
<h2>Lists as objects</h2>
7
<p>The above example demonstrates a new type of syntax, the<strong>object method call</strong>.</p>
7
<p>The above example demonstrates a new type of syntax, the<strong>object method call</strong>.</p>
8
<p><strong>Objects</strong>are entities that can store data, including other objects. Also, objects know how to handle the data in them. Programmers say that objects have their<strong>behavior</strong>.</p>
8
<p><strong>Objects</strong>are entities that can store data, including other objects. Also, objects know how to handle the data in them. Programmers say that objects have their<strong>behavior</strong>.</p>
9
<p>The behavior of an object is to provide<strong>methods</strong>- functions related to the owner object in some way. A method call is similar to a function call. The only difference is that it looks like object.method(...), which means we can always see which object's method we call.</p>
9
<p>The behavior of an object is to provide<strong>methods</strong>- functions related to the owner object in some way. A method call is similar to a function call. The only difference is that it looks like object.method(...), which means we can always see which object's method we call.</p>
10
<p>We will discuss<strong>Object Oriented Programming</strong>in a different course. For now, it is enough to know that methods are like functions - they can modify objects or return information about them.</p>
10
<p>We will discuss<strong>Object Oriented Programming</strong>in a different course. For now, it is enough to know that methods are like functions - they can modify objects or return information about them.</p>
11
<h2>The append and extend methods</h2>
11
<h2>The append and extend methods</h2>
12
<p>So in the code above, list l is the list object, and append and extend are methods of the list object:</p>
12
<p>So in the code above, list l is the list object, and append and extend are methods of the list object:</p>
13
<ul><li>The append adds an element to its list</li>
13
<ul><li>The append adds an element to its list</li>
14
<li>The extend adds all elements of the argument list to its list</li>
14
<li>The extend adds all elements of the argument list to its list</li>
15
</ul><p>Both methods add items to the end of the list. And both of them return None, meaning they have no result we can use. The point of calling these methods is to modify the associated object.</p>
15
</ul><p>Both methods add items to the end of the list. And both of them return None, meaning they have no result we can use. The point of calling these methods is to modify the associated object.</p>
16
<p>Beginners often make this mistake:</p>
16
<p>Beginners often make this mistake:</p>
17
<p>Here, on line 2, the hypothetical author wanted to store the expanded list in the variable l, but the variable ended up with the value None which was returned by append.</p>
17
<p>Here, on line 2, the hypothetical author wanted to store the expanded list in the variable l, but the variable ended up with the value None which was returned by append.</p>
18
<p>Note that a method can always return None and change its associated object.</p>
18
<p>Note that a method can always return None and change its associated object.</p>