zoukankan      html  css  js  c++  java
  • 向python3进发

    在Python2.x中,交互输入有input和raw_input两种方法

    1. input-----------tmd是个坑,就别用
    2. raw_input------把输入无论是Int或str都当做是str处理,加入要取到数字,需要强制类型转化

    在Python3.x中,只有input方法,但是效果跟Python2.x的raw_input一样一样的.

     1 #python2.x
     2 >>> s = int(raw_input('input:'))
     3 input:a
     4 >>> type(s)
     5 <type 'str'>
     6 
     7 >>> s = raw_input('input:')
     8 input:99
     9 >>> type(s)
    10 <type 'str'>
    12 >>> score = int(raw_input('input:'))
    13 input:99
    14 >>> type(score)
    15 <type 'int'>
    16 
    17 #python3.x (python 3.x 一切皆类型....看type输出.....)
    18 >>> s = int(input('input:'))
    19 input:a
    20 >>> type(s)
    21 <class 'str'>
    22 
    23 >>> s = input('input:')
    24 input:99
    25 >>> type(s)
    26 <class 'str'>
    27 
    28 >>> score = int(input('input:'))
    29 input:99
    30 >>> type(score)
    31 <class 'int'>

     一种不常见的格式化输出:

     1 >>> info='''
     2 ... ---------info---------
     3 ... name:{_name}
     4 ... age:{_age}
     5 ... '''.format(_name='zwj',_age=26)
     6 >>> print (info)
     7 
     8 ---------info---------
     9 name:zwj
    10 age:26
    View Code
    >>> print ('name:{name},age:{age}'.format(name='zwj',age=11))
    name:zwj,age:11

     一个可以密文输入的模块getpass

    >>> import getpass
    >>> username = input('username:')
    username:zwj
    >>> password = getpass.getpass('password:')
    password:
    >>> print ('user:%s,pwd:%s'%(username,password))
    user:zwj,pwd:mima
    >>> 

     Python3的keys(), values(), items()返回的都是迭代器,如果需要像Python2一样返回列表,只要传给list就行了(Python2中iteritems()方法同样返回迭代器,Python3中已经移除)

    >>> import sys
    >>> sys.version
    '3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
    >>> d={'k1':'v1','k2':'v2'}
    >>> print (d.keys(),d.values(),d.items())
    dict_keys(['k1', 'k2']) dict_values(['v1', 'v2']) dict_items([('k1', 'v1'), ('k2', 'v2')])
    >>> print (list(d.keys()),list(d.values()),list(d.items()))
    ['k1', 'k2'] ['v1', 'v2'] [('k1', 'v1'), ('k2', 'v2')]
    >>> 
    View Code

    除法运算

    >>> a=3
    >>> b=2
    >>> a/b  #默认浮点
    1.5
    >>> a//b #取整
    1
    >>> a%b
    1
    >>> b%a
    2
    >>> b/a
    0.6666666666666666
    >>> c=5
    >>> d=7
    >>> c//7
    0
    View Code

     一个可以密文输入的模块getpass

    >>> import getpass
    >>> username = input('username:')
    username:zwj
    >>> password = getpass.getpass('password:')
    password:
    >>> print ('user:%s,pwd:%s'%(username,password))
    user:zwj,pwd:mima
    >>> 

     Python3的keys(), values(), items()返回的都是迭代器,如果需要像Python2一样返回列表,只要传给list就行了

    >>> import sys
    >>> sys.version
    '3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
    >>> d={'k1':'v1','k2':'v2'}
    >>> print (d.keys(),d.values(),d.items())
    dict_keys(['k1', 'k2']) dict_values(['v1', 'v2']) dict_items([('k1', 'v1'), ('k2', 'v2')])
    >>> print (list(d.keys()),list(d.values()),list(d.items()))
    ['k1', 'k2'] ['v1', 'v2'] [('k1', 'v1'), ('k2', 'v2')]
    >>> 
    View Code

    除法运算

    >>> a=3
    >>> b=2
    >>> a/b  #默认浮点
    1.5
    >>> a//b #取整
    1
    >>> a%b
    1
    >>> b%a
    2
    >>> b/a
    0.6666666666666666
    >>> c=5
    >>> d=7
    >>> c//7
    0
    View Code

    位运算

    >>> a=60    #60 = 0011 1100 
    >>> b=13    #13 = 0000 1101 
    >>> a&b      #12 = 0000 1100  
    12
    >>> a|b        #61 = 0011 1101 
    61
    >>> a^b        #49 = 00110001
    49
    >>> ~a        #-61 = 1100 0011
    -61
    >>> a<<2    #240 = 1111 0000
    240
    >>> a>>2    #15 = 0000 1111
    15
    >>> 
    View Code

    try/except/else/finaly

    >>> print (1)
    1
    >>> try:
        print (1)
    except:
        print (2)
    else:
        print (3)
    finally:
        print (4)
    
        
    1
    3
    4
    
    ----------------
    >>> try:
        print (x)
    except:
        print (2)
    else:
        print (3)
    finally:
        print (4)
    
        
    2
    4
    View Code

     给字典排序

    from heapq import nsmallest,nlargest
    
    l = [
        {
            'name':'good4',
            'increase': 69,
            'price': 20,
        },
        {
            'name':'good1',
            'increase': 45,
            'price': 20,
        },
        {
            'name':'good2',
            'increase': 59,
            'price': 24,
        },
        {
            'name':'good3',
            'increase': 18,
            'price': 10,
        },
    ]
    
    r = nsmallest(3,l,key=lambda s:(s['price'],s['increase']))
    print(r)
    print(r[0])
    
    r = nlargest(3,l,key=lambda s:(s['price'],s['increase']))
    print(r)
    print(r[0])
    
    
    out:
    [{'price': 10, 'name': 'good3', 'increase': 18}, {'price': 20, 'name': 'good1', 'increase': 45}, {'price': 20, 'name': 'good4', 'increase': 69}]
    {'price': 10, 'name': 'good3', 'increase': 18}
    [{'price': 24, 'name': 'good2', 'increase': 59}, {'price': 20, 'name': 'good4', 'increase': 69}, {'price': 20, 'name': 'good1', 'increase': 45}]
    {'price': 24, 'name': 'good2', 'increase': 59}
  • 相关阅读:
    idea插件-RestfulToolkit
    java 调试技巧
    js foreach 不能中断的现象及理解
    SpringBoot 内嵌Tomcat的默认线程配置
    SpringMVC中从doDispatch如何一步步调用到controller的方法
    干掉hao123的第n+1种方法
    查找——二叉排序树(代码超详细注释)
    为什么单螺旋桨飞机会左偏?
    【转】Python 魔法方法大全
    通俗的讲解Python中的__new__()方法
  • 原文地址:https://www.cnblogs.com/diaosir/p/6224461.html
Copyright © 2011-2022 走看看