python 是解释型语言 -- pip (python的包管理工具)

git  分布式版本控制系统

pycharm  集成开发环境

编译和解释的区别是什么?

编译型

        编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件

        这样运行时计算机可以直接以机器语言来运行此程序

解释型

        解释器则是只在执行源程序时, 才一条一条的解释成机器语言给计算机来执行

编译型 vs 解释型

解释型:一边编译,一边执行

        优点:     跨平台兼容性好

                        修改代码比较方便

                        排错容易

        缺点:效率低

编译型:全部编译完成之后再执行

        优点:效率高

        缺点:代码修改需要重新编译

                   跨平台兼容性差

                   排错难


解释器类型

Cpython:默认是Cpython解释器, c语言实现(官方)

IPython:基于CPython之上的一个交互式解释器

Jpython:java实现, Python代码会先转化成Java 字节码

IronPython:c#实现

PyPy:python实现的python解释器 pypy运行效率高


cpython  vs  pypy

pypy:运行效率高  JIT(just in time 即时编译)

cpython: 支持的第三方库多



安装ipython解释器
[root@aliyun ~]# pip3 install ipython
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting ipython
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/53/09/958a4802489d28b2484114ee6414c7502ef57de6f2dbc9095b718640060c/ipython-7.16.3-py3-none-any.whl (783kB)
    100% |████████████████████████████████| 788kB 16.4MB/s 
Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/eb/37/791f1a6edd13c61cac85282368aa68cb0f3f164440fdf60032f2cc6ca34e/prompt_toolkit-3.0.36-py3-none-any.whl (386kB)
    100% |████████████████████████████████| 389kB 17.1MB/s 
