python输入与输出165

  1 s = 'Hello,Runoob'
  2 
  3 print(s)
  4 
  5 str(s)
  6 print(s)
  7 
  8 print(repr(s))
  9 
 10 print(1/7)
 11 print(str(1/7))
 12 print(repr(1/7))
 13 
 14 print('repr()的用法')
 15 x = 10*3.25
 16 y = 200*200
 17 s = 'x的值为:'+str(x)+',y的值为:'+str(y)
 18 print(s)
 19 
 20 s = 'x的值为:'+repr(x)+',y的值为:'+repr(y)
 21 print(s)
 22 
 23 
 24 print('repr()可以转义特殊字符')
 25 hello = 'hello ,runoob\n'
 26 print(hello)#hello ,runoob
 27 
 28 hellos = repr(hello)
 29 print(hellos)#'hello ,runoob\n'
 30 
 31 ts = ('Google','Runoob')
 32 repr((x,y,ts))
 33 print(type(ts))
 34 print(x,y,ts)#32.5 40000 ('Google', 'Runoob')
 35 
 36 print('输出平方根与立方根')
 37 for x in range(1,11):
 38     # print(x.rjust(2),x*x,x*x*x),,AttributeError: 'int' object has no attribute 'rjust''
 39    # print(x.rjust(2),x*x,x*x*x)
 40     print(str(x).rjust(2),str(x*x).rjust(5),str(x*x*x).rjust(10))
 41 
 42 
 43 '方式二'
 44 for x in range(1,11):
 45    # print('{0}{1}{2}'.format(x, x ** 2, x ** 3))
 46     #2d即两个空格
 47    print('{0:2d}{1:5d}{2:6d}'.format(x,x**2,x**3))
 48 
 49 
 50 
 51 print('zfill()即补足空格')
 52 for x in range(1,11):
 53     # print(x.rjust(2),x*x,x*x*x),,AttributeError: 'int' object has no attribute 'rjust''
 54    # print(x.rjust(2),x*x,x*x*x)
 55     print(str(x).zfill(2),str(x*x).zfill(5),str(x*x*x).zfill(10))
 56 
 57 
 58 
 59 'format()的用法'
 60 print('{0}和{1}'.format('Google','Runoob'))
 61 print('{1}和{0}'.format('Google','Runoob'))
 62 print('{name}网址:{site}'.format(name = '菜鸟教程',site = 'www.site.com'))
 63 #点列表 Google, Runoob, 和 Taobao。
 64 print('站点列表{0},{1},{other}'.format('Google','Runoob',other = 'Taobao'))
 65 
 66 '!a,!s,!r格式化某个值之前对其进行转换'
 67 import math
 68 #常量 PI 的值近似为: 3.141592653589793。
 69 print('常量PI的近似值为{}'.format(math.pi))
 70 print(type(math.pi))
 71 
 72 #使用!r或者!s后输出的值都是float类型的?
 73 pi2=math.pi
 74 print('常量PI的近似值为:{!r}'.format(pi2))
 75 print(type(pi2))
 76 
 77 pi3=math.pi
 78 print('常量PI的近似值为:{!s}'.format(pi3))
 79 print(type(pi3))
 80 
 81 #常量 PI 的值近似为 3.142。
 82 print('常量PI的近似值为{0:.3f}'.format(math.pi))
 83 print('常量PI的近似值为{0:.2f},{1:.3f}'.format(math.pi,math.pi))#常量PI的近似值为3.14,3.142
 84 
 85 # Runoob     ==>          2
 86 # Taobao     ==>          3
 87 # Google     ==>          1
 88 #int类型的直接为:10d,如果是str类型的为:5,没有d
 89 table = {'Runoob':2,'Taobao':3,'Google':1}
 90 print(type(table))
 91 for name,num in table.items():
 92     print('{0:5}===>{1:10d}'.format(name,num))
 93 print(type(num))
 94 print(type(name))
 95 
 96 #Runoob: 2; Google: 1; Taobao: 3(不会这是什么?)==============================================================================
 97 table = {'Google': 1, 'Runoob': 2, 'Taobao': 3}
 98 print('Runoob: {0[Runoob]:d}; Google: {0[Google]:d}; Taobao: {0[Taobao]:d}'.format(table))
 99 
100 
101 #其中的5是什么意思呢?
102 import math
103 print('常量 PI 的值近似为:%5.3f。' % math.pi)
104 
105 'input()从标准输入读入一行文本,这里的标准输入就是指的键盘'
106 # 请输入:菜鸟教程
107 # 你输入的内容是:  菜鸟教程
108 # str = input('请输入:')
109 # print('您输入的内容是:',str)
110 
111 
112 
113 f = open('E:\\foo.txt','a')
114 f.write('Python是一个非常好的语言。\n是的,的确非常好wb+,a!!')
115 f.close()
116 
117 print('写入并读取打印出来')
118 #这部分是写入文件中内容
119 f = open('E:\\foo.txt','w')
120 f.write('Python是一个非常好的语言。\n是的,的确非常好w!!')
121 #这部分是打开并读取文件内容
122 f = open('E:\\foo.txt','r')#没有这句是打印不出来内容的
123 str = f.read()
124 print(str)
125 
126 print('readline()的用法')
127 print('到这里了')
128 f = open('E:\\foo.txt','r')
129 str1 = f.readline()
130 print(str1)
131 f.close()
132 
133 print('readlines()的用法')
134 f = open('E:\\foo.txt','r')
135 str2 = f.readlines()
136 print(str2)
137 f.close()
138 
139 print('迭代读取每行')
140 f = open('E:\\foo.txt','r')
141 for line in f:
142     print(line,end='')
143 
144 print('读取写入的字数')
145 f = open('E:\\foo.txt','w')
146 num = f.write('Python是一个非常好的语言。\n是的,的确非常好w!!')
147 print(num)
148 
149 print("向 foo1.txt 文件中写入‘'www.runoob.com', 14’")
150 f = open('E\\foo1.txt','w')
151 value = 'www.runoob.com'
152 s = str(value)
153 f.write(s,14)
154 
155 
156 print("向 foo1.txt 文件中写入‘'www.runoob.com', 14’")
157 f2 = open('E:\\foo1.txt','w+')
158 value = 'www.runoob.com'
159 s = str(value)
160 f2.write(s)
161 
162 f2 = open('E:\\foo1.txt','r')
163 s = f2.read()
164 # s = str(f2)
165 print(s)

猜你喜欢

转载自www.cnblogs.com/jpr-ok/p/9230498.html
今日推荐