Python Flashcards
The language’s name isn’t about snakes, but about the popular British comedy troupe ___ (from the 1970s).
Monty Python
He is a Dutch programmer best known as the creator of the Python programming language, for which he was the “benevolent dictator for life” (BDFL) until he stepped down from the position on 12 July 2018.
Guido Van Rossum
Which of the following would result in an error?
A. num = 5
if num > 3: print(“greater”); print (“lesser”)
B. if 5 > 3: print(greater); print (lesser)
C. #if 5 > 3: print(greater); print (lesser)
D. if 5 > 3:
print(“greater”)
print (“lesser”)
B. if 5 > 3: print(greater); print (lesser)
Which of the following print() function invocations will cause a SyntaxError?
A. print(‘Greg\ ‘s book.’)
B. print(“‘Greg’s book.’”)
C. print(‘“Greg's book.”’)
D. print(“Greg's book.”)
E. print(‘“Greg’s book.”’)
E. print(‘“Greg’s book.”’)
You need a colon at the end of every block of code in Python.
true or false?
false
Which shortcut key is used to quickly comment or uncomment multiple lines of code?
CTRL + /
Which of the following names are illegal in Python?
enumerate 3
- starting with a number
- has space
- not descriptive
What is the output of the following snippet if the user enters two and four respectively?
x = int(input())
y = int(input())
x = x/y
y = y/x
print(y)
8.0
This method adds an element at a given position in the list.
insert()
Which version of Python uses ordered dictionaries?
Version 3.7
What is the output of the following snippet?
x = 1
y = 1.0
z = “1”
if x == y:
print(“one”)
if y == int(z):
print(“two”)
elif x == y:
print(“three”)
else:
print(“four”)
one
two
Which logical operator is used to check if at least one of two conditions is True?
or
With list comprehension you will have to write a for statement with a conditional test inside.
true or false?
false
Which shortcut key is used to terminate your program in an infinite loop?
CTRL + C
The ____ keyword catches anything which isn’t caught by the preceding conditions.
else
What is the output of the following snippet?
for i in range(0, 6, 3):
print(i, end=” “)
0 3
What is the output of the following snippet?
n = 3
while n>0:
print(n+1, end=”:”)
n -= 1
else:
print(n)
4:3:2:0
How do you check the current installed version of Python in your system? (what to type in the command prompt)
python –version
This acts as a placeholder in for loops.
pass
If the condition is False
* the code inside \_\_\_\_ keyword is executed * the code inside \_\_\_\_ keyword is skipped
answer, answer
else, if
What is the output of the following snippet?
print(“String #1”)
print(“String #2)
String #2
What keyword is Python’s way of saying “if the previous condition were not true, then try this condition”?
elif
What is the output of the following snippet?
This is
a multiline
comment. #
print(“Hello!”)
Syntax Error: invalid syntax
What is the expected output of the following snippet?
print((2*4.), (2%4), (2**3))
8, 2, 8
This mode appends to a file and creates the file if it does not exist or overwrites an existing file.
a
The attributes inside the class is called _____
variables
What is the correct syntax of printing all variables and function names of a module?
dir
These arguments allow you to pass multiple non-key arguments.
arbitrary
A package in python is a container that contains various functions to perform specific tasks.
true or false?
true
A user-defined blueprint or prototype from which objects are created.
*
class
Which of the following is a valid lambda function in Python?
A. lambda x: x * 2, y
B. lambda (x, y): x + y
C. lambda x, y: x + y
D. lambda x y: x + y
C. lambda x, y: x + y
To delete a file, you must import the OS module, and run its_______
os.remove()
What is python’s package manager?
pip
This type of polymorphism is used in dynamic languages such as Python, Perl, Ruby, PHP, and Javascript.
duck typing
How many types of __init__() constructor are there based on the report?
3
What is the correct syntax of importing only a specific function or variable?
from
The “___” method will overwrite the entire file.
w
True or False: Derived class is the other term for the Parent class and Base class is the other term for Child Class.
true or false?
false
Which keyword is used to define a lambda function in Python?
lambda
Can a lambda function have multiple expressions?
No, it can only have a single expression.
What do you call a function in inheritance that makes the child inherit all the properties from its source?
super() function
What package is installed to send http requests to APIs?
requests
To delete an entire folder, use the ______
os.rmdir()
In child class, we use this instead of pass. What do you call this function?
__init__() function
To define a function, you need to type _____ followed by a unique function name.
def
To write to an existing file, you must add a parameter to the _______ function:
open()
In order to overload operators, you must define their corresponding special methods in your class.
true or false?
true
What is the built-in function to open a file in Python?
open()
It is a good practice to always _________ the file when you are done with it.
close()
This type of polymorphism needs to have at least two classes.
Method Overriding
In Python, you can return one line in a file by using the _________ method:
readline()
When combining positional arguments and keyword arguments, you must put _______ arguments first.
positional
The os.remove module can remove a file or file path but cannot delete a directory.
true or false?
true
remove all elements from the dictionary
clear()
returns a copy of the dictionary
copy()
returns a dictionary with the specified keys and value
fromkeys()
returns the value of the specified key
get()
returns a list containing a tuple for each key value pair
items()
returns a list containing the dictionary’s keys
keys()
removes the element with the specified key
pop()
removes the last inserted key-value pair
popitem()
returns value of specified key; if key does not exist: insert the key with the specified value
setdefault()
updates dictionary with the specified key-value pairs
update()
returns a list of all values in the dictionary
values()
mechanism that allows you to create a hierarchy of classes that share a set of properties and methods by deriving a class from another class.
inheritance
With inheritance, there are two classes that we need to take note of:
- Parent Class or Base Class - the class being inherited from
- Child Class or Derived Class - the class that inherits from another class
most basic form of inheritance, wherein the child inherits from a single parent
Single Inheritance
occurs when there are two or more parent classes from which a child class may inherit.
Multiple Inheritance
the ability for a child class to be the parent class of another. Thus, to stretch the analogy, we have a grandparent, parent, child relationship that describes multilevel inheritance.
Multilevel Inheritance
resembles the classic hierarchical structure of an organisation tree. It has a parent with multiple children.
Hierarchical Inheritance
is simply an amalgam of the other types of inheritance
Hybrid Inheritance
Python also has the ___ function that will make the child class inherit all the methods and properties from its parent
super()