從0到1入門(mén)Python(筆記1)

Preparation before class

< First intro book of python for rookies >


Fundimental

tips:

1 文件開(kāi)頭加上 #coding:utf-8

2 function()基本可以無(wú)限嵌套

  • exp
a=3 
b=int(str(int(str(a))))
print(type(b))

run: <class 'int'>

3 Python區(qū)分大小寫(xiě)

一、變量

例:answer=42
answer = 42
標(biāo)識(shí)符 賦值符
var賦值時(shí),= 后可為int,int可進(jìn)行四則運(yùn)算;可為string,string可相加,表示鏈接多個(gè)string,可與int相乘,表示復(fù)制,復(fù)制次數(shù)=int,可套用函數(shù)。
  • exp
a=1/5+7*2-10
b='2'*2
c=int(b)
d='de'+' '+'amor'
code result
print(a) 4.199999999999999
print(b) 22
print(c) 22
print(d) de amor

二、print()

print()內(nèi)可進(jìn)行四則運(yùn)算,可套用函數(shù)運(yùn)算。
  • exp
code result
print(a+c) 26.2
print(a-c) -17.8
print(a*c) 92.39999999999998
print(a/c) 0.1909090909090909
print(type(c)) <class 'int'>

三、String

單引號(hào)和雙引號(hào)公用是一樣的,三個(gè)引號(hào)被用于過(guò)長(zhǎng)段的文字或者說(shuō)明,只要三引號(hào)不玩就可以隨意換行寫(xiě)下文字。
string可相加,表示鏈接多個(gè)string,可與int相乘,表示復(fù)制,復(fù)制次數(shù)=int,可套用函數(shù)

