Python Chapter 3 - Input and output operators

Input and output operators

An input and output

1.1 Output

`print()`函数用来向终端输出数据(其实也可以向文件输出数据,后面再讲)

  • When the value of a plurality of parameters can be passed, the output will python plurality of parameters separated by spaces.

    print("hell", "world", "你好")
    

  • print()Function will automatically add a default at the end \n. If you do not want to wrap you can pass a parameterend=""

    print("hell", "world", "你好", end="")
    print("哈哈哈")
    

1.2 input

input() Obtaining user input can be a keyboard, input data string will become.

name = input()	# 把从键盘输入的数据存储到变量 name 中
print("你的名字是:", name)

If a prompt to the user before the user input, you can inputpass the parameter, pythonwill use the argument passed to print()the function outputs, and then waits for the user to enter data.

name = input("请输入你的姓名:")
print("你的名字是:", name)

Second, the operation and expression

2.1 Expression

Most of the code we wrote earlier contains the expression ( Expressions). For example, 2 + 3it is an expression.

Expression can be further split into operator (Operators) and operands (the Operands)

E.g:

expression:2 + 3 * 4

among them:

+、*Is the operator

2、3、4Is the number of operations


python provides a variety of operators.

Any type of data can be used as an operand.

Operators 2.2

2.2.1 Arithmetic Operators

addition:+

When +at both ends are numbers, is the summation mathematics.

When +both ends are strings, the string is connected at both ends.

note:

  1. Boolean values Truecan be used as digital 1to use, Falseit can be used as a digital 0, so they can and numbers together. (Completion of object-oriented you are clear, in fact, boolinherited from int)
  2. string numbers together and allowed python
add1 = 3 + 4
print(add1)   # 7

add2 = 3 + True
print(add2)	# 4

add3 = 3 + False
print(add3)	# 3

add4 = True * 0
print(add4)	# 0

# add5 = "a" + 3  # 错误!不允许字符串和数字相加

Subtraction:-

Digital subtraction can only be used for operations.

multiplication:*

To give the product of two numbers. If a string is a integer, string, integer repetition is obtained.

result1 = 3 * 4.1
print(result1)

result2 = 2 * "Hello "
print(result2)

result3 = "world " * 3
print(result3)

power:**

x ** yReturn xof ypowerx^y

division:/

/That is, the division refers to mathematics (also known as true division), always get the floating-point number. And other language quite different.

result1 = 10 / 2
print(result1)

result2 = 10 / 3
print(result2)

result3 = -11.3 / 3
print(result3)

Divisible (Classic Division)://

Typically used to operate an integer divisible by two. (Although it could be floating point operations, but no one to do so)

result1 = 10 // 2
print(result1)

result2 = 10 // 3
print(result2)

result3 = -10 // 3
print(result3)

result4 = 10 // -3
print(result4)

Take the remainder (modulo):%

Returns the remainder after division. Generally used for integer modulus. Float can also be used

result1 = 10 % 2
print(result1)

result2 = 10 % -3	#  余数的符号与除数的符号相同。
print(result2)

result3 = -10 % 3
print(result3)

result4 = -2.4 % 2  # 取模一般用于整数运算。浮点数意义不大
print(result4)

2.2.2 Assignment Operators

= The most common assignment operator.

There is also a known compound assignment operators.

+=

a = 3
a += 2   # 等价于 a = a + 2
print(a)  # 5   

Arithmetic operator has a corresponding compound assignment operators.

2.2.3 Comparison operators

Compare two operands size relationship.

  1. If the two numbers to compare their size on mathematics.
  2. If the two strings are compared, they are compared in order code table. First compare the first letter, if equal then compare the second ...
  3. If there is a Boolean value involved in the comparison, then Truedo 1come by, Falsethey do 0come by.
  4. Numeric and string can not be compared!
  5. The results must be calculated comparison operators Boolean value.

Comparison operators operator comprising:

> 、< 、>=、<=、==、!=

Wherein: ==determining whether the two operands are equal, !=to determine whether two operands are unequal.

r1 = 3 > 2
print(r1)   # True

r2 = "ab" > "b1"
print(r2)   # False

r3 = True > False
print(r3)   # True

r4 = 1 == True
print(r4)   # True

note:

the biggest difference comparison operators and other languages are python, python comparison operators can be composed of links. Only the results of each operator are calculated True, the final result will be to True. OtherwiseFalse

r1 = 5 > 4 > 3
print(r1)	# True   因为 5 大于 4是 True,并且 4 大于 3 是 True

r2 = 5 > 4 == (2 + 1)
print(r2)	# False  因为 5 大于 4 是 True,但是 4 == 2 + 1 是 False

2.2.4 Logical Operators

Total python supports three logical operators.

and、or、not(AND, OR, NOT)

Logical NOT:not
r1 = not True
print(r1)	# False

r2 = not False
print(r2)	# True

note:

  1. For not, the result is always True 或 False, but the number can be any type of operation.
  2. It is empty of data, as will Falsebe treated, so notthen will becomeTrue
r1 = not True
print(r1)	# False

r2 = not False
print(r2)	# True

print(not 1)	# False

print(not 0)	# True

print(not "")	# True	

print(not [])	# True

print(not ())	# True

print(not {})	# True
And logic:and

There is a FalseresultFalse

Since any type can participate in the operation, so the more precise rules should be:

If the first one is False, or the equivalent False, the result is the first data, otherwise the end result should be the second data .

print(False and True)	# False
print(False and False)	# Fasle

print(0 and True)	# 0
print([] and False)	# []
print([1] and 20) # 20
print("a" and []) 

andHaving a short-circuit, that is, if the expression is a first Falseor equivalently False, is not determined again the second expression.

print(True and print("abc"))

print(False and print("bcd"))	# 由于第一个表达式是 False,所以不会计算第二个表达式的值

Note: It is Falseseven kinds of data

False、None、0、""、[]、()、{}

Or logic:or

There is a TrueresultTrue

Since any type can participate in the operation, so the more precise rules should be:

If the first one is True, or the equivalent True, the result is the first data, otherwise the end result should be the second data .

print(True or False)
print(False or True)
print(False or False)
print(1 or False)
print("" or 100)
print([] or (2,))

orAlso it has the effect of a short circuit, that is, if the expression is a first Trueor equivalently True, is not determined again the second expression.

Third, the operator priority

lambda Lambda expression
ifelse Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
*, @, /, //, % Multiplication, matrix multiplication division, remainder [5]
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation [6]
await x Await expression
x[index], x[index:index], x(arguments...), x.attribute Subscription, slicing, call, attribute reference
(expressions...), [expressions...], {key: value...}, {expressions...} Binding or tuple display, list display, dictionary display, set display

The priority is getting higher and higher from the top down.

When in actual use, for the place is not very clear, it is recommended to add parenthesis ( ()) to change the priority. Thus more readable.

For example: Although you know *it takes precedence over +, but the code was significantly better than code 2 1

Code 1:

2 + (3 * 4)

Code 2:

2 + 3 * 4

But do not abuse the use of parentheses. For example, the following is a bit abused. All without the outermost.

(2 + (3 * 4))

2.2.7 a few built-in functions related to digital type

abs(x)Returns xthe absolute value of the
argument xmust be an integer or floating

int(x)To xconvert an integer

xIt can be said floating point and integer. If you pass a string, the string of characters must be an integer

float(x)To xconvertfloat

x It can be a float, integer, string.

pow(x, y)Equivalent to x ** y

print(abs(-20))
print(int(20.3))
print(int("40"))
print(float("20.4"))

Guess you like

Origin www.cnblogs.com/yanadoude/p/12604387.html