网易首页 > 网易号 > 正文 申请入驻

python3参考秘籍-附PDF下载

0
分享至

简介

Python作为一个开源的优秀语言,随着它在数据分析和机器学习方面的优势,已经得到越来越多人的喜爱。据说小学生都要开始学Python了。

Python的优秀之处在于可以安装很多非常强大的lib库,从而进行非常强大的科学计算。

讲真,这么优秀的语言,有没有什么办法可以快速的进行学习呢?

有的,本文就是python3的基础秘籍,看了这本秘籍python3的核心思想就掌握了,文末还有PDF下载链接哦,欢迎大家下载。

Python的主要数据类型

python中所有的值都可以被看做是一个对象Object。每一个对象都有一个类型。

下面是三种最最常用的类型:

  • Integers (int)

整数类型,比如: -2, -1, 0, 1, 2, 3, 4, 5

  • Floating-point numbers (float)

浮点类型,比如:-1.25, -1.0, –0.5, 0.0, 0.5, 1.0, 1.25

  • Strings

字符串类型,比如:“www.flydean.com”

注意,字符串是不可变的,如果我们使用replace() 或者 join() 方法,则会创建新的字符串。

除此之外,还有三种类型,分别是列表,字典和元组。

  • 列表

列表用方括号表示:a_list = [2, 3, 7, None]

  • 元组

元组用圆括号表示:tup=(1,2,3) 或者直接用逗号表示:tup=1,2,3

Python中的String操作

python中String有三种创建方式,分别可以用单引号,双引号和三引号来表示。

基本操作my_string = “Let’s Learn Python!”
another_string = ‘It may seem difficult first, but you can do it!’
a_long_string = ‘’‘Yes, you can even master multi-line strings
that cover more than one line
with some practice’’’

也可以使用print来输出:

print(“Let’s print out a string!”)
String连接

String可以使用加号进行连接。

string_one = “I’m reading “
string_two = “a new great book!”
string_three = string_one + string_two

注意,加号连接不能连接两种不同的类型,比如String + integer,如果你这样做的话,会报下面的错误:
TypeError: Can’t convert ‘int’ object to str implicitly
String复制

String可以使用 * 来进行复制操作:

‘Alice’ * 5 ‘AliceAliceAliceAliceAlice’

或者直接使用print:

print(“Alice” * 5)
Math操作

我们看下python中的数学操作符:

操作符 含义 举例 ** 指数操作 2 ** 3 = 8 % 余数 22 % 8 = 6 // 整数除法 22 // 8 = 2 / 除法 22 / 8 = 2.75 ***** 乘法 3*3= 9 – 减法 5-2= 3 + 加法 2+2= 4 内置函数

我们前面已经学过了python中内置的函数print(),接下来我们再看其他的几个常用的内置函数:

  • Input() Function

input用来接收用户输入,所有的输入都是以string形式进行存储:

name = input(“Hi! What’s your name? “)
print(“Nice to meet you “ + name + “!”)
age = input(“How old are you “)
print(“So, you are already “ + str(age) + “ years old, “ + name + “!”)

运行结果如下:

Hi! What’s your name? “Jim”
Nice to meet you, Jim!
How old are you? 25
So, you are already 25 years old, Jim!

  • len() Function

len()用来表示字符串,列表,元组和字典的长度。

举个例子:

# testing len()
str1 = “Hope you are enjoying our tutorial!”
print(“The length of the string is :”, len(str1))

输出:

The length of the string is: 35

  • filter()

filter从可遍历的对象,比如列表,元组和字典中过滤对应的元素:

ages = [5, 12, 17, 18, 24, 32]
def myFunc(x):
if x < 18:
return False
else:
return True
adults = filter(myFunc, ages)
for x in adults:
print(x)
函数Function

python中,函数可以看做是用来执行特定功能的一段代码。

我们使用def来定义函数:

def add_numbers(x, y, z):
a= x + y
b= x + z
c= y + z
print(a, b, c)

add_numbers(1, 2, 3)

注意,函数的内容要以空格或者tab来进行分隔。
传递参数

函数可以传递参数,并可以通过通过命名参数赋值来传递参数:

# Define function with parameters
def product_info(productname, dollars):
print("productname: " + productname)
print("Price " + str(dollars))

# Call function with parameters assigned as above
product_info("White T-shirt", 15)

# Call function with keyword arguments
product_info(productname="jeans", dollars=45)

列表

列表用来表示有顺序的数据集合。和String不同的是,List是可变的。

看一个list的例子:

