part2: Python variables and simple type, print function introduced, Python keywords, built-in functions Introduction


Python is a weakly typed language, about the weak type has two meanings: 
(1), all variables without declaration can be used, or used for variable assignment from the end is declared the variable;
(2), data variables type can be changed at any time, may be assigned a plurality of times with a variable, and can be assigned numeric value string.

First, the single-line and multi-line comments

comments improve readability for explaining a row or part of the role and function of the program. Further comment is also an important way to debug a program, when debugging may not want to compile code execution commented. Notes can also post your own or other people over a period of time will understand the purpose of the code to help. Reasonable source code comments accounted for about one-third.

Python language can not be inserted in the middle of the string and annotation identifiers , in addition, you may insert null characters, or comment in any other place, for example at the end of one line of code.

Python comments in two forms: single-line and multi-line comments .
(1), single-line comments begin with the # until the end of the line is a comment. Single-line comments in the beginning of a line can also be at the end of a line of code, the source code can also be used commented. In the back row of numbers # code will be ignored interpreter.
(2), multi-line comments can wrap, can comment lines of code. Note the use of multi-line pair of three three single or double quotes .

Annotations Python source code, mainly to do some indication of what the code is to provide some descriptive information to a person, the interpreter will ignore the content of the comment. Add comments is an important way to debug a program, when the debugger, source code can be a row or a few lines of comment or uncomment, run the source code again, you can narrow down the error occurred, help troubleshooting.

Second, variable

Variable data is used to save the program. Variables like a small containers, which is loaded with data. Constant is used for data loading program.

The difference between variable and constant: the constant Once you save a data, which does not change; saved variables may change many times, as long as the procedures for re-assignment to a variable.

Python medium number (=) operator is the assignment operation, such as a = 15 is an assignment statement for a variable of the load 15, 15 is assigned to the variable a.

Two typical features of the Python language is weakly typed:
variables without declaration may direct assignment : a non-existent variable assignment is equivalent to define a new variable.
The data type of the variable can be changed dynamically : the same variable may be assigned a plurality of times, the integer value can be assigned first, second string assignment.

1, a weakly typed language interpretation
at the Python interactive interpreter, input a = 5, a is an integer variable at this time (by type (a) command can be seen). To change the value of a variable, and only need a new value to be assigned to the variable, the new value overrides the original value, for example, a = 'hello michael', this time a value of the variable has changed, and by type (a ) it is also known a type of command into a string.

In the interactive interpreter, you can directly enter a variable name to see the value, but in the code file, you must use the print () function to output variable values in order to see results.

2, print functions described
print () function has many functions, can output a plurality of variables simultaneously. Detailed syntax is as follows:
Print (value, ..., On Sep = '', End = '\ n-', File = sys.stdout, the flush = False)

value parameter can take multiple values, or variables.
default parameters sep spaces, separated by a space showing a plurality of output variables, for example, "|" is delimiters, modify sep = "|".
The default is a newline end parameter indicates print () function output after wrapping, if not wrap, the end parameter can be modified, for example, modify end = "".
parameter specifies file print () function of the output destination, the default value of the parameter file is sys.stdout, represents the standard output of the system, i.e. the screen, if you want output to a file,
can change the parameter file, for example: print ( 'hello world ! ', file = f) where f is an open file pointer.
parameter control output cache flush, the default is False, the default values can be obtained to maintain better performance.

The following sample code to print () function to the output file:
F = Open ( 'michael.txt', 'W', encoding = "UTF-. 8")
Print (, F = File "the Hello World!")
Print ( "go you, world! ", File = f)
f.

Close () . 3, variable naming
Python using the identifier to the variable names, an identifier is used to program variables, classes, methods named symbol identifier needs a valid name .

Identifier with a letter, an underscore (_) at the beginning, the back can have multiple letters, numbers, underscores, letters here can be English letters, Chinese characters . Since Python3 support UTF-8 character set, the identifier may be used herein multilingual UTF-8 characters can be represented. In addition, case sensitive identifier, the identifier is to be noted with the case of the letters.

In Python 2.x version to use Chinese characters or Chinese variable needs to be increased in the first line of the source program # Coding: UTF-8 , but also saved when you save as UTF-8 character set.

The rules for identifiers as follows:
(1), the identifier can be letters, numbers, underscores, but the numbers do not begin with, Chinese characters can be done at the beginning.
(2), the identifier can not be a Python keyword, but can contain keywords.
(3), the identifier can not contain spaces.


