Python Flashcards

1
Q

What is the naming convention for class names in Python?

A

CapWords convention (PascalCase)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Provide an example of a correctly named class in Python.

A

MyDog

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What should function names in Python be formatted as?

A

All lowercase with underscores separating words (snake_case)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a proper name for a function that calculates force based on mass and acceleration?

A

calculate_force

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What rules should variable names follow in Python?

A

All lowercase with underscores separating words (snake_case)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many spaces should be used per indentation in Python?

A

4 spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is considered bad practice for indentation in Python?

A

Using 2 spaces per indentation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is preferred for indentation: spaces or tabs?

A

Spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How should inline comments be formatted?

A

Separated by at least two spaces from the code and kept brief

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for block comments and inline comments?

A
my code  # inline comment

this is
# a block
# comment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Python script file extension?

A

.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you run a Python script using the command line?

A

python myscript.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the built-in function in Python for asking user input?

A

input()

The input() function takes in the user’s input, allowing interaction with the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which function is used to display output in Python?

A

print()

The print() function outputs data to the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a formatted string literal in Python?

A

A string prefixed with ‘f’ that allows expressions inside curly braces.

Example: print(f’Very nice to meet you, {name}!’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the three main ways to print multiple objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you declare a variable in Python?

A

name = ‘Grace’

This line of code stores the string ‘Grace’ in the variable name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is casting in Python?

A

Specifying the type of a variable using built-in data types.

For example, using str() to convert a number to a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you convert a numerical value to a string?

A

Using the function str().

For example, temperature = str(97.5) converts the number 97.5 to a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are some other built-in functions for type conversion in Python?

A
  • int()
  • float()
  • bool()

These functions convert values to integer, float, and boolean types, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can you access the data type of a variable?

A

By using the built-in function type().

For example, type(temperature) will return the type of the variable temperature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Under what circumstances will the bool() function return False?

A

The argument is empty, 0, None, or False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the exponentiation operator?

24
Q

What is the floor division (integer division) operator?

25
Q

What are the three logical operators?

A
  • and
  • or
  • not
26
Q

What are the three conditional statements?

A
  • if
  • else
  • elif
27
Q

How would you iterate from 0 to 2 using a loop?

A
for i in range(3):
28
Q

How would you iterate over an array nums?

A
for num in nums:
29
Q

What does the keyword pass do?

A

The pass keyword is mostly used as a placeholder in a loop. Nothing gets executed when pass is placed under a condition.

30
Q

What are the three clauses used in error handling?

A

try, except, finally

31
Q

What does the keyword finally do?

A

Executes a block of code regardless of which error handling clause, try or except, was executed.

32
Q

What is the built-in keyword in Python that is used to declare functions.

33
Q

What indicates the start of a function body?

34
Q

What is the built-in keyword that exits the function and returns the output?

35
Q

Small, inline functions that can have any number of arguments but only one expression.

A

lambda functions

36
Q

Lambda functions in Python are also known as ___ functions.

37
Q

The basic syntax of a lambda function is

A

lambda arguments : expression

38
Q

Called to delete an object.

A

destructors

39
Q

What is the syntax for a constructor?

A

def \_\_init\_\_(self, argument(s)):

40
Q

What is the syntax for defining a destructor in a class?

A

`def __del__(self):

41
Q

What is the syntax for using a destructor on an object?

A

del my_object

42
Q

What is the default access modifier for all members within a class?

43
Q

How to make a class member protected?

A

prepend one underscore

44
Q

How to make a class member private?

A

prepend two underscores

45
Q

What access modifier prevents members from being accessed outside of the class, unless it’s from a subclass.

46
Q

What access modifier declares members to be accessible within the class only

47
Q

In Python inheritance, what two things must a child class contain?

A
  • 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
48
Q

In Python inheritance, what is the syntax for referencing the parent class in the definition of the child class?

A

class Child(Parent):

49
Q

How does polymorphism work in Python?

A

Just name methods the same between different classes.

50
Q

Given the string intro = "My name is Jeff!"

How would you index the letter “M”?

51
Q

Given the string intro = "My name is Jeff!"

How would you index the word 'My'

A

intro[0:2]

52
Q

Given the string intro = "My name is Jeff!"

How would you index the word 'Jeff'?

A

intro[-5:-1]

53
Q

How can you measure the length of a string?

A

len()

e.g. len(my_string)

54
Q

What is the difference between Tuples and Lists?

A

Tuples are immutable

(can’t be modified)

Tuples are more memory efficient and slightly more time efficient than Lists

55
Q

What restriction is placed on Dictionary keys?

A

Must be immutable

e.g. strings, numbers, or tuples

56
Q

How to check for Set containment?

A

in keyword

e.g. my_string in my_set