python的變量和簡(jiǎn)單數(shù)字類型詳解
1. 變量
- 每個(gè)變量都存儲(chǔ)了一個(gè)值
- 在程序中可以隨時(shí)修改變量,但Python將始終記錄變量的最新值
message = "Hello Huang ZB!" print(message) message = "Goodbye Huang ZB!" print(message)
1.1 使用變量名時(shí)避免命名錯(cuò)誤
查看Traceback明白錯(cuò)誤
message = "Hello Huang ZB!" print(mesage)
2.字符串
Def:字符串就是一串字符。雙引號(hào)、單引號(hào)都可表示
2.1 修改字符串大小寫的方法
name = "huang zhibin" print(name.title()) #title()函數(shù)作用:將每個(gè)單詞首字母改為大寫
Huang Zhibin
其他方法:
name = "huang zhibin" print(name.title()) #title()函數(shù)作用:將每個(gè)單詞首字母改為大寫 print(name.upper()) #upper()函數(shù)作用:將字符串內(nèi)容全部轉(zhuǎn)換為大寫 print(name.lower()) #lower()函數(shù)作用:將字符串內(nèi)容全部轉(zhuǎn)換為小寫
Huang Zhibin
HUANG ZHIBIN
huang zhibin
2.2 合并字符串
方法:拼接
first_name = 'huang' last_name = 'zhibin' full_name = first_name + ' ' + last_name print('Hello, ' + full_name.title() + '!') #這個(gè) + 不可或缺
Hello, Huang Zhibin!
2.3 使用制表符或換行符來(lái)添加空白
- 在字符串中添加制表符,使用 \t (也可以理解為進(jìn)位符)
print("python") print("\tpython") # \t 表示制表符
python
python
在字符串中添加換行符,使用 \n
print("Languages:\nPython\nC\nJavaScript") # \n 表示換行符
Languages:
Python
C
JavaScript
同一字符串中可以同時(shí)包含制表符和換行符 字符串" \n\t ": 讓python換到下一行
print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
2.4 刪除空白
- python能夠找出字符串開頭和末尾多余的空白,為確保開末尾無(wú)空白,使用方法 rstrip()
- 為確保開開頭無(wú)空白,使用方法 lstrip()
- 同時(shí)剔除字符串兩端的空白,使用方法 strip()
information = ' 人生苦短,我學(xué)python ' print(information.rstrip()) print(information.lstrip()) print(information.strip())
人生苦短,我學(xué)python
人生苦短,我學(xué)python #右邊空格依然存在!
人生苦短,我學(xué)python
2.5 使用字符串時(shí)需要避免語(yǔ)法錯(cuò)誤
再修改程序時(shí)語(yǔ)法錯(cuò)誤也是一個(gè)重要的檢查指標(biāo)
3. 數(shù)字類型
3.1 整數(shù)
>>> 2+3 5 >>> 5-6 -1 >>> 4*5 20 >>> 36/6 6.0 >>> 3**2 9 >>> 2+2**2 6 >>> (2+2)*2 8
3.2 浮點(diǎn)數(shù)
>>> 0.2+0.3 0.5 >>> 0.2-0.3 -0.09999999999999998
保留兩位小數(shù)
print ('{:.2}'.format(變量))
3.3 復(fù)數(shù)
>>> 2+6j (2+6j) >>> (2+6j).real 2.0 >>> (2+6j).imag 6.0
3.4 使用函數(shù)str()避免類型錯(cuò)誤
age = 21 message = "Happy " + str(age) + "rd Birthday!" #將非字符串值轉(zhuǎn)化為字符串 print(message)
Happy 21rd Birthday!
4 .注釋
單行注釋
#
多行注釋
‘''
注釋不能嵌套?。。。。?#8203;
5 .python之禪
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- Python?數(shù)據(jù)類型中的字符串和數(shù)字
- 一起來(lái)學(xué)習(xí)一下python的數(shù)字類型
- Python的內(nèi)置數(shù)據(jù)類型中的數(shù)字
- 使用Python+OpenCV進(jìn)行卡類型及16位卡號(hào)數(shù)字的OCR功能
- python中的十大%占位符對(duì)應(yīng)的格式化的使用方法
- python連接數(shù)據(jù)庫(kù)后通過(guò)占位符添加數(shù)據(jù)
- python常見的占位符總結(jié)及用法
- python切片作為占位符使用實(shí)例講解
- python數(shù)字類型和占位符詳情
相關(guān)文章
python中sort sorted reverse reversed函數(shù)的區(qū)別說(shuō)明
這篇文章主要介紹了python中sort sorted reverse reversed函數(shù)的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05python虛擬機(jī)之描述器實(shí)現(xiàn)原理與源碼分析
在本篇文章當(dāng)中主要給大家介紹描述器背后的實(shí)現(xiàn)原理,通過(guò)分析?cpython對(duì)應(yīng)的源代碼了解與描述器相關(guān)的字節(jié)碼的指令,我們就可以真正了解到描述器背后的原理,需要的朋友可以參考下2023-05-05python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法
這篇文章主要介紹了python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02計(jì)算pytorch標(biāo)準(zhǔn)化(Normalize)所需要數(shù)據(jù)集的均值和方差實(shí)例
今天小編就為大家分享一篇計(jì)算pytorch標(biāo)準(zhǔn)化(Normalize)所需要數(shù)據(jù)集的均值和方差實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01