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