Python Flashcards
What are the 4 major data types?
Integer, Float, String, Boolean
What is an integer?
What category does it fall into?
What is it’s command
Real Number
Data Types
int( )
What is an float?
What category does it fall into?
What is it’s command?
Represents a floatingnumber (1.2, 2.0, 2.1)
Data Type
float( )
What is an boolean?
What category does it fall into?
What is it’s command?
Represents True (1) or False (0)
Data Type
bool( )
What is a string?
What category does it fall into?
What is it’s command?
Represents a sequence of characters enclosed in single quotes (‘’) or double quotes (“”).
Data Type
str( )
What is a list?
What category does it fall into?
Data Structure representing an ordered collection of items enclosed in square brackets ([]). Each element in a list is assigned a unique index, starting from 0 for the first element. The items can be of any data type and are mutable, meaning their values can be changed.
fruits = [“apple”, “banana”, “orange”]
What is a tuple?
What category does it fall into?
Data structure representing an ordered collection of items enclosed in parentheses (()). Each element in a tuple is assigned a unique index, starting from 0 for the first element. The items can be of any data type. Tuples are immutable, meaning their values cannot be changed once defined.
coordinates = (10.0, 20.0)
What is a set?
What category does it fall into?
Data structure representing an unordered collection of unique items enclosed in curly braces ({}). Sets are mutable, meaning their values can be changed.
numbers = {1, 2, 3, 4, 5}
What is a dictionary?
What category does it fall into?
Data structure representing a collection of key-value pairs enclosed in curly braces ({}). Each key-value pair is separated by a colon (:), and the keys are unique. Dictionaries are mutable, meaning their values can be changed.
person = {“name”: “John”, “age”: 30}
How would you write a list called numbers including numbers 1 to 5?
numbers = [1, 2, 3, 4, 5]
How would you code a tuple called coordinates with values of 10 and 20?
coordinates = (10.0, 20.0)
How would you code a set called fruits with values Apple, banana, and orange?
fruits = {“apple”, “banana”, “orange”}
How would you code a dictionary entry called person named John, age 30?
person = {“name”: “John”, “age”: 30}
What is a data type?
data types define the type of value that a variable can hold
What is casting?
Casting in Python refers to the process of changing the data type of a value. It allows you to convert a value from one type to another.
What is a Modulo?
How is it written?
What does it do?
Modulo (%): Returns the remainder of the division.
result = 10 % 3
print(result) # Output: 1
What is floor division?
How is it written?
Returns the quotient of the division, rounded down to the nearest integer.
result = 10 // 3
print(result) # Output: 3
What are variables?
What are they used for?
Variables in Python are used to store and manipulate data. A variable is a named location in the computer’s memory where you can store a value.
What does mutable mean?
Means the values can be changed.
What does immutable mean?
Means the values cannot be changed once defined.
How do you access an item in a list or tuple?
Access Apple in the following list?
fruits = [“apple”, “banana”, “orange”]
You can access individual elements in a list by their index. Remember that the index starts from 0. For example:
fruits = [“apple”, “banana”, “orange”]
print(fruits[0]) # Output: “apple”
print(fruits[1]) # Output: “banana”
How do you add items to a list or tuple?
What is the code to add grapes to this list?
fruits = [“apple”, “banana”, “orange”]
You can add new elements to the end of a list or tuple using the append() method.
fruits = [“apple”, “banana”, “orange”]
fruits.append(“grape”)
print(fruits) # Output: [“apple”, “banana”, “orange”, “grape”]
How do you modify the value of a list or tuple element?
What is the code to change banana to kiwi?
fruits = [“apple”, “banana”, “orange”]
You can change the value of an element in a list or tuple by assigning a new value to its index.
fruits = [“apple”, “banana”, “orange”]
fruits[1] = “kiwi”
print(fruits) # Output: [“apple”, “kiwi”, “orange”]
What is it called when you extract a portion of a list or tuple?
Extract banana, orange, and kiwi from below:
fruits = [“apple”, “banana”, “orange”, “kiwi”, “grape”]
You can extract a portion of a list using slicing. Slicing allows you to specify a range of indices to extract elements.
fruits = [“apple”, “banana”, “orange”, “kiwi”, “grape”]
sliced_fruits = fruits[1:4]
print(sliced_fruits) # Output: [“banana”, “orange”, “kiwi”]
How to you find out the length of a list?
You can determine the length of a list (i.e., the number of elements) using the len() function.
fruits = [“apple”, “banana”, “orange”]
length = len(fruits)
print(length) # Output: 3
What is unpacking a tuple?
What is it useful for?
How would you code it?
Unpacking: You can unpack the values of a tuple into separate variables. This is useful when you want to assign each element of a tuple to a different variable.
coordinates = (10.0, 20.0)
x, y = coordinates
print(x) # Output: 10.0
print(y) # Output: 20.0
How do you access/print values in a dictionary?
How would you code it?
Accessing Values: You can access the value associated with a specific key in a dictionary by using the key as an index.
person = {“name”: “John”, “age”: 30, “city”: “New York”}
print(person[“name”]) # Output: “John”
print(person[“age”]) # Output: 30
How do you modify values of a dictionary?
How would you code it?
Modifying Values: You can change the value associated with a specific key in a dictionary by assigning a new value to it.
person = {“name”: “John”, “age”: 30, “city”: “New York”}
person[“age”] = 35
print(person[“age”]) # Output: 35
How would you add a new key value pair of a dictionary?
Code city / New York into list person?
Adding New Key-Value Pairs: You can add new key-value pairs to a dictionary by using a new key as an index and assigning a value to it.
person = {“name”: “John”, “age”: 30}
person[“city”] = “New York”
print(person) # Output: {“name”: “John”, “age”: 30, “city”: “New York”}
How do you delete a key-value pair from a dictionary?
How do you code deleting
Removing Key-Value Pairs: You can remove a specific key-value pair from a dictionary using the del keyword.
person = {“name”: “John”, “age”: 30, “city”: “New York”}
del person[“age”]
print(person) # Output: {“name”: “John”, “city”: “New York”}
How do you check if a key exists in a dictionary?
What would the output be presented?
Checking Key Existence: You can check if a specific key exists in a dictionary using the in keyword.
person = {“name”: “John”, “age”: 30, “city”: “New York”}
print(“name” in person) # Output: True
print(“country” in person) # Output: False
How do you get keys and/or values from a dictionary?
Getting Keys and Values: You can retrieve all the keys or values from a dictionary using the keys() and values() methods, respectively.
person = {“name”: “John”, “age”: 30, “city”: “New York”}
keys = person.keys()
values = person.values()
print(keys) # Output: dict_keys([“name”, “age”, “city”])
print(values) # Output: dict_values([“John”, 30, “New York”])
How do you add elements into a set?
How would you code adding grapes to a set called fruits?
Adding Elements: You can add new elements to a set using the add() method.
fruits = {“apple”, “banana”, “orange”}
fruits.add(“grape”)
print(fruits) # Output: {“apple”, “banana”, “orange”, “grape”}
How do you remove elements from a set?
Removing Elements: You can remove elements from a set using the remove() method.
fruits = {“apple”, “banana”, “orange”}
fruits.remove(“banana”)
print(fruits) # Output: {“apple”, “orange”}
How do you check existence of an element in a set?
How would it show in output?
Checking Existence: You can check if an element exists in a set using the in keyword.
fruits = {“apple”, “banana”, “orange”}
print(“apple” in fruits) # Output: True
print(“grape” in fruits) # Output: False
How do you combine two sets?
Set1 and Set2
Union
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4}
How do you show the difference in two sets, set1 and set2?
set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference_set = set1.difference(set2)
print(difference_set) # Output: {1}
How do you see the common items of a set, set1 and set2?
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {2, 3}
How do you find out the length of a set?
Length: You can determine the number of elements in a set using the len() function.
fruits = {“apple”, “banana”, “orange”}
length = len(fruits)
print(length) # Output: 3
What are loops?
What are the two main types?
Loops in Python are used to repeatedly execute a block of code until a certain condition is met. They allow you to automate repetitive tasks and iterate over collections of data.
for loop
while loop
What is a “for” loop?
The for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects. It executes a block of code for each item in the sequence.
for i in range(5):
print(i) #output 0 1 2 3 4
What is a while loop?
The while loop is used to repeatedly execute a block of code as long as a certain condition is true. It continues executing the loop as long as the condition remains true.
count = 0
while count < 5:
print(count)
count += 1 #output 0 1 2 3 4
What is a function?
How do you define a function?
Define a function in Python, you can use the def keyword followed by the function name and a pair of parentheses. You can also specify parameters inside the parentheses if the function takes any inputs.
def calculate_area(length, width):
area = length * width
return area
result = calculate_area(5, 3)
print(result) # Output: 15
How do you sort?
The sort() method is used to sort a list in-place, meaning it modifies the original list.
numbers = [3, 1, 4, 2, 5]
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4, 5]