Session 1 - Part 2 Flashcards
What are collections of data in Python?
Collections of data in Python are structures designed to store multiple pieces of information.
What are the different types of collections of data in Python? - (3)
- Lists
- Tuples
- Dictionaries
What are lists - (2)?
A list is a collection of data that is ordered and changeable.
It allows you to store multiple items in a single variable.
What is the main difference between a list and a dictionary in Python?
The main difference between a list and a dictionary in Python is that a list is ordered and accessed by index, while a dictionary is unordered and accessed by key.
What are some examples of when you might use a list in Python?
You might use a list in Python to store a series of numbers, a list of names, or the results of an experiment involving multiple trials or participants.
How are elements accessed in a list compared to a dictionary in Python?
In a list, elements are accessed by their index position, starting from 0. In a dictionary, elements are accessed by their key.
How do you create an empty list in Python? - (2)
An empty list in Python can be created using square brackets:
e.g., my_list = []
What function can you use to determine the type of a variable in Python?
The type() function can be used to determine the type or class of a variable in Python.
What does the following code snippet create and print?
my_list = []
print(my_list)
print(type(my_list)) - (2)
The code creates an empty list named my_list and prints the list [].
It also prints the type of the variable my_list, which is list.
How do you add elements to a list in Python?
Elements can be added to a list in Python using the ‘append’ member function
What is the purpose of the append() method in Python?
The append() method in Python is used to add elements to the end of a list.
What does the len() function return when applied to a list?
The len() function returns the number of elements in a list when applied to that list in Python.
What does the following code snippet do? - (2)
my_list = []
my_list.append(100)
my_list.append(105)
my_list.append(120)
print(my_list)
print(len(my_list))
The code creates an empty list named my_list and appends the integers 100, 105, and 120 to it.
Output: Then it prints the list [100, 105, 120] and the length of the list, which is 3
What are member functions in Python? - (2)
Member functions are built-in functions specific to a data structure that allow operations to be performed on that data.
They are called using the syntax: dataStructure.functionName(argument1, …).
What is the purpose of the append() member function in Python lists?
The append() member function adds a single value to the end of a list.
We can use append() many times to add many
elements to a list
‘len’ is a normal function and ‘append’ is a
member function
Produce a list that contain elements from start and use
commas to separate the elements
Lists can contain data of
different types
Example of list containing different data types - (3)
my_new_list = [‘ant’, ‘bear’, ‘hi’,10, 20,50,60, 5.234]
first two elements are strings, and the next two are integers.
As usual, our strings have to be declared using quotes (in this case we used ‘, but we could equally well have used “).
What is the purpose of using quotes when declaring strings within a list? - (2)
my_new_list = [‘ant’, ‘bear’, ‘hi’, 10, 20, 50, 60, 5.234]
Quotes (either single or double) are used to denote string literals within a list.
This helps Python distinguish between string values and other types of data. In the given example, strings like ‘ant’, ‘bear’, and ‘hi’ are enclosed in single quotes to indicate they are string elements of the list.
What will be the output of the code snippet provided? - (4)
my_new_list = [‘ant’, ‘bear’, ‘hi’,10, 20,50,60, 5.234]
print(my_new_list)
print(len(my_new_list))
[‘ant’, ‘bear’, ‘hi’, 10, 20, 50, 60, 5.234]
8
The print(my_new_list) statement will display the contents of the my_new_list list, including strings and integers.
The print(len(my_new_list)) statement will output the length of the list, which is 8, indicating the total number of elements present in the list.
What happens when you append a list to another list in Python?
When you append a list to another list using the append() member function, the entire second list becomes a single element of the first list.
What will be the output of print(my_main_list) after appending my_other_list to it?
my_main_list = [1, 2, 3]
my_other_list = [10, 20]
my_main_list.append(my_other_list)
- (2)
The output of print(my_main_list) will include my_other_list as a single element within my_main_list, preserving its structure
Output: [1, 2, 3, [10, 20]] and 4
Question: What does the following Python code snippet do? - (4)
my_main_list = [1, 2, 3]
my_other_list = [10, 20]
my_main_list.append(my_other_list)
print(my_main_list)
print(len(my_main_list))
This code snippet initializes two lists, my_main_list and my_other_list, with some integer values.
Then, it appends my_other_list as a single element to my_main_list, resulting in my_main_list containing four elements: [1, 2, 3, [10, 20]].
Finally, it prints my_main_list and its length using the len() function.
Since my_main_list is
[1,2,3 [10,20]]) , the length of my_main_list is 4.
What is the difference between using .append() and .extend() to add elements from one list to another in Python? - (2)
When using .append(), the entire second list is added as a single element to the first list.
In contrast, when using .extend(), each element of the second list is added individually to the first list.
How can we add multiple elements from an existing list to another/our new list in Python?
Multiple elements from one list can be added to another list in Python using the .extend() member function.
Why might it be useful to use the .extend() function instead of the .append() function when working with lists in Python?
It might be useful to use the .extend() function instead of the .append() function when working with lists in Python because it allows adding each element of one list individually to another list, rather than adding the entire second list as a single element.
print or len works this way:
functionName(arguement 1, arguement 2)
Once we have a list, one of the obvious things that we want to be able to do is
access individual elements of the list.
What does it mean for Python to be a 0-indexed language?
In Python, being a 0-indexed language means that the first element in any sequence, including lists, is referred to as the “zeroth” element, and indexing starts from 0 rather than 1.
How do you access individual elements of a list in Python?
Individual elements of a list in Python are accessed using square brackets [], with the index of the desired element inside the brackets.
What are the valid indices for a list of length 4 in Python?
For a list of length 4 in Python, the valid indices are 0, 1, 2, and 3.
: What is the output of the given code snippet? - (4)
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list[0])
my_element = my_new_list[2]
print(my_element)
print(type(my_element))
The code initializes a list my_new_list containing four elements: ‘ant’, ‘bear’, 10, and 20.
It then prints the first element of the list (‘ant’).
Next, it assigns the third element of the list (10) to the variable my_element and prints its value.
Finally, it prints the data type of my_element, which is <class ‘int’>
Change the code you just saw (we repeat it again below for you) to set ‘my_element’ to the first entry in the list. What is the type now? - (5)
my_new_list = [‘ant’, ‘bear’, 10, 20]
my_element = my_new_list[xxxx]
print(my_element)
print(type(my_element))
To set my_element to the first entry in my_new_list, replace xxxx with 0 in the code.
The type of my_element will be <class ‘str’> because it will contain the string ‘ant’, which is the first entry in the list my_new_list.
Code:
my_new_list = [‘ant’, ‘bear’, 10, 20]
my_element = my_new_list[0]
print(my_element)
print(type(my_element))
We can extract the element from the list and pass it
print (or other function)
OR
extract elements and put it in a variable
How can you modify an element in a list in Python?
An element in a list in Python can be modified using square bracket indexing syntax, where you specify the index of the element to be modified and assign it a new value.
What is output of the code snippet below? - (3)
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list)
my_new_list[2] = ‘Hello’
print(my_new_list)
[‘ant’, ‘bear’, 10, 20]
[‘ant’, ‘bear’, ‘Hello’, 20]
The first print statement displays the original list [‘ant’, ‘bear’, 10, 20], and the second print statement displays the modified list after changing the third element to ‘Hello’.
What does the code my_new_list[2] = ‘Hello’ do?
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list)
my_new_list[2] = ‘Hello’
print(my_new_list)
The code my_new_list[2] = ‘Hello’ replaces the third element in the list my_new_list with the string ‘Hello’.
The square bracket indexing syntax in lists can also be used to modify
an element in a list
note that Python wraps
indexing around with negative numbers.
How can you access elements from the end of a list in Python? - (2)
In Python, you can access elements from the end of a list using positive indices or by using negative indices.
For example, -1 refers to the last element, -2 refers to the second-to-last element, and so on.
What does the code print(my_new_list[-1]) do? - (2)
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list[-1])
print(my_new_list[-2])
The code print(my_new_list[-1]) prints the last element of the list my_new_list.
This is achieved by using a negative index -1, which refers to the last element of the list.
What is the output of the code snippet provided?
print(my_new_list[-1]) do? - (2)
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list[-1])
print(my_new_list[-2])
20
10
What does the second print statement in the code snippet print(my_new_list[-2]) - (2)
my_new_list = [‘ant’, ‘bear’, 10, 20]
print(my_new_list[-1])
print(my_new_list[-2])
The second print statement in the code snippet print(my_new_list[-2]) prints the second-to-last element of the list my_new_list.
This is achieved by using a negative index -2, which refers to the element before the last element of the list.
What does the indexing notation [start:stop] in Python lists mean? - (2)
The indexing notation [start:stop] in Python lists is used to extract multiple elements from a list.
It specifies a range of indices from the start index (inclusive) to the stop index (exclusive), and returns a sublist containing the elements within that range.
The indexing notation for extracting multiple elements of list uses a
colon ‘:’
The indexing notation for extracting multiple elements of list uses a colon and its pattern is
[start:stop]
as Python is 0-indexed and end-points are exclusive bold text, the element
which has the last number will not be included
Explain the output of the given code snippet.
my_new_list = [‘ant’, ‘bear’, 10, 20]
Extract 1:3, meaning elements 1 and 2. Print the data and the length
print(my_new_list[1:3])
print(len(my_new_list[1:3]))
print(type(my_new_list[1:3]))
The code extracts elements at index 1 and 2 from my_new_list, prints the extracted elements ([‘bear’, 10]), their length (2), and their data type (<class ‘list’>).
Flashcard 3:
Missing start parameter in list slicing
my_new_list = [‘ant’, ‘bear’, 10, 20]
If we miss out the start parameter, the beginning of the list is presumed
print(my_new_list[:2]) # assumes start parameter is 0
Explain the output of the given code snippet – (2)
The code slices my_new_list from the beginning up to index 2 (exclusive), effectively extracting elements at index 0 and 1.
It prints [‘ant’, ‘bear’], assuming the start parameter as 0 when not explicitly provided.
Missing stop parameter in list slicing
my_new_list = [‘ant’, ‘bear’, 10, 20]
If we miss out the stop parameter, the end of the list is presumed
print(my_new_list[2:]) # assuming going right at end, 0 , 1, 10, 20
Describe the output of the given code snippet. - (2)
The code slices my_new_list from index 2 to the end, effectively extracting elements starting from index 2 to the end of the list.
It prints [10, 20], assuming the stop parameter is at the end of the list when it’s not explicitly provided.
Using the same number as start and stop in list slicing - (2)
python
Copy code
my_new_list = [‘ant’, ‘bear’, 10, 20]
If we use the same number as start and stop, we end up with an empty list
print(my_new_list[1:1]) # ‘[]’
What is the output of the given code snippet and why?
The code attempts to slice my_new_list from index 1 to index 1, resulting in an empty list [].
This happens because when the start and stop parameters are the same, no elements are included in the slice.
Assigning sliced data to a variable
my_new_list = [‘ant’, ‘bear’, 10, 20]
Extract 0:2, meaning elements 0 and 1. Put in a variable
my_data = my_new_list[0:2]
print(my_data)
What does the given code snippet do? - (2)
The code extracts elements at index 0 and 1 from my_new_list and assigns them to a new variable my_data.
It then prints the content of my_data, which would be [‘ant’, ‘bear’]