Python基础函数详解
print():用于打印输出,常用来调试信息,可以接受多个参数
例子:
print 'Hello Python'
a = 'hello' b = 'python' print a,b
print '%s%s' % ('hello',' python')
input():用于接收用户输入,返回类型与输入的类型一致,注意输入是字符串类型时,要加上’’或””,否则会报错
例子:
test = input('请输入账号:') print test
运行:
请输入账号:123456
123456
请输入账号:'test'
test
请输入账号:test
Traceback (most recent call last):
File "F:/AFT/TestProject/wxMsg/script/test_script.py", line 4, in <module>
test = input('请输入账号:')
File "<string>", line 1, in <module>
NameError: name 'test' is not defined
如上输入test是字符串,如果不用引号,则报错,而采用raw_input函数则不会报错。因为raw_input函数会自动转换为字符串,即使输入的是纯数字,也是字符串类型。
例子: a = raw_input('请输入账号:') print a
运行:
请输入账号:test
test
a = raw_input('请输入账号:') print type(a)
运行:
请输入账号:123
<type 'str'>
chr():用于返回对应的 ASCII 字符,参数的范围是0~255
例子:
print chr(97)
运行:
a
tuple():用于将列表或字典转换为元组
例子:
a = [1,2,3] print tuple(a)
运行:
(1, 2, 3)
当转换对象是字典时,将转换字典的键
a = {'a':1,'b':2,'c':3} print tuple(a)
运行:
('a', 'c', 'b')
len():用于返回对象长度或个数
例子:
a = {'a':1,'b':2,'c':3} print len(a) b = [1,2,3] print len(b) c = 'hello python' print len(c)
运行:
3
3
12
range(x, y, step):用于生成一个列表,常常用于for语句
参数一x: 计数从 x 开始。默认是从 0 开始
参数二y: 计数到 y 结束,但不包括 y
参数三step:步长,默认为1
例子:
print range(10) print range(2,12) print range(1,10,2) print range(-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) print type(a) print type(b) print type(c) print type(d)
运行:
<type 'str'>
<type 'list'>
<type 'dict'>
<type 'tuple'>
更多文章请浏览李老道自学网:http://www.sterson.com.cn/
相关知识
【Python】基础
Python中的花——详解花的图形绘制
python函数基本操作
python font函数
python 绘制一个四瓣花图
python七夕浪漫表白
python基础
【Python】(二)数据类型与转换
Python中函数加括号和不加括号的区别
【Python实用工具】(情人节献礼)turtle函数绘制动态玫瑰花
网址: Python基础函数详解 https://www.huajiangbk.com/newsview1266776.html
上一篇: 寇健:大豆衍生品为回归常态的大豆 |
下一篇: 【央视财经V讲堂】进口大豆=节省 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039