重新梳理Python基礎(3)

來源:互聯網
上載者:User

1. python輸入raw_input()函數。

a = raw_input() #得到的是字串a = int(raw_input()) #如果想得到int,只能這樣

如果輸入特殊字元,比如\t \n等,會和輸入一樣輸出,%r會輸出\\t、\\n,%s會輸出\t、\n

View Code

print "How old are you?"age = raw_input()print "How tall are you?"height = raw_input()print "How much do you weight?"weight = raw_input()print "So, you are %r old, %r tall and %r heavy." % (age, height, weight)print "So, you are %s old, %s tall and %s heavy." % (age, height, weight)print "So, you are %d old, %d tall and %d heavy." % (int(age), int(height), int(weight))

輸出

View Code

How old are you?\tHow tall are you?\nHow much do you weight?111So, you are '\\t' old, '\\n' tall and '111' heavy.So, you are \t old, \n tall and 111 heavy.

通常都是這麼寫的

View Code

age = raw_input("How old are you?")height = raw_input("How tall are you?")weight = raw_input("How much are you weight?")print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

input和raw_input的區別

當輸入為純數字時

  • input返回的是數實值型別,如int,float
  • raw_inpout返回的是字串類型,string類型

輸入字串為運算式

  input會計算在字串中的數字運算式,而raw_input不會。

  如輸入 “57 + 3”:

    •   input會得到整數60
    •   raw_input會得到字串”57 + 3”

其實

def input(prompt):    return (eval(raw_input(prompt)))

2. 在windows中查看doc,可以用如下命令

 python -m pydoc raw_input

3. 運行python指令碼的時候輸入參數,該如何寫。

看代碼

from sys import argvscript, first, second, third = argvprint "The script is called: ", scriptprint "Your first variable is: ", firstprint "Your second variable is: ", secondprint "Your third variable is: ", third

argv類似於java中的main(String[] args)。但是argv需要指定參數個數,並且運行指令碼的時候參數個數要對應,不然會報錯。並且第一個參數是python ex13.py 1 1 1中的ex13.py。

如果參數個數不對,輸出如下:

View Code

E:\SkyDrive\python\the hard way to learn python>python ex13.py 1 2 3 4Traceback (most recent call last):  File "ex13.py", line 2, in <module>    script, first, second, third = argvValueError: too many values to unpack

4. 讀取檔案並列印

在指令碼中讀取檔案,最好把檔案名稱作為指令碼的參數,以下是代碼

from sys import argvscript, filename = argvtxt = open(filename)print txt.read()
txt.close()file_name_again = raw_input("the filename you want to print \n > ")print open(file_name_again).read()

輸出

View Code

E:\SkyDrive\python\the hard way to learn python>python ex15.py ex15_sample.txtThis is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.the filename you want to print > ex15_sample.txtThis is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.