控制台输出: (python3出现的) 控制台输出: 连接多个字符串 控制台输出: 控制台输出: 控制台输出: 追加到后面 控制台输出: 插入 控制台输出: 接一个序列,字符串是一个序列 控制台输出: 移除一个元素 控制台输出: 删除一个元素 控制台输出: 控制台输出: (不可变的列表) 控制台输出:(不可修改的) 其他操作跟列表相同 {}:键值对 键—>值 控制台输出: 控制台输出: 控制台输出: 控制台输出: 作用: 控制台输出: 现在用函数代替 控制台输出: 读文件 a.txt 控制台输出: 写入文件 控制台输出: b.txt 关于Python学习之基础知识到这结束了,有问题的小伙伴,欢迎留言!!!1. 格式化输出
user_1='韩梅梅' user_2='李雷' print('{}对{}说:"hello"'.format(user_1,user_2))
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 韩梅梅对李雷说:"hello" Process finished with exit code 0
f-string
user_1='韩梅梅' user_2='李雷' print(f'{user_1}对{user_2}说:hello"')
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 韩梅梅对李雷说:hello" Process finished with exit code 0
2. +
print("are "+" you"+" ok")
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py are you ok Process finished with exit code 0
3.列表
my_list=[1,2,'a',1.3] print(my_list[-4])
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 1 Process finished with exit code 0
切片
my_list=[1,2,'a',1.3] print(my_list[1:3]) print(my_list[1:5:2]) print(my_list[:]) print(my_list[1:])
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [2, 'a'] [2, 1.3] [1, 2, 'a', 1.3] [2, 'a', 1.3] Process finished with exit code 0
append
my_list=[1,2,'a',1.3] my_list.append(111) print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 2, 'a', 1.3, 111] Process finished with exit code 0
insert
my_list=[1,2,'a',1.3] my_list.insert(1,'python') print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 'python', 2, 'a', 1.3] Process finished with exit code 0
extend
my_list=[1,2,'a',1.3] my_list.extend('python') my_list.extend([22,33,44]) print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 2, 'a', 1.3, 'p', 'y', 't', 'h', 'o', 'n', 22, 33, 44] Process finished with exit code 0
pop()
my_list=[1,2,'a',1.3] my_list.pop(1) print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 'a', 1.3] Process finished with exit code 0
remove
my_list=[1,2,'a',1.3] my_list.remove('a') print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 2, 1.3] Process finished with exit code 0
列表的修改
my_list=[1,2,'a',1.3] my_list[2]=33 print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py [1, 2, 33, 1.3] Process finished with exit code 0
4.元组
my_list=(1,2,'a',1.3) my_list[2]=33 print(my_list)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py Traceback (most recent call last): File "D:/pythoncode/1.py", line 51, in <module> my_list[2]=33 TypeError: 'tuple' object does not support item assignment Process finished with exit code 1
5.字典
user={ 'name':'Tom' } print(user)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py {'name': 'Tom'} Process finished with exit code 0
user={ 'name':'Tom', 'age':18, 'gender':'male' } print(user) print(user['age']) print(user['name'])
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py {'name': 'Tom', 'age': 18, 'gender': 'male'} 18 Tom Process finished with exit code 0
修改
user={ 'name':'Tom', 'age':18, 'gender':'male' } # 修改 user['age']=20 print(user)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py {'name': 'Tom', 'age': 20, 'gender': 'male'} Process finished with exit code 0
增加
user={ 'name':'Tom', 'age':18, 'gender':'male' } # 增加 user['fav']='打篮球' print(user)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py {'name': 'Tom', 'age': 18, 'gender': 'male', 'fav': '打篮球'} Process finished with exit code 0
6.函数
1.降低编程难度 2.增加代码复用
# 1+2+3+...100 n=1 s=0 while n<=100: s+=n n+=1 print(s)
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 5050 Process finished with exit code 0
def qiu_he(n,m): s=0 while n<=m: s+=n n+=1 return s print(qiu_he(1,100)) print(qiu_he(1,50))
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 5050 1275 Process finished with exit code 0
7.文件操作
f=open('a.txt',encoding='utf-8') s=f.read() print(s) f.close()
床前明月光 疑是地上霜 举头望明月 低头思故乡
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py 床前明月光 疑是地上霜 举头望明月 低头思故乡 Process finished with exit code 0
f=open('b.txt',mode='w',encoding='utf-8') f.write('我想要个n') f.write('女朋友n') f.close()
D:pythoncodevenvScriptspython.exe D:/pythoncode/1.py Process finished with exit code 0
我想要个 女朋友
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算