Python Flashcards

Learn Python

1
Q

What are the four data types in Python?

A

Lists, Tuples, Sets, and Dictionaries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a List?

A

a collection which is ordered and changeable. Allows duplicate members. []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Tuple?

A

a collection which is ordered and unchangeable. Allows duplicate members.
( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Set?

A

a collection which is unordered and unindexed. No duplicate members. { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Dictionary?

A

a collection which is unordered, changeable and indexed. No duplicate members. {A: B }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you access a List item?

A

list_name[Index]

Example:

thislist = [“apple”, “banana”, “cherry”]

print(thislist[1])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you change an item in a list’s value?

A

list_name[Index] = “New Value”

Example:
thislist = [“apple”, “banana”, “cherry”]
thislist[1] = “blackcurrant”
print(thislist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you loop through a List?

A

for x in list_name:
print(x)

Example:
thislist = [“apple”, “banana”, “cherry”]
for x in thislist:
print(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you check if an item exists in a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you find the list’s length?

A

print(len(list_name))

Example:
thislist = [“apple”, “banana”, “cherry”]
print(len(thislist))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you add items to the end of a list?

A

Use .append( ) Method

list_name.append(value)

Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.append(“orange”)
print(thislist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you add items to a list at a specified index?

A

Use .insert( ) Method

list_name.insert(index, value)

Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.insert(1, “orange”)
print(thislist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you remove a specific item from a List?

A

Use .remove( ) Method

list_name.remove(value)

Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.remove(“banana”)
print(thislist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you remove an item at a specified index?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you delete a specific index from a list using a keyword?

A

Use the del keyword

del list_name[index]

Example:
thislist = [“apple”, “banana”, “cherry”]
del thislist[0]
print(thislist)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you delete a whole list using a keyword?

A

Use the del keyword

del list_name

Example:
thislist = [“apple”, “banana”, “cherry”]
del thislist

17
Q

How do you empty a list?

A

Use the clear( ) Method

list_name.clear( )

Example:
thislist = [“apple”, “banana”, “cherry”]
thislist.clear()
print(thislist)

18
Q

How do you copy a list? (two ways)

A

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)

19
Q

How do you make a new list?

A

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)

20
Q

Name the built-in methods for Lists

A

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