【NOTE】Python 3(一)基础知识
Python 种类
- CPython
- IronPython
- JPython
- RubyPython
- pypy ...
安装 Python3
1)查看当前系统 Python 版本
$ python --version Python 2.7.5
2)查看 Python 位置
$ which python /usr/bin/python
3)查看 python 链接位置,python 指向默认的 python2.7
$ cd /usr/bin $ ls python* -al lrwxrwxrwx. 1 root root 7 7月 1 16:59 python -> python2 lrwxrwxrwx. 1 root root 9 7月 1 16:59 python2 -> python2.7 -rwxr-xr-x. 1 root root 7216 4月 11 15:36 python2.7 -rwxr-xr-x. 1 root root 1835 4月 11 15:36 python2.7-config lrwxrwxrwx. 1 root root 16 7月 1 18:45 python2-config -> python2.7-config lrwxrwxrwx. 1 root root 14 7月 1 18:45 python-config -> python2-config
4)备份 python 文件
$ sudo mv python python.bak
5)解压并安装
$ tar -xvf Python-3.5.5.tgz $ cd Python-3.5.5 $ ./configure --prefix=/usr/local/python/python3 $ make $ sudo make install $ sudo rm -rf /usr/bin/python $ sudo ln -s /usr/local/python/python3/bin/python3 /usr/bin/python $ python -V Python 3.5.5
6)修改 yum 配置文件
由于 yum 使用的 Python2.7,因此修改为旧版本
$ sudo vi /usr/bin/yum
将第一句修改成 #!/usr/bin/python2.7
Python 版 hello world
#!/usr/bin/env python # coding=utf-8 print "hello, word!"
Python文件执行
1)显示的指明 Python 解释器,需要 python 安装好,并且环境变量正确。
$ python hello.py
hello, word!
2)像执行 Shell 脚本一样的执行,程序的第一行说明了使用 Python 解释器来执行 Python 文件。
#!/usr/bin/env python
首先赋予 Python 文件可执行权限:
$ chmod +x hello.py $ ./hello.py hello, word!
指定编码格式
# coding=utf-8
如果不指定,Python 2 就不识别中文,因为 Python 2 解释器在加载 .py 文件中的代码时,会对内容进行编码(默认 ASCII),因此执行会报错。
#!/usr/bin/env python print "hello, word!" print "你好,世界!" #不指定编码格式
Python 2 执行结果:
$ python hello.py File "hello.py", line 3 SyntaxError: Non-ASCII character '\xe4' in file hello.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
有时候看到这样的指定编码格式也是一样的
# -*- coding:utf-8 -*-
当然Python 3 就不用指定编码格式了,因为 Python 3 的默认编码方式为 utf-8 。
注释
1)单行注释——#号
#注释内容
#!/usr/bin/env python # coding=utf-8 # 单行注释
2)多行注释——三个引号
#!/usr/bin/env python # coding=utf-8 """ 多行注释 多行注释 """ ''' 多行注释 多行注释 '''
变量
Python 的变量不需要声明,但是在使用前必须赋值,它就像是 shell 脚本那样定义变量。【变量名 = 值】
命名规则跟 C 语言、C++ 等一样,Python 的关键字不能用来命名。
Python 关键字
'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'
示例
#!/usr/bin/env python # coding=utf-8 name = "shelmean" password = 123 #整型值 print(name) print(password)
输出
$ ./varible.py shelmean 123
字符串
用引号引起来的就是字符串。
#!/usr/bin/env python # coding=utf-8 str = "string" str_ = 'string' _str_ = """string""" _str__ = '''string''' print(str) print(str_) print(_str_) print(_str__)
输出结果
$ ./string.py string string string string
字符串加法和乘法
字符串 + 字符串 就是把字符串连接在一起,字符串 x number 就是把字符串重复出现(number)次。
#!/usr/bin/env python # coding=utf-8 str_a = "string" str_b = "add" str_sum = str_a + str_b print(str_sum) str_mul = str_a * 3 print(str_mul)
执行结果
$ ./string.py stringadd stringstringstring
单引号和双引号引号以及三引号
1. 使用字符串变量时,外围引号需要一致,即最左边如果是单引号,最右边也要是单引号才行。对于字符串,使用三种引号引起来都可以。
2. 当字符串变量中存在单引号时,可以在最外层使用双引号,避免单引号产生歧义;反过来也行,用单引号包含双引号也是可以的。除此之外,也可以使用转义字符来 转义 字符串中的引号。
3. 当变量字符串中单引号和双引号都存在时,可以使用三引号来解决。
示例
#!/usr/bin/env python # coding=utf-8 name = 'shelmean' password = "123" print(name) print(password) str = "I'am a boy!" _str = 'I\'am a boy!' _str_ = '''it's a good idea for the "test"! ''' _str__ = """it's a good idea for the "test"!""" print(str) print(_str) print(_str_) print(_str__)
输出
$ ./varible.py shelmean 123 I'am a boy! I'am a boy! it's a good idea for the "test"! it's a good idea for the "test"!
input() 语句
等待用户输入数据。
Python 2
Python 2 中,用户输入的如果是字符串,需要加引号,不加引号,会把输入的内容当成是变量,如果没有定义该变量就会报错,有就不会。
1)没有定义变量,而输入又没有引号:( 把输入的内容当成变量,把 shelmean 变量的值赋给 name 变量,而 shelmean 变量没有定义 )
#!/usr/bin/env python
# coding=utf-8
name = input("请输入名字:")                            
print(name)
执行报错:
$ ./login.py 请输入名字:shelmean Traceback (most recent call last): File "./login.py", line 3, in <module> name = input("请输入名字:") File "<string>", line 1, in <module> NameError: name 'shelmean' is not defined
带引号输入:( 相当于给 name 变量赋值为 “shelmean” )
$ ./login.py 请输入名字:"shelmean" shelmean
2)定义变量,输入变量:( 把 user 变量的值赋值给 name,所以 name 的值为 “shelmean” )
#!/usr/bin/env python
# coding=utf-8
user = "shelmean" #定义user变量
name = input("请输入名字:")
print(name)
执行结果:
$ ./login.py 请输入名字:user shelmean
Python 3
在 Python 3 中,用户输入的是什么,就是什么,当成字符串处理。
1)不定义变量的情况
#!/usr/bin/env python
# coding=utf-8
name = input("请输入名字:")                           
print(name)
执行结果:( 输入的一律作为字符串处理 )
$ ./login.py 请输入名字:shelmean shelmea ------------------------ $ ./login.py 请输入名字:"shelmean" "shelmean"
2)定义变量的情况
#!/usr/bin/env python
# coding=utf-8
user = "shelmean"      #定义user变量                                
name = input("请输入名字:")
print(name) 
执行结果:( 即便是输入的变量,也当做字符串处理 )
$ ./login.py 请输入名字:user user
raw_input() 语句
在 Python 2 中,该语句可以实现和在 Python 3 中 input() 语句一样的效果,输入什么就是什么,作为字符串处理。
#!/usr/bin/env python
# coding=utf-8
#user = "shelmean"
#name = input("请输入名字:")
name = raw_input("请输入名字:")                        
print(name)
执行结果:( Python 2 中使用 raw_input() 语句输入时,不带引号输入不会被当做变量处理了 )
$ ./login.py 请输入名字:shelmean shelmean

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号