Python Learning section II-Basics

Source: Internet
Author: User
Tags python list

#!/usr/bin/edv The first line of the Python scripting language, the purpose is to point out that you want the code in your file to run it with what executable program.
#-*-Coding:utf-8-*-coded comments, Python 3 default utf-8 encoding, so you can normally parse Chinese without specifying

Print ("Hello world!")

Python is a high-level scripting language that combines explanatory, compiled, interactive, and object-oriented. After Python is installed, you need to set environment variables in Windows.

Python identifiers

In Python, identifiers are made up of letters, numbers, and underscores.

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore, which is not directly accessible by the _foo, must be accessed through the interface provided by the class and cannot be imported with the FROM XXX import *;

A __foo that begins with a double underscore represents a private member of a class; A __foo__ that begins and ends with a double underscore represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.

Python can display multiple statements on the same line, separated by semicolons ;

Python reserved characters

The following list shows the reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier name.

All Python keywords contain only lowercase letters.

and exec not assert the finally or break for pass class from print continue global raise def if return del import try elif in While else was with except Lambda yield

Line and indent

The biggest difference between learning Python and other languages is that Python's code block does not use curly braces {} to control classes, functions, and other logical judgments. Python's most distinctive feature is the use of indentation to write modules.

The amount of whitespace indented is variable, but all code block statements must contain the same amount of indentation whitespace, which must be strictly enforced .

Multi-line statements

A new line is generally used as the Terminator for a statement in a Python statement.

But we can use a slash (\) to divide a line of statements into multiple lines of display. The statement contains [], {}, or () parentheses do not need to use a multiline connector.

Python Quotes

Python can use quotation marks ( ' ), double quotation marks ( " ), three quotation marks (" ' or "") to denote a string, and the beginning and end of the quotation mark must be of the same type.

Where three quotation marks can be composed of more than one line, writing a shortcut syntax for multiple lines of text, often used in a document string, at a specific location of the file, as a comment.

Python comments

A single-line comment in Python starts with #. A multiline comment in Python uses three single quotation marks ("') or three double quotation marks (" ").

Python Empty Line

A blank line separates between functions or methods of a class, representing the beginning of a new piece of code. The class and function entries are also separated by a line of blank lines to highlight the beginning of the function entry.

Blank lines are different from code indentation, and empty lines are not part of the Python syntax. When you write without inserting a blank line, the Python interpreter runs without errors. However, the purpose of a blank line is to separate the code of two different functions or meanings, which facilitates the maintenance or refactoring of future code.

Remember: Blank lines are also part of your program code.

Wait for user input

The following program executes and waits for user input, and then exits after pressing ENTER:

# ! /usr/bin/env python

#-*-Coding:utf-8-*-

Input ("Press ENTER to exit, any other key displays ... \ n")

In the above code,\ n implements line wrapping. Once the user presses ENTER (enter) key to exit, the other keys are displayed.

Show multiple statements on the same line

Python can use multiple statements in the same row, using semicolons between statements (;) split

Print output

Print default output is a newline, if you want to implement a non-wrapping need to add a comma at the end of the variable ,

Multiple statements form a code group

Indenting the same set of statements constitutes a block of code, which we call the code group.

Compound statements such as if, while, Def, and class, the first line begins with a keyword, ends with a colon (:), and one or more lines of code after that line form the code group.

We refer to the first line and the following code group as a clause (clause).

PythonVariable Type

The value that the variable is stored in memory. This means that there is a space in memory when creating variables.

Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

Therefore, variables can specify different data types, which can store integers, decimals, or characters.

Assigning values to variables

Variable assignments in Python do not require a type declaration.

Each variable is created in memory and includes information about the identity, name, and data of the variable.

Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable.

Standard data types

There are several types of data that can be stored in memory.

For example, a person's age can be stored in numbers, and his name can be stored in characters.

Python defines a number of standard types for storing various types of data.

Python has five standard data types:

    • Numbers (digital)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)
Python numbers

Numeric data types are used to store numeric values.

They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created:

VAR1 = 1
VAR2 = 10

You can also use the DEL statement to delete references to some objects.

The syntax for the DEL statement is:

Del Var1[,var2[,var3[....,varn] []]

You can delete a reference to a single or multiple objects by using the DEL statement. For example:

del var
Del Var_a, Var_b

Python supports four different types of numbers:

    • int (signed integral type)
    • Long (longer integer [can also represent octal and hexadecimal])
    • Float (float type)
    • Complex (plural)
Python string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores.

Generally recorded as:

s= "A1A2 An "(n>=0)

It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to implement a string to get a string, you can use the variable [header subscript: tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail.

Like what:

s = ' Ilovepython '

S[1:5] The result is love.

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.

The result above contains the value L of s[1], and the maximum range taken does not include the upper boundary, or the value p of s[5].

The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation.

Python list

The list is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and strings that can even contain lists (that is, nesting).

The list is identified by [], which is the most common type of composite data for Python.

The cut of the value in the list can also be used to the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.

The plus + is the list join operator, and the asterisk * is a repeating operation.

Python tuples

A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.

Python Dictionary

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.

The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.

Python Data type conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

Python Learning section II-Basics

Related Article

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.