Collecting backcall (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/4c/1c/ff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951/backcall-0.2.0-py2.py3-none-any.whl
Collecting pygments (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/0b/42/d9d95cc461f098f204cd20c85642ae40fbff81f74c300341b8d0e0df14e0/Pygments-2.14.0-py3-none-any.whl (1.1MB)
    100% |████████████████████████████████| 1.1MB 34.3MB/s 
Collecting decorator (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/d5/50/83c593b07763e1161326b3b8c6686f0f4b0f24d5526546bee538c89837d6/decorator-5.1.1-py3-none-any.whl
Requirement already satisfied: setuptools>=18.5 in /usr/lib/python3.6/site-packages (from ipython)
Collecting traitlets>=4.2 (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/ca/ab/872a23e29cec3cf2594af7e857f18b687ad21039c1f9b922fac5b9b142d5/traitlets-4.3.3-py2.py3-none-any.whl (75kB)
    100% |████████████████████████████████| 81kB 117.8MB/s 
Collecting pickleshare (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl
Collecting pexpect; sys_platform != "win32" (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/39/7b/88dbb785881c28a102619d46423cb853b46dbccc70d3ac362d99773a78ce/pexpect-4.8.0-py2.py3-none-any.whl (59kB)
    100% |████████████████████████████████| 61kB 108.9MB/s 
Collecting jedi<=0.17.2,>=0.10 (from ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/c3/d4/36136b18daae06ad798966735f6c3fb96869c1be9f8245d2a8f556e40c36/jedi-0.17.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 32.5MB/s 
Collecting wcwidth (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/20/f4/c0584a25144ce20bfcf1aecd041768b8c762c1eb0aa77502a3f0baa83f11/wcwidth-0.2.6-py2.py3-none-any.whl
Collecting ipython-genutils (from traitlets>=4.2->ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none-any.whl
Requirement already satisfied: six in /usr/local/lib/python3.6/site-packages (from traitlets>=4.2->ipython)
Collecting ptyprocess>=0.5 (from pexpect; sys_platform != "win32"->ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/22/a6/858897256d0deac81a172289110f31629fc4cee19b6f01283303e18c8db3/ptyprocess-0.7.0-py2.py3-none-any.whl
Collecting parso<0.8.0,>=0.7.0 (from jedi<=0.17.2,>=0.10->ipython)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/93/d1/e635bdde32890db5aeb2ffbde17e74f68986305a4466b0aa373b861e3f00/parso-0.7.1-py2.py3-none-any.whl (109kB)
    100% |████████████████████████████████| 112kB 43.3MB/s 
Installing collected packages: wcwidth, prompt-toolkit, backcall, pygments, decorator, ipython-genutils, traitlets, pickleshare, ptyprocess, pexpect, parso, jedi, ipython
Successfully installed backcall-0.2.0 decorator-5.1.1 ipython-7.16.3 ipython-genutils-0.2.0 jedi-0.17.2 parso-0.7.1 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.36 ptyprocess-0.7.0 pygments-2.14.0 traitlets-4.3.3 wcwidth-0.2.6

[root@aliyun ~]# ipython
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.3 -- An enhanced Interactive Python. Type '?' for help.

In [1]: print("hello world")
hello world

In [2]:




python 的主流版本:python2.7   python3.6之后

python2 在2020年就不维护了:历史遗留问题



Linux里运行python3



[root@aliyun ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> print("hello \
... world")
hello world
>>> 


>>>  主要提示符
...  次要提示符
\     续行符(当前语句还未结束,和下一行合并)

给ab连续赋值
>>> a = b =1
>>> print(a,b)
1 1
>>> a
1
>>> b
1

注释
    单行注释   以#开头
    多行注释   用三引号包裹

赋值,连续赋值
>>> a = 1
>>> print(a)
1
>>> a = b = 1
>>> print(a,b)
1 1
>>> a
1
>>> b
1

多项赋值
>>> name, age = "sc", 18
>>> name
'sc'
>>> age
18

互换值
>>> a = 5
>>> b = 6
>>> a, b = b, a
>>> a
6
>>> b
5

查看变量a的地址
>>> id(a)
139700463368256

可以使用中文作为变量名(不推荐)
>>> 三创 = 1
>>> 三创
1

python的基本语法

python中使用缩进来区分代码段

1.同一等级的代码,不要使用缩进

2.每行语句的后面可以添加;

3.一行中输入多条语句,使用;隔开

4.续航使用\

标识符命令规范

1.变量名只能是字母,数字或下划线的任意组合

2.变量名的第一个字符不能是数字

3.关键字不能声明为变量名

4.好的标识符:username , age, add等


内建函数:不需要额外的操作,拿来就用,内嵌在解释器内部

print:输出到屏幕
>>> print("ok")
ok

>>> print("a", "b", 7)
a b 7

# 指定分割符
>>> print("a", "b", 8, sep="$")
a$b$8

# 指定追加符
>>> print("a", end="%")
a%>>> print("b")
b

file指定输出到哪里,默认输出到屏幕
还可以输出到文件
以读写的形式打开文件file.txt

f = open("file.txt", 'w+')
print("a", "b", file=f)

id : 查看对象的内存地址
>>> a = 1
>>> id(a)
139700463368096

help :查看帮助信息
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

dir : 查看对象的属性
>>> dir(a)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']


属性的使用:通过变量名.属性名的方式去访问
>>> name = "root"
>>> name.count("o")
2

type :查看对象的类型
>>> type(a)
<class 'int'>
>>> type(name)
<class 'str'>

int :整型,str:字符串类型

input 接受用户从键盘的输入,接受的类型都是字符串
普通字符串需要打引号,变量名不用

>>> username = input("please input your name: ")
please input your name: sc
>>> password = input("please input your password: ")
please input your password: 12
>>> print(username, password)
sc 12

隐藏密码输入(getpass模块)

[root@aliyun python-test]# cat test_pass.py 
import getpass
username = input("please input your name:")
password = getpass.getpass("please input password:")
print("your name is :", username)
print("your password is :", password)
[root@aliyun python-test]# python3 test_pass.py 
please input your name:sc
please input password:
your name is : sc
your password is : 123

python 之禅
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

python编码风格

PEP8规范
1.使用 4 个空格的缩进,不要使用制表符。
2.折行以确保其不会超过 79 个字符。
3. 使用空行分隔函数和类,以及函数内的大块代码。
4. 如果可能,注释独占一行。
5. 使用文档字符串。
6. 运算符周围和逗号后面使用空格,但是括号里侧不加空格: a = f(1, 2) + g(3, 4)。


flake8是一个语法检查工具

安装flake8: pip install flake8

使用 flake8:flake codestyle_test.py

[root@aliyun python-test]# pip3 install flake8
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead
Collecting flake8
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/cf/a0/b881b63a17a59d9d07f5c0cc91a29182c8e8a9aa2bde5
    100% |████████████████████████████████| 71kB 6.3MB/s 
Collecting mccabe<0.8.0,>=0.7.0 (from flake8)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/27/1a/1f68f9ba0c207934b35b86a8ca3aad8395a3d6dd7921c
Collecting pycodestyle<2.10.0,>=2.9.0 (from flake8)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/67/e4/fc77f1039c34b3612c4867b69cbb2b8a4e569720b1f19
    100% |████████████████████████████████| 51kB 103.1MB/s 
Collecting importlib-metadata<4.3,>=1.1.0; python_version < "3.8" (from flake8)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/22/51/52442c59db26637681148c21f8984eed58c9db67053a0
Collecting pyflakes<2.6.0,>=2.5.0 (from flake8)
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/dc/13/63178f59f74e53acc2165aee4b002619a3cfa7eeaeac9
    100% |████████████████████████████████| 71kB 105.2MB/s 
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata<4.3,>
Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in /usr/local/lib/python3.6/site-
Installing collected packages: mccabe, pycodestyle, importlib-metadata, pyflakes, flake8
  Found existing installation: importlib-metadata 4.8.3
    Uninstalling importlib-metadata-4.8.3:
      Successfully uninstalled importlib-metadata-4.8.3
Successfully installed flake8-5.0.4 importlib-metadata-4.2.0 mccabe-0.7.0 pycodestyle-2.9.1 pyflakes-2.5.0
[root@aliyun python-test]# flake8 test_pass.py 
test_pass.py:4:23: E231 missing whitespace after ','
test_pass.py:5:27: E231 missing whitespace after ','
[root@aliyun python-test]# cat test_pass.py 
import getpass
username = input("please input your name:")
password = getpass.getpass("please input password:")
print("your name is :",username)
print("your password is :",password)
[root@aliyun python-test]# vim test_pass.py 
[root@aliyun python-test]# flake8 test_pass.py 
[root@aliyun python-test]#

python的应用领域

1.爬虫

2.大数据(海量的数据) -- 数据分析,数据挖掘 --pandas

3.运维方向      脚本      平台(自动化)  节省时间,成本,减少人为失误

4.web开发 -- 小游戏 -- flask

5.人工智能 -- 机器学习