Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Foundation of Python Development

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Blog structure

Installation of Python

Python development tool IDLE

Python string and list

Python string and list

I. Overview of Python

Python was born in the early 1990 s and was mainly used in scientific computing research institutions in the early days. In recent years, due to the development of Web, big data and artificial intelligence, it has been widely used in system management task processing and Web programming, and has become one of the more popular programming languages.

Language Features of Python

Python is developed in C, but Python no longer has complex data types such as pointers in C. Because of the simplicity of Python, the code of the software is greatly reduced and the development task is further simplified. The programmer's focus is no longer on the syntax features, but on the tasks the program is trying to accomplish. The main features of Python language are as follows.

Preface the tasks to be accomplished. The main features of Python language are as follows.

(1) simple: Python language has fewer keywords, it has no semicolon. Code blocks are separated by spaces or tab indentation, simplifying loop statements. The Python code is simple, short, and easy to read. (2) easy to learn: Python is extremely easy to use because Python has extremely simple documentation. (3) Free, open source: users are free to publish copies of the software and read its source code. (4) High-level language: there is no need to worry about low-level details such as how to manage memory. (5) portability: Python has been ported to many platforms (modified to work on different platforms) (6) explanation: procedures written in Python language do not need to be compiled into binary code, you can run the program directly from the source code. (7) object oriented: Python supports both process oriented programming and object oriented programming. (8) extensibility: Python is developed in C language, so you can use C language to extend Python. (9) embeddability: you can embed Python into the Candleship + program, which enables the program to provide scripting functions to users. (10) Rich libraries: the Python standard library is huge and can help handle a variety of tasks, including regular expressions and document generation. Unit tests, threads. Database. Web browser, CGI.FTP. E-mail. Python language application scenario (1) system programming: it is convenient for system maintenance and management, and is an ideal programming tool for many Linux system administrators. (2) graphic processing: there is PIL. Tkinter and other graphics library support, can facilitate graphics processing. (3) Mathematical processing: the NumPy extension provides a large number of interfaces with standard math libraries. (4) text processing: the re module provided by Python supports regular expressions and also provides SGML. XML analysis module. (5) Database programming, Python can operate Microsoft SQL Server, Oracle. MySQL and other databases. (6) Network programming: provide rich modules, support sockets programming, and develop distributed applications conveniently and quickly. (7) Web programming: can be used as a development language for Web applications. (8) Multimedia applications: Python's PyOpenGL module encapsulates the OpenGL API II. Python version and installation

At present, there are two versions of Python, version 2 and version 3, which are not compatible and have different syntax. Many Python beginners will ask: which version of Python should I learn? The better answer to this question is usually to choose a Python course that works best for you, use which version of Python you use in the course, and then study the differences between different versions.

Download Python

Http:/ / www. Python.org

Install Python

It can always be done by default.

Python development tool IDLE

To learn the Python language, we must first master the development tool IDLE, which can easily run the code and do related debugging, and realize the syntax highlighting of the code. Intelligent functions such as code hints and code completion.

After installing Python, we can launch IDLE. IDLE from the start → IDLE (Python GUI) menu. After startup, the default is the Shell module, and each input-line code will be executed immediately after pressing enter.

IDLE parameter settin

Change the font

Changes can pull commands up and down

Write code using IDLE > import os > os.mkdir ('d:\ www')\\ create a directory IDLE editing mode on the local D disk

You can write a script (write one to create aaa on disk C and save it to run)

IV. Python variables

With a preliminary understanding of the module and use of Python, the following is the learning of Python syntax, starting with the use of the Python variable.

Case study

> xws=1\\ create variable > print xws\\ output variable 1

Note: you cannot use #, the beginning of the number

> print > print >

At the same time, three variables are assigned, which simplifies the code of multiple variable assignment.

Data types commonly used in Python

The built-in data types of Python are numbers, strings, tuples, lists, and dictionaries.

Figures

1. Integer and floating point type

