标签:
创始人Guido·van·Rossum,1989年为了打发无聊的圣诞节而编写的一个编程语言。
Python与C
C语言:代码编译得到机器码,机器码在处理器上直接执行。
Python:为解析后生成字节码,然后再解析为机器码,所以比C语言慢。Python是C开发的。
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的字节码字节码再编译成机器码。
其他: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
1 yum groupinstall ‘Development Tools‘ 2 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel 3 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.
标签:
原文地址:http://www.cnblogs.com/liangdalong/p/5302834.html