1. Calculator example
#/usr/bin/env python# -*- coding:utf-8 -*-# @time :2018/1/22 21:09# @Author :FengXiaoqing# @file :jsq.pydef 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 ())    &NBSp;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: addition a + b + c  ) print ("2: subtraction a - b - c  ) print ("3: multiplication a * b * c  ) print ("4: division a / b / c ..." ) method = input ("please input number:1/2/3/4: ") if method == "1": string = input ("Please enter expression:") add (String) elif method == "2": string = input (" Please enter an expression: ") reduce (String) elif method == "3": string = input ("Please enter expression:") ride (String) elif method == "4": string = input ("Please enter an expression:") division (String) else: print ("Please input 1/2/3/4 error")
2.tuple tuples
A1 = (1) a2 = (1,) print (A1) print (A2)
Results:
1 (1,)
When a tuple is a unit element, be sure to add "," otherwise unrecognized is a tuple type
Method: M = (1,2,3,4,5,6,7,8,1,34,6,6,3,2) # returns the subscript for the first value element; print (M.index (3)) #统计value元素的个数print (M.count (6))
Results:
23
3.dict Dictionary
Definitions of dictionaries (three ways):
D1 = dict (name = "Fxq", age = +) D2 = {' name ': ' Fxq ', ' age ': 20}d3 = dict ([' Name ', ' Fxq '), (' Age ', ')]) print (D1) print (D2) Print (D3)
Common methods of dictionaries:
Get (k) return k corresponding to the Valuesetdefault (k,v) if K does not exist, set a default value of V, and return V, if K exists, return k corresponding to the Valuekeys () Get all the Keysvalues () Get all the Valuesiteritems () for i,j in D.iteritems ():p rint (i,j) update () M.update (n) m,n are Dictpop (k) Delete k:v corresponding element zip (L1,L2) high-order functions, Dict (Zip (l1,l2)) convert L1 and L2 to Mm = dict (a=1,b=10,c=3 , d=9) print sorted (Mm.iteritems (), key= lambda d:d[1],reverse = true)
4. Other common operations
Help information:
Help () dir ()
Method:
STR () int () list () Dict () tuple () xrange () range () Iteritems () items () input Raw_inputlen () type () isinstance () determine what type , returns a bool Type A = 123print (Isinstance (a,int)) print () enumerate ()
Methods in string:
Find replace split join format StartsWith EndsWith
The differences in Python 2 and 3:
Python 2 print support print S1,S2,S3 just don't return to the line
Python 3 print is packaged into a function, print (s,end "") does not return to the line
Python 2 exists in xrange () range () D.iteritems () D.items
Only range () items () are present in Python 3
A tuple in Python, a dictionary, a calculator code instance, and the difference between Python2 and Python3