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

How to learn Python with Zero Foundation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to learn Python with Zero Foundation". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to learn Python with Zero Foundation".

1. Establish a development environment

It is very important to establish a development environment. To do any development, the first thing is to prepare the environment, and then you can try all kinds of things, and in the process of trying, you can gradually build confidence. Beginners are often frustrated by unexpected problems in the configuration of the environment.

Currently, there are several working environments that use Python:

Command line interaction environment: this is a working environment natively supported by Python, which interacts with Python directly on the command line.

Text editing environment: a stripped-down version of the integrated development environment, represented by text editors such as Sublime-Text. This environment retains only the core components that help you write code, mainly for scenarios where you work alone, and focus on writing code without too much division of labor and cooperation.

Integrated development environment: if you want to use Python to write large engineering projects, especially when many people work together, you can consider using a large integrated development environment (IDE) such as PyCharm. In this environment, IDE will integrate many functions needed in development, which is convenient for us to use.

Interactive research environment: in this interactive research environment, each code is equivalent to an increment on the previous code, so as to ensure that only the changed parts are re-executed, and there is no need to recalculate the unchanged parts. It is especially suitable for researchers to quickly verify the correctness of the model.

Beginners generally use the text editing environment, and of course they need to know how to enter code on the command line to interact with Python.

2. Understand the basics of programming language

With the work environment in place, we can begin to write and execute Python programs.

A script like Python is actually like a "movie script" that dictates a series of actions in the order from the back to the back, directing your computer's CPU, hard drive, operating system, and other parts to do this and that. So in order for the computer to understand, you need to write this "movie script" in the language used by the computer. For example, print ("hello world!") Such a sentence will cause the computer to call a series of parts and finally output its hello to the world on the screen, while the sentence "axiom 3: 5" will let the computer calculate the answer of 3: 5 and put the answer in a "box" named a.

So in the early days you learn some grammatical rules for writing "movie scripts", and in the process you will gradually learn some basic elements of the programming language:

Variables: variables are like boxes that store things, to hold temporary values or values that need to be used multiple times. Variables have different basic types, such as numeric and string types.

Operator: variables, as the name implies, can be constantly changed, and the means of change is to operate on variables (or operations). Operator is an intuitive way of data manipulation in Python language. It can operate on one or more variables or constants and return results. For example, the simplest addition, subtraction, multiplication and division have corresponding operators in Python.

Conditional control: during the execution of the Python program, we need to judge what the computer will do next according to the current situation. This is the conditional control statement, which splits the script into two parts, executing the first part in one case and the second part in the rest.

Loop control: there are scripts that we sometimes have to repeat, or they have a large degree of common parts (for example, output 100 integers between 1 and 100, each time the output is very similar), we use loop control statements. We can also break a loop (continue) or jump out of a loop (break).

Using these elements, we can do most of the calculations, you can write some simple programs, or you can use these language elements to command the Python virtual machine on the command line. Of course, if you have learned another language, this part of the foundation is handy, then it is more for you to be familiar with and adapt to the grammar style of Python.

3. Master the foundation of data structure

In order to complete more complex computing scenarios, Python provides several built-in data structures. In the so-called data structure, you can think of a set of variables as organized in a particular way, not just a single independent variable. Through a specific organization, the efficiency of programming can be greatly improved when dealing with some operations. Data structure is a compulsory course for computer majors. You need to learn more advanced data structures and their internal implementation, but Python data structures can be a very good starting point for learning.

The data structures built into Python include the following four types:

List: a list is an ordered and variable-length collection in the syntax of Python, in which elements can be added, deleted, and modified at any time. We can often use lists to represent "ordered" sequences such as queues and arrays.

Tuples: tuples (tuple) and lists are basically the same, both are ordered sequences, the difference is that once the elements in the tuple are defined, they can no longer be modified and can only be accessed through the subscript. Using tuples, we can organically combine some data to represent them.

Set: as a complex data type, Set, like its definition in mathematics, also acts as a "collection" representing a series of values in Python. Using sets, we can quickly determine the existence of an element.

Dictionary: dictionary (Dict) has no corresponding definition in mathematics, it is more like a data type borrowed from daily dictionaries. For example, a mobile phone address book is a typical data set that can be stored using a dictionary. Depending on the dictionary, we can easily find an element according to its ID (such as number, name, etc.).

These four data structures can cover most of the scenarios we work in. Skycode Camp's courses improve more examples. If you are proficient in these four data structures, you will find that programming efficiency will be greatly improved.

4. Master the basic concepts of functions.

In the actual process of writing a program, some code may be executed repeatedly. And these codes don't make any difference except for variables. This code is actually similar to the function f (x) in a mathematical expression, and when we assign a value to x, we get the corresponding result. This feature is also provided in Python, which is also called "function".

Functions modularize code that needs to be used repeatedly, reducing code duplication and increasing readability and maintainability. When you need to make changes, you can complete each change as long as you change the code within the definition.

The syntax for defining a function is actually very simple, and the key point about the function is the parameter definition of the function. The parameter definition of the Python function (that is, how to define the input to the function) is very unique, such as:

Default parameter

Variable parameter

Keyword parameter

Named keyword parameter

In essence, these features make it easier for function callers to pass parameters.

5. Object-oriented programming

Object-oriented is a programming method that is very consistent with human thinking, because the real world is made up of the interaction between objects and objects, so we can easily map the real world to software development. For example, a car, a blog, and a person all correspond to an object in the software system; and the object has its own state and behavior.

