Python study notes (a): Python basic grammar

Python study notes Marco major problem according to their own practice in Python and video to be recorded and summarized to facilitate a deeper impression review.

Programming Fundamentals

program

Enables a computer to identify a set of instructions executed and

electronic calculator

A machine capable of executing program

Modern computer

Alan Mathison Turing (Alan Mathison Turing, 1912 Nian 6 23 - June 7, 1954), British mathematician,
logician, known as the father of computer science, the father of artificial intelligence. The famous Turing proposed the Turing machine model laid the foundation for modern computer logic works of
von Neumann's famous Jewish Hungarian-American mathematician, computer scientists, physicists and chemists, the father of digital computers. He proposed as a binary number system based digital computer, the computer should be performed in program order, the computer should have five members.

Von Neumann architecture

CPU and controller by the arithmetic unit
arithmetic unit, perform various arithmetic operations, logic operations, data transfer
transmission data such as processing
controller controlling execution of the program
memory for program and data memory, such as memory
input device, the data or entered into the computer program, such as a keyboard, a mouse,
an output device, the processing result display data or programs to a user, such as a display, printer, etc.
CPU registers and there are multi-level cache cache

Programming Fundamentals

Computer Language: interaction between human and computer languages

Machine language:

A certain sequence of binary digits 0 and 1, referred to as machine instructions. It is a collection of machine instructions in machine language
and natural language too different, difficult to learn, hard to understand, hard to write, hard to remember, difficult troubleshooting

Assembly language:

Alternatively machine instructions with mnemonic number, called an assembly language. The ADD A, B refers to the count register A and register B is obtained by adding the number of register A into the
assembly language program requires written assembler program into machine instructions
assembler language is only slightly better remember some may believes that mnemonic corresponding machine instructions. Only the symbol itself close to natural language.

Language Category

Low-level language:

Machine-oriented language, including machine language, assembly language, not common different machines, different machines require different assembler or machine instructions

High-level language:

Close to natural language and mathematical language of computer languages, high-level language source code written first by the compiler converts source code into machine instructions of the program
in 1954, the official release of the Fortran language is the first high-level language, the formula is intended to translate
people need only concerned about how to write source code, the compiler to compile for different machines do care treatment

Low-level language to high-level language

The more high-level language, closer to natural human language and mathematical language
language more low-level, allow the machine to understand the
tools require a transition between high-level languages and low-level language: compiler, interpreter
C, C ++ and other languages source code needed native compiler, Java, Python, C # source code to be compiled into the interpreter an intermediate code (Bytecode), running on the virtual machine
compiler language, to convert the source code into object machine CPU instructions
converted into bytes interpreted languages, explained code runs on a virtual machine, intermediate code executed by an interpreter

Development of high-level language

Unstructured languages:

Number or label, the GOTO, the subroutine may have a plurality of inlets and outlets, branched, cyclic

Structured Language:

Any basic structure only allows only single inlet and outlet, the order, branches, loops, GOTO discarded
object-oriented language:
closer to the human sense of the world way, everything abstracted as the object, the association between objects into classes and inheritance, encapsulation, inheritance, polymorphism

Functional languages:

Old programming paradigm, applied mathematics, scene of parallel processing. Introduced to a lot of modern high-level languages, functions are "first-class citizens," higher-order functions

Program Program

Program:
Algorithms + Data Structures = Programs
data is the core of all program
data structures and data types are organized in a computer
algorithm that processes data, algorithms have good or bad
writing process difficult:
disarray Data
confuse the method of treatment
can not be designed to convert the data into a data structure, the treatment can not be converted into the algorithm
can not be used to design paradigm programming
world program Jie bug, but not debug

Python interpreter

Official CPython

C language development, the most widely used Python interpreter

IPython

An interactive, Enhanced Cpython

PyPy

Python written in Python language interpreter, JIT techniques, dynamically compiled Python code, a feature is dynamic compilation PyPy

Jython

Python source code compiled into Java byte code, running on the JVM

IronPython

And Jython Similarly, interpreter running on the .Net platform, Python code is compiled into bytecode .Net

Python basic grammar

Notes - # annotated text

digital

Integer, and does not distinguish between long int, hex 0xA, 0o10,0b10
BOOL, 2 values True, False
float
1.2,3.1415, -0.12,1.46e9 equivalent to 1.46 * 109
complex, 1 + 2j
string
using ' " sequence of single and double-quoted character, '' and '' 'three single and double quotation marks, can cross lines, which can be used in single and double quotation marks in the free string preceded by the prefix r or R indicates that the string is not special treatment

Escape sequence:

 \\ 表示\用\来转义另一个\,所以用来表示一个\。
 \t  表示Tab键,是一个制表符
 \r 表示回车键
 \n 表示换行
 \' 、\"还没搞清楚代表啥

 前缀r,把里面的所有字符当普通字符对待

indentation

未使用C等语言的花括号,而是采用缩进的方式表示层次关系
约定使用4个空格缩进

Continued row

在行尾使用 \
如果使用各种括号,认为括号内是一个整体,内部跨行不用 \

Basic grammar

Identifier

  1. A name used to refer to a value
  2. Only letters, numbers and underscore
  3. You can only begin with a letter or an underscore
  4. Can not be a python keywords like def, class can not be used as an identifier
  5. Python is case sensitive

Conventions:

Chinese are not allowed
not allowed to use ambiguous words, such as class_
Do not use begins with an underscore character representation in python

constant

Once assigned identifier can not change the value of the
python can not define constants

Literals

A separate quantity, for example 12, "abc", '2341356514.03e-9'

variable

After the assignment, you can change the identifier value

Python language type

Python is a dynamic language, strongly typed language

