Python commonly used data types []

int, float, string
integer, float, string
-------------------------------------- -
character string (string)
with the quoted text

>>> Chinese = 'mirror of the world'
>>> English = "mirror world"
>>> Number The = "666"
>>> Symbol = '' 'Section A! @' ''
>>> Mixture = 'Mirror mirror world world666 Section A! @ '
>>> Print (Chinese)
mirror World
>>> Print (English)
mirror world
>>> Print (Number The)
666
>>> Print (Symbol)
Section A! @
>>> Print (Mixture)
mirror mirror world666 world Section A! @

-------------------------------------------
integer (int)
a positive integer, negative integer and collectively zero, no decimal point
-------------------------------------- -----
float (float)
with decimal point, the result of calculation errors

Print >>> (0.55 + 0.3)
0.8500000000000001 than # 0.85

--------------------
* four * arithmetic
operator precedence (similar mathematical operation priority)

>>> print(499*561+10620-365)
290194
>>> print((5025-525)/100+18*17)
351.0

 

--------------------
* * string splicing
methods: '+' sign spliced data
object: Data Integration

 

————————————————————

* Data type of query --type () functions *
effect: Type query data
Example: print (type ( '') )

>>> number = 153
>>> print(type(number))
<class 'int'>


 

数据转换
转换数据类型的函数
str()、 int()和float()

 

 

 

 

整数转换字符串类型

>>> magic = '水仙花数'
>>> action = '是'
>>> unit = '个'
>>> expression = '=1x1x1+5x5x5+3x3x3'
>>> number = 153

两种写法:

 

>>> print(str(number)+expression+action+unit+magic)
153=1x1x1+5x5x5+3x3x3是个水仙花数

>>> print('153'+expression+action+unit+magic)
153=1x1x1+5x5x5+3x3x3是个水仙花数

 

————————————————————————
int()函数
#只有符合整数规范的字符串类数据,才能被int()强制转换

>>> number1 = '6'
>>> number2 = '1'
>>> print(int(number1)+int(number2))
7

 

print(int('3.8')) #另外小数形式的字符串也不能强制转换
运行后显示结果:ValueError:invalid literal for int() with base 10: '3.8'

但浮点数是可以被int()函数强制转换
print(int(3.8)) #int()函数会做取整处理,小数位直接抹零
3
——————————————————
float()函数

>>> height = 188.0
>>> weight = 180.0
>>> age = '89'
>>> print(float(height))
188.0
>>> print(float(weight))
180.0
>>> print(float(age))
89.0

 

*数据多次转换*

>>> slogan = '脸黑怪我咯'
>>> number = '7.8'
>>> unit = '张'
>>> sentence = '蓝票一个SSR都没有'
>>> print(slogan+str(int(float(number)))+unit+sentence)
脸黑怪我咯7张蓝票一个SSR都没有

 

Guess you like

Origin www.cnblogs.com/CH-TNT/p/11222884.html