创始人Guido·van·Rossum,1989年为了打发无聊的圣诞节而编写的一个编程语言。python
Python与C
C语言:代码编译获得机器码,机器码在处理器上直接执行。
Python:为解析后生成字节码,而后再解析为机器码,因此比C语言慢。Python是C开发的。linux
Python和其余语言比较
使用:Linux原装Python。
速度:Python在速度上可能稍显逊色。编程
Cpython:Python的官方版本,使用C语言实现,使用最为普遍,CPython实现会将源文件(py文件)转换成字节码文件(pyc文件),而后运行在Python虚拟机上。
Jyhton:Python的Java实现,Jython会将Python代码动态编译成Java字节码,而后在JVM上运行。
IronPython:Python的C#实现,IronPython将Python代码编译成C#字节码,而后在CLR上运行。(与Jython相似)
PyPy(特殊):Python实现的Python,将Python的字节码字节码再编译成机器码。ubuntu
其余:RubyPython、Brython ...框架
简化语法运维
1 Old: print "The answer is", 2*2 New: print("The answer is", 2*2) 2 Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline 3 Old: print # Prints a newline 4 New: print() # You must call the function! 5 Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr) 6 Old: print (x, y) # prints repr((x, y)) 7 New: print((x, y)) # Not the same as print(x, y)!
1 #!/usr/bin/env python #指定哪一个程序来运行,须要有执行权限
2 #-*-coding:utf-8-*- #万国码,UTF08
3 print("Hello World!")
安装过程:省略。编程语言
PyCharm是一种Python IDE,带有一整套能够帮助用户在使用Python语言开发时提升其效率的工具,好比调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制。此外,该IDE提供了一些高级功能,以用于支持Django框架下的专业Web开发。工具
默认Python版本2.6.6单元测试
PS:安装以前须要安装readline-devel包,解决python命令行下方向键显示^[[C^[[D的问题。测试
1 yum groupinstall 'Development Tools' 2 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel 3 yum install easy_install readline-devel #安装此包可解决python命令行内没法使用键盘方向键的问题 4 wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz 5 tar -xf Python-3.5.1.tgz 6 cd Python-3.5.1 7 ./configure --prefix=/usr/local/python3.5.1 8 make && make install 9 ln -s /usr/local/python3.5.1/bin/python3 /usr/bin/python3 [root@Python ~]# python3 Python 3.5.1 (default, May 10 2016, 15:36:08) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
hello.py
1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 4 print("Hello World!")
输出结果
[root@Python ~]# python3 hello.py Hello World!
全部引号内的内容都被认为字符串
1 >>> name = "Dalong" 2 >>> name2 = name 3 >>> print(name,name2) 4 Dalong Dalong 5 >>> name = "Jack" 6 >>> print(name,name2) 7 Jack Dalong 8 >>>
练习1:输入用户密码,并打印结果 getpass模块
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 import getpass 5 username = input("username:") 6 password = getpass.getpass("password:") 7 print(username,password)
执行结果
[root@Python ~]# python3 pass.py username:dalong password: dalong 123456
练习2:if 判断用户名密码是否正确
#!/usr/bin/env python #_*_coding:utf-8_*_ user = 'Dalong' passwd = 'Dalong123' username = input("username:") password = input("password:") if user == username and passwd == password: print("Welcome login") else: print("invalid username or password")
执行结果
[root@Python ~]# python if_v2.py username:dalong password:123 invalid username or password [root@Python ~]# python if_v2.py username:long password:dalong123 invalid username or password [root@Python ~]# python if_v2.py username:dalong password:dalong123 Welcome login
练习3:猜数字
猜错3次提示是否继续
#!/usr/bin/env python #_*_coding:utf-8_*_ age = 22 counter = 0 for i in range(10): if counter <3: guess_num = int(input("Input your guess num:")) if guess_num == age : print("Congratulations!You got it.") break #不继续运行,跳出整个循环 elif guess_num >age: print("Think Smaller!") else: print("Think Big!") else: continue_confirm = input("Do you want Y:") if continue_confirm == 'y': counter = 0 continue else: print("bye") break counter += 1 #counter = counter + 1
执行结果
Input your guess num:11 Think Big! Input your guess num:33 Think Smaller! Input your guess num:55 Think Smaller! Do you want Y:y Input your guess num:11 Think Big! Input your guess num:33 Think Smaller! Input your guess num:22 Congratulations!You got it.
Tab命令补全
>>> import sys #加载sys模块 >>> print(sys.path) #查看pthong环境变量路径 ['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages'] >>> exit() ubuntu@ubuntu:~$ cat /usr/lib/python3.5/distutils/tab.py #新建tab.py文件 #!/usr/bin/env python # python startup file import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter >>> in #再次运行输入命令时,按tab键结果 in input( int(