python001

1.
python3x :python 文件路径 回车
python2x :python2 文件路径 回车
python2 python3 区别:python2默认编码方式是ascii码
解决方式:在文件的首行:
#-*- encoding:utf-8 -*-
python3 默认编码方式utf-8
2.常量
一直不变的量(大写) π
BIR_OF_CHINA = 1949
3.注释
方便自己方便他人理解代码。
单行注释:#
多行注释:'''被注释内容''' """被注释内容"""

4.用户交互(input)
(1)等待输入,
(2)将你输入的内容赋值给了前面变量。
(3)input出来的数据类型全部是str
5.基础数据类型初始。
数字:int 12,3,45
+ - * / **
% 取余数
ps:type()
字符串转化成数字:int(str) 条件:str必须是数字组成的。
数字转化成字符串:str(int)
字符串:str,python当中凡是用引号引起来的都是字符串。
可相加:字符串的拼接。
可相乘:str * int
bool:布尔值。 True False。

6.if

score = int(input("输入分数:"))
if score > 100:
    print("我擦,最高分才100...")
elif score >= 90:
    print("A")
elif score >= 60:
    print("C")
elif score >= 80:
    print("B")
elif score >= 40:
    print("D")
else:
    print("太笨了...E")

7.whlie

1 count = 1
2 sum = 0
3 while count <= 100:
4     sum = sum + count 
5     count = count + 1
6 print(sum)

8.break(跳出循环)

1 #break
2 print('11')
3 while True:
4     print('222')
5     print(333)
6     break
7     print(444)
8 print('abc')

9.continue(跳出本次循环)

1 print(111)
2 count = 1
3 while count < 20 :
4     print(count)
5     continue
6     count = count + 1

10.格式化输出(%s(string),%d(int))

 1 #格式化输出
 2  % s d
 3 name = input('请输入姓名:')
 4 age = input('请输入年龄:')
 5 job = input('请输入工作:')
 6 hobbie = input('你的爱好:')
 7 
 8 msg = '''------------ info of %s -----------
 9 Name  : %s
10 Age   : %d
11 job   : %s
12 Hobbie: %s
13 ------------- end -----------------''' %(name,name,int(age),job,hobbie)

11.while else (有break,else不执行)

1 count = 0
2 while count <= 5 :
3     count += 1
4     if count == 3:break
5     print("Loop",count)
6 
7 else:
8     print("循环正常执行完啦")
9 print("-----out of while loop ------")

12.逻辑运算(and or not)

 1 优先级   ()> not > and > or
 2 print(2 > 1 and 1 < 4)
 3 print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
 4 T or T or F
 5 T or F
 6 print(3>4 or 4<3 and 1==1)  # F
 7 print(1 < 2 and 3 < 4 or 1>2)  # T
 8 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
 9 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
10 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # F
11 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
12 
13 ps  int  ----> bool   非零转换成bool True   0 转换成bool 是False
14 print(bool(2))
15 print(bool(-2))
16 print(bool(0))
17 #bool --->int
18 print(int(True))   # 1
19 print(int(False))  # 0
20 
21 
22 '''x or y x True,则返回x'''
23 print(1 or 2)  # 1
24 print(3 or 2)  # 3
25 print(0 or 2)  # 2
26 print(0 or 100)  # 100
27 
28 
29 print(2 or 100 or 3 or 4)  # 2
30 
31 print(0 or 4 and 3 or 2)
32 
33 '''x and y x True,则返回y'''
34 print(1 and 2)  #2
35 print(0 and 2)  #0
36 print(2 or 1 < 3) 
37 print(3 > 1 or 2 and 2)

猜你喜欢

转载自www.cnblogs.com/20181013python/p/9784059.html