万字干货,Python语法大合集,一篇文章带你入门(一)了解Python!必看系列

万字干货,Python语法大合集,一篇文章带你入门

前言

最近有许多小伙伴后台联系我,说目前想要学习Python,但是没有一份很好的资料入门。一方面的确现在市面上Python的资料过多,导致新手会不知如何选择,另一个问题很多资料内容也很杂,从1+1到深度学习都包括,纯粹关注Python本身语法的优质教材并不太多。

刚好我最近看到一份不错的英文Python入门资料,我将它做了一些整理和翻译写下了本文。这份资料非常纯粹,只有Python的基础语法,专门针对想要学习Python的小白。

注释

Python中用#表示单行注释,#之后的同行的内容都会被注释掉。

# Python中单行注释用#表示,#之后同行字符全部认为被注释。
复制代码

使用三个连续的双引号表示多行注释,两个多行注释标识之间内容会被视作是注释。

""" 与之对应的是多行注释
    用三个双引号表示,这两段双引号当中的内容都会被视作是注释
"""
复制代码

基础变量类型与操作符

Python当中的数字定义和其他语言一样:

#获得一个整数
3
# 获得一个浮点数
10.0
复制代码

我们分别使用+, -, *, /表示加减乘除四则运算符。

1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20
35 / 5  # => 7.0
复制代码

这里要注意的是,在Python2当中,10/3这个操作会得到3,而不是3.33333。因为除数和被除数都是整数,所以Python会自动执行整数的计算,帮我们把得到的商取整。如果是10.0 / 3,就会得到3.33333。目前Python2已经不再维护了,可以不用关心其中的细节。

但问题是Python是一个弱类型的语言,如果我们在一个函数当中得到两个变量,是无法直接判断它们的类型的。这就导致了同样的计算符可能会得到不同的结果,这非常蛋疼。以至于程序员在运算除法的时候,往往都需要手工加上类型转化符,将被除数转成浮点数。

在Python3当中拨乱反正,修正了这个问题,即使是两个整数相除,并且可以整除的情况下,得到的结果也一定是浮点数。

如果我们想要得到整数,我们可以这么操作:

5 // 3       # => 1
-5 // 3      # => -2
5.0 // 3.0   # => 1.0 # works on floats too
-5.0 // 3.0  # => -2.0
复制代码

两个除号表示取整除,Python会为我们保留去除余数的结果。

除了取整除操作之外还有取余数操作,数学上称为取模,Python中用%表示。

# Modulo operation
7 % 3  # => 1
复制代码

Python中支持乘方运算,我们可以不用调用额外的函数,而使用**符号来完成:

# Exponentiation (x**y, x to the yth power)
2**3  # => 8
复制代码

当运算比较复杂的时候,我们可以用括号来强制改变运算顺序。

# Enforce precedence with parentheses
1 + 3 * 2  # => 7
(1 + 3) * 2  # => 8
复制代码

逻辑运算

Python中用首字母大写的True和False表示真和假。

True  # => True
False  # => False
复制代码

用and表示与操作,or表示或操作,not表示非操作。而不是C++或者是Java当中的&&, || 和!。

# negate with not
not True   # => False
not False  # => True

# Boolean Operators
# Note "and" and "or" are case-sensitive
True and False  # => False
False or True   # => True
复制代码

在Python底层,True和False其实是1和0,所以如果我们执行以下操作,是不会报错的,但是在逻辑上毫无意义。

# True and False are actually 1 and 0 but with different keywords
True + True # => 2
True * 8    # => 8
False - 5   # => -5
复制代码

我们用==判断相等的操作,可以看出来True==1, False == 0.

# Comparison operators look at the numerical value of True and False
0 == False  # => True
1 == True   # => True
2 == True   # => False
-5 != False # => True
复制代码

我们要小心Python当中的bool()这个函数,它并不是转成bool类型的意思。如果我们执行这个函数,那么只有0会被视作是False,其他所有数值都是True

bool(0)     # => False
bool(4)     # => True
bool(-6)    # => True
0 and 2     # => 0
-5 or 0     # => -5
复制代码

Python中用==判断相等,>表示大于,>=表示大于等于, <表示小于,<=表示小于等于,!=表示不等。

# Equality is ==
1 == 1  # => True
2 == 1  # => False

# Inequality is !=
1 != 1  # => False
2 != 1  # => True

