Python for me to bite! This knowledge can learn a few life-saving!

Original link: https://shop40704199.m.youzan.com/wscgoods/detail/272t5vn5o19dr?step=2

640?wx_fmt=gif

640?wx_fmt=jpeg

640?wx_fmt=gif

1024 festival programmers, CSDN's code book store for programmers put a "price", the audience 20% off all books, electronic products can have a large coupon, before buying can end Gavin micro-channel customer service to receive a coupon Oh, only that can participate free single event, Gavin end micro-channel into the group draw , this article from the "zero-based easy Python"

640?wx_fmt=png

The first to write code

Do not be afraid this thing, writing code, in fact, and writing, you first need to have a title, then the title for dismantling. As for literary talent, that is, the code is pretty is another matter. I believe that many readers read a lot of books Basics of Python, Python or buy a lot of video lessons, but still do not know how to write code. The reason only one, is the idea of ​​coding has not changed.
The only change Python coding method of thinking is real. Only in actual combat you can find: Code because fewer or more than one letter, or is the code used is Chinese characters instead of English characters does not lead to the expected results appear; code can run, no syntax errors, but not the result they want to ...... but when you get the results by imitating other people's code to run, is not it also a sense of accomplishment. Then slowly he can learn to design code, it is also possible to teach someone else to write the code. The code will become imitate the design code, finally able to write the code yourself.
In short, our goal is, given any development project, you have the brain encodes ideas immediately, and the rest is hands-write the code only.

640?wx_fmt=jpeg

640?wx_fmt=png

Raw materials program - data

Before you start coding, we need to understand two things: First, coding rules; the second is what is data.
This article refers to the coding of Python written in high-level programming language. Since it is a language, there must syntax, and also need material, you can think of it as Chinese or English grammar, language equivalent coding rules, which is the basics of programming in Python. When other basic grammar is completed, we will be able to enter the combat phase of the project.
Then tell us about the data.
简单地说,数据就是在计算机中的任何东西,比如音乐、电影、文章等。Python编程就是利用自己的语法规则对其进行加工处理,然后呈现出想要的数据结果。所以你可以把程序或者代码看成一个服装加工厂:布料就是程序中使用的数据,机器就是根据语法处理数据,衣服就是代码输出的结果。
有时数据太多,不好理解。对其进行分类是一个方法,非常便于理解与处理。Python数据类型如下表所示。
640?wx_fmt=png

640?wx_fmt=png

学会写注释,方便你我他

