向python3进发

时间:2024-04-10 16:07:22

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

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

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

 #python2.x
>>> s = int(raw_input('input:'))
input:a
>>> type(s)
<type 'str'> >>> s = raw_input('input:')
input:
>>> type(s)
<type 'str'>
>>> score = int(raw_input('input:'))
input:
>>> type(score)
<type 'int'> #python3.x (python 3.x 一切皆类型....看type输出.....)
>>> s = int(input('input:'))
input:a
>>> type(s)
<class 'str'> >>> s = input('input:')
input:
>>> type(s)
<class 'str'> >>> score = int(input('input:'))
input:
>>> type(score)
<class 'int'>

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

 >>> info='''
... ---------info---------
... name:{_name}
... age:{_age}
... '''.format(_name='zwj',_age=26)
>>> print (info) ---------info---------
name:zwj
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

一个可以密文输入的模块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

位运算

>>> 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}