Chapter 3 part 5 Flashcards

1
Q

Lets recap what the range function does

A

Produces a sequence of consecutive integers from 0 up to, but NOT including the arguments value.

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

What are the 2 arguments of the range function?

A
  1. produces a sequence of consecutive integers 0 up to but NOT including the artument’s value
  2. the second argument produces also a consecutive integers from the FIRST argument’s value up to but NOT including the second argument’s value
    Ex:
    for number in range (5, 10):
    ….print(number, end=’ ‘)

5 6 7 8 9

Notice that 10 was not included

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

What are the 3 arguments of the range function?

A

Produces a sequence of integers from its first argument’s value up to, but not including, the second argument’s value, incrementing by the third argument’s value (known as the step
Ex:

for number in range(0, 10, 2):
…print(number, end=’ ‘)

0 2 4 6 8

Notice that we did not include 10 in our result, instead, the increments start at 2 and go up 2 at a time until it reaches as close to the second argument (10) as possible

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

What happens if the third argument is negative in a range function?

A

The sequence progresses from the first argument’s value down to, but not including the second argument’s value, decrementing by the third argument’s value as in:

for number in range(10, 0, -2):
…print(number, end=’ ‘)

10 8 6 4 2

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

T/F

Function call range(1, 10) generates the sequence 1 through 10

A

False. Function call range(1, 10) generates the sequence 1 through 9

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

What happens if you try to print the items in range (10, 0, 2)?

A

Nothing displays because the step is not negative. This is not a fatal error.

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

Things to know about decimals for menetary amounts in Python

A
  • Python’s built-in-floating point numbers work well for most applications
  • Floating-point values are stored in binary format
  • Some floating-point values are represented only approximately when they’re covered to binary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the Python Standard Library’s modules?

A

Groups of related capabilities
The decimal module defines type Decimal and its capabilities

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

How do you use capabilities from a module?

A

You must first import the entire module as in

import decimal

and refer to the Decimal type as decimal.Decimal or you must indicate a specific capability to import using from…import as we do here:

In[4]: from decimal import Decimal

This imports only the type Decimal from the decimal module so that you can use it in your code

You typically create a Decimal from a string

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

What is the >2 mean here: print(f’{year:>2}

A

The right aligned (>) indicates to align the year to the right and with a field width of 2 character positions

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

What is the ** < 10.2 ** mean here:
print(f’{amount:>10.2f)’)

A

This aligns the format to the left with <
The format specifier 10.2f states a field width of 10 with a decimal point and 2 digits to the right of the decimal point (.2)
f is for floating-point number
Formatting the amounts this way aligns their decimal points vertically

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

A field width specifies the ___blank___ to use when displaying a value

A

number of character positions

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

What happens when you execute a break statement in a while or for statement?

A

It immediately exits that statement
Ex:

for number in range(100):
…if number == 10:
……break
…print(number, end=’ ‘)

0 1 2 3 4 5 6 7 8 9

Notice that the if statement tells the program to break or terminate when number is 10 although the range is from 0-99

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

What does continue do in a while or for statement?

A

The loop skips the remainder of the loop’s suite
In a while condition, it is tested to determine whether the loop should continue executing
in a for condition, the loop processes the next item in the sequence (if any)

for number in range(10):
…if number ==5:
……continue
…print(number, end=’ ‘)

0 1 2 3 4 5 6 7 8 9

Notice that 5 was not included in the result

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

Use Boolean operators and, or, not to combine 2 conditions example

A

conditions:
- gender == ‘Female’
- age >= 65
Now combine

gender = ‘Female’
age = 70
if gender == ‘Female’ and age >= 65:
…print(‘Senior female’)

Senior female

Notice we used the Boolean operator and to combine gender and age here
It tests if these conditons are true. In this example, since both are true, expression 1 and expression 2 result in a true condition

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

Write an example of a Boolean operator or example

A

conditions:
- semester average >= 90
- final exam >= 90
Now combine

semester_average = 83
final_exam = 95
if semester_average >= 90 or final_exam >= 90:
…print(‘Student gets an A’)

Student gets an A

The or tests if one or both of the 2 conditions are true

17
Q

What is the difference when you use an and Boolean operator?

A

Python stops evaluating the condition if the entire condition is false
Same thing happens when Python is evaluating an or expression. It stops evaluating it as soon as it knows the entire condition is true
This is called a short-circuit evaluation

Ex:
gender == ‘Female’ and age >= 65

Stops evaluating completely if gender is not equal to ‘Female’

semester_average >= 90 or final_exam >= 90

Stops evaluating completely if semester_average is greater than or equal to 90 b/c then it’s true. If it’s less then execution continues in order to find out if condition is true with the final_exam

18
Q

What does the Boolean operator not do?

A

It ‘reverses’ the meaning of a conditioin - true becomes false and false becomes true
You can avoid using not by using a more known convension such as != which is used to check if 2 values are not equal. It returns true if the values on either side of the operator are different and false if they are the same
Ex:

grade = 87
if grade != -1
…print(‘The next grade is’, grade)

The next grade is 87

19
Q

Assume that i = 1, j = 2, k = 3, m = 2
What does each of the following conditions display?
a. (i >= 1) and (j < 4)

b. (m <= 99) and (k < m)

c. (j >= i) or (k == m)

d. (k + m < j) or (3 - j >= k)

e. not (k > m)

A

a. true
b. false
c. true
d. false
e. false

20
Q

What are the 3 measures of central tendency?

A

Mean
Median
Mode

Each represent a ‘central’ value in a set of values
- A value which is in some sense typical of the others

21
Q

What do the sum and len functions do?

A

sum adds integers
len returns the numbers of total integers
Ex to calc an average:

grades = [85, 93, 45, 89, 85]

sum(grades) / len(grades)
79.4

22
Q
A