Numeric types include integer, floating-point, Boolean and so on. Variables are managed by the basic data types built in Python, and the association between values and types is realized in the background of the program. And conversion and other operations. According to the value of the variable to automatically determine the type of variable, the programmer does not need to care about the type of variable space, as long as know that the created variable contains a number, the program only operates on this value.

> Xero123 > print x123 > Xero1.1 > print x1.1

The above code first defines the variable xvariant 123. At this time, the value of x is an integer. X is an integer variable. When x is 1. 98, x becomes a floating-point variable again, so we can see that the type of variable can be changed, which is similar to Java. There is a difference between C # language and so on. This is because when Python assigns an existing variable again, it actually creates a new variable. Even if the variable name is the same, but the identity is not the same, the identity of the variable can be output using the id function.

two。 Boolean type

Boolean is used for logical operations and has two values True. False means true or false.

> f = True > > print fTrue > > if (f): print 11 >

The code defines the variable f=True, and if is the judgment statement. If true, the print statement is executed, and the final output is 1, indicating that the statement was executed successfully.

Python operator

The arithmetic operators used in Python are basically the same symbols used in mathematical operations, consisting of +. -. *. / (add, subtract, multiply. Except) and parentheses, the operation order is also multiplication, division and then addition and subtraction, parentheses first. Here are a few examples to illustrate how to use them:

> print > string

The string type in Python is a set of numbers. A collection of letters and symbols used as a whole.

There are three ways to represent strings in Python, single quotation marks and double quotation marks. Three quotation marks, examples are as follows:

> name = 'aaa' > address = "bbb" > xws =' abc''' > print xwsabc

(1) single quotation marks, double quotation marks and triple quotation marks appear in pairs, if they begin with single quotation marks, they should end with single quotation marks, and cannot be mixed to represent strings. The code will report an error.

Other uses of strings

The string of Python can be multiplied by multiplying the string with-- an integer number. For example, multiply the string'a "by the number 3, and the result is the string aaa", which is the same as the string "a" concatenated three times. The code is as follows:

> print 3*'a'aaa > six. List

List is a very important data type in Python and is usually used as the return type of a function. It consists of a set of elements, the list can be added, deleted, and found, and the element values can be modified.

Definition of list

A list is a data structure built into Python, defined by square brackets, elements separated by commas, and the syntax is as follows:

List name = [element 1, element 2.] The value of the list

(1) the data in the list is ordered. It is arranged in the order in which it is defined, and the values of the elements in a certain position can be taken out separately. The syntax is as follows:

List name [index location]

Case study:

