Chapter 2 notes Flashcards
Python style convention for binary operators
Adding a space on each side of the assignment symbol = and binary operators like + to make programs more readable
Identifier
A variable name such as X, is an identifier. It may consist of letters, digits and underscores (_) but not begin with a digit.
Is Python case sensitive?
Yes, so number and Number are different identifiers because one begins with a lowercase and the other with upper
Type
Each value has a type that indicates the kind of data the value represents.
In [7]: type(x)
Out[7]: int
In[8]: type(19.5)
Out[8]: float
Floating-point number (float)
Floating-point number
A number with a decimal point, so Python displays float
A _______ performs a task when you _______ it by writing its name, followed by parthenesis, ()
Function
Call
The parenthesis contain the functions _______ —the data that the type function needs to perform its task
Argument
T/F The following are valid variable names: 3g, 87 and score_4
False. Because they begin with a digit, 3g and 87 are invalid names
T/F Python treats y and Y as the same integer
False, Python is case sensitive, so y and Y are different identifiers
Write the exponential operator
**
True division vs Floor division
True division: divides a numerator by a denominator and yields a floating-point number with a decimal point
In[5]: 7 / 4
Out[5]: 1.75
Floor division: divides a numerator by a denominator, yielding the highest integer that’s not greater than the result
In[6]: 7 // 4
Out[6]: 1
Python truncates (discards) the fractional part
What would be the answer for
In[9]: -13 // 4
Out[9]: -4
Floor division gives the closes integer that’s not greater than -3.25 which is -4
What happens when you divide by 0 with / or //?
Not allowed and results in an exception - a sign that a problem occurred
Python reports an exception with a traceback
What does a traceback do?
It indicates that an exception of type ZeroDivisionError occurred-most exception names end with Error.
In this image, where is the exception located?
The snippet number that caused the exception is specified by the 10 in the line
<ipython-input-10-cd759d3fcf39> in <module>()
</module></ipython-input-10-cd759d3fcf39>