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