標籤:執行 動態 比較 exe == imp pen 檔案的 類型
一. 變數:
解釋:將程式運算的中間結果這哪是儲存起來,以便後續程式調用。
1, 變數必須要有數字,字母,底線,任意組合。
2, 變數不能數字開頭。
3, 不能是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‘]
4,變數要具有可描述性。
5,變數不建議使用中文。
6,變數不能過長。
7,官方推薦:駝峰體 或 底線樣式
例如:駝峰體: AgeOfOldboy = 56
NumberOfStudents = 80
底線: age_of_oldboy = 56
number_of_students = 80
8, 變數要用‘ ‘," ",‘‘‘ ‘‘‘ # 三引號用於換行的字串:
9, 單雙引號配合使用
1 msg = "My name is Alex , I‘m 73 years old!"2 print(msg)
View Code
10,字串的使用
使用者互動input: input出來的資料都是字串類型。
1 s1 = ‘123‘2 s2 = ‘asd‘3 print(s1 + s2)
View Code
1 s3 = ‘中國‘2 print(s3*8)
View Code
二 . 常量:
解釋:一直不變的量,例如:π 社會安全號碼
使用:將變數全部大寫>>>常量。放在檔案的最上面。
三. 注釋:
解釋:協助你解釋說明,注釋不宜多,宜精。
1, 單行注釋:#
2,多行注釋:‘‘‘ 被注釋內容 ‘‘‘ """ 被注釋內容 """
四.bool值
print(1>2) # 結果為false
五.格式化輸出
代碼格式:
‘我叫%s,今年%d‘ % (‘文博‘, 18)
注意: s :字串 d:整數 f:浮點型
例如:
方法一
1 name = input(‘請輸入姓名:‘) 2 age = input(‘請輸入年齡:‘) 3 job = input(‘請輸入工作:‘) 4 hobby = input(‘請輸入愛好:‘) 5 6 msg = """ 7 ------------ info of %s ----------- 8 Name : %s 9 Age : %d10 job : %s11 Hobby: %s12 ------------- end -----------------13 """ % (name, name, int(age), job, hobby)14 print(msg)
View Code
方法二
1 dic = {"name": ‘alex‘, ‘age‘: 18, ‘job‘: ‘IT‘, ‘hobby‘: ‘女‘} 2 3 msg = """ 4 ------------ info of %(name)s ----------- 5 Name : %(name)s 6 Age : %(age)d 7 job : %(job)s 8 Hobby: %(hobby)s 9 ------------- end -----------------10 """ % dic11 print(msg)View Code
方法三(簡化版)
1 name = input(‘請輸入姓名:‘)2 age = input(‘請輸入年齡:‘)3 job = input(‘請輸入工作:‘)4 hobby = input(‘請輸入愛好:‘)5 6 msg = ‘Name: %s, Age: %d, job: %s, Hobby: %s‘ % (name, int(age), job, hobby)7 print(msg)
View Code
注意:在格式化輸出中只想單純的表示% ,寫成 %%
例如
1 msg = ‘我叫%s,今年%d,學習進度5%%‘ % (‘文博‘, 18)2 print(msg)
View Code
總結:如果你想製作一個字串類的模板,或者是想讓字串某些位置變成動態輸入,此時你想到用字串拼接,格式化輸出。
五. 運算
以下假設變數:a=10,b=20
1, 算術運算: + - * / % **
例如
1 a = 102 b = 33 print(a % b) # 取餘,結果14 print(2**2) # 乘以,結果45 print(a // b)# 除以,結果取整,結果為3
View Code
2, 比較運算: == != > < >= <=
以下假設變數:a=10,b=20
3, 賦值運算: += -= * / % **
以下假設變數:a=10,b=20
4, 邏輯運算:not and or
優先順序:() not and or :同一優先順序從左之後依次計算
注意: x or y , x為真,值就是x,x為假,值是y;
x and y, x為真,值是y,x為假,值是x。
1 x or y2 if x is True:3 return x4 else:5 # 此為說明,該代碼不能執行
View Code
1 x and y2 if x is True3 return y4 else:5 return x6 # 此為說明,該代碼不能執行
View Code
1 print(3 > 2 and 3 < 4 or 5 > 6 and 2 < 5)2 # 先比較3>2 and 3<4,即true and false,結果為true,3 # 再比較5 > 6 and 2 < 5,即false and true,結果為false4 # 再比較true or false, 結果為 true5 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)6 print(True or False)
View Code
5,成員運算:判斷某些元素在不在一個序列中 str,tuple, list, dict, set
6,
Python運算子優先順序
以下表格列出了從最高到最低優先順序的所有運算子:
5, int bool之間的轉換:
int ---> bool:非零即True
bool ---> int :True 1, False 0
變數/常量/格式化輸出/運算