Python4--if-else、while循环
2017-10-29 22:48
357 查看
1
color =
raw_input("你白吗?\n")
2
money =
int(raw_input("你的财产\n”))
# money 转换为int类型
3
beautiful =
raw_input("你美么?\n")
4
5 if color =="白"
and money >=100
and beautiful =="美":
6
print("你是白富美")
7 else:
8
print("你是矮穷矬”)
注意 : if else语句 后面的 : 切勿在中文模式下输入
比较运算符
==
!= —— <> 都是不等于
>=
<=
>
<
逻辑运算符
and a=1 and b=1
or a=1 or b=1
not not(a =1 and b=1)
if—elif—else
if 条件:
xxx
elif条件:
xxx
elif条件:
xxx
else条件:
xxx
例句:
1
age =
int(input("输入你的年龄:\n"))
2 if age <6
:
3
print("你还没有上学")
4 elif age <=12
:
5
print("在上小学")
6 elif age <=18
:
7
print("在上中学")
8 else:
9
print("你成年了")
while循环
10
11 while age <=18
:
12
print(age)
13
age =age +1
print(“*”), #python 不换行
print(“*”,end=“”) #python3 不换行
break 用法
结束 while循环
continue 用法
跳出本次 while循环(即while中 continue后面代码跳过不执行)
一个break 或者 continue 对应一个while 生效
1
i =1;
2 while i <=10:
3
#打印偶数
4
if i%2
==0:
5
print(i);
6
i +=1
7
if i ==
6:
8
break
9
print("结束")
10
11 while i<=20:
12
i +=1
13
if i%3
==0:
14
print(i)
15
if i ==15:
16
continue
17
print("====")
for循环
1
nums = [12,23,34,45,56,67,78]
2
i =
0
3 while i<len(nums):
4
print(nums[i])
5
i+=1
6
print('='*40)
7 for num
in nums:
8
print(num)
color =
raw_input("你白吗?\n")
2
money =
int(raw_input("你的财产\n”))
# money 转换为int类型
3
beautiful =
raw_input("你美么?\n")
4
5 if color =="白"
and money >=100
and beautiful =="美":
6
print("你是白富美")
7 else:
8
print("你是矮穷矬”)
注意 : if else语句 后面的 : 切勿在中文模式下输入
比较运算符
==
!= —— <> 都是不等于
>=
<=
>
<
逻辑运算符
and a=1 and b=1
or a=1 or b=1
not not(a =1 and b=1)
if—elif—else
if 条件:
xxx
elif条件:
xxx
elif条件:
xxx
else条件:
xxx
例句:
1
age =
int(input("输入你的年龄:\n"))
2 if age <6
:
3
print("你还没有上学")
4 elif age <=12
:
5
print("在上小学")
6 elif age <=18
:
7
print("在上中学")
8 else:
9
print("你成年了")
while循环
10
11 while age <=18
:
12
print(age)
13
age =age +1
print(“*”), #python 不换行
print(“*”,end=“”) #python3 不换行
break 用法
结束 while循环
continue 用法
跳出本次 while循环(即while中 continue后面代码跳过不执行)
一个break 或者 continue 对应一个while 生效
1
i =1;
2 while i <=10:
3
#打印偶数
4
if i%2
==0:
5
print(i);
6
i +=1
7
if i ==
6:
8
break
9
print("结束")
10
11 while i<=20:
12
i +=1
13
if i%3
==0:
14
print(i)
15
if i ==15:
16
continue
17
print("====")
for循环
1
nums = [12,23,34,45,56,67,78]
2
i =
0
3 while i<len(nums):
4
print(nums[i])
5
i+=1
6
print('='*40)
7 for num
in nums:
8
print(num)
相关文章推荐
- Python基础(条件判断和循环) if elif else for while break continue;
- Python中的判断、循环 if...else,while
- Python条件判断 if-else for循环 while循环 break continue
- Python for, while循环后面加else的作用
- python之for循环、if循环、while循环
- Python笔记-if语句、for循环、while循环
- python 基础-----数字,字符串,if while 循环 数据类型的转换简单介绍
- Oracle语法温故(for循环、while循环、if else 、方法声明、调用,过程方法声明、调用)
- python学习:while和if...else的运用
- Python 初体验之 最基本的if-else-while-for
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
- Python~if,while,for~顺序,判断,循环
- python (if-else ,for, while, break, continue)
- Python if for else elif while break loop pass 控制流工具
- python基础语法之if,else,for,while,continue,break
- 在Python的while循环中使用else以及循环嵌套的用法
- 流程控制之if...elif...else和流程控制之while循环
- Python学习笔记--for, while循环后面加else的作用
- Python入门 列表 for while if...else... 函数
- 20180403-Python if条件判断及while循环