python一天速成(附源码)
图片上传参考:Typora笔记上传到CSDN解决图片问题---亲测有效!_我很好请走开谢谢的博客-CSDN博客!
Python简介
print('hello world')#字符串 print(99.6)#浮点数/整数 print(3 + 4)#表达式 #输出到文件,使用file=...的形式 fp = open('D:/text.txt','a+') print('helloworld',file=fp) fp.close() #不进行换行输出 print('hello','world','python')
转义字符
print('hello\nworld')#/n换行 newline print('hello\tworld')#/t制表,占四位 print('hello\rworld')#回车(光标移动到开头) print('hello\bworld')#退格 print('老师说:\'大家好\'') #原字符,不希望字符串中的转义字符起作用,最后一个字符不能是反斜杠 print(r'hrllo\nworld')
二进制与字符编码
8bit = 1byte
1024byte = 1kb
1024kb = 1mb
1024mb = 1gb
常用的ASCII
A = 65
a = 97
0 = 48
标识符和保留字
import keyword print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
变量的定义和使用
name = 'YangSheng' print(name) print('标识', id(name)) print('类型', type(name)) print('值', name)
输出结果
YangSheng 标识 1940806642480 类型 <class 'str'> 值 YangSheng
内存分析图
变量的多次赋值
多次赋值后变量名会指向新的空间
name = 'YangSheng' name = 'QQxing' print(name) #输出QQxing
Python中常见的数据类型
整数类型:
可以表示整数、负数、0
整数可以表示为:二进制(0b开头)、十进制、八进制(0o开头)、十六进制(0x开头)
浮点数
-
由整数+小数组成
-
浮点存储不精确
解决方案:导入模块decimal
print(1.1 + 2.2) from decimal import Decimal print(Decimal('1.1') + Decimal('2.2'))
D:\python\python.exe D:/pythonProject/浮点数.py 3.3000000000000003 3.3
字符串类型
str1 = '人生苦短,我用python' str2 = "人生苦短,我用python" str3 = '''人生苦短, 我用python''' print(str1,type(str1)) print(str2,type(str2)) print(str3,type(str3))
D:\python\python.exe D:/pythonProject/字符串类型.py 人生苦短,我用python <class 'str'> 人生苦短,我用python <class 'str'> 人生苦短, 我用python <class 'str'>
进程已结束,退出代码0
数据类型转换
不做具体介绍
name = '张三' age = 20 print(type(name),type(age)) print('我叫'+name+'今年'+str(age) + '岁') print('我叫'+name+'今年',age,'岁')
python中的注释
input函数的使用
name = input('输入你的姓名\n') print(name+'你好')
#计算a + b a = float(input()) b = float(input()) print(a + b)
运算符
位运算符
运算符优先级
算数运算——>位运算——>比较运算
顺序结构
对象的布尔值
print(bool(False)) print(bool(0)) print(bool(0.0)) print(bool(None)) print(bool('')) print(bool("")) print(bool([]))#空列表 print(bool(list()))#空列表 print(bool(()))#空元组 print(bool(tuple()))#空元组 print(bool({}))#空字典 print(bool(dict()))#空字典 print(bool(set()))#空集合 #以上对象布尔值位False #其他对象布尔值都为True
条件表达式
#输入两个整数 a = int(input('请输入第一个数: ')) b = int(input('请输入第二个数: ')) #比较大小 if a >= b: print(a , '大于等于', b) else: print(a, '小于', b) #条件表达式 print( str(a) + '大于等于' + str(b) if a >= b else str(a)+ '小于'+ str(b))
pass语句
answer = input('您是会员吗?y/n\n') if answer == 'y': pass else: pass
range函数的使用
r = range(10)# [0, 1, 2, ... ,9]默认0开始,步长为1 print(r) print(list(r))#查看range对象中的整数序列 x = range(2, 10) print(list(x)) t = range(0,10,2) print(list(t)) print(10 in r)#判断10是否在r序列中 print(10 not in r)#判断10是否不在r序列中 print(range(1,20,1)) print(range(1,101,1))
输出:
D:\python\python.exe D:/pythonProject/range函数的使用.py range(0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 3, 4, 5, 6, 7, 8, 9] [0, 2, 4, 6, 8] False True range(1, 20) range(1, 101)
进程已结束,退出代码0
# #计算1-100之间的偶数和 #方法1 a = 0 sum = 0 while a <= 100: sum += a a += 2 print('1-100之间的偶数和为',sum) #方法2 a = 0 sum = 0 while a <= 100 and a % 2 == 0: sum += a a += 2 print('1-100之间的偶数和为',sum) #方法3 a = 0 sum = 0 for a in range(1, 101): if a % 2 == 0: sum += a a += 1 print('1-100之间的偶数和为',sum)
输出:
D:\python\python.exe D:/pythonProject/while循环.py 1-100之间的偶数和为 2550 1-100之间的偶数和为 2550 1-100之间的偶数和为 2550
进程已结束,退出代码0
for_in循环
#判断100到999之间的水仙花数 print('100到999之间的水仙花数有:') for n in range(100, 1000): a = n % 10 b = n // 10 % 10 c = n // 100 if a ** 3 + b ** 3 + c ** 3 == n: print(n, end=' ')
D:\python\python.exe D:/pythonProject/for_int循环.py 100到999之间的水仙花数有: 153 370 371 407 进程已结束,退出代码0
流程控制语句break
#录入密码,最多3次 for n in range(3): pwd = input("请输入密码:") if pwd == '8888': print('密码正确') break else: print('密码错误') b = 0 while b < 3: pa = input('请输入密码:') if pa == '123': print('密码正确') break else: print('密码不正确')
流程控制语句continue
#输出1-50之间所有5的倍数 for n in range(1, 51): if(n % 5 != 0): continue else: print(n, end=" ") print() for n in range(1, 51): if(n % 5 == 0): print(n, end=" ")
输出:
D:\python\python.exe D:/pythonProject/流程控制语句continue.py 5 10 15 20 25 30 35 40 45 50 5 10 15 20 25 30 35 40 45 50 进程已结束,退出代码0
else语句
for n in range(3): pwd = input('请输入密码:') if pwd == '123': print('密码正确') break else: print('密码不正确') else: print('对不起,三次机会均没有猜对') #------------------------------------------ a = 0 while a < 3: pwd = input('请输入密码:') if pwd == '333': print('密码正确') break else: print('密码错误') a += 1 else: print('对不起,三次机会均没有猜对')
嵌套循环
#9 * 9 乘法表 for i in range(1, 10): for j in range(1, i + 1): print(i , '*', j, '=', i * j,end='\t') print()
输出:
D:\python\python.exe D:/pythonProject/嵌套循环.py
1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 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
进程已结束,退出代码0
二重循环中的break和continue
for i in range(5): for j in range(1, 11): if j % 2 == 0: break print(j) for i in range(5): for j in range(1, 11): if j % 2 == 0: continue print(j,end='\t') print()
输出:
D:\python\python.exe D:/pythonProject/二重循环中的break和continue.py 1 1 1 1 1 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
进程已结束,退出代码0
知识点总结
什么是列表
创建,删除,增删查改
为什么需要列表
a = 10 list = ['hello','world', 100] print(id(list)) print(type(list)) print(list)
D:\python\python.exe D:/pythonProject/列表.py 1952402173568 <class 'list'> ['hello', 'world', 100]
进程已结束,退出代码0
内存分析图
列表的创建
lst = ['hello','world',666] lst1 = list(['ni','hao','ya']) print(lst) print(lst1)
输出
D:\python\python.exe D:/pythonProject/列表.py ['hello', 'world', 666] ['ni', 'hao', 'ya']
进程已结束,退出代码0
列表的特点
获取指定元素索引
lst = ['hello','world',666,'hello'] print(lst.index('hello')) # print(lst.index('nihao')) print(lst.index('hello',1,4))#[1,3)
获取表中指定的元素
lst = ['hello','world',666,'hello','aaa']\ #获取索引为2的元素 print(lst[2]) print(lst[-3]) print(lst[10])#超出范围则抛异常
输出:
D:\python\python.exe D:/pythonProject/列表.py Traceback (most recent call last): File "D:\pythonProject\列表.py", line 5, in <module> print(lst[10]) IndexError: list index out of range 666 666
进程已结束,退出代码1
获取列表中多个元素__切片操作
lst = [10,20,30,40,50,60,70,80] print('原列表',id(lst)) lst2 = lst[1:6:1] print('切的片段',id(lst2)) print(lst[1:6])
输出:
D:\python\python.exe D:/pythonProject/获取列表中多个元素__切片操作.py 原列表 2139436019328 切的片段 2139437744960 [20, 30, 40, 50, 60]
进程已结束,退出代码0
列表元素的判断及遍历
print('p' in 'python') print('k' not in 'python') lst = [10,20,'python','hello'] print(10 in lst) print(100 in lst) print(10 not in lst) print(100 not in lst)
输出:
D:\python\python.exe D:/pythonProject/列表元素的判断及遍历.py True True True False False True
进程已结束,退出代码0
遍历操作
lst = [10,20,'python','hello'] for item in lst: print(item, end=' ')
输出:
D:\python\python.exe D:/pythonProject/列表元素的判断及遍历.py 10 20 python hello 进程已结束,退出代码0
列表元素的增删改操作
```
lst = [10,20,30] print('原列表',lst) lst.append(100) print('添加元素后',lst) lst2 = ['hello','world'] lst.extend(lst2) print('添加元素后',lst) lst.insert(2,40) print('插入元素后',lst) lst3 = [True,False,'hello'] lst[1:] = lst3 print(lst)
输出:
D:\python\python.exe D:/pythonProject/列表元素的增删改操作.py 原列表 [10, 20, 30] 添加元素后 [10, 20, 30, 100] 添加元素后 [10, 20, 30, 100, 'hello', 'world'] 插入元素后 [10, 20, 40, 30, 100, 'hello', 'world'] [10, True, False, 'hello']
进程已结束,退出代码0
lst = [10,20,30,40,30,50] print('---------remove操作----------') lst.remove(30) print(lst) print('---------pop操作----------') # lst.remove(100) 元素不存在则抛出异常 lst.pop(1) lst.pop()#不指定默认移除最后一位 # lst.pop(100)索引不存在则抛出异常 print(lst) print('---------切片操作----------') lst[1:3]=[] print(lst) print('---------clear操作----------') lst.clear() print(lst) del lst#删除列表对象
输出:
lst = [10,20,30,40,30,50] print('---------remove操作----------') lst.remove(30) print(lst) print('---------pop操作----------') # lst.remove(100) 元素不存在则抛出异常 lst.pop(1) lst.pop()#不指定默认移除最后一位 # lst.pop(100)索引不存在则抛出异常 print(lst) print('---------切片操作----------') lst[1:3]=[] print(lst) print('---------clear操作----------') lst.clear() print(lst)
列表元素的修改
lst = [10,20,30,40] lst[2] = 100 print(lst) lst[1:3]=[300,400,500,600] print(lst)
输出:
D:\python\python.exe D:/pythonProject/列表元素的修改.py [10, 20, 100, 40] [10, 300, 400, 500, 600, 40]
进程已结束,退出代码0
列表元素的排序
lst = [20,10,30,60,50] lst.sort() print(lst) #通过关键字进行升序排序 lst.sort(reverse=True) print(lst) #使用内置函数sorted进行排序 lst1 = sorted(lst) print(lst1) desc_lst = sorted(lst,reverse=True) print(desc_lst)
输出;
D:\python\python.exe D:/pythonProject/列表元素的排序.py [10, 20, 30, 50, 60] [60, 50, 30, 20, 10] [10, 20, 30, 50, 60] [60, 50, 30, 20, 10]
进程已结束,退出代码0
列表生成式
lst = [i*i for i in range(1,10)] print(lst) lst2 = [i * 2 for i in range(1,6)] print(lst2)
输出:
D:\python\python.exe D:/pythonProject/列表生成式.py [1, 4, 9, 16, 25, 36, 49, 64, 81] [2, 4, 6, 8, 10]
进程已结束,退出代码0
列表总结
什么是字典
键值对、无序序列
字典的创建
scores = {'张三':100,'李四':98,'王五':45} print(scores) print(type(scores)) #dict创建字典 stu = dict(name='jack',age=20) print(stu) #空字典 d = {} print(d)
输出:
D:\python\python.exe D:/pythonProject/字典的创建.py {'张三': 100, '李四': 98, '王五': 45} <class 'dict'> {'name': 'jack', 'age': 20} {}
进程已结束,退出代码0
字典元素的操作
scores = {'张三':100,'李四':98,'王五':45} print(scores['张三']) # print(scores['陈六']) #使用get方法 print(scores.get('张三')) print(scores.get('陈六')) print(scores.get('麻七'),99)
输出:
D:\python\python.exe D:/pythonProject/字典的创建.py 100 100 None None 99
进程已结束,退出代码0
scores = {'张三':100,'李四':98,'王五':45} print('张三' in scores) print('张三' not in scores) #删除操作 del scores['张三'] print(scores) #清空操作 scores.clear() print(scores) #新增操作 scores['陈六'] = 98 print(scores) #修改操作 scores['陈六'] = 100 print(scores)
输出:
D:\python\python.exe D:/pythonProject/键的判断.py True False {'李四': 98, '王五': 45} {} {'陈六': 98} {'陈六': 100}
进程已结束,退出代码0
获取字典视图
scores = {'张三':100,'李四':98,'王五':45} keys = scores.keys() print(keys) print(type(keys)) print(list(keys))#将所有key组成的视图转换成列表 #获取所有value values = scores.values() print(values) print(type(values)) print(list(values)) #获取所有key-value对 iteams = scores.items() print(iteams) print(type(iteams))#元组
输出:
D:\python\python.exe D:/pythonProject/获取字典视图.py dict_keys(['张三', '李四', '王五']) <class 'dict_keys'> ['张三', '李四', '王五'] dict_values([100, 98, 45]) <class 'dict_values'> [100, 98, 45] dict_items([('张三', 100), ('李四', 98), ('王五', 45)]) <class 'dict_items'>
进程已结束,退出代码0
字典元素的遍历
item是键
scores = {'张三':100,'李四':98,'王五':45} for item in scores: print(item,scores[item], scores.get(item))
输出:
D:\python\python.exe D:/pythonProject/字典元素的遍历.py 张三 100 100 李四 98 98 王五 45 45
进程已结束,退出代码0
字典的特点
d = {'name':'张三','name':'李四'} print(d) d = {'name':'张三','nickname':'张三'} print(d)
输出:
D:\python\python.exe D:/pythonProject/字典的特点.py {'name': '李四'} {'name': '张三', 'nickname': '张三'}
进程已结束,退出代码0
字典生成式
items = ['Fruits','Books','Others'] prices = [96,78,85] d = {item:price for item,price in zip(items, prices)} print(d)
输出:
D:\python\python.exe D:/pythonProject/字典生成式.py {'Fruits': 96, 'Books': 78, 'Others': 85}
进程已结束,退出代码0
字典知识点总结
什么是元组
可变序列 列表,字典
不可变序列 字符串,元组
可通过id是否变化判断
元组的创建方式
t = ('Python','world', 98) print(t) print(type(t)) t2 = 'Python','world', 98#省略了小括号 print(t2) print(type(t2)) t3 = ('Python',)#如果元组只有一个元素,逗号不能省略 print(t3) print(type(t3)) t1 = tuple(('Python', 'world', 98)) print(t1) print(type(t1)) #空元组的创建方式 t4 = () print(t4)
输出;
D:\python\python.exe D:/pythonProject/元组的创建方式.py ('Python', 'world', 98) <class 'tuple'> ('Python', 'world', 98) <class 'tuple'> ('Python',) <class 'tuple'> ('Python', 'world', 98) <class 'tuple'> ()
进程已结束,退出代码0
为什么将元组设计成不可变的序列
t = (10,[20,30],9) print(t) print(type(t)) print(t[0],type(t[0]), id(t[0])) print(t[1],type(t[1]),id(t[1])) print(t[2],type(t[2]),id(t[2])) #将t[1]修改为100 print(id(100)) # t[1] = 100 元组不允许修改元素 t[1].append(100)#添加元素 print(t, id(t[1]))
输出: D:\python\python.exe D:/pythonProject/什么是元组.py (10, [20, 30], 9) <class 'tuple'> 10 <class 'int'> 2006750528016 [20, 30] <class 'list'> 2006755438208 9 <class 'int'> 2006750527984 2006750530896 (10, [20, 30, 100], 9) 2006755438208
进程已结束,退出代码0
元组的遍历
t = ('Python', 'world',98) print(t[0]) print(t[1]) print(t[2]) #遍历元组 for item in t: print(item)
输出:
D:\python\python.exe D:/pythonProject/元组的遍历.py Python world 98 Python world 98
进程已结束,退出代码0
什么是集合
集合的创建方式
s = {2,3,4,5,5,6,7,7}#不能重复 print(s) #set创建集合 s = set(range(6)) print(s, type(s)) s2 = set([1,2,3,4,5,5,6,6]) print(s2) s3 = set((1,2,2,4,4,6, 66)) print(s3) s4 = set('Python') print(s4, type(s4)) s5 = set({1,2,4,65,56,77}) print(s5, type(s5)) #定义空集合 s6 = set() print(s6, type(s6))
输出:
D:\python\python.exe D:/pythonProject/集合的创建方式.py {2, 3, 4, 5, 6, 7} {0, 1, 2, 3, 4, 5} <class 'set'> {1, 2, 3, 4, 5, 6} {1, 2, 66, 4, 6} {'t', 'y', 'h', 'o', 'n', 'P'} <class 'set'> {65, 2, 1, 4, 56, 77} <class 'set'> set() <class 'set'>
进程已结束,退出代码0
集合的相关操作
s = {10,20,30,60} print(10 in s) print(100 in s) print(10 not in s) print(100 not in s) s.add(40) print(s) s.update({200,400,300}) print(s) s.update([666,555,333]) s.update((2,4,3)) print(s) s.remove(2) print(s) s.discard(3)#越界不抛异常 print(s) s.pop()#任意删除 print(s) s.clear()#清空 print(s)
输出:
D:\python\python.exe D:/pythonProject/集合的相关操作.py True False False True {40, 10, 20, 60, 30} {40, 200, 10, 300, 400, 20, 60, 30} {2, 3, 4, 40, 200, 10, 555, 300, 333, 400, 20, 666, 60, 30} {3, 4, 40, 200, 10, 555, 300, 333, 400, 20, 666, 60, 30} {4, 40, 200, 10, 555, 300, 333, 400, 20, 666, 60, 30} {40, 200, 10, 555, 300, 333, 400, 20, 666, 60, 30} set()
进程已结束,退出代码0
集合之间的关系
s = {10,20,30,40} s2 = {30,20,10,40, 50} print(s == s2) print(s != s2) s3 = {80} print(s.issuperset(s2)) print(s3.issuperset(s2)) s4 = {10,80,90} print(s4.isdisjoint(s))#有交集为False
输出:
D:\python\python.exe D:/pythonProject/集合之间的关系.py False True False False False
进程已结束,退出代码0
集合的数据操作
s1 = {10,20,30,40} s2 = {20,30,40,50,60} #交集 print(s1.intersection(s2)) print(s1 & s2) # intersection 等价于 & #并集 print(s1.union(s2)) print(s1|s2) #差集 print(s1.difference(s2)) print(s1 - s2) #对称差集 print(s1.symmetric_difference(s2)) print(s1^s2)
输出:
D:\python\python.exe D:/pythonProject/集合的数据操作.py {40, 20, 30} {40, 20, 30} {40, 10, 50, 20, 60, 30} {40, 10, 50, 20, 60, 30} {10} {10} {50, 10, 60} {50, 10, 60}
进程已结束,退出代码0
集合生成式
#列表生成式 lst = [i * i for i in range(6)] print(lst) #集合生成式 s = {i * i for i in range(6)} print(s)
输出:
D:\python\python.exe D:/pythonProject/集合生成式.py [0, 1, 4, 9, 16, 25] {0, 1, 4, 9, 16, 25}
进程已结束,退出代码0
列表、字典、元组、集合总结
字符串创建和驻留机制
a = 'Python' b = "Python" c = '''Python''' print(a, id(a)) print(b, id(b)) print(c, id(c))
输出:
D:\python\python.exe D:/pythonProject/字符串创建和驻留机制.py Python 2044636056688 Python 2044636056688 Python 2044636056688
进程已结束,退出代码0
字符串的常规操作
s = 'hello,hello' print(s.index('lo')) print(s.find('lo'))#find不会抛异常 print(s.rindex('lo')) print(s.rfind('lo'))#rfind不会抛异常
输出:
D:\python\python.exe D:/pythonProject/字符串的常规操作.py 3 3 9 9
进程已结束,退出代码0
大小写转换
s = 'hello,hello' # print(s.index('lo')) # print(s.find('lo')) # print(s.rindex('lo')) # print(s.rfind('lo')) print(s.upper()) print(s.lower()) print(s.swapcase()) s.lower() print(s.capitalize()) s.upper() print(s.title())
输出:
D:\python\python.exe D:/pythonProject/字符串的常规操作.py 3 3 9 9 HELLO,HELLO hello,hello HELLO,HELLO Hello,hello Hello,Hello
进程已结束,退出代码0
字符串对齐操作
s = 'hello,world' print(s.center(20,'*')) print(s.ljust(20,'*')) print(s.rjust(20,'*')) print(s.zfill(20)) print(s.zfill(10)) print('-8910'.zfill(8))
输出:
D:\python\python.exe D:/pythonProject/字符串对其.py hello,world* hello,world*** ***hello,world 000000000hello,world hello,world -0008910
进程已结束,退出代码0
字符串分割
s = 'hello world Python' lst = s.split() print(lst) s1 = 'hello|world|Python' print(s1.split()) print(s1.split(sep='|')) print(s1.split(sep='|',maxsplit=1)) print(s1.rsplit(sep='|',maxsplit=1))
输出:
D:\python\python.exe D:/pythonProject/字符串的分割.py ['hello', 'world', 'Python'] ['hello|world|Python'] ['hello', 'world', 'Python'] ['hello', 'world|Python'] ['hello|world', 'Python']
进程已结束,退出代码0
字符串的判断
s = 'hello,python' print('1.',s.isidentifier()) print('2.','hello'.isidentifier()) print('3.','张三_'.isidentifier()) print('4.','张三_123'.isidentifier()) print('5.','\t'.isspace()) print('6.','abc'.isalpha()) print('7.','张三'.isalpha()) print('8.','张三1'.isalpha()) print('9','123'.isdecimal()) print('10','123四'.isdecimal()) print('12.', '123'.isnumeric()) print('13.', '123四'.isnumeric()) print('14.', '123abc张三'.isalnum())
输出:
D:\python\python.exe D:/pythonProject/字符串的判断.py
False
True
True
True
True
True
True
False 9 True 10 False
True
True
True
进程已结束,退出代码0
字符串的其他操作
s = 'hello, Python' print(s.replace('Python','Java')) s1 = 'hello,Python,Python,Python' print(s1.replace('Python','Java',3)) lst = ['hello','java','python'] print('|'.join(lst)) print(''.join(lst))
输出:
D:\python\python.exe D:/pythonProject/字符串其他操作.py hello, Java hello,Java,Java,Java hello|java|python hellojavapython
进程已结束,退出代码0
字符串的比较
print('apple' > 'app') print('apple' > 'banana') print(ord('a'),ord('b')) print(ord('杨')) print(chr(97),chr(98)) print(chr(26472)) #== 和 is区别 #== 比较value #is 比较id a = b = 'python' c = 'python' print(a == b) print(b == c) print(a is c) print(b is c) print(id(a)) print(id(b)) print(id(c))
输出:
D:\python\python.exe D:/pythonProject/字符串的比较.py True False 97 98 26472 a b 杨 True True True True 2105604467504 2105604467504 2105604467504
进程已结束,退出代码0
字符串的切片操作
s = 'hello,Python' s1 = s[:5] s2 = s[6:] s3 = '!' new_Str = s1 + s3 + s2 print(s1) print(s1) print(new_Str) print(s[::-1]) print(s[-6::1])
输出:
D:\python\python.exe D:/pythonProject/字符串的切片操作.py hello hello hello!Python nohtyP,olleh Python
进程已结束,退出代码0
字符串的编码转换
s = '天涯共此时' print(s.encode(encoding='GBK'))#GBK一个中文占2个字符 print(s.encode(encoding='UTF-8'))#GBK一个中文占3个字符 byte = s.encode(encoding='GBK') print(byte.decode(encoding='GBK')) byte = s.encode(encoding='UTF-8') print(byte.decode(encoding='UTF-8'))
输出:
D:\python\python.exe D:/pythonProject/字符串的编码转换.py b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1' b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6' 天涯共此时 天涯共此时
进程已结束,退出代码0
函数
函数的创建和调用
def calc(a,b): c = a + b return c res = calc(10,20) print(res)
输出:
D:\python\python.exe D:/pythonProject/函数的创建.py 30
进程已结束,退出代码0
函数的参数传递
函数的参数传递内存分析
函数返回值
函数参数的总结