Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Python Interview Questions?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ = int(num)3Order
Put the learning moves in the order that makes the concept easiest to apply.
These questions and answers cover some fundamental Python concepts that are often discussed in interviews.
This page contains a list of typical Python Interview Questions and Answers.
Python Interview Questions
This page contains a list of typical Python Interview Questions and Answers.
Python Interview Questions
These questions and answers cover some fundamental Python concepts that are often discussed in interviews.
- A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.
- A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available from within any scope, global and local.
- An iterator is an object that contains a countable number of values.
- An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
- Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods iter() and next().
- All classes in Python have a function called init(), which is always executed when the class is being initiated.
- We can use the init() function to assign values to object properties, or other operations that are necessary to do when the object is being created.
- Use lambda functions when an anonymous function is required for a short period of time.
Lists, tuples, and sets are all used to store multiple items in a single variable, but they have different properties:
- A list is ordered and changeable. It allows duplicate values.
- A tuple is ordered but unchangeable (immutable). It also allows duplicates.
- A set is unordered, unindexed, and contains only unique items. It is changeable, but you cannot modify individual elements by index.
- You can use the isalnum() method, which returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
- You can use the int() function, like this:
num = "5"
convert = int(num)- Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
- Python uses indentation to indicate a block of code.
- Python will give you an error if you skip the indentation.
print(type(x))- SET
- Inheritance allows us to define a class that inherits all the methods and properties from another class.
- Parent class is the class being inherited from, also called base class.
- Child class is the class that inherits from another class, also called derived class.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")- Above ten, and also above 20!
- Text Type: str
- Numeric Types: int , float , complex
- Sequence Types: list , tuple , range
- Mapping Type: dict
- Set Types: set , frozenset
- Boolean Type: bool
- Binary Types: bytes , bytearray , memoryview
- Membership operators are used to test if a sequence is present in an object. The in and not in operators are examples of these:
x = ["apple", "banana"]
print("banana" in x) # returns True
x = ["apple", "banana"]
print("pineapple" not in x) # returns True- The pass statement
- Arbitrary Arguments are often shortened to *args in Python documentations.
- If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition. This way the function will receive a tuple of arguments, and can access the items accordingly.
- To create a module just save the code you want in a file with the file extension .py : def greeting(name): print("Hello, " + name)
- Now we can use the module we just created, by using the import statement: import mymodule mymodule.greeting("Jonathan")
def greeting(name):
print("Hello, " + name)import mymodule
mymodule.greeting("Jonathan")- No, because: list2 will only be a reference to list1 , and changes made in list1 will automatically also be made in list2 .
- To make a copy of a list, you can use copy() or the list() method.
- You can return a range of characters by using the "slice syntax".
- Specify the start index and the end index, separated by a colon, to return a part of the string, for example:
b = "Hello, World!"
print(b[2:5])- A Class is like an object constructor, or a "blueprint" for creating objects.
- You can create a class with the class keyword: class MyClass: x = 5 Now we can use the class named MyClass to create objects: Create an object named p1, and print the value of x: p1 = MyClass() print(p1.x)
class MyClass:
x = 5p1 = MyClass()
print(p1.x)