python入门
第二章 python入门
1.1 解释器安装
1.1.1 官网查找版本
1.1.2 选择版本
1.1.3 安装
1.1.4 改路径
下载成功
1.1.5 手动添加环境变量
又键点击此电脑进入属性-》高级系统设置-》环境变量-》双击Path新建添加解释器路径
在中端打开win+R输入cmd进入命令提示行,输入命令:解释器路径(建议: .py后缀)
-
文件.py
#!/usr/bin/env python 在linux系统中指定解释器的路径
# _*_ coding:utf-8 _*_
print('你好')
-
运行:解释器 + 文件路径
-
在linux上有一种特殊的执行方式
-
给文件赋予一个可执行的权限
-
./a.py
-
# 环境变量
# python -->python.exe
# 在任何目录下都能够找到python.exe文件
# 才能在任意位置输入python命令启动python解释器
2.1 编码
2.1.1 编码基础
-
ascii 8位表示一个东西,2**8:只适用于英文和数字
-
Unicode,万国码,32位表示一个东西,2**32:语言种类多,占的资源多:只用了21位。
-
ecs2
-
ecs4
-
-
utf-8,给Unicode压缩,用尽量少的位数表示一个东西,以8个位为单位:完全弥补了unicodedd的占资源多的情况,中文3个字节
-
gbk;中文2个字节
-
gb2312;中文用2给字节
-
密码本:记录二进制和文字的对应关系
-
不同的密码本之间不能互相识别。
2.1.2 python解释器编码
-
py2:ascii(默认编码),如果想要修改默认编码,在文件开头加:
# -*- coding:utf-8 -*- -
py3:utf-8(默认编码)
-
文件编码:建议///:编写文件时,保存文件要用utf-8格式。注意:以什么编码保存就要用什么编码方式打开,否责出现乱码。
-
重点: '中国12he' : GBK: 8个字节 '中国12he' : UTF-8: 10个字节
2.1.3 编码进阶
-
数据在内存中全部是以Unicode编码的,除了bytes类型,但当你的数据用于网络传输或者存储到硬盘,必须是(bytes类型)以非Unicode编码(utf-8,gbk等)
-
英文:
-
str:'hello' 内存中的编码方式:Unicode,表现形式:'hello'
-
bytes:特殊类型【字节】,内存中的编码方式:非Unicode 表现形式:b'hello'
-
8位 = 1bytes= 1字节
-
单位
-
8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
常⽤到TB就够了
-
str和bytes的区别
-
str :1.编码方式Unicode 存储 2. 表现形式:字符串 '' '''''' """ """ 3.文字文本 4.存储少量信息
-
bytes:1.非Unicode存储 2.表现形式:前面加b 3.字节文本 4.用于数据的网络传输和存储
-
-
-
中文:
-
str :'中国' 内存中的编码方式:Unicode,表现形式:'中国'
-
bytes:
-
内存中的编码方式: 非Unicode # Utf-8
表现形式:b'\xe4\xb8\xad\xe5\x9b\xbd'
-
-
-
str ---> bytes 和 bytes ---> str【str和bytes之间的转换】
**str ---> bytes**
s1 = '中国'
b1 = s1.encode('utf-8') # 编码
print(b1,type(b1)) # b'\xe4\xb8\xad\xe5\x9b\xbd'
b1 = s1.encode('gbk') # 编码 # b'\xd6\xd0\xb9\xfa' <class 'bytes'>
**bytes ---> str**
b1 = b'\xe4\xb8\xad\xe5\x9b\xbd'
s2 = b1.decode('utf-8') # 解码
print(s2)
-
gbk ---> utf-8【编码之间的转换】
**gbk ---> utf-8**
b1 = b'\xd6\xd0\xb9\xfa'
s = b1.decode('gbk')
print(s) # '中国'
b2 = s.encode('utf-8')
print(b2) # b'\xe4\xb8\xad\xe5\x9b\xbd'
2.2 变量和常量
问:为什么要有变量?
2.2.1 为某个值创建一个“外号”,以后在使用时候通过此外号就可以直接调用。
content = "钓刀鱼要用刀,刀鱼要在岛上钓"
content = 666
print(content) # 666
变量命名要求:
1.变量名只能包含:字母/数字/下划线
2.数字不能开头
3.不能是python的关键字:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
4.建议:
-
见名知意: name = ”alex” age=18
-
用下划线连接:alex_dad = "吴沛琪"
补充:AlexDad= “吴沛琪”(驼峰式代码)
-
变量小高级
# 变量的小高级:
age1 = 18
age2 = age1
age3 = age2
age2 = 12
# print(age1,age2,age3) # 18 12 18
2.2.2 常量:就是设置一个不能改变的值,而且需要全部大写。
2.3 输入
-
input输入得到的内容永远是字符串。
-
py版本区别
-
py2:name = raw_input("请输入你的姓名")
-
py3: name = input("请输入姓名")
-
2.4 输出
print(你想要输出的东西)
-
py2:print (空格)“你好”
-
py3:print(‘你好’)
-
py2.7:接近py3两种兼容
2.5 注释
# 单行注释
"""
多行注释
"""
'''被注释的类容'''
难以理解的代码后面,加注释。
函数,类,文件都需要注释,解释说明。
2 .6基础语法
2.6.1 if条件判断
if 条件:
执行语句
if条件语句的基本结构?
```python
if 条件:
执行语句
```
```python
if 条件:
执行语句
else:
执行语句
```
```python
if 条件:
执行语句
elif 条件:
执行语句
else:
执行语句
```
```python
if 的嵌套
if 条件:
执行语句
if 条件:
执行语句
else:
执行语句
else:
执行语句
```
2.6.2 while循环语句
-
while Ture 真名题
-
while False 假名题
-
while 1:比较高效
1.while后面加入条件
while 1>0 and 2>1:
print('人生苦短,我用python。')
3.数字相加
count = 1
while True:
count += 1
print(count) # 计数思想
4.请通过循环,1 2 3 4 5 6 8 9 10
count = 1
while count <= 10:
if count != 7:
print(count)
count += 1
count = 1
while count <= 10:
if count == 7:
pass
else:
print(count)
count += 1
count = 0
while count < 10:
if count == 6:
count += 1
count += 1
print(count)
2.6.3 for循环
基本结构
for 变量 in 数据类型:
执行语句
注:for 可以和break,contiune一起搭配使用和while 一样
name = 'alex'
for item in name:
print(item)
break #:终止循环
print('123')
name = 'alex'
for item in name:
print(item)
continue:回到开始的循环
print('123')
注意:for和while的应用场景:有穷尽优先使用for,无穷尽用while
range: 类似于列表,自定制数字范围的数字列表
和for搭配使用,range(0,10) # 帮助你生成一个数字列表 [0,1,2,3,4,5,6,7,8,9]
for i in range(10)#(0,10) (0,10,1)
print(i)
v1 = [11,22,33,44]
# 1.内部会将v1转换成迭代器
# 2.内部反复执行 迭代器.__next__()
# 3.取完不报错
for item in v1:
print(item)
break :终止当前循环,程序遇到break就退出。
continue:跳出本次循环,继续下一轮循环下一轮循环。
while... else:条件不成立就执行。
3 运算符
3.1.1算术运算
练习题:1~100之间所有的数相加
total = 0
count = 1
while count <= 100:
total += count
count += 1
print(total)
练习题:打印1~100之间的奇数。
count = 1
while count <= 100:
if count % 2 == 1:
print(count)
count += 1
3.1.2 赋值运算
就是 += ,-=
count = 1
while count <=100:
print(count)
count +=1
3.1.3 逻辑运算
-
一般情况,用于做判断
if 1 > 2 and 1 > 0: False
print('666')
-
还可以用于取值
-
or
'''
对于or,如果value = 1 or 9
第一值如果转换成布尔值是真,则取value第一值,如果是假则取第二值
如果有多个or,则从左到右依次进行上述流程
'''
示例
v1 = 0 or 1
v2 = 8 or 10
v3 = 0 or 9 or 8
-
and
'''
对于and,如果遇到value = 1 and 9这种情况
如果第一值转换成布尔值是True,则取第二个值
如果第一值转换成布尔值是False,则取value第一个值
如果有多个and条件,则从左到右一次进行上述流程
'''
示例
v1 = 1 and 9
v2 = 1 and 0
v3 = 0 and 7
v4 = 0 and ""
v5 = 1 and 0 and 9
结合
先看and再看or
v1 = 1 and 9 or 6
print(v1)
-
not :取反
优先级
not 2 > 1 False
其他
-
优先级在没有括号的情况下即优先级是()>not>and>or,同一优先级从左到右计算。
-
3.1.4 成员运算【重点】
-
in (在)
value = "我是中国人"
# 判断‘中国’是否在value所指代的字符串中。‘中国’是否是value所指代
v1 = ‘中国’ in value
#示例
content = input(‘请输入内容’)
if ‘退钱’ in content:
print(‘包含敏感字符’)
#示例
while True:
content = input('请输入内容:')
if "退钱“ in content:
print(’包含敏感字符‘)
else:
print(content)
break
-
not in (不在)
4 字符格式化
4.1格式化输出
当你遇到这样的需求:字符串中想让某些位置变成动态可传入的,首先要考虑到格式化输出。
4.2 字符串格式化存在的意义
1 %s
msg = "我是%s,年龄%s" % ('alex',19,)
# 也可以是字典
msg = "我是%(n1)s,年龄%(n2)s" % {'n1':'alex','n2':9}
# 字符串格式化存在的意义
name = input('姓名:')
do = input('在干什么:')
template = "%s在教室,%s。" %(name,do,)
print(template)
# 直接做占位符
# template = "我是%s,年龄%s, 职业%s。" %("alex",73,'讲鸡汤',)
# print(template)
2 %d 只能接受数字
# template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
# print(template)
3 %%
# name = 'alex'
# template = "%s现在手机的电量是100%%" %(name,)
# print(template)
4 练习
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobby = input('请输入爱好:')
msg = '''
------------ info of Alex Li ----------
Name : %s
Age : %s
job : %s
Hobbie: %s
------------- end ----------------'''
data = msg %(name,age,job,hobby,)
print(data)
-
format(格式化输出)
# format: 格式化输出
# # 第一种用法:
# msg = '我叫{}今年{}性别{}'.format('大壮',25,'男')
# 第二种用法:
msg = '我叫{0}今年{1}性别{2}我依然叫{0}'.format('大壮', 25,'男')
print(msg)
# 第三种用法:
a = 100
msg = '我叫{name}今年{age}性别{sex}'.format(age=a,sex='男',name='大壮')
print(msg)
v1 = '我是{0},年龄{1}'.format(*('alex',19,)) *args(位置参数)
v1 = '我是{name},年龄{age}'.format(**{'name' = 'alex','age' = 19}) **kwarg(关键字参数)
-
f'{}' :新特新:**python3.6以后才有
name = 'alex'
age = 18
msg = f'我叫{name} 年龄{age}'
print(msg)
# 可以加表达式
dic = {'name':'alex','age':18}
msg1 = f'我叫{dic["name"]},今年{dic["age"]}'
print(msg1)
name = 'alex'
msg2 = f'{name.upper()}'
print(msg2)
# 结合函数写
def _sum(a,b):
return a + b
msg = f'最终的结果是:{_sum(10,20)}'
print(msg)
# ! , : { } ;这些标点不能出现在{} 这里面。
# 优点:
# 1. 结构更加简化。
# 2. 可以结合表达式,函数进行使用。
# 3. 效率提升很多

浙公网安备 33010602011771号