for Loopsrange Functionrangerangerange Valuesrange function?I have posted homework 6 here.
for Loopsfor loop in Python has the following format
for VARIABLE_NAME in LIST_OF_VALUES:
STATEMENT
STATEMENT
...
for loop works differently from for loops
in other computer languages
for keyword is followed by a variablein keyword followed by a list of valuesfor that prints the powers of 2
$ cat powers_of_2.py
# this program uses a for loop to print the powers of 2
# from 1 to 10
for number in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
value = 2 ** number
print("2 to the power", number, "is", value)
$ python3 powers_of_2.py
2 to the power 1 is 2
2 to the power 2 is 4
2 to the power 3 is 8
2 to the power 4 is 16
2 to the power 5 is 32
2 to the power 6 is 64
2 to the power 7 is 128
2 to the power 8 is 256
2 to the power 9 is 512
2 to the power 10 is 1024
for loop that you can do with this loop in other languagesfor loops makes some things easierrange Functionrange function to make it easy to create a list of integersrange function creates a special kind of list that consists of a sequence of integers
>>> for number in range(10): ... print(number) ... 0 1 2 3 4 5 6 7 8 9
range>>> for number in range(1, 11): ... print(number) ... 1 2 3 4 5 6 7 8 9 10
rangerange function with one or two argumentsrange a third argumentrange how much to add to each value to get the next number in the sequence>>> for number in range(2, 11, 2): ... print(number) ... 2 4 6 8 10
range Valuesrange to print the numbers from 10 down to 1>>> for number in range(10, 2, -1): ... print(number) ... 10 9 8 7 6 5 4 3
>>> for number in range(10, 0, -1): ... print(number) ... 10 9 8 7 6 5 4 3 2 1
range function?for loop in Python is more powerful than the for loop in other languagesfor loop to work on such a list we need a separate function to create itrangeif statement inside another if statement
you can have a loop inside another loop
for loops you must be sure that you use different names for each loop variable
$ cat times_table.py
# This script creates a times table using nested for loops
for row in range(1, 11):
for column in range(1, 11):
entry = row * column
print(entry, end="\t")
print()
$ python3 times_table.py
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
set a variable to 0
for number in the range start value to finish value
add the number to the variable
print the result
>>> total = 0 >>> for number in range(1, 11): ... total = total + number ... >>> print(total) 55
$ cat add_many_numbers.py
# adds a series of numbers entered by the user
# uses a for loop to do this after asking the user
# for the number of entries to be added
entries = int(input("How many entries? "))
total = 0
for entry_no in range(entries):
number = int(input("number: "))
total = total + number
print("Total", total)
$ python3 add_many_numbers.py
How many entries? 5
number: 34
number: 54
number: 123
number: 345
number: 55
Total 611
range to give me a list of valuestotal = total + number
total += number
$ cat add_many_numbers_2.py
# adds a series of numbers entered by the user
# uses a for loop to do this after asking the user
# for the number of entries to be added
entries = int(input("How many entries? "))
total = 0
for entry_no in range(entries):
number = int(input("number: "))
total += number
print("Total", total)
$ python3 add_many_numbers_2.py
How many entries? 5
number: 48
number: 243
number: 53
number: 175
number: 65
Total 584
| Operator | Example | Equivalent To |
|---|---|---|
| += | num += 5 | num = num + 5 |
| -= | num -= 5 | num = num - 5 |
| *= | num *= 5 | num = num * 5 |
| /= | num /= 5 | num = num / 5 |
| //= | num //= 5 | num = num // 5 |
| %= | num %= 5 | num = num % 5 |
| **= | num **= 5 | num = num ** 5 |
>>> number = 5 >>> number += 1 >>> number 6 >>> number -= 1 >>> number 5 >>> number *= 3 >>> number 15 >>> number = 7 >>> number /= 2 >>> number 3.5 >>> number = 7 >>> number //= 2 >>> number 3 >>> number = 7 >>> number %= 2 >>> number 1 >>> number = 2 >>> number **= 3 >>> number 8
$ cat average.py
# this program asks the user how many numbers
# they have to enter, then performs a running
# total and computes the average
entries = int(input("How many entries? "))
total = 0
for entry_no in range(entries):
number = int(input("number: "))
total += number
average = total / entries
print("Average", average)
$ python3 average.py
How many entries? 10
number: 10
number: 9
number: 8
number: 7
number: 6
number: 5
number: 4
number: 3
number: 2
number: 1
Average 5.5
$ cat average_3.py
# this program averages a series of numbers entered
# by the user using a sentinel to indicate the
# end of input
sentinel = 0
total = 0
entries = 0
print("Enter numbers when prompted")
print("When you are done, enter 0")
number = int(input("number: "))
while number != sentinel:
total += number
entries += 1
number = int(input("number: "))
average = total / entries
print("Average", average)
Enter numbers when prompted
When you are done, enter 0
number: 5
number: 4
number: 3
number: 2
number: 1
number: 0
Average 3.0
for loop to
a while loopimport statement
import MODULE_NAME
import statement loads the module code into memorystr, int and float are examples of functions that return a value
>>> result
'5'
>>> result = int('5')
>>> result
5
>>> result = float('5')
>>> result
5.0
print
>>> result = print("Hello world!")
Hello world!
>>> result
>>>
calculate_gross_pay get_hourly_rate calculate_overtime
def FUNCTION_NAME([PARAMETER][...]):
STATEMENT
STATEMENT
...
def
# prints the address of UMB
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
print_umb_address()
$ cat -n umb.py
1 # this program contains a function that
2 # prints the address of our campus
3
4 # prints the address of UMB
5 def print_umb_address():
6 print("University of Massachusetts at Boston")
7 print("100 Morrissey Boulevard")
8 print("Boston, Massachusetts 02125-3393")
9
10 print("I teach at UMass/Boston")
11 print()
12 print_umb_address()
13 print()
14 print("I am the IT Program Director in the Computer Science Department")
$ python3 umb.py
I teach at UMass/Boston
University of Massachusetts at Boston
100 Morrissey Boulevard
Boston, Massachusetts 02125-3393
I am the IT Program Director in the Computer Science Department
print statements on line 10 and 11print statementsprint is built inside the interpreter
# this program prints some information about me
# prints the address of our campus
# prints the address of UMB
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
# prints some information about me
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')
print("Some information about me")
print()
print_personal_info()
print()
print_umb_address()
# this program prints some information about me
# prints the address of our campus
# prints the address of UMB
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
# prints some information about me
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')
def main():
print("Some information about me")
print()
print_personal_info()
print()
print_umb_address()
main()
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')
print("Some information about me")
print()
print_personal_info()
print()
print_umb_address()
print("Some information about me")
print()
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
print_umb_address()
print()
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')
print_personal_info()
# prints the address of UMB def print_umb_address():
# prints the address of UMB def print_umb_address():
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
# prints the address of UMB
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
# prints some information about me
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')
# prints the address of UMB
def print_umb_address():
print("University of Massachusetts at Boston")
print("100 Morrissey Boulevard")
print("Boston, Massachusetts 02125-3393")
# prints some information about me
def print_personal_info():
print('Glenn Hoffman')
print('Information Technology Program Director')
print('Computer Science Department')
print('University of Massachusetts at Boston')
print('ghoffman@cs.umb.edu')
print('Science 3-92A')