Python study note 01: basic concepts, python_01

Source: Internet
Author: User
Tags alphanumeric characters

Python study note 01: basic concepts, python_01

Directory

1. Introduction to Python

2. Common Data Structures in Python

2.1 What is a list?

2.2 What are tuples?

2.3 what is a dictionary?

2.4 index and sharding

3. Other Basic Concepts

3.1 Data Types and variables

3.2 Generator

3.3 iterator

Module 3.4

 

 

1. Introduction to Python

Development History:

Python was designed by Guido van rosum at the National Institute of mathematics and computer science in the Netherlands at the end of 1980s and the beginning of 1990s.

Python itself has evolved from many other languages, including ABC, Modula-3, C, C ++, Algol-68, SmallTalk, Unix shell and other scripting languages.

Like Perl, Python source code also complies with the GPL (GNU General Public License) protocol.

Currently, Python is maintained by a core development team. Guido van rosum still plays a crucial role in guiding its progress.

 

Features:

Python is an interpreted, object-oriented, advanced programming language with dynamic semantics.

To complete the same task, the C language requires 1000 lines of code, Java requires only 100 lines, and Python may only need 20 lines.

 

Function:

What can I do with Python? You can perform routine tasks, such as automatically backing up your MP3 file. You can create websites. many well-known websites, including YouTube, are written in Python. You can also play the background of online games, many online games are developed in Python at the backend. In short, we can do a lot of things.

Python also has something that cannot be done, such as writing an operating system, which can only be written in C language; writing mobile apps can only use Swift/Objective-C (for iPhone) and Java (for Android); write 3D games, preferably C or C ++.

 

What types of applications are suitable for development?

The first choice is network applications, including websites and background services;

Second, there are many gadgets that are commonly needed, including script tasks required by the system administrator;

In addition, programs developed in other languages are encapsulated for ease of use.

Many large websites are developed using Python, such as YouTube, Instagram, and Douban in China. Many large companies, including Google, Yahoo, and even NASA (NASA), use Python extensively.

 

Disadvantages:

The first drawback is that the running speed is slow, which is very slow compared with the C program. Because Python is an interpreted language, your code will be translated into a machine code that the CPU can understand in one line during execution, this translation process is very time-consuming, so it is very slow. The C program is directly compiled into a machine code that can be executed by the CPU before running, so it is very fast.

The second disadvantage is that the Code cannot be encrypted. Release the hosts file. It is impossible to roll out the C code from the machine code. Therefore, all compiled languages do not have this problem, while the interpreted language must publish the source code.

 

 

2. Common Data Structures in Python 2.1 What is a list?

List is the most frequently used data type in Python.

The list can implement the data structure of most collection classes. It supports characters, numbers, strings, and even lists (so-called nesting ).

The list is identified. Is the most common Composite data type in python. You can see this code.

The variable [header Subscript: tail subscript] can also be used to split the list. The corresponding list can be truncated, starting from left to right index with 0 by default, from right to left, the index starts from-1 by default. The subscript can be null to indicate that the header or tail is obtained.

 

The plus sign (+) is a list join operator, and the asterisk (*) is a repeated operation. Example:

#! /Usr/bin/python #-*-coding: UTF-8-*-list = ['abcd', 786, 2.23, 'john', 70.2] tinylist = [123, 'john'] print list # print list of output complete list [0] # print list [] # print list of elements from the second to the third output [2 :] # print tinylist * 2 # print list + tinylist # print the combined list twice

 

Output result of the above instance:

 ['abcd', 786, 2.23, 'john', 70.2] abcd [786, 2.23] [2.23, 'john', 70.2] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.2, 123, 'john']

 

 

2.2 What are tuples?

Tuples are another data type, similar to List ).

The tuples are identified. Internal elements are separated by commas. However, an element cannot be assigned a value twice, which is equivalent to a read-only list.

#! /Usr/bin/python #-*-coding: UTF-8-*-tuple = ('abcd', 786, 2.23, 'john', 70.2) tinytuple = (123, 'john ') print tuple # print tuple [0] # print tuple [] # print tuple [] # print tuple [2:] # print tinytuple * 2, all elements from the third end to the end of the list # print tuple + tinytuple twice output tuples # print the combination of tuples

 

Output result of the above instance:

('abcd', 786, 2.23, 'john', 70.2) abcd (786, 2.23) (2.23, 'john', 70.2) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.2, 123, 'john')

 