# More comparisons
1 < 10  # => True
1 > 10  # => False
2 <= 2  # => True
2 >= 2  # => True
复制代码

我们可以用and和or拼装各个逻辑运算:

# Seeing whether a value is in a range
1 < 2 and 2 < 3  # => True
2 < 3 and 3 < 2  # => False
# Chaining makes this look nicer
1 < 2 < 3  # => True
2 < 3 < 2  # => False
复制代码

注意not,and,or之间的优先级,其中not > and > or。如果分不清楚的话,可以用括号强行改变运行顺序。

list和字符串

(海量免费测试资料加1140267353,群内还会有同行一起交流哦~)

关于list的判断,我们常用的判断有两种,一种是刚才介绍的==,还有一种是is。我们有时候也会简单实用is来判断,那么这两者有什么区别呢?我们来看下面的例子:

a = [1, 2, 3, 4]  # Point a at a new list, [1, 2, 3, 4]
b = a             # Point b at what a is pointing to
b is a            # => True, a and b refer to the same object
b == a            # => True, a's and b's objects are equal
b = [1, 2, 3, 4]  # Point b at a new list, [1, 2, 3, 4]
b is a            # => False, a and b do not refer to the same object
b == a            # => True, a's and b's objects are equal
复制代码

Python是全引用的语言,其中的对象都使用引用来表示。is判断的就是两个引用是否指向同一个对象,而==则是判断两个引用指向的具体内容是否相等。举个例子,如果我们把引用比喻成地址的话,is就是判断两个变量的是否指向同一个地址,比如说都是沿河东路XX号。而==则是判断这两个地址的收件人是否都叫张三。

显然,住在同一个地址的人一定都叫张三,但是住在不同地址的两个人也可以都叫张三,也可以叫不同的名字。所以如果a is b,那么a == b一定成立,反之则不然。

Python当中对字符串的限制比较松,双引号和单引号都可以表示字符串,看个人喜好使用单引号或者是双引号。我个人比较喜欢单引号,因为写起来方便。

字符串也支持+操作,表示两个字符串相连。除此之外,我们把两个字符串写在一起,即使没有+,Python也会为我们拼接:

# Strings are created with " or '
"This is a string."
'This is also a string.'

# Strings can be added too! But try not to do this.
"Hello " + "world!"  # => "Hello world!"
# String literals (but not variables) can be concatenated without using '+'
"Hello " "world!"    # => "Hello world!"
复制代码

我们可以使用[]来查找字符串当中某个位置的字符,用len来计算字符串的长度。

# A string can be treated like a list of characters
"This is a string"[0]  # => 'T'

# You can find the length of a string
len("This is a string")  # => 16
复制代码

我们可以在字符串前面加上f表示格式操作,并且在格式操作当中也支持运算,比如可以嵌套上len函数等。不过要注意,只有Python3.6以上的版本支持f操作。

# You can also format using f-strings or formatted string literals (in Python 3.6+)
name = "Reiko"
f"She said her name is {name}." # => "She said her name is Reiko"
# You can basically put any Python statement inside the braces and it will be output in the string.
f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long."
复制代码

最后是None的判断,在Python当中None也是一个对象,所有为None的变量都会指向这个对象。根据我们前面所说的,既然所有的None都指向同一个地址,我们需要判断一个变量是否是None的时候,可以使用is来进行判断,当然用==也是可以的,不过我们通常使用is。

# None is an object
None  # => None

# Don't use the equality "==" symbol to compare objects to None
# Use "is" instead. This checks for equality of object identity.
"etc" is None  # => False
None is None   # => True
复制代码

理解了None之后,我们再回到之前介绍过的bool()函数,它的用途其实就是判断值是否是空。所有类型的默认空值会被返回False,否则都是True。比如0,"",[], {}, ()等。

# None, 0, and empty strings/lists/dicts/tuples all evaluate to False.
# All other values are True
bool(None)# => False
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False
bool(())  # => False
复制代码

除了上面这些值以外的所有值传入都会得到True。

变量与集合

输入输出

Python当中的标准输入输出是input和print

print会输出一个字符串,如果传入的不是字符串会自动调用__str__方法转成字符串进行输出。默认输出会自动换行,如果想要以不同的字符结尾代替换行,可以传入end参数:

# Python has a print function
print("I'm Python. Nice to meet you!")  # => I'm Python. Nice to meet you!

# By default the print function also prints out a newline at the end.
# Use the optional argument end to change the end string.
print("Hello, World", end="!")  # => Hello, World!
复制代码

使用input时,Python会在命令行接收一行字符串作为输入。可以在input当中传入字符串,会被当成提示输出:

# Simple way to get input data from console
input_string_var = input("Enter some data: ") # Returns the data as a string
# Note: In earlier versions of Python, input() method was named as raw_input()
复制代码

变量

Python中声明对象不需要带上类型,直接赋值即可,Python会自动关联类型,如果我们使用之前没有声明过的变量则会出发NameError异常。

# There are no declarations, only assignments.
# Convention is to use lower_case_with_underscores
some_var = 5
some_var  # => 5

# Accessing a previously unassigned variable is an exception.
# See Control Flow to learn more about exception handling.
some_unknown_var  # Raises a NameError
复制代码

Python支持三元表达式,但是语法和C++不同,使用if else结构,写成:

# if can be used as an expression
# Equivalent of C's '?:' ternary operator
"yahoo!" if 3 > 2 else 2  # => "yahoo!"
复制代码

上段代码等价于:

if 3 > 2:
    return 'yahoo'
else:
    return 2
复制代码

list

Python中用[]表示空的list,我们也可以直接在其中填充元素进行初始化:(海量免费测试资料加1140267353,群内还会有同行一起交流哦~)

# Lists store sequences
li = []
# You can start with a prefilled list
other_li = [4, 5, 6]
复制代码

使用append和pop可以在list的末尾插入或者删除元素:

# Add stuff to the end of a list with append
li.append(1)    # li is now [1]
li.append(2)    # li is now [1, 2]
li.append(4)    # li is now [1, 2, 4]
li.append(3)    # li is now [1, 2, 4, 3]
# Remove from the end with pop
li.pop()        # => 3 and li is now [1, 2, 4]
# Let's put it back
li.append(3)    # li is now [1, 2, 4, 3] again.
复制代码

list可以通过[]加上下标访问指定位置的元素,如果是负数,则表示倒序访问。-1表示最后一个元素,-2表示倒数第二个,以此类推。如果访问的元素超过数组长度,则会出发IndexError的错误。

# Access a list like you would any array
li[0]   # => 1
# Look at the last element
li[-1]  # => 3

# Looking out of bounds is an IndexError
li[4]  # Raises an IndexError
复制代码

list支持切片操作,所谓的切片则是从原list当中拷贝出指定的一段。我们用start: end的格式来获取切片,注意,这是一个左闭右开区间。如果留空表示全部获取,我们也可以额外再加入一个参数表示步长,比如[1:5:2]表示从1号位置开始,步长为2获取元素。得到的结果为[1, 3]。如果步长设置成-1则代表反向遍历。

# You can look at ranges with slice syntax.
# The start index is included, the end index is not
# (It's a closed/open range for you mathy types.)
li[1:3]   # Return list from index 1 to 3 => [2, 4]
li[2:]    # Return list starting from index 2 => [4, 3]
li[:3]    # Return list from beginning until index 3  => [1, 2, 4]
li[::2]   # Return list selecting every second entry => [1, 4]
li[::-1]  # Return list in reverse order => [3, 4, 2, 1]
# Use any combination of these to make advanced slices
# li[start:end:step]
复制代码

如果我们要指定一段区间倒序,则前面的start和end也需要反过来,例如我想要获取[3: 6]区间的倒序,应该写成[6:3:-1]。

只写一个:,表示全部拷贝,如果用is判断拷贝前后的list会得到False。可以使用del删除指定位置的元素,或者可以使用remove方法。

# Make a one layer deep copy using slices
li2 = li[:]  # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.

# Remove arbitrary elements from a list with "del"
del li[2]  # li is now [1, 2, 3]

# Remove first occurrence of a value
li.remove(2)  # li is now [1, 3]
li.remove(2)  # Raises a ValueError as 2 is not in the list
复制代码

insert方法可以指定位置插入元素,index方法可以查询某个元素第一次出现的下标。

# Insert an element at a specific index
li.insert(1, 2)  # li is now [1, 2, 3] again

# Get the index of the first item found matching the argument
li.index(2)  # => 1
li.index(4)  # Raises a ValueError as 4 is not in the list
复制代码

list可以进行加法运算,两个list相加表示list当中的元素合并。等价于使用extend方法:

# You can add lists
# Note: values for li and for other_li are not modified.
li + other_li  # => [1, 2, 3, 4, 5, 6]

