zoukankan      html  css  js  c++  java
  • 学习Python3基础知识过程中总结

    print()中end==""的用法


    例子:用Python3输出九九乘法表:

    for i in range(1,10):
        for j in range(1,i+1):
            s=i*j
            print ('%d*%d==%d	'%(i,j,s),end="")
        print("")#换行

    其中第4行输出中," "是表示制表符,end==""是表示这句输出由什么结尾,默认是end==" "也就是换行,现在代码中所写将换行改成了以空字符串结尾。即不换行。

    推荐使用IPython


    推荐使用 IPython: Jupyter and the future of IPython

    主要特点和便捷的地方:
    1. 按 tab 键可以补全,包括 import 时的模块,类对象的成员等。
    2. 方便查看文档,想查看任意对象的文档时,直接在后面加个 "?",如:

    In [12]: import os
    
    In [13]: os.mkdir?
    Type:       builtin_function_or_method
    String Form:<built-in function mkdir>
    Docstring:
    mkdir(path [, mode=0777])
    
    Create a directory.

    3. 方便查看源代码,上面提到一个问号查看文档,两个问号就可以查看源码了,而且有高亮:

    In [21]: import logging
    
    In [22]: logging.error??
    Type:       function
    String Form:<function error at 0x7f8ab845f320>
    File:       /usr/lib/python2.7/logging/__init__.py
    Definition: logging.error(msg, *args, **kwargs)
    Source:
    def error(msg, *args, **kwargs):
        """
        Log a message with severity 'ERROR' on the root logger.
        """
        if len(root.handlers) == 0:
            basicConfig()
        root.error(msg, *args, **kwargs)

    来自知乎用户推荐:https://www.zhihu.com/question/28840494

    input()的使用


    def calcute_profit(I):
        return I*0.01
    I=int(input('净利润:'))
    profit=calcute_profit(I)
    print('利润为%d时,应发奖金为%d元'%(I,profit))

    operator.itemgetter()函数 、dict.items()函数、sorted()函数


    对字典进行排序,但是是按照字典的值从大到小进行排序。

    import operator
    x={1:2,3:4,4:3,2:1,0:0}
    sorted_x=sorted(x.items(),key=operator.itemgetter(1),reverse=True)
    print(sorted_x)
    
    
    
    #输出
    [(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]

    operator.itemgetter()函数 :

    operator模块中的itemgetter()函数。用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子。

    a = [1,2,3] 
    >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值
    >>> b(a) 
    2 
    >>> b=operator.itemgetter(1,0)   //定义函数b,获取对象的第1个域和第0个的值
    >>> b(a) 
    (2, 1) 

    要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。

    参考博客:https://blog.csdn.net/u011475210/article/details/77770772

    dict.items()函数:

    Python 字典(Dictionary) items()方法以列表的形式返回可遍历的(键, 值) 元组数组。

    看例子:

    dict = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'}
     
    print "字典值 : %s" %  dict.items()
     
    # 遍历字典列表
    for key,values in  dict.items():
        print key,values

    输出:

    字典值 : [('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')]
    Google www.google.com
    taobao www.taobao.com
    Runoob www.runoob.com

    注意:对字典进行排序是不可能的,只能把字典转换成另一种方式才能排序。字典本身是无序的,但是如列表元组等其他类型是有序的,所以需要用一个元组列表来表示排序的字典。

    sorted()函数:

    Python3 sorted() 函数对所有可迭代的对象进行排序操作。

    sorted 语法:

    sorted(iterable, key=None, reverse=False)

    参数说明:

    • iterable -- 可迭代对象。
    • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
    • reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
    • 返回值---返回重新排序的列表。

    注意:

    sort 与 sorted 区别:

    sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

    list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

    平常练习可在runoob上的Python100例中学习Python的各种基础知识:

    http://www.runoob.com/python/python-100-examples.html

  • 相关阅读:
    Maven中配置生成单元测试报告配置
    maven-surefire-plugin总结
    Java程序内存分析:使用mat工具分析内存占用
    利用内存分析工具(Memory Analyzer Tool,MAT)分析java项目内存泄露
    JVM性能监控工具(一)-jdk命令行工具
    Java线程详细监控和其dump的分析使用—-分析Java性能瓶颈
    JDK自带内存及线程分析工具
    使用jstack和TDA进行java线程dump分析
    qt 插件开发
    精通linux设备驱动开发 笔记
  • 原文地址:https://www.cnblogs.com/dudududu/p/8821113.html
Copyright © 2011-2022 走看看