1/44
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What does Python do with anything after the pound (#) sign?
It ignores it
Benefits of writing comments in Python
Understand the code that was written at a later time
Helps others to read the code
Temporarily “comment out” code instead of deleting it
Variable
A name that refers to a value in the computer's memory
What does the assignment operator (=) do?
The name on the left gets the value on the right
How should “=” be read in Python?
“Gets”
Compute the right side first and then attach the name to the result
What must happen before you can use a variable?
You must assign a value before you use it
What happens when you reassign a variable?
Reassigning replaces the old value
If count = 10, what does count = count + 1 do?
The right side is computed first as 11, then count gets 11
What does count =+ 1 mean?
It is shorthand for count = count + 1
Other shorthand assignment operators introduced
-=
*=
/=
What naming rules does Python enforce for variables?
Start with a letter or underscore
No spaces
No $, #, or @ symbols
Cannot start with numbers
Variable names are case-sensitive
Python keywords cannot be used
What naming conventions were introduced for variables?
snake_case (name_of_variable)
CamelCase (Name of Variable)
Why is pet_name better than thenameofpet?
It’s shorter, easier to scan, and harder to mistype
What data type is used for whole numbers (integers)?
Int — used for counting and when fractions cannot occur
What data type is used for fractional numbers (decimals)?
float — used for measurements and money
True or False: You do not declare the data type of a variable in Python. Python decides it from the value you assign.
True
String
A sequence of characters (letters, digits, symbols) treated as one unit
What is the data type for a string?
str
How must string literals be written?
They must be wrapped in matching quotes, either ‘ ‘ or “ “
Concatenation
Joining two or more strings with +
What are the two jobs of the + symbol?
Adds numbers
Joins strings
Which one happens depends on the type
What does the type() function do?
It tells you what Python thinks a value is
Why is year = “2023” a string rather than a number?
The quotes decide, not the characters inside
When code behaves strangely, what is almost always the fastest first move?
Check type()
What do int(), float(), and str() do?
They build a new value of the requested type
What is the result of “5” + “3” and why?
“53” because the two values are strings and are joined together
What is the result of “5” * 3
“555” — the string is repeated three times
Why would int(year) + 1 work if year = “2023”?
int(year) converts the string to an integer first, then Python can do the math
When can conversion between types fail?
When the value cannot be converted to the requested type. For example, float(“abc”) raises a ValueError because the text has to look like a number
What data type does everything typed by a user arrive as?
A string (str)
//
Integer Division
**
Exponent
%
Remainder
Order of Precedence for Python Math
() —> ** —> *, /, //. % —> +, -
True or False: / always produces a float, even when the division is exact
True
True or False: Use / if you want a whole number
False
4 / 2
2.0
4 / / 2
2
Floats are ___________, not exact decimals
Approximations
Dividing by zero creates what type of error?
A ZeroDivisionError
What are Python’s comparison operators?
>, <, <=, >=, ==, !=
What data type results from a comparison?
bool
What are the two values of bool?
True
False
When an expression contains both calculations and comparisons, which happens first?
Calculations
== vs. =
== asks a question while = gives an instruction