1、string分片索引,string[ ]
  • rules

    lyric = "Isn't she lovely"

    I s n ' t s h e l o v e l y
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
  • exp

    lyric="Isn't she lovely"
    
    code result
    print(lyric[2]) n
    print(lyric[-3]) e
    print(lyric[0:10]) #the 10th is excluded,截取第0~第9個(gè)字符 Isn't she
    print(lyric[-4:-1]) #the -1st is excluded vel
    print(lyric[-1:-4] 空,說(shuō)明無(wú)法R-->L倒排索引到字母
    print(lyric[-3:]) #From -3 to the end ely
    print(lyric[:7]) #From start to 7 Isn't s
  • 實(shí)際應(yīng)用場(chǎng)景

    url = 'http://ww1.site.cn/14d2e8ejw1exjogbxdxhj20ci0kuwex.jpg'
    file_name = url[-10:]
    print(file_name)
    
    run result: 0kuwex.jpg
    
    phone_number1 = '13862861236'
    hiding_number1 = phone_number1.replace(phone_number1[3:7],'*' *4)
    print(hiding_number1)
    
    run result: 138****1236
    
2、字符串的方法

Python的對(duì)象擁有各種功能、特性,專(zhuān)業(yè)術(shù)語(yǔ)稱(chēng)之為——方法(method)
假設(shè)對(duì)象為 computer,computer可以計(jì)算。則語(yǔ)法可表示為 computer.calculate()

3、字符串格式化符

對(duì)于需要填空的情況,可用.format()進(jìn)行批量處理。

  • exp
print('Thomas get {} runs in the {} inning'.format('2','8'))
print('Thomas get {run_number} runs in the {inning_number} inning'.format(run_number=3+1,inning_number='9+1'))
print('Thomas get {1} runs in the {0} inning'.format('2','8'))

run result:
Thomas get 2 runs in the 8 inning
Thomas get 4 runs in the 9+1 inning
Thomas get 8 runs in the 2 inning

變量需在.format()內(nèi)賦值,否則報(bào)錯(cuò),例子如下

run_n=1
inning_n=1
print(run_n)
print('Thomas get {run_n} runs in the {inning_n} inning'.format(run_n,inning_n))

run result:
1
Traceback (most recent call last):
  File "D:/python_lab/test.py", line 5, in <module>
    print('Thomas get {run_n} runs in the {inning_n} inning'.format(run_n,inning_n))
KeyError: 'run_n'






<center>? 疑問(wèn)</center>

下面的代碼為何可以先對(duì)變量賦值,然后可以帶入,還需要學(xué)習(xí)

city = input("write down the name of city:")
url = "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city)

此段代碼可以填補(bǔ)空缺中的城市數(shù)據(jù)






四、函數(shù)

Python3.50 版本有68個(gè)內(nèi)建函數(shù)(build-in function)。
內(nèi)建函數(shù):就是python自帶函數(shù)
python官網(wǎng)中對(duì)各函數(shù)的介紹:https://docs.python.org/3/library/functions.html

  • 創(chuàng)建函數(shù)
    • 常見(jiàn)語(yǔ)法單詞

      1. def(即define)含義是創(chuàng)建/定義函數(shù) ←關(guān)鍵字
      2. arg(即argument,參數(shù))
      3. return(即返回結(jié)果) ←關(guān)鍵字

      Syntax 語(yǔ)法

      def function (arg1, arg2):
          return 'something'
      

      語(yǔ)法口訣

      Define a function named 'function' which has two arguments: arg1 and arg2, returns the result ---'something'

      注意!

      • 符號(hào)只能使用英文符號(hào)
      • 需要縮進(jìn)的地方一定要縮進(jìn),縮進(jìn)是為了表明語(yǔ)句和邏輯的從屬關(guān)系,是python中最顯著的特征之一。
  • exp
    def fahrenheit_converter(C):#定義Fahrenheit_converter函數(shù),函數(shù)參數(shù)為 C
         fahrenheit = C*9/5+32 #定義變量fahrenheit與C的轉(zhuǎn)換關(guān)系
         return str(fahrenheit)+'°F' #返回string化的Fahrenheit
    C2F =fahrenheit_converter(35) #定義變量C2F等于Fahrenheit_converter,參數(shù)值等于35
    print(C2F) #打印C2F變量
     
      ---
    run result:
    95.0°F
    
  • Practice
    def hypotenuse_calculator(a,b):
        h=pow(pow(a,2)+pow(b,2),0.5)
        return str(h)
    
    long_side=hypotenuse_calculator(3,4)
    print('the longest side equals to '+long_side)
    
    ---
    run result:
    the longest side equals to 5.0
    

五、傳遞函數(shù)與參數(shù)類(lèi)型

傳遞參數(shù)的方式有兩種:a.位置參數(shù) positional argument b.關(guān)鍵詞參數(shù) keyword argument

  • 位置參數(shù):按照參數(shù)的位置傳入?yún)?shù)值的方式叫位置參數(shù)。

    def trapezoid_area(base_up, base_down, height):
        return 1/2*(base_up + base_down)*height
        
    trapezoid_area(1,2,3)
    
  • 關(guān)鍵詞參數(shù):直接帶上關(guān)鍵詞賦值,關(guān)鍵詞的順序可以隨意排列。

    def trapezoid_area(base_up, base_down, height):
        return 1/2*(base_up + base_down)*height
        
    trapezoid_area(base_up=1,base_down=2,height=3)
    

    注意: 位置參數(shù)和關(guān)鍵詞參數(shù)可混用,但關(guān)鍵詞方式不能放在前部,且必須按照定義參數(shù)時(shí),參數(shù)的排列順序來(lái)。

  • exp
trapezoid_area(1,2,height=3)     #正確
trapezoid_area(base_up=1,base_down=2,3)    #錯(cuò)誤
trapezoid_area(height=3,base_down=2,1)    #錯(cuò)誤

?P48-50有疑問(wèn)

六、設(shè)計(jì)自己的函數(shù)

  • exp

    def text_create(name,msg): #定義函數(shù)的名稱(chēng)和參數(shù);
        desktop_path='c://users/lokoR/Desktop/' #open打開(kāi)新建文件的路徑
        full_path=desktop_path+name+'.txt' #傳入的參數(shù)加上桌面路徑再加上后綴就是完整的文件路徑
        file=open(full_path,'w') #打開(kāi)文件,'w'代表作為寫(xiě)入模式,意思是:如果沒(méi)有就在該路徑創(chuàng)建,如果有則覆蓋
        file.write(msg) #寫(xiě)入傳入的參數(shù)msg
        file.close() #關(guān)閉文本
        print('Done') #顯示 Done
     text_create('hello','hello world') #給參數(shù)賦值,name=hello,msg=hello world
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容