Python-02 quick introduction

Source: Internet
Author: User

Here, we will quickly introduce Python's process-oriented programming from the perspective of C language developers.


1> Basic Data Type

Integer, long integer, floating point, and plural.

The first three types are similar to those of C, and the plural number estimation is used for scientific computing. It is not clear what scenarios are used. (Is it used for Fourier transformation? After studying how python can replace Matlab)

Note: The division result of two integers is a floating point number.


Operators not available in C language:

** 2 ** 3 = Power 3 of 2 = 8

// Take the integer part, 2 // 3 = 0,-2 // 3 =-1, 2 //-3 =-1 (negative numbers are somewhat involved, if you are not clear about it, perform multiple tests)


Important skills:

Dir (_ builtins _) displays all built-in functions

Help (function name) displays function help information


Note: python does not need to declare a variable or specify a variable type. You can use a reference pointer to the memory.


2> complex types

2.1 string: ''' three symbols are equivalent (different from Perl)

\ Escape, consistent with the C language.

We recommend that you use '''' to define strings.


Operation:

+ String connection (note that python does not provide the addition of strings and integers. For example, if "123" + 4 returns an error TypeError, you must convert it yourself, for example, "123" + str (4) = '000000 ')

* Duplicate string ('A' * 3 = 'aaa ')

= Whether the string is equal

! = Not equal

<>>=< = Comparison Function

Whether the in string exists. Note that the ''null string exists in any other string ''in 'A'-> True

Len Length

[M: n] Slice. It should be noted that it corresponds to [m, n) in mathematics rather than [m, n] For example, s = "01234" s [] = '1' instead of '12 '.

For other functions, see

Dir (str)


2.2 List

List is equivalent to a Perl array, which is defined.

Note that Python does not have a type distinction, so each item in the array does not need to be of the same type.

For example, the following definition is acceptable.

MyComplexList = [122, 12.5, "123", [1, 2, 3, 3, 3]

It is easy to understand every item to make a reference pointer.

Access: myComplexList [3] [1] returns 2


Common functions:

In

Len

Max

Min

Sum

For more functions, see

Dir (list)

From the perspective of Data Structure, list can be used either as a Stack or as a Queue. Very convenient.


2.3 tuple: unchangeable list

Function reference

Dir (tuple)


2.4 dict

Similar to the perl hash, everyone's favorite.

Mydict = {'key1': value1, 'key2': value2, key3: value3 ,...}

Function reference

Dir (dict)

3. Control Process

3.1 Boolean True or False

And C Language &&

Or C language |

Not C!

You may need to perform an in-depth test on what is True or False.

Intuitively, the following should be False.

Int 0

Float 0.0

Str''

List []

Tuple []

Dict {}

We recommend that you test it before using it. Understanding Boolean is the most important thing for process-oriented programming. Otherwise, the if/for/while statements below are all messy.


3.2. if

If condition:

Elif condition:

Else:

Main: python uses indentation instead of {} to control subprogram blocks.

Each condition must be followed by one:

Elif and else are optional, either or not.


3.2 while

While condition:

...

Else:

...


Note: else is an optional statement. Frankly speaking, we haven't figured out how to use it yet. Is it a well-known goto statement function?

We recommend that you do not use it. One thing is more than one thing.


3.3

For variable in range (...):

...


The range format is as follows:

Start and stop, where start and step are optional. For example, range (10) is equivalent to range (0, 10, 1)


4 Functions

Defining functions in python requires five steps.

Step 1: Design Test Cases

Step 2: Write the function manual

Step 3: Check the function name and variable name.

Step 4: Write Functions

Step 5: Test

For example, a simple add function


Def myadd (leftValue, rightValue ):

''' (Int, int)-> int

This function adds two integers. The input parameter must be an integer.

>>> Myadd (3, 5)

8

'''

Return leftValue + rightValue

By now, python can do most of the things that the C language can do.


5. Download the tool:

Python: http://www.python.org/downloads/

Python: http://www.jetbrains.com/pycharm/download/

Pythonwin: http://sourceforge.net/projects/pywin32/files/


Let's Go !!!




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.