Chapter 2 notes Flashcards

1
Q

Python style convention for binary operators

A

Adding a space on each side of the assignment symbol = and binary operators like + to make programs more readable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Identifier

A

A variable name such as X, is an identifier. It may consist of letters, digits and underscores (_) but not begin with a digit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is Python case sensitive?

A

Yes, so number and Number are different identifiers because one begins with a lowercase and the other with upper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Type

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Floating-point number

A

A number with a decimal point, so Python displays float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A _______ performs a task when you _______ it by writing its name, followed by parthenesis, ()

A

Function
Call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The parenthesis contain the functions _______ —the data that the type function needs to perform its task

A

Argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

T/F The following are valid variable names: 3g, 87 and score_4

A

False. Because they begin with a digit, 3g and 87 are invalid names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

T/F Python treats y and Y as the same integer

A

False, Python is case sensitive, so y and Y are different identifiers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write the exponential operator

A

**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True division vs Floor division

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What would be the answer for
In[9]: -13 // 4

A

Out[9]: -4
Floor division gives the closes integer that’s not greater than -3.25 which is -4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when you divide by 0 with / or //?

A

Not allowed and results in an exception - a sign that a problem occurred
Python reports an exception with a traceback

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a traceback do?

A

It indicates that an exception of type ZeroDivisionError occurred-most exception names end with Error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In this image, where is the exception located?

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where does it show the code that caused the exception?

A

The line that begins with
———-> 1 shows the code that caused the exception

17
Q

Where does it show the exception that occurred?

A

Followed by the colon (:) and an error message with more information about the exception

ZeroDivisionError: division by zero

18
Q

An exception also occurs if you try to use a variable that you have not yet created

A
19
Q

How does a remainder operator (%) work?

A

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

20
Q

Redundant parenthesis

A

If the presence and absence yields the same result

21
Q

Rules of operator precedence

A

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

22
Q

Operators’ grouping

A

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

23
Q

Exponential operator grouping

A

This is the only one that groups from right to left

24
Q

What is a mixed-type expression?

A

Expressions containing an integer and a floating-point number. These always produce a floating-point number.

25
Q

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)

A

D. Is incorrect

26
Q

T/F in nested parentheses, the expression in the innermost pair evaluates last

A

False, the expression in the innermost parentheses evaluates first

27
Q

Evaluate the expression 3 * (4 - 5) with and without parentheses. Are the parentheses redundant?

A

No
With will yield -3
Without will yield 7

28
Q

Evaluate the expression 4 ** 3 ** 2, (4 ** 3) ** 2 and 4 ** (3 ** 2). Are there any parentheses redundant?

A

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

29
Q
A