python内置对象、运算符、内置函数

实验二

1、输入一个四位整数。分别计算并输出其个、十、百、千位上的数字。

num=int(input())
print(num%10)
print(num%100//10)
print(num//1000)

总结:要先把数字转换成整型。

2、任意输入3个国家的英文单词,按字典顺序输出,按字符串长度从短到长

cou = (input('PLease enter 3 coutry name:'))
b=cou.split()
b.sort()
print(b)
b.sort(key=len)
print(b)
>>> cou=input('please enter 3 words:')
    please enter 3 words:game code projects
>>> cou
    'game code projects'
>>> list(cou)
    ['g', 'a', 'm', 'e', ' ', 'c', 'o', 'd', 'e', ' ', 'p', 'r', 'o', 'j', 'e',     'c', 't', 's']
>>> cou.split()
    ['game', 'code', 'projects']

  • split()

    这个函数的作用是将字符串根据某个分隔符进行分割,默认是空格,也可以在()内填别的分隔符

  • list()

    会将字符串分割成单个表示

  • sort()

    实现的是从小到大的排序

总结:可以用split()将字符串分割成列表,直接用列表分割不对。

3、用随机函数产生10个随机整数,实现:(1)求出并输出10个数中的最大值和最小值;(2)按从小到大的顺序排序并输出这10个随机数;(3)用过滤函数将其中的奇数过滤掉,显示其中的偶数。

import random
number=[random.randint(0,100)for i in range(10)]
print('max is:',max(number))
print('min is:',min(number))
print('整理后为:',sorted(number))
def odd(n):
    return n % 2 == 0
lst = filter(odd,number)
print('过滤后为:',list(lst))
  • radom()

    random.randint() ,专门获取某个范围内的随机整数。

  • sort()与sort()

    1. sort 是应用在 list 上的方法,属于列表的成员方法,sorted 可以对所有可迭代的对象进行排序操作。
    2. list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
    3. sort使用方法为ls.sort(),而sorted使用方法为sorted(ls)
  • filter()

    filter 的中文含义是“过滤器”,在 Python 中,它就是起到了过滤器的作用。

    filter(function, iterable)
    Construct a list from those elements of iterable for which function returns true. iterable may be either
    a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the r
    esult also has that type; otherwise it is always a list. If function is None, the identity function is assum
    ed, that is, all elements of iterable that are false are removed.
    Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function i
    s not None and [item for item in iterable if item] if function is None.

总结:sorted()返回的是一个新的列表,filter()可以定义函数来过滤,过滤过后要list()才能输出

4、通过map函数,将range函数产生的一组5-10(即[5,6,7,8,9]之间的数与另一组10-15(即[10,11,12,13,14])之间的数进行对应元素值相乘,将得到的新的列表数据输出显示。

x=range(5,10)
y=range(10,15)
z=map(lambda x,y:x*y,x,y)
print(list(z))
  • range()

    返回值貌似并不是列表,range过的值要list()过

    >>> x=range(5,10)
    >>> y=range(10,15)
    >>> z=map(lambda x,y:x*y,x,y)
    >>> z
    <map object at 0x00000248A0A6E748>
    >>> x
    range(5, 10)
    >>> list(x)
    [5, 6, 7, 8, 9]
    
  • lambda()

  1. 在 lambda 后面直接跟变量
  2. 变量后面是冒号
  3. 冒号后面是表达式,表达式计算结果就是本函数的返回值

总结:map()和lambda()可以代替for循环

5、输出一个"hello,world,my name is : xxx",其中xxx是由你输入的一个名字,输出你名字中每个字的Unicode编码。

name=input('Hello world,my name is ')
name1=name.encode('unicode_escape')
print(name1)
  • 中文转Unicode编码:text.encode("unicode_escape")

  • Unicode编码转中文:text.decode("unicode_escape")

6、dir()

dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法dir(),该方法将被调用。如果参数不包含dir(),该方法将最大限度地收集参数信息。

注意点:用完方法要用list()才能输出

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 个人笔记,方便自己查阅使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik阅读 67,777评论 0 5
  • 函数调用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜阅读 1,199评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • 内置函数Python解释器内置了许多功能和类型,总是可用的。他们是按字母顺序列在这里。 abs(x)返回一个数的绝...
    uangianlap阅读 1,272评论 0 0
  • abs() 函数 描述 abs() 函数返回数字的绝对值。 语法 以下是 abs() 方法的语法: abs( x ...
    时光是座城阅读 508评论 0 2