Basic Python syntax

Source: Internet
Author: User

Basic Python syntax

The visual screen for learning the basic python syntax is similar to the matlab syntax. Compared with the C and C ++ languages, it is very simple: no variables or data structures need to be defined, A large number of encapsulated functions are available for calling.

I. Preparations

Compare several statistical tools such as python, R, SPSS, SAS, and matlab:

Both SAS and SPSS are commercial software. The former is expensive, while the latter has no technical content during full-window operations (f );

The matlab data processing function is really good. It takes 1000 hours to use a matrix to process the network of nodes. The advantage lies in drawing and small-scale data processing;

Python and R are fully open-source, with many resources, flexibility, and powerful data processing capabilities.

As a scripting language, Compare python, perl, and ruby:

The classic perl language, as the birthplace of python and perl, seems outdated and obscure (f)

Ruby is similar to python, and its philosophical significance lies in "There are always ways to do it .". Some people commented that ruby in China is suitable for large teams, and it is difficult for small teams to maintain it. Ruby is purely object-oriented.

Python is simple, easy to learn, and readable. Its philosophical significance lies in There shoshould be one -- and preferably only one -- obvious way to do it. python tends to combine process-oriented and object-oriented.

Now that the two are similar, select python to get started.

To learn a new language, I mainly pay attention to the differences between C: 1.Identifier: Whether it can contain special characters. 2. PairCase sensitivity. 3. added or removedData Type. 4.Input and OutputMethod. 5. Is it true?Object-oriented. In addition, the arithmetic logic operation rules are the same; if-else statements, for loops, while loops are similar; function definitions are basically the same.

Differences between object-oriented and process-oriented:

Process-oriented is to analyze the steps required to solve the problem, and then use functions to implement these steps Step by step. You can call these steps one by one. (For step Division issues, call functions in turn)

Object-oriented refers to dividing a problem transaction into various objects. The object is created not to complete a step, but to describe the behavior of a thing in the whole process of solving the problem. (Function division problem, object call function)

Ii. basic grammar knowledge points

(1) identifier: It is defined in the same C language. It must start with a letter or underline and can contain letters, underscores, and numbers.

Note: The identifiers starting with the following line have special meanings.

> Class attributes that cannot be accessed directly starting with a single underscore (_ foo) must be accessed through the interface provided by the class. They cannot be imported using "from xxx import;

> (_ Foo) starting with a double underline represents a private member of the class;

> The (_ foo _) that begins and ends with a double underline represents a special identifier for special methods in python. For example, _ init _ () represents the constructor of the class.

(2) variable assignment:

 

> Simple Value assignment: Variable (Variable) = Value (Value ).

> Multi-variable assignment, Variable1, variable2,... = Value1, Value2 ,...

> Multi-objective assignment, a = B = variable

> Auto-changing value assignment, for example, + =,-=, * =. In the auto-variable assignment, python only calculates the value once, while the normal writing method requires two computations. The auto-variable assignment modifies the original object instead of creating a new object.

Note: The principle of python variables is the same as that of the C language pointer. Modifying the variable value only points to a new address without changing the original address value.

(3) Case sensitivity: Case sensitivity

(4) Notes:#... Comment on a single line; '''... ''' comment on multiple lines.

(4) data types: string, int, float, list, tuple, boolen,

Note: The string can be '',. If the string contains single quotation marks, double quotation marks are required, or single quotation marks in the string are escaped. Line feed with three quotation marks.

Tuple () is used for data, and [] is used for list data.

(5) input and output:

Str1 = raw_input ('input: ') # The input is converted to string storage.

Type (str1) # test variable type

Id (str1) # Return the memory address of the Variable

F = float (str1) # Forced type conversion

Print (format (f, '. 2%') # format the output m. n %, m is the placeholder, and n is the precision.

Printf format (0.3456, '. 2%'), f # multiple outputs are separated by commas (,). The output function brackets can be omitted.

Note: If the placeholder value of the specified output is greater than the valid number of digits, It is right aligned. If the value is smaller than the valid number of digits, The placeholder is ignored and displayed as usual.

(6) Functions

Function Type: internal function, provided by a third party, custom function

Four types of internal function libraries: character function library (import not required), mathematical function library (import math), OS function library (import OS), and network function library (import socket )...

Third-party functions require installation and import

Custom functions:

Def testfunc (a, B, c ):

(Tab) content

The Return Value of the function, which can be an integer, number, or ancestor. A function can be used as a parameter, a dictionary, or a list element.

Real parameter assignment: testfunc (, 3) can be assigned one by one. testfunc (B = 2, a = 1, c = 3) can be assigned to specified parameters ); parameters with predefined values cannot start with parameters without predefined values and can be modified.

(7) if-elif-else, while-else,

If a> 0:

Statement

Else:

If a = 0:

Statement

Else:

 

While 1:

While I <= 5:

Web. open_new_tab (http://www.cadn.com );

I + = 1

Time. sleep (0.8)

Else:

OS. system ('killall/F/IM chrome.exe ')

 

For target in sequences:

Statement

Sequences can be: lists, tuples, strings, files

The number of cycles in for is controlled in the statement.

 

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.