Python automated operation and maintenance course learning
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 version: 3.5.2
--Python IDE: PyCharm 2016.3.2
三, 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
2), directly through Python Execution: python installation path
> /usr/bin/python3.5 HelloWorld.py
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.
a, b = 3, "jmw" print(a, b) print(type(b), type(a)) ######### 下面为输出结果: 3 jmw <class> <class></class></class>
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))
示例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)
格式化输出:
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位小数
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)
五、流程控制:条件判断语句(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")
六、流程控制: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)
七、流程控制: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
示例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!!!")
示例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代码注释:
# 单行注释用 井号“#” 开头
''' 或者 """ 多行注释采用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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
