小言_互联网的博客

Python学习之基础知识

349人阅读  评论(0)

1. 格式化输出


  
  1. user_1= '韩梅梅'
  2. user_2= '李雷'
  3. print( '{}对{}说:"hello"'.format(user_1,user_2))

控制台输出: 


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 韩梅梅对李雷说: "hello"
  3. Process finished with exit code 0

 f-string

(python3出现的)


  
  1. user_1= '韩梅梅'
  2. user_2= '李雷'
  3. print( f'{user_1}{user_2}说:hello"')

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 韩梅梅对李雷说:hello "
  3. Process finished with exit code 0

2. +

连接多个字符串

print("are "+" you"+" ok")

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. are you ok
  3. Process finished with exit code 0

3.列表


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. print(my_list[ -4])

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 1
  3. Process finished with exit code 0

切片


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. print(my_list[ 1: 3])
  3. print(my_list[ 1: 5: 2])
  4. print(my_list[:])
  5. print(my_list[ 1:])

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 2, 'a']
  3. [ 2, 1.3]
  4. [ 1, 2, 'a', 1.3]
  5. [ 2, 'a', 1.3]
  6. Process finished with exit code 0

append

追加到后面


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list.append( 111)
  3. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 2, 'a', 1.3, 111]
  3. Process finished with exit code 0

insert

插入


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list.insert( 1, 'python')
  3. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 'python', 2, 'a', 1.3]
  3. Process finished with exit code 0

extend

接一个序列,字符串是一个序列


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list.extend( 'python')
  3. my_list.extend([ 22, 33, 44])
  4. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 2, 'a', 1.3, 'p', 'y', 't', 'h', 'o', 'n', 22, 33, 44]
  3. Process finished with exit code 0

pop()

移除一个元素


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list.pop( 1)
  3. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 'a', 1.3]
  3. Process finished with exit code 0

remove

删除一个元素


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list.remove( 'a')
  3. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 2, 1.3]
  3. Process finished with exit code 0

列表的修改


  
  1. my_list=[ 1, 2, 'a', 1.3]
  2. my_list[ 2]= 33
  3. print(my_list)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. [ 1, 2, 33, 1.3]
  3. Process finished with exit code 0

4.元组

不可变的列表


  
  1. my_list=( 1, 2, 'a', 1.3)
  2. my_list[ 2]= 33
  3. print(my_list)

控制台输出:(不可修改的


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. Traceback (most recent call last):
  3. File "D:/pythoncode/1.py", line 51, in <module>
  4. my_list[ 2]= 33
  5. TypeError: 'tuple' object does not support item assignment
  6. Process finished with exit code 1

其他操作跟列表相同


 5.字典

{}:键值对 键--->值


  
  1. user={
  2. 'name': 'Tom'
  3. }
  4. print(user)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. { 'name': 'Tom'}
  3. Process finished with exit code 0


  
  1. user={
  2. 'name': 'Tom',
  3. 'age': 18,
  4. 'gender': 'male'
  5. }
  6. print(user)
  7. print(user[ 'age'])
  8. print(user[ 'name'])

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. { 'name': 'Tom', 'age': 18, 'gender': 'male'}
  3. 18
  4. Tom
  5. Process finished with exit code 0

修改


  
  1. user={
  2. 'name': 'Tom',
  3. 'age': 18,
  4. 'gender': 'male'
  5. }
  6. # 修改
  7. user[ 'age']= 20
  8. print(user)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. { 'name': 'Tom', 'age': 20, 'gender': 'male'}
  3. Process finished with exit code 0

增加


  
  1. user={
  2. 'name': 'Tom',
  3. 'age': 18,
  4. 'gender': 'male'
  5. }
  6. # 增加
  7. user[ 'fav']= '打篮球'
  8. print(user)

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. { 'name': 'Tom', 'age': 18, 'gender': 'male', 'fav': '打篮球'}
  3. Process finished with exit code 0

6.函数

作用:


   
  1. 1.降低编程难度
  2. 2.增加代码复用

  
  1. # 1+2+3+...100
  2. n= 1
  3. s= 0
  4. while n<= 100:
  5. s+=n
  6. n+= 1
  7. print(s)

 控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 5050
  3. Process finished with exit code 0

现在用函数代替


  
  1. def qiu_he(n,m):
  2. s= 0
  3. while n<=m:
  4. s+=n
  5. n+= 1
  6. return s
  7. print(qiu_he( 1, 100))
  8. print(qiu_he( 1, 50))

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 5050
  3. 1275
  4. Process finished with exit code 0

7.文件操作

读文件


  
  1. f=open( 'a.txt',encoding= 'utf-8')
  2. s=f.read()
  3. print(s)
  4. f.close()

a.txt


  
  1. 床前明月光
  2. 疑是地上霜
  3. 举头望明月
  4. 低头思故乡

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. 床前明月光
  3. 疑是地上霜
  4. 举头望明月
  5. 低头思故乡
  6. Process finished with exit code 0

写入文件


  
  1. f=open( 'b.txt',mode= 'w',encoding= 'utf-8')
  2. f.write( '我想要个\n')
  3. f.write( '女朋友\n')
  4. f.close()

控制台输出:


  
  1. D:\pythoncode\venv\Scripts\python.exe D:/pythoncode/ 1.py
  2. Process finished with exit code 0

b.txt


  
  1. 我想要个
  2. 女朋友

关于Python学习之基础知识到这结束了,有问题的小伙伴,欢迎留言!!!


转载:https://blog.csdn.net/weixin_44364444/article/details/105880582
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场