W3 - Formatting Flashcards
What is a script?
collection of code in a single file, non-interactive (run all at once, like java, and none of the weird python interaction weve been doing lately) (and also no return unless printed!)
what is a program
code in a script
there are three code execution types: individually, in code cells, and in scripts. which does jupyter notebooks do?
code cells!
files with scripts are named with…
a .py at the end
what is a docstring
strings in 3 quotation marks, for longer comments and descriptions of user-made functions
write a prompt asking me to type something, then if i type ‘something’ it will print ‘bro you suck’
str = input(‘type something: ‘)
if (str == ‘something’):
print(‘bro you suck’)
else:
print(‘thanks’)
can == be used on python strings?
YES!
true/false: print lines create a new line after every finished print
false though it does, use the \n or the newline character when not specified, having a comma and the end = ‘whateveryouwantit to end with’ command ends it with whatever you put in there
what type are inputs returned as by the input function?
strings and strings ONLY
how to put a quotation mark in a print
' (single quote string)
or
" (double quoted string)
or
“ OR ‘ (depending on the quotation marks the string uses, it will allow the opposite to exist without explicity using a backslash
what is an f-string?
stands for formatted string
created via a f or F put in front of a string, allows you to put an int into a print or string like this
morality = ‘evil’
print(f’ur mum is {morality}’)
OUTPUT:
ur mum isevil
^note the lack of spaces
true/false: The input function can read directly into an integer variable
false
Which of the following are true? Check all that apply.
A. The input function always reads the user’s input as a string
B. Comments are executed by Python
C. When the newline character is printed, the cursor immediately moves down to the next line
D. Every print function prints a separate line in the output
A and C
what is worn code?
disgusting code understood only when you write it, never understandable w/o comments (write once, read never)
3 ways to print int variable in str with/without format: (no typecasting!)
print(‘Nobody expects the’, unexpected1, ‘!’)
^basic print we’re used to, adds a space as opposed to the formatted prints
print(f’Nobody expects the {unexpected2}!’)
^formatted, thus curly braces
print(f’Nobody expects the{unexpected3:25s}!’)
^formatted with 25 total spaces allotted (and taken up by the string), and as its a string it is put to the left
strings are _____ justified, ints are ______ justified, and floats are ______ justified
left, right, right
what is the string format specifier? float? int?
s, f, d
print a float with a field width of 8 and 2 decimal numbers
print(f”The float entered is {myfloat:8.2f}”)
what are the g and e specifiers?
g is for general format of floats, e is for scientific notation of floats.
g rounds to six total digits (left or right of decimal point). if the # of whole numbers exceeds 6, g converts the float into scientific notation to keep it at 6 digits.
ex.
pi = 3.1415926535
print(“what is {pi:g}”)
would return
what is 3.14159
print(f’The value is ${7223344.9912313123123:g}.’)
would return
The value is 7.22334+e06
or
print(“what is {pi:e}”)
would return
what is 3.141592e+00
f vs g operator
f rounds to 6 decimals, g to 5
print the float variable myfloat in e format
print(f’{myfloat:e}’)
Write statements that will prompt the user for their favorite integer, and store it in an integer variable named favint.
string = input(“what’s your favorite integer? “)
favint = int(strint)
how do you justify a formatted string left or right?
use the > or < symbol (pointing at the direction you want to justify)
ex. print(f’The string is {“hi there”:>12s}!!’)
justifies to the right
print(f’The string is {“hi there”:<12s}!!’)
justifies to the left
true/false: printing a formatted string with a specified field width and a decimal length, such as 6.3f, will result in a value with the length and decimals NOT INCLUDING the decimal point
false, always includes decimal point
what will this return:
print(f’The value is ${79.99:3.2f}.’)
The value is $79.99.
python does not remove any digits/the decimal point despite the field width being smaller than the float.
what will this print:
print(f’And now for: {myword= :>10s}!!’)
And now for: myword= cool!!
what is an execution time error?
error caused by something found only during runtime (divide by a variable, variable is set to 0)
what is the standard python indentation?
4 spaces (not tab!)
what works as effectively a BOOLEAN version of indexOf() in python?
in
what is elif?
python for else if
if num < 0:
code
elif num == 0:
blah
what does a semicolon do?
nothing in python, but in jupyter notebook, it suppresses the output thats normally printed out automatically (of the last line)
Simplify this statement:
if num < 0:
num = 0;
else:
num = num;
if num < 0:
num = 0
Simplify this statement:
if val >= 4:
print(‘ok’)
elif val < 4:
print(‘smaller’)
if val >= 4:
print(‘ok’)
else
print(‘smaller’)
how to check if string is numbers, letters, and letters or numbers
x.isdigit()
x.isalpha()
x.isalnum()
what does x.islower() and x.isupper() return?
how does this compare to x.lower() and x.upper()?
true or false,
.lower() and .upper() return the actual upper and lower values. they could be set equal to the current strings to see if true or false.
how to use in keyword
check if x is in y?
x in y
are formatted floats rounded or truncated?
rounded
for i in range(2)
what will the final value of i be?
1