Python Learning Notes

Source: Internet
Author: User

Documenting the Python learning process


#! /usr/bin/python

Organization Line-The first two characters of the source file are #!, followed by a program. The function is to tell the user which interpreter to use when executing the program.

Python is case-sensitive, ensuring that there are no spaces or tabs at the beginning of each line of characters match either


Run the program

1. Python test.py used to run the specified path of the Python program

2../test.py used in the current directory of the program

3. Test can change the program to any name in the environment variable directory, or you can run path= $PATH:/home/test Add directory/home/test to the environment

Variables in


Help can be used when you need to get help for a function or statement, press Q to exit the helper information

such as: Help (str)

Help (' print ') to get commands with helpful information when you need to enclose the command single quotation marks


Python has 4 types of numbers

Integer:

Long integers: 122222222222222222222222222222222, integers with large numbers

Floating-point number: 2.11,52.3e-4 E-4 represents 10 of the 4-time Square

Plural: ( -5+4j), (2.3-4.6J)


String

Single quote: ' This is a desk '

Double quotes: "This is a desk" same as single quote

Triple quotation mark "or" ": a string used to represent multiple lines, with the ability to use single and double quotation marks between three quotation marks

Escape character \: Used to represent special characters, such as: \ needs to be represented by \ \, ' What ' s ' needs to be ' what\ ';

In a string, if the row does not have a separate \, the string will continue on the next line instead of starting a new line

Natural string: Used to denote an ordinary string that does not need to be treated with an escape character, specified by a string preceded by R or R. Example: R "What's your name \ n"

Unicode string: Standard method for writing international text, preceded by a string of U or u

The string is immutable, and the literal meaning of the string: ' what\ ' your name ' will automatically turn to ' what ' your name '

It is best to handle regular expressions with natural strings, otherwise you will need to use a lot of \. such as: ' \\1 ' written R ' \1 '


Identifier

is the name used to identify something, the rule is as follows:

The first character must be an English letter (case) or an underscore (_)

The following sections can be made up of letters (case), underscores (_), or numbers (0-9)

Case sensitive

Valid identity name: i,_my_name,name_34

Invalid identity name: 2,5name,my-name

Variables can handle different types of values, and the base type is number and string.


Operator

+ 、-、 *, <, >, <=, >=, = =,! =, not, and, or,/,//(rounding), * * (Power 3**3 i.e.: 3*3*3),% (remainder), "(left)," (Shift right)

To move a number of bits to the left or right by a certain number, because each number is represented in memory as either a bit or a binary number, which is 0 and 1. If the 2<<2 gets 8,--2 is represented as 10 by bits, 2 bits left shifted by 1000, binary number 8), & (with), | (or), ^ (XOR), ~ (bitwise rollover)


Control flow Statements


If statement

If ... :


Elif ... :


Else

Note that the IF statement contains a colon at the end-it tells Python to follow a block of statements, and the elif and else clauses must have a colon at the end of the logical line, followed by a > corresponding block of statements, and another if statement can be used in an if block, and so on- This is called a nested if statement, and the elif and else sections are optional

After Python executes a complete if statement and its associated elif and else clauses, it moves to the next statement in the IF statement block.


While statement

While ... :


Else


When the while loop condition becomes false, the else block is executed-even when the condition is first tested. If the while loop has an else clause, it will begin

is executed, unless your while loop will never end.


For loop

For ...:


Else


All we do is provide two numbers, and range returns the number of a sequence. This sequence starts from the first number to the second number. For example, range (1,5) gives the sequence [1, 2, 3, 4]. By default, range has a step of 1. If we provide a third number for range, then it will be the step size. For example, Range (1,5,2) gives [1,3]. Remember, range extends upward

To the second number, that is, it does not contain a second number.

such as: C expression: for (int 1=0;i<5;i++) to Python would be like this for the I in range (0,5)

Remember that the Else section is optional. If you include else, it is always executed once for the For loop, unless you encounter a break statement


Break statement

Used to terminate a loop statement, that is, even if the loop condition is not called false or the sequence has not been fully recursive, the loop statement is stopped.

If you terminate from a for or while loop, any corresponding loop else blocks will not execute.


Continue statements

Continue

Statements after continue are ignored only when the condition is satisfied, and subsequent statements are executed when the condition is not met. The statement also works for the For loop


Function

Custom functions

def test ():


Calling functions

Test ()


function formal Parameters

The arguments that the function takes are the values that you provide to the function so that the function can do something with these values. These arguments are like variables, except that > their values are defined when we call the function, not within the function itself.

Parameters are specified internally within the parentheses of the function definition, separated by commas. When we call a function, we provide the value in the same way. Note-the parameter name in the letter > number is: formal parameter, and the value you provide to the function call is called: argument.

Example:

#! /usr/bin/python

Def Printmax (A, B): #本例中, A, a, or a parameter

If a > B:

Print a, ' is maximum '

Else

Print B, ' is maximum '


Printmax (3, 4) # directly give literal values


x = 5

y = 7

Printmax (x, y) #调用函数时传递的值则为实参



Python Learning Notes

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.