In the variable name can not contain spaces, can not start with a number, can contain special characters underscore outside, can not contain other special characters. For example:
valid variable name: michael, mi_chael, _MiChael.
Not valid variable name: # mi Chael, 3michael

4, Python keywords and built-in functions
Python keywords and built-in function names are not recommended for use as variable names. For the following reasons:
(1), if a keyword as a variable name, Python interpreter will complain.
(2), if using a variable name as a built-in function, built-in function of this variable will be covered, the corresponding built-in function can not be used.

Use the command to view all the keywords in Python:
Import keyword # import keyword module
keyword.kwlist # View list of keywords
Keywords are:
False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return,
try, while, with, yield above those keywords can not be used as a variable name.

Python provides built-in functions are:
abs (), all (), any (), basestring (), bin (), bool (), bytearray (), callable (), chr (), classmethod (), cmp (), compile (), complex ( ), delattr (), dict ( ), dir (), divmod (), enumerate (), eval (), execfile (), file (), filter (), float (), fonnat (), frozenset (), getattr (), globals (), hasattr (), hash (), help (), hex (), id (), input (), int (), isinstance (), issubclass (), iter (), len ( ), list (), locals ( ), long (), map (), max (), memoryview (), min (), next (), object (), oct (), open (), ord (), pow (), print (), property (), range (), raw_input (), reduce (), reload (), repr (), reversed (), zip (), round (), set (), setattr ( ), slice (), sorted ( ), staticmethod (), str (), sum (), super (), tuple (), type (), unichr (), unicode (), vars (), xrange (), Zip (), __import __ () , apply (), buffer (), coerce (), intern.

These built-in functions above, partly Python2 version, these built-in function names and do not use as identifiers, or built-in functions it will be overwritten.

Third, the value type

Python supports multiple value types, such as supporting integer, floating point, and the like complex.

1, integer

In Python3, the integer value without distinguishing short, int, long type, is int. In Python2, for a large integer type is long. For large integers, Python does not limit what the greatest integer, then a big integer overflow problem usually does not occur.

Python also supports integer None value (null).

There are four integer value Python representation:
(1), decimal format: The most common integer, i.e. a decimal integer.
(2), in binary form: integer 0b or 0B beginning, an integer of 0 and 1 only.
(3), octal form: integer 0o or the beginning of 0O, an integer from 0 to 7 only eight digits.
(4), in hexadecimal form: an integer of 0x or 0X, there is an integer of 0 ~ 9, a ~ f sixteen.

Example is as follows:
Hex1 = 0x10F # define a hexadecimal integer
hex2 = 0Xcf # define a hexadecimal number integer
print (hex1, hex (hex2) ) # default output will convert a hexadecimal number to decimal, hex () function can be converted into a hexadecimal number

bin_val = 0b101 # define an integer binary number
oct_val = 0o777 # define an integer octal
print (bin_val, oct_val) # same output is converted to a decimal number, is output: 5511

in Python3, it is also possible values (including floating point) increase underscores as a separator, without affecting the value itself underlined . E.g:
one_million = 1_000_000 # one_million actual value is 1000000
NUM = NUM actual value 123_456_789 # 123456789

2. float
floating point number is a number with a decimal point, Python is a floating-point type float, float has two representations in the form of:
(1), decimal format : usually common floating point, for example: 3.14,31.415,0.314 like. There must be a decimal floating point number, otherwise they will be treated as integers.
(2), scientific notation form : e.g. 3.14e3. Only you can use scientific notation in the form of floating-point representation, 31400 is an integer value, but 314e2 is a floating-point value.

In Python, division the divisor is not 0, 0 is not divided. Integer and float value can not be divided by zero.

4, a plurality of
Python supports complex (type complex), the imaginary part of the complex is represented by j or J. To calculate the complex, it may be introduced cmath module, which has various functions to support complex arithmetic.
3.14j = 2 + M1
M2 =. 5 - 1.18j # define two complex numbers M1, M2
Print (M1, type (M1)) is output #: (2 + 3.14j) <class 'Complex'>
Print (M1 + M2 ) # complex arithmetic: (+ 1.96j. 7)

import cmath # import complex arithmetic module cmath
print (cmath.sqrt (-1)) # square root output: 1j
root print (cmath.sqrt (m1)) # can be calculated from the defined

Guess you like

Origin www.cnblogs.com/Micro0623/p/11408557.html