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>
Where does it show the code that caused the exception?
The line that begins with
———-> 1 shows the code that caused the exception
Where does it show the exception that occurred?
Followed by the colon (:) and an error message with more information about the exception
ZeroDivisionError: division by zero
An exception also occurs if you try to use a variable that you have not yet created
How does a remainder operator (%) work?
It will yield the remainder after the left operant is divided by the right operand:
In[12]: 17 % 5
Out[12]: 2
17 divided by 5 yields a quotient of 3 and a remainder of 2. This operator is mostly common used with integers but also can be used with other numeric types like 7.5 % 3.5
Redundant parenthesis
If the presence and absence yields the same result
Rules of operator precedence
Generally the same as those in algebra. Total of 4
1. Expressions in parenthesis evaluate first. Nested parenthesis ( a / (b - c)), the expression in the innermost parenthesis (b - c) evaluates first
2. Exponential operations evaluate next. If several, Python applies them from right to left
3. Multiplication, division and modulus operations evaluate next. All are on the same level of precedence and are evaluated from left to right
4. Addition and subtraction operations evaluate last. Both are on same level of precedence and will be evaluated from left to right
Operators’ grouping
When we have a + b + c, since all are on same level of precedence, we will evaluate from left to right in a manner of groups
So we first do a + b
Then that result + c
Exponential operator grouping
This is the only one that groups from right to left
What is a mixed-type expression?
Expressions containing an integer and a floating-point number. These always produce a floating-point number.
Given that y = ax^3 + 7, which of the following is not a correct statement for this equation?
A. Y = a * x * x * x + 7
B. Y = a * x ** 3 + 7
C. Y = a * (x * x * x) + 7
D. Y = a * x * (x * x + 7)
D. Is incorrect
T/F in nested parentheses, the expression in the innermost pair evaluates last
False, the expression in the innermost parentheses evaluates first
Evaluate the expression 3 * (4 - 5) with and without parentheses. Are the parentheses redundant?
No
With will yield -3
Without will yield 7
Evaluate the expression 4 ** 3 ** 2, (4 ** 3) ** 2 and 4 ** (3 ** 2). Are there any parentheses redundant?
In[3]: 4 ** 3 ** 2
Out[3]: 262144
In[4]: (4 ** 3) ** 2
Out[4]: 4096
In[5]: 4 ** (3 ** 2)
Out[5]: 262144
Only the last expression is redundant