# Concatenate lists with "extend()"
li.extend(other_li)  # Now li is [1, 2, 3, 4, 5, 6]
复制代码

我们想要判断元素是否在list中出现,可以使用in关键字,通过使用len计算list的长度:

# Check for existence in a list with "in"
1 in li  # => True

# Examine the length with "len()"
len(li)  # => 6
复制代码

tuple

tuple和list非常接近,tuple通过()初始化。和list不同,tuple是不可变对象。也就是说tuple一旦生成不可以改变。如果我们修改tuple,会引发TypeError异常。

# Tuples are like lists but are immutable.
tup = (1, 2, 3)
tup[0]      # => 1
tup[0] = 3  # Raises a TypeError
复制代码

由于小括号是有改变优先级的含义,所以我们定义单个元素的tuple,末尾必须加上逗号,否则会被当成是单个元素:

# Note that a tuple of length one has to have a comma after the last element but
# tuples of other lengths, even zero, do not.
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'>
复制代码

tuple支持list当中绝大部分操作:

# You can do most of the list operations on tuples too
len(tup)         # => 3
tup + (4, 5, 6)  # => (1, 2, 3, 4, 5, 6)
tup[:2]          # => (1, 2)
2 in tup         # => True
复制代码

我们可以用多个变量来解压一个tuple:

# You can unpack tuples (or lists) into variables
a, b, c = (1, 2, 3)  # a is now 1, b is now 2 and c is now 3
# You can also do extended unpacking
a, *b, c = (1, 2, 3, 4)  # a is now 1, b is now [2, 3] and c is now 4
# Tuples are created by default if you leave out the parentheses
d, e, f = 4, 5, 6  # tuple 4, 5, 6 is unpacked into variables d, e and f
# respectively such that d = 4, e = 5 and f = 6
# Now look how easy it is to swap two values
e, d = d, e  # d is now 5 and e is now 4
复制代码

解释一下这行代码:

a, *b, c = (1, 2, 3, 4)  # a is now 1, b is now [2, 3] and c is now 4
复制代码

我们在b的前面加上了星号,表示这是一个list。所以Python会在将其他变量对应上值的情况下,将剩下的元素都赋值给b。

补充一点,tuple本身虽然是不可变的,但是tuple当中的可变元素是可以改变的。比如我们有这样一个tuple:

a = (3, [4])
复制代码

我们虽然不能往a当中添加或者删除元素,但是a当中含有一个list,我们可以改变这个list类型的元素,这并不会触发tuple的异常:

a[1].append(0) # 这是合法的
复制代码

dict

dict也是Python当中经常使用的容器,它等价于C++当中的map,即存储key和value的键值对。我们用{}表示一个dict,用:分隔key和value。

# Dictionaries store mappings from keys to values
empty_dict = {}
# Here is a prefilled dictionary
filled_dict = {"one": 1, "two": 2, "three": 3}
复制代码

dict的key必须为不可变对象,所以list、set和dict不可以作为另一个dict的key,否则会抛出异常:

# Note keys for dictionaries have to be immutable types. This is to ensure that
# the key can be converted to a constant hash value for quick look-ups.
# Immutable types include ints, floats, strings, tuples.
invalid_dict = {[1,2,3]: "123"}  # => Raises a TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}   # Values can be of any type, however.
复制代码

我们同样用[]查找dict当中的元素,我们传入key,获得value,等价于get方法。

# Look up values with []
filled_dict["one"]  # => 1
filled_dict.get('one') #=> 1
复制代码

我们可以call dict当中的keys和values方法,获取dict当中的所有key和value的集合,会得到一个list。在Python3.7以下版本当中,返回的结果的顺序可能和插入顺序不同,在Python3.7及以上版本中,Python会保证返回的顺序和插入顺序一致:

# Get all keys as an iterable with "keys()". We need to wrap the call in list()
# to turn it into a list. We'll talk about those later.  Note - for Python
# versions <3.7, dictionary key ordering is not guaranteed. Your results might
# not match the example below exactly. However, as of Python 3.7, dictionary
# items maintain the order at which they are inserted into the dictionary.
list(filled_dict.keys())  # => ["three", "two", "one"] in Python <3.7
list(filled_dict.keys())  # => ["one", "two", "three"] in Python 3.7+

