1 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 list, regular variable, 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.
Temperature = 25 # Assign a value to the variable
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
- How would you check some statement and criteria ?
if is the first condition to check, and it triggers its block if the condition is True.
Use if, for one-condition problems.
Temperature = 25 # Assign a value to the variable
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
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.
if Temperature > 24: # If statement to check if the number is greater than 24
print(“The weather is hot outside.”)
else:
print(“The weather is not hot outside”)
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.
if Temperature > 24: # If statement to check if the number is greater than 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”)
- 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.
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.
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 # Assign values to variables
is_citizen = True
if age >= 17 and is_citizen: # If statement using the ‘and’ operator to check both conditions
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
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: # If statement using the ‘and’ operator to check both conditions
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
- What libraries are necessary to visualize our data?
Seaborn and Matplotlib are two of the most popular libraries in Python for data visualization
Import seaborn: import seaborn as sns
Import Matplotlib: import matplotlib.pyplot as plt
- In what cases we need to import Seaborn and Matplotlib?
To visualize 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
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.