Python 基本文法 學習之路(三),python基本文法

來源:互聯網
上載者:User

Python 基本文法 學習之路(三),python基本文法

定義變數

在Python中,定義一個變數是很簡單的。而且,在Python中,定義是不需要用分號結尾的。例如:

a = 10b = 3print(a*b)

 

 

判斷語句

Pyhon的if判斷語句是由if、elif、else組成,每個判斷都是由:結尾。Python的判斷也是非常簡單的。

# coding=utf-8fenshu = 90if fenshu >= 90 :    print("成績優秀")elif fenshu >= 75 :    print("優秀")elif fenshu >=60 :    print("及格")else :    print("不及格")
# coding=utf-8 是指明pycharm的編譯語言為utf-8
每個print都要縮排

迴圈

 

# coding=utf-8for i in range(0,100) :    print (i)

在python中,是不支援直接的字串拼接數值的。如果想要拼接,則需要用到.format的索引,在{}裡從0開始,例如:

# coding=utf-8for i in range(0,100) :    print ("序號 {0},{1}".format(i,"Hello,Python"))

 

定義函數

在python中定義函數,用def來定義。可定義有參數的,也可以定義無參數的。

# coding=utf-8def a() :    print("Hello,Python")def b(c,d):    if c>d :        return c    if c<d :        return da()print(b(10,11))

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.