Python基礎(chǔ)知識(shí)學(xué)習(xí)-Python數(shù)據(jù)類型

入門(mén)Python,先學(xué)一下基礎(chǔ)知識(shí)吧!跟我一起來(lái)復(fù)習(xí)!
1 Python數(shù)據(jù)類型
1.1 字符串
在Python中用引號(hào)引起來(lái)的字符集稱之為字符串,比如:'hello'、"my Python"、"2+3"等都是字符串 Python中字符串中使用的引號(hào)可以是單引號(hào)、雙引號(hào)跟三引號(hào)
print ('hello world!')
hello world!
c = 'It is a "dog"!'
print (c)
It is a "dog"!
c1= "It's a dog!"
print (c1)
It's a dog!
c2 = """hello
world
!"""
print (c2)
hello
world
!
轉(zhuǎn)義字符''
轉(zhuǎn)義字符\可以轉(zhuǎn)義很多字符,比如\n表示換行,\t表示制表符,字符\本身也要轉(zhuǎn)義,所以\ \表示的字符就是\
print ('It\'s a dog!')
print ("hello world!\nhello Python!")
print ('\\\t\\')
It's a dog!
hello world!
hello Python!
\ \
原樣輸出引號(hào)內(nèi)字符串可以使用在引號(hào)前加r
print (r'\\\t\\')
\\\t\\
子字符串及運(yùn)算
s = 'Python'
print( 'Py'in s)
print( 'py'in s)
True
False
取子字符串有兩種方法,使用[]索引或者切片運(yùn)算法[:],這兩個(gè)方法使用面非常廣
print (s[2])
t
print (s[1:4])
yth
字符串連接與格式化輸出
word1 = '"hello"'
word2 = '"world"'
sentence = word1.strip('"') + ' ' + word2.strip('"') + '!'
print( 'The first word is %s, and the second word is %s' %(word1, word2))
print (sentence)
The first word is "hello", and the second word is "world"
hello world!
1.2 整數(shù)與浮點(diǎn)數(shù)
整數(shù)
Python可以處理任意大小的整數(shù),當(dāng)然包括負(fù)整數(shù),在程序中的表示方法和數(shù)學(xué)上的寫(xiě)法一模一樣
i = 7
print (i)
7
7 + 3
10
7 - 3
4
7 * 3
21
7 ** 3
343
7 / 3#Python3之后,整數(shù)除法和浮點(diǎn)數(shù)除法已經(jīng)沒(méi)有差異
2.3333333333333335
7 % 3
1
7//3
2浮點(diǎn)數(shù)
7.0 / 3
2.3333333333333335
3.14 * 10 ** 2
314.0
其它表示方法
0b1111
15
0xff
255
1.2e-5
1.2e-05
更多運(yùn)算
import math
print (math.log(math.e)) # 更多運(yùn)算可查閱文檔
1.0
1.3 布爾值
True
True
False
False
TrueandFalse
False
TrueorFalse
True
notTrue
False
True + False
1
18 >= 6 * 3or'py'in'Python'
True
18 >= 6 * 3and'py'in'Python'
False
18 >= 6 * 3and'Py'in'Python'
True
1.4 日期時(shí)間
import time
now = time.strptime('2016-07-20', '%Y-%m-%d')
print (now)
time.struct_time(tm_year=2016, tm_mon=7, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=202, tm_isdst=-1)
time.strftime('%Y-%m-%d', now)
'2016-07-20'
import datetime
someDay = datetime.date(1999,2,10)
anotherDay = datetime.date(1999,2,15)
deltaDay = anotherDay - someDay
deltaDay.days
5
還有其他一些datetime格式
查看變量類型
type(None)
NoneType
type(1.0)
float
type(True)
bool
s="NoneType"
type(s)
str
類型轉(zhuǎn)換
str(10086)
'10086'
?float()
float(10086)
10086.0
int('10086')
10086
complex(10086)
(10086+0j)
歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
*聲明:本文于網(wǎng)絡(luò)整理,版權(quán)歸原作者所有,如來(lái)源信息有誤或侵犯權(quán)益,請(qǐng)聯(lián)系我們刪除或授權(quán)事宜
評(píng)論
圖片
表情
