1、Python中大写字母命名的变量,默认为常量,不可修改;列如:MYSQL_CONNECTION = '192.168.1.1'
2、pip install 模块 --cmd安装模块
3、import os --引用os模块
res=os.popen("ipconfig").read() --获得IP地址
>>> import os
>>> res=os.popen("ipconfig").read()
>>> print(res)
Windows IP 配置
无线局域网适配器 WLAN:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
无线局域网适配器 本地连接* 2:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
以太网适配器 以太网:
连接特定的 DNS 后缀 . . . . . . . :
IPv4 地址 . . . . . . . . . . . . : 192.168.1.109
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.1.1
import sys --引用sys模块
sys.path --显示环境变量
>>> import sys
>>> sys.path
['', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']
>>>
其中' ' 为当前目录
4、字符格式化:%s\%d\%f
1 #age=%s 输出字符串
2 name=input("input your name:")
3 age=input("input your age:")
4 job=input("input your job:")
5 #多行字符串用两个三引号框起来
6 msg='''
7 information of user %s:
8 -------------------
9 name: %s
10 age : %s
11 job : %s
12 --------End--------
13 '''%(name,name,age,job) #字符格式化
14 print(msg)
15 #age=%d 用来输出十进制整数,按整型数据的实际长度输出
16 name=input("input your name:")
17 age=int(input("input your age:")) 18 job=input("input your job:") 19 msg=''' 20 information of user %s: 21 ------------------- 22 name: %s 23 age : %d 24 job : %s 25 --------End-------- 26 '''%(name,name,age,job) 27 print(msg) 28 #age=%f 用来输出实数(包括单双精度),以小数形式输出 29 name=input("input your name:") 30 age=int(input("input your age:")) 31 job=input("input your job:") 32 msg=''' 33 information of user %s: 34 ------------------- 35 name: %s 36 age : %f 37 job : %s 38 --------End-------- 39 '''%(name,name,age,job) 40 print(msg)
5、for循环猜年龄
1 age=26
2 counter=0
3 for i in range(10):
4 print("--counter:",counter)
5 if counter <3:
6 guess_number=int(input("your guess number:"))
7 if guess_number==age: 8 print("Congretulations,you got it!") 9 break 10 elif guess_number>age: 11 print("Think smaller.") 12 else: 13 print("Think bigger.") 14 else: 15 continue_confirm=input("do you want to try continue:") 16 if continue_confirm=="y": 17 counter=0 18 continue 19 else: 20 break 21 counter=counter+1
6、type 查看数据类型
7、布尔值:真或假 --o==False 1==True
8、字符串拼接不用+,用%
1 name="charlie" 2 print("i am %s"%name)