Python programming basics: input/output functions, comments and indentation

Python is a simple and easy-to-learn programming language that is widely used in web development, data analysis, artificial intelligence and other fields. Whether you're a beginner or someone with some programming experience, you can start building your programming skills with the basics of Python.

theory

Development of Python language

The inventor of Python language-Guido van Rossum (Dutch).

The Python language has a rich and powerful library that can easily connect various modules made in other languages ​​​​(especially C/C++).

The Python language is also called a glue language because it can easily combine various modules made in other languages.

  1. The Python language was born in 1989, but the earliest available version was born in 1991. It has experienced the evolution process from Python2 to Python3 in the past 20 years.
  2. In October 2000, Python version 2.0 was released, opening a new era of widespread application of Python.
  3. In 2010, the Python2.x system released the last version, with the main version number 2.7, which was used to end the development of the 2.x series and no longer make major improvements.
  4. In December 2008, Python 3.0 was released. This version of the interpreter was completely implemented in an object-oriented manner and made many major improvements at the syntax level.
  5. In 2016, all Python's important standard libraries and third-party libraries have evolved and developed under the Python 3.x version. The Python language version upgrade process has ended.

Insert image description here

  1. Application areas of Python
  2. Web development: The Python language is supported by many frameworks such as Django, Flask, and Tornado for web development.
  3. Data analysis and scientific computing: In terms of data analysis and scientific computing, Python is supported by many third-party libraries, such as Numpy, Pandas, Matplotlib, etc.
  4. Artificial intelligence and machine learning: Third-party libraries such as Tensorflow, Keras, and PyTorch in Python can quickly implement model construction, training, and deployment.
  5. Automated testing and operation and maintenance: The third-party library Selenium plays a decisive role in the field of automated testing and operation and maintenance. Automated testing has become one of the necessary skills for test engineers.
  6. Web crawler: With the rise of big data and data mining, crawler technology has played a very important role in the Internet. It can crawl a large amount of data on the Internet due to its fast speed.
  7. Game development: Python also has many applications in the game field. For example, "Civilization 6" is written in Python language.
  • Python development tools
  1. PyCharm

Insert image description here

Classification of programming languages

  • programming language

What programming language is it? It refers to an interactive system that allows the computer to identify and understand the user's operation intentions. To put it bluntly, it is a language that can communicate between the user and the computer. It is a computer instruction organized according to specific rules, and these instructions It allows computers to automatically perform various computing operations.

  1. Machine language: It is a binary language that directly uses binary code to express instructions. It is a programming language that can be directly recognized and executed by computer hardware.
  2. Assembly language: Use convenient mnemonics to correspond one-to-one with instructions in machine language.
  3. High-level language: It is a computer programming language close to natural language. Python and Java are both high-level languages.

The difference between static languages ​​and scripting languages

  • Compiled

Compiled type refers to the process of converting source code into target code. Usually the source code is high-level language code and the target code is machine language code. The computer program that performs compilation is called a compiler.

Insert image description here

  • Interpretive

Interpretation is the process of converting source code into target code one by one. The computer program that performs interpretation is called an interpreter. The portability of the interpreted version is better, but the disadvantage is that the source code needs to be saved at all times. If the source code is lost, the program cannot be run.

Insert image description here
Languages ​​that are executed in a compiled way become static languages, Java is a static language, languages ​​that are executed in an interpreted way are scripting languages, and Python, Javascript, and PHP are all scripting languages.

Code writing

How to write IPO procedures

Input is input, Process is processing, and Output is output.

Insert image description here

Output function print

Grammatical structures

print(输出内容)

In Python, you can use the built-in print function to perform the output operation of the program, which is the O, output in the IPO operation; the output content can be a number or a string. If it is a string, you need to use quotation marks. caused.

Select the folder and create a new Python file

Insert image description here
Insert image description here

Define a few variables first, and then use the print function to demonstrate the output.

Insert image description here

a=100 # 定义变量a,值为100
b=50 # 定义变量b,值为50
print(886)
print(a) # 实际上输出的变量的值
print(a+b) # 输出a和b的运算结果
print('摔跤猫子')
print("摔跤猫子")
print('''摔跤猫子''')
print("""摔跤猫子""")

Right-click to run and you can see that the variables and operation results are output on the console. When the output result is a string, you can use single quotes, double quotes, triple single quotes, or triple double quotes.

Insert image description here

What if you want to output multiple contents at once without line breaks? You can use English half-width commas to separate the output contents.

Insert image description here

print(a,b,'摔跤猫子')

Use the print function to output the characters corresponding to the ASCII code

Insert image description here

# 示例3 输出ASCII码对应的字符
print('b') # 直接输出了b
print(chr(98)) # 也输出了b 使用chr将98转换成ASCII表中的字符

Use the print function to output Chinese Unicode code

Insert image description here

# 使用print函数输出中文Unicode码
print(ord('摔'))
print(ord('跤'))
print(ord('猫'))
print(ord('子'))

You can also use the chr function to convert back. There is a one-to-one relationship between encoding and characters.

Insert image description here

# 转回去
print(chr(25684))
print(chr(36324))
print(chr(29483))
print(chr(23376))

Use the print function to output content to a file

fp=open('note.txt','w') # 打开文件,这里w指的是write,写的意思
print('摔跤猫子',file=fp) # 将摔跤猫子这几个字写入到note.txt文件中
fp.close() # 关闭文件

After running the function, a file will be generated in this directory
Insert image description here
Reload with GBK to display

Insert image description here
Insert image description here
The complete format of the print() function:

print(value,...,sep='',end='\n',file=None)

When multiple data are output at once without line breaks, how is the space in the middle generated?
It is called the separator generated by sep in the print function

Insert image description here

There is a blank line below each output. This is because of the \n decision in the print function.

Insert image description here
If the data of the end parameter is specified, blank lines will not be output.

print('摔跤',end='-->')

Insert image description here

Multiple print function outputs, the results are displayed in one line

Insert image description here

print('摔跤',end='-->')
print('猫子')

When outputting, you can also use the plus sign to connect two strings. During the connection process, you can only connect strings with strings, otherwise an error will be reported.

Insert image description here
Insert image description here

input function input

Insert image description here
Insert image description here

name=input('请输入您的昵称:')
print('您输入的昵称为:'+name)

Use int to convert the data. As you can see in the figure below, after the conversion is successful, you cannot use the plus sign to connect them, so you can use commas to output on the same line.

Insert image description here
Insert image description here

Comments and indentation in Python

The annotated text that programmers use to explain code functions in the code can improve the readability of the code. The content of the annotations is ignored by the Python interpreter and will not be executed by the computer. The annotations include single-number annotations, multi-line annotations and Chinese statements. Note.

Insert image description here

# coding=utf-8
# 这是中文声明注释,它一定要写在第一行

# 这是单行注释

'''
这是多行注释
'''

"""
这也是多行注释
"""

Guess you like

Origin blog.csdn.net/weixin_42794881/article/details/134639309