Object-oriented: definition of classes

You need to understand a core concept of object-oriented programming: classes. Class describes the state and behavior of a class of objects. Think of a class as a car design diagram, and each car produced according to this car design diagram is an instance of the class, which is called an object.

State and behavior, corresponding to Python language, are the properties and methods of a class and object (the functions in the class are generally called methods). Attribute methods can carry out external access control, from the implementation of the object-oriented feature of "encapsulation".

So when it comes to the programming language Python, you first need to know how to define a class, as well as its properties and methods.

Object-oriented: understanding inheritance and polymorphism

On the basis of "encapsulation", object-oriented programming has the characteristics of "inheritance" and "polymorphism". Suppose we have a class of animals (Animal), which contains some of the most basic features of animals. Now we are going to write a new class of dogs (Dog). Obviously we know that dogs are also a kind of animals, so it must also contain all the characteristics of animals. Therefore, using the "inheritance" feature, we do not need to rewrite all the code of the animal class in the dog, but inherit the animal class when the dog class is created. At this time, we call the animal class the parent class of the dog, and the dog is called the subclass of the animal class. The characteristic of "polymorphism" makes it possible for dogs to rewrite this method when inheriting animal methods, making it more in line with the characteristics of dogs.

Object-oriented programming method is widely used in large-scale projects. Now when people talk about object-oriented programming, it is not only a programming method, but also a programming method to understand what every programmer must do in object-oriented programming.

6. Learn functional programming

Functional programming may seem strange to you, but many styles of functional programming are becoming more and more popular. What is functional programming? In fact, functional programming can be supported as long as the language treats functions as first-class citizens (or with a similar effect with the help of tools). Treating a function as a first-class citizen means that a function can pass parameters, assign values, and return like variables. The writing way of functional programming makes the coding more efficient and greatly improves the production efficiency.

You need to understand some basic concepts such as high-order functions, anonymous functions (lambda expressions), closures, and so on; the best way to understand functional programming is to start with the following high-order functions that are very useful.

Functional programming: understanding the use of higher-order functions

Functional programming:

Map

Map is mostly used for processing list data, using a method for each element of the sequence in turn, such as:

Def f (x): return x*xmap (f, [1pm 2pm 3])

The code will get [f (1), f (2), f (3)], so the result is [1mem4je 9]

Reduce

Using the idea of recursion, reduce takes the result of the previous calculation as the first parameter and the next element as the second parameter:

Def f (x1memx2): return x1*x2reduce (f, [1meme2min3re4])

It is actually equivalent to f (1 ~ 2), 3), 4), and the result is 24.

Filter

Filter will filter the original sequence according to the calculation result of the passed function object f. For an element x, leave the element with f (x) as true (True) and delete the element with f (x) as False

Sort

Sort determines the size relationship between the two numbers based on the result returned by the function, and sorts the elements in the sequence according to them, such as:

Def compare (x, y): return y-x # reverse myList.sort (compare)

The resulting myList will be a sequence arranged from large to small.

Functional programming: lambda expressions

Lambda expressions are anonymous functions that implement the function directly without defining the function name. For example, with the sorted code above, we can do this:

MyList.sort (lambda XBE y: YMZ x)

It is directly defined and used by lambda expressions, so that there is no need to define an additional compare method, and the code is more concise.

Functional programming: closures

In a normal function, the return values are objects such as numbers, strings, and so on. But in fact, the return value of a function can be a function. The returned function contains not only its own code, but also the parameters of the call, and the functions returned multiple times do not interfere with each other, which is called "closure".

The learning of functional programming is not complicated, and learning to use functional programming can often get twice the result with half the effort. Being able to skillfully use Python functional programming is also an ability that every Python programmer should master.

7. Master more modules in the standard library

We all say that Python is easy to use and easy to learn, thanks in large part to the powerful standard library provided by Python. The standard library means that Python has packaged a lot of low-level features for us to use. Therefore, we need to master some basic modules in the standard library.

Input and output: the most "basic" module is the programming of input and output. We all know that we can complete the input and output of data through input () and output (). But what if you want to read a picture? Binary file? Even all kinds of documents in strange formats. At such times, we need to customize this process through IO programming.

Exception handling: the programs we write may also produce a lot of errors, even some of which we expected to produce at design time. But we can't let these errors affect the running of our program. For example, if you use the Python crawler to get 1000 web pages, if the program goes wrong when you start climbing the first web page, then our program will normally terminate automatically, so that subsequent pages will not be crawled. But we can use exception handling to "catch" these errors, record pages that were not successfully acquired, and then continue to work on subsequent pages. In this way, we can safely let the program run for one night, and then come back the next day to deal with the web pages that have generated exceptions.

Multi-process and multi-thread: we can also use multi-process and multi-thread to make our Python programs have the ability to deal with many complex things in parallel. Parallelism is a method that can make full use of the performance of computer computing core (CPU/GPU). At a time when the growth of single-core computing power is gradually slowing down, parallelism is a rare technology that can greatly increase the speed of computing. Imagine that if it takes 20 hours to train a model with a single-core CPU, it is likely to be reduced to less than an hour through a multicore GPU (the GPU itself is parallel). This will greatly increase the efficiency of adjusting the model parameters.

At this point, I believe you have a deeper understanding of "Zero basic how to learn Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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