Python Programming Fundamentals

python basis

Tags (separated by spaces): python- basis


A, python Overview

table of Contents meaning
Founder Dutch (Guido van Rossum)
Founding Time in 1989
Python philosophy import this The Zen of python

Two, python2 and python3 difference

Point of difference python2 python3
Function of language print keyword print () function
Divisible / Divisible / Natural addition to
input function raw_input 3.x is raw_inputrenamed input, not in useraw_input
round function
String Unified use of unicode characters
grammar Exception is caught, thrown syntax change

Three, python basis

3.1 Architecture

3.1.1 Turing machine

  • program
  • It enables a computer to identify a set of instructions executed and
  • electronic calculator
  • A machine capable of executing program
  • Modern computer
  • Turing, laid the foundation for modern computer logic works
  • Von Neumann architecture

3.1.2 Von Neumann architecture

  • Five components of the computer
  • Operator: to complete various computing
  • Controller: Control program execution
  • Memory: memory for programs and data, such as memory (i.e., loss of power failure)
  • Input device: the data entered into a computer program or
  • An output device: the result output processing program or presentation to a user; disk

Special note:
. 1, the CPU by the arithmetic unit, and a controller (central processing unit)
2, the CPU registers and there are multi-level cache (the Cache)
. 3, all data are loaded into memory first, and then processing to the cpu, Therefore cpu do not directly interact with the disk; while the CPU does not directly use the data in memory, you must register first loaded into a register and then calculate, calculate and then complete the move from the register to memory


3.2 language system

3.2.1 computer language

  • The interaction between human and computer languages

  • Low-level language:
  • 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
    • Assembly language
      • Some instructions use mnemonic substitute machine, called an assembly language.
        - number written in assembly language programs need to compile program into machine instructions

    Not common different machines, different machines require different assembler or machine instructions

  • High-level language
    • Close to natural language and mathematical language of computer language

3.2.2 Development of high-level language

  • Unstructured languages
    • GOTO, the subroutine may have a plurality of inlet and outlet
    • There are branches, loops
  • Structured language
    • Any basic structure only allows only the inlet and outlet unique
    • Order, branching, looping, repeal GOTO
  • Object-oriented language
    • Closer to the world of human cognition way, all things into abstract objects, objects to the relationship between abstract classes and objects
    • Encapsulation, inheritance, polymorphism
  • Functional languages
    • Old programming paradigm, application of mathematical calculations, parallel processing of the scene;
    • Function is a "first-class citizens," higher-order functions

The arrival of the era of big data, compute large amounts of data, so the code closer to a mathematical formula, the more streamlined; therefore once again be introduced to a lot of modern high-level languages

3.2.4 low-level language and high-level language

  • The more advanced language, closer to natural language and mathematical language of mankind
  • The more low-level language, allows the machine to understand
  • The need for a transition between high-level languages ​​and low-level language tools, compilers, interpreters

1, C, C ++ and other languages source code needs to be compiled locally (with the operating system type and CPU instruction set of closely related, and therefore under different operating systems, need to be recompiled)
2, the Java, Python, C # source code needed compiled into intermediate code interpreter (bytecode, byte code), running on a virtual machine

3.2.5 compiled languages ​​and interpreted languages

  • Compiled language, the source code into target CPU instructions machine
  • Interpreted language, convert the interpreted byte code runs on a virtual machine, intermediate code executed by an interpreter

Virtual machine, compile once, run anywhere, equivalent to different operating systems and CPU instruction set and other related things, there is a virtual machine to complete, as long as they deploy virtual machines installed, you can run the bytecode program


3.3 Program Program

  • program:
  • Algorithms + Data Structures = Programs
  • Data is the core of all programs
  • Data type and data structure is organized in the computer
  • Algorithm is a way to process data, the algorithm has good or bad

3.4 Python basic grammar

3.4.1 basic grammar

  • Notes: #
  • digital:
    • Integers, does not distinguish between long and int
      • 0xa (16 hex), 0O10 (octal), 0b10 (binary)
      • bool (True, False)
    • Float
      • Only do some data approximation expression; not infinite loop, irrational number, etc.
    • plural
  • String
    • Single quotes, double quotes, triple quotes
  • Escape character
Escape character meaning
\ Escape \
\t Escape the Tab key
\r Enter Escape
\n Escape Wrap
' Escaped single quotes
" Escaped double quotes
> 前缀r, 把里面的所有字符当普通字符对待 
  • indentation
    • Agreed to use 4 spaces to indent
  • Continued row
    • Using the end of the line, and then directly CRLF
  • Identifier
    • Only by numbers, letters, underscores, and can not start with the numbers, you can not use the keyword
    • In python is case sensitive

    • Conventions:
      • Chinese are not allowed
      • Do not allow the use of ambiguous words
      • Do not use leading underscores in python
  • constant
    • python can not define constants
  • Literals
    • A separate amount
  • variable
    • After the assignment, you can change the identifier value
  • Operators Operator
    • Arithmetic operators
Operators meaning
/ In addition to natural
// Divisible
% Remainder
+ addition
- Subtraction
* multiplication
* square
> `1/0` 除零异常
  • Bitwise Operators
