1.计算器实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#/usr/bin/env python # -*- coding:utf-8 -*- # @time :2018/1/22 21:09 # @Author :FengXiaoqing # @file :jsq.py def add(string):
total = 0
numbers = []
numbers + = string.split( "+" )
for num in numbers:
total + = int (num.strip())
print ( "{0} = {1}" . format (string,total))
def reduce (string):
result = 0
numbers = []
numbers + = string.split( "-" )
result = int (numbers[ 0 ].strip())
numbers.pop( 0 )
for num in numbers:
result - = int (num.strip())
print ( "{0} = {1}" . format (string,result))
def ride(string):
total = 1
numbers = []
numbers + = string.split( "*" )
for num in numbers:
total * = int (num.strip())
print ( "{0} = {1}" . format (string, total))
def division(string):
result = 0
numbers = []
numbers + = string.split( "/" )
result = int (numbers[ 0 ].strip())
numbers.pop( 0 )
for num in numbers:
result / = int (num.strip())
print ( "{0} = {1}" . format (string, result))
if __name__ = = '__main__' :
print ( "####################################################" )
print ( "##################欢迎来到计算中心##################" )
print ( "####################################################" )
print ( "1: 加法 a + b + c ..." )
print ( "2: 减法 a - b - c ..." )
print ( "3: 乘法 a * b * c ..." )
print ( "4: 除法 a / b / c ..." )
method = input ( "Please input number:1/2/3/4: " )
if method = = "1" :
string = input ( "请输入表达式:" )
add(string)
elif method = = "2" :
string = input ( "请输入表达式:" )
reduce (string)
elif method = = "3" :
string = input ( "请输入表达式:" )
ride(string)
elif method = = "4" :
string = input ( "请输入表达式:" )
division(string)
else :
print ( "Please input 1/2/3/4 ERROR" )
|
2.tuple元组
1
2
3
4
|
a1 = ( 1 )
a2 = ( 1 ,)
print (a1)
print (a2)
|
结果:
1
2
|
1 ( 1 ,)
|
Tuple 是单元素时候,一定要加"," 否则无法识别是tuple类型
1
2
3
4
5
6
|
方法: m = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 , 34 , 6 , 6 , 3 , 2 )
# 返回第一个value元素的下标; print (m.index( 3 ))
#统计value元素的个数 print (m.count( 6 ))
|
结果:
1
2
|
2 3 |
3.dict字典
字典的定义(三种方式 ):
1
2
3
4
5
6
|
d1 = dict (name = "fxq" ,age = 20 )
d2 = { 'name' : 'fxq' , 'age' : 20 }
d3 = dict ([( 'name' , 'fxq' ),( 'age' , 20 )])
print (d1)
print (d2)
print (d3)
|
字典的常用方法:
1
2
3
4
5
6
7
8
9
10
|
get(k) 返回K所对应的value setdefault(k,v) 如果k不存在,设置一个默认值v,并返回v,如果k存在,返回k对应的value keys() 获得所有的keys values() 获得所有的values iteritems() for i,j in d.iteritems(): print (i,j)
update() m.update(n) m,n都是 dict
pop(k) 删除k:v 对应的元素 zip (l1,l2) 高阶函数, dict ( zip (l1,l2)) 把l1和l2转换成
mm = dict (a = 1 ,b = 10 ,c = 3 ,d = 9 )
print sorted (mm.iteritems(),key = lambda d:d[ 1 ],reverse = True )
|
4.其他常用操作
帮助信息:
1
2
|
help ()
dir ()
|
方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
str ()
int ()
list ()
dict ()
tuple ()
xrange ()
range ()
iteritems() items() input raw_input len ()
type ()
isinstance () 判断是什么类型,返回一个 bool 类型
a = 123
print ( isinstance (a, int ))
print ()
enumerate ()
|
字符串中方法:
find replace split join format startswith endswith
python 2 和3 中的区别:
python 2 print 支持 print s1,s2,s3 就是不回车在一行
python 3 print 包装成一个函数,print(s,end"") 不回车在一行
python 2 中 存在 xrange() range() d.iteritems() d.items
python 3 中只存在 range() items()
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任
本文转自 枫叶云 51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2064014