General cards Flashcards

1
Q

How do you find the name attribute?

A

.__name__

type_name = type_select.\_\_name\_\_
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why won’t pounds update correctly in this block?

elif ounces > 16 :
    pounds = ounces/16
    if pounds > 2000:
        tons = pounds/2000
        pounds =  pounds - (2000 * tons)
        remaining_ounces = (pounds -int(pounds)) * 16
A

tons could end up a float, which will then confuse the calculations

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

How do you collect multiple inputs?

A
uquan = int(input())
usto = []
for _ in range(uquan):
    usto.append(str(input()))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you open and read a file?

A
my_journal = 0pen('file.txt') # create file object

contents = read(my_journal) # read file text into a string
contents.close()  # close the file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe file.read([bytes])

A

The file.read([bytes]) method returns the file contents as a string.
The optional ‘bytes’ argument allows you to specify the size in memory you want to read.

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

Describe file.readlines()

A

The file.readlines() method returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on

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

What kind of loop is commonly used to iterate through lines of a file?

A

``` for line in f:
print(line, end=””)
~~~

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

Describe file.write()

A

The file.write() method writes a string argument to a file.

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

Does the file.write() methods accept integers as an argument?

A

No, they must be converted to strings:

f.write(str(5.75))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a ‘mode’ when opening files?

A

A mode indicates how a file is opened, e.g., whether or not writing to the file is allowed, if existing contents of the file are overwritten or appended, etc.

The most used modes are ‘r’ (read) and ‘w’ (write). The mode is specified as the second argument in a call to open(), e.g., open(‘myfile.txt’, ‘w’) opens myfile.txt for writing. If mode is not specified the default is ‘r

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

How do you trim the default new line added by print?

A
print(f'{ret}', end = "")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

describe max([x, y, z], key=abs)

A

The line max([x, y, z], key=abs) is a concise and efficient way to determine the value among x, y, and z with the largest magnitude (absolute value), while preserving the original sign of the number.

Explanation:
[x, y, z]:

Creates a list of the three input numbers for comparison.
max(…, key=abs):

The max function is used to find the maximum value in the list.
The key=abs argument tells max to compare the absolute values of the numbers, rather than their actual values.
Return Value:

Once the maximum value by absolute magnitude is identified, max returns the original number (including its sign).

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

floor division, gives the result of the last even division. Result will be a “mathematical integer” but whether it’s a Python int or float depends on numbers used.

A

print( 2.1 // 2 )

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

modulo (integer remainder)

A

print( 5 % 2 )

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

capitalizes first letter of each word

A

courseProper = myCourse.title()

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

determines if the string consists of lowercase characters

A

myCourse.islower()

17
Q

slice a string

A

name[ start : stop ]

18
Q

split method, returns a list

A

mySplitPhraseList = myNewPhrase.split(“ “)

19
Q

creates a new list (via a slice) with the second and third values from myNumbers

A

yourNumbers = myNumbers[1:3]

20
Q

creates a new list (slice!) with 1st, 2nd, and 3rd values from myNumbers

A

altNumbers = myNumbers[:3]

21
Q

creates a new list (sliced again!) with 4th, 5th, and 6th values from myNumbers

A

newNumbers = myNumbers[3:]

22
Q

looks for a match and returns Boolean value.

A

matchList = 9.2 in myNumbers

23
Q

returns copy of list ordered largest to smallest

A

listSortAgain = sorted(myNumbers, reverse=True)

24
Q

returns copy of list ordered smallest to largest

A

listSort = sorted(myNumbers)

25
removes last item from the list
myNames.pop()
26
removes second item from the list
myNames.pop(1)
27
a string is used to join together values from list
listJoined = ", ".join(myNames)
28
creates a new set, empty
idNums = set()
29
adds value to set
idNums.add(12345)
30
removes value from set, if found
idNums.discard(12345)
31
set(myNumbers)
converts a list to a set
32
list(idNums)
converts a set to a list
33
in verifies if key is in dictionary
if 'Igor' in studentGrades:
34
print( studentGrades.get('Bismark') )
get method looks up value by key