Python2.7 Basic Syntax

Source: Internet
Author: User

1. Keywords

The Python standard library provides the keyword module, which can output all the keywords for the current version:

>>>Importkeyword>>>keyword.kwlist[' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','exec','finally',' for',' from','Global','if','Import','inch',' is','Lambda',' not','or','Pass','Print','Raise','return','Try',' while',' with','yield']>>>

2. Notes:

Single-line comment with #, multiline comment with "' or" "

3. Variables:

Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will be created after the variable is assigned.

    • Python can assign values to multiple variables at the same time, such as a, B = 1, 2.
    • The Numeric division (/) always returns a floating-point number to get the integer using the//operator.
    • A variable can point to different types of objects by assigning values.
    • In mixed calculations, Python converts an integer to a floating-point number

4. String:

The string str in Python is enclosed in single quotation marks (') or double quotation marks ("").

Escape special characters with backslash (\)

Strings can be concatenated with the + operator string, or repeated with the * operator

>>> text='ice'+'  cream'print(text) Ice cream>>> text='ice cream'print(text) ice Creamice Creamice Cream

Use three quotation marks ("..." or "" "..." ") to specify a multiline string

" " AAABBBCCC " " Print (text) AAABBBCCC

>>> text='iccecream'print(text) Iccecream

If you do not want the backslash to escape, you can add an R or R before the string to represent the original string.

>>> Text1 = r'E:\notice'print  text1e:\notice

The

Python string cannot be changed. Assigning a value to an index position causes an error

>>> text='ice cream'>>> text[0]='t'  Traceback (most recent):  '<pyshell#117>' in < Module>    text[0]='t'str '  not support item assignment

5: Three mesh operator

if Else y Print (z)

6. Branch

 whileTrue:score= Int (Input ("Please input your score:"))    if<= Score <= 100:        Print('A')    elifScore >= 80:        Print('B')    elifScore >= 70:        Print('C')    elifScore >= 60:        Print('D')    Else:        Print('Your score is too low')

7. Cycle

The general format for the For loop is as follows:

For <variable> in <sequence>:

<statements>

Else

<statements>

>>> Languaegs = ['C','C + +','Java','python']>>> forLanguageinchLanguaegs:Print(language, Len (language)) ('C', 1)('C + +', 3)('Java', 4)('python', 6)>>>

A looping statement can have an ELSE clause

 forNuminchRange (2, 10):     forXinchRange (2, num):ifnum%x = =0:Print(Num,'equals'X'*', num//x) Break    Else:        #no element found in loop        Print(Num,'Is a prime number')

Cond...

Python2.7 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.