Python basic syntax

Source: Internet
Author: User

Reference: http://www.jb51.net/article/50759.htm

1. Note: single-line # comments

Multi-line "' Notes

2.

if:

Elif:

else:

3. Although the variable does not need to be defined beforehand, it must be assigned when it is used, otherwise the error

Local variables:

defF1 (): x=12#Local Variables    PrintxdefF2 (): Y=13#Local Variables    Printydeff3 ():PrintX#Error: The variable x is not defined and does not contradict the "no predefined data type required"    PrintydefMain (): F1 () F2 ( )#f3 () #变量报错Main ()Print 'end2!'

Modifying the value of a global variable

defModifyglobal ():GlobalX#global variable definition    Print 'write x =-1'x=-1defMain ():#Printlocalx ()#printlocaly ()#Readglobal ()Modifyglobal () x=200#y=100Print 'before modified global x=', Xmain ()Print 'After modified global x=',PrintX

Loop statement: With C in the expression of difference, C has while and do......while form; under Python: While with while......else ... Form

 while i<5:     print'Welcome you! '      I=i+1
 while i<5:     print'Welcome you! '      ielse:     print"whileover! "  # Loop Normal End

Note: If the while unhealthy state ends (that is, does not end with a loop condition), the Else statement does not execute.

4. Look at the type function of the variable, type ():

View the memory address function ID () of the variable:

5. Comma operator (,): You can implement connection strings and numeric data.

6. Format control:%f floating-point number,%s string,%d double-precision floating-point number (this is consistent with the output of C).

7. Input function Raw_input (), raw_input () input is the character type.

8. function definition and its invocation:

#define FUNCTION:ADD (function description)defAdd (x, y):#function Head, note Colon, parameter x, yZ=x+y#function Body    returnZ#return value#Define main functiondefMain (): a=12b=13C=add (A, B)#function call, argument A, B    PrintCmain ()#no parameter function callsPrint 'end1!'

Note: The similarities and differences between this part and C are:
1, the form participates in the use of the argument, no parameter function, has the parameter function, the default parameter and so on the rule is consistent.
such as def add (x,y=2), the call can be add (3) or add (3,4), add (x,y=34)
The 2,c parameter needs to specify the data type, which Python does not need.
The return value of 3,python allows for more than one. Such as:

defTest (N1,N2):PrintN1,Printn2 N=n1+n2 M=n1*N2 P=n1-n2 e=n1**N2returnn,m,p,ePrint 'Entry programme1'Sum,multi,plus,powl=test (2,10)#This is a method of assignment that is not in the C language.Print 'sum=', SumPrint 'multi=', MultiPrint 'plus=', plusPrint 'powl=', Powlre=test (2,10)PrintRe#The data type is: ' Tuple 'PrintRe[0],re[1],re[2],re[3]Print 'end1!\n'

Python basic syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.