Functions Built-in Flashcards

1
Q

mylist = [True, True, True]
x = all(mylist)
print(x)

A

True

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

mylist = [False, True, False]
x = any(mylist)
print(x)

A

True

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

x = ascii(“My name is Ståle”)

print(x)

A

‘My name is St\e5le’

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

x = bin(3)

print(x)

A
# The result will always have the prefix 0b
0b11
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

x = bool(1)

print(x)

A

True

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

x = bytearray(2)

print(x)

A

bytearray(b’\x00\x00’)

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

x = bytes(2)

print(x)

A

b’\x00\x00’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
def x():
  a = 5

print(callable(x))

A

True

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

x = chr(97)

print(x)

A

a

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

x = complex(3,5)

print(x)

A

(3+5j)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
class Person:
  name = "John"
  age = 36
  country = "Norway"

delattr(Person, ‘age’)

A

The age attribute will be deleted from the Person class

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

x = dict(name = “John”, age = 36, country = “Norway”)

print(x)

A

{‘name’: ‘John’, ‘age’: 36, ‘country’: ‘Norway’}

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

x = divmod(5, 2)

print(x)

A

(2, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
x = ('apple', 'banana', 'cherry')
y = enumerate(x)

print(list(y))

A

[(0, ‘apple’), (1, ‘banana’), (2, ‘cherry’)]

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

ages = [12, 17, 18, 24]

adults = filter(lambda a: a>=18, ages)
for x in adults:
print(x)

A

18

24

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

x = float(3)

print(x)

A

3.0

17
Q

x = format(0.5, ‘%’)

print(x)

A

50.000000%

18
Q

mylist = [‘apple’, ‘banana’, ‘cherry’]
x = frozenset(mylist)
print(x)

A

frozenset({‘apple’, ‘banana’, ‘cherry’})

19
Q
class Person:
  name = "John"
  age = 36
  country = "Norway"

x = getattr(Person, ‘age’)

print(x)

A

36

20
Q
class Person:
  name = "John"
  age = 36
  country = "Norway"

x = hasattr(Person, ‘age’)

print(x)

A

True

21
Q

x = hex(255)

print(x)

A
#'0x' prefixes everything
0xff
22
Q

What is id()?

A

Returns a memory address. It will be different every time.

23
Q

print(“Enter your name:”)

x = input()

A

Allows the user to input stuff

24
Q

x = isinstance(5, int)

print(x)

A

True

25
Q
class myAge:
  age = 36
class myObj(myAge):
  name = "John"
  age = myAge

x = issubclass(myObj, myAge)

print(x)

A

True

26
Q

x = iter([“apple”, “banana”, “cherry”])
print(next(x))
print(next(x))
print(next(x))

A

apple
banana
cherry

27
Q

x = map(lambda a: len(a), (‘apple’, ‘banana’, ‘cherry’))

print(list(x))

A

[5, 6, 6]

28
Q

x = oct(200)

print(x)

A
#Always starts with '0o'
0o310
29
Q

f = open(“demofile.txt”, “r”)

print(f.read())

A

Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

30
Q

x = ord(“h”)

print(x)

A
#converts to unicode. opposite of ascii().
104
31
Q

x = pow(4, 3)

print(x)

A

64

32
Q

alph = [“a”, “b”, “c”, “d”]

ralph = reversed(alph)

for x in ralph:
print(x)

A

d
c
b
a

33
Q
class Person:
  name = "John"
  age = 36
  country = "Norway"

setattr(Person, ‘age’, 40)

The age property will now have the value: 40

x = getattr(Person, ‘age’)

print(x)

A

40

34
Q

a = (“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”)

x = slice(2)

print(a[x])

A

(‘a’, ‘b’)

35
Q

a = (“b”, “g”, “a”, “d”, “f”, “c”, “h”, “e”)

x = sorted(a)

print(x)

A

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]

36
Q

x = tuple((“apple”, “banana”, “cherry”))

print(x)

A

(‘banana’, ‘cherry’, ‘apple’)

37
Q
a = ["John", "Charles", "Mike"]
b = ["Jenny", "Christy", "Monica"]

x = zip(a, b)

print(list(x))

A

[(‘John’, ‘Jenny’), (‘Charles’, ‘Christy’), (‘Mike’, ‘Monica’)]

38
Q
def hacker_speak(txt):
	return txt.translate(str.maketrans('aeis', '4315'))

print(hacker_speak(‘games for life!’))

A

g4m35 f0r l1f3!

39
Q

print(eval(‘3*2’))

A

6