Expressions and Variables Flashcards
Two types of numbers? Their Functions?
Integers “int()” and decimals “float()”
Arithmetic operators? Symbols?
arithmetic operators \+ plus addition - minus subtraction * times multiplication / divided by division ** power exponentiation
How many decimal places in Python?
15
“Please excuse my dear aunt Sallie” or BEDMAS
Operator Precedence (Parantheses [or brackets], exponent, multiplication, division [equal precedent so do from left to right], addition, subtraction
What is a variable?
Placeholder for important values; used to avoid recomputing values and to give values names that help reader to understand code
Features of a valid variable name?
Consists of letters, numbers, underscores; starts with a letter; case-sensitive (capitalization matters
Eg. legal name: ninja, Ninja, n_i_n_j_a
illegal name: 1337,1337ninja
+= operator
Used to update a variable to a new value . Eg. my_age = my_age + 1 can instead be my_age += 1
Which of the following are syntactically correct strings in Python? Why?
- “This course is great!”
- “It’s a beautiful day.”
- “Hello
- “Goodbye’
- ‘Hello, world.’
Correct:
- “This course is great!”
- “It’s a beautiful day.”
- ‘Hello, world.’
Strings need to start and end with the same type of quotation mark. 3 and 4 don’t.
To display a value in the console, what Python keyword do you use?
What does it mean when a line in Python starts with a “#”.
Eg. #yo
That means it’s a comment.
Which of the following arithmetic expressions are syntactically correct in Python?
- 3 * ((2 - 9) + 4)) * (2 + (1 - 3))
- 8 / -2
- 5 * 3 (7 - 2)
- (7 - 2) / (3 ** 2)
- 5 - 1 - 3 - 7 - 0
- Incorrect. Extra closing parentheses after the 4
- Correct
- Incorrect. Missing an operator between 3 and (7 - 2)
- Correct
- Correct
Which of the following can be used as a variable name in Python?
- ounces
- my.number
- __number__
- my_number
5, MYnumber
6, ounce(s)
- valid
- “my.number” is not valid bc a single variable can’t contain a period; only when the first part is a class
- Valid; though variables rarely start with underscores
- Valid
- Valid, though convention says don’t start a variable with a capital
- “ounce(s)” is not valid bc variables can’t contain parentheses
Assume you have values in the variables x and y. Which statement(s) would result in x having the sum of the current values of x and y?
- y += x
- x = y + x
- x += y
- x += x + y
- Wrong
- Right
- Right
- Wrong