Lecture 7:lists and mutability,dictionaries,pseudocode,introduction to efficiency list and variability, dictionary, pseudo Code, efficiency Lists and mutability List and variability
>>> L1 = [123]>>> L2 = L1>>> print L2[123]>>> L1[04>>> print L2[423]
def F(L):l[0] =4L1 = [1,2,3]L2 = [1,2,3]L3 = L1PrintL1 = = L2F (L1)PrintL1 = = L2PrintL1PrintL2PrintL3#输出TrueFalse[4,2,3][1,2,3][4,2,3]
Dictionaries dictionary
3wschool Python Dictionary
- Mutable can be changed.
- Not ordered unordered
- Generalized indexing Index
- Key, value pair key value pair
-
Etof = {' One ':' UN ',' Soccer ':' Football '}Printetof[' Soccer ']Printetof[0]PrintEtofntos = {1:' One ',2:' both ',' One ':1,' both ':2}PrintNtos.keys ()PrintNtos.keysdelntos[' One ']PrintNTOSL = [[' UN ',' One '], [' deux ',' both ']] def keysearch(L, k): forEleminchLifelem[0] = = k:returnelem[1]return NonePrintKeysearch (L,' deux ')# OutputFootballPrintetof[0]keyerror:0{' Soccer ':' Football ',' One ':' UN '}[1,2,' both ',' One ']<built-inchMethod Keys of Dict object at0x7fc52bf0b1e0>{1:' One ',2:' both ',' both ':2}two
Pseudo Code Pseudocode
To seek the hypotenuse of right triangle:
- Input value for base enter bottom edge as float
- Input value for height enter high as float
- sqrt (base**2 + height **2) Compute hypotenuse
- Output value in hypotenuse outputs
ImportMath# Get BaseInputok =False while notInputok:base = input (' Enter base: ')ifType (Base) = = Type (1.0): Inputok =True Else: Print (' Error. Base must is floating point number. ')# Get HeightInputok =False while notInputok:height = input (' Enter height: ')ifType (height) = = Type (1.0): Inputok =True Else: Print (' Error. Height must is floating point number. ') Hyp = math.sqrt (Base * base + Height * height)Print ' Base: '+ STR (base) +', Height: '+ str (height) +', Hyp: '+ STR (HYP)
Improved:
def getfloat(requestmsg, errormsg):Inputok =False while notInputok:val = input (requestmsg)ifType (val) = = Type (1.0): Inputok =True Else: Print (ERRORMSG)returnValbase = GetFloat (' Enter base: ',' error:base must be a float ') height = getfloat (' Enter height: ',' error:height must be a float ') Hyp = math.sqrt (Base * base + Height * height)Print ' Base: '+ STR (base) +', Height: '+ str (height) +', Hyp: '+ STR (HYP)
Efficiency efficiency efficiency–orders of growth
- Choice of algorithm algorithm selection
- Map a problem into a class of algorithms of some efficiency maps the problem to an efficient algorithm
Space & Time & Spaces
- How much memory does it takes to consume as much storage space
- What is the number of the the basic steps needed as a function of the input size the method of calculation has several steps
Random access model
- Best Case–min
- Worst Case–max
- Expected CASE–AVG
MIT Open Class: Python notes 7 list and variability, dictionary, efficiency