Python language programming

Source: Internet
Author: User

Python is a high-level scripting language that combines explanatory, compiled, interactive, and object-oriented.

Python 's design is highly readable, with English keywords often used in other languages, some punctuation in other languages, and it has a more distinctive grammatical structure than other languages.

Python is an interpreted language: this means that there is no compilation in the development process. Similar to the PHP and Perl languages.

Python is an interactive language: This means that you can Execute your program in a Python prompt directly interacting with the execution.

Python is an object-oriented language : This means that python supports object-oriented styles or code encapsulation in object programming techniques.

Python is a beginner's language:python is a great language for novice programmers, and it supports a wide range of application development, from simple word processing to WWW browser to gaming.

Python features

1. easy to learn 2. easy to read

2.3. Easy Maintenance 4. a wide range of standard libraries

3.5. Interactive mode 6. Portable 7. Expandable

4.8. database 9.GUI programming . can be embedded

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 starts with a single underscore and is not directly accessible by the _foo is accessed through the interface provided by the class .

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 in Python , such as __init__ () represents the constructor of the class.

Python can display multiple statements on the same line, by using semicolons ; separate, such as:

Python characters

Python Quotes

Python can use quotation marks ('), double quotation marks ( "), three quotation marks ( ' ' or '" " ) to represent a string, 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.

Print output

The print default output is newline, and if you want to implement no wrap, you need to add a comma to the end of the variable:

The result of the execution is:

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, with a colon (:) ends with one or more lines of code following the line constituting the code group.

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

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:

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

The syntax for the DEL statement is:

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

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 Conditional Statements

A Python conditional statement determines the code block that executes by executing the result of one or more statements (True or False).

you can easily understand the execution of conditional statements by :

The Python Program language specifies that any non- 0 and non-null (null) values are true,0 or null to false.

in Python programming , if statements are used in the execution of control programs, in the basic form:

Python Loop Statement

This section will introduce you to Python 's looping statements, which are normally executed sequentially.

Programming languages provide a variety of control structures that allow for more complex execution paths.

Looping statements allow us to execute a statement or group of statements multiple times, following the general form of a looping statement in most programming languages:

Python provides a for Loop and a while loop ( There is no do in python : while loop):

Loop Control Statements

loop control statements can change the order in which statements are executed. Python supports the following loop control statements:

Python Object-oriented

Python has been an object-oriented language since its inception , which is why it is easy to create a class and object in Python. In this section we will detail Python 's object-oriented programming.

If you have not been exposed to object-oriented programming languages before, you may need to first understand some of the basic features of object-oriented languages and form a basic object-oriented concept in your mind that will help you learn more easily. Python 's object-oriented programming.

Let's start with a brief look at some of the basic features of object-oriented.

Introduction to Object-oriented technology

class (Class): used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.

Class variables: Class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.

Data members: Class variables or instance variables are used to manipulate the data related to the class and its instance objects.

method Overrides: If the method inherited from the parent class does not meet the requirements of the subclass, it canbe overridden, which is called the override of the method, also known as the override of the method.

Instance variable: A variable defined in a method that acts only on the class of the current instance.

Inheritance: A derived class (derived class) that inheritsthe fields and methods of thebase class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type Dog derives from the Animal class, which is the analog " is a (is-a )" Relationship (example,Dog is a Animal).

Instantiation: Creates an instance of a class, the concrete object of the class.

Method: A function defined in a class.

Object: an instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.

Create Class

use the class statement to create a new class, followed by the name of the class and ending with a colon, as in the following example :

The help information for a class can be classname.__doc__ view.

Class_suite consists of class members, methods, and data properties.

Python String

string is The most commonly used data type in Python. We can use quotation marks (' or ') to create a string.

Creating a string is simple, as long as you assign a value to the variable. For example:

The result is:

Python lists (list )

sequence is The most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has 6 built-in types of sequences, but the most common are lists and tuples.

Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

in addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

lists are the most common Python data type, which can appear as a comma-separated value within a square bracket.

Data items for a list do not need to have the same type

Create a list by enclosing separate data items separated by commas in square brackets. As shown below:

accessing values in a list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

The result of the above example output:

To delete a list element

You can use the Del statement to delete the elements of a list, as in the following example:

The result of the above example output:

Python Escape Character

Python uses a backslash (\) to escape characters when special characters are needed in characters . As the following table:

Delete a tuple

element values in tuples are not allowed to be deleted, but we can use the Del statement to delete an entire tuple, as in the following example :

when the above instance tuple is deleted, the output variable will have exception information, the output is as follows:

Python language programming

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.