Home  >  Article  >  Backend Development  >  Python automated operation and maintenance course learning

Python automated operation and maintenance course learning

高洛峰
高洛峰Original
2017-02-13 17:25:461877browse

This article is a summary of the learning content on the first day of the Old Boy Python automated operation and maintenance course.

The general content is as follows:

Introduction to Python

The first Python program: Hello World

Python variables

User interaction (user Input, output)

Process control: conditional statements (if/elif/else), loop statements (for/while/break/continue)

1. Introduction to Python language:

1. Python is a high-level programming language with interpreted language, dynamic type, and strong type definition language. Developed by Guido van Rossum during the Christmas period of 1989, the first official version of the Python compiler was born in 1991. It has become one of the mainstream programming languages.

2. Mainly used in cloud computing, WEB development, scientific research and data analysis, artificial intelligence, finance, system operation and maintenance, graphical GUI, etc.

3. Advantages and disadvantages of Python:

Advantages: simple, clear, elegant; high development efficiency; strong portability; scalability; embeddability.

Disadvantages: Slower execution than C language/JAVA (the PyPy interpreter sometimes executes faster than C); code cannot be encrypted (interpreted language); threads cannot take advantage of multi-CPU issues.

4. Python interpreter: There are many Python interpreters, such as CPython, IPython, PyPy, Jython, IronPython, etc., but the most widely used one is CPython.

2. Regarding the environment for running all Python codes in this article:

 --Operating system: Ubuntu 16.10 (Linux 4.8.0)

   Python automated operation and maintenance course learning

 --Python version: 3.5.2

    Python automated operation and maintenance course learning

   --Python IDE: PyCharm 2016.3.2

     Python automated operation and maintenance course learning

三, The first program: Hello World

Use the vim/vi command to create a new Python File, the command is "HelloWorld.py", vim HelloWorld.py.

Enter the positive content in HelloWorld.py:

#!/usr/bin/python3.5                     
# 告诉Linux系统,要通过/usr/bin/python3.5解释器来执行正面的代码
# -*- coding: utf-8 -*-           
# Python2中必须添加这个一行,告诉Python解释器,要以UTF-8的编码形式执行正面的代码;Python3中默认UTF-8,可以不用添加本行。
# Author: Spencer Jiang           
# 作者

print("Hello, World!")           
# 打印Hello, World!

Two operating modes:

1), give Grant executable permissions to HelloWorld.py, and then execute: chmod 755 HelloWorld.py

> chmod 755 HelloWorld.py
> ./HelloWorld.py

    Python automated operation and maintenance course learning

     2), directly through Python Execution: python installation path

> /usr/bin/python3.5 HelloWorld.py

  Python automated operation and maintenance course learning

3. Python variables

 1 , Rules for variable definition:

      • The variable name can only be any combination of letters, numbers or underscores

      • Variable The first character of the 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', 'from', 'global', 'if' , 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', ' with', 'yield']


    • ## Examples of identifier names are i , __my_name, name_23 and a1b2_c3.

    • Examples of identifier names are 2things, this is spaced out and my-name.


 2. Variable assignment: assign a value to a variable through an equal sign

Example: name = "Spencer Jiang"

  You can also assign multiple variables in one line. Such as:

a, b = 3, "jmw"
print(a, b)
print(type(b), type(a))

######### 下面为输出结果:
3 jmw
<class> <class></class></class>

 

4. User interaction and formatted output:

User input Python3 uses the input() function That's fine. Pyhton2 is a bit complicated, so I won't learn it yet.

   input()函数能接收从用户输入的任务字符,并以字符串类型返回用户输入的字符。

  示例1(UserInput.py): name = input("Please input your name: ")   

               age = int(input("Please input you age: "))    # 将用户输入的字符转换成int类型,再赋值给变量 age。

#!/usr/bin/python3.5
# -*- coding:utf-8 -*-
# Author: Spencer Jiang

name = input("Please input your name: ")

age = int(input("Please input you age: "))

print("Your Name: %s, Your Age: %d" % (name, age))

  Python automated operation and maintenance course learning

   示例2: 用户名、密码的输入,通过getpass模块,将密码隐藏显示。(HidePassword.py)  

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

import getpass
username = input("Please input your username: ")

password = getpass.getpass("Please input your password: ")

