Hello, everyone, I am classmate Yu. I'm here to update the article again, I hope it can help everyone!
basic usage
output variable
Let's talk about output variables first. print
Functions have a huge advantage. As long as they are variables, they can be completely output, regardless of type.
Unlike C language, the type must be converted
a=10 #整型变量
print(a)
b=10.12 #浮点型变量
print(b)
c='CSDN' #字符串变量
print(c)
d=('1', 2, 'hello') #元组变量
print(d)
e=['1', 2, 'hello'] #列表变量
print(e)
f={
'hello':1, 'world':2} #字典变量
print(f)
operation result:
You can also output multiple variables at once, ,
separated by commas (commas must be used)
a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f)
Python
The default spacer is a space, we can change the spacer to other characters
such as:
a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f,sep="\n") #'\n'表示换行符
Change the spacer to a dot.
a=10 #整型变量
b=10.12 #浮点型变量
c='CSDN' #字符串变量
d=('1', 2, 'hello') #元组变量
e=['1', 2, 'hello'] #列表变量
f={
'hello':1, 'world':2} #字典变量
print(a,b,c,d,e,f,sep=".") #'\n'表示换行符
format string
directly on the form
Format | illustrate |
---|---|
%s |
string format |
%c |
Characters and their ASCII codes |
%d |
Decimal有符号整数 |
%u |
Decimal无符号整数 |
%f |
real number (with decimal point symbol) |
%e |
Real numbers (scientific notation) |
pending decimal
Note, this is a very important knowledge point, you must remember it!
ch=2.7182818
print('ch=%.*f' %(3,ch)) #保留3位小数
Let's see if Python
it will be rounded when retaining decimals
ch=1.1999
print('ch=%.*f' %(1,ch)) #保留1位小数
Answer:
It is very convenient to be rounded
Another way is to use format
the function
ch=3.1415926
print('ch={:.3f}'.format(ch))
Positive output
Output +
numbers with a positive sign ( ) (default 保留6位小数
):
ch=2.7182818
print("%+f" %ch)
Common format string usage
1. Output integer%d
time=13
print("It is %d o'clock." %time)
2, the output string is used%s
ch="programming"
print("I love %s ." %ch)
3. Output decimals%f
avg=95.5
print("The average score of our class is %f" %avg)
We found that %f
when using the format string, Python
by default 保留6位小数
, if you want to keep other digits, you can write like this
avg=95.5
print("The average score of our class is %.*f" %(1,avg))
Terminators and separators
terminator
print
The default terminator of a function is a newline ( \n
)
for i in range(0,5):
print("%d" %i) #print(i)
We can end=''
control the terminator with a function
For example, change the terminator to a space
for i in range(0,5):
print("%d" %i,end=" ") #结束符为空格
You can also use more characters to replace the terminator
for i in range(0,5):
print("%d" %i,end="~") #将结束符设为 ~
spacer
print
The default delimiter of the function is a space (
)
a=10
b=20
print(a,b)
We can sep=''
control the spacer with the function
a=10
b=20
print(a,b,sep="\n")
sep=''
Functions and end=''
function brackets can be filled任意字符