W3 - Formatting Flashcards

1
Q

What is a script?

A

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!)

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

what is a program

A

code in a script

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

there are three code execution types: individually, in code cells, and in scripts. which does jupyter notebooks do?

A

code cells!

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

files with scripts are named with…

A

a .py at the end

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

what is a docstring

A

strings in 3 quotation marks, for longer comments and descriptions of user-made functions

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

write a prompt asking me to type something, then if i type ‘something’ it will print ‘bro you suck’

A

str = input(‘type something: ‘)
if (str == ‘something’):
print(‘bro you suck’)
else:
print(‘thanks’)

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

can == be used on python strings?

A

YES!

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

true/false: print lines create a new line after every finished print

A

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

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

what type are inputs returned as by the input function?

A

strings and strings ONLY

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

how to put a quotation mark in a print

A

' (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

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

what is an f-string?

A

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

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

true/false: The input function can read directly into an integer variable

A

false

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

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

A and C

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

what is worn code?

A

disgusting code understood only when you write it, never understandable w/o comments (write once, read never)

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

3 ways to print int variable in str with/without format: (no typecasting!)

A

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

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

strings are _____ justified, ints are ______ justified, and floats are ______ justified

A

left, right, right

17
Q

what is the string format specifier? float? int?

A

s, f, d

18
Q

print a float with a field width of 8 and 2 decimal numbers

A

print(f”The float entered is {myfloat:8.2f}”)

19
Q

what are the g and e specifiers?

A

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

20
Q

f vs g operator

A

f rounds to 6 decimals, g to 5

21
Q

print the float variable myfloat in e format

A

print(f’{myfloat:e}’)

22
Q

Write statements that will prompt the user for their favorite integer, and store it in an integer variable named favint.

A

string = input(“what’s your favorite integer? “)
favint = int(strint)

23
Q

how do you justify a formatted string left or right?

A

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

24
Q

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

A

false, always includes decimal point

25
Q

what will this return:
print(f’The value is ${79.99:3.2f}.’)

A

The value is $79.99.

python does not remove any digits/the decimal point despite the field width being smaller than the float.

26
Q

what will this print:
print(f’And now for: {myword= :>10s}!!’)

A

And now for: myword= cool!!

27
Q

what is an execution time error?

A

error caused by something found only during runtime (divide by a variable, variable is set to 0)

28
Q

what is the standard python indentation?

A

4 spaces (not tab!)

29
Q

what works as effectively a BOOLEAN version of indexOf() in python?

A

in

30
Q

what is elif?

A

python for else if
if num < 0:
code
elif num == 0:
blah

31
Q

what does a semicolon do?

A

nothing in python, but in jupyter notebook, it suppresses the output thats normally printed out automatically (of the last line)

32
Q

Simplify this statement:
if num < 0:
num = 0;
else:
num = num;

A

if num < 0:
num = 0

33
Q

Simplify this statement:

if val >= 4:
print(‘ok’)
elif val < 4:
print(‘smaller’)

A

if val >= 4:
print(‘ok’)
else
print(‘smaller’)

34
Q

how to check if string is numbers, letters, and letters or numbers

A

x.isdigit()
x.isalpha()
x.isalnum()

35
Q

what does x.islower() and x.isupper() return?
how does this compare to x.lower() and x.upper()?

A

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.

36
Q

how to use in keyword

A

check if x is in y?

x in y

37
Q

are formatted floats rounded or truncated?

A

rounded

38
Q

for i in range(2)

what will the final value of i be?

A

1

39
Q
A