python基礎&條件陳述式

來源:互聯網
上載者:User

標籤:尾碼名   關鍵字   time   inpu   int   break   一個   用法   nal   

基礎:  1.第一句python的用法
  • python對尾碼名不限制,可以是任意的
  • 匯入模組時,如果不是.py檔案(以後檔案尾碼名是py)
  2.input的用法:
  • 永遠等待,直到使用者輸入了值。就會將輸入的值賦值給一個東西
  3.變數名:
  • - 字母
  • - 數字
  • - 底線

ps:數字不能開頭;不能是關鍵字;最好不要和python內建的東西重複***

關鍵字:[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

 例如:

1 a1 = "歡迎你的到來!"    #變數2 print(a1)3 ===========4 print("歡迎你的到來")
  4.條件陳述式
  • if 條件 (代碼塊在條件陳述式後一個層級)
1 if 條件:2     print("Ok")3 else:4     print("Error")
  •  if 嵌套
1 if 1=1:2     if 2=2:3         print("hello")4         print("hello2")5     else:6         print("error")7 else:8     print("error2")
  • if  elif 多條件判斷
 1 inp = input(‘請輸入你的會員層級:‘) 2 if inp == "進階會員": 3     print(‘歡迎進階會員‘) 4 elif inp == "鉑金會員": 5     print(‘歡迎鉑金會員‘) 6 elif inp == "鑽石會員": 7     print(‘歡迎鑽石會員‘) 8 else: 9     print(‘歡迎普通會員‘)10 print(‘歡迎光臨本店......‘)

知識點:if基本語句  else

補充:pass 代指空代碼,無意義;僅僅用於表示代碼塊。如果python不想輸出任何東西,寫pass直接跳過不輸出任何語句。

  5.基礎資料型別 (Elementary Data Type)
  •  字串(引號):
1 name = "alex"2 name = ‘alex‘3 name = """alex"""4 name = ‘‘‘alex‘‘‘

字串使用方法:

 1 加法: 2 n1 = ‘alex‘ 3 n2 = ‘tom‘ 4 n3 = n1+n2 5 print(n3)    #輸出效果alextom 6  7 乘法: 8 n1 = ‘alex‘ 9 n2 = n1*1010 print(n2)   #輸出效果alexalexalexalexalexalexalexalexalexalex
  • 數字:
 1 eg: 2 age = 19 3  4 加、減、乘、除、次方、取餘: 5 a1 = 10 6 a2 = 20 7 a3 = a1 + a2 8 a3 = a1 - a2 9 a3 = a1 * a210 a3 = 100 / 1011 a3 = 3**4       #擷取3的4次方12 a3 = 39 % 8   #擷取39除以8得到的餘數13 a3 = 39 // 8   #擷取39除以8得到的商14 15 判斷奇數、偶數;16 a = 1517 temp = a % 218 if temp == 0:19     print(‘偶數‘)20 else:21     print(‘奇數‘)
  6.迴圈
  • 死迴圈:
1 import time2 while 1 == 1:3     print(‘ok‘,time.time())
1 import time2 count = 03 while count < 10:4     print(count)5     #count = 1006     count = count + 17 print(123)

 ps:  

1.while ....else 迴圈   

2.continue  終止當前迴圈,開始下一次迴圈; break 終止所以迴圈

 

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.