10 月 122020
 
#打印1-100范围内偶数

#判断是否偶数
number = range(1,101) #定义取值范围1-100赋值给变量

for i in number: #定义for循环,元素赋值变量
    if (i%2) == 0: #判断,比较运算,取模为0时为True
        print(i)

print(bool((3%2 == 0))) #False
print(bool((4%2 == 0))) #True

#改造,将偶数写入列表,并打印列表中的元素

#打印1-100范围内偶数

#判断是否偶数
number = range(1,101) #定义取值范围1-100赋值给变量
list = [] #空列表

for i in number: #定义for循环,元素赋值变量
    if (i%2) == 0: #判断,比较运算,取模为0时为True
        #print(i)
        list.append(i) #改造,将偶数写入列表,并打印列表中的元素

print(bool((3%2 == 0))) #False
print(bool((4%2 == 0))) #True
print(list) #打印列表
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
False
True
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 
52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
PS D:\Python>

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)