Quiz 2 Flashcards
What is an f-string? and what’s it used for?
Formatted string literal - used to format output of expressions
What is a statement
programming instruction
What is an expression
Code that returns a value (eg. calculations)
What is an assignment
A new variable is assigned
What is a string literal
Text enclosed in quotations
What does an object represent
A value (eg. in x=4, 4 is automatically created as an object)
What does immutable mean? What is mutability?
Immutable means it can’t be changed. Mutability is on object’s ability to be changed.
Which data types are mutable
Which data types are immutable
strings and integers
What is a literal?
A specific value
What is a logical operator/boolean logic?
Treats operands as True or False (for example, x>4 -> True).
What are the three examples of logical operands?
And, or, not
What is a while loop (difference between that and a for loop?)
Loop that goes until we reach a condition
NEED TO ADD AN ITERATOR (i-0)
Could continue forever (for loop has a range)
What is a for loop (difference between that and a while loop?)
Loop that has an end
Automatically puts in the iterator
Has a range (while loop could continue forever)
what is a modulus (example)
%
division with a remainder
ex: 7%2 = 1
== vs =
== - checking IF something is true (x==5, does x equal 5?)
= - this IS true (ex: x=5)
What is a value?
Content being stored (types: strings, integers…)
What is a float
decimal
What is an integer?
whole number
What does // do?
how many times the divider can fit in
ex: 5//2 = 2
2 can fit in 5 twice
What is pseudocode
Explanation in sentences
Do you need to add an iterator for a for loop?
No! (automatically knows)
what happens if we had a break into the loop?
It skips the else statement
What does n += 1 mean?
n = n + 1
what is len ( ) used for?
finding the length of a string
What does a 0 indexed language mean?
python starts from 0 when counting (not from 1)
What is included in this range? for i in range (0, 5)
0, 1, 2, 3, 4
what does the escape sequence \n do? ex: print(“My name…\nIs John..”)
makes a newline
Ex: My name…
Is John..
What does the escape sequence \t do? ex: print(“1. Bake cookies\n\t1.1. Preheat oven”)
Tab (indent)
1. Bake cookies
(tabed)1.1. Preheat oven
What is a raw string?
ex what will this print out?: raw_sequence = r’ Hello, my name is \n Lydia’
print(“raw_sequence”)
when escape sequences are ignored
ex: Hello, my name is \n Lydia
What does the escape sequence \ do?
ex what will this print out?: print(“\home\users\”)
prints a \
ex: \home\users\
What does the escape sequence ' do?
ex what will this print out?: print(“Name: John O'Donald”)
prints a single quote ‘
Name: John O’Donald
What does the escape sequence " do?
ex what will this print out?: print(“He said, "Hello friend!"”)
prints a double quote “
He said, “Hello friend!”
What does ord ( ) do?
returns encoded integer value for a string of length one
ex: ord(‘A’) = 65
What does chr( ) do?
returns a string of one character for an encoded integer
ex: chr(‘65’) = A
What are the 4 types of f-strings (that we’re using)?
d - Decimal (for integer values only)
e - Exponent notation
f - Fixed point notation
s - string (default presentation type)
What is :d specification
What will this print?
ex: number = 4
print(f’ {number:d} ‘ )
d - Decimal (integer values only)
will print: 4
What is :e specification
what will this print?
ex: number = 44
print(f’ {number:e} ‘ )
e - exponent notation
will print: 4.400000e+01
What is :f specification
what will this print?
ex: number = 4
print(f’ {number:f} ‘ )
f - fixed-point notation
will print: 4.000000
What is in an advanced f-string?
(f”{Expression:[fill][Align][Sign][Width][.Precision][type]}
my_num = 123.987
ex: (f”{my_num:+08.2f}”)
print: +0123.99
fill - 0’s or white spaces
align - sign to know where the answer goes (don’t worry about this as much)
sign - adding a +/- in front
width - how many spots there are (ex: 8)
.Precision - how many decimals
type - f, s, d, e
What does print( ) do?
Prints a newline (same as \n)