honPyt Flashcards
(166 cards)
While using Python in interactive mode, which variable is the default prompt for continuation lines?
…
While using Python in interactive mode, which variable holds the value of the last printed expression?
_ (underscore)
With the variable assignment name=”Python”, what will be the output of print(name[-1])?
“n”
What would be the output of this code?
def welcome(name): return "Welcome " + name, "Good Bye " + name
wish = welcome(“Joe”)
print(wish)
(Brainscape doesn’t seem to allow indentation, so just pretend this is all indented properly)
(‘Welcome Joe’, ‘Good Bye Joe’)
What would be the output of this code?
A = 15 B = 10 C = A/B D = A//B E = A%B print(C) print(D) print(E)
1.5
1
5
What does the “//” operator do?
Floor division (or integer division), a normal division operation except that it returns the largest possible integer.
(this behaves differently when negative numbers are involved)
What would be the output of this code?
Elements=["ether", "air", "fire", "water"] print(Elements[0]) print(Elements[3]) print(Elements[2]) Elements[2] = "earth" print(Elements[2])
ether
water
fire
earth
What would be the output of this code?
data = (x*10 for x in range(3)) for i in data: print(i) for j in data: print(j)
(Brainscape doesn’t seem to allow indentation, so just pretend this is all indented properly)
0
10
20
(this only returns the first loop, as generator data can only be used once)
What would be the output of this code?
names = ['a', 'b', 'c'] names_copy = names names_copy[2] = 'h' print(names) print(names_copy)
[‘a’, ‘b’, ‘h’]
[‘a’, ‘b’, ‘h’]
In Python, Assignment statements do not copy objects, they create bindings between a target and an object
It only creates a new variable that shares the reference of the original object
What would be the output of this code?
x = 3 + 2j
y = 3 - 2j
z = x + y
print(z)
(6+0j)
What would be the output of this code?
for i in range(3):
print(i)
else:
print(“Done!”)
(Brainscape doesn’t seem to allow indentation, so just pretend this is all indented properly)
0
1
2
Done!
What is the slicing operator in Python?
:
What does trunc() do?
trunc() rounds down positive floats, and rounds up negative floats
math. trunc(3.5) # 3
math. trunc(-3.5) # -3
Can you use remove() to delete an element from an array by giving it an index number?
No
What keyword can be used to remove an item from a list based on its index?
del
numbers = [50, 60, 70, 80]
del numbers[1] # numbers = [50, 70, 80]
How would you use pop() to remove the first element of an array?
pop(0)
numbers = [50, 60, 70, 80]
numbers.pop(0) # [60, 70, 80]
What would be the output of this code?
a, b, c, d = 1, ‘cat’, 6, 9
print(c)
6
What would be the output of this code?
a, b, c, d = 1
print(c)
TypeError: cannot unpack non-iterable int object
What is the lambda keyword used for?
It is used for creating simple anonymous functions
What keyword can be used to force a particular exception to occur?
raise
Which module will process command line arguments for a Python script?
os
What is the syntax of a lambda function on Python?
lambda arguments: expression
How would you make a deep copy of an array?
Use copy()
arr2 = arr1.copy()
Using filter() how would you use lambda to select all numbers over 5 from array my_list?
filter(lambda x: x > 5, my_list)