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)
运行: