今?概要:
- 环境搭建
- 编码相关
- Pycharm
- 输出
- 数据类型
字符串 整型 布尔类型 - 变量(有坑)【*】
- 条件语句
- 循环语句
- 字符串格式化
- 运算符【*】
1.环境搭建
安装Python解释器(3.9.0)
C语?写出来的软件。
- 上层,Python语法写的代码。
- 软件,执?并翻译
- 底层,计算机识别的代码。
在安装?录下?个有?个可执??件。
终端设备:xx/xxx/xx/xxx/python.exe
D:\code\xxx.py
?般情况下:
C:\python39
- python.exe
- Scripts(bin)
- pip.exe ,包管理?具
- Lib
site-packages(安装的第三?的包)
- 。。。。
内置包
>>> C:\python39\python.exe D:\code\xxx.py
>>> C:\python39\Script\pip.exe install
requests
?定要安装在?便找的?录。
/Library/Frameworks/Python.framework/Versio
ns/python3.9/python 代码?件路径
/Library/Frameworks/Python.framework/Versio
ns/python3.9/bin/pip 代码?件路径
环境变量
/Library/Frameworks/Python.framework/Versio
ns/python3.9
/Library/Frameworks/Python.framework/Versio
ns/python3.9/bin
>>>python 代码
>>>pip install xxx
多个Python环境
- C:\python39
- python.exe
- python3.9.exe
- Scripts
- pip.exe
- pip3.9.exe
- C:\python38
- python.exe
- python3.8.exe
- C:\python37
- python.exe
- python3.7.exe
...
>>>python xxx.py
2.编码
?件存储编码
存储时候,??种编码。
打开时候,也?定要?同样的编码,否则就会乱码。
编码 ascii编码,256个。
ascii编码,256个。
unicode ucs2
00000000 00000001 我
00000000 00000010 想
ucs4
00000000 00000000 00000000 00000001
你
00000000 00000000 00000000 00000001
飞
utf-8编码,对unicode的压缩(?络传输或?件存储的时 都需要?他) 在Python3版本中:
name = "tom" # unicode
data = name.encode("utf-8") # utf-8
# 写?件或?络传输
在Python2版本中:
# python解释器转换成了utf-8编码。
name = "欧阳" # utf-8编码
data = name.decode("utf-8") # unicode
3.写代码
传统的?式 IDE,集成开发环境。【Pycharm + 不要汉化】 Python3解释器,默认编码是utf-8。
print("你好啊")
python2解释器,默认编码ascii
# -*- coding:utf-8 -*-
print("你好欧阳")
4.输出
print("hello world")
print(11,22,333,44)
print("你好欧阳", end="") # \n
print("Hello")
5.初识数据类型
- 字符串?类
"xxxx"
"xad"
"老武"
- 整型
12
77
99
- 布尔类型
True
False
5.1 字符串 str
定义
v1 = "老王"
v2 = '老陈'
v3 = """asdf
asdfasd
asdfasdf
asdf
asdf"""
v4 = str("xxxx")
拼接(相加)
"老王" + '老陈'
乘法
"老王" * 3
5.2 整型 int
整数,python3中没有了整型和?整形的区分。
8+9
12-10
19*8
18/2
19%2
5.3 布尔值 bool
True/False
if 1>2:
pass
else:
pass
5.4 转换
字符串 <-> 整型 整型和字符串 -> 布尔类型 整型转换布尔类型:0-> False 字符串转换成布尔类型:"" -> False
if False:
pass
if "老陈":
pass
练习题
print( "100" *3 )
print( int("100") *3 )
print( int("123") + int("88") )
print( str(111) + str(222) )
print( str(111)*3 )
print( int("8") > 7 )
print( str(111) == 111 )
print( bool(-1) )
print( bool(0) )
print( bool("") )
print( bool("你好") )
print( True == True)
print( True == False)
print( bool("") == bool(0) )
在?较??时,?般都是同?种数据类型来?较。 “程聪” > “欧阳”
6.变量
变量,昵称,代号。
data = "中华??共和国"
value = "啊维"
res = data + value
6.1 变量要求
字?、数字、下划线。 不能数字开头 不能?python内置内置关键字 建议: ?名知意
age = 123
size = 345
多个单词?下划线凭借
user_data_info = 123
_ = 9
n0 = 123
n+9 = q23
6.2 变量的赋值相关内存
v1 = 9
v2 = 10
v3 = v1 + v2
v1 = "a彪"
v2 = "a彪"
v1 = "a1彪"
v2 = v1
v1 = "老王"
v1 = 123
v1 = "老卓"
v2 = v1
v1 = "老安"
v1 = 123
v2 = str(v1)
7.输?
# 1.输出:请输??户名
# 2.等待?户输?
# 3.赋值给v1变量(?论你输?什么,都是字符串类型)
v1 = input("请输??户名")
v1 = input("请输??户名")
res = "你好" + v1
print(res)
v1 = input("请输?数字:") # 123
v2 = input("请输?数字:") # 100
res = v1 + v2
print(res)
v1 = input("请输?数字:") # 123
v2 = input("请输?数字:") # 100
res = int(v1) + int(v2)
print(res)
print("欢迎使?xx系统,请选择功能:1.登录;2.注册;3.注
销")
v1 = input("请选择:")
if v1 == "1":
pass
8.条件语句
if 条件{
...
}else{
...
}
if 条件 :
调整成?,执?这个缩进??的代码
。。。
。。。
。。
else:
调整成?,执?这个缩进??的代码
..
if True:
pass
if 9:
pass
if 1 > 4:
pass
if 2 == 5:
pass
v1 = 3 == 9
data = 9
if data > 10:
pass
name = input("请输??户名:")
pwd = input("请输?密码:")
if name == "root" and pwd == "123":
print("登录成功")
else:
print("登录失败")
if 条件:
pass
if 条件:
pass
elif 条件:
pass
elif 条件:
pass
else:
pass
if 1:
if 1>9:
pass
else:
pass
else:
pass
print("欢迎使?10086")
print("1.?机业务;2.宽带业务;3.企业业务")
choice = input("请选择:")
if choice == "1":
print("?机业务")
print("1.话费查询;2.流量查询;3.??服务")
second_choice = input("请输?:")
if second_choice == "1":
print("话费查询...")
elif second_choice == "2":
print("..,")
elif choice == "2":
pass
elif choice == "3":
pass
else:
print("输?错误")
9.循环
while循环
while 条件:
代码
代码
while True:
print("下午好困")
while 1>2:
print("下午好困")
count = 0
while count < 5:
print("?累?困")
count = 9
print("结束")
count = 0
while count < 5:
print("?累?困")
count = count + 1
print("结束")
break,终?循环,跳出循环
count = 0
while count < 5:
print("?累?困")
break
print("好像下课")
count = count + 1
print("结束")
continue,结束本次循环,开始下次循环
count = 0
while count < 5:
print("?累?困")
continue
print("好像下课")
count = count + 1
print("结束")
练习题:
# 1.输??户名直到输?正确位置
while True:
name = input("请输??户名:")
if name:
break
print("输?错误,请重新输?")
# 2.输?密码直到输?正确位置
while True:
name = input("请输?密码:")
if name:
break
print("输?错误,请重新密码")
if name == "alex" and pwd == "sb":
print("成功")
else:
print("失败")
# 三次机会。
count = 0
while True:
# 1.输??户名直到输?正确位置
while True:
name = input("请输??户名:")
if name:
break
print("输?错误,请重新输?")
# 2.输?密码直到输?正确位置
while True:
name = input("请输?密码:")
if name:
break
print("输?错误,请重新密码")
if name == "alex" and pwd == "sb":
print("成功")
break
else:
print("失败")
count = count + 1
# 1 2 3
if count == 3:
print("错误次数?完了,直接终?")
break
# 三次机会。
count = 0
while True:
# 1.输??户名直到输?正确位置
while True:
name = input("请输??户名:")
if name:
break
print("输?错误,请重新输?")
# 2.输?密码直到输?正确位置
while True:
name = input("请输?密码:")
if name:
break
print("输?错误,请重新密码")
if name == "tom" and pwd == "sb":
print("成功")
break
for循环
print("失败")
count = count + 1
# 1 2 3
if count == 3:
print("错误次数?完了,直接终?")
break
for循环
name = "a阳a阳阳阳" # int和bool不可以被for循
环。
for i in name:
print(i)
name_list = ["a森","a伟","a波"]
for i in name_list:
print(i)
v1 = range(1,10) # [1,2,3,4,5,6,7,8,9]
v2 = range(1,10,2) # [1,3,5,7,9]
v3 = range(10,1,-1) # [10,9,....., 2]
for i in range(3):
pass
for i in range(10):
pass
break和continue在for循环中可以使? 练习题
for i in range(3):
# 1.输??户名直到输?正确位置
while True:
name = input("请输??户名:")
if name:
break
print("输?错误,请重新输?")
# 2.输?密码直到输?正确位置
while True:
name = input("请输?密码:")
if name:
break
print("输?错误,请重新密码")
if name == "alex" and pwd == "sb":
print("成功")
break
print("失败")
循环的嵌套
for i in range(1,10):
# i=1
for j in range(1,10):
print(i,j)
输出?副扑克牌:
color = ["?桃", "红桃", "梅花", "??"]
for i in range(1, 14):
for j in color:
data = j + str(i)
print(data)
color = ["?桃", "红桃", "梅花", "??"]
for i in range(1, 14):
print(i)
for j in color:
print(j)
break
data = j + str(i)
print(data)
10.字符串格式化
10.1 format
v1 = "我是{0},今年{1}岁 {0}。 ".format("a辉",19)
v2 = "我是{n1},今年{n2}岁".format(n1="a辉",n2=19)
v3 = "我是{},今年{}岁".format("a辉",19)
tpl = "我是{},今年{}岁"
v1 = tpl.format("a辉",19)
v2 = tpl.format("a卓",19)
10.2 %
v1 = "我是%s,今年%d岁" %("a辉",19)
v1 = "我是%(n1)s,今年%(n2)d岁" %{"n1":"a辉","n2":19}
tpl = "我是%s,今年%d岁"
v1 = tpl %("a辉",19)
10.3 f-string
name = "王卓"
age = 19
v1 = f"我叫{name},今年{age}岁。"
11.运算符相关
加减乘除 赋值运算
v1 = 123
v2 = 0
v2 = v2 + 1 # v2 += 1
v2 = v2 - 1 # v2 -= 1
v2 = v2 * 1 # v2 *= 1
in 和 not in
# 内容是否包含某些值。
if "发" in "?发?阿斯蒂芬":
print(True)
else:
print(False)
# 内容是否包含某些值。
if "发" not in "?发?阿斯蒂芬":
print(True)
else:
print(False)
color = ["?桃", "红桃", "梅花", "??"]
if "红桃" in color:
pass
else:
pass
not / and / or 传统的使?
if 1==1 and 2==2:
pass
if 1> 2 or 4<5:
pass
特殊?法
v1 = 2>1 and 3==4 # True
v1 = 9 and 8 # 8
v2 = 0 and 8
v3 = "" and 8
v4 = 6 or 9
v5 = 0 or 9
v6 = 9 and 8 or "" and "a安" or "蒙蔽了"
8 or "" or "蒙蔽了"
8 or "蒙蔽了"
8
data = 9
if data:
v1 = data
else:
v1 = 88
v1 = data or 88
v1 = not 9 and 8 or "" and 12
|