Chapter 3 part 5 Flashcards
Lets recap what the range function does
Produces a sequence of consecutive integers from 0 up to, but NOT including the arguments value.
What are the 2 arguments of the range function?
- produces a sequence of consecutive integers 0 up to but NOT including the artument’s value
- 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
What are the 3 arguments of the range function?
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
What happens if the third argument is negative in a range function?
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
T/F
Function call range(1, 10) generates the sequence 1 through 10
False. Function call range(1, 10) generates the sequence 1 through 9
What happens if you try to print the items in range (10, 0, 2)?
Nothing displays because the step is not negative. This is not a fatal error.
Things to know about decimals for menetary amounts in Python
- 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
What are the Python Standard Library’s modules?
Groups of related capabilities
The decimal module defines type Decimal and its capabilities
How do you use capabilities from a module?
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
What is the >2 mean here: print(f’{year:>2}
The right aligned (>) indicates to align the year to the right and with a field width of 2 character positions
What is the ** < 10.2 ** mean here:
print(f’{amount:>10.2f)’)
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
A field width specifies the ___blank___ to use when displaying a value
number of character positions
What happens when you execute a break statement in a while or for statement?
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
What does continue do in a while or for statement?
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
Use Boolean operators and, or, not to combine 2 conditions example
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