注释就是在代码里添加的解释说明。代码是告诉阅读的人做什么事情,而注释是告诉阅读的人为什么这么做。这点在学习编程时特别重要,尤其在刚开始时一定要养成写注释的习惯,不要嫌麻烦,因为注释便于阅读代码的人理解。
在实际工作中,项目一般都很大,需要协作完成。如果没写注释就给下一个人阅读,那么阅读就可能变成一件特别痛苦的事情。有时候代码漂不漂亮也要看注释是不是全面。不过目前的普遍状况是,国内很多程序员,即使是工作了很多年的老程序员也不喜欢写注释。因为麻烦,他们认为这是多干活。这个观点是错误的,因为即使是自己写的代码,几年以后可能自己都不记得写的是什么了。另外,一些大公司代码注释写得都非常全面,比如Google、Oracle等。
在Python中,单行注释用井号(#)表示,注释就是#后面的内容;多行注释用一对三引号(''' ''')或者一对三个双引号(""" """)表示。单行注释一般用于某一行的解说说明,而多行注释一般用于整个文本或者某一个代码区域的解释说明,其中三个双引号表示对整个文档的说明。对于这些注释,Python解释器不会执行具体的内容。在下面的例子中,Python解释器会忽略注释,只输出Hello World。
例子:多行注释与单行注释

"""
    本章为第三天内容:夯实基础的内容
    主要包括:
        数据类型
        变量等
"""
'''
    此处是多行注释
    可以写很多行
'''

# 打印输出Hello World!
# 注意:请在#与注释内容中间留一个空格
print('Hello World!')

运行结果如下所示。

Hello World!

注意:并不是每一行代码都需要注释,只有关键的地方才需要注释,例如,新的语法点、代码重点解决的问题、重要的细节、结论等。

640?wx_fmt=png

常量与变量

常量,顾名思义就是值不能被改变的量,比如5、10等数字或者一个字符串的文本。

与常量相对应的就是变量,顾名思义就是值一直在改变的量。因为值在改变,我们需要给它取一个名字,也就是标识符。在Python编程中,我们把标识符称为变量名,并且使用等号(=)把变量名和值关联起来,具体的语法是:

变量名 = 值

例子:

# 定义变量,并使用print()函数打印出来
# my_name是变量名,刘德华为值
# 变量名不变,值可以变,比如值换成周杰伦
my_name = "刘德华"
print(my_name)
my_name = "周杰伦"
print(my_name)

运行结果如下所示。

刘德华
周杰伦

注意:变量存在内存中。Python语言对大小写敏感,例如my_name与My_name对于Python语言来说是两个不同的变量。

变量命名规则

变量命名是有一定规则的。如果违背了规则,则会出错,具体规则如下所示。

变量名只能以字母或下画线开头,不能以数字开头,但是可以以数字结尾。

例子:

 '''
    变量规则介绍:
    第一个语句错误;
    第二个语句正确
''' 
3_log = 'This is a log file'
log_3 = 'This is a log file'

运行结果如下所示。

3_log = 'This is a log file'
     ^
SyntaxError: invalid token

变量名不能包含空格,否则认为是语法错误。比如my name是错误的,解决方法是使用下画线(_)连接起来,变成my_name。

不能用Python中的关键字作为变量名。

变量命名方法

在符合变量命名规则的前提下,变量名最好简短、易懂,即从变量名就能看出其代表的意思。比如my_name肯定比a好懂(千万不要使用a、b、c做变量名)。

当变量需要用两个以上单词表示时,常用的命名方法有两种。

第一种命名方法

驼峰式大小写,即第一个单词的首字母小写,第二个单词的首字母大写,例如firstName、lastName。也可以每一个单词的首字母都采用大写,例如FirstName、LastName、CamelCase。它也被称为Pascal命名法。

第二种命名方法

两个单词不能直接用连字符(-)或者空格连接,但是可以使用下画线连接,比如first_name、last_name。

640?wx_fmt=png

数字类型

整数

整数又称为整型,也就是int类型,在Python中,可以直接对整数进行算数运算。操作与操作符如下表所示。

640?wx_fmt=png

例子:

'''
    整数运算
'''
# 加法
add = 3 + 4
# 在Python中,format方法是格式化输出,也就是在{}处替换变量的值。后面项目实战中会经
#常用到
print('3+4的值是 {}'.format(add))

# 减法
sub = 10 - 8
print('10 - 8的值是{}'.format(sub))

# 乘法
multi = 23 * 3
print(' 23 * 3 的值是{}'.format(multi))

# 除法
div = 10 / 2
print('10 /2 的值是{}'.format(div))

# 取模,返回除法的余数
delivery = 7 % 3
print('7%3的值是{}'.format(delivery))

# 取整除,返回商的整数
round_number = 7 // 3
print(' 7 //3 的值是{}'.format(round_number))

# 幂运算——X的几次方
power = 7 ** 3
print('7**3的值是{}'.format(power))

运行结果如下所示。

3+4的值是7
10 - 8的值是2
 23 * 3的值是69
10 /2 的值是5.0
7%3的值是1
 7 //3 的值是2
7**3的值是343

浮点数

带小数点的数字都是浮点数。它也可以进行类似整数的运算,比如加、减、乘、除等。
例子:

'''
    浮点数运算
'''
print('以下为浮点数运算的例子')
# 加法
add = 0.2 + 0.1
#Python中,format方法是格式化输出的,也就是在{}处替换变量的值。后面项目实战中会经常用到
print('0.2+0.1的值是 {}'.format(add))

# 补充内容
# 格式化输出format
# 在Python 3.6以上版本中,为了减少{},可以使用f' '的方法
com = 'Complex'
comp = 'complicated'

# Python 3.6以下版本的用法
print('\n Python 3.6以下的format用法:')
print('{} is better than {}'.format(com,comp))

# Python 3.6以上版本的用法
print('\n Python 3.6以上的format用法:')
print(f'{com} is better than {comp}')

# 减法
sub = 10.9 - 8.1
print('10.9 - 8.1的值是{}'.format(sub))

# 乘法
multi = 0.1 * 3
print(' 0.1 * 3 的值是{}'.format(multi))

# 除法
div = 10.0 / 2.0
print('10.0 /2.0 的值是{}'.format(div))

# 取模,返回除法的余数
delivery = 7 % 4.3
print('7%4.3的值是{}'.format(delivery))

# 取整除,返回商的整数
round_number = 7 // 4.3
print(' 7 //4.3 的值是{}'.format(round_number))

# 幂运算——X的几次方
power = 7 ** 2.0
print('7**2.0的值是{}'.format(power))

运行结果如下所示。

0.2+0.1的值是 0.30000000000000004

Python 3.6以下版本的format用法:
Complex is better than complicated
10.9 - 8.1的值是2.8000000000000007
 0.1 * 3 的值是0.30000000000000004
10.0 /2.0 的值是5.0
7%4.3的值是2.7
 7 //4.3 的值是1.0
7**2.0的值是49.0

注意:结果包含的小数位数可能是不确定的,这个是可以忽略的。

640?wx_fmt=png

布尔类型

Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算。
1.与运算:只有两个布尔值都为True时,计算结果才为True。
例子:

True and True   # ==> True
True and False   # ==> False
False and True   # ==> False
False and False   # ==> False

2.或运算:只要有一个布尔值为True,计算结果就是True。

例子:

True or True   # ==> True
True or False   # ==> True
False or True   # ==> True
False or False   # ==> False

3.非运算:把True变为False,或者把False变为True。

例子:

not True   # ==> False
not False   # ==> True

布尔运算在计算机中用来做条件判断,根据运算结果为True或者False,计算机可以自动执行不同的后续代码。

在Python中,布尔类型还可以与其他数据类型做and、or和not运算。

例子:

#布尔类型
a = True
print(a and 'a=T' or 'a=F')

运行结果如下所示。

a=T

计算结果不是布尔类型,而是字符串a=T,这是为什么呢?因为Python把0、空字符串和None看成False,其他数值和非空字符串都看成True,所以True and 'a=T'计算结果是'a=T'。继续计算'a=T' or 'a=F',所以计算结果还是'a=T'。
要解释上述结果,又涉及and和or运算的一条重要法则:短路运算。短路运算符的意思是,运算符左右的表达式只有在需要求值的时候才进行求值。比如x or y,Python从左到右进行求值,先对表达式x进行真值测试,如果表达式x是真值,则根据or运算符的特性,不管y表达式的bool结果是什么,运算符的结果都是表达式x,表达式y不会进行求值。
在计算a and b时,如果a是False,根据与运算法则,则计算结果必定为False,因此返回a;如果a是True,则整个计算结果必定取决于b,因此返回b。
在计算a or b时,如果a是True,根据或运算法则,则计算结果必定为True,因此返回a;如果a是False,则计算结果必定取决于b,因此返回b。
所以Python解释器在做布尔运算时,只要能提前确定计算结果,就不会往后算了,直接返回结果。

640?wx_fmt=png

字符串类型

什么是字符串

字符串就是一系列字符。在Python中,单引号、双引号或者三引号里面的内容就是字符串。如果字符串中包括单引号或者双引号,那么可以使用“\”对字符串中的字符进行转义。

例子:

# 单引号里面的文本就是字符串
‘I am a boy’

# 双引号其实和单引号一样,一般推荐使用单引号
"欢迎你加入Python实战圈"

# 三引号表示的字符串,一般是很长的文字
# 三引号一般用来写文本注释
'''
我们实战圈的第一个项目就是“如何7天入门Python”
每一天都会安排学习内容,只需要40分钟就可以搞定
学完以后记得写作业并提交到“知识星球”
刚开始,学习节奏放慢一些
计划三天更新一次内容
希望你能参与进来
'''

# 转意字符串(\n)
command = 'Let\'s go!'
print('\n使用转义字符输出 :',command)

运行结果如下所示。

使用转义字符输出 :Let's go!

字符串的基本用法

1.添加空白
在编程中,一定的空白输出是为了方便阅读。Python常用的添加空白的方法有制表符(\t)、空格或者换行符(\n)。制表符表示把文字空两格输出。
例子:

# 添加空白
# 制表符可以组合使用
print("欢迎来到Python实战圈,\n")
print('\t你想要学习 Python 的哪方面内容,请留言。')

运行结果如下所示。

欢迎来到Python实战圈,
你想要学习 Python 的哪方面内容,请留言。

2.拼接字符串
拼接字符串就是把两个或两个以上的字符串合并在一起。该操作在项目中经常用到,比如爬虫时,网页的正则表达式(以后会介绍)太长,可以用拼接的方法连接起来;也可以把两个变量的字符串拼接为一个等。Python使用加号(+)来拼接字符串。
例子:

# 拼接字符串
log_1_str = 'The error is a bug.'
log_2_str = ' We should fix it.'
log_str = log_1_str + log_2_str
print('\n拼接后的字符串就是:',log_str)

运行结果如下所示。

拼接后的字符串就是:The error is a bug. We should fix it.

字符串的常见运算

1.修改字符串的大小写

在Python中,你会经常听到的两个名词是函数和方法。函数就是能独自完成特定任务的独立代码块,可以被调用;方法是面向对象编程语言中使用到的名词。Python是面向对象的编程语言,面向对象就是一切都是对象,比如你、我、他,统称为人(people),人就是一个对象。人可以奔跑(run),奔跑就是一个方法,合起来就是people.run()。

例子:

# 字符串大小写转换
welcome = 'Hello, welcome to Python  practical circle'

# title(),每个单词的首字母大写
print('\n每个单词的首字母大写:', welcome.title())

# capitalize(),段落的首字母大写
print('\n段落的首字母大写:',welcome.capitalize())

# lower(),所有字母小写
print('\n所有字母小写:',welcome.lower())

# upper(),所有字母大写
print('\n所有字母大写:',welcome.upper())

# 大写转小写,小写转大写
print('\n大写转小写,小写转大写:',welcome.swapcase())

# String.isalnum(),判断字符串中是否全部为数字或者英文,符合就返回True,不符合就返回False,如果里面包含符号或者空格之类的特殊字符,那么也会返回False
print('\n判断字符串是否全部为数字或者英文:',welcome.isalnum())

# String.isdigit(),判断字符串中是否全部为整数
print('\n判断字符串中是否全部为整数:', welcome.isdigit())

运行结果如下所示。

每个单词的首字母大写:Hello, Welcome To Python  Practical Circle

段落的首字母大写:Hello, welcome to python  practical circle

所有字母小写:hello, welcome to python  practical circle

所有字母大写:HELLO, WELCOME TO PYTHON  PRACTICAL CIRCLE

大写转小写,小写转大写:hELLO, WELCOME TO pYTHON  PRACTICAL CIRCLE

判断字符串是否全部为数字或者英文:False

判断字符串中是否全部为整数:False

2.删除字符串两端的空白
删除字符串两端的空白,在数据清理时经常被用到。常见的操作是去除两端或者一端的空格。
例子:

    # 删除两端的空白
love_Python = '  Hello, Python Practical Circle  '

    # 删除字符串两端的空白
print('删除字符串两端的空白',love_Python.strip())

    # 删除字符串右侧的空白
print('删除字符串右侧的空白',love_Python.rstrip())

    # 删除字符串左侧的空白
    print('删除字符串左侧的空白',love_Python.lstrip())

运行结果如下所示。

    删除字符串两端的空白 Hello, Python Practical Circle
    删除字符串右侧的空白   Hello, Python Practical Circle
    删除字符串左侧的空白 Hello, Python Practical Circle

3.其他注意事项

Python中字符串的操作非常多,以上只列出了部分常用操作。有一点需要注意的是,Python中的字符串不允许修改值,只允许覆盖值。也就是说,字符串只能重新赋值。

字符串的切片

切片(slice)操作是Python中经常用到的操作。字符串的切片就是从一个字符串中获取子字符串(字符串的一部分)。我们使用一对方括号、起始偏移量(start)、终止偏移量(end),以及可选的步长(step)来定义一个切片。

语法:[start:end:step]
• [:] 提取从开头(默认位置0)到结尾(默认位置-1)的整个字符串
• [start:] 从start提取到结尾
• [:end] 从开头提取到end-1
• [start:end] 从start 提取到end-1
• [start:end:step] 从start提取到end-1,每step个字符提取一个
• 左侧第一个字符的位置/偏移量为0,右侧最后一个字符的位置/偏移量为-1

例子:

# 字符串切片
word = 'Python'
print(word[1:2])
print(word[-2:])
print(word[::2])
print(word[::-1])

运行结果如下所示。

y
on
Pto
nohtyP

各种类型之间的转换

在Python中,各个数据类型是可以互相转化的,并且可以使用type()函数查看某一个变量的类型。

语法:type(变量名) 用来查看变量的数据类型

type () function is often used in the actual project, because only knows what type of variables can make the appropriate operation, such as a dictionary and a list of different types of operations. The type of conversion project is also frequently used in actual combat, such as a supermarket monthly sales is a character type, the type can be converted to digital statistics, such as the calculation of averages, etc., as shown in the following specific transformational grammar.
grammar:

float(a) 将变量a转换为浮点数
int(b) 将变量b转换为整数
str(c)将变量c转换为字符串
其中a、b、c为任意变量类型

example:

'''
    各种数据类型之间的转换
'''

print('\n各个数值类型的转换')
number = 100

# number的数据类型是整型,用int表示
print('number的数据类型是:')
print(type(number))

# 将整数转换为浮点数
float_number = float(number)
print('\nfloat_number的数据类型是:')
print(type(float_number))

# 将整型转换为字符串
print('\nnumber转换为字符串类型')
str_number = str(number)
print('str_number的数据类型是:')
print(type(str_number))

# 将字符串转换为整型int()或者浮点数float()
print('\nstr_number转换为数字类型')
int_str_number = int(str_number)
float_str_number = float(str_number)
print('int_str_number的数据类型是:')
print(type(int_str_number))
print('float_str_number的数据类型是:')
print(type(float_str_number))

Run results are shown below.

各个数值类型的转换
number的数据类型是:
<class 'int'>

float_number的数据类型是:
<class 'float'>

number转换为字符串类型
str_number的数据类型是:
<class 'str'>

str_number转换为数字类型
int_str_number的数据类型是:
<class 'int'>
float_str_number的数据类型是:
<class 'float'>

This article taken from the book "Easy zero-based Python"

640?wx_fmt=jpeg

(Click on the seal surface of the solution book details)

This is an interesting, helpful, eager to learn Python programming book! Easy to understand language, fun interesting case to give readers easy, step by step from scratch to master Python 3 program.

640?wx_fmt=jpeg

640?wx_fmt=gif
Welcome to join our group code book, where you can talk to people and technology together in life, together with the technical talk, talk about working life together, because the group is full of 100 people, can only add micro signal into the oh
640?wx_fmt=png

640?wx_fmt=gif

Guess you like

Origin blog.csdn.net/csdnsevenn/article/details/102735107