11.Python variables and their use

No matter what programming language to use, the ultimate goal is to process the data. Program in the programming process in order to process data more convenient, generally stores it in a variable.

Vividly see, variables like a small container for data program "dress." In addition to variables, as well as constant, it can also be used to "dress" the data, the difference between them is that once saved after a constant data, the data can not be changed; but the data is stored in a variable may change as many times as procedures for re-assignment to a variable.

Python  equal sign (=) as the assignment operator, for example, a = 20 is an assignment statement, this statement for a variable load 20, the assignment process is called, is about 20 assigned to the variable a.

Note that the variable name is actually identifier, in the name, it is necessary to comply with  Python identifier naming convention , but also to avoid  Python built-in functions and  Python reserved word the same name.

If you come into contact with C, C ++ , the Java  these high-level programming language will find them in the use of variables to store data, you must specify the data type of the variable that can be stored to the C language as an example:

int a = 20

This line of C code mean, a plastic declare a variable to store an integer of 20, which means we can not use a decimal storage, as well as character strings and other types of data, this type of programming language is called a strongly typed language .

And strongly typed language that corresponds to a weakly typed language, Python is one of the typical representative. A weakly typed language has the following two characteristics:

  1. No need to declare variables can be directly assigned to a non-existent variable assignment is equivalent to define a new variable.
  2. Data type of a variable may be dynamically changed: while the same variable may be assigned an integer value, while the string is assigned.


For readers not based programming, you can not really write Python programs, but first open the Python interactive interpreter, in this interactive interpreter "test" Python. First, enter the following in the Python interpreter:

>>> a = 5
>>>

The above code does not generate any output, but a variable is stored in a the interactive interpreter, the variable a is 5.

If we want to see the value of a variable, the variable can be entered directly in the interactive interpreter. For example, here would like to see the value of a variable, you can enter a directly.

>>> a
5
>>>

You see, Python interpreter output variable a value of 5.

Next, we try to change the value of a variable, the new value is assigned to a variable, for example:

>>>a = 'Hello , Charlie'
>>>

This causes the original value of the variable a new value is overwritten, in other words, when the value of the variable is no longer a 5, but rather the string "Hello, Charlie", a type of a character string has become. Now enter a, so that the interactive interpreter displays the value of a:

>>> a
'Hello , Charlie'

If you want to see at this time of a type, you can use Python's type () built-in function.

>>> type(a)
<class 'str'>
>>>

Can be seen, a type is str (a string representing the type).

Figuratively speaking, the equivalent of a magic function of "black box", you can provide "some data, the" black box "data will be processed to the" black box ", this process includes the conversion and output , such as print () is a function, its role is the output of the incoming data. here action type () function of the type of incoming data is for output.


Finally, if you want to interact with the above procedure is converted into a real Python program, as long as each line of code will be entered in a process file, and to print () function to output variables, and finally to save the file as .py the end of the source file.

In an interactive interpreter, as long as the input variable name, the interactive interpreter will output value of a variable, but must be used in the Python program print () function to output variables.

The above interaction process corresponding to the following procedures:

  1. # Define a value type variable
  2. a = 5
  3. print(a)
  4. # Re-assigned to a string variable
  5. a = 'Hello , Charlie'
  6. print(a)
  7. print(type(a))

IDLE command or by using the python "Run-> Run Module" menu item to run the program (the specific operation may read the " first Python program " section), you can see the following output:

5
Hello , Charlie
<type 'str'>

Guess you like

Origin www.cnblogs.com/youqc/p/12066142.html