The first day of Python

Source: Internet
Author: User
Tags python script

Install Python

First, Windows
1. Download the installation package:

https://www.python.org/downloads/

2. Installation directory:

Python installed in the C \ Directory

3. Configure Environment variables:

"Right-click Computer" – "Properties" – "Advanced system Settings" – "Advanced" – "Environment Variables" – "In the Second content box, locate a row with the variable named path, double-click the" –> "Python installation directory to append to the variable value, and split"

Second, Linux

PYTHON-V Check Version

1, install GCC, for compiling;

2, download the source package, https://www.python.org/ftp/python/;

3, decompression and access to source files;

4. Compile and install

./configuremake allmake install

5. View version
/usr/local/bin/python2.7-v
6. Modify the default Python version
mv/usr/bin/python/usr/bin/python2.6
Ln-s/usr/local/bin/python2.7/usr/bin/python

7. To prevent Yum from performing exceptions, modify the Python version used by Yum
Vi/usr/bin/yum
Change the head #!/usr/bin/python to #!/usr/bin/python2.6

Third, Python's first code

Create a hello.py file

Print (' Hello world ')

Execute Python hello.py to

Iv. Explanatory device

The role of the interpreter: explicitly stating that the hello.py script is executed by the Python interpreter;

#!/usr/bin/env python

--Coding:utf8- -<--– function is to specify the character set, can support Chinese display (there is a strict distinction on the python2, Python3 not)

Print (' Hello world ')

V. Notes

When the line stares: # is annotated content
Multiline Comment: "" "Annotated Content" ""

Vi. 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

#!/usr/bin/env python
#--Coding:utf8--

Import Sys

Print (SYS.ARGV)

Vii.. 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.

Viii. variables

1. Defining variables

#!/usr/bin/env python

#--Coding:utf-8--

Name = "Xiaobai"
The code above declares a variable named: Name, and the value of the variable name is: "Xiaobai"
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 ']

Nine, enter input

#!/usr/bin/env python

#--Coding:utf-8--

Name = input (' Please enter your name: ')

Print (name) output variable name

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

#!/usr/bin/env python

#--Coding:utf-8--

Import Getpass

#将用户输入的内容赋值给 the name variable

PWD = Getpass.getpass ("Please enter password:")

Print (PWD)

Ten, Process Control

Requirement One: Login verification password

#!/usr/bin/env python

#--Coding:utf-8--

#提示输入用户名

#提示输入密码

#判断用户名与密码是否正确 if the correct output login successful welcome xxx if failed output login failed, please re-login

Import Getpass

Name = input (' Please enter user name: ')

PWD = input (' Please enter password: ')

If name = = "Root" and pwd = = "123":

Print (' login successful, welcome root ')

Else

Print (' Login failed, please re-login ')

Requirement Two: Export its permissions according to user input content

#根据用户输入内容打印其权限

#xiaobai Super Admin

#xiaoqiang General Administrator

#xiaohong

#其他 Ordinary Members

Name = input (' Please enter your name: ')

If name = = Xiaobai:

Print (' Super admin ')

elif name = = Xiaoqiang:

Print (' normal admin ')

elif name = = Xiaohong:

Print (' housing ')

Else

Print (' normal member ')

Add: "=" represents the assignment "= =" For comparison,

Xi. and while Loops

1. Basic cycle

#while conditions:

#如果条件为真, execute the statement;

#如果条件为假, the statement is not executed;

2. Break

Break to exit all loops

While true:

    print (‘python’)    break    print (‘666’)

The above statement only outputs Python, and exits the loop after a break.

3, continue

Continue is used to exit the current loop and continue the next loop

While true:

print (‘python’)continueprint (‘666’)

Output is always output python

Exercises

1. Use while loop input 1 2 3 4 5 6 8 9 10
2. For all numbers of 1-100
3. All odd numbers in output 1-100
4. All even numbers in output 1-100
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
6. User Login (three chance retry)

The first day of Python

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.