Python學習(一)

來源:互聯網
上載者:User

標籤:常量   迴圈語句   tin   ==   +=   字母   字串   info   多行   

一、Python中的變數定義

與JAVA類似

1、由字母數字底線構成

2、首字母不能為數字

3、不用關鍵字

二、常量

全部由大寫字母構成,不更改它,實際上可更改

三、注釋

單行注釋:#

多行注釋:三個單引號或三個雙引號

四、資料類型

bool:True,False

int:+、-、*、/、%、**

str:單引號或雙引號包裹、兩者實際沒有特別區別,在使用上如果語句中使用了單引號,則外層使用單引號,或反之,例如:

print("I‘m a student")

字串中的+和*

+在字串中為拼接

例如:

print("拼接"+"成功")

*為該字串拼接次數

例如:

print("拼接"*8)
五、使用者互動

使用input,由input獲得的輸入類型均為str,用法:

input(“提示的資訊")

例如:

input("輸入資訊")
六、判斷語句if

1、單層判斷if

  if 條件:

    結果

if True    print("True")

 

2、雙層判斷if-else

  if 條件;

    結果

  else:

    結果

if False:    print("True")else:    print("False")

3、多層判斷if-elif-else,PS:最後的else可加可不加

  if 條件:

    結果

  elif 條件:

    結果

  else:

    結果

例如:

age = int(input("輸入數字"))if age>5:    print("a>5")elif age==5:    print("a=5")elif age<5:    print("a<5")

4、if可多層嵌套

例如:

if True:    if False:        print("這是嵌套")
七、迴圈語句1、while迴圈語句

 while 條件:

  結果

例如;

while True:    print("永真迴圈")

  while 條件:

    結果

  else:

    結果

當完整的執行完while中的條件後,會執行else語句,沒有完整執行完,或中途跳出,不會執行else語句

例如:

count = 0
while count<=3:
i = int(input("輸入數字"))
if i==5:
print("中途跳出")
break
print(count)
count += 1
else:
print("完整執行完")

 

2、for迴圈語句

  a、for 自訂參數 in "字串":

    自訂參數每次取一個字元

  例如:

for letter in "Andraw":    print("letter為:",letter)

  結果:

  b、for 自訂參數 in 字串集

    自訂參數每次取一個字串

  例如:

  

msg = [‘apple‘,‘banana‘,‘mango‘]for fruit in msg:    print("fruit為:",fruit)

  結果:

 

  c、for 自訂參數 in range(數值n)

    自訂參數從0開始取,直到n-1

  例如:

for i in range(5):    print(i)

  結果:

  d、for-else語句,與while-else類似

 

3、break和cotinue使用

break:跳出當前迴圈

continue:放棄此次迴圈,跳到迴圈判斷位置

4、pass的使用

pass:什麼都不執行,一般用於佔位

 

Python學習(一)

聯繫我們

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