1, the curly braces in the C language are removed, and the statement block is aligned with the empty glyd. (Space usually 2 or 4, but no limit)
2, to use Chinese in the Py file code, you need to add the following code to the first line:
#-*-Coding:utf-8-*-
Or is:
#coding: Utf-8
3, execute the python file command: >>>python first.py
4. Use the # number for comments.
5, arithmetic operation 1/2 result is 0, 1.0/2 result is 0.5. The two operands are integers and are treated as an integer.
The exponential operator is **,print 2**3, and the result is 8.
6, the variable does not require a declaration type. S= "Hello"; s=5;s=5.0. The same variable can be given different types of values multiple times.
7, Output statement print
# Direct Output string print "Hello World" # Direct output multiple strings print "Hello World", "Python", "Study" # string formatted output print "Let's talk about%s."% My_nam e# integer formatted output print "he ' s%d cm tall."% my_height# output of multiple parameters print "He's got%s eyes and%s hair."% (My_eyes, My_hair)
%r:string (converts any Python object using repr ()).
%s:string (converts any Python object using str ()).
The 8,print statement wraps automatically, and if you don't want to wrap it, you need to add a comma (python2.x) at the end.
Print "Hello", print "World"
9, a line of code is too long, you can add ' \ ' backslash to write at the end of the line.
Print End1 + End2 + end3
10,\n represents the line break. \ is an escape character, and this is consistent with C #. For example: \ "can escape double quotes."
11, three double quotes can be placed in the middle of a multi-line statement block, and do not need to escape.
Print "" "There ' s something going on here. With the three double-quotes. We are able to type as much as we do. Even 4 lines if we want,or 5,or 6. "" "
12, the correct display of special characters in the string, can be escaped with \, or can be enclosed in three quotation marks, this does not need to escape.
13, Input function:
Print "How is old is you?",
Age = Raw_input ()
Equivalent to
Age = Raw_input ("What old is You?")
There is also an input function that requires a Python expression to be entered, and the corresponding type is obtained after the calculation.
The Raw_input function, however, considers the input as a string. We need to transform ourselves, int (raw_input ()) so that we can turn into an integral type.
14, formatted string is%r, not \ r, I made a mistake again.
15, to get the parameters behind the python command, you need to introduce the argv of the SYS module.
The FROM sys import argv# parameter is assigned to the following 3 variables, the first being a Python file name pythonfile,firstarg,secondarg=argv# or assigning all inputs to an array allargument=argv
16, the Print file content is printed fp.read (), not print FP,FP is just a file handle.
17, formatted with%r, the variable is a string, the string will be added to the side of a single quotation mark ' or double quotation marks. If the string is formatted to output to a file, it is better to use%s.
18, function declaration with def.
# * Represents the parameter is indeterminate def print_two (*args): arg1, arg2 = args print "arg1:%r arg2:%r"% (Arg1, arg2) # a parameter def print_one (AR G1): print "arg1:%r"% arg1# no parameter def print_none (): print "I got nothing." Print_two ("Zed", "Shaw") Print_one ("first!") Print_none ()
19, a function can return multiple variables, which is really a bright spot.
Def Secret_formula (started): Jelly_beans = started * jars = jelly_beans/1000 crates = jars/100 ret Urn Jelly_beans, jars, cratesstart_point = 10000beans, jars, crates = Secret_formula (start_point)
20, referring to a function of the module, such as Ex25.py's OpenFile function, if the reference is an import ex25, then call the time Ex25.openfile, the preceding ex25 module name can not be less. If you use the From ex25 import OpenFile way, you can not write ex25 prefix. From ex25 Import *, you can import all functions of ex25, although it is similar to import ex25, but you can avoid writing prefixes.
The 21,pop function removes the element of the corresponding index, even if the function is called, and the array is passed in the same way as C #, which is a reference pass, and the value is still removed.
22, the multi-conditional statement is the If ... elif...else ...
The value of the array returned by 23,range (0,5) is [0,1,2,3,4] and does not contain 5.
24,for statement for I in Range (0,4): Print I
25,while statement while I<5:print i i++
26,exit (0) normal exit procedure. From sys import exit.
The 27,in test element is in a collection.
28, two forward slash \ \ represents a downward divide. 3//2.0 = = 1.0
29,yield can implement iterations and then use it in a for statement, which is the same as in C #.
# yielddef Getodd (max): i = 1 while i < max: if i% 2 = = 1: yield i i + = 1 # notation:python hasn ' T + + operationfor odd in Getodd (): print Odd, ",",
The 30,with statement, in fact, is similar to the using statement in C #.
# with# File object implements the __enter__ and __exit__ method# so we can use Directlywith open (r "E:\temp\py\test.txt") As F:
You can write a class to implement the __enter__ and __exit__ methods, note that __exit__ must have four parameters.
31,try/catch/else/finally
# Try/except/else/finallydef Dividenum (A, B): try: num = A/b raise Nameerror ("Badname") # Raise a error Except Zerodivisionerror: print "Divide by Zero" except Nameerror as e: print "message:%s"% e.message Except: print "There is a error" Raise # re-raise the error else: print "There is no error. num=%r "% num finally: print" This is finally block " print Dividenum (2) Print dividenum (10, 0)
32, to use the variables defined in the module in the function, you need to use the Global keyword in the function for the variables in the module
def change (): global x x = 2 x = 50change () print "x=%d"% x
The result above is 2. Note: If you just access the value of x, it is possible not to add global, but it is not recommended, which is not conducive to code reading.
If the local variable and the module variable have the same name, you can use the built-in function globals to access the module variable
def change (x): globals () [' x '] = 2 x = 50change (x) print "x=%d"% x
33,exec executes the Python statement, Eval evaluates the value of the Python expression, and the result is a variable of type.
# Excute python statement exec ("print ' abc '") exec ("a = 1 + 2") print a# compute python statementn = eval ("1 + 3") # out put <type ' int ' >print type (n)
34, delete an element of an array with the DEL statement.
LST = [1,2,3]print lst# Delete the last Elementdel lst[2]print lst# Delete all the Elementdel lst# the Follew line would be Errorprint LST
,....
Python Basic Notes