日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python简单笔记

發(fā)布時(shí)間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python简单笔记 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 和 {變量} 也可在變量中使用

>>> a = "hello" >>> b = f"我后面將會(huì)顯示a的值 {a} " >>> print(b) 我后面將會(huì)顯示a的值 hello

換行輸出

實(shí)例:

>>> print("ABC\nDEF") ABC DEF

不換行輸出

格式:end=‘’
實(shí)例:

a = "ABC" b = "DEF" print(a,end='') print(b)

執(zhí)行結(jié)果:

ABCDEF

變量

變量

格式:變量名稱 = values

實(shí)例:

one = 1 two = 2 three = one + two print(three)

輸出結(jié)果:

3

全局變量

全局可使用
你可以這樣寫:

var = 520 def fun():var = 1314print(var, end='')fun() print(var)

執(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).

實(shí)例1: >>> a = "one" >>> b = "two" >>> print("{1}比{0}大".format(a,b)) #{}中取第一個(gè)值位置參數(shù)就是0第二個(gè)就是1以此類推...,不標(biāo)記位置參數(shù)默認(rèn)0->開始 two比one大 實(shí)例2: formatter = "{} {} {} {}" formatter1 = 1 formatter2 = 2 formatter3 = 3 forma = 4 print(formatter.format(1, 2, 3, 4)) print(formatter.format("one", "two", "three", "four")) print(formatter.format(True, False, False, True)) print(formatter.format(formatter1,formatter2,formatter3,forma)) print(formatter.format("Try your","Own text here","Maybe a poem","Or a song about fear"))

執(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í)例:

>>> a = "one" >>> b = "two" >>> print("%s比%s大" %(b,a)) two比one大

更多格式化詳解

接收用戶輸入

格式:變量 = 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)容是:

小a:我是小a 小b:我是小b 小c:我是小c def save_file(z,x):boy = open('D:/a.txt','w')#以寫入的方式打開這個(gè)文件如不存在會(huì)自動(dòng)添加girl = open('D:/b.txt','w')boy.writelines(z)#將z收到的結(jié)果寫入boygirl.writelines(x)#將x收到的結(jié)果寫入girlboy.close()#寫完記得關(guān)閉這個(gè)文件girl.close()#寫完關(guān)閉里面就有了 def set_up(chuanru): #<--入?yún)⒖赼 = open('d:/shiyan.txt')z = []x = []for i in a:(one,two) = i.split(':',1)# 1代表分割1次if one == '小a':z.append(two)#將two的結(jié)果添加到zif one == '小b':x.append(two)#將two的結(jié)果添加到xsave_file(z,x)#在關(guān)閉文件前調(diào)用傳參給sava_filea.close() #要養(yǎng)成用完關(guān)閉的習(xí)慣set_up('d:/shiyan.txt')#調(diào)用傳參給set_up,括號(hào)中可以隨便傳這里面沒用到 ##上面這句為調(diào)用函數(shù)代碼,入?yún)⒖?

執(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。