Python

Python Menu

A list is, much like a string, a sequence of values. In a string, the values are characters; they may be of any type in a sequence. Elements or sometimes items are called the values in a list.

There are a variety of ways to create a new list; the easiest is to put the elements in square brackets ([and]):

[10, 11, 12, 13]
['jobs', 'skills', 'software']

The first example is a list of four integers. The second is a list of three strings. The elements of a list don't have to be the same type. The following list contains a string, a float, an integer, and another list:

['mytext', 3.0, 7, [10, 20]]

A list within another list is called a nested list

A list that contains no elements is called an empty list; you can create one with empty brackets, [].

As expected, you can assign list values to variables:

software= ['Autocad', 'Photoshop', 'Indesign'] numbers = [45, 341] empty = []
print(software, numbers, empty)

Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. Here is an example of how to build a list.

mylist = [] mylist.append('hello') mylist.append('world') mylist.append('goodbye') print(mylist[0]) # prints hello print(mylist[1]) # prints world print(mylist[2]) # prints goodbye
# prints out hello, world, goodbye for x in mylist: print(x)

Accessing an index which does not exist generates an error.

mylist = [1,2,3] print(mylist[10])

Exercise

In this exercise, you will need to add numbers and strings to the correct lists using the "append" list method. You must add the numbers 1,3, and 7 to the "my_numbers" list, and the words 'hello' and 'world' to the my_strings variable.

You will also have to fill in the variable second_name with the second name in the names list, using the brackets operator []. Note that the index is zero-based, so if you want to access the second item in the list, its index will be 1.

my_numbers = [] my_strings = [] my_names = ["Raymond", "Ivan", "Rich"]
# write your code here second_name = None
# this code should write out the filled arrays and the second name in the names list (Eric). print(my_numbers) print(my_strings) print("The second name on the names list is %s" % second_name)
my_numbers = [] my_strings = [] my_names =["Raymond", "Ivan", "Rich"]
# write your code here my_numbers.append(1) my_numbers.append(3) my_numbers.append(7)
my_strings.append("hello") my_strings.append("world")
second_name = my_names[1] # this code should write out the filled arrays and the second name in the names list (Ivan). print(my_numbers) print(my_strings) print("The second name on the names list is %s" % second_name)
test_object('my_numbers', incorrect_msg="Don't forget to change `my_numbers` to the correct value as from the instructions.") test_object('my_strings', incorrect_msg="Don't forget to change `my_strings` to the correct value as from the instructions.") test_object('second_name', incorrect_msg="Don't forget to change `second_name` to the correct value as from the instructions.") test_output_contains("[1, 3, 7]",no_output_msg= "Make sure your string matches exactly as from the instructions.") test_output_contains("['hello', 'world']",no_output_msg= "Make sure your float matches exactly as from the instructions.") test_output_contains("The second name on the names list is Ivan",no_output_msg= "Make sure your integer matches exactly as from the instructions.") success_msg("Excellent!")

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods