This is the best Python tutorial I've ever seen: ten minutes to get to know python

best python tutorial ve seen


This article mainly introduces the concise ten minutes Python Introductory tutorial ,Python The simplicity of the language itself also makes all kinds of Python The introduction to shutter is very popular , This paper is one of the articles in China , Friends in need can refer to

【 brief introduction 】

Python It's a dynamically interpreted programming language .Python Can be in Windows、UNIX、MAC And other operating systems , It can also be in Java、.NET Development platform .

【 characteristic 】

1 Python Use C Language development , however Python No more C Complex data types such as pointers in languages .

2 Python It has strong object-oriented characteristics , And it simplifies the object-oriented implementation . It eliminates the type of protection 、 abstract class 、 Object oriented elements such as interfaces .

3 Python Code blocks use spaces or tab indents to separate code .

4 Python have only 31 A reserved word , And there's no semicolon 、begin、end Wait for the sign .

5 Python It's a strongly typed language , After the variable is created, it will correspond to a data type , Different types of variables that appear in a unified expression need to be typed .

【 Build development environment 】

1 You can go to www.python.org Download installation package , And then through configure、make、make install Installation .

2 You can also go to www.activestate.com To download ActivePython The component package .(ActivePython It's right Python Binary packaging of core and common modules , It is ActiveState Issued by the company Python development environment .ActivePython bring Python It's easier to install , And can be applied to a variety of operating systems .ActivePython Some common Python Expand , as well as Windows Programming interface of environment ). Yes ActivePython Come on , If you are windows user , download msi Just install the package ; If you are Unix user , download tar.gz The package can be decompressed directly .

3 Python Of IDE, Include PythonWin、Eclipse+PyDev plug-in unit 、Komodo、EditPlus

【 edition 】

python2 And python3 It's the two major versions at the moment .

In the following two cases , It is recommended to use python2:

1 When you have no complete control over the environment you are about to deploy ;

2 When you need to use certain third-party packages or extensions ;

python3 It's officially recommended and fully supported in the future , At present, many function improvements are only in python3 On the version .

 This is the best I've ever seen Python course : Ten minutes to meet you Python

 Xiaobian is a python Development Engineer , Here I have a new set of my own python System learning course , Including from the foundation of python Script to web Development 、 Reptiles 、 Data analysis 、 Data visualization 、 Machine learning, etc . Those who want these materials can pay attention to Xiaobian , And in the backstage private letter small make up :“07” Can receive .

【hello world】

1 establish hello.py

2 Programming :
if __name__ == \'__main__\': print "hello word" 


3 Run the program :
python ./hello.py 


【 notes 】

1 Whether it's line comments or paragraph comments , in # Add a space to comment .

2 If you need to use Chinese comments in your code , Must be in python The following comments are added at the beginning of the document :

# -* - coding: UTF-8 -* -

3 The following comment specifies the interpreter
#! /usr/bin/python


【 file type 】

1 Python The file types are divided into 3 Kind of , Source code 、 Byte code and optimization code . All of these can be run directly , There is no need to compile or connect .

2 Source code to .py Extension name , from python To explain ;