The following is invalid because the tuples cannot be updated. The list can be updated:

#! /Usr/bin/python #-*-coding: UTF-8-*-tuple = ('abcd', 786, 2.23, 'john', 70.2) list = ['abcd ', 786, 2.23, 'john', 70.2] tuple [2] = 1000 # list of illegal applications in the tuples [2] = 1000 # list of valid applications

 

 

2.3 what is a dictionary?

A dictionary is the most flexible built-in data structure type in python except the list. A list is a combination of ordered objects, and a dictionary is a collection of unordered objects.

The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.

The dictionary is identified. A dictionary consists of an index (key) and its corresponding value.

#! /Usr/bin/python #-*-coding: UTF-8-*-dict = {} dict ['one'] = "This is one" dict [2] = "This is two" tinydict = {'name ': 'john', 'code': 6734, 'dept ': 'sales'} print dict ['one'] # print dict [2] # print tinydict # print the complete dictionary print with the output key 'one' tinydict. keys () # print all keys. values () # output all values
  2.4 index and sharding

Index:All the elements in the list or tuples are numbered-starting from 0. These elements can be accessed by serial numbers, as shown in:

>>> greeting = ‘Hello’>>> greeting[0]‘H’

 

You can obtain elements through indexes. All lists or tuples can be indexed in this way. When a negative index is used, the number starts from the last element in the list. The first element is-1, and the second element is-2. The number increases sequentially from the right to the left absolute value according to the negative value.

 

Parts:Similar to using indexes to access a single element, you can use the sharding operation to access a certain range of elements. Shards are implemented using two indexes separated by colons:

>>> numbers = [1,2,3,4,5,6,7,8,9,10]>>> numbers[3:6][4,5,6]>>> numbers[0:1][1]

 

In short, the implementation of the sharding operation requires two indexes as the boundary. The 1st indexes are contained in the shard, while the 2nd indexes are not included in the shard.

 

 

3. Other Basic Concepts 3.1 Data Types and variables

Data Type:

Data stored in the memory can be of multiple types.

For example, the person. s age is stored as a numerical value and his or her addresses are stored as alphanumeric characters.

Python has some standard types used to define operations, and they are possible for each of them to store methods.

Python has five standard data types:

Numbers (number)

String (String)

List)

Tuple (tuples)

Dictionary)

Variable:

Variables in Python do not need to be declared. Variable assignment is a process of variable declaration and definition.

Each variable created in the memory contains the variable identifier, name, and data.

Each variable must be assigned a value before it can be used.

Equals sign (=) is used to assign values to variables.

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

 

3.2 Generator

By using the list generation method, we can directly create a list. However, due to memory restrictions, the list capacity must be limited. In addition, creating a list containing 1 million elements not only occupies a large storage space, but if we only need to access the first few elements, the space occupied by the vast majority of elements is wasted.

Therefore, if the list elements can be calculated by some algorithm, can we continue to calculate the subsequent elements in the loop process? In this way, you do not need to create a complete list to save a lot of space. In Python, this type of computing mechanism is called generator.

 

3.3 iterator

Data types that can be directly applied to a for loop include:

One type is set data types, such as list, tuple, dict, set, and str;

The first type is generator, which includes generator and generator function with yield.

 

These objects that can directly act on the for loop are collectively referred to as iteration objects: Iterable.

The generator can not only act on the for loop, but also be continuously called by the next () function and return the next value until the StopIteration error is thrown to indicate that the next value cannot be returned.

The object that can be called by the next () function and continuously return the next value is called the Iterator: Iterator.

Generators are all Iterator objects, but list, dict, and str are Iterable (iteratable objects) but not Iterator.

All objects that can act on the for loop are of the Iterable type;

All objects that can act on the next () function are of the Iterator type. They represent a sequence of inert computing;

Set data types such as list, dict, and str are Iterable but not Iterator. However, you can use the iter () function to obtain an Iterator object.

 

Module 3.4

The module enables you to logically organize your Python code segments.

Assigning relevant code to a module makes your code easier to use and understand.

The module is also a Python object and has random name attributes for binding or reference.

Simply put, a module is a file that saves Python code. The module can define functions, classes, and variables. The module can also contain executable code.

 

 

References:

1. http://www.shouce.ren/api/view/a/4615

2. https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

 

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.