输入和输出

安装Pycharm: https://blog.csdn.net/u014044812/article/details/78727496

 

输出:

就只有一个语句 print ()

1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 
4 print ("Hello World")

打印出结果,python 有一点比较方便的就是不用管是什么数据类型,直接打印即可。

但是其输出也有一定的输出格式

1.输出不同的进制

1 >>> print ('%o' % 20)
2 24
3 >>> print ('%d' % 20)
4 20
5 >>> print ('%x' % 20)
6 14
7 >>> 

2.小数的输出

 1 >>> print('%f' % 1.11)  # 默认保留6位小数
 2 1.110000
 3 >>> print('%.1f' % 1.11)  # 取1位小数
 4 1.1
 5 >>> print('%e' % 1.11)  # 默认6位小数,用科学计数法
 6 1.110000e+00
 7 >>> print('%.3e' % 1.11)  # 取3位小数,用科学计数法
 8 1.110e+00
 9 >>> print('%g' % 1111.1111)  # 默认6位有效数字
10 1111.11
11 >>> print('%.7g' % 1111.1111)  # 取7位有效数字
12 1111.111
13 >>> print('%.2g' % 1111.1111)  # 取2位有效数字,自动转换为科学计数法
14 1.1e+03

3.字符串的输出

%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串

>>> print('%s' % 'hello world')  # 字符串输出
hello world
>>> print('%20s' % 'hello world')  # 右对齐,取20位,不够则补位
         hello world
>>> print('%-20s' % 'hello world')  # 左对齐,取20位,不够则补位
hello world         
>>> print('%.2s' % 'hello world')  # 取2位
he
>>> print('%10.2s' % 'hello world')  # 右对齐,取2位
        he
>>> print('%-10.2s' % 'hello world')  # 左对齐,取2位
he

4.其他的字符串格式代码

5.常用转义字符如下

 

接下来是输入

输入一些字符串,每次输入都是以字符串的形式

1 >>> name = input()
2 hello
3 >>> name
4 'hello'
5 >>> 

当你想用的是输入整数的时候,可以直接进行转化

1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 
4 a = input()
5 b = input()
6 
7 sum = int(a) + int(b)
8 print (sum)

运行后

 1 2 3 >> 

同时:  python 可以同时为多个变量赋值,如 a,b = 1,2

     一个变量可以通过赋值指向不同类型的对象 

     数值的除法总是返回一个浮点数,要获取整数需要使用//操作符

     在混合计算时,python总是先把整型转换为浮点数

 

输出的时候

print("价格是{:.2f}元".format(c))

其中的 { }表示槽,后续变量填充到槽中

{:.2f} 表示将变量c填充到这个位置时取小数点后两位

 

posted @ 2018-03-15 20:36  ouyang_wsgwz  阅读(202)  评论(0编辑  收藏  举报