Python Flashcards
(56 cards)
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?
//