Part B: Python Flashcards
How to execute code in a Jupyter Notebook. What happens when you press SHIFT+ENTER
To execute code in a Jupyter Notebook, press shift and enter at the same time
How do you assign a value to a variable in Python?
Define a variable and equal it to a number, string, list, or dictionary
Number: x = 5
String: name = “Alice”
List: numbers = [1, 2, 3, 4, 5]
Dictionary: person = {“name”: “Bob”, “age”: 30}
Differences between regular variable, list, and dictionary?
Regular variables in Python are used to store single data points. A variable can hold data of any type, like integers, floating-point numbers, strings, or even complex objects.
List can store more than one element.
A dictionary in Python is an unordered collection of data in a key:value pair form. Dictionary stores more than one element and is able to make a link between elements.
How would you ensure the input data is a string?
data = input(“Enter something: “)
data = str(data) # Ensures that data is a string
print(data)
OR
print(type(“input”)) # Prints the type of the input
How to define strings in Jupyter?
Put the data in quotation mark
Welcome = “Welcome to my exam”
How can you determine the type of a variable in Python?
Use the type function
print(type(“variable”))
#Prints the type of the variable
What is the purpose of an if statement?
It is a fundamental control structure that enables a program to make decisions and execute different code branches based on whether a given condition is True or False.
IOW: a conditional statement used to execute a block of code only when a specific condition is met.
Temperature = 25
#Assign a value to the variable
if Temperature > 24:
print(“The weather is hot outside.”)
#If statement to check if the number is greater than 24
How would you check some statement and criteria?
In short:
- Use IF, for one-condition problems
- Use ELSE for two-condition problems
- Use ELIF for more than two condition problems
IF is the first condition to check, and it triggers its block if the condition is True.
Use if, for one-condition problems, for example:
Temperature = 25
#Assign a value to the variable
if Temperature > 24:
print(“The weather is hot outside.”)
#If statement to check if the number is greater than 24
ELIF follows an if or another elif and provides additional conditions to check. It is useful when multiple, mutually exclusive conditions need to be checked sequentially. Use elif for more than two condition problems, for example:
if Temperature > 24:
print(“The weather is hot outside.”)
elif Temperature > 20:
print(“The weather is okay outside”)
elif Temperature < 15:
print(“The weather is not good outside”)
else:
print(“The weather is not hot outside”)
#If statement to check if the number is greater than 24
ELSE does not have a condition; it catches anything that wasn’t caught by the preceding IF and ELIF statements. It’s essentially the “default” action when all other conditions fail. Use else for two condition problems, for example:
if Temperature > 24:
print(“The weather is hot outside.”)
else:
print(“The weather is not hot outside”)
#If statement to check if the number is greater than 24
How is a dictionary different from a list in Python?
List: A list is an ordered sequence of elements. Each element in a list can be accessed by its position, or index, within the list. Indexes in lists are integers starting from 0 for the first element.
Accessed by its index.
List: numbers = [1, 2, 3, 4, 5]
Dictionary: A dictionary is an unordered collection of data in a key-value pair format. Each element in a dictionary is stored as a key paired with a value. The keys must be unique within a single dictionary, and they are typically used to describe or identify the associated value. Elements in a dictionary would be link together.
Accessed by its key.
Dictionary: person = {“name”: “Bob”, “age”: 30}
Use cases of list and dictionary? (when is it best to use list and when is it best to use dictionary)
List: Ideal for ordered tasks where the arrangement of elements is significant, such as storing a list of numbers, processing items in a specific sequence, or iterating through elements in the order they were added.
Dictionary: Used when you need a logical association between key:value pairs of items. Dictionaries are faster at finding values than lists since they use a hashing mechanism to store and retrieve data. They are ideal for representing real-world data that involves mapping between unique identifiers and data (like a database).
Explain the difference between the “and” & “or” operators
Functionality: The and operator checks that all conditions specified are True. If all conditions are True, then the and expression itself returns True. If any one of the conditions is False, the and expression returns False.
age = 17
is_citizen = True
#Assign values to variables
if age >= 17 and is_citizen:
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
#If statement using the ‘and’ operator to check both conditions
Functionality: The or operator checks if at least one of the conditions is True. The or expression returns True as soon as one of its conditions is True. It only returns False if all conditions are False.
if age >= 17 or is_citizen:
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
#If statement using the ‘and’ operator to check both conditions
What libraries are necessary to visualize our data?
Seaborn and Matplotlib are two of the most popular libraries in Python for data visualisation
Import seaborn: import seaborn as sns
Import Matplotlib: import matplotlib.pyplot as plt
Bonus: Seaborn is quick, whereas Matplotlib offers more customisation options
In what cases we need to import Seaborn and Matplotlib?
To visualise our data and find a trend. They offer a wide array of plots and customization options, making it highly flexible for creating sophisticated and professional figures.
What function can execute and run a loop as many times as we need?
The For Loop function, which provide a powerful, flexible way to handle repetitive tasks
Define a list of numbers
numbers = [1, 2, 3, 4, 5]
Loop through each number in the list
for number in numbers:
print(number)
Why do we use For Loops?
Repetition: For loops are used to repeat a block of code multiple times. For example, if you want to perform the same operation on every element of a list, a for loop can automate and streamline this process.
Control Structure: For loops offer a way to include decision-making processes within the loop, using conditional statements like IF, to perform different actions depending on the item.