diff --git "a/\345\276\252\347\216\257.py" "b/\345\276\252\347\216\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..bdea07dda509340cad41cb92d52a6b19d06a9130 --- /dev/null +++ "b/\345\276\252\347\216\257.py" @@ -0,0 +1,47 @@ +''' +1. 循环的目的:使得计算机可以进行大量重复的运算 +2. python中有两种循环,第一种是for A in B循环,依次把list或者tuple中的元素迭代出来,把B中的元素依次代入A中。 +3. 第二种循环是while循环,只要条件满足,就不断循环,条件不满足时退出循环。 +4. break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环,并直接开始下一轮循环。 + 这两个语句通常都必须配合if语句使用。 +''' + +# for 循环 +# for x in ...循环就是把每个元素代入变量x,然后执行缩进块的语句。 +names = ['Bob', 'Helen', 'Black'] +for name in names: + print(name) + +# 计算1-10整数的和 +sum = 0 +for x in [1,2,3,4,5,6,7,8,9,10]: + sum = x+sum +print(sum) + +# 计算1-100整数的和, range()函数可以用来生成小于101的整数 +sum = 0 +for x in list(range(101)): + sum = x + sum +print(sum) + +# while循环 +sum = 0 +n = 99 +while n > 0: + sum = sum + n + n = n - 2 +print(sum) + +# continue +for i in tuple(range(10)): + if i % 2 == 0: # 利用continue语句跳过偶数 + continue + print(i) + +# break +n = 0 +while n < 10: + print(n) + n = n + 1 + if n > 5: + break \ No newline at end of file diff --git "a/\346\235\241\344\273\266\345\210\244\346\226\255.py" "b/\346\235\241\344\273\266\345\210\244\346\226\255.py" new file mode 100644 index 0000000000000000000000000000000000000000..1115ccfc0bb455d9c03824c45fa26c3d93369303 --- /dev/null +++ "b/\346\235\241\344\273\266\345\210\244\346\226\255.py" @@ -0,0 +1,23 @@ +''' +1. 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做。 + +''' + +# 年龄判断 +age = 16 +if age >= 18: + print('You age is %d' % age) + print('Adult') +else: + print('You age is %d' % age) + print('Teenager') + +# 分支语句判断 +age = input('Please enter your name: ') +age = int(age) +if age < 2: + print('Baby') +elif age < 18: + print('Teenager') +elif age < 70: + print('Labor') \ No newline at end of file