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