Python Flashcards
What is the naming convention for class names in Python?
CapWords convention (PascalCase)
Provide an example of a correctly named class in Python.
MyDog
What should function names in Python be formatted as?
All lowercase with underscores separating words (snake_case)
What is a proper name for a function that calculates force based on mass and acceleration?
calculate_force
What rules should variable names follow in Python?
All lowercase with underscores separating words (snake_case)
How many spaces should be used per indentation in Python?
4 spaces
What is considered bad practice for indentation in Python?
Using 2 spaces per indentation
What is preferred for indentation: spaces or tabs?
Spaces
How should inline comments be formatted?
Separated by at least two spaces from the code and kept brief
What is the syntax for block comments and inline comments?
my code # inline comment this is # a block # comment
What is the Python script file extension?
.py
How would you run a Python script using the command line?
python myscript.py
What is the built-in function in Python for asking user input?
input()
The input() function takes in the user’s input, allowing interaction with the program.
Which function is used to display output in Python?
print()
The print() function outputs data to the console.
What is a formatted string literal in Python?
A string prefixed with ‘f’ that allows expressions inside curly braces.
Example: print(f’Very nice to meet you, {name}!’)
What are the three main ways to print multiple objects?
Multiple arguments
print('Very nice to meet you, ', name, '!')
Concatenation
print('Very nice to meet you! My name is ' + name, ' and I am ' + age + ' years old')
Formatted String Literals
print(f'Very nice to meet you, {name}!')
How do you declare a variable in Python?
name = ‘Grace’
This line of code stores the string ‘Grace’ in the variable name.
What is casting in Python?
Specifying the type of a variable using built-in data types.
For example, using str() to convert a number to a string.
How do you convert a numerical value to a string?
Using the function str().
For example, temperature = str(97.5) converts the number 97.5 to a string.
What are some other built-in functions for type conversion in Python?
- int()
- float()
- bool()
These functions convert values to integer, float, and boolean types, respectively.
How can you access the data type of a variable?
By using the built-in function type().
For example, type(temperature) will return the type of the variable temperature.
Under what circumstances will the bool() function return False?
The argument is empty, 0, None, or False.
What is the exponentiation operator?
**
What is the floor division (integer division) operator?
//
What are the three logical operators?
- and
- or
- not
What are the three conditional statements?
- if
- else
- elif
How would you iterate from 0 to 2 using a loop?
for i in range(3):
How would you iterate over an array nums
?
for num in nums:
What does the keyword pass
do?
The pass keyword is mostly used as a placeholder in a loop. Nothing gets executed when pass is placed under a condition.
What are the three clauses used in error handling?
try, except, finally
What does the keyword finally
do?
Executes a block of code regardless of which error handling clause, try
or except
, was executed.
What is the built-in keyword in Python that is used to declare functions.
def
What indicates the start of a function body?
:
What is the built-in keyword that exits the function and returns the output?
return
Small, inline functions that can have any number of arguments but only one expression.
lambda
functions
Lambda functions in Python are also known as ___ functions.
anonymous
The basic syntax of a lambda function is
lambda
arguments : expression
Called to delete an object.
destructors
What is the syntax for a constructor?
def \_\_init\_\_(self,
argument(s)):
What is the syntax for defining a destructor in a class?
`def __del__(self):
What is the syntax for using a destructor on an object?
del my_object
What is the default access modifier for all members within a class?
public
How to make a class member protected?
prepend one underscore
How to make a class member private?
prepend two underscores
What access modifier prevents members from being accessed outside of the class, unless it’s from a subclass.
protected
What access modifier declares members to be accessible within the class only
private
In Python inheritance, what two things must a child class contain?
- Name of the parent class in the definition of the child class
- Constructor of the parent class called within the constructor of the child class
In Python inheritance, what is the syntax for referencing the parent class in the definition of the child class?
class Child(Parent):
How does polymorphism work in Python?
Just name methods the same between different classes.
Given the string intro = "My name is Jeff!"
How would you index the letter “M”?
intro[0]
Given the string intro = "My name is Jeff!"
How would you index the word 'My'
intro[0:2]
Given the string intro = "My name is Jeff!"
How would you index the word 'Jeff'
?
intro[-5:-1]
How can you measure the length of a string?
len()
e.g. len(my_string)
What is the difference between Tuples and Lists?
Tuples are immutable
(can’t be modified)
Tuples are more memory efficient and slightly more time efficient than Lists
What restriction is placed on Dictionary keys?
Must be immutable
e.g. strings, numbers, or tuples
How to check for Set containment?
in
keyword
e.g. my_string in my_set