Basic Experience of Python (1) and Basic Experience of python

Source: Internet
Author: User

Basic Experience of Python (1) and Basic Experience of python

I. Variables

Variable assignment: name = "Meng"

The code above declares a variable named name. The value of the variable name is: "Meng"

Variable definition:

A container for storing data in the container.

Significance of the variable:

Save the intermediate results or status of the program execution for subsequent Code calls.

Variable definition rules:
    • Variable names can only be any combination of letters, numbers or underscores;
    • The first character of the variable name cannot be a number;
    • Keywords cannot be declared as variable names;
Ii. indent:

Python designers intentionally design highly restrictive syntaxes so that bad programming habits (for example, the next line of the if statement is not indented to the right) cannot be compiled to force programmers to develop good programming habits. In addition, the Python language uses indentation to indicate the start and exit of the statement block (Off-side rules), rather than using curly braces or certain keywords. Increasing indentation indicates the beginning of the statement block, while decreasing indentation indicates the exit of the statement block. Indentation is part of the syntax.

For example, if statement:

Age = int (input ("age:") if age <21: print ("you cannot smoke! ") Print (" this sentence is not in the if statement block ")

Note: The above example is the Python code.

According to PEP, four spaces must be used to represent each level of indentation (it is unclear how the four spaces are defined. In actual writing, the number of spaces can be customized, but the number of spaces must be equal for each indentation ).

Iii. Notes:

When the line comment: # the content to be commented out

#name = "Meng"

Multi-line comment: "commented content" Or '''commented content '''

'''Age = int (input ("age:") if age <21: print ("you cannot smoke! ") Print (" this sentence is not in the if statement block ")'''

4. single quotation marks, double quotation marks, and three quotation marks

In general, there is no difference between single quotes and double quotes. However, if the character string contains the same characters, escape using \. For example:

1 print("hello word")2 print('hello word')

The results 1 and 2 are both hello word.

3 print("I\'m")4 print("I'm")

The results of 3 and 4 are both I'm

5 print('I"m')6 print('I\"m')

The results of 5 and 6 are all I "m ".

The three quotation marks are different from each other: "What you see is what you get". Use the three quotation marks for a multi-line string (if you use single or double quotation marks, you need to escape them). For example:

print('''my name is Meng,hello word,I like python.''')

The result is:

My name is Meng,

Hello word,

I like python.

V. user input:
#! /Usr/bin/env python #-*-coding: UTF-8-*-name = input ("What is your name? ") # Copy the content entered by the user to the name variable print (" hello "+ name)

The preceding result is:

What is your name? Meng # hello Meng
6. Python internal execution process: compilation process overview:

When we execute the Python code, the Python interpreter uses four steps to "Disassemble" Our Code and is finally executed by the CPU and returned to the user.

First, lexical analysis is performed when the user inputs the code to Python for processing. For example, if the user inputs a keyword or the input keyword is incorrect, lexical analysis is triggered, incorrect code will not be executed.

In the next step, Python performs syntax analysis. for example, if the colon after "for I in test:" is written as another symbol, the code will not be executed.

The final key process is generated by Python before Python is executed. pyc file. This file is the bytecode. If you accidentally modify the bytecode, Python will compare it with the bytecode file generated last time it re-compiles the program, otherwise, the modified bytecode file will be overwritten to ensure the accuracy of the bytecode after each compilation.

So what is bytecode? The bytecode corresponds to the PyCodeObject object in the python virtual machine program .. Pyc files are the representation of bytecode on disks. In simple terms, during code compilation, functions, classes, and other objects in the Code are first classified and processed, and then bytecode files are generated. With the bytecode file CPU, you can directly identify the bytecode file for processing, and then Python can execute it.

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.