变量:为了存储程序运算过程中的一些中间 结果,为了方便日后调用
常量:固定不变的量,字符大写
变量的命名规则
1、字母数字下划线组成
2、不能以数字开头,不能含特殊字符和空格
3、不能以保留字命名
4、不能以中文命名
5、定义变量名,应有意义
6、驼峰式命名、下划线分割单词
7、变量名区分大小写
数据运算
表达式if else语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
if 3 > 2:
print("yes")
elif 3 == 2:
print("equal")
else:
print("no")
View Code
注释
1、' ' #单行注释
2、" " 单行注释
3、''' ''' 多行注释
input() #用户输入
字符串拼接(+) print("a"+"b")
数学运算 + - * / //(整除) %(取余) **(指数运算)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 a = 2**3
2 print(a) #结果为8
3
4 b = 9%2
5 print(b) #结果为1
View Code
比较运算符
输出三个数的最大值和三个数的最小值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 num1 = int(input("num1:"))
2 num2 = int(input("num2:"))
3 num3 = int(input("num3:"))
4
5 num_max = 0
6 if num1 > num2:
7 num_max = num1
8 if num_max > num3:
9 print("max nums: %s " % num_max)
10 else:
11 print("max nums: %s " % num3)
12 else:
13 num_max = num2
14 if num_max > num3:
15 print("max nums: %s " % num_max)
16 else:
17 print("max nums: %s " % num3)
View Code
赋值运算符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 num1 = 2
2
3 def nums(num1):
4
5 num1 += 2 #等价于num1 + 2 = 6
6 #num1 -= 2 #等价于num1 +-2 = 2
7 #num1 *= 2 #等价于num1 * 2 = 8
8 #num1 /= 2 #等价于num1 / 2 = 2.0
9 return num1
10
11 print(nums(4))
View Code
逻辑运算符
and (且,并且)
只有两个条件都为True,那么结果为True
or (或)
两个条件其中一个为True,那么结果为True
not (非)
not 5 > 3 等价于 5 < 3 ,结果False
not 5 < 3 等价于 5> 3 ,结果True
短路原则
条件1 and 条件2
条件1 or 条件2
---对于and
如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式的计算结果就一定为假 ,第二个条件就不会被计算。
---对于or
如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式的计算结果就一定为真,第二个条件就不会被计算。
while 循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 age = 50
2 while True:
3 num = int(input(">>>:"))
4 if num > age:
5 print("大于age")
6 elif num < age:
7 print("小于age")
8 else:
9 print("等于age")
10 break
View Code
九九乘法表练习
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #while 循环
2
3 a = 1
4 while a <= 9:
5 b = 1
6 while b <= a:
7 print(("%s * %s = %s") % (a, b, a * b), end=" ")
8 b += 1
9 print()
10 a += 1
11
12 #for循环
13
14 for i in range(10):
15 if i > 0 :
16 for j in range(i+1):
17 if j > 0:
18 print(("%s * %s = %s")%(i,j,i*j),end=" ")
19 print()
View Code
九九乘法表练习,运行结果
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 九九乘法表练习,运行结果
2 '''
3 1 * 1 = 1
4 2 * 1 = 2 2 * 2 = 4
5 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
6 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
7 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
8 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
9 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
10 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
11 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
View Code