Python Flashcards
Learn Python
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)