Getting Started with Python you know what?

Foreword

What is the computer language

The computer is used to calculate a machine, people let the computer do computers have to do!

The need to control the computer (that is, programming languages) by the language of the computer!

In fact, computer language and human language is not essentially different, the main difference is that different communication!
Computer language has gone through three stages:

1) Machine Language

  • Machine language programming via binary coding
  • Good efficiency, write too much trouble

2) The symbol language (compilation)

  • Encoding using symbols instead
  • When programming, without the use of a binary, but written symbols directly
  • After completion, the symbol is converted into machine code, and then executed by the computer symbols into machine code called an assembly process requires
  • The process of machine code into symbols referred to disassemble
  • Assembly language generally apply only to certain hardware, compatibility is poor

3) Advanced language

  • The syntax of basic and high-level language is now English-like syntax, and hardware and the relationship is not so close. That we can perform on different hardware systems through the development of high-level language program
  • And high-level language is also more easy to learn together, and now we know the language are basically high-level languages ​​such as: C, C ++, C #, Java, JavaScript, Python etc ..

Compiled languages ​​and interpreted languages

The computer only recognizes the binary coding (machine code), so any language must first be converted to machine code execution when handed over to the computer, which is like print ( 'hello') must be converted to machine code so similar 1010101.

Depending on the timing of the conversion, the language is divided into two categories:

1). Compiled language

  • Such as: C language
  • Compiled language, the code will be executed before the code is compiled to machine code, machine code and then handed over to the computer to perform
    • Execution: a (source) - compiler -> b (compiled machine code)
  • Features:
    • Particularly fast execution speed
    • Cross-platform rather poor

2). Interpreted language

  • Such as: Python JS Java
  • Interpreted language, the code is not compiled before execution, but while compiling execution while at the same time the implementation of
    • Execution: a (source) - interpreter -> interpreted
  • Features:
    • Perform more slowly
    • Better cross-platform

Let's cut to the chase and talk about Python.

Python Introduction

Python is an interpreted language.

Python (English pronunciation: / paɪθən / American pronunciation: / paɪθɑːn /), it is a widely used high-level programming language, belonging to general-purpose programming language created by Guido van Rossum, the first edition published in 1991. It can be regarded as a modified (addition of a number of advantages in other programming languages, such as object-oriented) in LISP. As an interpreted language, Python's design philosophy emphasizes readability and concise syntax code (particularly the use of spaces to indent a block of code division, instead of using braces or keywords). Compared to C ++ or Java, Python allows developers to express the idea with less code. Whether small or large programs, the language structure of the program are trying to make clear.

Python uses:

  • WEB application
  • Facebook watercress
  • Crawler
  • Scientific Computing
  • Automated operation and maintenance
  • Large data (clean data)
  • cloud computing
  • Desktop software / games
  • artificial intelligence

Python development environment to build

Development environment to build is to install the Python interpreter

Python interpreter Category:

  • CPython (official)
    with c language Python interpreter
  • PyPy
    in Python language Python interpreter
  • IronPython
    with Python interpreter written in .net
  • JPython
    written in Java Python interpreter

step:

1. Download the installation package-3.6.exe Python
Here Insert Picture Description
2. Installation (fool installation, default ..)

The installation is complete you can see the following procedures:
Here Insert Picture Description

3. Open a command line window, enter the following appears python
Here Insert Picture Description
Python interactive interface

When we entered through the Windows command line Python, the interface is entered into the Python interactive interface

structure:

Version and copyright statement:

Python 3.6.3 (tags/v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Command Prompt:

>>>

At the command prompt, you can enter Python commands directly! Enter the complete command will be executed immediately Python interpreter!

Python installed, it will automatically install a Python development tools IDLE, IDLE can also enter through the interactive mode. But the difference is that you can view in the IDLE statement prompted by the TAB key.

IDLE is actually an interactive interface, but he can have a few simple tips, and you can save the code.

Note: Interactive mode you can only enter a line of code, it is one line, so he does not apply to our daily development!

Can only be used to do some daily simple test!

We generally will write Python code to a py file, and then executed by the instruction python
code file.

Python and the integration of Sublime

1. Sublime executed in the Python code, + b can be performed automatically by the control panel built Sublime ctrl

This implementation, support is not good for the Chinese in some versions of Sublime, and can not use the input () function.

2. Use SublimeREPL to run python code

FIG follows: Select SublimeREPL-> Python to execute Python code
Here Insert Picture Description
Here Insert Picture Description
, but this is very troublesome. For convenience we can set a shortcut key, press f5 the automatic execution of the current Python code.

Set the following shortcut keys to run python code?

Found Preferences -> Key Bindings, and then copy the following contents into the brackets on the right. Then save.

So that we can automatically execute the current Python code Press F5 shortcut key.

{ "keys": ["f5"], "caption": "SublimeREPL:Python","command": "run_existing_window_command", "args":{"id": "repl_python_run","file": "config/Python/Main.sublime-menu"}},

Here Insert Picture Description

Here Insert Picture Description

Some basic concepts of Python

1. Expressions

Expression is something similar to a mathematical formula

For example: 10 + 58--4

Expressions generally only used to calculate some of the results, will not have a material impact on the program

If you enter an expression in the interactive mode, interpreter automatically outputs the result of the expression

2. Statement

General statements in the program need to complete certain functions, such as printing information, access to information, to assign values to variables
such as:

print()
input()
a = 10

Perform general statement of the program will have some impact

Not necessarily output statements in interactive mode execution results

3. Program (program)

Program is a one of the statements and expressions one by one made of.

4.函数(function)

函数就是一种语句,函数专门用来完成特定的功能
函数长的形如:xxx()

函数的分类:

1). 内置函数
由Python解释器提供的函数,可以在Python中直接使用
2). 自定义函数
由程序员自主的创建的函数
当我们需要完成某个功能时,就可以去调用内置函数,或者自定义函数

