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

What do you need to know to get started with Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you what you need to know to get started with Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

What is computer language?

A computer is a machine used for computing. The computer has to do what people tell the computer to do!

Need to control the computer through the computer language (that is, the programming language)!

In fact, there is no essential difference between computer language and human language, the difference is that the subject of communication is different!

The development of computer language has experienced three stages:

1)。 Machine language

Machine languages write programs by binary coding.

The execution is efficient, and it is too troublesome to write

2)。 Symbolic language (assembler)

Use symbols instead of machine codes

When writing a program, you don't need to use binary, but write symbols directly.

After writing, you need to convert symbols into machine code, and then the computer performs the process of converting symbols to machine code called assembly.

The process of converting machine code into symbols is called disassembly

Assembly language is generally only suitable for some hardware, and its compatibility is poor.

3)。 high-level language

The grammar of high-level languages is basically similar to that of today's English, and it is not so closely related to hardware. In other words, we can develop programs in high-level languages that can be executed in different hardware systems.

And high-level languages are easier to learn. Now the languages we know are basically high-level languages such as C, C++, C #, Java, JavaScript, Python and so on.

Compiled language and interpreted language

Computers can only recognize binary codes (machine codes), so any language must be converted to machine code before it is handed over to the computer for execution, that is, machine code like print ('hello') must be converted to machine code like 1010101.

According to the timing of the conversion, languages are divided into two categories:

1)。 Compiled language

Such as: C language

Compiled language, which compiles the code into machine code before the code is executed, and then leaves the machine code to be executed by the computer

Execution process: a (source code)-compile-> b (compiled machine code)

Features:

The execution speed is very fast.

The cross-platform is relatively poor.

2)。 Interpretive language

Such as: Python JS Java

An interpretive language that does not compile code before execution, but compiles while executing at the same time

Execution process: a (source code)-interpreter-> interpretation execution

Features:

The execution speed is slow.

Cross-platform is better.

Let's get to the point and talk about Python.

Introduction of Python

Python is an interpretive language.

Python (British pronunciation: / "pa" θ: n / American pronunciation: / "pa" θ: n /) is a widely used high-level programming language, which is a general-purpose programming language created by Guido Van Rothum. The first edition was released in 1991. You can think of it as an improved LISP that incorporates the advantages of some other programming languages, such as object orientation. As an interpretive language, Python's design philosophy emphasizes the readability of code and concise syntax (especially using space indentation to divide code blocks rather than using curly braces or keywords). Allows developers to express ideas in less code than C++ or Java,Python. Whether it is small or large programs, the language tries to make the structure of the program clear.

The purpose of Python:

WEB application

Facebook Douban

Crawler program

Scientific calculation

Automatic operation and maintenance

Big data (data cleaning)

Cloud Computing

Desktop softwar

artificial intelligence

Construction of Python development environment

The construction of the development environment is to install the interpreter of Python

The interpreter category of Python:

CPython (official)

Python interpreter written in C language

PyPy

Python interpreter written in Python language

IronPython

Python interpreter written in. Net

JPython

Python interpreter written in Java

Steps:

1. Download the installation package python-3.6.exe

two。 Installation (silly installation, default option..)

After the installation, you can see the following programs:

3. Open a command line window and enter python with the following

The interactive interface of Python

When we enter Python through the Windows command line, the interface we enter is the interactive interface of Python

Structure:

Version and copyright notice:

Python 3.6.3 (tags/v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.

Command prompt:

> > >

You can enter Python instructions directly from the command prompt! The instructions entered will be executed immediately by the Python interpreter!

When you install Python, you will automatically install IDLE, a development tool for Python, and you can enter interactive mode through IDLE. But the difference is that in IDLE, you can use the TAB key to view the prompt of the statement.

IDLE is actually an interactive interface, but it can have some simple tips and save the code.

Note: interactive mode can only allow you to enter one line of code, it is to execute one line, so it is not suitable for our daily development!

It can only be used to do some simple daily tests!

We usually write the Python code into a py file and then execute it through the python instruction

The code in the file.

Integration of Python and Sublime

1. Execute the Python code in Sublime and press ctrl + b to automatically execute it in the Sublime built-in console

This mode of execution does not support Chinese well in some versions of Sublime, and the input () function cannot be used.

two。 Use SublimeREPL to run python code

As shown below: select SublimeREPL- > Python to execute the Python code

But it's troublesome. To facilitate the setting of shortcut keys, press f5 to automatically execute the current Python code.

How about setting shortcuts to execute python code as follows?

Find Preferences-- > Key Bindings, and copy the following to the square brackets on the right. And save it.

This allows us to press the F5 shortcut key to automatically execute the current Python code.

{"keys": ["f5"], "caption": "SublimeREPL:Python", "command": "run_existing_window_command", "args": {"id": "repl_python_run", "file": "config/Python/Main.sublime-menu"}}

Several basic Concepts of Python

1. Expression.

An expression is something similar to a mathematical formula.

For example: 10 + 58-4

Generally speaking, expressions only calculate some results and will not have a substantial impact on the program.

If you enter an expression in interactive mode, the interpreter automatically outputs the result of the expression

two。 Statement

In a program, statements generally need to perform certain functions, such as printing information, obtaining information, and assigning values to variables.

For example:

There is no one to answer the question? The editor has created a Python learning exchange QQ group: 857662006 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Print () input () a = 10

The execution of the statement will generally have a certain impact on the program.

The execution result of the statement may not be output in interactive mode

3. Program (program)

A program is made up of a statement and an expression.

4. Function (function)

A function is a statement that is used specifically to perform specific functions.

The length of the function is like: xxx ()

Classification of functions:

1)。 Built-in function

