[Python] entry 1

Source: Internet
Author: User

I. Preface: I followed Java, VC ++, C, C #...... And then start with another language.

2. Understand Python

1. Introduction: python is an object-oriented, literal translation computer programming language with rich and powerful class libraries. It is often called the glue language. It can easily connect various modules made in other languages (especially C/C ++.

2. History: as an inheritance of the ABC language, the suffix is. py.

3. Limitations: Forced indent: Indentation indicates the same syntax Block

Single-line statements and command line output problems

4. Development Environment: Python 3.0 syntax is different from Python 2.0

3. Go to Python

1) Hello World

Create test. py and open it in Python Editor

Print ("Hello World") to save

Press F5 or click Run> RUN module.

Display Hello world in the output box

Note: The Python 3.0 compiling environment is different from the python 2.0 compiling environment syntax.

2) interpreter of the python Tool

Enter 3 + 5 and press Enter. Expected result 8 is displayed.

Value x = 5 output x 5

3) syntax Recognition

Example 1:

<Strong> for X in range (5, 10): <br/> Print (x) # indent is required here </strong>Output: 5, 6, 7, 8, 9, 10

Example 2: The range () function indicates the range from 1 to 4.

Range (5) function represents from 0 to 4

<Strong> for N in range (5, 10): <br/> for I in range (1, N): <br/> If I % 2 = 0: <br/> Print (I) <br/> # Break <br/> else: <br/> Print ("not aviable") <br/> </strong>Output: 2, 4, 2, 4, 6 ......

If break is added, 2, 2, 2, and 2 are output. // The result is 2% 2 = 0. This For Loop exits.

Example 3: Use of functions

<Strong> def fib (n): <br/> A, B = 0, 1 <br/> while B <n: <br/> Print (B) <br/> A, B = B, A + B <br/> fib (10) <br/> </strong>Output the Fibonacci sequence

Example 4: default parameters

Def test (name, num = 7, age = 18): <br/> Print ("name:", name) <br/> Print ("Num:", num) <br/> Print ("Age:", age) <br/> test ("tianshuai ") <br/> Print ("**************") <br/> test ("tianshuai",) <br/>Output:Name: tianshuai <br/> num: 7 <br/> Age: 18 <br/> ************* <br/> Name: tianshuai <br/> num: 1 <br/> Age: 22Example 5: The default value is parsed only once. When the default value is a variable object, such as a linked list, Dictionary, or most class instances, there are some differences. For example, the following function accumulates its parameter values in subsequent calls:Def fun (A, L = []): <br/> L. append (a) <br/> return l <br/> Print (fun (1) <br/> Print (fun (2 )) <br/> Print (fun (3) <br/>Output:[1] <br/> [1, 2] <br/> [1, 2, 3]If you do not want to share the default parameter values between different function calls, you can programDef f (a, L = []): <br/> if l is not none: <br/> L = [] <br/> L. append (a) <br/> return l <br/> Print (F (1) <br/> Print (F (2 )) <br/> Print (f (3 ))Output:[1] <br/> [2] <br/> [3] 
Example 6: parameter keywordsDef test (name, num = 7, age = 18): <br/> Print ("name:", name) <br/> Print ("Num:", num) <br/> Print ("Age:", age) <br/> test ("tianshuai ") <br/> Print ("**************") <br/> test ("tianshuai) <br/> Print ("++") <br/> test (num = 1, name = "Xiaowang ") // you do not have to specify the parameter keyword in the original order of parameters to call <br/> Print ("$ ##########$ #") <br/> test (num = 1, age = 20, name = "Xiaoli") <br/>Output:Name: tianshuai <br/> num: 7 <br/> Age: 18 <br/> ************* <br/> Name: tianshuai <br/> num: 1 <br/> Age: 22 <br/> ++ <br/> Name: xiaowang <br/> num: 1 <br/> Age: 18 <br/># ###$ #########< br/> Name: xiaoli <br/> num: 1 <br/> Age: 20









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.