Python basic syntax

Source: Internet
Author: User

Python's syntax is very concise, so programs written in Python are readable and easy to understand. This chapter describes the basic syntax and concepts of Python.

python file Types

1, source code. Python's source code extension ends with a py, can be run directly or can be opened or modified with an editor. such as print_hello.py.

2, byte code. Python source files are compiled with PYc suffix files that can be run directly, but cannot be opened or modified with the editor. such as PRINT_HELLO.PYC.

3, optimize the code. The Python source file is optimized with a file extension of pyo. cannot be opened or modified with the editor.

coding specifications for Python

1. Naming rules

The Python language has its own naming conventions, but the naming conventions are not prescribed, but rather a customary usage. Common specifications are as follows:

(1) Variable names, package names, module names (that is, Python file names) are usually lowercase and can use underscores.

(2) The first letter of the class name is capitalized and the object name is lowercase. The properties and methods of the class are prefixed with an object. The private variable and private method of the class are prefixed with two underscores.

(3) The function name is usually lowercase, with an underscore or the first letter capitalized to increase the readability of the name, and the import function is prefixed with the module name.

2, code indentation and colon

  code indentation refers to the way you enter spaces or tabs before each line of code, Represents a hierarchical relationship between each line of code. Code indentation in the programming style is conducive to code reading and understanding, for C, Java syntax, code indentation is only as a good habit of programming, but for Python, code indentation is a syntax. The Python language does not use curly braces or begin...end. Separating blocks of code, instead using colons and code indentation to differentiate between code levels. In accordance with THE PEP Programming specification, it is recommended to indent with 4 space keys. Here's an example:

num = 1if num = =1    :print"", numelse  :    print"",num    = num + 1print  "  ", num

3. Specification of module Import

A module is a collection of classes or functions for dealing with a class of problems. The import of the module is similar to the package import in Java, using the import statement. In Python, the program calls the standard library or the class of the third-party library, in the following ways, we introduce the standard library SYS as an example.

(1) Import sys. Import all classes and functions of the SYS module

(2) from sys import path. Import the path function of the SYS module

(3) from sys import path as A. Import the path function of the SYS module and rename it to a.

4. Comments

The comment symbol for Python is the # sign. The statement python for the comment is skipped. As follows:

# variable naming specification Sumpay == 200
variables and Constants

1, the name of the variable

A variable is made up of numbers, letters, or underscores. The 1th character of a variable must be a letter or an underscore, and other characters can consist of letters, numbers, or underscores.

# correct variable name var_1 = 1= 2

2. Assigning values to variables

Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables. As shown below:

# integer variable # floating-point variables " 1.1 " # String Variables
Data Type

data types are the basis for programming language syntax. Different programming languages have different data types, but they all have several commonly used data types. Python has several built-in data types, namely numbers, strings, tuples, lists, dictionaries. This section focuses on number types, which are described separately after strings, tuples, lists, and dictionary types.

1. Digital

Python's number types are integer, float, Boolean, fractional, and plural. When writing a program using Python, you do not need to declare the type of the variable. The variables are managed by the basic data types built into Python. In the background of the program to implement numeric and type associations, as well as type conversion operations. In Python, you define variables in the following way:

# Defining integer Variables # defining floating-point variables " 1.1 " # Defining string Variables

python automatically determines the type of variable based on the value of the variable, the programmer does not need the backbone of the new variable exactly what type, as long as the creation of a variable to hold a number, the future work is only to operate on this value, Python will be responsible for this number of declaration cycle. If you need to display a view variable type can be obtained by using the type () built-in function, as follows:

>>> num = 1>>> type (num)'int'>>>> num = 1.1>> > type (num)'float'"1.1">>>  Type (str)'str'>
Operators and Expressions

Python's operational symbols include arithmetic operators, relational operators, and logical operators. An expression is a formula that consists of numbers or strings and operators. Expressions are often used to judge the condition of a statement and a loop statement. expression is the basis for learning to control half the blood of a statement. This section describes the use of various expressions in Python.

1. Arithmetic operators and arithmetic expressions

arithmetic operators include arithmetic, modulo and exponentiation operators. The arithmetic operators in Python are: + (plus),-(minus), * (multiplication sign),/(division sign),% (redundancy), * * (exponentiation). An example of an arithmetic expression is as follows:

>>> 1 + 12>>> 2-11>>> 2 * 36>>> 4/22>>>0>>> 1.0/ 2.00.5>>> 3%21>>> 2**38

2. Relational operators and relational expressions

A relational operator is a symbol that compares two objects. The relational operator in Python is:< (less than), <= (less than equals), > (greater than), >= (greater than or equal), = = (equals),! = (not equal to). An example of a relational expression is as follows:

>>> 2>1true>>> 1<=2true>>> 1==2False>>> 1!=2 True

3. Logical operators and Logical expressions

A logical expression is a sentence that is concatenated with logical operators and variables. There are only 3 logical operators of any language, logical and logical, or logical. C, logical operators of Java syntax with &&, | |,! Indicates that Python is represented by an and, or, not. Examples of Python's logical expressions are:

 and TrueFalse  and Falsefalse or falsetrue

Python basic syntax

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.