Statically compiled language

Achieve declare a variable type, type can not be changed
compile-time checking

Dynamically compiled languages

Without prior declaration type, ready to be assigned to other types
do not know what type of programming, it is difficult to infer

Strongly typed language

Operation, must be forced type conversion between different types of the same type. print ( 'a' + 1)

Weakly typed language

Different types may be operated automatically implicit conversion, JavaScript in console.log (1 + 'a')

Operators Operator

Arithmetic operators

 + - * / % **

In addition to natural / floating point result, divisible //. Note: in 2.x / and // are divisible
bitwise
& | ~ ^ << >>
in the usual manner: multiplication and division multiple of 2, corresponding to 32 @ 4 >> 2 32
12,0xc, 0o14,0b1100

How much is equal to 12, and why
 等于-13,原因如下:
    12的二进制为:0000 1100
  ~12的二进制为:1111 0011
  计算会将~12看成是一个负数,将其转化为原码在给使用者看,于是:
  转化为原码后的二进制为:1000 1101  这个二进制就是-13的二进制,所以~12为-13。

The original code, anti-code, complement, negative notation

Original code

5 => 0b101,1 => 0b1 ,-1 => -0b1, bin(-1)

Inverted

Positive anti-code the same as the original code; reverse code symbol bits remaining constant negative bitwise

Complement

Complement positive numbers with the same original code; complement negative sign bit remaining unchanged after bitwise +1

Negative representation

CPU digital arithmetic circuit implements an adder, a subtractor but without, the addition subtraction is converted into
negative numbers stored in the computer's complement, the complement-1 1111 1111
5-1 => 5 + (--1 ) intuitively 0b101-0b1, in fact, the computer is 0b101 + 0b11111111, discard the overflow bit
to 12 -13 Why?
Equal to 10 ^ 9? 10 -9 equal? why

Operators

Comparison Operators

! = ==> <> = <=
Returns a bool value
1 < '1' 1 == '1'
chained comparison operators:
. 4>. 3> 2. 4> MyNumber> = 1

Logical Operators

And or and or not
short-circuit operators:
and if the first expression is False, the back is not necessary calculated, and this logical expression must be False
or True if the first expression, the back is not necessary to calculate this logical expression must be True

Assignment Operators

a = min (3,5)
+ = - = * = / =% =等
x = y = z = 10

Member operator

in, not in
the identity of the operator
is, is not
arithmetic operators> Bitwise Operators> identity operator> member operator> Logical operators
can not remember, in parentheses
long expression, the use of parentheses,
easy to understand, easy to read

Expressions Expression

A combination of numbers, symbols, parentheses, variables, etc.

Arithmetic expression
logical expression
assignment expression
in Python, an assignment that is defined, if a variable is defined, equivalent to redefine the assignment. But before referring to first determine the variables have already been defined.

Memory Management

Variables without prior notice, do not need to specify the type, which is characteristic of dynamic languages.
Programming generally without concern for the survival of the variables, we do not care about memory management

python reference counting number of the reference record of all objects
当对象引用数变为0,它就可以被垃圾回收GC
计数增加:赋值给其它变量就增加引用计数,例如x=3;y=x
计数减少:
函数运行结束时,局部变量就会被自动销毁,对象引用计数减少
变量被赋值给其它对象。例如x=3;y=x;x=4

About performance, you need to consider variable references, but the free memory, or try not to release the memory, see the demand. This is done mainly to solve the problem of garbage collection and memory fragmentation, and memory management will affect the performance of programs. JVM-depth knowledge of specific aspects of Java can refer.

Program control

order

According to a section of the order execution
for example, wash your hands, then eat, then washing dishes

Branch

Depending on the judgment, the conditions are met under certain conditions, the statement is executed
, for example, wash your hands, if rice is not ready to play the game, if the rice well, you eat, if rice is not done, takeout

cycle

Conditions are met repeatedly executed, does not meet or no longer perform is not executed
, for example, wash your hands, do not look good meal, no good, for a while to see whether a good time, look for a moment at a time until a good meal before they can be eat. Here cycling conditions are no good food, no good meal, you cycle point of view there is no dinner ready.

Single-branch structure

The if statement

  if condition:
        代码块

a condition must be bool type, this place has an implicit conversion bool (condition)
IF. 1 <2:
Print ( '. 1 less Within last 2')

Block

After the colon similar to the if statement is a block of statements
in the if, for, def, class, etc.

False equivalent Boolean value corresponds bool (value)

空集合
空字符串
空字典
空列表
空元祖
None对象
0

The above four kinds of type mainly empty, plus the equivalent Flase None 0 and
the program control further comprises a multi-branch configuration and branch nesting. Mainly through multi-branch if, elif, else statements.
The branch of the branch can be nested, nested loop may be nested with each other multilayer.

cycle

While including the statements and for statements. while statements can be set to an infinite loop. statement is taken for an object execution Block (code blocks) from iteratable (available iteration), the finished then taken from iteratable until take complete.
continue statement is interrupted when the current cycle of execution, continue to the next cycle.
The break statement is to terminate the current cycle.

Circulation continue, break statement

to sum up

continue and break the cycle is a control statement, only affect the current cycle, including while, for loop
if the nested loop, continue and break that affect only one loop is located
continue and break out are not statement block, if cond: break not out of if, but if the loop is terminated break outside where

Loop else clause

grammar

while condition:
         block
else:
         block
for element in iteratable:
         block
else:
         block

If the normal execution cycle ended, the else clause; if you use break to terminate and abort, the else clause will not be executed.

Published 11 original articles · won praise 14 · views 5460

Guess you like

Origin blog.csdn.net/ssnszds/article/details/89261426