# Get all values as an iterable with "values()". Once again we need to wrap it
# in list() to get it out of the iterable. Note - Same as above regarding key
# ordering.
list(filled_dict.values())  # => [3, 2, 1]  in Python <3.7
list(filled_dict.values())  # => [1, 2, 3] in Python 3.7+
复制代码

我们也可以用in判断一个key是否在dict当中,注意只能判断key。

# Check for existence of keys in a dictionary with "in"
"one" in filled_dict  # => True
1 in filled_dict      # => False
复制代码

如果使用[]查找不存在的key,会引发KeyError的异常。如果使用get方法则不会引起异常,只会得到一个None

# Looking up a non-existing key is a KeyError
filled_dict["four"]  # KeyError

# Use "get()" method to avoid the KeyError
filled_dict.get("one")      # => 1
filled_dict.get("four")     # => None
# The get method supports a default argument when the value is missing
filled_dict.get("one", 4)   # => 1
filled_dict.get("four", 4)  # => 4
复制代码

setdefault方法可以为不存在的key插入一个value,如果key已经存在,则不会覆盖它:

# "setdefault()" inserts into a dictionary only if the given key isn't present
filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5
复制代码

我们可以使用update方法用另外一个dict来更新当前dict,比如a.update(b)。对于a和b交集的key会被b覆盖,a当中不存在的key会被插入进来:

# Adding to a dictionary
filled_dict.update({"four":4})  # => {"one": 1, "two": 2, "three": 3, "four": 4}
filled_dict["four"] = 4         # another way to add to dict
复制代码

我们一样可以使用del删除dict当中的元素,同样只能传入key。

Python3.5以上的版本支持使用**来解压一个dict:

{'a': 1, **{'b': 2}}  # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}  # => {'a': 2}
复制代码

set

set是用来存储不重复元素的容器,当中的元素都是不同的,相同的元素会被删除。我们可以通过set(),或者通过{}来进行初始化。注意当我们使用{}的时候,必须要传入数据,否则Python会将它和dict弄混。

# Sets store ... well sets
empty_set = set()
# Initialize a set with a bunch of values. Yeah, it looks a bit like a dict. Sorry.
some_set = {1, 1, 2, 2, 3, 4}  # some_set is now {1, 2, 3, 4}
复制代码

set当中的元素也必须是不可变对象,因此list不能传入set。

# Similar to keys of a dictionary, elements of a set have to be immutable.
invalid_set = {[1], 1}  # => Raises a TypeError: unhashable type: 'list'
valid_set = {(1,), 1}
复制代码

可以调用add方法为set插入元素:

# Add one more item to the set
filled_set = some_set
filled_set.add(5)  # filled_set is now {1, 2, 3, 4, 5}
# Sets do not have duplicate elements
filled_set.add(5)  # it remains as before {1, 2, 3, 4, 5}
复制代码

set还可以被认为是集合,所以它还支持一些集合交叉并补的操作。

# Do set intersection with &
# 计算交集
other_set = {3, 4, 5, 6}
filled_set & other_set  # => {3, 4, 5}

# Do set union with |
# 计算并集
filled_set | other_set  # => {1, 2, 3, 4, 5, 6}

# Do set difference with -
# 计算差集
{1, 2, 3, 4} - {2, 3, 5}  # => {1, 4}

# Do set symmetric difference with ^
# 这个有点特殊,计算对称集,也就是去掉重复元素剩下的内容
{1, 2, 3, 4} ^ {2, 3, 5}  # => {1, 4, 5}
复制代码

set还支持超集和子集的判断,我们可以用大于等于和小于等于号判断一个set是不是另一个的超集或子集:

# Check if set on the left is a superset of set on the right
{1, 2} >= {1, 2, 3} # => False

# Check if set on the left is a subset of set on the right
{1, 2} <= {1, 2, 3} # => True
复制代码

和dict一样,我们可以使用in判断元素在不在set当中。用copy可以拷贝一个set。

# Check for existence in a set with in
2 in filled_set   # => True
10 in filled_set  # => False

# Make a one layer deep copy
filled_set = some_set.copy()  # filled_set is {1, 2, 3, 4, 5}
filled_set is some_set        # => False
复制代码

点击链接直接跳转下一章:万字干货,Python语法大合集,一篇文章带你入门(二)收藏必备!https://blog.csdn.net/PythonCS001/article/details/107643898

猜你喜欢

转载自blog.csdn.net/PythonCS001/article/details/107643768