print(username, password)

    Python automated operation and maintenance course learning

     格式化输出:

    1)、print()函数中添加%号来格式化输出。

      输出字符串:%s ,输出数值 %d, 输出浮点数%f等, 示例:

#!/usr/bin/python
# -*- coding:utf-8 -*-
# Function : The format output
# Date : 2017-02-10
# Author : Spencer Jiang

username = "Spencer Jiang"
age = 45
salary = 231.32

print("Your name is : %s " % username)
print("Your age is : %d " % age)
print("Your salary is : %f " % salary)
print("Your salary2f is : %.2f " % salary)                        
# 保留2位小数

  Python automated operation and maintenance course learning

    2)、 通过format()函数进行格式化输出。

#!/usr/bin/python
# -*- coding:utf-8 -*-
# Function : The format output
# Date : 2017-02-10
# Author : Spencer Jiang

username = "Spencer Jiang"
age = 45
job = "IT Service"
salary = 231.32

info = '''
Name: [_username]
Age: [_age]
Job: [_job]
Salary: [_salary]
'''.format(_username = username, _age = age, _job = job, _salary = salary)

print(info)

  Python automated operation and maintenance course learning

五、流程控制:条件判断语句(if/elif/else):

  每个条件后面都以冒号结束,换行(条件为真时要执行的代码,以缩进作为代码块标志,python官方建议缩进4个空格)

      示例1:猜年龄(数字)游戏(GuessAge.py)。

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

age_of_spencer = 65         
#先设定的年龄的数值
guess_age = int(input("guess an age: "))

if guess_age == age_of_spencer :
    print("Yes, You got it!")
elif guess_age > age_of_spencer :
    print("No, your number is a litter bigger")
else:
    print("No, your number is a litter smaller")

  Python automated operation and maintenance course learning

六、流程控制:for循环(for x in range(10))、break、continue:

   当满足循环条件时,执行循环语句块的代码,当不满足循环条件时,循环语句就结束。

   for/while 循环外也可以跟一个else。

   break: 当执行break时,就结束整个循环;

   continue: 当执行continue,就结束本次循环,直接进行下次循环。

         示例1:输出0到15中的2、4、6、8等4个数字(PrintNumber.py)。

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

for i in range(0,15,2):
    if i == 0 :                                 
     # 跳过 0 这个数字
        continue
    if i > 9 :                    
     # 大于9 就退出循环
        break
    else:
        print(i)

  Python automated operation and maintenance course learning

 七、流程控制:while循环、break、continue(与for循环类似)

  while 循环,需要有一个计数器,或者在循环语句块中有终止while条件的语句,否则会一直运行下去。

      示例1(WhileLoop.py): 打印0~9数字

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

count = 0         
 # 计数器

while True :
    print(count)
    count = count + 1
    if count > 9 :
        break

  Python automated operation and maintenance course learning

  示例2(GuessAgeWhile.py):猜年龄(数字): 只能猜3次机会。  

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

age_of_spencer = 65

count = 0

while count  age_of_spencer :
        print("No, your number is a litter bigger")
    else:
        print("No, your number is a litter smaller")
    count += 1
else:
    print("You guess to much times!!!")

  Python automated operation and maintenance course learning

  

    示例2,每猜3次不正确后,弹出提示,看用户是否还要继续猜下去。如果用户输入的是“n"就表示停止。

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
# Author: Spencer Jiang

age_of_spencer = 65

count = 0

while count  age_of_spencer :
        print("No, your number is a litter bigger")
    else:
        print("No, your number is a litter smaller")
    count += 1    if count == 3 :
        continue_confirm = input("Do you want to continue to guess?")
        if continue_confirm != 'n' :
            count = 0else:
    print("You guess to much times!!!")

  Python automated operation and maintenance course learning

八、 Python代码注释: 

  #          单行注释用 井号“#” 开头

   ''' 或者 """      多行注释采用3对单引号或3对双引号将要注释的行包围进来。

       同时3对引号,也可以表示对字符串的赋值(段落文字),如:

info = """ your information :
name : jmw
age : 32
"""   

九、作业:

 1. Simulate user login interface: 1) The user enters the user name and password; 2) If the login is successful, a welcome message is displayed; 3) If the login fails more than 3 times, the account will be locked.

2. Three-level menu: The three-level regions of province, city and county are menus.

For more articles related to learning Python automated operation and maintenance courses, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn