Python Learning Note one

Source: Internet
Author: User

I. Programming language classification

Programming language is the medium of communication between programmers and computers, and programmers write their own thought processes according to the grammatical style of programming language. There is no difference between a program and a normal file before it is run, and the characters written in the file characters have specific grammatical meanings only at run time.

Programming languages are divided into: machine language, assembly language and high-level languages according to the development process.

Machine language: Stand on the computer's point of view, using the computer can understand the binary programming, directly manipulate the hardware. The advantage is the lowest level and the fastest execution speed. The disadvantage is the most complex, the development efficiency is lowest.

Assembly language: Stand on the computer's point of view, the use of English identifiers instead of binary programming, the essence is still directly operating hardware. The advantage is the lower level, the fastest execution speed. The disadvantage is the complexity, the development efficiency is lowest.

High-level language: Standing on the human side, programming with human language characters, no longer directly manipulating hardware.

According to the compilation method is divided into: compiled and interpreted type.

Compiled: After the program is written, the compiler is executed, the program is translated into machine language, and then the program is executed directly in the machine language. The advantage is that execution is fast and does not depend on the language environment to run. The disadvantage is debugging trouble, cross platform difference.

Interpreted: translates the program into machine language at run time, so the speed is slower than the compiled language. The advantages are convenient debugging and good cross-platform. The disadvantage is that the execution speed is slow and relies on the interpreter to run.

Learning difficulty from high to low, execution efficiency from high to low, development efficiency from low to high. Speed is not the key (bottleneck theory), development efficiency is kingly.

Two. Introduction to Python

Python is an interpreted language that needs to be executed through a Python interpreter.

Python3 test.py Program Execution Process:

1. Start Python3.exe First

2. Read the test.py from the hard disk into memory.

3, explain the execution of the file content (recognize Python syntax)

Three. Variables

1, the change refers to changes, the volume refers to a certain state. The essence of program execution is the change of a series of States, which is the most direct embodiment of the program, and uses variables to save the state and state changes of the program execution.

2. Variable names can only be any combination of letters, numbers, and underscores, but a character cannot be a number, and a variable cannot be named with a keyword.

3, the variable naming method includes hump type and slide line type, hump type is numberofstudents=50, underline type is number_of_students=50.

4, y=x= ' AA ' the reference counter of ' AA ' is 2, when the reference counter is 0 o'clock, Python automatically reclaims memory.

5. The three elements of a variable are the variable value, the type of the variable, and the ID number of the variable.

Iv. user interaction with the program

Using input in Python3, you can accept any value entered by the user and convert the string type.

Input in Python2, what type the user enters, and what type it is stored in.

The Raw_input function in Python2 is the same as the input in Python3.

A single-line comment in Python uses ' # ', and a multiline comment uses "" "" "". Important sections add comments to make it easier for others to read code

V. Basic data types

1, int integral type num=5

2, float float type speed=4.3

3. String country= a bunch of characters defined in the "China" single or double quotation marks.

Between string and string + alive *, use "+" to stitch strings, using * repeatable strings.

4, list, in [] separated by a comma, storing multiple arbitrary types of elements.

info = [' abc ', 123,[' xxx ', 987]]

5, dictionary, defined in curly braces, comma-delimited key:value,value can be any type, but key must be immutable type.

info = {

' Name ': ' xx ',

' Age ': 30, '

' Sex ': ' Male '}

A mutable type is a variable, such as a list and a dictionary, in which the ID is constant.

An immutable type is an immutable type when value changes and the ID is changed.

6, Boolean type, True and false, the state is set up and not established, mainly used in the logical operation of the judgment.

Vi. formatted output

The string uses placeholders in place of the variable values, such as%s and%d, which represent the values of the string and integral type, respectively.

Print ("My name is%s,my-age is%d"% (' xxx ', 30))

Seven. Basic operators

Arithmetic operations:

+ Two numbers added

-Subtraction of even numbers

* Multiply the number or repeat a string several times

/x divided by Y

% Division takes remainder

* * Returns the y power of X

Returns the integer part of the quotient

Comparison operation:

All comparison operators return 1 for true, 0 for false, and the other for the special variable true and false equivalents.

= = Compare objects for equality

! = Compare whether two objects are not equal

<> Compare whether two objects are not equal

> returns whether x is greater than Y

< returns whether x is less than Y

>= returns whether x is greater than or equal to Y

<= returns whether x is less than or equal to Y

Assignment operations

= Simple assignment operator

+ = addition Assignment operation A+=b equivalent to A=a+b

-+ subtraction Assignment Operation A-=b equivalent to A=a-b

*= multiplication Assignment Operation A*=b equivalent to A=a*b

/= Division assignment Operation A/=b equivalent to a=a/b

%= modulo assignment Operation A%=b equivalent to A=a%b

**= Power character paper operation A**=b equivalent to A=a**b

= divisible Assignment operation a//=b equivalent to a=a//b

Logical operations

and Boolean "and". X and Y, X and Y have one false, the result is false, two are true, and the result is true.

or boolean "or". x or y, x and Y have one true, the result is true, two are false, and the result is false.

Not Boolean "not". False if X is True,not x, or True if X is False,not x.

Eight. If...else Control process

If statement:

Age = 61if age>60:    print ("Can retire")

If...else:

Age = 61if age>60:    print ("can retire") Else:    print ("Continue tapping the Code")

If...elif...else:

Age = 61if age>60:    print ("Can retire") elif age>20:    print ("Continue tapping the Code bar") Else:    print ("Go to School")

9.while Cycle

If the loop condition is true, the loop body is executed and the condition is re-judged after the execution is completed. If the condition is false, the loop body does not execute and the loop terminates.

Count = 0while count<10:    print (count)    count+=1

Using break and Continue,break in the loop can jump out of this layer loop, continue can jump out of this cycle.

#打印100以内的奇数

Count = 1
While True:
If count>100:
Break
If count%2==0:
Count+=1
Continue
Print (count)
Count+=1

Python Learning Note one

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.