Python Flashcards
append() + example
Adds an item to the end of the list.
pop() + example
Removes and returns an item at a specific index.
insert() + example
Inserts an item at a specific index.
remove() + example
Removes the first occurrence of a value.
count() + example
Counts occurrences of a specified value.
index() + example
Returns the index of the first occurrence of a value.
are list mutable in python?
Lists in Python are mutable, meaning they can be modified.
numbers = [1, 2, 3, 4, 5]
float_numbers = [1.1, 2.2, 3.3]
names = [“Alice”, “Bob”, “Charlie”]
names = [“Alice”, “Bob”, “Charlie”]
give me list of tuples
coordinates = [(1, 2), (3, 4), (5, 6)]
what is + example
int (Integer)
Definition: Represents whole numbers, positive or negative, without a decimal point.
what is + example
float (Floating-Point Number)
Definition: Represents real numbers with a decimal point.
x = 3.14 # Positive float
y = -2.71 # Negative float
z = 0.0 # Zero as a float
what is + example
matrix
Python does not have a built-in matrix type, but you can create matrices using nested lists or libraries like NumPy.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
what is + example
tuple
Definition: An ordered, immutable collection of elements. Once created, a tuple cannot be modified.
Syntax: Defined using parentheses () or without them.
tuple1 = (1, 2, 3) # Tuple with integers
tuple2 = (“a”, “b”, “c”) # Tuple with strings
tuple3 = (1, “hello”, 3.14) # Mixed data types
Tuples are faster than lists.
Useful for data that should not change.
what is + example
dictionary
Definition: An unordered collection of key-value pairs.
Syntax: Defined using curly braces {} with keys and their corresponding values.
person = {
“name”: “Alice”,
“age”: 25,
“city”: “New York”
}
Convert int to float (function)
num_int = 5 # Integer
num_float = float(num_int) # Convert to float
Convert float to int (function)
num_float = 5.7 # Float
num_int = int(num_float) # Convert to integer
Convert int to str (function)
num_int = 123 # Integer
num_str = str(num_int) # Convert to string
Convert str to int (function)
num_str = “456” # String
num_int = int(num_str) # Convert to integer