Python basic applications

Source: Internet
Author: User

Python Basic Applications

Python is a high-level programming language for interpreting, object-oriented, and dynamic data types. Since the the early 1990s Python language was born, it has been widely used in processing system management tasks and web programming. Python has become one of the most popular programming languages. In January 2011, it was ranked as the 2010 language by the Tiobe programming language leaderboard. Since 2004, Python usage has grown linearly.

Due to the simplicity, readability and extensibility of the Python language, there is an increasing number of research institutions that use Python to make scientific calculations abroad, and some well-known universities have adopted the Python-taught programming curriculum. For example, MIT's introductory Course in computer science and programming is taught in the Python language. Many open source scientific computing packages provide Python's calling interface, such as the famous Computer Vision Library OpenCV, the three-dimensional visual library VTK, and the medical image processing library ITK. Python's dedicated Scientific computing expansion library is more, such as the following 3 very classical scientific extension libraries: NumPy, scipy, and matplotlib, which provide Python with fast array processing, numerical operations, and drawing capabilities, respectively. As a result, the Python language and its many expansion libraries make up the development environment ideal for engineering, scientific researchers working with experimental data, charting, and even developing scientific computing applications.

Python Data Type

Integer: 0x indicates hexadecimal

Floating-point numbers: scientific notation,

Boolean value: True, False, can be used with and, other, and not operations

Null: Represented by none, cannot be represented as 0

String Connection +,* len

Three quotation marks "": three quotation marks, mainly is the article when the line is better. A method that can be used as an article to bulk replace 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. For example:

The output result is

A single-line comment in Python starts with #.

Python variables

A variable is the memory location where the stored value is saved. In other words, when you create a variable, you can keep some space in memory.

Based on the data type of the variable, the interpreter allocates memory and determines what can be stored in the reserved memory. Therefore, by assigning different data types to variables, the data types that can be stored in these variables are integers, decimals, or characters, and so on. In Python, variables do not require explicit declaration types to preserve memory space. When you assign a value to a variable, Python automatically issues the declaration. The equals sign (=) is used to assign a value to a variable

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

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.

In Python the constructor is defined in the form of Def __init__ (self,,,), where it is noteworthy that the underscore is made up of two components that are prone to neglect.

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

Identifiers in Python are case-sensitive.

The same variable can be repeatedly assigned, and can be different types of variables

Python data type: integer int, floating-point float

Do not limit size in Python

Floating-point number has a limit of 17 significant digits

Python string

The string in Python is identified as a continuous character set represented in quotation marks. Python allows double or double quotation marks. You can use the fragment operator ([] and [:]) to get a subset of a string (a substring) whose index starts at index 0 at the beginning of the string, and 1 represents the last character in the string.

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

Python container types: lists and tuples

Lists are the most versatile of the Python composite data types. A list contains items that are separated by commas and enclosed in square brackets ([]). In a way, a list is similar to an array in C. One of the differences between them is that all items in the Python list can be of different data types, whereas arrays in the C language are only of the same type.

The values stored in the list can be accessed using the slice operator ([] and []), starting at 0 at the beginning of the list, and 1 representing the last item in the list. The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operator.

A tuple is another series data type that is very similar to a list. Tuples are separated by multiple values by commas. However, unlike lists, tuples are enclosed in parentheses (()).

The main difference between a list and a tuple is that the list is enclosed in parentheses ([]), the elements and sizes in the list can be changed, and tuples are enclosed in parentheses (()) and cannot be updated. Tuples can be thought of as read-only lists.

Create a list with a [] or list ()

Create an element with () or a tuple

+ Connect two lists * Copy n-times list len () list length in the presence of an element [Start:end:step] is a slice

Continuous sequence generator: Range function

Range (n) sequence from 0 to-1

Range (start,end) sequence from start to End-1

Range (Start,end,step) from start to end-1, step interval step

Python Container type Collection

Collections are unordered combinations of non-repeating elements

Python container type: Dictionary dict

A mapping object maps a hash value to an arbitrary object. The mapping type is a mutable object. There is currently only one standard mapping type, which is the dictionary. numeric types used as key values follow normal numeric comparison rules: If two numbers are equal (for example, 1 and 1.0), they can be used interchangeably to index the same dictionary entry. (note, however, that because computers store floating-point numbers as approximations, it is unwise to use them as key values for a dictionary)
The key value of a dictionary can be almost any value. Include list dictionaries and other mutable types (comparisons are by value rather than the type of the object itself) cannot be a key value.?
A dictionary can be created with a comma-delimited list of key-value pairs (key:value) surrounded by curly braces, for example
such as: {′jack′:4098,′sjoerd′:4127}or{4098:′jack′,4127:′sjoerd′}, or constructed with a dictionary constructor. X

Dict.clear () Delete all the elements in the dictionary

Dict.copy () Returns a copy of the dictionary (shallow copy)

Dict.get (Key,default=none) Returns the value of the key in the dictionary dict, or the value of the default if the key is not present in the dictionary (note that the default value of the parameter is None)

Dict.has_key (key) if the key (key) exists in the dictionary, returns true, otherwise false

Dict.items () Returns a list containing the tuple (key, value) in the dictionary

Dict.keys () Returns a list containing the keys in the dictionary

Dict.values () Returns a list that contains all the values in the dictionary

Dict.pop (key[, default]) is similar to method get () If the key key exists in the dictionary, deletes and returns Dict[key], and throws a Keyerror exception if the key key does not exist and the value of default is not given.

Python basic applications

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.