1. array and Variability
When an array is created, it is bound to an object.
L1 = [1, 2, 3] L2 = L1L1[0] = 4 print(L2)#=》[4, 2, 3]
L2 = L1 means that L2 and L1 point to the same object, while L1 [0] = 4 changes the value of the object, so the final L2 value also changes, can be compared with the following example
A = 1 # a points to object 1b = a # B points to object aa = 4 # at this time a points to object 4 print (B) # = "1, because B still points to object 1, so there is no change
2. Dictionary
The dictionary includes the following basic features:
Dictionary:
-Variable (same as array)
-The elements in the dictionary are unordered (the elements in the array are ordered), so they cannot be obtained through subscripts.
-Obtain the <key, value> element through the index.
That is to say, elements can only be obtained through the key value:
# Obtain the dictionary element EtoF = {'one': 'UN', 'soccer ': 'football'} EtoF ['soccer'] # = "'football' EtoF [0] # => wrongEtoF #=>{ 'one': 'UN', 'soccer ': 'football '}
Obtain all key values at a time
# Obtain the key value NtoS = {1: 'one', 2: 'two', 'one': 1, 'two': 2} NtoS. keys () # = "dict_keys (['two', 1, 2, 'one']) # Delete the member del NtoS ['one'] NtoS # = "{'two': 2, 1: 'one', 2: 'two'} whose key value is one '}
Dictionary efficiency is very high, because the hash algorithm enables us to retrieve members in linear time. That is to say, even if the number of dictionary elements increases, efficiency will not be affected.
3. pseudocode
Pseudocode is used to describe the steps for solving the problem
Use pseudocode to describe the steps for straight triangle Oblique Edge length:
From the above example, we can find some features of pseudo code:
Modular.
The basic information of the required value. For example, both B and h are floating point type.
Control flow. The above code is sequential.
Abstract. We didn't specifically mention how to find the square root.
Code Implementation
import math#Get baseinputOK = Falsewhile not inputOK: #if inputOk == False base = input('Enter base: ') if type(base) == type(1.0): inputOK = True else: print('Error. Base must be floating point number.')#Get HeightinputOK = Falsewhile not inputOK: height = input('Enter height: ') if type(height) == type(1.0): inputOK = True else: print('Error. Height must be floating point number.')hyp = math.sqrt(base*base + height*height)print('Base: '+str(base)+',height: '+str(height)+', hyp: '+str(hyp))
It should be noted that the input of the latest python version will convert the user input to the string type, and 2. the X version will maintain the type of the user input, so the above algorithm is not applicable to the latest version.
Of course, let's talk about the code that determines whether the input is valid and re-compile it using the function.
# Determine whether the input data is valid def getFloat (requestMsg, errorMsg): inputOK = False while not inputOK: val = input (requestMsg) if type (val) = type (1.0 ): inputOK = True else: print (errorMsg) return valbase = getFloat ('enter base: ', 'error: base must be a float') height = getFloat ('enter height :', 'error: height must be afloat ')
Separate functions from specific implementations.
You need to develop the following habits:
Use pseudocode to solve the problem first.
4. Code Running Efficiency
Although the computer performance has improved a lot, the complexity of the problem has increased faster. Therefore, we need to improve the code running efficiency. We have two learning objectives:
First, we need to understand efficiency in two dimensions: time and space.
Space: memory usage
Time: the running time of the program.
The simplest way to judge the time is to run the program directly, but this is also the most stupid way. Because time depends not only on the quality of algorithms, but also on the size of input and machine performance, and also on the programming language itself.
In the next lecture, we will focus on this issue.