Python Flashcards
(95 cards)
What is the Python equivalent of null?
None = nothing there in python/null
How do you create a null-like value to a variable in Python?
varName = None
How do you print a None value in Python?
print(varName)
where varName=None
What is a tuple in Python?
Tuples are ordered IMMUTABLE lists defined within ()
x = (1,5,2,3)
TUPLE
Ordered=values aren’t sorted into an order and can’t be as tuples are immutable/can’t be changed or modified.
What is a list in Python?
A list [] is a mutable collection of items defined with square brackets, e.g., [1, 2, 3].
Zero-Indexed.
To access a value just varName[2] or whatever index.
Which Python data structure only stores unique values?
Sets = {} only store unique values so if you create set1 = {1,1,1}
set1= {1} as the others get ignored.
If you wrote:
a = 1
b = 1
c = 1
my_set = {a, b, c}
Same result, my_set={1} — even though you used three different variable names, they all resolve to the same value, so only one instance goes into the set.
If you create a Python set like {1, 2, 1}, how many elements are stored?
Two elements {1,2} are stored—Python sets discard duplicates before storing anything.
How do you check the data type of a variable in Python?
e.g. you have a var called varName
type(varName)
Use the type() function, e.g., type(x)
What does the len() function do?
It returns the number # of elements in a sequence like a list, string, tuple, or set.
len([1, 2, 3]) # Returns 3
How do you create a set with unique values?
A set {} is an unordered collection of unique items, created with curly braces. Duplicates are automatically removed
my_set = {1, 2, 2, 3}
print(my_set) # Output: {1, 2, 3}
How do you define and call a function in Python?
Define a function with def keyword; call it by name with parentheses and arguments (what you enter instead of the placeholders).
def add(a, b):
return a + b
result = add(2, 3) # result is 5
How do you write a for loop to iterate over a range?
Use for x in range(start, stop): to loop through numbers. for i in range (1,100):
for i in range(1, 4):
print(i) # Prints 1 2 3 each on a new line
How do you check if a value exists in a list or set?
Use the if and in keyword to test membership of a value in a list [] or set ()
if 2 in [1, 2, 3]:
print(“Found 2”)
What is linting in programming?
Linting is the automated process of checking your code for errors, stylistic issues, and potential bugs before running it.
VS Code uses syntax highlighting (not linting) powered by language grammar files and themes to color code your text (keywords, variables, strings, etc.).
What colors code in VS Code?
VS Code uses syntax highlighting powered by language grammar files and themes to color code your text (keywords, variables, strings, etc.).
What is Python?
Python is a simple, interpreted, dynamically typed, easy-to-read programming language used to build websites, automate tasks, analyze data, and more.
What is the difference between interpreted and compiled languages?
Interpreted languages (like Python) run code line by line at runtime. Compiled languages (like C++) convert code into machine language before running, making them faster but less flexible.
What is a class in Python?
A class in Python is a blueprint for creating objects that defines their data (attributes) and behavior (methods - fn declared in a class).
What is a method in Python?
A method () in Python is a function (fn) defined inside a class that operates on instances of that class, typically using self to access object data.
What is the purpose of the __init__ method in a Python class?
The __init__ method is a special constructor that initializes a new object’s attributes when it is created.
How do you call a method attached to an object in Python?
By using dot notation: objectName.methodName()
What does self refer to inside a method?
self refers to the current instance of the class on which the method is called.
self = $_ equivalent = the current object in the pipeline that’s selected.
What are dunder methods in Python? What does dunder stand for?
Dunder (__Double Under__) methods are special methods (defined in a class (template) with double underscores before and after the method names that customize how objects created with classes behave with built-in operations.
They tell Python how your object should behave when you do stuff like:
Make a new one (__init__)
Print it (__str__)
Add two objects (__add__)
Think of dunder methods as magic buttons that control how your objects act in different situations. Without them, Python wouldn’t know what to do with your objects in those cases.
What do dunder methods do in a Python class?
__Dunder__ methods change how built-in Python operations like printing, adding, or comparing work for objects created from that class.