Back to notes
Describe how inheritance works in Python with an example.
Press to flip
Inheritance allows a class to inherit methods and properties from another class. Example: `class ChineseChef(Chef):` will inherit from `Chef` and can override methods or add new ones.
What does the `__init__` method do in a Python class?
The `__init__` method initializes an object’s attributes when an instance of the class is created.
Explain how to prompt a user for input and display a message using their input.
Use `input()` to get input from the user and concatenate the input with other strings using `+` operator to display a message.
What is the difference between `.write()` and `.append()` when handling files in Python?
`.write()` overwrites the file content while `.append()` adds new content to the end of the file.
What is the output of the following code? print(abs(-5))
5
What is the output of the following code? print("Hello, " + "World!")
Hello, World!
Explain how to use the `.lower()` and `.isupper()` string methods.
`.lower()` converts all characters in a string to lowercase. `.isupper()` returns `True` if all characters in a string are uppercase letters.
How do you install Python and what version should you download?
Download Python from python.org/downloads and choose version 3.x as it is the future of Python and more supported than 2.x.
How do you define a dictionary in Python and access its elements?
Define a dictionary using curly braces `{}` with key-value pairs, and access elements using square brackets `[]` or the `.get()` method.
What will the following code output? phrase = "Draft Academy" print(phrase.index("A"))
6
How do you create a basic calculator to add two numbers in Python?
Use `input()` to get two numbers from the user, convert them to `float`, add them, and `print` the result.
How can you ensure you are using Python 3.x in PyCharm?
Configure the interpreter in PyCharm's settings to use Python 3.x.
How can you import a built-in Python module and use one of its functions?
Use the `import` statement to import the module and call its functions using `module_name.function_name()`. Example: `import math` and `math.sqrt(36)`.
How do you determine if a student is on honor roll using a method within a `Student` class?
Define an `on_honor_roll` method that checks if the student’s GPA is 3.5 or higher and returns `True` if it is, otherwise `False`.
How do you execute a Python script from the command line?
Navigate to the script’s directory in the command line and type `python script_name.py`.
Explain and write a function to calculate the power of a number.
Define a function `raise_to_power` that takes two arguments (base and exponent), uses a loop to multiply the base by itself exponent times, and returns the result.
Write a for loop that prints each item in a list called `friends`.
```python friends = ["Jim", "Karen", "Kevin"] for friend in friends: print(friend) ```
Previous
Next