my_list = [1, 2, 3]
my_list2 = [“a”, “b”, “c”]
my_list3 = [“4”, d, “book”, 5]

除此之外,还可以使用list() 来对元组进行转换:

alpha_list = list((“1”, “2”, “3”))
print(alpha_list)
添加元素

我们使用append() 来添加元素:

beta_list = [“apple”, “banana”, “orange”]
beta_list.append(“grape”)
print(beta_list)

或者使用insert() 来在特定的index添加元素:

beta_list = [“apple”, “banana”, “orange”]
beta_list.insert(“2 grape”)
print(beta_list)
从list中删除元素

我们使用remove() 来删除元素

beta_list = [“apple”, “banana”, “orange”]
beta_list.remove(“apple”)
print(beta_list)

或者使用pop() 来删除最后的元素:

beta_list = [“apple”, “banana”, “orange”]
beta_list.pop()
print(beta_list)

或者使用del 来删除具体的元素:

beta_list = [“apple”, “banana”, “orange”]
del beta_list [1]
print(beta_list)
合并list

我们可以使用+来合并两个list:

my_list = [1, 2, 3]
my_list2 = [“a”, “b”, “c”]
combo_list = my_list + my_list2
combo_list
[1, 2, 3, ‘a’, ‘b’, ‘c’]
创建嵌套的list

我们还可以在list中创建list:

my_nested_list = [my_list, my_list2]
my_nested_list
[[1, 2, 3], [‘a’, ‘b’, ‘c’]]
list排序

我们使用sort()来进行list排序:

alpha_list = [34, 23, 67, 100, 88, 2]
alpha_list.sort()
alpha_list
[2, 23, 34, 67, 88, 100]
list切片

我们使用[x:y]来进行list切片:

alpha_list[0:4]
[2, 23, 34, 67]
修改list的值

我们可以通过index来改变list的值:

beta_list = [“apple”, “banana”, “orange”]
beta_list[1] = “pear”
print(beta_list)

输出:

[‘apple’, ‘pear’, ‘cherry’]
list遍历

我们使用for loop 来进行list的遍历:

for x in range(1,4):
beta_list += [‘fruit’]
print(beta_list)
list拷贝

可以使用copy() 来进行list的拷贝:

beta_list = [“apple”, “banana”, “orange”]
beta_list = beta_list.copy()
print(beta_list)

或者使用list()来拷贝:

beta_list = [“apple”, “banana”, “orange”]
beta_list = list (beta_list)
print(beta_list)
list高级操作

list还可以进行一些高级操作:

list_variable = [x for x in iterable]

number_list = [x ** 2 for x in range(10) if x % 2 == 0]
print(number_list)

元组

元组的英文名叫Tuples,和list不同的是,元组是不能被修改的。并且元组的速度会比list要快。

看下怎么创建元组:

my_tuple = (1, 2, 3, 4, 5)
my_tuple[0:3]
(1, 2, 3)
元组切片numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
print(numbers[1:11:2])

输出:

(1,3,5,7,9)
元组转为list

可以使用list和tuple进行相互转换:

x = (“apple”, “orange”, “pear”)
y = list(x)
y[1] = “grape”
x = tuple(y)
print(x)
字典

字典是一个key-value的集合。

在python中key可以是String,Boolean或者integer类型:

Customer 1= {‘username’: ‘john-sea’, ‘online’: false, ‘friends’:100}
创建字典

下面是两种创建空字典的方式:

new_dict = {}
other_dict= dict()

或者像下面这样来初始赋值:

new_dict = {
“brand”: “Honda”,
“model”: “Civic”,
“year”: 1995
}
print(new_dict)
访问字典的元素

我们这样访问字典:

x = new_dict[“brand”]

或者使用dict.keys()dict.values()dict.items()来获取要访问的元素。

修改字典的元素

我们可以这样修改字典的元素:

#Change the “year” to 2020:
new_dict= {
“brand”: “Honda”,
“model”: “Civic”,
“year”: 1995
}
new_dict[“year”] = 2020
遍历字典

看下怎么遍历字典:

#print all key names in the dictionary
for x in new_dict:
print(x)
#print all values in the dictionary
for x in new_dict:
print(new_dict[x])
#loop through both keys and values
for x, y in my_dict.items(): print(x, y)
if语句

和其他的语言一样,python也支持基本的逻辑判断语句:

  • Equals: a == b

  • Not Equals: a != b

  • Less than: a < b

  • Less than or equal to a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

看下在python中if语句是怎么使用的:

