The path to Python-basic knowledge 02

Source: Internet
Author: User
Tags python script

First, Hello world!

Assuming you have already installed Python, enter it on the Linux command line:

$python

Will go directly to Python. Then, at the command line prompt >>> later enter:

>>>print (' Hello world! ')

You can see it and then output it on the screen:

Hello world!

Second, the Interpreter

If you want to execute a python 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

123 #!/usr/bin/env python print"hello,world"

So, execute:. /hello.py Can.

PS: Need to give hello.py execute permission before execution, chmod 755 hello.py

Third, Content coding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)
PS: What should be shown tells the Python interpreter what code to use to execute the source code. (2.7 #-*-conding:utf--*-3.X default utf-8)

#!/usr/bin/env python#-*-coding:utf-8-*-  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, which makes developing Python programs very concise. The class library includes three:

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

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

123456 #!/usr/bin/env python# -*- coding: utf-8 -*- import sys  printsys.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.

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

Seven, variable

declaring variables

1234 #!/usr/bin/env python# -*- coding: utf-8 -*- name = "Wupeiqi"

The code above declares a variable named: Name, and the value of the variable name is: "Wupeiqi"

the role of a variable : a nickname that refers to what is stored in an address in memory

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 ']

#一个等号是赋值, the two equals sign is the comparison,! = is not equal to.

Eight, input
12345678 #!/usr/bin/env python# -*- coding: utf-8 -*- # 将用户输入的内容赋值给 name 变量name = raw_input("请输入用户名:")  # 打印输入的内容print name

2.7 Medium Raw_input
3.X in input

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

12345678910 #!/usr/bin/env python# -*- coding: utf-8 -*- import getpass  # 将用户输入的内容赋值给 name 变量pwd = getpass.getpass("请输入密码:")  # 打印输入的内容printpwd

Ix. Process Control and indentation

Requirement One, user login verification

1234567891011121314151617181920 #!/usr/bin/env python# -*- coding: encoding -*- # 提示输入用户名和密码 # 验证用户名和密码#     如果错误,则输出用户名或密码错误#     如果成功,则输出 欢迎,XXX!importgetpass   name =raw_input(‘请输入用户名:‘)pwd =getpass.getpass(‘请输入密码:‘) ifname == "alex"andpwd =="cmd":    print"欢迎,alex!"else:    print"用户名和密码错误"

Demand two, according to the user input content output its permissions

123456 # 根据用户输入内容打印其权限 # alex --> 超级管理员# eric --> 普通管理员# tony,rain --> 业务主管# 其他 --> 普通用户
1234567891011 name =raw_input(‘请输入用户名:‘)   ifname =="alex"    print"超级管理员"elifname =="eric":    print"普通管理员"elifname =="tony"orname =="rain":    print"业务主管"else:    print"普通用户"

PS: outer variables, which can be used by inner variables, inner variables, cannot be used by outer variables.

Ten, while loop

1. Basic cycle

12345 while条件:         # 循环体    #当循环到尾部时,判断条件是否True,如果是继续执行。

2. Break

Used to jump out of the current loop, and the following code is no longer executed.

1234 whileTrue:    print "123"    break    print"456"

3, continue

Used to jump out of this loop and re-execute the next loop.

1234 whileTrue:    print "123"    continue    print"456"

XI. encoding, decoding

#py2 #-*-Conding:utf-8-*-temp = "Jay Chou" #utf -8# decoding, need to specify what the original encoding Temp_unicode = Temp.decode (' utf-8 ') #编码, you need to specify what encoding to become TEMP_ GBK = Temp_unicode.encode (' GBK ') #py3自动转换utf -8unicodegbk#py3 removes the python unicode type temp = "Jay Chou" #utf -8TEMP_GBK = Temp.encode (' GBK ') print (TEMP_GBK)

12. Operators

#算术运算 +-*/% modulo operation  can be used to determine parity power//take divisible PY2:9/2 = 49/2 = 4.5 (Import module from __future__ Import Division) PY3:9/2 = 4.5# comparison operation = = equals! = Not equal to <> not equal to (2) <>>=<= #赋值运算 =+=-=*=/=**=%=//= #逻辑运算and与or或not非 # member operation in

  

The path to Python-basic knowledge 02

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.