Basic Knowledge of Python Scripts

This section is a tutorial of the Python script language, which only introduces the basic knowledge of the Python scripts.

Data Types and Variables

Start the Python GUI (the shell) and type the following commands to learn some basic data types and data structures of Python.

 

The first command line assigns the numerical value (2+2) to the variable "k". The second command line print k tries to print out the value "k" but fail because of a syntax error. Print is a function and the correct syntax to print(k), which means the variable k must be surrounded with parentheses. The third command line prints the value of the k variable successfully.

Unlike C or Java, you do not need to explicitly declare the type of a variable, such as integer, string, list, or other types. Instead, after the variable is assigned a value, its data type is defined automatically. For example, the command after print (k) assigns a string to the variable "str", then the "str" variable is a string type.

Functions

You need to be familiar with the string data type and its basic functions. For example, the len (str) function counts the length of a string and the str.split() function splits the "str" string by a delimiter. By default, it is a space and returns a list of substrings.

Data Structures

The most used data structure is the list. The str.split() function returns a list of substrings. The a = str.split() command defines the "a" variable as a list. You can print the list by entering the print(a) command.

Another useful data structure is the dictionary. An entry in the dictionary includes a key and a value. Dictionaries are indexed by keys, which can be any immutable types; strings and numbers can always be keys. The dict1[“dog”] command= "steven" assigns the "steven" (string) value to the "dog" key.

Loop

You can use a list or dictionary for loops. For example, type the following commands in the shell UI:

Another common loop control flow statements are while, if, and break. If the if statement is true, the next iteration of the loop continues, otherwise, the break statement breaks off the smallest enclosing for or while loop.

The following example introduces two methods to calculate the sum value from 1 to 100:

The second method uses the range statement and generates a different result from the first method. This is a common error in the range statement. Because range(1,100) means that "i" will loop from 1 to 99, and stop on 100.

If you want to calculate the sum value from 1 to 1000, you have to modify the code lines in the proceeding example accordingly to meet the requirement. To better reuse the code, you can define a function as follows:

 

1.Use the keyword def to declare a function definition, and then type the function name and the parenthesized list of formal parameters.

2.Enter the statement of the function at the next line with indented.

3.Use the return function to return the result. The return function returns Null if no expression arguments are specified or the function fails for some reasons. This is different from other languages where the return function is necessary to exit a function.

4.Use the print function to print the result.

Example

After you have some basic understanding of Python, you can try to write a simple function by yourself.

The following example is a function designed to convert the 6d:22h:59m:17s usage time format of a device to the total seconds format. Click File > New Window from the Python shell UI to open a new window to write the example.

def getAge(age):
      time = age.split(':')
      if (len(time) != 4):
            return 0
      for i in range(0, 4):
            time[i] = time[i].strip('dhms')
      rtn = (int)(time[3]) + ((int)(time[2]) + ((int)(time[1]) + (int)(time[0])*24)*60)*60
      return rtn

To test the function, add the following code lines.

if (__name__ == "__main__"):
    print (getAge("6d:22h:59m:17s"))

When Python runs this example, it will execute the blocks starting from if (__name__ == "__main__").

Tips:

– You can add comment lines starting with the number sign (#) symbol into Python.

– The age.split(" ")  function splits the string by the double quotation (" ") delimiter.

– The time[0] to time[3] are string variables. To convert them into integer variables, put (int) before them.

– Python has strict indent rules. Make sure that the code lines are indented correctly for each control flow or function, otherwise you will get a syntax error.

– The proceeding introduction aims at giving you a quick sense of Python scripts. See http://docs.python.org/2/tutorial/index.html for more in-depth Python tutorials.