### 獲得更多資料歡迎進入[我的網站](http://rlovep.com/)或者 [csdn](http://blog.csdn.net/peace1213)或者[博客園](http://www.cnblogs.com/onepeace/)
> 本節主要介紹print,import和input,t函數,包括他們在python2.7和python3 的區別以及用法。下面附有之前的文章;
# python3的print函數的變化
python3之前的print是簡單的語句比如要打印hello world
~~~
>>> print 'hello world'
hello world
>>>
~~~
而python3之后的版本中print已經變為了函數。比如要打印必須加上();如下:
~~~
#直接按語句打印會出現錯誤:
peace@peace:~$ python
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
File "<stdin>", line 1
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'
#應該用函數打印
>>> print ('hello world')
hello world
>>>
~~~
# print函數的功能
**注意(python3之前的print語句功能基本沒有變)**
### 使用逗號輸出:
使用print時,也可以在語句中添加多個表達式,每個表達式用逗 號分隔;在用逗號分隔輸出時,print語句會在每個輸出項后面自動添加一 個空格;
注意:不管時字符串還是其他類型都是轉化為字符串進行打印
~~~
>>> print('peace',22)
peace 22
>>> print(1,2,3)
1 2 3
#輸出元祖必須這樣輸出;
>>> print((1,2,3))
(1, 2, 3)
>>>
#變量也是可以的
>>> name='peace'
>>> print(name,22)
peace 22
>>>
#可以使用‘+’連接字符串
>>> print('hello'+','+'peace')
hello,peace
>>>
#如果在結尾加上逗號,name接下來的語句會與前一行、打印在一行;(python3之前才有效)
#建立douhao.py在里面輸入
print 'peace',
print 22
#在輸入如下語句即可:
peace@peace:~/workspace/python$ python2.7 douhao.py
peace 22
~~~
# import函數
### 導入格式
將整個模塊導入,格式為:import somemodule;
從某個模塊中導入某個函數,格式為:from somemodule import somefunction;
從某個模塊中導入多個函數,格式為:from somemodule import firstfunc, secondfunc, thirdfunc
將某個模塊中的全部函數導入,格式為:from somemodule import *
### 兩個模塊同名函數解決辦法
1導入模塊使用模塊名進行調用;
~~~
#導入模塊
import module1
import module2
#調用同名函數的方法
module1.open()
module2.open()
~~~
2使用關鍵字as進行改名
~~~
#導入函數,并給函數取相應的別名
from module1 import open as open1
from module2 import open as open2
~~~
3例子
~~~
>>> from cmath import cos as s
>>> from math import cos as c
>>> s(30)
(0.15425144988758405+0j)
>>> c(30)
0.15425144988758405
>>>
~~~
# input函數
Python3中用input()取代了raw_input(),當然這僅僅是重命名,使用上并沒有不同;python3之前的input()不再取用;
input()函數不管你輸入什么返回的是字符串;
~~~
#與python3之前的raw_input()相同
>>> k=input('intput int ')
intput int 12
>>> k
'12'
>>>
~~~
#### 相關鏈接:
[python3入門之類](http://rlovep.com/2015/09/23/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E7%B1%BB/)
[python3入門之函數](http://rlovep.com/2015/09/06/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%87%BD%E6%95%B0/)
[python3入門之循環](http://rlovep.com/2015/09/06/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%BE%AA%E7%8E%AF/)
[python3之if語句](http://rlovep.com/2015/08/05/python3%E4%B9%8Bif%E8%AF%AD%E5%8F%A5/)
[python3入門之賦值語句介紹](http://rlovep.com/2015/08/03/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E8%B5%8B%E5%80%BC%E8%AF%AD%E5%8F%A5%E4%BB%8B%E7%BB%8D/)
[python3入門之print,import,input介紹](http://rlovep.com/2015/08/03/python3%E5%85%A5%E9%97%A8%E4%B9%8Bprint%EF%BC%8Cimport%EF%BC%8Cinput%E4%BB%8B%E7%BB%8D/)
[python3入門之set](http://www.cnblogs.com/onepeace/p/4791578.html)
[python3入門之字典](http://rlovep.com/2015/07/29/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%AD%97%E5%85%B8/)
[python3入門之字符串](http://rlovep.com/2015/07/28/python%E5%85%A5%E9%97%A8%E4%B9%8B%E5%AD%97%E7%AC%A6%E4%B8%B2/)
[python3入門之列表和元組](http://rlovep.com/2015/07/14/python%E5%85%A5%E9%97%A8%E4%B9%8B%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84/)
[python3入門之軟件安裝](http://rlovep.com/2015/07/14/python%E5%85%A5%E9%97%A8%E4%B9%8B%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85/)
[python3爬蟲之入門和正則表達式](http://rlovep.com/2015/09/23/python3%E7%88%AC%E8%99%AB%E4%B9%8B%E5%85%A5%E9%97%A8%E5%92%8C%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/)