https://github.com/benevolentprof/inf1340-2015-notebooks
In [ ]:
x = 1
y = 2
y != x
In [ ]:
if temperature < 20 and minutes > 12:
print("The temperature is in the danger zone.")
In [ ]:
if temperature < 20 or temperature > 100:
print("The temperature is too extreme.")
In [ ]:
if not(temperature > 100):
print("This is below the maximum temperature.")
In [1]:
x = 5
y = 6
a = 1000
b = 2000
x > y and a < b
Out[1]:
In [ ]:
# Not correct
if age > 18 :
print("You can vote")
# Correct
if age >= 18 :
print("You can vote")
# Correct, but less desirable
# Possibly incorrect if age is a float
if age > 17 :
print("You can vote")
while
, a Condition-Controlled Loopfor
, a Count-Controlled Loop vowel_or_consonant()
function from last class
In [3]:
def vowel_or_consonant():
"""
Exercise: Vowel or Consonant
Reads a letter of the alphabet from the user. (You can assume that it's
lowercase.) If the user enters a, e, i, o or u then your program should
display "vowel". If the user enters y then your program should display
"sometimes a vowel, sometimes a consonant". Otherwise your program should
display a message indicating that the letter is a "consonant".
"""
vowels = list("aeiou")
letter = raw_input("Input a letter of the alphabet: ")
if letter in vowels:
print("vowel")
elif letter == "y":
print("sometimes a vowel, sometimes a consonant")
else:
print("consonant")
In [5]:
vowel_or_consonant()
vowel_or_consonant()
vowel_or_consonant()
In [7]:
# Write a program that will echo back what the user types in
# Inputs: string from user, choice on whether to continue
# Processing: None
# Output: print string from user
# Create a variable to control the loop
keep_going = "y"
while keep_going == "y":
user_input = raw_input("What did you say? ")
print(user_input)
keep_going = raw_input("Again? Enter y ")
In [9]:
# Write a program that will echo back what the user types in
# Inputs: string from user, choice on whether to continue
# Processing: None
# Output: print string from user
# Create a variable to control the loop
keep_going = "y"
while keep_going == "y":
keep_going = raw_input("Again? Enter y ")
In [ ]:
# Don't do this
# This is an infinite loop, because loop condition never changes
while True:
print("One Infinite Loop")
In [10]:
for number in [1, 2, 3, 4, 5]:
print(number)
In [11]:
for animal in ["giraffe", "otter", "bears", "panda", "capybara", "elephant", "python"]:
print(animal)
In [12]:
for element in [5, "string", 75.62]:
print(element)
In [13]:
# These two loops are not the same!
for number in [1, 2, 3, 4, 5]:
print(number)
for number in range(5):
print(number)
In [15]:
for number in range(1, 6):
print(number)
In [18]:
for number in range(1, 50, 7):
print(number)
Can use a variable to control the number of times a for loop iterates
In [ ]:
iterations = raw_input("How many times? ")
iterations = int(iterations)
for count in range(iterations):
print(count)
In [31]:
# This program calculates the sum of a series of
# numbers entered by the user
iterations = raw_input("How many times? ")
iterations = int(iterations)
sum = 0
product = 1
for count in range(1, iterations + 1):
sum += count
product *= count
print(sum)
print(product)
In [ ]:
# Let's re-write the examples
# Example: x = x + 1
x = 5
x += 1
# Example: total = total + number
total += number
# Example: balance = balance - withdrawal
balance -= withdrawal
The greatest common divisor of two positive integers, n and m, is the largest number, d, that divides evenly into both n and m. Here is one algorithm to solve this problem.
Initialize d to the smaller of m and n
While d does not evening divide m or d does not evening divide n do
Decrease the value of d by 1
Report d as the greatest common divisor of n and m
Write a program that reads two positive integers from the user and uses this algorithm to determine and report their greatest common divisor.
Enter a positive integer: 5
Enter a positive integer: 10
The greatest common divisor of 5 and 10 is 5.
In [ ]: