从零开始的Linux运维屌丝之路,资源免费分享平台   运维人员首选:简单、易用、高效、安全、稳定、社区活跃的开源软件

4、Python格式化输出、运算符

发布:蔺要红03-02分类: Python

Python快速入门:格式化输出、运算符

一、格式化输出
f  = float
d = digit
s = string
当为 Age :       %s  的时候、不管输入数字还是字符串都不会报错 
%s  :      占位符
[root@test01 ~]# cat user_input.py 
#!/usr/bin/python
name = input("姓名:")
age = input("年龄:")
job = input("工作:")
hometown = input("家庭地址:")
info = '''
------------info of %s----------
Name:        %s
Age :        %s
Job :        %s
Hometown:    %s
-------------end----------------
'''%(name, name,age,job,hometown)
print(info)
[root@test01 ~]# python user_input.py 
姓名:蔺要红 
年龄:22
工作:运维
家庭地址:山东临沂

------------info of 蔺要红----------
Name:        蔺要红
Age :        22
Job :        运维
Hometown:    山东临沂
-------------end----------------
限制输入的为数字 
Age :        %d   #input输入的都为string 所以就算设置为d% 也是字符串
可以通过打印type来判断
[root@test01 ~]# cat user_input_test.py 
#!/usr/bin/python
name = input("姓名:")
age = input("年龄:")
print("age",type(age))
print("name",type(name))
[root@test01 ~]# python user_input_test.py 
姓名:lin
年龄:22
age <class 'str'>
name <class 'str'>
input输入的都为str(字符串)  更正
[root@test01 ~]# cat user_input_test.py 
#!/usr/bin/python
name = input("姓名:")
age = int(input("年龄:"))   #int转成数字,把输入的结果再交给int
print("age",type(name))
print("name",type(age))
[root@test01 ~]# python user_input_test.py 
姓名:lin
年龄:22
age <class 'str'>
name <class 'int'>
改为:float
Age :        %f    #改为float
[root@test01 ~]# python user_input.py 
姓名:lin
年龄:22
工作:linux
家庭地址:shandong
age <class 'int'>

------------info of lin----------
Name:        lin
Age :        22.000000
Job :        linux
Hometown:    shandong
-------------end----------------
二、运算符
计算机可以进行的运算有很多种,不是加减乘除那么简单,运算符按照种类可以分为
算数运算
比较运算
逻辑运算
赋值运算
成员运算
身份运算
位运算
实践过程:
#逻辑运算实践
>>> a = 10
>>> b = 15
>>> a == 10 and b == 15
True
>>> a == 10 and b > 15
False
>>> a == 10 or b > 15
True
>>> not  a == 10 or b > 15
False
>>> a = 10
>>> b = 15
>>> a == b
False
>>> a > b
False
>>> a < b
True
>>> 10%2
0
>>> 11%2
1
>>> a != b
True
>>> a
10
>>> a +=3
>>> a
13
>>> a -=5
>>> a
8
>>> a // 2
4
>>> a // 3
2
>>> a
8
>>> a *=2
>>> a
16
>>> a /= 4
>>> a
4.0



 
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,如有侵权我会在24小时之内删除!

欢迎使用手机扫描访问本站