Python Flashcards
Learn Python (20 cards)
What are the four data types in Python?
Lists, Tuples, Sets, and Dictionaries
What is a List?
a collection which is ordered and changeable. Allows duplicate members. []
What is a Tuple?
a collection which is ordered and unchangeable. Allows duplicate members.
( )
What is a Set?
a collection which is unordered and unindexed. No duplicate members. { }
What is a Dictionary?
a collection which is unordered, changeable and indexed. No duplicate members. {A: B }
How do you access a List item?
list_name[Index]
Example:
thislist = [“apple”, “banana”, “cherry”]
print(thislist[1])
How do you change an item in a list’s value?
list_name[Index] = “New Value”
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist[1] = “blackcurrant”
print(thislist)
How do you loop through a List?
for x in list_name:
print(x)
Example:
thislist = [“apple”, “banana”, “cherry”]
for x in thislist:
print(x)
How do you check if an item exists in a list?
if in
if “Carol”/2/22.3,etc. in list_name:
print(“Carol”/2/22.3,etc.)
Example:
thislist = [“apple”, “banana”, “cherry”]
if “apple” in thislist:
print(“Yes, ‘apple’ is in the fruits list”)
How do you find the list’s length?
print(len(list_name))
Example:
thislist = [“apple”, “banana”, “cherry”]
print(len(thislist))
How do you add items to the end of a list?
Use .append( ) Method
list_name.append(value)
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.append(“orange”)
print(thislist)
How do you add items to a list at a specified index?
Use .insert( ) Method
list_name.insert(index, value)
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.insert(1, “orange”)
print(thislist)
How do you remove a specific item from a List?
Use .remove( ) Method
list_name.remove(value)
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.remove(“banana”)
print(thislist)
How do you remove an item at a specified index?
Use the .pop( ) Method
list_name.pop(index value)
Note: if no index is inputted then it will be the last time of the list deleted.
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.pop()
print(thislist)
How do you delete a specific index from a list using a keyword?
Use the del keyword
del list_name[index]
Example:
thislist = [“apple”, “banana”, “cherry”]
del thislist[0]
print(thislist)
How do you delete a whole list using a keyword?
Use the del keyword
del list_name
Example:
thislist = [“apple”, “banana”, “cherry”]
del thislist
How do you empty a list?
Use the clear( ) Method
list_name.clear( )
Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.clear()
print(thislist)
How do you copy a list? (two ways)
Use the copy( ) Method
duplicated_list = original_list_name.copy( )
or
Use the list( ) Method
duplicated_list = list(originial_list_name)
Note: You cannot copy a list by typing list2 = list 1. This is because list2 will only be a reference to list1 and changes made in list1 will automatically also be made in list2.
.copy( ) Example:
thislist = [“apple”, “banana”, “cherry”]
mylist = thislist.copy()
print(mylist)
.list( ) Method Example:
thislist = [“apple”, “banana”, “cherry”]
mylist = list(thislist)
print(mylist)
How do you make a new list?
Using the list( ) constructor
list((value,value,value,value))
Note: The double brackets
Example:
thislist = list((“apple”, “banana”, “cherry”)) # note the double round-brackets
print(thislist)
Name the built-in methods for Lists
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list