So learn python, grandfather learned! Introduction to super simple Python

Li Xuepeng 2021-02-22 13:27:54
learn python grandfather learned introduction


Install and run the interactive interpreter

In the vast majority linux and UNIX System installation ( Include Mac OS X),Python The interpreter already exists . We can type... At the prompt python Command to verify ( The author's environment ubuntu)

fnngj@fnngj-H24X:~$ python Python 2.7.3 (default, Sep 26 2012, 21:53:58) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 

Ok ! If you type in python When the above information appears in the command , Indicates that we have installed python Environmental Science .

If not installed , stay ubuntu /Debin linux , Then you can use the following command to install Python:

 $ apt-get install python

If it is window Installation below , as well as linux Download the source package installation , I believe this kind of person's problem is easy to find , I won't explain it in detail here . I believe it's better than the first configuration java The environment is much simpler .


 Learn this way Python, Grandfather has learned ! Super simple Python introduction


Type in the command , See if it works .

>>> print "hello,world!" hello,world!

Be careful : If you want to launch an interactive interpreter , Sure ctrl+D

What is the algorithm ?

Before serious programming , Let's talk about computer programming first , In short , Computer programming is to tell the computer what to do . Computers can do a lot of things , But it doesn't think for itself , We need to tell it the details , You need to tell it the algorithm in a language that the computer can understand .“ Algorithm ” But is “ Program ” perhaps “ The recipe ” Another pleasant way of saying ---- Describe in detail how to do sth .

Tofu with shallot 、 tofu 、

First , Take some tofu ;( Declare a variable )

Then add shallots to the tofu .( Add two variables )

If you need special spicy tofu .( Consider specific conditions )

Cooked --- Every time 10 Check every minute .( Repeat this command )

Recipes and algorithms contain some elements ( object 、 goods ), And instructions ( sentence ).“ Shallot ” and “ tofu ” It's the elements . The instructions include “ Add shallots to tofu ” etc. .


 Learn this way Python, Grandfather has learned ! Super simple Python introduction


Numbers and expressions :

Interactive python The interpreter can be used as a powerful calculator

>>> 1-2 -1 >>> 1+2 3 >>> 32784*13 426192 >>> 1/2 0

What circumstance ?1 except 2 It's not supposed to be equal to 0.5 Well ? How is 0 Well ? “/” Used for rounding , It's not divisible enough , Must be 0 了 , Try floating point numbers

>>> 7/2 3>>> 1.0/2.0 0.5

try “%” Remainder

>>> 6 % 3 0 >>> 6 / 3 2 >>> 6 % 3 0 >>> 7 / 3 2 >>> 7 % 3 1 >>> 13 % 9 4>>> 0.75 % 0.5 0.25

See, Division (/) And surplus (%) It's different .

6 / 3 It happened to be 2 to be divisible by , So the remainder (%) yes 0 , 13 / 9 I can only bring 1 individual 9 , The remainder is 4 .

Now try again “ ** ” Power operation ( chengfang ) operator

>>> 2*2*2 8 >>> 2**3 8 >>> 2**6 64 >>> -3**2 -9 >>> (-3)**2 9

2 Of 3 The power can be a power symbol (**) Express , It's quite convenient .

The ratio of the power sign is negated ( Unary minus operator ) High priority , therefore -3**2 Equate to -(3**2), If you want to calculate (-3)**2 , You need to explicitly state .

long integer

python Can handle very large numbers

>>> 100000000000000000000000 100000000000000000000000L >>> 100000000000000000000000L 100000000000000000000000L

Ordinary integers in 2 147 483 647 to -2 147 483 647 Between , Excess can be represented by long shaping (L) , We can also transform integers into integers by ourselves ( Add... At the end of the input “L”)

Variable

This is no different from other programming languages , If you've never been exposed to a program , I believe it's easy for you to understand .

>>> x = 3 >>> x*2 6>>> x = 5 >>> x * 2 1


hold 3 Assign a value to x , that x The current value of the variable is 3, x multiply 2 The result is 6 , I have put the 5 Assigned to x , Then with 2 The result of multiplication must be 10 了 .

Define variable rules : Cannot start with a number , It can contain letters 、 Numbers and underscores .


 Learn this way Python, Grandfather has learned ! Super simple Python introduction


sentence

In fact, we've talked about the sentence at the beginning print It's a printout statement .

>>> 2*2 4 >>> print 2*2 4

Although the result is the same , The expression above , Here's the sentence .

function

I've talked about using the power operator (**) To calculate the power . in fact , You can use functions instead of operators , The function is pow :

>>> 2**38>>> pow(2,3)8

Functions are like small programs that can be used to perform specific functions .python Many of our functions do wonderful things . We can also customize functions . therefore , We usually put pow The function provided by the system is called Built-in functions .

>>> 10+pow(2, 3*5)/3.010932.666666666666>>> abs(-10)10>>> 18/131>>> round(18.4/13.7)1.0


We can use function calls and operators to create more complex expressions ; abs() Function can get the absolute value of several people ,round The function rounds the floating-point number to the nearest integer value .

modular

Think of modules as importing into python To enhance the expansion of its functions . You need to use a special person's command import To import modules .

>>> import math>>> math.floor(32.9)32.0>>> int(math.floor(32.9))32

floor The function is round . because floor() Function belongs to math modular , So import the module before using it (import math), And then according to ( modular . function ) The format uses the functions of this module .

If you want to put the 32.9 Convert to integer (32) Not floating-point numbers (32.0), have access to int function .

When making sure you don't import multiple functions with the same name ( Import from different modules ) Under the circumstances , You may not want to call a function every time , Write the name of the module . that , have access to import Another form of command :

>>> from math import sqrt>>> sqrt(9)3.0>>> sqrt(8)2.8284271247461903

sqrt() Function is used to calculate the square root of a number .



 Learn this way Python, Grandfather has learned ! Super simple Python introduction


A complete set of systematic Py thon Study the tutorial .

From the most basic to the framework to the actual project learning materials have been sorted out

Follow and reply to “01” You can get it for free

版权声明
本文为[Li Xuepeng]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210221025238192t.html

  1. Python中的解决中文字符编码的问题
  2. Solving the problem of Chinese character coding in Python
  3. Translation: practical Python Programming 02_ 01_ Datatypes
  4. Installation and use of Python and tensorflow in win10 environment (Python version 3.6, tensorflow version 1.6)
  5. Python series 46
  6. Linux安装Python3
  7. 【python接口自动化】- 正则用例参数化
  8. Python RestFul Api 设计
  9. filecmp --- 文件及目录的比较│Python标准库
  10. Installing python3 on Linux
  11. [Python] Matplotlib 圖表的繪製和美化技巧
  12. (資料科學學習手札108)Python+Dash快速web應用開發——靜態部件篇(上)
  13. 翻譯:《實用的Python程式設計》02_01_Datatypes
  14. 【python接口自动化】- 正则用例参数化
  15. 翻译:《实用的Python编程》02_02_Containers
  16. 两年Java,去字节跳动写Python和Go
  17. [Python interface automation] - regular use case parameterization
  18. Python restful API design
  19. 翻译:《实用的Python编程》02_02_Containers
  20. 两年Java,去字节跳动写Python和Go
  21. 翻译:《实用的Python编程》02_02_Containers
  22. Python基于粒子群优化的投资组合优化研究
  23. ubuntu部署django项目
  24. 兩年Java,去位元組跳動寫Python和Go
  25. 翻譯:《實用的Python程式設計》02_02_Containers
  26. 这样学习Python,爷爷都学会了!超简单Python入门
  27. [Python] 基于 jieba 的中文分词总结
  28. 【python】递归听了N次也没印象,读完这篇你就懂了
  29. [Python] 基于 jieba 的中文分词总结
  30. 人理解迭代,神则体会递归,从电影艺术到Python代码实现神的逆向思维模式
  31. [Python] 基於 jieba 的中文分詞總結
  32. Python属于后端开发还是前端开发?Python入门!
  33. 【python】递归听了N次也没印象,读完这篇你就懂了
  34. 一天快速入门python
  35. 学习Python对年龄有没有要求?30岁可以吗?
  36. 清华教授!12小时整理的最全Python教程(文末无偿分享)
  37. 使用Python开发DeFi项目
  38. python 函数详解
  39. Python工程师是做什么的?前景如何?
  40. Filecmp -- comparison of files and directories
  41. Python - zip() 函数
  42. 30 周年生日,Python 先驱是怎么评价这门语言的?
  43. Drawing and beautifying skills of [Python] Matplotlib chart
  44. Python + dash rapid web application development static components
  45. Translation: practical Python Programming 02_ 01_ Datatypes
  46. python将excel自适应导入数据库
  47. 从小白到大师,这里有一份Pandas入门指南
  48. [Python] 茎叶图和复合饼图的画法
  49. [Python interface automation] - regular use case parameterization
  50. Translation: practical Python Programming 02_ 02_ Containers
  51. Two years of Java, to write Python and go
  52. Translation: practical Python Programming 02_ 02_ Containers
  53. Two years of Java, to write Python and go
  54. Python-geoplot 空间核密度估计图绘制
  55. Python-seaborn 经济学人经典图表仿制
  56. python空间绘图- regionmask掩膜操作示例
  57. Python 空间绘图 - Cartopy 经纬度添加
  58. Python-pykrige包-克里金(Kriging)插值计算及可视化绘制
  59. Python 批量重采样、掩膜、坡度提取
  60. python - 多种交通方式可达圈分析