[go: up one dir, main page]

0% found this document useful (0 votes)
83 views5 pages

Lists Defined

Here is the solution using a list comprehension: def squares(start, end): return [n*n for n in range(start, end+1)] print(squares(2, 3)) print(squares(1, 5)) print(squares(0, 10)) This defines a function called squares that takes in start and end parameters. It returns a list comprehension with the expression n*n (to square each value of n) and a for loop iterating over the range from start to end+1. Running it prints the expected output lists of squared numbers between the given ranges.

Uploaded by

Nurmuslimahhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views5 pages

Lists Defined

Here is the solution using a list comprehension: def squares(start, end): return [n*n for n in range(start, end+1)] print(squares(2, 3)) print(squares(1, 5)) print(squares(0, 10)) This defines a function called squares that takes in start and end parameters. It returns a list comprehension with the expression n*n (to square each value of n) and a for loop iterating over the range from start to end+1. Running it prints the expected output lists of squared numbers between the given ranges.

Uploaded by

Nurmuslimahhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lists Defined

Lists in Python are defined using square brackets, with the elements stored in the list separated by
commas: list = ["This", "is", "a", "list"]. You can use the len() function to return the number of
elements in a list: len(list) would return 4. You can also use the in keyword to check if a list contains a
certain element. If the element is present, it will return a True boolean. If the element is not found in
the list, it will return False. For example, "This" in list would return True in our example. Similar to
strings, lists can also use indexing to access specific elements in a list based on their position. You can
access the first element in a list by doing list[0], which would allow you to access the string "This".

In Python, lists and strings are quite similar. They’re both examples of sequences of data. Sequences
have similar properties, like (1) being able to iterate over them using for loops; (2) support indexing; (3)
using the len function to find the length of the sequence; (4) using the plus operator + in order to
concatenate; and (5) using the in keyword to check if the sequence contains a value. Understanding
these concepts allows you to apply them to other sequence types as well.

Modifying Lists
While lists and strings are both sequences, a big difference between them is that lists are mutable. This
means that the contents of the list can be changed, unlike strings, which are immutable. You can add,
remove, or modify elements in a list.

