python基礎(chǔ)知識

01 python基礎(chǔ)知識

變量和字符串

變量:變量就是一個盒子,用來存放你想存的東西

賦值:把想存的東西放進(jìn)變量這個盒子里

打印:把這個東西顯示在桌面上

a = 1
print(a)
1

字符串:字符串是一段文字,可以使用單引號、雙引號、三引號表示

a = 'Jack'
print(a)
b = ''' I don't know.'''
print(a + b)
print(a + b*2)
Jack
Jack I don't know.
Jack I don't know. I don't know.

字符串替換replace

a = 'Jack'
b = ''' I don't know.'''
s = a + b*2
s = s.replace('Jack','TIM')
print(s)
TIM I don't know. I don't know.

字符串切片

a[0:2]
'Ja'

字符串格式化:

'含義%s的字符串'%字符串內(nèi)容

'含有{}的字符串'.format('字符串內(nèi)容')

a = 'abc%sdef'%'000'
print(a)
abc000def
a = 'abd{}def'.format('000')
print(a)
abd000def
### 數(shù)據(jù)結(jié)構(gòu)

列表 [數(shù)據(jù)1,數(shù)據(jù)2]

字典 {'數(shù)據(jù)1':data1,'數(shù)據(jù)2':data2}

元祖 (數(shù)據(jù)1,數(shù)據(jù)2)

集合 {數(shù)據(jù)1,數(shù)據(jù)2}

a = ['a' , 'b' ,['a' , 'b']]
b = {'name':'jack','age':'20'}
c = (1,2,3)
d = {1,2,3}
print(a)
print(b)
print(c)
print(d)
['a', 'b', ['a', 'b']]
{'name': 'jack', 'age': '20'}
(1, 2, 3)
{1, 2, 3}
a.append('d')
print(a)
a.remove('d')
print(a)
['a', 'b', ['a', 'b'], 'd']
['a', 'b', ['a', 'b']]

條件判斷

a = 1
if a ==1:
    print('a等于1')
else:
    print('a不等于1')
a等于1

循環(huán)

for i in range(1,11):
    j = i+1
    print(j)
2
3
4
5
6
7
8
9
10
11

函數(shù)及內(nèi)建庫

常見內(nèi)建函數(shù)

type : 查看數(shù)據(jù)類型

len : 查看元素長度

str : 修改類型為字符串

int :修改數(shù)據(jù)字符串為數(shù)值

round : 保留對應(yīng)小數(shù)位數(shù)

def : 自定義函數(shù)

自定義函數(shù)

def plus(a,b):
    return "%d plus %d is: %d "%(a,b,a+b)
c = plus(101,110)
print(c)
101 plus 110 is: 211 

第三方庫

安裝 在CMD中運(yùn)行pip install 庫名

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容