程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to learn Python from scratch

編輯:Python

from : Micro reading    https://www.weidianyuedu.com

Python What is it? ?

Its builder Guido van Rossum say :

“high-level programming language, and its core design philosophy is all about code readability and a syntax which allows programmers to express concepts in a few lines of code.”

Which translates as :“Python It's a high-level programming language , The core design idea is the readability of the code , For the convenience of programmers, only a few lines of code are needed to express the meaning of the concept ”

Now Python It's data science , machine learning , Deep learning or the mainstream programming language of network programming , It is also a beautiful programming language , So let's learn quickly . If you have a little programming foundation , The next moment will let you know Python The whole picture of .

Basics

Variable variables

simply , A variable is a value .

stay Python in , It's easy to define a variable and give it a value , such as :

Define a variable one, And assign it a value of 1.

Okay , Now you can follow suit , For example, we can define the following variables :

Variable two be equal to 2, some_number be equal to 10000

Variables can have different types , For example, Boolean variables , character string , Floating point numbers, etc :

control flow , Conditional statements Control Flow: conditional statements

if Statement determines what statement should be executed by the authenticity of the expression immediately following it , such as :

If 2 Greater than 1, Just print

2 is greater than 1.

Add else, amount to if The following expression does not hold , We will carry out else, such as :

1 It's definitely better than 2 Small , therefore , We will carry out else The following statements Print

1 is not greater than 2

You can also add elif, It means more than one condition , such as :

loop Looping/iterator

Python There are two ways to cycle in :While perhaps for

while loop , If while The following conditional expression is true, Will loop until the conditional expression is false. such as :

When num Less than or equal to 10 when , Will print , until num be equal to 11 End of cycle .

for loop , There will be a fixed variable interval to cycle , such as :

We go through for The loop realizes the above while Loop the same result . Simple .

list : aggregate | array | data structure List: Collection | Array | Data Structure

Through the above learning, we know that variables can only store one value , If we need to store a lot of values , Like millions of , In addition to defining millions of variables , We can also use columns :

list List Is a collection that can be used to store a list of values , such as :

Here comes your question , So how do you get any of these values , Let's imagine the concept of index , We from 0 Start sequencing , It means the second 0 Values equal to 1, The first value is equal to 2, And so on , stay Python Inside, we get :

If you actually want to save the fruit you want to eat , such as :

Now you know how to define a column , And how to extract a value , Here comes your question again , I want to add one how to add ?

We usually use append To increase , Super easy , such as :

Dictionaries : Key value data structure

Dictionary: Key_Value Data Structure

In some cases you can't just use numbers as an index , Let's look at the dictionary structure , such as :

You can see , The dictionary structure can be composed of key1,key2,key3 As index , Each one corresponds to one Value To pair up , Imagine how we can get each pair of values ?

Let's create a dictionary for the giant panda first , such as :

here name,color,gender,age Constitute the dictionary structure of keys, And its matching yuanyuan,blackwhite,male,2 Constitute the dictionary structure of values.

Here comes your question again , How can we add his partner's name to the panda Dictionary ? It's simple , Direct addition :

iteration : Loop based on data structure iteration:Looping through Data structure

For example, we need to print all my favorite fruits , We can use cycles for:

To print a panda Dictionary , We can also use for loop :

Or you can use key,value:

One thing to be clear here is , It's not necessary to use key The name , You can use any other , For example, we use attr, Equally effective :

Classes and objects Class & Object

Let's have some theory first :

object Object Is the object in the real world , Like cars , house , park , cat , Dogs, etc , The object has two basic characteristics : data Data And behavior Behavior.

The data of the car can be the number of wheels , Number of doors , A few seats, etc , The behavior can be to start , Parking , come on. , Display oil quantity, etc .

In the object-based programming environment , We put the data Data Treat as attribute Attributes, And put the behavior Behavior As a method Methods, A class can be seen as an abstraction of an object , An object is an instance of a class , For example, all cars have the same class , Chevrolet , Ford is two examples of this class .

Let's take a look at it in detail Python How to express , We define a class car:

Let's define an object , Like Ford :

ford Namely car An instance of this class , We talked about the properties of this class earlier , Let's write it out :

here init It's a way , It's a construction method , Now let's instantiate our Ford :

4 A wheel , A gasoline engine ,4 seat , Maximum speed 140 Mai .

We need to continue defining methods , So that we can get a property in the object , This is a behavior of the object , such as :

number_of_wheels Get the number of wheels , set_number_of_wheels Used to set the number of wheels .

stay Python in , We can also use @property To define get and set :

Let's use them , In object ford in :

How simple , You can get the number of wheels , Then we can change it to 2 individual .

Smart, you may have found a slight difference , When we change the number of wheels ,2 Not a parameter , It's a direct assignment , This is a kind of writing pythonic Of setter and getter Code method .

encapsulation : Hidden information Encapsulation: Hiding Information

seeing the name of a thing one thinks of its function , Is to restrict direct access to data and methods in an object , In other words , Is all the internal manifestations of an object , Such as method , data , It's all hidden , Invisible from the outside .

Let's understand public and non-public The difference between instance variables and methods :

Common instance variables Public Instance variables

We can be in a Python Class initializes a public instance variable , such as :

first_name Is a public instance variable ,

If we define it in a class , such as :

Then we don't need to assign values like that , Because all class object instances will be initialized :

Okay , You've learned about simple public instance variables , And class properties , Public instance variables can be modified , This is its characteristic , Be careful , such as :

Non public instance variables Non-public Instance Variable

seeing the name of a thing one thinks of its function , These are defined in the class , Variables that cannot be modified or obtained directly from the outside , There is usually an underscore as the head :

Look for the _email, This is a non-public instance variable , We can also define and use :

It can also be done by means of :

Or just now , We added notes to help you see the differences , Understand the use and limitations of non-public instance variables :

Public methods Public Method

Empathy , Public methods mean that we can use them directly outside the class , such as :

Test it ,

Private method non-Public Method

Like non-public instance variables , Underline the head _, Same attribute , We define private methods as follows :

Invoke this private method in an object :

Private methods cannot be called directly through object names , Can only be passed in the instance method self Call or external through python Supported special methods to call .

Python in , Functions and methods are different . Methods generally refer to functions bound by specific instances , When calling a method through an object , The object itself will be passed as the first parameter , Ordinary functions do not have this feature .

Inherit : Behaviors and characteristics Inheritance:behavior and character

Inheritance means that an object can inherit the methods and properties of its parent object , It's like a son inheriting his father's big eyes , a flat nose , Outstanding height and other characteristics , He also inherited his father's bad temper , Introverted behavior .

In object-oriented programming , One class can inherit the methods and data of another class , such as , Let's go back to the car class above , We define an electric car electric_car A new class of , It can inherit the class of the car car:

For this new class , We do not need to define the existing methods of the parent class , But just use it directly :

To sum up

Python How the variable works

Python Conditional statement

Python The loop statement of

How to use Columns

Dictionary key value set

Loop operation in data structure

Objects and classes

Object properties

Object behavior methods

Python Of getters,setters as well as @property

Encapsulate hidden information

Inherited behavior and characteristics

When you are about Python With a general but relatively basic cognition , You will choose to further study and consolidate a certain knowledge point according to your own needs , So I smoothly enter the beautiful journey of reading other people's code or writing my own code .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved