Basic data types and type conversion operator &

A, int

  • Positive and negative integers such as: 0-100
  • Binary, octal, hexadecimal numbers

Hexadecimal representation in Python

>>> 11            # 十进制
>>> 0b01011       # 二进制
>>> 0o71          # 八进制
>>> 0x3af         # 十六进制
>>> type(0)
<class 'int'>
>>> type(-100)
<class 'int'>
>>> type(0b01011)
<class 'int'>
>>> type(0o71)
<class 'int'>
>>> type(0x3af)
<class 'int'>
>>>

Second, the float

Numbers with a decimal point, that is, we usually say decimals.

0.1
-0.7

Third, the string type

python are used in quotes string object

s1 = "www.qfedu.com"
s2 = 'lenovo'
s3 = '1314'
s4 = '()(*)'

Fourth, Boolean type

Only two Boolean values

  • True for true
  • False for false
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>>

V. convert between data types

1. Other types convert integer
int () is a built-in function

# 转换
>>> int(0.1)
0
>>> int(3.9)
3
>>> int("30")
30

int () can not contain a non-target character string type to convert integer numbers of symbols
such as decimal point., empty string ""

>>> int('192.168.1.100')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '192.168.1.100'
>>>
>>> int("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

2. convert other types of floating point

# 转换
>>> float(1)
1.0
>>> float('1')
1.0
>>> float('-1')
-1.0
>>> float(-1)
-1.0
>>> float('1.3')
1.3

3. The other type String to convert

>>> str(1)
'1'
>>> str(1.0)
'1.0'
>>> str(True)
'True'
>>> str(False)
'False'
>>>

4. converted to other types Boolean

>>> bool(0)
False
>>> bool(1)
True
>>> bool("")
False
>>> bool(" ")
True
>>> bool("lenovo")
True
>>>

Six, operator

>>> 10 + 2     #加法
12

>>> 10 - 3     #减法
7

>>> 10 * 3     #乘法
30

>>> 10 / 3     #除法
3.3333333333333335

>>> 10 // 3     #整除
3

>>> 10 % 3      #取余
1 
>>> 

>>> n = 5     
>>> n += 2
>>> n += 2
>>> n
9
>>> 

Published 14 original articles · won praise 9 · views 462

Guess you like

Origin blog.csdn.net/Adley_zk/article/details/104652675