python基本資料型別

2021-08-02 23:32:33 字數 2530 閱讀 4597

字串

面試題案例: 123和「123」一樣麼?

- 從數字角度講

- 從程式語言的識別來講

>>> a = 123

>>> stra = "123"

>>> a == stra

false

>>>

print a

123>>>

print stra

123>>> a + stra

traceback (most recent call last):

file "", line 1, in

typeerror: unsupported operand type(s) for +: 'int'

and'str'

>>> num1 = 123

>>> type

(num1)

>>> type

(123)

>>> num2 = 999999999999999

>>> type

(num2)

強制定義為長整型: num3 = 123l

>>> num3 = 123l

>>> type

(num3)

>>> f1 = 12

>>> type

(f1)

>>> f2 = 12.0

>>> type

(f2)

>>> c = 3.14

>>> type

(c)>>> c = 3.14j

>>> type

(c)

# 字串定義的第一種方式:

>>> str1 = 'our company is westos'

# 字串定義的第二種方式:

>>> str2 = "our company is westos"

# 字串定義的第三種方式:

>>> str3 = """our company is westos"""

>>> type(str1)

'str'>

>>> type(str2)

'str'>

>>> type(str3)

'str'>

>>> say = 'let\'s go'

>>> say

"let's go"

>>> say = "let's go "

>>> say

"let's go "

>>> mail = "tom: hello i am westos "

>>>

print mail

tom: hello i am westos

>>> mail = "tom:\n hello\n i am westos "

>>>

print mail

tom:

hello

i am westos

>>> mail = """tom:

... i am jack

... good luck

... """

>>>

print mail

tom:

i am jack

good luck

>>> mail

'tom:\n\ti am jack\n\tgood luck\n'

>>> a = 'abcde'

>>> type(a)

'str'>

>>> a[0]

'a'>>> a[1]

'b'>>> a[3]

'd'>>> a[0]+a[1]

'ab'

>>> a[0]+a[2]

'ac'

>>> a

'abcde'

>>> a[1:5:2]

'bd'

>>> a[1:5] //代表切片取出第2個到第4個

'bcde'

>>> a[:5]

'abcde'

>>> a[4:]

'e'>>> a[4:1] //python中預設是從左向右取值

''>>> a[4:1:-1] //當步長為-1時,從右向左取值

'edc'

>>> a[:]

'abcde'

>>> a[-1]

'e'>>> a[-4:-1] //代表倒數第2個到倒數第4個切片

'bcd'

>>> a[-2:-4]

''>>> a[-2:-4:1]

''>>> a[-2:-4:-1]

'dc'

python基本資料型別

物件是python中最基本的概念,python中資料以物件的形式出現 無論是python提供的內建物件,還是使用python或是像c擴充套件庫這樣的擴充套件語言工具建立的物件。物件時記憶體中的一部分,包括數值和相關操作的集合。python程式可以分解成模組 語句 表示式以及物件,如下 1 程式由模組...

Python基本資料型別

1 python中一切都是物件。2 每乙個資料都有乙個id標示,用id 可以檢視。也可以用type檢視是什麼型別。3 常用的資料型別 int 整型 數字 boole true 值 賦值,要用大寫 a true string 字串 也稱作序列。list 列表 tuple 元組 dict 字典 set ...

Python基本資料型別

python內建許多資料基本型別。資料型別dt 表示形式 int整形如 1,0,1,float 浮點型如 1.1,0.0,1.1,str字串如 單引號或雙引號括起來的形式 hello python list 列表如 1,2 巢狀列表 1,2,3 tuple 元組如 1,2 set無序列表如 comp...