python basic syntax (2)

Comment

Comment syntax

Comment line

Python comments: Lines opened with # are comments

# 这是一行注释

Comments in C language: all comments using // are comments

// 这是一行注释

docstring

What is enclosed in triple quotes is called a documentation string, which can also be regarded as a comment.
Characteristics of stable strings:
1: Can contain multiple lines of content
2: Generally placed at the beginning of the file/function/class
3: """ or ''' Either can (equivalent)

Notes for python:

"""
这是文档字符串
这是文档字符串
"""

Notes on C language:

/*这是C语言的注释*/

Annotation specifications

  1. Accurate content: The content of comments should be consistent and matched with the code, and should be updated in a timely manner when the code is modified .
  2. Reasonable length: Notes should be neither too concise nor lengthy .
  3. Use Chinese: Most comments are like this, at least for me, unless the code you write is for foreigners to see

input Output

Interaction with users

The program needs to interact with the user.
The process by which the user transfers information to the program is called "input".
The process by which the program displays the results to the user is called "output".
Here are
some examples of input and output:
A piece of code in C language

#include<stdio.h>
int main()
{
    
    
int a=0;
printf("%d",scanf("%d",&a));
return 0;
}

Let's analyze it.
First, we input through the scanf function , and then print out the input function through the printf function. This is called output.

Output via console

The most basic method of input and output is the console. The user inputs some strings through the console, and the program prints out some strings through the console.

The most common method of input and output is the graphical interface. For example, the QQ, browser, steam, etc. we usually use do not require the user to enter commands, but only operate by clicking the mouse on the window and clicking the button.

Python uses the print function to output to the console

print('hello')

Not only can it output a string, but it can also output a variable of other types.

a = 10
print(a)
b = True
print(b)

More often, what we want to be able to output is a mixture of strings and variables.
Example: Output num = 10

num = 10
print(f'num = {
      
      num}')

Note:
A string that uses f as a prefix is ​​called an f-string.
{ } can be used to embed another variable/expression.

Input via console

python uses the input function to read user input from the console

num = 0
num = input('请输入一个整数: ')
print(f'你输入的整数是 {
      
      num}')

Note:
The parameter of input is equivalent to a "prompt message", or it may not be present. The
return value of input is the content entered by the user. It is a string type.

a = input('请输入第一个整数: ')
b = input('请输入第二个整数: ')
print(f'a + b = {
      
      a + b}')

The final result is
a+b=1020.
The result here is string concatenation, not arithmetic operation. If you want to perform arithmetic operation, you need to convert the type first.

a = input('请输入第一个整数: ')
b = input('请输入第二个整数: ')
a = int(a)
b = int(b)
print(f'a + b = {
      
      a + b}')

The result is that
the variable is converted to the int type through int().
Similarly, the corresponding type conversion can be completed using float(), bool(), str(), etc.

We can see that the input function is actually similar to the scanf function in C language, but if the user inputs a string in the input function, arithmetic operations will not be performed. This is also some of the differences between the two.

We can see that the forced type conversion in python is int(a), while the forced type conversion in C language is (int)a. This is also the difference between the two.

Code example: Enter 4 decimals and find the average of the 4 decimals.

a = input('请输入第一个数字: ')
b = input('请输入第二个数字: ')
c = input('请输入第三个数字: ')
d = input('请输入第四个数字: ')
a = float(a)
b = float(b)
c = float(c)
d = float(d)
avg = (a + b + c + d) / 4
print(f'平均值: {
      
      avg}')

Guess you like

Origin blog.csdn.net/2301_79178723/article/details/132802661