A small collection of Python operations

Source: Internet
Author: User

Hello, everyone! Senior, has not been updated, today run to (water a blog) memo, because some math problems need to use the programming calculation (answer), and then sometimes make lazy don't want to hand calculate, of course, can not do the computer to do the problem but the phone AH ~, in the cell phone c4droid, but not very useful, So decisive Qpython, then have to learn python, but it is easy to forget ... So this article is mainly used for memo, remember some common python operation.

Update lol~~ at irregular intervals

0. Operators:

This is almost the same as C + +, just ++/--these two no, and or non-logical operation into "and or nor", the current use of no difference, but not good, so say ...

(bitwise operations are all possible, Python has a exponentiation operator is ' * * ', this C + + is not)

1. Assignment Value:

Can be assigned directly, but must be pre-declared when used, such as:

A = 1b = 2

What

2. Cycle:

There are for loops, such as:

For I in Range (1,10):

Equivalent to in C + +:

for (int i=1;i<10;i++)

Pay attention to left and right open, note colon!

Allow looping nesting, but note the colon and indent!

Here are just a few simple things to do, such as simplifying the operation of your code like C + + ... Subsequent update lol~

3. Judgment:

There is if/elif/else judgment:

Like what:

A = 2if a<3:print "Less than 3" Elif A==3:print "Equal to 3" Elif A<5:print "bigger than 3 but less than 5" else:print "Bigger than 5 or equal to 5"

Don't forget the colon!

Don't forget to indent!

4. Arrays

Definition of a one-dimensional array:

A = range (100)

On the line ...

This is similar to the C + +:

int a[100];

This can also be the case:

A = []

Give an example:

A = []a.append (1) a.append (1) for I in Range (2,41): A.append (a[i-1]+a[i-2]) for I in range (0,40):p rint A[i]

Note Array subscripts start at 0 and cannot access the array elements that are out of bounds (more secure than C + +)

What about two-dimensional?

Such

A=[]for i in range (0,100): A.append ([]) for J in Range (0,100): a[i].append (0)

Equivalent to in C + +:

int a[100][100];

This will solve the problem of series!

5. Functions

Functions such as C + +, Python functions can be recursive, return values or do not return a value. Can be placed in front of the entire program, or can be placed in the main program

Example of a corner-valley conjecture:

def f (n): If N==1:return 0elif not (n&1): return f (n>>1) +1else:return f (n*3+1) +1print F (99)

Attention Indent!

Almost no, if there is a need for the operation will continue to update!

A small collection of Python operations

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.