python基础练习之数据类型

上机练习:基本数据类型

(一)数值基本运算

  1. 基本数值运算:+, -, *, /, //, %, **
  2. 进制间转换: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 (
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值