3 The source file is compiled and generated with the extension .pyc The file of , The compiled byte file . This file cannot be modified with a text editor .pyc Files are platform independent , Can run on most operating systems . The following statement can be used to generate pyc file :
import py_compilepy_compile.compile(‘hello.py') 


4 The optimized source file will be .pyo For the suffix , That is to optimize the code . It can't be modified directly with a text editor , The following command can be used to generate pyo file :

python -O -m py_complie hello.py 


【 Variable 】

1 python Variables in do not need to be declared , The operation of variable assignment is the procedure of variable declaration and definition .

2 python A new assignment in , A new variable will be created . Even if the name of the variable is the same , The identifications of variables are not the same . use id() Function to get the variable ID :
x = 1print id(x)x = 2print id(x) 


3 If the variable is not assigned , be python It is assumed that the variable does not exist

4 Variables defined outside a function can be called global variables . Global variables can be accessed by any function inside the file and by any external file .

5 Global variables should be defined at the beginning of the file .

6 You can also put global variables in a special file , And then through import To quote :

gl.py The contents of the document are as follows :

Xiaobian is a python Development Engineer , Here I have a new set of my own python System learning course , Including from the foundation of python Script to web Development 、 Reptiles 、 Data analysis 、 Data visualization 、 Machine learning, etc . Those who want these materials can pay attention to Xiaobian , And in the backstage private letter small make up :“07” Can receive .

_a = 1_b = 2 

use_global.py Reference global variables in :
import gldef fun(): print gl._a print gl._bfun() 


【 Constant 】

python There is no reserved word to define constants in . You can define a constant class to realize the function of constant .
class _const: class ConstError(TypeError): pass def __setattr__(self,name,vlaue): if self.__dict__.has_key(name): raise self.ConstError, “Can't rebind const(%s)”%name self.__dict__[name]=valueimport syssys.modules[__name__]=_const() 


【 data type 】

1 python The number types of are divided into integer types 、 Long integer 、 floating-point 、 Boolean type 、 Plural type .

2 python There is no character type

3 python There is no common type inside , Any type is an object .

4 If you need to see the type of the variable , have access to type class , This class can return the type of the variable or create a new type .

5 python Yes 3 There are two ways to represent string types , Single quotation marks 、 Double quotes 、 Three quotes . Single quotation marks and double quotation marks work the same way .python Programmers prefer single quotes ,C/Java Programmers are used to using double quotation marks to represent strings . You can enter single quotation marks in three quotation marks 、 Characters such as double quotes or newlines .

【 Operators and Expressions 】

1 python Autoincrement and autodecrement operators are not supported . for example i++/i- It's wrong. , but i+=1 Yes. .

2 1/2 stay python2.5 It would have been equal to 0.5, stay python2.5 And then it will be equal to 0.

3 It's not equal to != or <>

4 Equal to == Express

5 In logical expressions and Representation logic and ,or Represent logical or ,not Express logical not

【 Control statement 】

1 Conditional statements :
if ( expression ) : sentence 1else : sentence 2 


2 Conditional statements :
if ( expression ) : sentence 1elif ( expression ) : sentence 2…elif ( expression ) : sentence nelse : sentence m 


3 Conditions of nested :
if ( expression 1) : if ( expression 2) : sentence 1 elif ( expression 3) : sentence 2 … else: sentence 3elif ( expression n) : …else : … 


4 python There is no such thing as switch sentence .

5 Loop statement :
while( expression ) : …else : … 


6 Loop statement :
for Variable in aggregate : … else : … 


7 python Does not support similar c Of for(i=0;i<5;i++) Such a loop statement , But with the help of range simulation :
for x in range(0,5,2): print x 

 This is the best I've ever seen Python course : Ten minutes to meet you Python


【 Array correlation 】

1 Tuples (tuple):python A built-in data structure in . Tuples are made up of different elements , Each element can store different types of data , Such as a string 、 Numbers, even elements . Tuples are write protected , That is, the tuple cannot be modified after it is created . Tuples often represent a row of data , Elements in tuples represent different data items . You can think of tuples as immutable arrays . An example of creating a tuple is as follows :
tuple_name=(“apple”,”banana”,”grape”,”orange”) 


2 list (list): Lists are similar to tuples , It's also made up of a set of elements , List can be added 、 Delete and find operations , The value of the element can be modified . A list is an array in the traditional sense . An example of creating a list is as follows :
list=[“apple”,”banana”,”grage”,”orange”] 


have access to append Method to append elements to the tail , Use remove To delete the element .

3 Dictionaries (dictionary): By key - A collection of value pairs , The values in the dictionary are referenced by keys . The key is separated from the value by a colon , key - Value pairs are separated by commas , And it's contained in a pair of curly braces . The creation example is as follows :
dict={“a”:”apple”, “b”:”banana”, “g”:”grage”, “o”:”orange”} 


4 Sequence : Sequences are collections with indexing and slicing capabilities . Tuples 、 Lists and strings belong to sequences .

【 Function related 】

1 python The program is made up of packages (package)、 modular (module) And functions . A package is a collection of modules . A module is a collection of functions and classes that deal with a certain kind of problems .

2 A package is a toolbox for specific tasks .

3 The package must contain a __init__.py file , It is used to identify that the current folder is a package .

4 python Our program is made up of modules . Modules organize a set of related functions or code into a file , A file is a module . The module consists of code 、 Functions and classes . The import module uses import sentence .

5 The function of package is to realize the reuse of program .

6 A function is a piece of code that can be called many times , An example of a function definition is as follows :
def arithmetic(x,y,operator): result={ “+”:x+y, “-“:x-y, “*”:x*y, “/”:x/y } 

7 Function return value can be used return To control .

【 String related 】

1 Format output :
format=”%s%d” % (str1,num)print format 


2 use + Merge strings :
str1=”hello”str2=”world”result=str1+str2 


3 String interception can be done through index / section , It can also be done through split function .

4 Slicing strings :
word=”world”print word[0:3] 


5 python Use == and != To compare strings . If the two variables being compared are of different types , Then the result must be different .

【 Document processing 】

1 Simple handling of files :
context=”hello,world”f=file(“hello.txt”,'w')f.write(context);f.close() 


2 Reading files can use readline() function 、readlines() Functions and read function .

3 Writing to a file can use write()、writelines() function

 This is the best I've ever seen Python course : Ten minutes to meet you Python


【 Objects and classes 】

1 python use class Reserve words to define a class , The first character of the class name should be capitalized . When the type a programmer needs to create cannot be represented by a simple type , You need to define classes , Then create objects from the defined classes . Define class examples : class Fruit: def grow(self): print “Fruit grow”

2 When an object is created , There are three features , The handle to the object 、 Properties and methods . Method to create an object :
fruit = Fruit()fruit.grow() 


3 python There is no protection type modifier

4 Class methods are also divided into public methods and private methods . Private functions cannot be called by functions other than this class , Private methods cannot be called by external classes or functions .

5 python Using functions ”staticmethod()“ or ”@ staticmethod“ The method of instruction transforms ordinary functions into static methods . Static methods are equivalent to global functions .

6 python The constructor for is called __init__, The destructor is called __del__

7 How to use inheritance :
class Apple(Fruit): def … 


【 Connect mysql】

1 use MySQLdb Module operation MySQL The database is very convenient . The sample code is as follows :
import os, sysimport MySQLdbtry: conn MySQLdb.connect(host='localhost',user='root',passwd='',db='address'except Exception,e: print e sys.exit()cursor=conn.cursor()sql='insert into address(name, address) values(%s, %s)'value=((“zhangsan”,”haidian”),(“lisi”,”haidian”))try cursor.executemany(sql,values)except Exception, e: print esql=”select * from address”cursor.execute(sql)data=cursor.fetchall()if data for x in data: print x[0],x[1]cursor.close()conn.close() 

I hope the content of this article can help you , Happy New Year! !

ending

One more sentence at the end , Xiaobian is a python Development Engineer , Here I have a new set of my own python System learning course , Including from the foundation of python Script to web Development 、 Reptiles 、 Data analysis 、 Data visualization 、 Machine learning, etc . Those who want these materials can pay attention to Xiaobian , And in the backstage private letter small make up :“07” Can receive .

版权声明
本文为[The style of ancient Taoism and Confucianism]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210222175941621f.html

  1. Python 空间绘图 - 房价气泡图绘制
  2. Translation: practical Python Programming 02_ 02_ Containers
  3. Research on Portfolio Optimization Based on particle swarm optimization
  4. Ubuntu deploying Django project
  5. Two years of Java, write Python and go without byte beating
  6. Translation: practical Python Programming 02_ 02_ Containers
  7. So learn python, grandfather learned! Introduction to super simple Python
  8. python3 多线程 与 mongo亿级消费日志数据 新鲜demo 【优化第一版】
  9. Summary of Chinese word segmentation based on Jieba
  10. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  11. Summary of Chinese word segmentation based on Jieba
  12. From movie art to Python code to realize God's reverse thinking mode
  13. Summary of Chinese word segmentation based on Jieba
  14. ARIMA模型预测CO2浓度时间序列-python实现
  15. Python belongs to back-end development or front-end development? Introduction to Python!
  16. python isinstance()
  17. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  18. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  19. 如何用Python一键修改上万个文件名
  20. One day quick start to Python
  21. Python 学习笔记: List
  22. 翻译:《实用的Python编程》02_03_Formatting
  23. Is there any age requirement for learning Python? Is 30 OK?
  24. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  25. Using Python to develop defi project
  26. Detailed explanation of Python function
  27. Python 可变类型作为函数默认参数时的副作用
  28. What do Python engineers do? What's their future?
  29. 这是我见过最好的Python教程:十分钟带你认识Python
  30. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  31. Python - zip() function
  32. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  33. Python类的调用以及私有和公有属性方法的调用
  34. Python类的专有方法
  35. Python基础之:数字字符串和列表
  36. How did Python pioneers evaluate this language on their 30th birthday?
  37. Python基础之:数字字符串和列表
  38. Python基础之:数字字符串和列表
  39. 窥探未来不是梦,python数据分析轻松实现
  40. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  41. 阿里、华为Python工程师总结的实用技巧,只有你还没看?
  42. 酸了!看到抖音上Python程序员晒得工资条......
  43. Python基础之:数字字符串和列表
  44. Importing excel into database adaptively by Python
  45. Python安装教程
  46. Python安装教程
  47. From Xiaobai to master, here is a guide to pandas
  48. [Python] drawing method of stem leaf diagram and compound pie diagram
  49. Drawing of Python geoplot spatial kernel density estimation map
  50. Python Seaborn economist's classic chart imitation
  51. Python space drawing - regionmask mask operation example
  52. Python space drawing - cartopy longitude and latitude add
  53. Python pykrige package Kriging interpolation calculation and visual rendering
  54. Python batch resampling, mask, slope extraction
  55. Python - Analysis of reachable circle of multiple traffic modes
  56. Python space drawing bubble drawing
  57. Python 3 multithreading and Mongo 100 million consumption log data fresh demo
  58. ARIMA model for predicting time series of CO2 concentration
  59. python isinstance()
  60. How to modify tens of thousands of file names with one key in Python