在Python2.x中,交互输入有input和raw_input两种方法
- input-----------tmd是个坑,就别用
- 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
>>> 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) \n[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‘)] >>>
除法运算
>>> 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
一个可以密文输入的模块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) \n[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‘)] >>>
除法运算
>>> 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
位运算
>>> 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 >>>
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
给字典排序
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}
时间: 2024-11-05 15:53:44