Python3学习笔记

发表于 2020-06-02
阅读 201

介绍

介绍

大名鼎鼎的脚本语言 Python,如今不会 py 大概不能被称作程序员了吧

所以呢,我们今天还是来恶补一下吧

本文包括 Windows 平台的 Python 环境安装方法和 Linux 平台的 Python 环境安装方法

教程

Windows安装

下载

官网下载页面

https://www.python.org/downloads/source/

我们选择的是这个版本

https://www.python.org/ftp/python/3.8.3/python-3.8.3-embed-amd64.zip

解压缩

下载后将 python-3.8.3-embed-amd64.zip 压缩包解压到一个文件夹里

我们选择的文件夹路径是 D:\TFHome\python-3.8.3

环境变量

我们需要将 Python 的文件夹路径设置到系统环境变量的 Path 里面

2cb4014e5af4ba4b.jpg

查看版本

windows版本只有一个 python.exe

C:\Users\tongfu>python --version
Python 3.8.3

安装setuptools & pip

Python3 下按照原来的方式去安装 setuptools、pip 都行不通了,因为它会提示你没有 setuptools 模块(我有 setuptools 我还装它?),这明显是一个 bug

解决方法1:

打开官方的安装介绍

https://packaging.python.org/tutorials/installing-packages/

找到 get-pip.py 链接

右键这个链接“另存为”这个脚本文件到本地

通过 python 执行这个脚本文件

如果一切顺利的话,你会发现 setuptools 和 pip 都安装好了

解决方法2:

如果上面的脚本不好用,可以换这个试试

打开官方的安装说明

https://pip.pypa.io/en/stable/installing/

找到这行,这本来是 Linux 的命令,但是我们可以通过复制网址到浏览器的方式下载 get-pip.py 脚本文件

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

通过 python 执行这个脚本文件

如果一切顺利的话,你会发现 setuptools 和 pip 都安装好了

其他

如果还不行?这个......我也没有办法了!

Linux安装

准备

进入 packages

mkdir /packages
cd /packages

依赖库

yum -y install python-devel libffi-devel

下载

官网下载页面

https://www.python.org/downloads/source/

我们选择的是这个版本

https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz

下载 Python3

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz

编译安装

tar -xzvf Python-3.8.3.tgz
cd Python-3.8.3
./configure --prefix=/tongfu.net/env/python-3.8.3/
make
make install
cd ../

建立软连接

ln -s /tongfu.net/env/python-3.8.3/bin/python /usr/bin/
ln -s /tongfu.net/env/python-3.8.3/bin/python3 /usr/bin/
ln -s /tongfu.net/env/python-3.8.3/bin/pip3 /usr/bin/

查看版本

包括Python2和Python3两个版本

[root@tongfunet]# python --version
Python 2.7.5
[root@tongfunet]# python3 --version
Python 3.8.3

安装setuptools

通过setup.py安装模块的时候需要这个模块支持

官方下载页面

https://pypi.org/project/setuptools/#files

wget https://files.pythonhosted.org/packages/25/bf/a37e89d3148221fedd4def62bb68749041d79f3840d58a7943f81a6f6c6e/setuptools-47.1.1.zip
unzip setuptools-47.1.1.zip
cd setuptools-47.1.1
python3 setup.py build
python3 setup.py install
cd ../

安装pip

Python3 的 Linux 版本自带一个 pip3,我们可以直接使用,就不用自己安装了

使用

Windows 和 Linux

Python 是跨平台的脚本语言,脚本语言的特征是

Windows

通过扩展名映射方式绑定,通俗说法就是,右键 *.py 文件点击“打开方式”,选择 python.exe 即可

Linux

通过首行定义的解析器路径,通俗说法就是,类似:#!/usr/bin/python 这样的写法

所有其他的都是一样的,我们只要学会了一项,就可以在两个平台运行

helloworld

编辑脚本文件 test.py,内容如下

#!/usr/bin/python3

print 'Hello World'

授权脚本权限

chmod +x test.py

运行脚本

[root@tongfunet]# ./test.py
Hello World

变量赋值

通过 = 对变量进行赋值

sfc = "hello world"

funRet = callFuntion()

if 条件语句

格式

条件判断语句放一行,最后以“:”结束,语句主题缩进4个空格

单条件判断语句

if str == '':
    print ("str is empty")

多条件与关系判断语句

if str is None and str == '':
    print ("str is undefined")

多条件或关系判断语句

if str == 'hello' or str == 'world':
    print (str)

多条件混合判断语句

if str != '' and ( str == 'hello' or str == 'kitty' ):
    print (str)

逻辑条件选择

我们来一个多个条件选择的语法

if str == 'apple':
    act = "eat"
elif str == 'bear':
    act = "drink"
elif str == 'car':
    act = "drive"
else:
    act = "none"

函数

有返回值的函数

def getSum(num1, num2):
    sum = hum1 + num2
    return num

无返回值的函数

def printResult(res):
    print (res)

对象

注意事项

  • 我们通过 class 定义对象

  • 实例化的时候不需要 new,这点和其他语音差别很大

  • 对象没有可见属性,这点也很奇葩

  • 对象所有方法的第一个参数是对象自己,且必须定义,这个很无语

定义对象

class MyClass:
    def __init__(self, id, name):
        self.id = id
        self.name = name

    def Fun1(self, str):
        str = "{" + str + "}"
        return str

    def Fun2(self):
        print ("[" + str(self.id) + "] " + self.name)

调用对象

myClass = MyClass(123, "demo")
myClass.Fun1("hello")
myClass.Fun2()
exit()

try catch finally

使用类似 java 的 try{} catch{} 语句去捕获意外错误,避免程序中断运行

try:
    subprocess.check_output("cmd_not_exists",shell=True)
except subprocess.CalledProcessError as e:
    print (e)
finally:
    print ("unable to call cmd_not_exists")


鬼谷子叔叔

跟福哥学编程吧~~
日志
212
浏览
1626