General cards Flashcards
How do you find the name attribute?
.__name__
type_name = type_select.\_\_name\_\_
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
tons
could end up a float, which will then confuse the calculations
How do you collect multiple inputs?
uquan = int(input()) usto = [] for _ in range(uquan): usto.append(str(input()))
How do you open and read a file?
my_journal = 0pen('file.txt') # create file object
contents = read(my_journal) # read file text into a string
contents.close() # close the file
Describe file.read([bytes])
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.
Describe file.readlines()
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
What kind of loop is commonly used to iterate through lines of a file?
``` for line in f:
print(line, end=””)
~~~
Describe file.write()
The file.write() method writes a string argument to a file.
Does the file.write() methods accept integers as an argument?
No, they must be converted to strings:
f.write(str(5.75))
What is a ‘mode’ when opening files?
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 do you trim the default new line added by print?
print(f'{ret}', end = "")
describe max([x, y, z], key=abs)
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).
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.
print( 2.1 // 2 )
modulo (integer remainder)
print( 5 % 2 )
capitalizes first letter of each word
courseProper = myCourse.title()
determines if the string consists of lowercase characters
myCourse.islower()
slice a string
name[ start : stop ]
split method, returns a list
mySplitPhraseList = myNewPhrase.split(“ “)
creates a new list (via a slice) with the second and third values from myNumbers
yourNumbers = myNumbers[1:3]
creates a new list (slice!) with 1st, 2nd, and 3rd values from myNumbers
altNumbers = myNumbers[:3]
creates a new list (sliced again!) with 4th, 5th, and 6th values from myNumbers
newNumbers = myNumbers[3:]
looks for a match and returns Boolean value.
matchList = 9.2 in myNumbers
returns copy of list ordered largest to smallest
listSortAgain = sorted(myNumbers, reverse=True)
returns copy of list ordered smallest to largest
listSort = sorted(myNumbers)