You can add elements to the end of a list using the append method. You call this method on a list using
dot notation, and pass in the element to be added as a parameter. For example, list.append("New
data") would add the string "New data" to the end of the list called list.

If you want to add an element to a list in a specific position, you can use the method insert. The method
takes two parameters: the first specifies the index in the list, and the second is the element to be added
to the list. So list.insert(0, "New data") would add the string "New data" to the front of the list. This
wouldn't overwrite the existing element at the start of the list. It would just shift all the other elements
by one. If you specify an index that’s larger than the length of the list, the element will simply be added
to the end of the list.

You can remove elements from the list using the remove method. This method takes an element as a
parameter, and removes the first occurrence of the element. If the element isn’t found in the list, you’ll
get a ValueError error explaining that the element was not found in the list.

You can also remove elements from a list using the pop method. This method differs from the remove
method in that it takes an index as a parameter, and returns the element that was removed. This can
be useful if you don't know what the value is, but you know where it’s located. This can also be useful
when you need to access the data and also want to remove it from the list.

Finally, you can change an element in a list by using indexing to overwrite the value stored at the
specified index. For example, you can enter list[0] = "Old data" to overwrite the first element in a list
with the new string "Old data".
Tuples
As we mentioned earlier, strings and lists are both examples of sequences. Strings are sequences of
characters, and are immutable. Lists are sequences of elements of any data type, and are mutable. The
third sequence type is the tuple. Tuples are like lists, since they can contain elements of any data type.
But unlike lists, tuples are immutable. They’re specified using parentheses instead of square brackets.

You might be wondering why tuples are a thing, given how similar they are to lists. Tuples can be useful
when we need to ensure that an element is in a certain position and will not change. Since lists are
mutable, the order of the elements can be changed on us. Since the order of the elements in a tuple
can't be changed, the position of the element in a tuple can have meaning. A good example of this is
when a function returns multiple values. In this case, what gets returned is a tuple, with the return
values as elements in the tuple. The order of the returned values is important, and a tuple ensures that
the order isn’t going to change. Storing the elements of a tuple in separate variables is called
unpacking. This allows you to take multiple returned values from a function and store each value in its
own variable.

Iterating Over Lists Using Enumerate


When we covered for loops, we showed the example of iterating over a list. This lets you iterate over
each element in the list, exposing the element to the for loop as a variable. But what if you want to
access the elements in a list, along with the index of the element in question? You can do this using the
enumerate() function. The enumerate() function takes a list as a parameter and returns a tuple for each
element in the list. The first value of the tuple is the index and the second value is the element itself.

List Comprehension Examples


You can create a list from a sequence using a for loop, but there’s a more streamlined way
to do this by using a list comprehension. List comprehensions allow you to create a new
list from a sequence or a range in a single line.

Simple List Comprehension


For example, [ x*2 for x in range(1,11) ] is a simple list comprehension. This single line of
code iterates over a range from 1 to 10, multiplies each element in the range by 2, and
creates a new list from all multiples of 2 from 2 to 20.

### Simple List Comprehension

print("List comprehension result:")

# The following list comprehension compacts several lines

# of code into one line:

print([x*2 for x in range(1,11)])


### Long form for loop

print("Long form code result:")

# The list comprehension above accomplishes the same result as

# the long form version of the code:

my_list = []

for x in range(1,11):

my_list.append(x*2)

print(my_list)

# Click Run to compare the two results.

List Comprehension with Conditional


Statement
You can also use conditionals with list comprehensions to build even more complex and
powerful statements. You can do this by appending an if statement to the end of the list
comprehension. For example, [ x for x in range(1,101) if x % 10 == 0 ] generates a new
list containing all the integers divisible by 10 from 1 to 100. The if statement evaluates
each value in the range from 1 to 100 to check if it’s evenly divisible by 10. If it is, the
number is added to a new list.

### List Comprehension with Conditional Statement

print("List comprehension result:")

# The following list comprehension compacts multiple lines

# of code into one line:

print([ x for x in range(1,101) if x % 10 == 0 ])


### Long form for loop with nested if-statement

print("Long form code result:")

# The list comprehension above accomplishes the same result as

# the long form version of the code:

my_list = []

for x in range(1,101):

if x % 10 == 0:

my_list.append(x)

print(my_list)

# Click Run to observe the two results.

RunReset

List comprehensions can be really powerful, but they can also be complex, resulting in
code that’s hard to read. Be careful when using them, since it might make it more difficult
for someone else looking at your code to easily understand what the code is doing. It is a
best practice to add descriptive comments about any list comprehensions used in your
code. This helps to communicate the purpose of list comprehensions to other coders.
Comments will also help you remember the goal of the code when performing future code
additions and maintenance.

Practice exercise

This exercise will walk you through how to write a list comprehension to create a list of
squared numbers (n*n). It needs to return a list of squares of consecutive numbers
between “start” and “end” inclusively. For example, squares(2, 3) should return a list
containing [4, 9].

1. The function receives the variables “start” and “end” through the function
parameters.
2. In the return line, start by entering the list brackets [ ]
3. Between the brackets [ ], enter the arithmetic expression to square a variable “n”.
4. To the right of the square expression, write a for loop that iterates over “n” in a
range from the “start” to “end” variables.
5. Ensure the “end” range value is included in the range() by adding 1 to it.
6. Run your code to see if it works! If needed, the solution to this code is included in
the “Study Guide: List Operations and Methods” reading under “Skill Group 2” (list
comprehensions).
def squares(start, end):

return ___

print(squares(2, 3)) # Should print [4, 9]

print(squares(1, 5)) # Should print [1, 4, 9, 16, 25]

print(squares(0, 10)) # Should print [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

You might also like