Loading

Python/零起点(一、数字及元组)

Python/零起点(一、数字及元组)

int整型 int()强行转换成整型数据类型
int整型是不可变,且是不可迭代的对象

一、整型数字用二进制位数表示案例:

1 age=7               #设定一个数字赋值给age变量
2 num=age.bit_length()#把age用几个二进制表示,赋值给num
3 print(num)
4 -------------------------------------------------------------------
5 运行结果:
6 3
7 
8 Process finished with exit code 0

二、元组 

tuple元组 tuple()强行转换成元组数据类型:
tuple元组是不可变的数据类型,是可迭代的对象

元组查看元素的下标案例:

1 t=(1,2,3,4)  #创建一个元组
2 t1=t.index(3)   #查看元组的元素,返回下标赋值给t1
3 print(t1)
4 ---------------------------------------------------
5 运行结果:
6 2
7 
8 Process finished with exit code 0

元组中统计元素案例:

1 t=(1,2,3,4)  #创建一个元组
2 t1=t.count(1)   #统计元组中元素出现的次数。
3 print(t1)
4 ------------------------------------------
5 运行结果:
6 1
7 
8 Process finished with exit code 0

元组通过索引获取元素案例:

1 t=(1,2,3,4)  #创建一个元组
2 print(t[2])
3 ----------------------------
4 运行结果:
5 3
6 
7 Process finished with exit code 0

 

posted @ 2017-05-13 21:37  Meet~  阅读(341)  评论(0编辑  收藏  举报