Python First lesson

Source: Internet
Author: User

    • What is Python?

Object-oriented computer programming language; In 1989, the author, Guido van Rossum, invented Python in Amsterdam in order to spend Christmas in the boring time. (no need to explain the tough life)

Simplicity, ease of understanding, scalability. Or just beginning to learn python, it's a little more powerful is not forced. After all, it's just a little bit of what other people say, and they don't really feel the power.

    • Field

Cloud computing: The hottest language in cloud computing, Openstack

Web development: An excellent web framework, Django

Scientific Computing/Artificial intelligence

System operation and maintenance: essential skills for operation and maintenance

Finance: Quantitative Trading, financial analysis

Graphical GUI

    • The classification of Python in programming languages

Explanatory language: Program code written in high-level languages needs to be translated into machine language that can be understood by the computer before it is run. The process of translation is divided into two types: compilation and Interpretation. Compilation translates the code from the program to the machine language one at a time between runs. The explanation is that when the program source code is run, a code is run and the next code is translated.

Dynamic type language: Dynamic language refers to the language in which data type checking is done during run time (you do not need to specify a data type for a variable when writing a program in a dynamic type language); Static language refers to the language in which you need to define the data type in advance when writing program code.

Strongly typed definition language: The data type of a variable of a strongly typed definition language cannot be changed without casting; the data type of a variable of a weakly typed definition language can be ignored.

    • Python Program execution process

      Python is an explanatory language, which is a process that does not compile before running the program, and the program code is translated one at a time when the program is run. But in fact Python is precompiled between runs, the result of Precompilation is an in-memory pycodeobject file, which is a bytecode file, and the Python interpreter is actually translating the code inside the file. When the program finishes running, the Python interpreter saves Pycodeobject to the local pyc file.

      When you need to run the program again, you do not perform the precompilation process, but instead load the PYc file locally to run in memory.
    • The difference between Python2 and Python3
    1. Print
Print Statements in the Python2
>>>print ' My name ', ' is ', ' Bu gaosu ni '


Print Statements in the Python3
>>>print (' My Name ', ' is ', ' Bu gaosu ni ')
My name is bu gaosu ni

2. The default character encoding for Python3 is Unicode,python2 's default character encoding is ASCLL

    • Hello World
      1 Print ("hello,world")

      The program code prints "Hello,world" on the screen. The print function prints what needs to be printed on the screen.

      Python2 and 3 have made a little change to the print function:

Print in Python2 does not need to use double brackets directly with the content that needs to be printed, separated by "," between multiple content:

Print " Hello,world "

In Python3, you need to include the printed content with double brackets after print.

    • Variable

The name of an actual value pointing to the memory address space. Python is a dynamic type language that does not need to declare variables when specifying the data type, directly assigning a value to the variable.

    1. Variable names consist of numbers, letters, underscores
    2. The first character of a variable name cannot be a number
    3. Some words cannot be used as variable names: [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' Finall Y ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' Try ', ' While ', ' with ', ' yield ']
    • Comments

Single-line Comment: #被注释内容

Multi-line Comment: ' Annotated content '

    • Specify interpreter

      Under the Linux system, if you do not specify a Python interpreter to use in the script, you run the script by executing the python [script name] command when you run the script. To run the script using "./hello.py", you need to specify the interpreter at the beginning of the script

      #/usr/bin/env/pychon

    • Data type

      Data types are currently exposed to more than one type of int, long, string, float, and Boolean values. In string concatenation and formatted output,%s represents a string,%d represents an integer, and%f represents a floating-point number

    • The user enters the input function

The input function receives inputs and returns input data, with input and raw_input two functions in Python2. Input and raw_input are not the same in use, Raw_input is more convenient than input, more in line with the use of human-computer interaction. Input defaults to a Python-compliant string or number, an expression, so when you need to enter a string, and the string entered when the program runs without a quoted program default to the input is an undefined variable, resulting in an error that prevents the program from continuing to run. Raw_input has improved on this point by default as the input content is treated as a string.

Also note that the data type of the variable that is assigned by the input function is a string, and if the content of the value is numeric and numeric, you need to force the string to be converted to an integral type.

1>>> Age=input ("How old is you :")2How old is you:183>>>type (age) # # #type函数判断一个变量或者一个值的数据类型4<class 'Str'>5>>> Age=int (Input ("How old is you :"))6How old is you:187>>>type (age)8<class 'int'>

There is no longer a raw_input in Python, instead the raw_input is replaced directly with input.

>>> input ('input your name:') input your name:haha'haha  '

Usually we enter the password, the input is not want to be seen, only need to verify the correctness of the password in the background. You can use the module Getpass if you want to enter a password without being visible

Import getpass>>> getpass.getpass ('password:') password:      # Enter the password 123 when not visible '123'
    • string concatenation

Sometimes it is necessary to use a variable in the content that needs to be printed, when it is impossible to put a variable in the same quotation mark, and it is too cumbersome to use multiple print statements, then a string concatenation is required. There are many ways to implement string concatenation in Python, where only the "+" number is used, and other methods are described later in this section.

>>> name='hehe'>>> msg=''Hello,'+name + ' welcome'print(msg) Hello,hehe Welcome

string concatenation each use of a "+" needs to open up a piece of space in memory, the efficiency is low.

Python First lesson

Related Article

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.