Python Basic Syntax

Source: Internet
Author: User

2017-06-24 09:50:14

One, variable

The requirements for variable names in Python are basically consistent with C + +

(1) can only start with a letter or an underscore, and cannot begin with a number

(2) The other part of the identifier can have a letter glide line and number composition, not allowed to have special characters such as $ and so on (C + + can have $, and can be placed in the first place)

(3) No spaces allowed, no-split

In Python, you only need to assign a value to a variable without having to declare a specific data type, which is different from C + +.

ID (): Get the variable address

Eval (string s): Look in memory for the variable name of s, given his value

Second, the judgment statement using colon and indentation to complete

The colon must be added

If xxxx:

elif xxxx:

else xxxx:

#程序中出现中文会报错, you need to add CODING=GBK.

It is important to note that there are no switch case statements in Python

#CODING=GBK#a Syntaxerror:non-utf-8 code error is a coding problem that needs to be added CODING=GBKifname=="seu":    Print("Southeast University")Else:    Print("Other Universities") name= Input ("Please enter the school:")ifname=="UCB":    Print("Berkeley, California")Else:    Print("Other Universities")

Third, the circular statement

The colon must be added

1. While ...:

2, for...in:

For: In is another looping statement that uses each item in the queue one at a sequence of objects. The Else section is optional. If it contains else, it is always executed once for the end of the for loop, unless a break statement is encountered.

 for  in range (1,5):    print(i)else:    print(  " Loop End ")

Range () generates a sequence that is open and closed, the third parameter is the step size, and the default value is 1

Iv. Operators and expressions

In a different place from C + +:

1, multiplication * can represent two number multiplication, if the string multiplied by the number is to copy the string more than one

2, Power operation * * A**b, indicating A's power of B

3, with or not in English means and or not

Priority level:

Five, function

def xxx (xxx,xxx):

Using the DEF keyword to declare a function, there can be a default value exists in the formal parameter, it is worth noting that C + + is consistent only at the end of the parameter has a default value

To modify the external parameter values, you need to add the global keyword to tell the compiler that this is an external parameter.

Key parameter technology: that is, the direct use of parameter name assignment operation, to avoid the parameter order, the number of parameters of the problem, it appears more convenient

def Fun (x,y=5,z=10):   print("x =", X,"y =  ", y,"z =", Z) fun (3,z=1)

Alternatively, you can use the return statement to return a value that uses pass to represent an empty statement

Six, document string

You can use __DOC__ (note double underline) to call to see the first unassigned string in a function or class, which is commonly used to explain function

def Fun (x,y=5,z=10):   " This function is to output three numbers.  "   print("x =", X,"y =", y ,"z =", z)print(fun.  __doc__)

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.