The functions provided by the Python interpreter can be used directly in Python

2)。 Custom function

A function created independently by the programmer

When we need to complete a function, we can call the built-in function or customize the function

Two elements of a function:

1)。 Parameters.

The content in () is the parameter of the function.

There can be no parameters in the function, or there can be multiple parameters, separated by the use of multiple parameters.

2)。 Return value

The return value is the return result of the function, and not all functions have return values.

Basic syntax of Python

Strict case sensitivity in Python

Each line in Python is a statement, and each statement ends with a new line

Each line of statement in Python should not be too long (it is recommended in the specification that each line should not exceed 80 characters)

A statement can be written in multiple lines, and when multiple lines are written, the statement ends with

Python is a strict indentation language, so don't write indentation casually in Python

Use # to express comments in Python. The content after # belongs to comments, and the contents of comments will be ignored by the interpreter.

We can explain the program through comments, and we must form a good habit of writing comments.

Comments are simple and straightforward. It is customary for # to be followed by a space

Literals and variables

Literal quantity is a value of one by one, for example: 1, 2, 3, 4, 5, 6, hello.

The literal quantity means its literal value, which can be used directly in the program.

Variables (variable) variables can be used to hold literals, and the literals stored in variables are variable

The variable itself does not have any meaning, it will express different meanings according to different literals.

Generally speaking, when we develop, we seldom use literals directly. We save literals to variables and refer to literals through variables.

Data type

The data type refers to the worthwhile type of the variable, that is, what values can be assigned to the variable.

In Python, there are several types of data that can be processed directly: integers, floating point numbers, strings, Boolean values, lists, tuples, dictionaries, collections.

We will describe these data types in detail later.

Object (object)

Python is an object-oriented language

Everything is an object!

When the program is running, all the data is stored in memory and then run!

An object is an area of memory dedicated to storing specified data.

An object is actually a container used specifically to store data

As we learned before, numeric values, strings, Boolean values, and None are all objects.

The structure of the object

Three kinds of data are saved in each object.

1)。 Id (identity)

Id is used to identify the uniqueness of objects, and each object has a unique id

The id of an object is the same as a person's certificate number.

You can view the id of an object through the id () function

Id is generated by the parser, and in CPython, id is the memory address of the object

Once an object is created, its id can never be changed

2)。 Type (type)

Type is used to identify the type to which the current object belongs

For example: int str float bool

Type determines what functions the object has.

Check the type of object through the type () function

Python is a strongly typed language. Once an object is created, it cannot be modified.

3)。 Value (value)

A value is the specific data stored in an object.

For some object values can be changed

Objects are divided into two categories, mutable objects and immutable objects

The value of a mutable object can be changed

The value of an immutable object cannot be changed

Variables and objects

The object is not stored directly in the variable, which is more like an alias to the object in Python

What is stored in the variable is not the value of the object, but the id (memory address) of the object

When we use variables, we are actually looking for objects through the object id

Objects stored in a variable are changed only when the variable is reassigned

Variables and variables are independent of each other, and modifying one variable will not affect another.

Type conversion

The so-called type conversion, which converts an object of one type into another.

Type conversion does not change the type of the object itself, but creates a new object based on the value of the current object

Operator (operator)

Operator can operate on one or more values or a variety of operations

For example, +, -, and = are all operators.

Classification of operators:

1. Arithmetic operator

two。 Assignment operator

3. Comparison operator (relational operator)

4. Logical operator

5. Conditional operator (ternary operator)

Thank you for reading! This is the end of this article on "what do you need to know about the introduction to Python"? I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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

Development

Wechat

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

12
Report