Python's first knowledge of learning notes

Source: Internet
Author: User

Always heard that the simple use of Python voice and powerful, and today finally can't help borrowing a book, start contact with it, the following combination of books and some of their own experience, write something just touch python, focus on some and C + + has a different place.

(1) Enter input (), output print ()

Note Enter input () in parentheses is the prompt, the return value is the number of inputs

such as A=input ("Please enter a number")

(2) Data type

1) The use of variables in Python does not need to declare variables in advance, can be used directly, this is a bit like matlab, rather than c.

2) Theinteger size represented in Python is limited to the size of the memory and is not a fixed number of bytes . This is so cool, big integer operations don't need to be converted into strings to handle anymore.

3) You can use [] to access an item in a sequence such as a string, but the str type and the basic numeric type are fixed, and the value cannot be changed once set

Such as:

(3) There are some combination data types in Python

such as tuples, lists, where tuples are fixed size, and lists are mutable, creating lists can be used []

(4) all passed variables in Python are passed by reference (unlike in C + + you can select values, pointers, and references)

This is all passed by reference, and parameter passing for fixed types (such as strings, integers, tuples) is actually similar to passing by value in C, while parameter passing for a mutable type (such as a list) is similar to passing by reference in C.

Such as:

(5) Logical operators

1) identity operator is

This is because the Python variables mentioned earlier are passed by reference, so use is to verify that two variables point to the same object.

It is important to note that the difference between a string variable and a normal variable, the so-called reference is actually a "label", the memory of a certain or a piece of data to add a "tag" as an alias, if a variable is assigned to a string that already exists in memory, then memory will not be re-stored again, Other types, such as integers, need to store two copies (this is also true in C + +).

2) comparison operator

chained comparisons can be made in Python (This is not possible in C + +)

If it could be written like this 0<a<=10

3) member operator in, not in

This shows that the representation of a string in Python does not distinguish between single and double quotes

4) logical operator and or not

Note that Python is still using short-circuit logic, but the return value is not a bool type, but instead returns the result (the result refers to the last operand before exiting the logical decision)

(6) Statement block

Python does not use {} as a C + + to differentiate a block of statements, but instead uses the indentation method directly, with 4 spaces for each level of indentation.

(7) Control flow statements

1) if

If condition 1:

Statement 1

Elif Condition 2:

Statement 2

Elif Condition 3:

Statement 3

。。。

Else

Statement n

2) While is almost the same as C + +

3) for...in

4) Exception Handling

Try

Try_suite

Execpt Exception1 as Val1:

Working with statements 1

Execpt Exception2 as Val2:

Working with statements 2

...

(8) Arithmetic operations

Most are similar to C + +, just pay special attention to "/" in Python is not rounded, but to get a decimal

(9) Creation and invocation of functions

function format:

def functionname (Arg1,arg2,...)

Function statements

There is no need to define the return value of the function, if there is a return value, direct return, if not, it is considered to be no return value

(10) Call of the module

Module invocation using: Import module name

The existing module can be called directly using the import, which can be saved as a. py file, and then invoked using import

Here is a small program that generates a random number:

1 ImportRandom2 3 4 defGet_int (msg,min,default):5      whileTrue:6         Try:7Line=input (msg)8             if( notLine andDefault is  notNone):9                 returndefaultTenI=Int (line) One             ifi<min: A                 Print("input must >=min") -             Else: -                 returnI the         exceptValueError as err: -             Print(ERR) -              -  +  -Rows=get_int ("rows", 1, None) +Cols=get_int ("cols", 1, None) AMin=get_int ("min",-100, 0) atMax=get_int ("Max", min,100) -  -  -row=0 -  whilerow<rows: -Col=0 inLine="" -      whilecol<cols: toI=random.randint (Min,max) +s=Str (i) -Line=line+s theLine=line+" " *Col=col+1 $     Print(line)Panax Notoginsengrow=row+1;

Python's first knowledge of 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.