函数的两个要素:

1). 参数
()中的内容就是函数的参数
函数中可以没有参数,也可以有多个参数,多个参数之间使用,隔开
2). 返回值
返回值是函数的返回结果,不是所有的函数都有返回值

Python的基本语法

  • 在Python中严格区分大小写
  • Python中的每一行就是一条语句,每条语句以换行结束
  • Python中每一行语句不要过长(规范中建议每行不要超过80个字符)
  • 一条语句可以分多行编写,多行编写时语句后边以结尾
  • Python是缩进严格的语言,所以在Python中不要随便写缩进
  • 在Python中使用#来表示注释,#后的内容都属于注释,注释的内容将会被解释器所忽略

我们可以通过注释来对程序进行解释说明,一定要养成良好的编写注释的习惯
注释要求简单明了,一般习惯上#后边会跟着一个空格

字面量和变量

字面量就是一个一个的值,比如:1,2,3,4,5,6,‘HELLO’

字面量所表示的意思就是它的字面的值,在程序中可以直接使用字面量

变量(variable)变量可以用来保存字面量,并且变量中保存的字面量是不定的

变量本身没有任何意思,它会根据不同的字面量表示不同的意思

一般我们在开发时,很少直接使用字面量,都是将字面量保存到变量中,通过变量来引用字面量

数据类型

数据类型指的就是变量的值得类型,也就是可以为变量赋哪些值

在Python中,能够直接处理的数据类型有以下几种:整数、浮点数、字符串、布尔值、列表、元组、字典、集合。

以后我们详细介绍这几个数据类型。

对象(object)

Python是一门面向对象的语言

  • 一切皆对象!
  • 程序运行当中,所有的数据都是存储到内存当中然后再运行的!
  • 对象就是内存中专门用来存储指定数据的一块区域
  • 对象实际上就是一个容器,专门用来存储数据
  • 像我们之前学习的数值、字符串、布尔值、None都是对象

对象的结构

每个对象中都要保存三种数据

1). id(标识)

  • id用来标识对象的唯一性,每一个对象都有唯一的id
  • 对象的id就相当于人的身份证号一样
  • 可以通过id()函数来查看对象的id
  • id是由解析器生成的,在CPython中,id就是对象的内存地址
  • 对象一旦创建,则它的id永远不能再改变

2). type(类型)

  • 类型用来标识当前对象所属的类型
  • 比如:int str float bool
  • 类型决定了对象有哪些功能
  • 通过type()函数来查看对象的类型
  • Python是一门强类型的语言,对象一旦创建类型便不能修改

3). value(值)

  • 值就是对象中存储的具体的数据
  • 对于有些对象值是可以改变的
  • 对象分成两大类,可变对象 不可变对象
  • 可变对象的值可以改变
  • 不可变对象的值不能改变

变量和对象

  • 对象并没有直接存储到变量中,在Python中变量更像是给对象起了一个别名
  • 变量中存储的不是对象的值,而是对象的id(内存地址),
  • 当我们使用变量时,实际上就是在通过对象id在查找对象
  • 变量中保存的对象,只有在为变量重新赋值时才会改变
  • 变量和变量之间是相互独立的,修改一个变量不会影响另一个变量

类型转换

  • 所谓的类型转换,将一个类型的对象转换为其他对象
  • 类型转换不是改变对象本身的类型,而是根据当前对象的值创建一个新对象

运算符(操作符)

运算符可以对一个值或多个值进行运算或各种操作

比如 + 、-、= 都属于运算符

运算符的分类:

Arithmetic Operators 1.
2. assignment operators
3. comparison operators (relational operator)
4. Logical Operators
The conditional operator (ternary operator)

Guess you like

Origin www.cnblogs.com/xxpythonxx/p/11705772.html