table of Contents
7.31 PY formatted output Foundations and basic operators
Three ways formatted output
effect
The information entered by the user into a fixed format printing, due to the change of python version, there is a different formatted output, there is no practical effect, but can be more convenient to print out the result, more flowers in Hu show.
Placeholder
Placeholder with% s,% pass in variable names with characters you want to output.
.format format
{With} placeholder, with .format traditional values, Hu spent in the show, no practical effect.
f-String formatting
py3.6 unique version, format with print (f '{} {} {}'), simple to use and show flowers in HU. And you can customize an integer of decimal places, print (f '{s: .2f}')
use
For example: output format output in three ways: "My name is Wangzhi Hui, my height is 185, my weight is 110"
name = '汪智慧'
height = 185
weight = 110
print('My name is %s , my height is %s,my weight is %s'
%(name,height,weight)) # %s表示匹配所有字符
print("My name is {}, my height is {}, my weight is {}" .format(name,height,weight))
print(f'My name is {name}, my height is {height}, my weight is {weight}')
Basic operators
Arithmetic Operators
Arithmetic operators grabbed what we started from the primary school arithmetic, too simple, not to show ......
+ - * / % ** // (%取余,**幂,//整除)
Comparison Operators
Two conditions are compared, the Boolean value T / F
== != <> > < >= <= (!=与<>类似,都是表示不等于)
print('a' == 'abc') # False
print('a' != 'abc') # True
print( 1+1 == 2 ) # True
Assignment Operators
Value is about the value assigned to the variable, including monohydric and dihydric assignment operator assignment operator
Mono: i.e., arithmetic operators + - * /% // ** (% Remainder ** power, divisible //)
Two yuan: + = - = = * / ** =% = = = //
a += b # a=a+b
a %= b # a=a%b
a **= b # a=a**b
a //= b # a=a//b
Logical Operators
I.e. symbolic logic operations, Boolean operation to give
and or not
3 > 3 and 1 > 2 or 2 > 1 # False
Identity operator
Identity operator used to compare two of the memory cells.
and is the difference between ==: is configured to determine whether two variables refer to the same objects (whether in the same memory space), a reference value for determining == variables are equal.
x = 257
y = x
z = 257
print(f'x is y:{x is y}') # x is y:True
print(f'x == y:{x == y}') # x == y:True
print(f'x is z:{x is z}') # x is z:False
print(f'x == z:{x == z}') # x == z:True
Extended: python operator in priority
python operator precedence in mathematics equivalent of the first count and then count addition and subtraction multiplication and division, in fact, not necessary, your high priority brackets on the line ...