Operators meaning
vertical Position or
& lie in
~ Bitwise
^ XOR
<< Left Shift
>> Right shift

3.4.2 python language type

  • python is a dynamic language, strongly typed language

  • Dynamic and static compiled language compiled languages
    • Dynamically compiled languages
      • Without prior declaration type, ready to be assigned to other types
      • When the program does not know what type, it is difficult to infer
    • Statically compiled language
      • Achieve declare a variable type, type can not be changed
      • Compile check
  • Strongly typed language, and a weakly typed language (relative concept)
    • Strongly typed language
      • Operation, must be forced type conversion between different types of the same type.
    • Weakly typed language
      • Different types may be operated automatically implicit conversion; javascript

    If the string plus the number being given, was strongly typed language; the other hand, if the error is not cast, compared with a weakly typed language

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

  • Bitwise Operators
Operators meaning
vertical Position or
& lie in
~ Bitwise
^ XOR
<< Left Shift
>> Right shift
  • The original code, anti-code, complement, negative notation
    • Original code
    • bin(-1) 5 ---> 0b0101,-1---> -0b1
    • 反码
    • 正数的反码和源码相同, 负数的反码符号位不变其它按位取反
    • 补码
    • 正数的补码与源码相同, 负数的补码符号位不变,其余按位取反后 +1;
    • 补码的补码, 就是源码

计算机中的所有数据, 都是补码

- 负数表示法
- 数字电路的CPU中的运算器实现了加法器, 但没有实现减法器, 减法是转换成加法
- 负数在计算机中使用补码存储, -1的补码为 `0b1111 1111`
    
- 实例
- `5-(-1)`
> 推理过程:
    直觉上是 `0b101 - 0b1`

但计算机中是: 0b101 + 0b11111111, 溢出位舍弃

- `~12` 为什么是 `-13`?
> 推理过程:
    ======  `~12` (12取反)
    `0000 1100` --->12的补码
    `1111 0011` --->取反后, 是补码, 最高位为 1, 因此为负数
    `1000 1101` --->补码的补码就是原码, 因此取反 +1; 因此为 `-13`


- `10^9`等于? `10^-9`等于?为什么?

推理过程:
待补充


3.5 Python 解释器

  • 官方 CPython
  • C语言开发, 最广泛的Python解释器
  • IPython
    • 一个交互式,功能增强的Cpython
  • PyPy
    • Python语言开发写的Python解释器, JIT技术(just in time, 动态编译Python代码)
  • Jython
    • Python 的源代码编译成Java的字节码, 跑在 JVM 上(Java虚拟机)
  • IronPython
    • 与Jython类型, 运行在 .Net 平台上的解释器, Python代码被编译成 .Net 的字节码

3.6 内存管理

  • 变量无须实现声明,也不需要指定类型
    • 动态语言的特性
  • 编程中一般无需关心变量的存亡, 也不用关心内存的管理
  • python使用引用计数记录所有对象的引用数
    • 当对象引用数变为 0, 它就可以被垃圾回收(GC)
    • 计数增加: 赋值给其它变量就增加引用计数
    • 计数减少:
      • 函数运行结束时, 局部变量就会被自动销毁, 对象引用计数减少
      • 变量被赋值给其它对象
  • 术语:
    • 内存空洞, 引用计数, 内存整理

3.7 python程序控制

3.7.1 概念

  • 顺序
    • 顺序执行
  • 分支
    • 根据不同的情况判断, 条件满足执行该条件下的语言
  • 循环
    • 条件满足反复执行, 条件不满足则跳过

3.7.2 真值表

  • 空字符串, 空字典, 空元组, 空列表, None对象, 0 都等价于 False

3.7.3 分支

3.7.3.1 单分支结构
    if 语句
        if condition:
            代码块

condition 必须是一个bool类型, 这个地方有一个隐式转换bool(condition)

3.7.3.2 多分支结构
    if condition:
        pass
    elif condition:
        pass
    else:
        pass

分支结构, 无论满足多少分支条件,都只能进一个分支(先进入前面的分支)

3.7.3.3 分支嵌套
# 分支嵌套语法结构
if condition:
    pass
else:
    if condition:
        pass
    elif condition:
        pass
    else:
        pass

嵌套结构, 可以是分支、循环的嵌套
可以互相嵌套多层

3.7.3.4 循环

while 循环

while condition:
    block

当条件满足即condition为 True, 进入循环体, 执行 block

for 循环

for element in iteratable:
    block

当可迭代对象中有元素可以迭代, 进入循环体, 执行 block

break 语句

  • 终止当前循环

continue 语句

  • 结束本次循环

循环 continue, break 语句
1、continue 和 break 是循环的控制语句, 只影响当前循环, 包括while, for循环
2、如果循环嵌套, continue和break也至影响语句所在的那一层循环
3、continue和break 不是跳出语句块, 所以 if condition: break 不是跳出 if , 而是终止if外的break 所在的循环

循环 else 子句

如果循环正常的执行结束, 就会执行else, 如果被break打断, 则不执行 else

Guess you like

Origin www.cnblogs.com/jingru-QAQ/p/11357508.html