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”]