1. 格式化输出
-
user_1=
'韩梅梅'
-
user_2=
'李雷'
-
print(
'{}对{}说:"hello"'.format(user_1,user_2))
控制台输出:
-
D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/
1.py
-
韩梅梅对李雷说:
"hello"
-
-
Process finished
with exit code
0
f-string
(python3出现的)
-
user_1=
'韩梅梅'
-
user_2=
'李雷'
-
print(
f'{user_1}对{user_2}说:hello"')
控制台输出:
-
D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/
1.py
-
韩梅梅对李雷说:hello
"
-
-
Process finished with exit code 0
2. +
连接多个字符串
print("are "+" you"+" ok")
控制台输出:
-
D:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.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()
a.txt
-
床前明月光
-
疑是地上霜
-
举头望明月
-
低头思故乡
控制台输出:
-
D:\pythoncode\venv\Scripts\python.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:\pythoncode\venv\Scripts\python.exe D:/pythoncode/
1.py
-
-
Process finished
with
exit code
0
b.txt
-
我想要个
-
女朋友
关于Python学习之基础知识到这结束了,有问题的小伙伴,欢迎留言!!!
转载:https://blog.csdn.net/weixin_44364444/article/details/105880582
查看评论