Skip to content

Instantly share code, notes, and snippets.

@TomColBee
Created June 17, 2018 16:40
Show Gist options
  • Save TomColBee/c0a3329544eb31310a3e641f6c4faea0 to your computer and use it in GitHub Desktop.
Save TomColBee/c0a3329544eb31310a3e641f6c4faea0 to your computer and use it in GitHub Desktop.
Python Bible: For Loops Examples
# print numbers 1 to 50
for number in range(1,50):
print(number)
# print letters a,b,c,d
for letter in 'abcd':
print(letter)
# Count number of vowels and consonants
vowels = 0
consonants = 0
for letter in "Hello":
if letter.lower() in "aeiou":
vowels = vowels+1
elif letter == " ":
pass
else:
consonants = consonants + 1
print("There are {} vowels and {} consonants in this string.".format(vowels,consonants))
# Create dictionary students with two keys, male and female
students = {
"male": ["Tom", "Charlie", "Harry", "Frank"],
"female": ["Sarah", "Huda", "Samantha", "Emily", "Elizabeth"]
}
# Only print out students with an "a" in their name
for key in students.keys():
# for each name, if "a" is in the name, then print the name
for name in students[key]:
if "a" in name:
print(name)
# print the keys
# print(key)
# print the students in each key
# print(students[key])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment