Python Learning-Fundamentals-1

Source: Internet
Author: User
Tags arithmetic operators exit in logical operators naming convention

1. Computer History

Computers use two states of high and low voltages to describe information. The computer can understand that only the binary data, that is, 010100011....,1 bits can represent only 2 states, n bits can represent a state of 2 of the n-th species.

So if you want to describe the weather condition: sunny, rainy, windy, snowy, frost, you need to use 3 bits.

2. Programming language History

The computer can only understand binary data, the binary data contains a lot of instructions, operations, data, the computer through the CPU directly run binary code to perform different functions, binary code is called machine language.

But machine language (binary code) is too readable for humans and too difficult to write, so humans need to use their own language for programming to manipulate computers.

In the beginning, human beings classify and mark the code in the machine language, and use the English words to mark the binary code containing the operation behavior and the data, forming the assembly language. The mode of assembly language is: operation behavior + manipulated number.

The operation behavior and the operands are all using English words instead of a string of 010101 binary code, so that humans only need to write assembly language, and then the assembly language through the previous mapping relationship to the binary can be.

Through assembly language, human beings can easily write code, but also through the conversion to get the corresponding binary code to let the computer run.

However, the assembly language is still poor readability, writing is not very convenient, to achieve a simple function requires a lot of assembly code, code redundancy is still very high.

Thus, the human imitation of the corresponding relationship between the assembly and the binary, the high-level language and the compilation of correspondence, at this time, human beings only need to write high-level language, and then through the compiler to convert the high-level language into assembly or directly into the binary code.

High-level language code is very close to human English, so it is very convenient to use.

Because of grammatical rules and semantics, high-level languages are divided into many kinds, such as: Python,java, C, C + +, Javascript,ruby and so on.

3. Compiled high-level language/explanatory high-level language

High-level language close to human English, writing code is very readable, but the computer can not directly understand and execute the source code, because the computer only know the binary code, so no matter what kind of high-level language, you need to convert the source code into binary code to be executed by the computer.

During the conversion process, there are 2 different situations:

1. Compiled type

Compiled high-level language, before the program executes, you need to advance the source code through the compiler to the target file (binary code), and then the computer directly executes the target file.

Advantages: High efficiency and fast speed of computer execution

Disadvantages:

If the program needs to be modified, the source code must be modified and compiled again, the modification is inconvenient

Once the compilation is complete, the target file will only run on the current operating system and CPU architecture and cannot be used on other platforms unless recompiled

Examples: c, C + +, C #

2. Explanatory type

Interpreted high-level language, the interpreter in real-time read the source code and compiled into two-level code to the computer execution.

Advantages:

Modification of the program directly to the source files can be easily modified

As long as the computer installed the corresponding platform interpreter, the source code can be executed, that is, a source code can be run on different platforms, platform compatibility is good

Disadvantages:

Low efficiency and slow operation because of the need for real-time compilation

Requires a matching version and platform interpreter to work with the source code

Examples: Python, Java, Ruby

4. Python version

Python has 2 mutually incompatible large versions, version 2 up to 2.7, and version 3 to 20180603 up to 3.6.5. Version 2 officially announced will be maintained until 2020, it is recommended to choose version 3 encoding now.

5. Python interpreter

The interpreter is also a program that translates Python source code into machine language. The interpreter can be written in multiple languages, in the following ways:

CPython, the official release of the interpreter, written in C language

Ipython, with a shell on the CPython Foundation, enhances user interactivity

PyPy, it is said that the use of JIT technology dynamic real-time compilation makes the code run faster, but currently immature

Jpython and Icronpython, written in Java and. NET, are generally not used

6. python File Execution

Two ways to execute a python file is to start the interpreter and use the Python source file as a parameter, and the interpreter reads the code in the file. One is to start the interpreter and write code execution in the interactive environment.

7. Variables

1, the reason for the existence of variables

Used to save the results of periodic calculations, while variables are used to describe some of the data in the program, good variable naming and use can increase the readability of the code.

2. Variable naming specification

Variable names can be any combination of numbers, letters, and underscores, note: Special characters other than underscores are not allowed

Variable name cannot start with a number

Variable names cannot be the same as Python predefined keywords

Note: Python can use Chinese as a variable name!

3. Variable definition syntax

Age = 26

Defines a variable whose name is age, which points to an object in memory that has a value of 26

8. Constants

The constant naming convention is the same as the variable, but the constant naming convention is all uppercase.

Constants are used to hold basic immutable data, and Python does not provide a C-like const keyword to enforce immutability of constants, so constants in Python are conventionally immutable.

9. Python installation

www.python.org official website Download the latest version of 3.6.5, according to the computer configuration choose 32 or 64 bit, install the time to choose Add to Path and customize the installation location and other advanced options, installation can be directly into the shell Python interpreter.

10. Input/Output

Python provides shell input through the input function and provides shell printing through the print function.

The input function blocks program execution until it gets the shell input value

11. Comments

Regardless of the high-level language, we encode the ideas, information, steps to simplify the actual code. When the information in the brain is translated into actual code, there is a lot of loss of the amount of data that the code conveys is very small.

So the individual code cannot restore the full amount of information encoded, and we need to use annotations to compensate for the amount of code missing. Excellent annotation + code can provide very high readability, but also convenient for subsequent code use and maintenance.

Note There are several specifications to follow:

1, the content of the note should maintain strong consistency with the code, the comments should be concise, accurate expression of the meaning of the Code, coding background, etc.

2. Can use Chinese or English

3, pay attention to the use of comments, where appropriate and necessary to use the comments, such as: important process nodes, complex code block interpretation, etc.

12. Data type

Computers only know the binary, for the computer, the data has no category, all are 010101010 ...

But for human beings, the data and information in the human world are multifarious, and the process of coding is actually the process of modeling the real world in the computer world, so we need to classify the data of the human world.

The underlying data types are: numbers (integers + decimals), strings, Boolean values

We use int to represent the integer type, float to represent the decimal type

We use STR to denote string types

We use bool to denote a Boolean value type

Numeric types are used for calculations, string types are used to represent information, Boolean types are used to determine

13. Formatted output of string

The so-called formatted output, in fact, is in the coding process to define the presentation template, in the template through the placeholder represents the subsequent corresponding data populated in this.

PLACEHOLDER:

%d integer types

%f Decimal Type

%s String type (%s is a universal match, that is, any data type can be populated into the%s placeholder)

%r original Format (the original format of the data is stored in the%r placeholder, such as the original format of the string containing quotation marks)

14. Operators

Arithmetic operators

+-*/(note that the quotient obtained by the/division operator can be a decimal, i.e. 5/2 = 2.5)

% (the result of the floor is the integer part of the quotient, such as 5//2 = 2. The results obtained by modulo are not evenly divisible, such as 5%2 = 1, and 2 modulo can be used to judge the parity. )

* * (Power operation, 5**2 = 25)

Comparison operators

= = > < >= <=! = (= = Indicates whether the values of both sides of the object are equal, use! = in Python3) is not equal to, no longer uses <>)

Assignment operators

= (assigned value)

+=  -=  *=  /=

//=  %=  **=

(based on the original value of the operation, and assigned to the original variable, such as a = 5, a%= 2, at this time a = 1)

logical operators

And or no (logical judgment uses short-circuit judgment, so: when using and, the probability is small to put in front. When using or, the probability of a big drop in front. )

15. Process Control-Branch

The program defaults to execution code that has no branching from top to bottom. Most of the time we need to decide what code the program will execute next by judging a certain state, condition, and value in the middle of the program's operation.

A branch provides multiple paths of execution, with branches having two branches and multiple branches.

Multiple branches can be divided because there may be many different conditions, and the probability of these conditions should be 1.

Branches need to pay special attention to the logic of conditional judgments, and multiple conditions will only execute one of the conditions, and the conditions are opposite to each other.

By using the If Elif elif else to provide multiple branches, the conditional Boolean results determine whether the condition is true or not.

Note: The conditional judgment should put the probability of large in front, the probability of a small put behind, for the inability to clearly determine the specific conditions can be placed in the else uniform match. Also, when judging the return value of input, note that the input return value must be a string.

16, Process Control-cycle-while

The semantics after the while keyword is: is the condition of the judgment set? If it is established, the body of the loop is executed once again to determine whether the condition is established. If not, skip this loop body and execute subsequent code.

Because loop code has the risk of causing a dead loop, any loop definition requires a pre-designed loop bounce condition. The loop can jump through a break in the loop body, or it cannot be skipped by a while condition. (Of course, you can also exit in the loop body)

By continue you can end the current loop ahead of time, start the next loop while judging, and note that the continue is applied to skip the code that should be executed later.

Because continue can skip subsequent code that should be executed, the subsequent execution code may include a loop-out setting such as: i + = 1. Therefore, the use of continue will have the risk of death cycle, the need to deal with the problem of circular jumping.

17. While...else ...

It is said that the else behind the while is executed if the while is performing normally. If the while does not execute properly if interrupted by a break, else will not be executed. I think it is unreasonable to use else whether or not to execute to determine if the loop is performing properly, consider the following code:

1i = 112  whileI < 10:3     Print('i is:', i)4i + = 15     ifi = = 3:6          Break7 Else:8     Print('loop Normal execution complete')9 Ten #The loop does not execute properly, in fact the loop is not executed at all, and else is executed. So else is used to judge One #The while is performing normally I don't think it's reasonable. 
18, while the use of

Consider the following exercises:

# use while to complete the output of the shape ##  * #  * * #  * * * #  * * * * #  * * * * * #  * * * * #  * * * #  * * #  *
1Star = 12Most_star = 53  whileStar <=Most_star:4     Print(' *'*star)5     ifStar = =Most_star:6          whileMost_star >0:7Most_star-= 18             Print(' *'*Most_star)9          BreakTenStar + = 1 One  Arow = 1 -Most_row = 5 -  whileRow <=Most_row: the     Print(' *'*row) -Row + = 1 -row = Most_row-1 -  whileRow >0: +     Print(' *'*row) -Row-= 1

The while loop can only print One direction, and if it is two in the opposite direction, two while is required. Two while can be written as a second while placed at the end of the first while.

Python Learning-Fundamentals-1

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.