python 基本数据类型

基本数据类型分为:

1、数字类型,int型

将字符串转化成数字

a="123"
print(type(a))
b=int(a)
print(type(b))

a='0011'
#转化成16进制
b=int(a,base=16)
##查看2进制长度
a=5
print(a.bit_length())

2.字符串类型 string

列表中可以嵌套任何类型,类表 中的元素可以是数字、字符串、布尔值、字典等所有的都能放进去

列表元素是可以被修改的

name = 'xianyanghua'
print(name.capitalize())
#Xianyanghua
print(name.count('a'))
#3
print(name.center(50,'*'))
#   *******************xianyanghua********************
print(name.startswith('xian'))
#True
print('+'.join(name))
#x+i+a+n+y+a+n+g+h+u+a
print('\n123 '.strip())
#123
print('1+2+3+4'.split('+'))
#[u'1', u'2', u'3', u'4']
msg = 'my name is {name} and i am {age} old'
print(msg.format(name=name,age=20))

3.列表

#创建
fruit = ['apple','pear','grape','orange']   

#切片
print(fruit[1])      #pear
print(fruit[1:3])    #['pear', 'grape']
print(fruit[-1])     #orange
print(fruit[:2])     #['apple', 'pear']

# 追加
fruit.append('peach')
print(fruit)         #['apple', 'pear', 'grape', 'orange', 'peach']

# 删除
fruit.remove('peach')   #删除指定的
print(fruit)         #['apple', 'pear', 'grape', 'orange']

fruit.pop()          #删除列表最后一个元素
print(fruit)         #['apple', 'pear', 'grape']

del fruit[2]         #删除指定的索引
print(fruit)         #['apple', 'pear']

# 插入
fruit.insert(1,'grape')   #把‘grape’加入到索引为1的位置
print(fruit)         #['apple', 'grape', 'pear']

# 修改
fruit[2] = 'orange'  #直接修改
print(fruit)         #['apple', 'grape', 'orange']

# 扩展
fruit1 = ['apple','orange']
fruit2 = ['pear','grape']
fruit1.extend(fruit2)
print(fruit1)         #['apple', 'orange', 'pear', 'grape']

# 统计
print(fruit1.count('apple'))    #1

# 排序
fruit1.sort()
print(fruit1)     #['apple', 'grape', 'orange', 'pear']

fruit1.reverse()
print(fruit1)     #['pear', 'orange', 'grape', 'apple']

# 获取下标
print(fruit1.index('apple'))    #3

# 同时获取下标和值
for index,item in enumerate(fruit1):
    print(index,item)
    
# 结果    
0 pear
1 orange
2 grape
3 apple

4.元组

元素不可修改,不能被 增加或者删除

元组的一级元素不可被修改或者删除、增加

fruit = ('apple','orange','grape')

# 常用功能
print(fruit.count('apple'))   #1
print(fruit.index('orange'))  #1
print(fruit[1]) #orange

5.字典

# 创建
fruit = {1:'apple',2:'orange',3:'grape'}
print(fruit)

# 增加
fruit[4] = 'pear'
print(fruit)      #{1: 'apple', 2: 'orange', 3: 'grape', 4: 'pear'}

# 修改
fruit[4] = 'peach'
print(fruit)      #{1: 'apple', 2: 'orange', 3: 'grape', 4: 'pear'}

# 删除
fruit.pop(4)       #删除指定的key
print(fruit)       #{1: 'apple', 2: 'orange', 3: 'grape'}

# 查找value
print(fruit.get(1))     #apple
#更新
fruit.update({1:'banana',4:'pearl'})
print(fruit) #{1: u'banana', 2: u'orange', 3: u'grape', 4: u'pearl'}

# 循环
for k,v in fruit.items():
    print(k,v)

1 apple
2 orange
3 grape

for k in fruit.keys():
    print(k)

1
2
3

for item in  fruit:
    print(item)
1
2
3


for v in fruit.values():
    print(v)
    
apple
orange
grape

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值