首次写博客,勿喷!以后无特殊声明都是在python3.5下操作。
1.python2和python3最基础的差异
1.print
python2和python3在新手看来最大的差别无异乎是print了,我分别演示下:
liukai@bogon:~$ python -V
Python 2.7.10
liukai@bogon:~$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a="haha"
>>> print a
haha
>>> print(a)
haha
===========================================
liukai@bogon:~$ python3 -V
Python 3.5.1
liukai@bogon:~$ python3
Python 3.5.1 (default, Jan 8 2016, 23:14:28)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a="haha"
>>> print(a)
haha
>>> print a
File "<stdin>", line 1
print a
^
SyntaxError: Missing parentheses in call to 'print'
总结:python3只支持print(),python2都支持。
2.input
root@lktest:~# python -V
Python 2.7.11
root@lktest:~# python
Python 2.7.11 (default, Jan 20 2016, 11:58:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=raw_input("input your number: ")
input your number: hello
>>> a
'hello'
>>> b=input("input your number: ")
input your number: hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
================================================
root@lktest:~# python3
Python 3.5.1 (default, Jan 3 2016, 03:35:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=raw_input("input your number: ")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> a=input("input your number: ")
input your number: hello
>>> a
'hello'
总结:用户输入函数,python2用raw_input(),python3用input()
2.python基础知识
1.数据类型
1.整型
root@lktest:~# python3
Python 3.5.1 (default, Jan 3 2016, 03:35:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=100
>>> a=-10
>>> a=0
2.浮点型
>>> b=1.23
>>> b=-1.1
>>> b=12.3e8
3.字符串
>>> c="hello"
>>> c="My God"
>>> c="abc\n123"
4.布尔值
>>> d=True
>>> d=False
>>> 6>5
True
>>> 1>4
False
5.空值
空值用None表示,但是None != 0
6.变量
>>> e="hello"
>>> f=10
>>> print(e,f)
hello 10
未完待续...