Python learns "02" Python Basics

Source: Internet
Author: User
Tags python script sublime text

First, Python internal execution process

Second, the Interpreter

Specifies that Python scripting be executed by the Python interpreter

If you want to execute a hello.py script like executing a shell script, for example: ./hello.py , then you need to specify the interpreter in the header of the hello.py file, as follows:

# !/usr/bin/env python Print ("hello,world")

As a result, execute: ./hello.py .

Note: You need to give hello.py execute permission before execution, chmod 755 hello.py

Prompt for Python interactive environment: >>>

Prompt for Windows command-line mode: c:>

In the Python interactive environment to enter the code directly, press ENTER, you can immediately get the code execution results, but in the command line mode, the execution of the Python ##.py file, does not.

In command-line mode, you can execute Python into the Python interactive environment, or you can perform a Python hello.py run a. py file

Two types of text editors: Sublime text, notepad++

Execute the python file, and on the command line, switch to the directory where the Python file is located, and then enter the Python ###.py command.

CPython uses >>> as a prompt, while Ipython uses in [ordinal]: as a prompt.

Python interactive mode is mainly used for debugging Python code, but also easy for beginners to learn, it is not a formal environment to run Python code!

What is the difference between Python's interactive mode and the direct running. py file ?

Entering Python directly into interactive mode is equivalent to starting the Python interpreter, but waiting for you to enter the source code one line at a line.

Running the. py file directly is equivalent to launching the Python interpreter and then executing the source code of the. py file at once, and you are not given the opportunity to enter it interactively.

Third, Content coding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII(American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, It can only be represented by a maximum of 8 bits (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

Unicode(Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

Error: ASCII code cannot be expressed in Chinese

1 # !/usr/bin/env python 2   3 Print " Hello, World "

Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3   4 Print " Hello, World "

Iv. notes

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

V. Execute script incoming parameters

Python has a large number of modules, the class library has three kinds:

    • Python-supplied modules
    • Industry-Open Source modules
    • Modules developed by programmers themselves

For example:

Python internally provides a SYS module where SYS.ARGV is used to capture parameters passed in when executing a python script

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3   4 Import SYS 5   6 Print sys.argv

Vi.. pyc file

When you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated after the Python interpreter was compiled.

Note: Code is compiled to generate bytecode, and bytecode can be obtained by decompile.

Seven, variable

1. Declaring variables

" Tom "

# Name: variable name, Tom: variable content, =: Assignment number

The function of a variable: to refer to something in memory by means of a symbol

Rules for variable definitions:

    • Variable names can only be any combination of letters, numbers, or underscores
    • The first character of a variable name cannot be a number
    • The following keywords cannot be declared as variable names
      [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

2. Assigning values to variables

" Wupeiqi "  "Alex"

" Wupeiqi "  = name1

Eight, input

Get user input: Input function

For example:

>>> name = input (" Please enter user name:") Please enter your username: Tom>>> name'  Tom'

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

Import getpass>>> pwd = getpass.getpass () Passwd:>>> pwd123456

Note: Python's IDE does not support hidden echoes, and Windows CMD can

Ix. Process Control and indentation

Example one: User logon verification

#!/usr/bin/env python#-*-coding:utf-8-*-  #loading the Getpass libraryImportGetpass name= Input ('Please enter user name:') PWD= Getpass.getpass ('Please enter your password:')  ifName = ="Tom"  andPWD = ="111111":    Print "Welcome, tom!. "Else:    Print "incorrect user name and password"

Example two: Export its permissions based on user input

#print their permissions based on what the user has entered  #Alex--Super Admin#Eric--General Administrator#Tony,rain--- business executives#other--Ordinary usersname= Raw_input ('Please enter user name:')  ifName = ="Alex":Print "Super Admin"elifName = ="Eric":    Print "General Administrator"elifName = ="Tony" orName = ="Rain":    Print "Business Supervisor"Else:    Print "Normal User"

Ten, while loop

1. Basic cycle

 while < conditions >:    < loop body >#  If the condition is true, execute the loop body #  If the condition is false, do not execute the loop body 

2.

Break: End Loop

Continue: Skips the current loop and starts the next loop directly

Be especially careful not to misuse break and continue statements. Break and continue can cause too many logical forks for code execution and error-prone. Most loops do not need to use the break and continue statements, the above two examples can be removed by overwriting the loop condition or modifying the loop logic, and removing the break and continue statements.

Sometimes, if the code is written with a problem, it will cause the program to fall into a "dead loop", that is, forever loop. You can then use CTRL + C to exit the program, or force the end of the Python process.

Python learns "02" Python Basics

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.