首页 > 编程语言 > 详细

Learn Python 012: for loop

时间:2017-07-16 17:48:48      阅读:251      评论:0      收藏:0      [点我收藏+]
# 1

vowels = 0
consonants = 0

for letter in ‘supercalifragilisticexpialidocious‘:
    if letter.lower() in ‘aeiou‘:
        vowels = vowels + 1
    elif letter == ‘ ‘:
        pass
    else:
        consonants = consonants + 1
print(‘There are {} vowels in the text.‘.format(vowels))
print(‘And there are {} consonants in the text.‘.format(consonants))

# 2

students = {
    ‘male‘: [‘Bob‘, ‘Clark‘, ‘Frank‘, ‘Graig‘],
    ‘female‘: [‘Alice‘, ‘Emma‘, ‘Helen‘]
}
for key in students.keys():
    for name in students[key]:
        if ‘a‘ in name:
            print(name)

 

Learn Python 012: for loop

原文:http://www.cnblogs.com/mxyzptlk/p/7190998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!