目录
上机练习:基本数据类型
(一)数值基本运算
- 基本数值运算:+, -, *, /, //, %, **
- 进制间转换:hex( ) 十六进制, oct( ) 八进制, bin( )二进制
33 + 7 # 加
40
33 - 7 # 减
26
33 * 7 # 乘
231
33 // 7 # 整除
4
33 / 7 # 除
4.714285714285714
33 % 7 # 余数
5
divmod (33,7) # 整除除法和余数
(4, 5)
33 ** 7 # 乘方
42618442977
hex (33) # 转十六进制
'0x21'
oct (33) # 转八进制
'0o41'
bin (7) #转二进制
'0b111'
(二)类型转换
1,0,‘abc’,None,1.2,False,‘ ’
str(), bool(), int(), float(), is None, ==, !=
str (0)
'0'
str ('abc')
'abc'
str (None)
'None'
str (1.2)
'1.2'
str (False)
'False'
str (' ')
' '
str ('')
''
bool (1)
True
bool (0)
False
bool ('abc')
True
bool (None)
False
bool (1.2)
True
bool (False)
False
bool (' ') # 引号中间有空格
True
bool ('') # 引号中间没有空格
False
注意:布尔型中‘’和‘空格‘不一样
int (0)
0
int (1.2)
1
int (False)
0
1 is None
False
'abc' is None
False
'' is None
False
False is None
False
None is None
True
0 == None
False
1 != None # 不等于
True
(三)字符串基本操作
+, *, len( ), [ ], in,ord( ), chr( ),含有中文的字符串
注意:字符串一定要加引号!
'hello' + 'world'
'helloworld'
'hello' * 5
'hellohellohellohellohello'
len ('hello')
5
'hello world' [3:8:3]
'lw'
'hello world' [:10:4]
'hor'
'hello world' [4::2]
'owrd'
'hello word' [-1::2]
'd'
'hello world' [-1:-10:-2]
'drwol'
'oo' in 'hello world'
False
'he' in 'hello world'
True
('hello world' [4::2]) * 3
'owrdowrdowrd'
ord ('A')
65
ord ('a')
97
ord ('1')
49
chr (66)
'B'
chr (