Contents
- more_range_examples.py
- range_analysis.py
- simple_for.py
- sum5.py
more_range_examples.py 1/4
[top][prev][next]
# More range examples
# Sara Sprenkle
print("-------------- range(1, 15, 3) ------------")
for a in range(1,15,3):
print(a)
print("-------------- range(5, -15, -5) ------------")
for b in range(5, -15, -5):
print(b)
# Demonstrate these after handout ...
# Won't display anything
print("-------------- range(5, -15, 5) ------------")
for counter in range(5, -15, 5):
print(counter)
# Won't display anything
print("-------------- range(-5, 15, -5) ------------")
for counter in range(-5, 15, -5):
print(counter)
print("-------------- range(5.5, 15, 1.5) ------------")
# Note that range expects integer values
for counter in range(5.5, 15, 1):
print(counter)
range_analysis.py 2/4
[top][prev][next]
# Example of for loops using range
# by Sara Sprenkle
# Question: what does range do?
for i in range(10):
squared = i * i
print(i , "^2 =", squared)
print(i)
# QUESTION FOR CLASS:
# How is i changing each time through the loop?
simple_for.py 3/4
[top][prev][next]
# Examples of for loops using range
# by Sara Sprenkle
# The "chorus" gets repeated 5 times
for i in range(3):
print("You say 'hello'")
print("And, I say 'goodbye'...")
num_repetitions = 5
print()
# for loop with only one statement that gets repeated
for x in range(num_repetitions): print("Repeat the chorus!")
sum5.py 4/4
[top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111, 09.19.2012
print("This program will add up 5 numbers given by the user.")
total = 0
for i in range(1, 6): # alternatively, for i in range(5):
num = eval( input("Enter number " + str(i) + ": ") )
total = total + num
print("The total is", total)
Generated by GNU enscript 1.6.4.