• 技术文章 >Python技术 >Python基础教程

    Python中常用的函数介绍

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-12 11:23:42转载2640

    Python中常用的几种函数

    1、input函数

    input()函数:主要作用是让用户输入某个内容并接收它。

    1

    2

    3

    #输入你的年龄

    >>> age = input("my age is :")

    my age is :20

    执行代码后输入年龄,年龄被存放到age变量中,执行print后终端会打印出年龄。

    1

    2

    3

    4

    >>> age = input("my age is :")

    my age is :20

    >>> print(age)

    20

    2、raw_input()函数

    raw_input():读取输入语句并返回string字符串,输入以换行符结束。

    1

    2

    3

    4

    5

    6

    >>> name = raw_input("please input your name:")

    please input your name:abc

    >>> print(name)

    abc

    >>> type(name)

    <type 'str'>

    注意:input()和raw_input()函数的区别:(1)input支持合法python表格式“abc",字符串必须用引号扩起;否则会报语法错误(2) raw_input()将所有输入作为字符串,返回string,而input()输入数字时具有自己的特性,返回输入的数字类型int或float。

    1

    2

    3

    4

    5

    6

    7

    #1、语法报错

    >>> name = input("input your name:")

    input your name:abc

    Traceback (most recent call last:)

       File "<stdin>",line 1, in <module>

       File "<string>",line 1, in <module>

    NameError: name 'abc' is not defined

    1

    2

    3

    4

    5

    #2、正确输入

    >>> name = input("input your name:")

    input your name:"abc"

    >>> print(name)

    abc

    1

    2

    3

    4

    5

    #3、input()数据类型

    >>> age = input("your age:")

    your age:20

    >>> type(age)

    <type 'int'>

    1

    2

    3

    4

    5

    #4、raw_input() 数据类型均为str

    >>> age = raw_input("your age:")

    your age:20

    >>> type(age)

    <type 'str'>

    3、format()函数

    format():格式化字符串函数,常用功能是插入数据和数字格式化;

    ①插入数据

    eg :输出 ”你叫***,你的年龄***“;

    先输入用户的姓名和年龄,用变量name和age接收数据。

    1

    2

    3

    4

    >>> name = input("please enter your name:")

    please enter your naem:root

    >>> age = input ("please enter your age:")

    please enter your age:18

    使用format()实现数据的插入;其中 {} 为占位符,具体数据再后面导入。

    1

    2

    >>> print('your name {}, your age {}.format(name,age))

    your name root, your age 18

    ②数字格式化

    eg:保留数据的两位小数;不带小数为:{:.0f};

    1

    2

    >>> print("{:.2f}".format(3.1415926))

    3.14

    4、range()函数

    range():如需处理一组数字列表,且数字列表满足一定规律时使用;range()函数可以生成一个从0到x-1的整数序列。

    同时range(a,b)可以取某个区间的整数。

    eg:①、打印0到9的整数。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    >>> for a in range(10):

    ...     print(a)

    ...    

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    ②、打印10到15之间的整数。

    1

    2

    3

    4

    5

    6

    7

    8

    >>> for a in range(10,15):

    ...     print(a)

    ...

    10

    11

    12

    13

    14

    注意:range(a,b)取区间数时包头不包尾,尾数要+1。

    5、xrange()函数

    xrange():与range()类似,但不创建列表,而是返回一个xrange对象,它的行为与列表相似,但只在需要时计算列表值,当列表很大时,可以为我们节省内存。

    1

    2

    3

    4

    5

    6

    7

    >>> a = xrange(10)

    >>> print a[0]

    0

    >>> print a[1]

    1

    >>> print a[9]

    9

    python学习网,免费的在线学习python平台,欢迎关注!

    专题推荐:python 函数
    上一篇:Anaconda安装报错:Failed to create Anaconda menus 下一篇:Python中常用的内置函数介绍

    相关文章推荐

    • python函数、类的区别• python如何定义函数• Python:函数参数类型和排序的总结

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网