python3六大数据类型的操作

 

遍历字典

>>> a={"name":"xiaoyiyi","age":18}

>>> for i in a:  #I是key值

...     print a[i]

...

18

xiaoyiyi

 

range(10):输出0-9的列表

range(2,7):2-6的列表

 

>>> for i in range(0,51,5):  #step为5

...     print i

...

0

5

10

15

20

25

30

35

40

45

50

>>>

 

 

>>> for i in range(10,0,-1): #递减

...     print i

...

10

9

8

7

6

5

4

3

2

1

>>>

 

使用range()和len()结合遍历输出容器类数据,优点是可以把i输出

>>> for i in range(len(a)):

...     print a[i]

...

aaa

bbb

ccc

>>>

 

>>> for i in range(len(a)):

...     print i

...

0

1

2

>>>

 

round() 四舍五入

 

>>> import math

>>> math.ceil(4.0001)

5.0

>>> math.floor(4.999)

4.0

>>>

 

>>> import ramdom

>>> random.random()

0.3898325921927488

 

>>> random.randrange(0,11,2) #随机0到10的偶数

6

 

>>> random.randrange(0,11)#随机0到10的数

5

>>>

 

>>> random.choice([10,20,30,40])#列表中随机选一个

40

 

>>> print(r"aaa\\''bb") #原样输出

aaa\\''bb

>>> print("aaa\\''bb")

aaa\''bb

>>>print ("%s---%d"%("nihao",5)) string格式化输出

 

>>> print("4.5556==>%0.2f"%(4.5556))

4.5556==>4.56

>>>

 

字符串查找,find index都可以,index找不到会报错,find返回-1

>>> name

'chenjian'

>>> name.find("ss")

-1

>>> name.index("ss")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: substring not found

>>>

 

Str 去除空格

>>> s=" name "

>>> s

' name '

>>> len(s)

6

>>> s.strip()

'name'

>>> s

' name '

>>> len(s.strip())

4

>>>

 

List

>>> a

['aaa', 'bbb', 'ccc', 60]

>>> a.append([10,20,30])

>>> a

['aaa', 'bbb', 'ccc', 60, [10, 20, 30]]

>>> a.extend(10,20,30)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: extend() takes exactly one argument (3 given)

>>> a.extend([10,20,30])

>>> a

['aaa', 'bbb', 'ccc', 60, [10, 20, 30], 10, 20, 30]

>>>

 

>>> a.pop(0)

'aaa'

>>> a

['bbb', 30, 'ccc', 60, [10, 20, 30], 10, 20, 30]

>>> a.pop(0)

'bbb'

>>> a

[30, 'ccc', 60, [10, 20, 30], 10, 20, 30]

>>>

>>> a.remove("ccc")

>>> a

[30, 60, [10, 20, 30], 10, 20, 30]

>>>

 

 

Python 2.7没有copy吗???

>>> a

[30, 60, [10, 20, 30], 10, 20, 30]

>>> b=a.copy()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'list' object has no attribute 'copy'

>>>

 

>>> t=(2) #一个元素的元祖,默认不是元祖

>>> t

2

>>> type(t)

<type 'int'>

>>> tt=(3,)

>>> tt

(3,)

>>> type(tt)

<type 'tuple'>

>>>

 

Set是无序不重复元素集合

set()或者{}来创建集合,空集合只能是set(),{}是用来创建字典的

>>> s=set()

>>> s={1,2,3,4,5}

>>> s[0]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'set' object does not support indexing

>>> s.add(8)

>>> s

set([1, 2, 3, 4, 5, 8])

>>> s.update([9,10])

>>>

>>> s

set([1, 2, 3, 4, 5, 8, 9, 10])

>>> s.remove(3)

>>> s

set([1, 2, 4, 5, 8, 9, 10])

>>> s,remove(6)#不存在的话就报错

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'remove' is not defined

>>> s.discard(6)#不存在不会报错

>>> s

set([1, 2, 4, 5, 8, 9, 10])

>>> s.disacrd()8

  File "<stdin>", line 1

    s.disacrd()8

               ^

SyntaxError: invalid syntax

>>> s.disacrd(8)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'set' object has no attribute 'disacrd'

>>> s.discard(8)

>>> s

set([1, 2, 4, 5, 9, 10])

>>>

 

Dic

d.items()返回键值对

d.get(“aaa”) 

>>> d={"name":"Hedy","age":18,"sex":"f"}

>>> d

{'age': 18, 'name': 'Hedy', 'sex': 'f'}

>>> d.get("habit")

>>> d.get("habit","drawing”)#如果这个值不在字典里,就设置值为drawing

'drawing'

>>> d["habit"]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

KeyError: 'habit'

>>>

 

>>> d.update({'age':22,"phone":'11111’})#批量更改

>>> d

{'phone': '11111', 'age': 22, 'name': 'Hedy', 'sex': 'f'}

>>

>>> d.pop("phone")

'11111'

>>> d

{'age': 22, 'name': 'Hedy', 'sex': 'f'}

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值