python简单笔记
Remarks:python中注意縮進(jìn)(Tab鍵或者4個(gè)空格)
print(輸出)
格式:print(values)
字符串、數(shù)字、變量等都可以輸出: 實(shí)例: print(1)->1 print(1+1)->2 a = "hello" print(a)->hello print(f"a的值是{a}")->a的值是hello多行輸出:
print("""aaaaaaaaaaaa aaaaaaaaa aaaaaaaaaa""")結(jié)果:
aaaaaaaaaaaa aaaaaaaaa aaaaaaaaaa說明括號(hào)中 f 和 {變量} 配合可提取字符串中的變量,同print("a的值是",a)效果一樣
f 和 {變量} 也可在變量中使用
換行輸出
實(shí)例:
>>> print("ABC\nDEF") ABC DEF不換行輸出
格式:end=‘’
實(shí)例:
執(zhí)行結(jié)果:
ABCDEF變量
變量
格式:變量名稱 = values
實(shí)例:
one = 1 two = 2 three = one + two print(three)輸出結(jié)果:
3全局變量
全局可使用
你可以這樣寫:
執(zhí)行結(jié)果:
1314520也可以這樣寫使用 global 關(guān)鍵字:
def fun():global varvar = 1314print(var, end='')fun() print(var)執(zhí)行結(jié)果:
13141314一般多用在函數(shù)內(nèi),聲明變量的作用域?yàn)槿肿饔糜颉?/p>
下面是一個(gè)錯(cuò)誤的示例:
def fun():var = 1314print(var, end='')fun() print(var) # 這一步就會(huì)報(bào)錯(cuò)因?yàn)関ar為函數(shù)中的局部變量,外面根本沒用var這個(gè)變量注意: 盡量不要使用全局變量,會(huì)導(dǎo)致代碼可讀性變差,代碼安全性降低
格式化
format
格式 {位置0}{位置1}.format(參數(shù)a,參數(shù)b)
注意:format前面有個(gè)點(diǎn).
執(zhí)行結(jié)果:
1 2 3 4 one two three four True False False True 1 2 3 4 Try your Own text here Maybe a poem Or a song about fear%d、%s、%f
%d:有符號(hào)整數(shù)(十進(jìn)制)
%s :字符串形式
%f:小數(shù)
實(shí)例:
更多格式化詳解
接收用戶輸入
格式:變量 = input()
實(shí)例1:
print("How old are you?", end=' ') age = input() print("How tall are you?", end=' ') height = input() print("How much do you weigh?", end=' ') weight = input() print(f"So, you're {age} old, {height} tall and {weight} heavy")結(jié)果:
How old are you? 18 How tall are you? 180 How much do you weigh? 100 So, you're 18 old, 180 tall and 100 heavy實(shí)例2:
print("請(qǐng)輸入你的年齡:",end='') a = int(input()) #執(zhí)行到這會(huì)等待用戶輸入 print(f"你的輸入的年齡是{a}")結(jié)果:
請(qǐng)輸入你的年齡:18 #執(zhí)行到這會(huì)等待用戶輸入 你的輸入的年齡是18實(shí)例3:
age = int(input("How old are you? ")) height = input("How tall are you? ") weight = input("How much do you weigh? ") print(f"So, you're {age} old, {height} tall and {weight} heavy")結(jié)果:
How old are you? 18 How tall are you? 180 How much do you weigh? 50 So, you're 18 old, 180 tall and 50 heavy模塊導(dǎo)入
from sys import argv #argv獲取當(dāng)前腳本路徑 # read the WYSS section for how to run this print(argv) script = argv print("The script is called:", script)執(zhí)行結(jié)果:
['D:/xuexi/python練習(xí).py'] The script is called: ['D:/xuexi/python練習(xí).py']讀取文件
格式:open()
實(shí)例:
shiyan.txt 的內(nèi)容是:
執(zhí)行結(jié)果:
如果沒有 a.txt 和 a.txt 會(huì)自動(dòng)在結(jié)果路徑中創(chuàng)建
a.txt --> 小a:我是小a
b.txt --> 小b:我是小b
文件打開方式:
模式 可做操作 若文件不存在 是否覆蓋 r 只能讀 報(bào)錯(cuò) - r+ 可讀可寫 報(bào)錯(cuò) 是 w 只能寫 創(chuàng)建 是 w+ 可讀可寫 創(chuàng)建 是 a 只能寫 創(chuàng)建 否,追加寫 a+ 可讀可寫 創(chuàng)建 否,追加寫函數(shù)
格式:
def functionname():
一個(gè)簡(jiǎn)單的函數(shù)
def test():print("This is one function")a = 1b = 2print(a + b)test() #調(diào)用函數(shù)結(jié)果:
This is one function 3可傳參的函數(shù)
def test(a,b):print("This is one function")print(a + b)test(1,2) #調(diào)用函數(shù)結(jié)果:
This is one function 3帶默認(rèn)值的
def test(a,b=2):print("This is one function")print(a + b)test(1) #調(diào)用函數(shù)結(jié)果:
This is one function 3設(shè)置默認(rèn)值后也可以傳新值:
def test(a,b=2):print(f"This is one function")print(a + b)test(1,3) #調(diào)用函數(shù)結(jié)果:
This is one function 4注意: 默認(rèn)參數(shù)只能在非默認(rèn)參數(shù)之后(下面將演示一段錯(cuò)誤的代碼):
def function(a,b=1,c,d=2): #這樣寫是錯(cuò)誤的,因?yàn)榉悄J(rèn)參數(shù)c不應(yīng)該出現(xiàn)在b之后,應(yīng)該在b之前簡(jiǎn)單命令,未完結(jié)
轉(zhuǎn)載于:https://www.cnblogs.com/weibgg/p/10787078.html
總結(jié)
以上是生活随笔為你收集整理的python简单笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SG函数
- 下一篇: 西刺代理python_python爬取西