if 5 > 1:
print(“That’s True!”)

if语句还可以嵌套:

x = 35
if x > 20:
print(“Above twenty,”)
if x > 30:
print(“and also above 30!”)

elif:

a = 45
b = 45
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)

if else:

if age < 4:
ticket_price = 0
elif age < 18:
ticket_price = 10
else: ticket_price = 15

if not:

new_list = [1, 2, 3, 4]
x = 10
if x not in new_list:
print(“’x’ isn’t on the list, so this is True!”)

Pass:

a = 33
b = 200
if b > a:
pass
Python循环

python支持两种类型的循环,for和while

for循环for x in “apple”:
print(x)

for可以遍历list, tuple,dictionary,string等等。

while循环#print as long as x is less than 8
i =1
while i< 8:
print(x)
i += 1
break loopi =1
while i < 8:
print(i)
if i == 4:
break
i += 1
Class

Python作为一个面向对象的编程语言,几乎所有的元素都可以看做是一个对象。对象可以看做是Class的实例。

接下来我们来看一下class的基本操作。

创建classclass TestClass:
z =5

上面的例子我们定义了一个class,并且指定了它的一个属性z。

创建Objectp1 = TestClass()
print(p1.x)

还可以给class分配不同的属性和方法:

class car(object):
“””docstring”””
def __init__(self, color, doors, tires):
“””Constructor”””
self.color = color
self.doors = doors
self.tires = tires

def brake(self):
“””
Stop the car
“””
return “Braking”

def drive(self):
“””
Drive the car
“””
return “I’m driving!”
创建子类

每一个class都可以子类化

class Car(Vehicle):
“””
The Car class
“””

def brake(self):
“””
Override brake method
“””
return “The car class is breaking slowly!”

if __name__ == “__main__”:
car = Car(“yellow”, 2, 4, “car”)
car.brake()
‘The car class is breaking slowly!’
car.drive()
“I’m driving a yellow car!”

异常

python有内置的异常处理机制,用来处理程序中的异常信息。

内置异常类型

  • AttributeError — 属性引用和赋值异常

  • IOError — IO异常

  • ImportError — import异常

  • IndexError — index超出范围

  • KeyError — 字典中的key不存在

  • KeyboardInterrupt — Control-C 或者 Delete时,报的异常

  • NameError — 找不到 local 或者 global 的name

  • OSError — 系统相关的错误

  • SyntaxError — 解释器异常

  • TypeError — 类型操作错误

  • ValueError — 内置操作参数类型正确,但是value不对。

  • ZeroDivisionError — 除0错误

异常处理

使用try,catch来处理异常:

my_dict = {“a”:1, “b”:2, “c”:3}
try:
value = my_dict[“d”]
except KeyError:
print(“That key does not exist!”)

处理多个异常:

my_dict = {“a”:1, “b”:2, “c”:3}
try:
value = my_dict[“d”]
except IndexError:
print(“This index does not exist!”)
except KeyError:
print(“This key is not in the dictionary!”)
except:
print(“Some other problem happened!”)

try/except else:

my_dict = {“a”:1, “b”:2, “c”:3}
try:
value = my_dict[“a”]
except KeyError:
print(“A KeyError occurred!”)
else:
print(“No error occurred!”)

  • https://github.com/ddean2009/www.flydean.com/blob/master/python/python3-cheatsheet.pdf

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
刘品言夫妇一家三口现身街头,半岁女儿首曝光,白白胖胖太萌了

刘品言夫妇一家三口现身街头,半岁女儿首曝光,白白胖胖太萌了

小椰的奶奶
2026-06-04 12:22:05
30万手机成“摆件”,销售回应:不能回收置换,“二手价3000元”

30万手机成“摆件”,销售回应:不能回收置换,“二手价3000元”

中新经纬
2026-06-04 18:31:06
日本这回亏到肉疼!1592次架航班直接归零,小泉气得喊话中国!

日本这回亏到肉疼!1592次架航班直接归零,小泉气得喊话中国!

芳芳历史烩
2026-06-06 10:49:42
汕头火锅差评后续,恶心一幕发生!四游客全网逃匿仍发黑料太无耻

汕头火锅差评后续,恶心一幕发生!四游客全网逃匿仍发黑料太无耻

北纬的咖啡豆
2026-06-06 14:38:57
不服!01输给u19国足后,哥伦比亚主帅直言压着对手打没赢!

不服!01输给u19国足后,哥伦比亚主帅直言压着对手打没赢!

