count =0for i in [1, 3, 6, 10, 15, 21]: count = count +1print(count)
Sum
Solution
total =0for num in [1, 3, 6, 10, 15, 21]: total = total + numprint(total)
Counting and Summing
Helpful pattern to understand
Can be easily replaced by len and sum in
simple cases
Mean
Solution
total =0count =0for num in [1, 3, 6, 10, 15, 21]: total = total + num count = count +1mean = total / countprint("Total:", total, "Count:", count, "Mean:", mean)
import mathnums = [1, 4, 7, 12, 3]largest =-math.inffor num in nums:if num > largest: largest = numprint(largest)
Min
Solution
import mathnums = [1, 4, 7, 12, 3]smallest = math.inffor num in nums:if num < smallest: smallest = numprint(smallest)
min function
Solution
import mathdefmin(nums): smallest = math.inffor num in nums:if num < smallest: smallest = numreturn num
Linear Search
Sometimes we want to find a specific value
This can be accomplished by checking items one by one
Check if a word has no vowels
Solution
word =input("Enter a word with no vowels:")valid_word =Truefor letter in word:for vowel in'aeiou':if letter == vowel: valid_word =Falseif valid_word:print("That's correct")else:print("Your word included a vowel.")