python basis - if flow control statements

Flow control if statement:

The first syntax:

if conditions are: # quotes is the result of conditions that were separate and
result # 1 four spaces or a Tab, tells the program is to meet this condition.
Results
If the condition is true (true) execution result 1, result 2 and then, if the conditions are false (False) 2 direct result.

The second syntax:

if conditions:
results 1
the else:
Results
Exercise:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

The first if statement:

if conditional (a bool value):
(indentation) the condition is true execute a block of code here
Here Insert Picture Description
note: added else avoided when the above conditions are met we both print out the
Here Insert Picture Description
second if statement:
if condition:
Code block
else:
block
third if statement:
if condition:
block
elif (else if):
block
elif:
block
else: # here to deny all of the above
code block
elif and else difference is: elif but also denied the above continuation condition back, else direct negation, then print
Here Insert Picture Description
if, elif, else as long as the preceding conditions are satisfied, even if the latter does not meet the back of the output, only choose one way.
Here Insert Picture Description

Nesting:

Here Insert Picture Description
Here Insert Picture Description
Usually in order to ensure readability, usually no more than three nested.

Zhaohuaxishi (review):

1、认识和了解python:
python是一门解释性语言,弱类型语言。
假设: a = 12
a = “alex”
上面两个a都表示变量,但是第一个a类型是int(整数)
第二个a的类型是str(字符串)
2、python发展历程:
2008年发布了2.x和3.x保证了共存
3、第一个python程序:
(1)、python的程序可以写在命令行中进行编写和执行
命令行进入方式:win+r=>回车=>输入python
注意:区分中英文输入的符号。
(2)、把程序写在py文件中可以反复执行程序,python文件的后缀名是xx.py。
4、变量:
变量是程序运行过程中产生的中间值,供给后面的程序使用
5、变量的命名规则:
(1)、必须是数字、字母、下划线组合。
如:=”alex“ //这里的_是个变量
print(
) //打印出变量
(2)、不能是纯数字也不能是数字开头。
(3)、切记!切记!切记!变量里面一定不能有中文。
(4)、不能用关键字,关键字就是python已经占用了的东西。
(5)、不宜太长。
(6)、要有意义。
(7)、区分大小写和中文字符。
(8)、推荐使用驼峰,例如:JingYi。推荐使用下划线明显区分每个单词,如:jing_yi
6、常见的数据类型:
(1)、整数(int):可以做加减乘除、逻辑判断、条件判断
(2)、字符串(str):可做加(必须是字符串和字符串)乘(字符串和整数,表示重复几次)
(3)、布尔类型(bool):True和False(开头字母必须大写)
7、常量:
全部都要大写
8、交互:input
(1)、 s = input(”提示语“)#所得到的是字符串类型
(2)、int()把字符串转化为整数
9、if语句:
if 条件:
(前面有Tab)代码块
当条件满足执行代码块

if 条件:
(前面有Tab)代码块1
else:
(前面有Tab)代码块2
#当条件为真,执行代码块1,否则执行代码块2

if 条件1:
(前面有Tab)代码块1
elif 条件2
代码块2
elif …
else:
(前面有Tab)else代码块

嵌套:
if和else可以无限的嵌套,但是为了可用性建议嵌套三到五行左右。

发布了7 篇原创文章 · 获赞 3 · 访问量 233

Guess you like

Origin blog.csdn.net/qq_41397071/article/details/104033577