qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          python學習之基本語法

          學到這里已經很不耐煩了,前面的數據結構什么的看起來都挺好,但還是沒法用它們做什么實際的事。

           

          基本語句的更多用法


           

          使用逗號輸出

          >>> print 'age:',25 age: 25

          如果想要同時輸出文本和變量值,卻又不希望使用字符串格式化的話,那這個特性就非常有用了:

          >>> name = 'chongshi' >>> salutation = 'Mr' >>> greeting = 'Hello.' >>> print greeting,salutation,name Hello. Mr chongshi

           

           

          模塊導入函數

          從模塊導入函數的時候,可以使用

          import somemodule

          或者

          form somemodule immport  somefunction

          或者

          from somemodule import somefunction.anotherfunction.yetanotherfunction

          或者

          from somemodule import *  

          最后一個版本只有確定自己想要從給定的模塊導入所有功能進。

          如果兩個模塊都有open函數,可以像下面這樣使用函數:

          module.open(...)

          module.open(...)

          當然還有別的選擇:可以在語句末尾增加一個as子句,在該子句后給出名字。

          >>> import math as foobar   #為整個模塊提供別名 >>> foobar.sqrt(4) 2.0 >>> from math import sqrt as foobar  #為函數提供別名 >>> foobar(4) 2.0

           

          賦值語句

          序列解包

          >>> x,y,z = 1,2,3 >>> print x,y,z 1 2 3 >>> x,y=y,x >>> print x,y,z 2 1 3

          可以獲取或刪除字典中任意的鍵-值對,可以使用popitem

          >>> scoundrel ={'name':'robin','girlfriend':'marion'} >>> key,value = scoundrel.popitem() >>> key 'name' >>> value 'robin'

          鏈式賦值

          鏈式賦值是將同一個值賦給多個變量的捷徑。

          >>> x = y = 42 # 同下效果: >>> y = 42 >>> x = y >>> x 42

          增理賦值

          >>> x = 2 >>> x += 1  #(x=x+1) >>> x *= 2  #(x=x*2) >>> x 6

           

           

          控制語句


           if 語句:

          復制代碼
          name = raw_input('what is your name?') if name.endswith('chongshi'):     print 'hello.mr.chongshi' #輸入 >>>  what is your name?chongshi  #這里輸入錯誤將沒有任何結果,因為程序不健壯 #輸出 hello.mr.chongshi
          復制代碼

           

          else子句

          復制代碼
          name = raw_input('what is your name?') if name.endswith('chongshi'):     print 'hello.mr.chongshi' else:   print 'hello,strager' #輸入 >>>  what is your name?hh  #這里輸和錯誤 #輸出 hello,strager
          復制代碼

           

          elif 子句

          它是“else if”的簡寫

          復制代碼
          num = input('enter a numer:') if num > 0:     print 'the numer is positive' elif num < 0:     print 'the number is negative' else:   print 'the nuber is zero' #輸入 >>>  enter a numer:-1 #輸出 the number is negative
          復制代碼

           

          嵌套

          下面看一下if嵌套的例子(python是以縮進表示換行的)

          復制代碼
          name = raw_input('what is your name?') if name.endswith('zhangsan'):     if name.startswith('mr.'):         print 'hello.mr.zhangsan'     elif name.startswith('mrs.'):         print 'hello.mrs.zhangsan'     else:         print 'hello.zhangsan' else:     print 'hello.stranger'
          復制代碼

            如果輸入的是“mr.zhangsan”輸出第一個print的內容;輸入mrs.zhangshan,輸出第二個print的內容;如果輸入“zhangsan,輸出第三個print的內容;如果輸入的是別的什么名,則輸出的將是最后一個結果(hello.stranger

           

          斷言

          如果需要確保程序中的某個條件一定為真才能讓程序正常工作的話,assert 語句可以在程序中設置檢查點。

          復制代碼
          >>> age = 10 >>> assert 0 < age < 100 >>> age = -1 >>> assert 0 < age < 100 , 'the age must be realistic'  Traceback (most recent call last):   File "<pyshell#8>", line 1, in <module>     assert 0 < age < 100 , 'the age must be realistic' AssertionError: the age must be realistic
          復制代碼

           

           

          循環語句


           打印1100的數(while循環)

          復制代碼
          x= 1 while x <= 100:     print x   x += 1 #輸出 1 2 3 4 . . 100
          復制代碼

          再看下面的例子(while循環),用一循環保證用戶名字的輸入:

          復制代碼
          name = '' while not name:     name = raw_input('please enter your name:') print 'hello.%s!' %name #輸入 >>>  please enter your name:huhu #輸出 hello.huhu!
          復制代碼

          打印1100的數(for 循環)

          復制代碼
          for number in range(1,101):   print number #輸出 1 2 3 4 . . 100
          復制代碼

          是不是比while 循環更簡潔,但根據我們以往學習其它語言的經驗,while的例子更容易理解。

           

          一個簡單for 語句就能循環字典的所有鍵:

          復制代碼
          d = {'x':1,'y':2,'z':3} for key in d:   print key,'corresponds to',d[key] #輸出 >>>  y corresponds to 2 x corresponds to 1 z corresponds to 3
          復制代碼

           

          break語句

          break 用來結束循環,假設找100以內最大平方數,那么程序可以從100往下迭代到0,步長為-1

          復制代碼
          from math import sqrt for n in range(99,0,-1):     root = sqrt(n)     if root == int(root):         print n         break #輸出 >>>  81
          復制代碼

           

          continue 語句

          continue結束當前的迭代,“跳”到下一輪循環執行。

          復制代碼
          while True:     s=raw_input('enter something:')     if s == 'quit':         break     if len(s) < 3:         continue   print 'Input is of sufficient length' #輸入 >>>  enter something:huzhiheng  #輸入長度大于3,提示信息 Input is of sufficient length enter something:ha        #輸入長度小于3,要求重輸 enter something:hah       #輸入長度等于3,提示信息 Input is of sufficient length enter something:quit       #輸入內容等于quit,結果
          復制代碼

           

           

          posted on 2014-02-10 11:27 順其自然EVO 閱讀(143) 評論(0)  編輯  收藏 所屬分類: python

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 望奎县| 谷城县| 固镇县| 迁安市| 兴安盟| 开化县| 泊头市| 商都县| 和静县| 镇雄县| 文化| 桑植县| 白水县| 榆树市| 建湖县| 石首市| 绵阳市| 仲巴县| 改则县| 云南省| 枣阳市| 陆丰市| 阳春市| 六安市| 阿拉善右旗| 贵阳市| 江津市| 襄城县| 武功县| 六安市| 澄城县| 新沂市| 绥阳县| 平顺县| 屯昌县| 台中县| 虹口区| 泉州市| 鲁山县| 海南省| 东乡族自治县|