Skip to content

python笔记

待了解:爬虫、数据分析、机器学习、神经网络

python下载地址

https://www.python.org/downloads

官方学习文档

Python 教程

基础语法

查看python保留字

python
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', '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']

注释

python
# 单行注释
'''
多行注释1
'''
'''
多行注释2
'''

多行语句

python
total = item_one + \
        item_two + \
        item_three

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠 \

python
total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']

数字(Number)类型

  • int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
  • bool (布尔), 如 True。
  • float (浮点数), 如 1.23、3E-2
  • complex (复数) - 复数由实部和虚部组成,形式为 a + bj,其中 a 是实部,b 是虚部,j 表示虚数单位。如 1 + 2j、 1.1 + 2.2j

字符串

声明字符串,转义字符为\

python
'单引号声明字符串'
"双引号声明字符串"
'''
三引号声明字符串1(可以当做注释用)
'''
"""
三引号声明字符串2(可以当做注释用)
"""

字符串相关基本操作

python
str='123456789'

print(str) # 输出字符串
print(str[0:-1]) # 输出第一个到倒数第二个的所有字符
print(str[0]) # 输出字符串第一个字符
rint(str[2:5]) # 输出从第三个开始到第六个的字符(不包含) 
print(str[2:]) # 输出从第三个开始后的所有字符 
print(str[1:5:2]) # 输出从第二个开始到第五个且每隔一个的字符(步长为2,也就是下标+2) 
print(str * 2) # 输出字符串两次 
print(str + '你好') # 连接字符串

print('hello\nrunoob') # 使用反斜杠(\)+n转义特殊字符 
print(r'hello\nrunoob') # 在字符串前面添加一个 r,表示原始字符串,不会发生转义

等待用户输入

python
input("\n\n按下 enter 键后退出。")

同一行显示多条语句

语句之间使用分号 ;

python
import sys; x = 'runoob'; sys.stdout.write(x + '\n')

print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":

python
aaa = 1  
print(aaa)  # 会换行
print(aaa, end=" ") # 不会换行,会在行位加一个空格

import 与 from...import

python 用 import 或者 from...import 来导入相应的模块 将整个模块(somemodule)导入,格式为: import somemodule 从某个模块中导入某个函数,格式为: from somemodule import somefunction 从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc 将某个模块中的全部函数导入,格式为: from somemodule import *

导入sys模块

python
import sys
print('================Python import mode==========================') 
print ('命令行参数为:') 
for i in sys.argv: 
	print (i) 
print ('\n python 路径为',sys.path)

上面的输出如下

text
================Python import mode==========================
命令行参数为:
D:/PyCharm 2024.2.3/plugins/python-ce/helpers/pydev/pydevconsole.py
--mode=client
--host=127.0.0.1
--port=56550
 python 路径为 ['D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pydev', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\third_party\\thriftpy', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pydev', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_display', 'D:\\python\\python313.zip', 'D:\\python\\DLLs', 'D:\\python\\Lib', 'D:\\python', 'D:\\python\\Lib\\site-packages', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_matplotlib_backend', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_plotly_backend', 'D:\\code\\python_test']

导入sys模块的 argv,path 成员

python
from sys import argv,path  #  导入特定的成员
 
print('================python from import===================================')
print('path:',path) # 因为已经导入path成员,所以此处引用时不需要加sys.path

输出如下

text
================python from import===================================
path: ['D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pydev', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\third_party\\thriftpy', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pydev', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_display', 'D:\\python\\python313.zip', 'D:\\python\\DLLs', 'D:\\python\\Lib', 'D:\\python', 'D:\\python\\Lib\\site-packages', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_matplotlib_backend', 'D:\\PyCharm 2024.2.3\\plugins\\python-ce\\helpers\\pycharm_plotly_backend', 'D:\\code\\python_test']
python
# 查看类型
type(123)
# 转化字符串
str(123)
# 转化数字
int("123")
# 转化为浮点数
float(123)