20,190,701 preview notes

Computer and Python 2.1 basis

2.1.1 CPU Memory Hard Disk Operating System

  • CPU
    • Computing Center computer, the brain of the computer
  • RAM
    • Local data temporarily stored data, provisional application loaded
    • Common memory size: 4G, 8G, 16G
    • Read speed
  • hard disk
    • Local long-term storage of data
    • 1TB=1024G 1GB=1024MB 1MB=1024KB 1KB=1024B
    • Read speed is slow
  • operating system
    • a softweare
    • Connect your computer hardware and all software

2.1.2 Python history

Python founder Guido van Rossum (Guido van Rossum), Python advocating beautiful, clear and simple

Python currently has two versions: Python2.x and Python3.x

  • Roughly the difference between the two versions of the source code
    • Python2.x: Because of the early C, Java, and other languages ​​of cattle involved in writing to supplement, resulting in duplication of code and more redundant, non-standard
    • Python3.x: code standards, clear and simple

2.1.3 Programming Language Category

  • Compiled
    • The code is compiled into binary all at once, and then execute
    • Compiler: each row is to compile the source code into machine language, and save it as a binary file
    • Advantages and disadvantages
      • Advantages: high efficiency, due compiled into a binary file, so you can run from a separate locale.
      • Disadvantages: low efficiency of development can not be cross-platform
    • Representatives language: C, C ++, Go
  • Interpreted
    • Progressive interpreted as a binary, line by line execution
    • Interpreter: only in the implementation of the program, one by one statement interpreted into machine language to perform computer
    • Advantages and disadvantages
      • Advantages: development of high efficiency, cross-platform, flexible modification
      • Disadvantages: low efficiency
    • Representatives language: Python, Ruby, PHP

2.1.4 dynamic and static languages ​​(extension)

  • Dynamic Languages
    • Do data type checking during operation language
    • Representatives language: Python, Ruby
  • Static language
    • During the compilation of data type checking, writing in the program is to declare all variables
    • Representatives language: C, C ++, java

2.1.5 strongly typed and weakly typed language definition language (extension)

  • Strongly typed language:
    • Always strongly typed language, variables must be coerced into another data type to the specified type.
  • Weakly typed language definitions:
    • Data types can be neglected language, variables can be given different types of data values

2.1.6 Python advantages and disadvantages

  • advantage
    • Elegant, clear and simple
    • Development of high efficiency - a very strong third-party libraries
    • High-level language - regardless of the underlying details
    • Portability - can be ported to almost run on all platforms
    • Scalability - may be used by using a part program written in C or C ++
    • Embeddable - can be embedded in the Python C or C ++ program
  • Shortcoming
    • Slow
    • Code can not be encrypted
    • Thread can not take advantage of multi-CPU

2.1.6 Python species

Different Python interpreter

  • CPython
    • Python official version, using the C language, C language recognition may be converted into bytecode
  • JPython
    • Identification can be converted into Java bytecode
  • ironPython
    • It can be converted into a language recognizable .net bytecode
  • pypy
    • Dynamic compilation

2.2 Python installation

Python's official website

  1. In the official website to download the installation package corresponding to the system

  2. installation steps

    Note: Sometimes after installation there will be "Disable path length limit" button, if there is, click on it, Path length disables the system automatically limits

  3. an examination

    Open win + r, enter cmd carriage return, open the Windows command interface

    Appear above the interface is the installation was successful.

2.3 a first python code

Default python 3.x version, python 2.x version special instructions.

  1. Writing in notepad ++ inprint ("hello world")

  2. Save the file as t1.py

  1. win + r, enter cmd, enter the command interface file to run just

    Successful operation

2.4 Variable

  1. Why variables?

    • When the expression is too complex to be assigned to a variable is not easy to write
  2. What is a variable?

    • Variable value stored in the memory
  3. How to define variables?

    name = "小明"
    #这种形式就是在定义一个名称叫name的变量
    
    name : 变量名
    = : 赋值
    "小明" : 值
  4. Explanation

    Variables can only point data can not point to a variable. Variables in memory is uniquely named.

2.4.1 Naming

Variable naming rules:

  • Variables must be a combination of letters, numbers, or underscore
  • The first letter variable names may not be numbers
  • python keywords can not be used as a variable
    • ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
  • 不建议使用中文或拼音
  • 变量名称要有意义
  • 变量名称最好不要过长
  • 变量名称区分大小写
  • 变量名称推荐写法:
    • 驼峰体:OldBoy
    • 下划线:old_boy

2.5 常量

常量:常量就是把变量名称大写,一直保持不变的量。

NAME = "小明"
#类似以上的就是常量,赋值方法和变量类似;但是python没有真正意义上的常量。

2.6 注释

  • 为什么需要注释?

    • 进行解释说明,便于理解
  • 如何注释?

    • 单行注释:#

    • 多行注释:''' 内容 ''' """ 内容 """

      # 单行注释
      
      '''
      多行注释
      '''
      
      """
      多行注释
      """
  • 在哪使用?

    • 在难以理解的代码后进行注释
    • 在函数,类等进行注释

2.7 基础数据类型

2.7.1 数据类型

  • number —— 数字
  • string —— 字符串
  • list —— 列表
  • tuple —— 元组
  • set —— 集合
  • dictionary —— 字典

2.7.2 int 整型

整型即为整数类型

  • 范围
    • 32位机器:-231~231-1,即-2147483648~2147483647
    • 64位机器:-263~263-1,即-9223372036854775808~9223372036854775807

数值运算:

  • +:加法
  • -:减法
  • *:乘法
  • /:除法,得浮点数
  • //:除法,得整数
  • %:取余
  • **:乘方

2.7.3 str 字符串

  • 什么是字符串?

    • 在python中用单引号''或双引号""或三个单引号括'''或三个双引号"""起来的都是字符串
    • 单引号和双引号没有区别,可以配合使用。
    • 在内容有换行时,可以使用三个单引号
  • 字符串的操作

    • 字符串的拼接

      str1 = "hello"
      str2 = "world"
      str = str1 + str2
      print (str)
    • 字符串的复制

      str1 = "hello"
      str = str1 * 2
      print (str)

2.7.4 bool 布尔类型

bool 布尔值:True(真) False(假)

2.7.5 type() 判断数据类型

利用type()进行数据类型的判断

date1 = 123
date2 = "123"
date3 = True

print (type(date1),type(data2),type(data3))

2.7.6 数据类型转换

实例:

data1 = 123
data2 = str(data1)
print (type(data2))
函数 描述
str(x) 将x转换为字符串
int(x) 将x转换为整数(必须为数字的前提)

2.8 用户交互 input

利用input可以进行用户之间的交互,交互获得的内容是字符串

usename = input('输入用户名:')
print (usename,type(usename))

2.9 流程控制语句 if

  1. 基本结构:

    if 条件:
        结果
    else:
        结果

    实例:

    if 1 > 2:
        print ("ok")
    else:
        print ("not ok")
  2. 多项选择结构:

    if 条件:
        结果
    elif 条件:
        结果
    elif 条件:
        结果
    else:
        结果

    实例:

    num = int(input("输入数字:"))
    if num == 3:
        print ("1")
    elif num == 4:
        print ("2")
    elif num == 7:
        print ("3")
    else:
        print ("5")
  3. 嵌套判断

    if 条件:
        if 条件:
            结果
        else:
            结果
    else:
        结果

    实例:

    if 3 > 2:
        if 5 < 6:
            print ("ok")
        else:
            print ("not ok")
    else:
        print ("False")

Guess you like

Origin www.cnblogs.com/os-linux/p/11116961.html