python基础之基本数据类型

1.int 整数

2.bool 布尔

3.str 字符串,一般放小量数据

4.list 列表,可以存放大量的数据

5.dict字典,以key:value的形式存储数据

6.set集合(数学)

7.tuple 元祖,不可变

int 数据类型

  整数:常见的操作+-*/ // % **

   bit_length():一个数的二进制长度;1的10 进制是1,二进制也是1

s = '你好晨曦我爱你'
print(s[3])
t = 1
print(t.bit_length())
测试
C:UserszrdAppDataLocalProgramsPythonPython37python.exe G:/python/v/test-2.py
曦
1

 2的10进制是2,而2进制是10

t = 2
print(t.bit_length())
测试

2

  bool 布尔

类型转换之字符串转换整数

t = "20"
b = int(t)
print(type(b))
测试
<class 'int'>

  数字转换字符串

a = 10
print(type(a))
b = str(a)
print(type(b))
测试
<class 'int'>
<class 'str'>

   数字转换成布尔

b = 78
print(type(b))
c = bool(b)
print(c)
测试
<class 'int'>
True
a = True
print(int(a))
测试
1
a = False
print(int(a))
测试
0

  

结论:想转换成xxx类型,就用xxx(目标);True转化出来是1,False转化出来是0;在int类的数字转换成bool数据类型,只有0是False,其他的都是False

print(bool(0))
print(bool(""))
print(bool("False"))注意""
print(bool(False))
print(bool(" ")) 
print(bool(987666))
print(bool(-97))
测试
False
False
True
False
True
True
True

  

 

草都可以从石头缝隙中长出来更可况你呢
原文地址:https://www.cnblogs.com/rdchenxi/p/10961715.html