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>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))
示例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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.