[Python] input and output and operator

Table of contents

1. Input and output

1.1 Interaction with users

1.2 output through the console

1.3 Input via console 

2. Operators

2.1 Arithmetic operators

2.2 Relational Operators

 2.3 Logical operators

 2.4 Assignment Operators


1. Input and output

1.1 Interaction with users

The program needs to interact with the user

The process by which the user passes information to the program, called "input"

The process by which package results are displayed to the user, called "output"

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

For example:

Pycharm runs the program, and the pop-up window below can be regarded as the console

 The most common method of input and output is a graphical interface, such as QQ, NetEase Music, browser, etc.

1.2 output through the console

Python uses the print function to output to the console


print('hello')

Other types of variables can also be output

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

It is also possible to mix strings and variables in the output

For example: output a = 10

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

 Note: 

Strings prefixed with f are called f -strings
Inside you can use { } to embed another variable / expression

1.3 Input via console 

Python uses the input function to read user input from the console .

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

 

 Note:

The input parameter is equivalent to a " prompt message ", or there is no
The return value of input is the content entered by the user . It is a string type

 For example: I input a + b in the console, ideally it should be sum, but the final output is string concatenation

a = input('')
b = input('')
print(f'a + b = {a + b}')

 For example: if you want the output result to be sum, you need to convert the variable to int 

a = input('请输入第一个数:')
b = input('请输入第二个数:')

a = int(a)
b = int(b)
print(f'a + b = {a + b}')

Note: 

Use int() to convert the variable to int type .
Similarly , use float( ), bool( ), str( ), etc. to complete the corresponding type conversion.
Code example : input 4 decimals , find the average of 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}')

2. Operators

2.1 Arithmetic operators

Like + - * / % ** // These operators that perform arithmetic operations are called arithmetic operators

1) / Take the result as a division operation, unlike c/java to take the integer part of the result

example:


a = 5 / 2
print(a)

 / is the same as our usual division rule, 0 cannot be used as a divisor, otherwise an exception will be thrown

example:

a = 5 / 0
print(a)

2) % is to find the remainder

Example 7 When divided by 3, the quotient is 2 and the remainder is 1 

a = 7 % 2
print(a)

 3) ** is a power, which is how many powers in arithmetic

Not only can count integer powers , but also decimal powers

Example: 4 to the power of 2 is 16

        4 to the power of 0.5 is 2

a = 4 ** 2
b = 4 ** 0.5
print(a)
print(b)

 4) // It is rounding and division ( also called floor division ). Integers are divided by integers , and the result is taken as an integer (directly discard the decimal part, and round down to take an integer smaller than the result, not rounded) 

Example: Divide 7 by 2 and the result is 3.5, take 3

       The result of dividing -7 by 2 is -3.5, take -4 (here it is obvious that the rounding down is reflected)

a = 7 // 2
b = -7 // 2
print(a)
print(b)

2.2 Relational Operators

A series of operators like < <= > >= == != are called relational operators , which compare the relationship between operands
<= is " less than or equal to "
>= is " greater than or equal to "
== is " equal to "
!= is " not equal to "

 1) Compare integers

The expression returns True if the relation is true . The expression returns False if the relationship does not match

a = 10
b = 20
print(a < b)
print(a <= b)
print(a > b)
print(a >= b)
print(a == b)
print(a != b)

 2) Compare strings

String comparison size , the rule is " dictionary order "
About lexicographic order :
Imagine an English dictionary , the words above are arranged in alphabetical order . If the first letter is the same , compare the second letter . ( such as the famous word abandon).
We think that the closer a word is in the dictionary , the smaller it is , and the later it is , the bigger it is .

example:

a = 'hello'
b = 'world'
print(a < b)
print(a <= b)
print(a > b)
print(a >= b)
print(a == b)
print(a != b)

 

3) Floating point comparison

For floating-point numbers , do not use == to determine equality.

Because the representation of floating point numbers in the computer is not accurate ! In the calculation process , very small errors are prone to occur

example:

print(0.1 + 0.2 == 0.3)

See what the result of 0.1 + 0.2 is in Python

print(0.1)
print(0.2)
print(0.3)
print(0.1 + 0.2)

 

 It can be seen that the result of 0.1 + 0.2 is not 0.3. This is a problem introduced by the floating-point number format stipulated in the IEEE754 standard

The correct comparison method : no longer strictly compare equality , but determine that the difference is less than the allowable error range
a = 0.1 + 0.2
b = 0.3
print(-0.000001 < (a - b) < 0.000001)

 2.3 Logical operators

A series of operators like and or not are called logical operators
and and. If both operands are True, the final result is True, otherwise it is False. ( one false is false )  
or or. Both operands are False, and the final result is False. Otherwise True. ( one true is true )  
not Logical inversion. If the operand itself is True, return False. Itself is False, returns True.  

example:

a = 10
b = 20
c = 30

print(a < b and b < c)
print(a < b and b > c)
print(a > b or b > c)
print(a < b or b > c)
print(not a < b)
print(not a > b)

 

 Note: The operation a < b and b < c is equivalent to a < b < c . This setting is different from most programming languages

 About short-circuit evaluation

Similar to other programming languages , Python also has rules for short-circuit evaluation .
For and, if the expression on the left is False, the whole must be False, and the expression on the right is no longer executed.
For or, if the expression on the left is True, the whole must be True, and the expression on the right is no longer executed

example:

print(10 > 20 and 10 / 0 == 1)
print(10 < 20 or 10 / 0 == 1)

 

 2.4 Assignment Operators

1) Use of =

= means assignment . Note the distinction from ==
= In addition to basic usage , you can also assign values ​​to multiple variables at the same time
example:
chain assignment
a = b = 10

multiple assignment

a, b = 10, 20
Code Example : Swap Two Variables
Basic writing
a = 10
b = 20
tmp = a
a = b
b = tmp
Based on multiple assignment
a = 10
b = 20
a, b = b, a
2) Compound assignment operator
Python also has some compound assignment operators. For example += - = *= /= %=  
where a += 1 is equivalent to a = a + 1. The same is true for other compound assignment operators
example:
​​​​​​​
a = 10
a = a + 1
print(a)

b = 10
b += 1
print(b)

 

Guess you like

Origin blog.csdn.net/m0_60494863/article/details/127841740