> num = ['01] > print num [0] 01 > print num [1] 02 > print num [2] 03 >

A list num is defined, and three strings are stored in the order of 001 strings. 002. 003. When taking a value, use the list name num plus square parentheses, and the number indicates the index position, which should be increased in turn starting with 0.

You can get a set of elements in the specified range of the list, with the following syntax:

List name [start position: end position

You can output the element before the start position to the end position, excluding the end position

> num = ['01BZ] > print num [0:1] [' 01'] > print num [0:2] ['01BZ] [02O2'] > print num [0:3] [] > > modify the element values of the list

The syntax is as follows:

List name [Index location] = value > num= ['01'] > num [0] =' 04' > print num ['04',' 02'] >

When defining a list, the element of index position 0 is "001". After changing its value to "004", the element of index position 0 becomes "004".

Add list element

You can add an element to the end of the list. The syntax is as follows:

List name. Append (elemental value) > num= ['01pr] > > print num [' 01pr] > num.append ('04') > print num [' 01pr] 03', '04'] >

Insert a new element before the specified location in the list, with the following syntax:

List name. Insert (index position, element value) > > num= ['01pc] > num.insert (' 01pc004') > print num ['01pc004'] > delete list elements > num= [' 1fujing2'] > del num [1] > print num ['1'] > > del num\\ > delete the entire list > print numTraceback (most recent call last): File ", line 1 In print numNameError: name 'num' is not defined > > find list elements

The syntax is as follows:

Element value in list name

> num= [] >'1' in numTrue\\ exist >'4' in numFalse\\ does not exist > merge list

Multiple lists can be merged by using the plus sign. > a = ['1fuzhongjing2'] > b = [' 3fujijiang4'] > c=a+b > print c ['1fujingjing2',' 3fujinjing4'] > list of frequently asked questions

(1) Index out of bounds is a common mistake when using a list, such as having three elements in the list. Because the index position is calculated from 0, the maximum index value is 2. 5. If the index value is greater than 2. Indicates that the index is out of bounds and the program cannot be executed. The sample code is as follows:

> bun= > print bun ['01] Traceback [5] Traceback (most recent call last): File ", line 1, in print bun [5] IndexError: list index out of range > > seven. Tuple

Uple, like lists, is also a data structure of Python, which consists of different elements, each of which can store different types of data, such as strings. Numbers, even tuples. However, tuples cannot be modified, that is, tuples cannot be modified after they are created. Tuples usually represent one-row data, while the elements in tuples represent different data items.

Creation of tuples

Tuples are defined by keyword parentheses. Once created, the contents of tuples cannot be modified. The syntax of the definition is as follows:

Tuple name = (element 1, element...) > > num= > > num [0] = '04'Traceback (most recent call last): File ", line 1, in num [0] =' 04'TypeError: 'tuple' object does not support item assignment >

You can see that it cannot be modified.

The difference between elements and lists

Operation of tuple

Tuples can also perform a series of operations on their elements.

Tuples do not allow you to delete element values in tuples, but you can delete entire tuples. The syntax is as follows:

Del tuple name

Tuples and lists can be converted to each other. The syntax for converting tuples to lists is as follows

List (list name) > num = ('1Chronicles)\\ tuple > listnum = list (num)\\ convert to list > print listnum [' 1chronicles] > listnum [0] ='4'\\ modify list > > print listnum ['4codes,' 2'' '3'] > print type (num)\\ output tuple type > print type (listnum)\\ output list type >

The syntax for converting a list to a tuple is as follows:

Tuple (list name) > num= ['3] > > tuplenum = tuple (num) > print type (num) > print type (tuplenum) > eight. Dictionaries

Dictionary (dict) is an important data type in Python. A dictionary is a collection of 'key-value' pairs, and the values in the dictionary are referenced by keys.

The creation of a dictionary

Each element of the dictionary consists of key-value pairs (key-value), with key values separated by colons, key-value pairs separated by commas, and enclosed in curly braces. The key is only--, there can be no more than one, and its value is unordered, the key can be a number, a string, a tuple, or a string as a key. The syntax for definition is as follows:

Dictionary name = {key 1: value 1, key 2: value 2zhangsan':'111','lisi':'222'.) > a = {'zhangsan','111',:'lisi','222'} SyntaxError: invalid syntax3 > > a = {' zhangsan':'111','lisi':'222'} > print a {'lisi':'222',' zhangsan':'111'} > the value operation of the dictionary

The values of dictionaries are different from tuples and lists, which obtain the values of the corresponding positions through numerical indexes, while dictionaries obtain the corresponding values through keys. The syntax for the value is as follows:

Dictionary [key]

> a = {'zhangsan':'111','lisi':'222'} > print a ["zhangsan"] 111 > adding, modifying and deleting dictionaries

To add new elements to the dictionary, you only need to assign values to the new keys, and keys that do not exist in the dictionary will be added automatically. Examples are as follows:

> a = {'zhangsan':'123','lisi':'456'} > a [' zhangsan'] = '321' > print a {'lisi':'456',' zhangsan':' 321'} >

Delete the elements in the dictionary and use the del function. The syntax is as follows:

> del a ['zhangsan'] > > a = {' zhangsan':'1','lisi':'2'} > del a ['zhangsan'] > print a {' lisi':'2'} >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report