Basic grammar knowledge of Python entry 2

Basic grammar knowledge of Python entry 2

Today is the second update. Yesterday’s article shows 101 readings.Thank you very much to all the classmates and predecessors
who took the time to read, ღ( ´・ᴗ・`) more than heart
, I hope that everyone will have some gains after reading. The knowledge behind is constantly progressive. The first level is the accumulation and application of the foundation. Therefore, the foundation must be firmly laid before it can be used better. Today's content is also very basic , Feel free to take a look and understand, OK, let’s start today’s topic.

1. Reserved words
in Python. Some words in Python are given specific meanings, so they cannot be used when naming any object. You can view them by keyword, but you don’t need to remember this, because when your name is wrong An error will be reported, the code is as follows:

# 开发时间:2020/11/1 13:40

import keyword  # 首先当然需要导入keyword呀
print(keyword.kwlist)

Importing third-party modules (import+module name) is one of the methods, and we will continue to explain when we share this part of knowledge later

The results of the operation are as follows: (The output content is the name that cannot be used, it is ok to understand it a little)

E:\Python\python.exe E:/py/CSDN博客/语法2.py
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

Process finished with exit code 0

2. Identifiers in Python
Identifiers are the names of variables, functions, classes, modules, and other objects; pay attention to the following rules: (It’s ok if you look at it at a glance, he will prompt you when your identifier is wrong)
- ---->You can use letters, numbers, and underscore _ to name <-----
--------------------->Cannot use numbers starting with <-- --------------
------------------------>Cannot be a reserved word<------ -------------
----------------------->Strict case sensitive<-------- ---------

2. Variables in
Python①. Variables are composed of identification (id), type (type), and value (value). Variables can be assigned multiple times. After assignment, the variable name will point to the new variable space. See the code for details:

# 开发时间:2020/11/1 13:40
name='雅嘿哟'    # name就是变量名
print('标识',id(name))  
print('类型',type(name))
print('值value',name)  # 直接输出结果就是其value值
print('-----------------------------------------------')
name='new'   # 变量的多次赋值变量名会指向新的变量空间
print('new')

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
标识 31368176
类型 <class 'str'>
值value 雅嘿哟
-----------------------------------------------
new

Process finished with exit code 0

3. Data types commonly used in Python
Note: Pay attention to the distinction between Chinese and English punctuation, otherwise it is easy to report errors
①. Integer type: int → 98, 4354;
Integer English is integer, which can represent positive, negative and zero, different bases Use different representations: [Decimal → is the default base, input directly according to the element number], [Binary→Start with 0b], [Octal→Start with 0o], [Hexadecimal→Start with 0x]

②. Floating-point number type: float→ 98.53;
floating-point number is composed of integer and decimal part. Due to the inaccuracy of floating-point number storage, there may be uncertain decimal places when calculating floating-point number. The solution is Import the module decimal, the code is as follows:

from decimal import Decimal    # 从decimal库中导入Decimal
n1=2.2
n2=2.1
print(n1+n2)
print(n1+n3)    # 并不是所有的浮点数的计算结果会出问题,为了避免出现需要使用下面的Decimal模块
print(Decimal(str(n1)) + Decimal(str(n2)))  # 注意需要将n1和n2转换为字符串

str(n1) is the conversion of data type, which converts floating-point number type to string type. The knowledge of data type conversion will be discussed later in this article (* ̄︶ ̄)
The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
4.300000000000001
5.4
4.3

Process finished with exit code 0

③. Boolean type: bool→True, False;
Boolean type is Boolean in English, bool is abbreviation, Boolean type can indicate true or false, Boolean value can be converted into integer: True is equivalent to 1, False is equivalent to 0, see the following code for details :

m1 = True
m2 = False
print(type(m1))
print(type(m2))
# 布尔值可以转化为整数计算
print(m1 + 1)  # True相当于1
print(m2 + 1)  # False相当于0

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
<class 'bool'>
<class 'bool'>
2
1

Process finished with exit code 0

④. String type: str
a, string can be defined in three ways [single quotation mark''] [double quotation mark ""] [three quotation mark'''''' or """ """];
b,Note that all punctuation is in English, and errors will be reported in Chinese;
C. The character string defined by single and double quotation marks must be on one line;
d. The character string defined by triple quotation marks can appear on multiple consecutive lines.
See the following code for details:

s1='python'
s2="python"
s3='''hello

python'''
s4="""hello
python"""
print(s1,type(s1))
print(s2,type(s2))
print('---------------------------')
print(s3,type(s3))
print('---------------------------')
print(s4,type(s4))

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
python <class 'str'>
python <class 'str'>
---------------------------
hello

python <class 'str'>
---------------------------
hello
python <class 'str'>

Process finished with exit code 0

4. Conversion of data types in
Python①, convert other data types to string types, the code is as follows:

a=1.243
b=15
c=True
print(type(a),type(b),type(3))  # 使用type查看a,b,c的数据类型
print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
<class 'float'> <class 'int'> <class 'int'>
1.243 15 True <class 'str'> <class 'str'> <class 'str'>

Process finished with exit code 0

②, convert other data types into integer types,
It should be noted that: a. Only when the string is an integer can be converted, otherwise an error will be reported; b. The conversion of a floating-point number to an integer type will be zeroed and rounded
The specific code is as follows:

a=1.243   # 浮点数类型转换成整数类型会抹零取整
b='15'
c=True
print(type(a),type(b),type(c))  # 使用type查看a,b,c的数据类型
print(int(a),int(b),int(c),type(int(a)),type(int(b)),type(int(c)))  # a,b,c 数据类型都转换成了整数类

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
<class 'float'> <class 'str'> <class 'bool'>
1 15 1 <class 'int'> <class 'int'> <class 'int'>

Process finished with exit code 0

③, convert other data types into floating-point number types
It should be noted that: a. The text type cannot be converted into a floating point number; b. The integer type is converted into a floating point number, and the end is .0
The specific code is as follows:

a=1
b='15'
c='15.0'
d=True
print(type(a),type(b),type(c),type(d))
print(float(a),float(b),float(c),float(d),type(float(a)),type(float(b)),type(float(c)),type(float(d)))

The results are as follows:

E:\Python\python.exe E:/py/CSDN博客/语法2.py
<class 'int'> <class 'str'> <class 'str'> <class 'bool'>
1.0 15.0 15.0 1.0 <class 'float'> <class 'float'> <class 'float'> <class 'float'>

Process finished with exit code 0

5, Python comments in the ( _ thanks to the patience to read this students and seniors)
and finally to the last knowledge of the matter today, O (∩_∩) O haha ~
Python comment is very simple, I believe that especially in front of the code is also I found some gray fonts with # in front of them. In fact, they are the content of comments, so that you and others can understand the meaning and functions of the code you wrote; generally, the writing of a relatively large code requires teamwork. If some content is not commented Maybe teammates are reading the Bible
①, single-line comments with # in front of the content;
②, multi-line comments use triple quotation marks ``''''';
③, the shortcut comment is Ctrl+/ (this can also select multiple lines of comments at the same time, which is very convenient )
Let's take a look at the code:

# 真的非常感谢各位拔冗阅读我的文章,你们的点赞和收藏是对我莫大的支持和鼓励
'''
python
真的是一门很好入门的语言
Python
它的库真的非常丰富,便捷了我们的使用
'''

Well, today’s knowledge sharing is over. Thank you for reading and liking. This week’s weekend is also over. As long as there are not many courses, we will stick to it. Let’s cheer together. ღ( ´・ᴗ・`) than heart

Guess you like

Origin blog.csdn.net/qq_45227014/article/details/109424771