林子说事
2026-06-06 11:21:01
东风导弹泄密案!间谍郭万钧一家三口,全部被处以死刑

东风导弹泄密案!间谍郭万钧一家三口,全部被处以死刑

番外行
2026-03-31 08:28:28
男子花15元买彩票,中了6022万元!

男子花15元买彩票,中了6022万元!

深圳晚报
2026-06-03 19:37:04
梅根发布莉莉贝特公主5岁生日照,红发绿裙很可爱,神似父亲哈里

梅根发布莉莉贝特公主5岁生日照,红发绿裙很可爱,神似父亲哈里

人物档案局
2026-06-04 19:28:59
1974年女兵王季迟请假奔丧,政委见亲属王树声,隐藏身份曝光

1974年女兵王季迟请假奔丧,政委见亲属王树声,隐藏身份曝光

磊子讲史
2026-03-24 14:38:24
获利500万!又一网红爆款被曝含“伟哥”,网友:智商税

获利500万!又一网红爆款被曝含“伟哥”,网友:智商税

品牌观察官
2026-06-06 15:55:07
立刻停止食用这些粗粮,吃得越多,肠癌风险越高?医生告诉你真相

立刻停止食用这些粗粮,吃得越多,肠癌风险越高?医生告诉你真相

垚垚分享健康
2026-06-04 15:22:37
难怪说千万不要买二手物品,质量是其次,他的来历才更让人避讳

难怪说千万不要买二手物品,质量是其次,他的来历才更让人避讳

另子维爱读史
2026-06-03 21:56:51
炸了!“中国宝洁”,一代日化大王爆雷

炸了!“中国宝洁”,一代日化大王爆雷

新浪财经
2026-06-06 15:40:20
26位明星出演,耗资1亿票房仅343万,开心麻花这块金字招牌玩砸了

26位明星出演,耗资1亿票房仅343万,开心麻花这块金字招牌玩砸了

靠谱电影君
2026-06-06 20:47:32
美媒炸锅:伊朗用中国出口单兵防空导弹FN-16,把F-15E打下来了?

美媒炸锅:伊朗用中国出口单兵防空导弹FN-16,把F-15E打下来了?

潋滟晴方DAY
2026-06-05 17:12:39
伊朗一对“情侣”躲进谷仓中拥吻,被发现时,已经吻了2800年!

伊朗一对“情侣”躲进谷仓中拥吻,被发现时,已经吻了2800年!

抽象派大师
2026-06-05 01:43:30
降雨非常凶猛!广州天气:还在外面的快躲起来

降雨非常凶猛!广州天气:还在外面的快躲起来

环球网资讯
2026-06-06 17:32:16
今日18点,中国金花冲法网冠军!或追平王曦雨成就,双喜临门?

今日18点,中国金花冲法网冠军!或追平王曦雨成就,双喜临门?

刘姚尧的文字城堡
2026-06-06 08:27:11
“艺考名师”涉嫌性侵猥亵至少23人:当女友面侵犯女学生

“艺考名师”涉嫌性侵猥亵至少23人:当女友面侵犯女学生

大风新闻
2026-06-05 22:49:05
订单暴跌,北向资金加仓139%,国资委旗下,机器人唯一低估真龙

订单暴跌,北向资金加仓139%,国资委旗下,机器人唯一低估真龙

财报翻译官
2026-06-06 15:05:55
2026-06-06 21:47:00
flydean程序那些事
flydean程序那些事
最通俗的解读,最深刻的干货!
356文章数 438关注度
往期回顾 全部

科技要闻

SpaceX与谷歌签署300亿美元算力协议

头条要闻

妻子曾遭性侵婚后服用过量药物离世 丈夫4个月后殉情

头条要闻

妻子曾遭性侵婚后服用过量药物离世 丈夫4个月后殉情

体育要闻

上海男篮时隔24年夺冠,恐怖的在后面?

娱乐要闻

33岁男演员金泽去世,曾搭档龚俊

财经要闻

百亿国资绑定,追觅“黑色星期五”背后

汽车要闻

BJ30旅行家完成多路况节油实测 百公里油耗突破5升

态度原创

房产
健康
教育
游戏
艺术

房产要闻

重磅官宣!美丽沙侨中也要扩建,新增42个班!

新规能促进干细胞治疗乱象规范吗?

教育要闻

纸短情长!校长手书“侨批”给高考学子暖心送考

失眠组全新力作!《漫威金刚狼》线性叙事玩法详解

艺术要闻

Rose Frantzen:2026年罗里克大奖获得者

无障碍浏览 进入关怀版