Misc. Flashcards

(30 cards)

1
Q

//

What does the above do

A

Floor Division (returns a whole number rounded down)

The whole number could be an int or a float

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

What’s the short-command for quick-save

A

Ctrl + s

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

What’s the best case to use when naming .py files

A

lower-case letters with underscores

E.g.,

python_app.py

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

What is the best naming convention when defining a constant

A

ALL_CAPITALS_WITH_UNDERSCORES

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

How do you indent multiple lines at the same time

A

Highlight all the lines then hit tab

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

When importing functions from modules, you will generally need to use dot notation, e.g.,

import processor
print(processor.function_1)

How can you import functions and NOT have to use dot notation

A

from processor import function_1

print(function_1)

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

Short command for redo

A

Ctrl + Shift + Z

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

are “num // num” & “math.floor(num / num)” identical

A

Both divide & round down, but…

“num // num” will return either an int or a float, but “ “math.floor(num / num)” always returns an int

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

“or” returns True if either condition is true. But what if both conditions are true

A

“or” will also return True

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

What does input() do

A

It pauses the program and waits for the user to type something, then returns that input as a string.

E.g.,
name = input(“What is your name? “)
print(“Hello, “ + name)

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

What is the difference between “continue”, “pass”, and “break”

A

break: Exits the loop completely

continue: Skips the rest of the current loop iteration and moves to the next one

pass: Does nothing — it merely avoids syntax errors

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

Return infinity

A

return float(“inf”)

or

import math
return math.inf

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

Return negative infinity

A

return float(‘-inf’)

or

import math
return -math.inf

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

What does abs() do

A

abs() removes the sign of the number - always making it positive (or zero)

(It does not round floats)

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

lst = [1, 2, 3]

How would the two operations below affect “lst”?

lst.append([4, 5])

lst.extend([4, 5])

A

append: [1, 2, 3, [4, 5]]

extend: [1, 2, 3, 4, 5]

append() adds one thing to the end of a List.

extend() adds each item from another iterable one by one

Note that append() results in a List within a List, but extend() does not.

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

lst = [1, 2, 3]

How would the two operations below affect “lst”?

lst.append([4])

lst.extend([4])

A

append: [1, 2, 3, [4]]

extend: [1, 2, 3, 4]

Append() results in a List within a List, but extend() does not

17
Q

lst = [1, 2, 3]

1) Can both of the below execute?
2) If so, or if not, why?
3) What result do they individually produce?

lst.append(4)

lst.append([4])

A

1) Yes, both can execute.
2) append() always accepts one thing — no matter what that one thing is. It doesn’t open or unpack that thing.
3) lst.append(4): [1, 2, 3, 4]
lst.append([4]): [1, 2, 3, [4]]

18
Q

lst = [1, 2, 3]

1) Can both of the below execute?
2) If so, or if not, why?
3) What result do they individually produce?

lst.extend(4)

lst.extend([4])

A

1) Only lst.execute([4]) can execute.
2) extend() requires an iterable.
3) lst.extend(4): TypeError
lst.extend([4]): [1, 2, 3, 4]

19
Q

lst = [1, 2, 3]
lst.extend((4))
print(lst)

.extend() can accept any iterable (including Tuples). However, when “lst” is printed, it returns an Error.
1) Why?
2) How can lst.extend((4)) be fixed?

A

1) Python does not recognise “4” as a Tuple just because it’s in parentheses - “4” also requires a comma after it.

2) lst.extend((4,))

20
Q

lst = [ ]
for i in 100:
lst.append(i)

Why does the above return an Error

A

100 is not an iterable

21
Q

Return 2 ways of finding the square root of 25

A

import math
return math.sqrt(25)

or

return 25 ** 0.5

22
Q

What does the filter() function do, and how do you use it?

A

filter(function, iterable) returns only the elements of the iterable for which the function returns True.

E.g.,
def is_even(num):
return num % 2 == 0

numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(is_even, numbers))

.# Result: [2, 4, 6]

23
Q

What happens if you use filter(None, iterable) in Python?

A

It filters out all falsy values from the iterable — like False, None, 0, ‘’, [], etc.

24
Q

def longer_than_three(s):
return len(s) > 3

What build-in function can be used with the function above to keep only the strings longer than 3 characters from the List “words” below.

words = [“cat”, “house”, “sun”, “planet”]

A

filter()

E.g.,
result = list(filter(longer_than_three, words))

.# Result: [‘house’, ‘planet’]

25
def is_positive(n): return n >= 0 What build-in function can be used with the function above to remove all negative numbers from a the List "nums" below. nums = [-5, 3, 0, -2, 8, -1]
filter() E.g., non_negatives = list(filter(is_positive, nums)) ## Footnote .# Result: [3, 0, 8]
26
When should I use "is" vs "=="
Use "==" for simply checking if two values are equal Use "is" for comparing **identities** (i.e., whether two variables refer to the same object in memory) ## Footnote E.g., a = [1, 2] b = [1, 2] a is b # ❌ False (not the same object) vs a = [1, 2, 3] b = a a is b # ✅ True
27
print([1,2] is [1,2]) What does the above print
False ## Footnote "is" compares **identities** (i.e., whether two variables refer to the same object in memory), not for checking if two values are equal per se. E.g., a = [1, 2] b = a a is b # ✅ True
28
a = [1,2] print(a is [1,2]) What does the above print
False ## Footnote "is" compares **identities** (i.e., whether two variables refer to the same object in memory), not for checking if two values are equal per se. E.g., a = [1, 2] b = a a is b # ✅ True
29
a = [1,2] b = a print(a is b) What does the above print
True ## Footnote "is" compares **identities** (i.e., whether two variables refer to the same object in memory), not for checking if two values are equal per se.
30
a, b = a+1, b+1 a, b += 1 a, b += 1, 1 All of the above appear to do the same thing, but which will actually work
a, b = a+1, b+1 & a, b += 1, 1 work. a, b = a+1, b+1 Is most Pythonic