As the saying goes, a journey of a thousand miles begins with a single step. No matter what you do, the foundation is the most important. When you think you are "proficient" in a certain language, have you ever asked yourself:
"Can you express this knowledge in the simplest words? Can people who don't understand understand it?"
When you are truly proficient in a language, I believe you can do it. If you can't do it, then you need to look down and learn the basics.
In order to help everyone thoroughly understand Python, July Online has displayed pictures and texts on major knowledge points.
Covers a large number of logical processing ideas and corresponding cases of text concepts and codes. You can directly assist your digestion and absorption of knowledge through the logical processing process of text concepts and pictures.
1. Understanding Python
Python is a computer programming language. It is a dynamic, object-oriented scripting language that was originally designed for writing automated scripts (shells). With the continuous updates of the version and the addition of new language features, it is increasingly used for independent and large-scale projects. development.
The founder of Python is the Dutchman Guido van Rossum. During the Christmas period of 1989, in Amsterdam, in order to kill the boredom of Christmas, Guido decided to develop a new script interpreter as an inheritance of the ABC language.
ABC is a teaching language designed by Guido.
In Guido’s own opinion, ABC is a very beautiful and powerful language that is specially designed for non-professional programmers.
But the ABC language did not succeed. The reason, Guido believes, is that it is not open.
Guido is determined to avoid this mistake in Python. At the same time, he wanted to achieve something that had been glimpsed in ABC but never came to fruition.
In this way, Python was born in the hands of Guido.
It can be said that Python developed from ABC. Python has become one of the most popular programming languages.
Since 2004, the usage of python has grown linearly. Python2 was released on October 16, 2000, and the stable version is Python2.7.
Python3 was released on December 3, 2008 and is not fully compatible with Python2. In January 2011, it was named the 2010 Language of the Year by the TIOBE Programming Language Ranking.
Due to the simplicity, readability and scalability of the Python language, there are an increasing number of research institutions using Python for scientific computing abroad. Some well-known universities have adopted Python to teach programming courses.
For example, the basics of programming at Carnegie Mellon University and the introduction to computer science and programming at MIT are taught using the Python language.
Many open source scientific computing software packages provide Python calling interfaces, such as the famous computer vision library OpenCV, three-dimensional visualization library VTK, and medical image processing library ITK.
There are even more dedicated scientific computing extension libraries for Python, such as the following three very classic scientific computing extension libraries: NumPy, SciPy and matplotlib, which respectively provide fast array processing, numerical operations and Drawing function.
Therefore, the development environment composed of the Python language and its numerous extension libraries is very suitable for engineering and scientific researchers to process experimental data, make charts, and even develop scientific computing applications.
Related recommendations: "Python Video Tutorial"
2. Python application scenarios
1. Web application development
Python is often used for web development. For example, through the mod_wsgi module, Apache can run web programs written in Python.
Python defines the WSGI standard application interface to coordinate communication between Http servers and Python-based Web programs. Some web frameworks, such as Django, TurboGears, web2py, Zope, etc., allow programmers to easily develop and manage complex web programs.
2. Automated scripts for operating system management and server operation and maintenance
In many operating systems, Python is a standard system component. Most Linux distributions, as well as NetBSD, OpenBSD and Mac OS X, have integrated Python and can run Python directly under the terminal.
There are some Linux distribution installers written in Python language, such as Ubuntu's Ubiquity installer, Red Hat Linux and Fedora's Anaconda installer.
Gentoo Linux uses Python to write its Portage package management system. The Python standard library contains multiple libraries that call operating system functions.
Through the third-party software package pywin32, Python can access Windows COM services and other Windows APIs.
Using IronPython, Python programs can directly call the .Net Framework. Generally speaking, system management scripts written in Python are better than ordinary shell scripts in terms of readability, performance, code reuse, and scalability.
3. Web crawler
Python has a large number of HTTP request processing libraries and HTML parsing libraries, as well as a mature and efficient crawler framework Scrapy and a distributed solution scrapy-redis, which are very widely used in crawler applications.
4. Scientific computing
NumPy, SciPy, Pandas, and Matplotlib allow Python programmers to write scientific computing programs.
5. Desktop software
PyQt, PySide, wxPython, and PyGTK are powerful tools for quickly developing desktop applications in Python.
6. Server software (network software)
Python has perfect support for various network protocols, so it is often used to write server software and web crawlers.
The third-party library Twisted supports asynchronous network programming and most standard network protocols (including clients and servers), and provides a variety of tools that are widely used to write high-performance server software.
7. Games
Many games use C to write high-performance modules such as graphics display, and use Python or Lua to write game logic and servers.
Compared with Python, Lua has simpler functions and smaller size; while Python supports more features and data types.
8. Concept implementation, early product prototypes and iterations
Google, NASA, and Facebook all use Python extensively internally.
3. Basic knowledge of Python
1. Grammar basics: Comments
Mark certain codes in the program Explain that this is the role of comments, which can greatly enhance the readability of the program. Commonly used single-line comments in python are #, and common multi-line comments are three pairs of single quotes ''' '''
# 这是一个单行注释 # print("七月你好,我是八月.") 此行代码不执行显示. # 快捷键: ctrl + / 快速设置注释 和 快速取消注释 ''' 这是一个多行注释 该范围内的代码不会执行显示 '''
2. Grammar basics: data type
3. Grammar basics: keywords and identifiers
Keywords
python Some identifiers with special functions, this is what is called Keywords. Keywords are already used by Python, so developers are not allowed to define their own identifiers with the same names as keywords
So what are the keywords in Python?
['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']
So how do we query what keywords are in python?
# 在python中若想查询python中有哪些关键字可以先导入keyword模块 import keyword #导入关键字模块 print(keyword.kwlist) #查询所有关键字
Actual operation
Warm reminder: We cannot use these keyword names when naming variables or files, otherwise an error will be reported and thrown. Abnormal ~ please pay attention.
Identifier
Some symbols and names customized by Flowers of the Motherland (developer) in the program. Identifiers are defined by ourselves, such as variable names, function names, class names, etc.
Identifier rules
There are also certain rules for defining these identifiers: identifiers are composed of letters, underscores and numbers, but they cannot start with numbers. They can start with underscores or letters. Note that in python , identifiers are strictly case-sensitive. The variable Andy is not equal to the variable andy. These are two variables.
Identifier naming rules
The naming rules can be simply understood as we need to abide by the relevant rules when naming. The main purpose is to let others understand what it means at a glance. , what it is used for. It can improve the readability of the code. For example: name is defined as name, and student is defined as student.
Camel case naming method
At the same time, when multiple words are needed to be combined, we use camel case naming method:
Little camel case naming method: The first word starts with Start with a lowercase letter; the first letter of the second word is capitalized, for example: myName, aDog
Camel case naming method: The first letter of each word is capitalized, for example: FirstName, LastName. However, there is another naming method that is more popular among programmers, which is to use underscore "_" to connect all words, such as send_buf
The relationship between the name and the object
We can use = etc. No. to establish a reference relationship between the defined variable name and the object. You can also use del to release this reference relationship.
# For example
name = 'The most handsome man in the world' # I assigned the string value of the most handsome man in the world to the variable name name, which can be understood as, next time I pass name The name can be connected to the most handsome person in the world.
Practical operation
# del 解除引用关系 age = 17 # 永远17岁 del age # del 是 delete 的缩写,此行代码表示删除 age 和 17 之间的引用关系 print(age) # 因为已经删除引用关系,所以访问不到 17 这个数据,则抛出异常
Practical operation
Name Space
When we define some variable names, function names, and class names, there will be a space to accommodate these names and their corresponding data types and data contents.
# 举个例子 height = '190CM' # 小编的理想身高.但是现实很残酷. def func1(): pass class Fun(): pass
Actual operation
##Use %whos to view the name space. We can also use %reset to clear this space. After clearing, we need to re-execute A process defined once can be re-stored in the namespace. Actual operation4. 语法基础:输出
普通的输出
python中的输出
用 print() 在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出 '李铁柱',用代码实现如下:
name = '李铁柱' print(name)
运行结果
print()也可以打印整数,或者计算结果
# 举个例子 print(300) print(100 + 200)
实际操作
因此,我们可以把计算100 + 200 的结果打印得更漂亮一点:
print('100 + 200 = ', 100 + 200)
实际操作
注意,对于100 + 200,Python 解释器自动计算出结果 300,但是,'100 + 200 =' 是字符串而非数学公式,Python 把它视为字符串,直接进行打印输出。
5. 语法基础:格式化输出
格式化操作的目的,比如说有下列代码:
print('大家好,我叫 李铁柱,我今年 24 岁') print('大家好,我叫 候德柱,我今年 25 岁') print('大家好,我叫 候不柱,我今年 26 岁')
大家仔细观察一下,能够看到,我在 print 输出自我介绍的时候,用了很多的相同字体,就只有一些不同的字段,那么咱们能否简化一下程序呢?
age = 24 print("我今年%d岁" % age) age += 25 print("我今年%d岁" % age) age += 26 print("我今年%d岁" % age)
实际操作
在程序中,看到了 % 这样的操作符,这就是 Python 中格式化输出。
age = 18 name = "小岳岳" print("我的姓名是%s, 年龄是%d" % (name, age))
运行结果
在这里咱们一次使用了两个格式化符号,分别是:%s 和 %d。
%s:表示格式化输出字符串
%d:表示格式化输出十进制整数
除了这两个格式化输出字符还有很多,往下看。
常用的格式化符号
6. 语法基础:输入
现在,你已经可以用 print() 输出你想要的结果了。但是,如果要让用户从电脑输入一些字符怎么办?Python 提供了一个 input(),可以让用户输入字符串,并存放到一个变量里。比如输入用户的名字:
name = input() # input()
当你输入name = input()并按下回车后,Python交互式命令行就在等待你的输入了。这时,你可以输入任意字符,然后按回车后完成输入。
输入完成后,不会有任何提示,Python 交互式命令行又回到就绪状态了。那我们刚才输入的内容到哪去了?答案是存放到 name 变量里了。可以直接输入 name 查看变量内容:
7. 语法基础:运算符
标准操作符
python 同大多数语言一样,也有自己的操作符,分别用来应对不同的工作内容。今天要讲的是 Python 中最常用的几种标准操作符:算术运算符、赋值运算符、比较运算符、逻辑运算符。
算数运算符
注意:混合运算时,优先级顺序为: ** 高于 * / % // 高于 + - ,为了避免歧义,建议使用 () 来处理运算符优先级。并且,不同类型的数字在进行混合运算时,整数将会转换成浮点数进行运算。
实际操作
a = 10 b = 20 print(a+b) print(a-b) print(a*b) print(a/b)
赋值运算符
实际操作
a = 10 # 单个赋值 b = 20 c,d,e = 30,40,50 # 多个赋值 c = 30 , d = 40 , e = 50 print(a,b,c,d,e)
复合赋值运算符
实际操作
c = 40 a = 20 # print(c += a) # 直接 print() 是无法输出结果的哟 c += a print(c) c -= a print(c) c *= a print(c) c /= a print(c) c %= a print(c)
8. 语法基础:常用的数据类型转换
数据类型转换
有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可。
以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值。
类型转换
# 给小伙伴们举几个例子 # int(): 将数据转换为 int 类型 str1 = "10" num1 = int(str1) print(int) # int() 处理浮点数,只留下整数部分,舍弃小数部分(并不是四舍五入操作) num2 = int(3.74) print(num2) # float() 将数据转化为浮点数 str2 = "3.14" f1 = float(str2) print(type(f1)) # str() : 转换为 字符串类型 num1 = 10 f1 = 3.14 print(type(str(num1))) # eval(): 将字符串形式的数据,转换为原本的类型 str1 = "3.14" print(type(eval(str1)))
操作结果
The above is the detailed content of What you must know in Python. For more information, please follow other related articles on the PHP Chinese website!