Vous êtes sur la page 1sur 21

Lists and Tuples

in Python
Girls Who Code
Lists and Tuples
Lists and tuples are different types of data
collections.

Lists are ordered and dynamic (changeable), while


tuples are ordered and static (unchangeable).
Lists
Outside of Python, lists are also known as arrays.
They are written with square brackets.
Accessing Items in Lists
Each item in a list is assigned an index number.
To access an item in a list, refer to its index number:
Changing the
Value of an Item
To change the value of an item in a list, refer again to
the name of the list and the value’s index number:
To print out the entire list with brackets and single
quotation marks, write “print (nameOfList)” :
Python List Methods

https://www.programiz.co
m/python-programming/
methods/list
Tuples
Tuples are collections of data that are ordered
and unchangeable. They are initialized and
declared using parentheses rather than brackets.

Creating tuples:
Items in Tuples are
Unchangeable
Once you create a tuple and set its values, you
cannot change the value of the items.
Accessing Items in Tuples
To access or call on items in a tuple, refer to the
index number inside square brackets.
Tuple Functions

Check
To check if an item exists in
a tuple, use the word “in”
with “if” statements.
Tuple Length
To get the length of a
tuple, use the statement
“len ( thisTuple )”
Other Tuple Methods
Count Method
Index Method
Python Tuple Lessons and More

https://www.w3schools.co
m/python/python_tuples.a
sp
Referenced Websites

- https://www.w3schools.com/python/python_lists.asp
- https://www.w3schools.com/python/python_tuples.asp
-
Lists and Tuples
del nameOfList [ 0 ]

nameOfList.pop ( )

- remove an item from a list at a specified index, or the last item


of a list if index is not specified

- remove the specified item


- return a check if an item exists
List Methods
nameOfList.append (“lemon”)

- add an item to the end of the list

nameOfList.insert (1, “pear”)

- insert an item at a specific position

print ( len (nameOfList) )

- return the list length


Lists and Tuples
Lists and tuples are different types of data
collections.

Lists are ordered and dynamic (changeable), while


tuples are ordered and static (unchangeable).
Generating Random
Numbers on Python
In Python, the group of functions that produces random numbers is
called “import random”.

In this example, the code outputs a random number between


1 and 5 using the function “ random.randint ”.

Vous aimerez peut-être aussi