python内置函数( )可以返回列表长度_python 内置函数

print():用于打印输出,常用来调试信息,可以接受多个参数

例子:

print 'Hello Python'

a ='hello'b ='python'printa,b

print '%s%s' % ('hello',' python')

input():用于接收用户输入,返回类型与输入的类型一致,注意输入是字符串类型时,要加上’’或””,否则会报错

例子:

test =input('请输入账号:')printtest

运行:

请输入账号:123456

123456

请输入账号:'test'

test

请输入账号:test

Traceback (most recent call last):

File "F:/AFT/TestProject/wxMsg/script/test_script.py", line 4, in

test = input('请输入账号:')

File "", line 1, in

NameError: name 'test' is not defined

如上输入test是字符串,如果不用引号,则报错,而采用raw_input函数则不会报错。因为raw_input函数会自动转换为字符串,即使输入的是纯数字,也是字符串类型。

例子:

a =raw_input('请输入账号:')printa

运行:

请输入账号:test

test

a =raw_input('请输入账号:')printtype(a)

运行:

请输入账号:123

chr():用于返回对应的 ASCII 字符,参数的范围是0~255

例子:

print chr(97)

运行:

a

tuple():用于将列表或字典转换为元组

例子:

a = [1,2,3]printtuple(a)

运行:

(1, 2, 3)

当转换对象是字典时,将转换字典的键

a = {'a':1,'b':2,'c':3}printtuple(a)

运行:

('a', 'c', 'b')

len():用于返回对象长度或个数

例子:

a = {'a':1,'b':2,'c':3}printlen(a)

b = [1,2,3]printlen(b)

c ='hello python'printlen(c)

运行:

3

3

12

range(x, y, step):用于生成一个列表,常常用于for语句

参数一x: 计数从 x 开始。默认是从 0 开始

参数二y: 计数到 y 结束,但不包括 y

参数三step:步长,默认为1

例子:

printrange(10)printrange(2,12)printrange(1,10,2)printrange(-1,-10,-2)

运行:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

[1, 3, 5, 7, 9]

[-1, -3, -5, -7, -9]

type():用于返回对象的类型

例子:

a ='12345'b = [1,2,3,4,5]

c = {'a':1,'b':2,'c':3,'d':4,'e':5}

d = (1,2,3,4,5)printtype(a)printtype(b)printtype(c)printtype(d)

运行:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值