马士兵python_马士兵:python学习(一)
python學(xué)習(xí)
一. 輸出函數(shù)print(P6)
1. 輸出數(shù)字和字符串
print(520)
print(52.01)
print("hello world")
print(hello world) #報(bào)錯(cuò)
2. 輸出表達(dá)式:結(jié)果
print(3+2)
3. 輸出到到文件夾的文件(pythonStudy文件夾里的text.txt)
注意:
① a+: 讀寫的方式創(chuàng)建文件。沒(méi)同名文件創(chuàng)建,有的話在文件內(nèi)容后面追加
② file = 必加
fp = open('c:/pythonStudy/text.txt', 'a+')
print("hello wold", file = fp)
fp.close()
4. 不換行輸出
print("hello", "world", "!")
hello world !
二. 轉(zhuǎn)義字符(P7)
1. 定義
\ +轉(zhuǎn)義字符的首字母 轉(zhuǎn)義字符
2. 常見(jiàn)用法
名稱用法反斜杠\\
單引和雙引\’ 和 \"
換行\(zhòng)n
return\r
水平制表符\t
退格\b
print('hello\n world') #換行
hello
_world
#水平制表符
print('hello\tworld') #\t三個(gè)空格,補(bǔ)齊八個(gè)
print('helloooo\tworld')#四個(gè)空格,補(bǔ)齊12個(gè)
hello___world
helloooo____world
#\r
print('hello\rworld') # return 回到開(kāi)頭 然后
print('hello\rwo')
world
wo
#反斜杠
print('http:\\\\www.baidu.com') #要四個(gè)反斜杠,兩個(gè)輸出一個(gè)
http:\\www.baidu.com
3. 原字符(不希望轉(zhuǎn)意字符起作用,在字符串前加r或R)
print(r"hello\nworld")
hello\nworld
三.二進(jìn)制與字符編碼(P8)
八位(8bit)= 1 字節(jié)(byte 或者 B)
漢字在計(jì)算機(jī)里也歸于字符,每個(gè)漢字對(duì)應(yīng)了一個(gè)數(shù)字,數(shù)字可以是十進(jìn)制 十六進(jìn)制 二進(jìn)制等等…但最終在計(jì)算機(jī)里都會(huì)變?yōu)槎M(jìn)制被計(jì)算機(jī)認(rèn)識(shí)
二進(jìn)制0b 八進(jìn)制0o
四. python中的標(biāo)識(shí)符與保留字
name = ('楊不鴿')
print("標(biāo)識(shí)", id(name)) #存放地址
print('value', name)
print('類型', type(name))
標(biāo)識(shí) 2255127658736
value 楊不鴿
類型
2.多次賦值會(huì)指向新的空間(舊空間為空間垃圾)
name = ('楊不鴿')
name = ('楊鴿子了!')
print(name)
楊鴿子了!
五. python中的變量和數(shù)據(jù)類型
1.常用類型
int
float
bool
str
2. 浮點(diǎn)數(shù)存儲(chǔ)不精確
n1= 3.2
n2= 3.1
print(n1 + n2)
6.300000000000001
解決:通過(guò)模塊decimal
from decimal import Decimal
print(Decimal('3.2') + Decimal('3.1'))
6.3
*浮點(diǎn)數(shù)不是所有相加都不準(zhǔn)備,有的是準(zhǔn)確的
3.單引號(hào)和雙引號(hào)只能在一行使用,分行會(huì)報(bào)錯(cuò);三引號(hào)字符串可以分布在連續(xù)的多行
print('''楊不鴿
還是
楊哥''')
楊不鴿
還是
楊哥
4.數(shù)字類型轉(zhuǎn)換(拼接的時(shí)候用):str( )和int( )
name = '楊鴿'
age = 18
print(name + age) #報(bào)錯(cuò)
print(name + str(age))
楊鴿18
5. 轉(zhuǎn)int( )
s1 = '128'
f1 = 98.7
s2 = '98.7'
ff = True
s3 = 'hello'
print (type(s1), type(f1), type(s2), type(ff),type(s3))
print(int(s1))
print(int(f1)) #float轉(zhuǎn)int 小數(shù)部分抹去
#print(int(s2))
print(int(ff))
#print(int(s3)) #str轉(zhuǎn)int 必須是數(shù)字
128
98
1
6. 轉(zhuǎn)浮點(diǎn)型float()
(1) 數(shù)字符串中的非數(shù)字串沒(méi)辦法轉(zhuǎn)成float
(2) 整數(shù)轉(zhuǎn)的時(shí)候后面+ .0
六. python中的注釋
1. 單行注釋
:#開(kāi)頭 直到換行
2. 多行注釋
:并沒(méi)有單獨(dú)的多行注釋表寄,將一對(duì)三引號(hào)之間的代碼成為多行注釋
3. 中文編碼聲明注釋
:在文件開(kāi)頭加上中文聲明的注釋,用以指定源碼文件的編碼格式(python3不需要寫了)
筆記總結(jié)于馬士兵python基礎(chǔ)學(xué)習(xí)視頻:https://www.bilibili.com/video/BV1wD4y1o7AS
原文鏈接:https://blog.csdn.net/wistonty11/article/details/108650340
總結(jié)
以上是生活随笔為你收集整理的马士兵python_马士兵:python学习(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 赛宁网安-r3kapig联合战队冲击DE
- 下一篇: websocket python爬虫_p