a python Basics

a python Basics

First, the way to write the python (there are three)

  1. Edit (subparagraph applies to debug code) directly after the input or python3 python command, and finally use the command:: interacts exit () exit
  2. Run after using the vi editor to edit
  3. After giving execute permissions With vi editor can be run directly (defects: a fixed implementation, but to use python)
    Note: in order to run directly after modifying permissions

Second, comments

  1. Single-line comments

    Start with #, everything on the right as a description, not a real program to be executed, play a supporting role in explanation.

  2. Multi-line comments

    ( '' ') Beginning with three consecutive single quotes, ending in three consecutive single quotation marks (' ''), in the middle of all quoted text play a supporting role explained.

  3. Encoding functions illustrated as interpreter
    Note: (# coding = utf-8 ), if not in the line python2 prints an error message, python3 this line of code can not

Third, variable

  1. Define the variable (python will automatically determine the definition of variables when type)
    Note: The first time was defined variables, use variables are the future

  2. Type of the variable

    print variable type

Fourth, keywords and identifiers

First, the identifier
  • Identified by letters, underscores, and an array, and can not begin with a number
  • python identifier is case sensitive
Second, keyword
  • Keywords are already using python, it does not allow developers to define the same name identifier

    View keyword (python3)

Five, print output

  1. Q: How do all the contents of a one-line print?

  2. print formatted output (see% such operator python is formatted output) when there are multiple variables variables with parentheses to enclose

  3. The two print into a print output

  • In interactive mode: help (print) command may be used to view the print (print automatically added after a character interbank)

Six, input input (received default string)

  • The input python3


    input in python3 is equivalent to the python raw_input, will be calculated in python input
  • Example: two input summing function completions (type of conversion is required)

Seven operators

  • Arithmetic Operators
  • Assignment Operators
  1. Direct assignment
  2. Exchange value
  • Compound assignment operators

    Note:
    A =. 11
    A = + 4-6. 3 ===> A =. 11 (4-6 +. 3)

Eight, if the judge sentences

    格式:if 条件 :(冒号)
  • Example: drunk driving deduction practice

A, if determination logic

  • Example 1: in the 0 to 9, 4 to less than 7, greater than the office award

  • Example 2: Rock Paper Scissors
    NOTE: use the (Random) toolkit

九、 while

A, while circulation
  • Example 1: Find the accumulated even number of 1 to 100 and

Two, while nesting
  • Example 2: preparation of a multiplication table
    (% -2d) represents and occupies two positions Left

Ten, break and continue

  • break: jump directly to the most recent cycle
  • continue: skipping only the times of execution

Eleven, for circulation

  • for circulation and the subscript


    following while equivalent

Twelve, sliced

Refers to a slice taken object manipulation wherein a part of the operation. Strings, lists, tuples support the slicing operation

    格式:[起始 : 结束 : 步长]   步长指在选取区间的间隔

Note: Select the range belongs to the left close right open type, that is, from "Start" to start, to an end before the "end" (end position itself is not included). And these three can be negative

  • Example 1: name = "abcdefg", remove the standard character from 0 to 2
  • Example 2: name = "abcdefg", selected steps of the string 2
  • Example 3: name = "abcdefg", the string reversal
  • Example 4: name = "abcdefg", the string length information acquiring

to sum up

braces in python (:) colon indented plus a tab is equivalent to the C language ({}) flower
python no ++ i and i ++

Guess you like

Origin www.cnblogs.com/luosir520/p/11442686.html