第二章习题
2016-05-14 19:25
267 查看
2-1
>>> a1=232 >>> a1 232 >>> print a1 232 >>> type(a1) <type 'int'> >>> a2='wef23' >>> a2 'wef23' >>> print a2 wef23 >>> type(a2) <type 'str'> >>> a3=[32,'322'] >>> a3 [32, '322'] >>> print a3 [32, '322'] >>> type(a3) <type 'list'> >>> a4=(23,'23') >>> a4 (23, '23') >>> print a4 (23, '23') >>> type(a4) <type 'tuple'>
2-2
/article/7675454.htmlhttps://www.zhihu.com/question/20790240
两个参考链接
(a)
用python可执行程序去运行,调用/usr/bin/env 下的python来执行。为了防止用户没有将python装在默认的/usr/bin路径里,当执行时,首先到env中查找python的安装路径,再调用对应路径下的解释器程序来完成。 #!/usr/bin/python写死了python路径。数字计算
(b)
#!/usr/bin/env python 1+2*4
没有输出
(c)
没有以变量的形式给输出或打印(d)
有何不同?9
(e)
#!/usr/bin/env python print 1+2*4
2-3
>>> a=2;b=5 >>> print a+b #jiafa 7 >>> print a-b #jianfa -3 >>> print a*b #chengfa 10 >>> print a/b #chufa 0 >>> print a%b #quyu 2 >>> print a//b 0 >>> print a**b #chengfang 32
2-4
(a)
>>> raw_input("please enter a char:") please enter a char:fa 'fa'
(b)
>>> a=raw_input("please enter a number:") please enter a number:54 >>> a '54' >>> b=int(raw_input("please enter a number:")) please enter a number:54 >>> b 54 >>> type(a) <type 'str'> >>> type(b) <type 'int'>
2-5
(a)
while>>> #!/usr/bin/env python >>> i=0 >>> while i<11: print i i+=1 0 1 2 3 4 5 6 7 8 9 10
(b)
for>>> for i in range(11): print i 0 1 2 3 4 5 6 7 8 9 10
2-6
#!/usr/bin/env python i = raw_input("please input a number:") i=int(i) if i>0: print "%d is more than 0" % i elif i<0: print "%d is less than 0" % i else: print "%d is equal 0" % i
2-7
while#!/usr/bin/env python char1 = raw_input("please input a char:") num =len(char1) i=0 while i<num: print char1[i] i+=1 please input a char:juanjuan j u a n j u a n
for
#!/usr/bin/env python char1 = raw_input("please input a char:") num =len(char1) for i in range(num): print char1[i] please input a char:juanjaun j u a n j a u n
2-8
预先输入#!/usr/bin/env python a=[23,32,43,54,73] i=len(a) j=0;sum1=0 while j<i: sum1+=a[j] j+=1 print sum1
接受用户输入
#!/usr/bin/env python a=[1,1,1,1,1] i=len(a) j=0;sum1=0 while j<i: a[j]=raw_input("please input a number:") sum1+=int(a[j]) j+=1 print sum1
for
预先输入
#!/usr/bin/env python a=[234,34,2,322,24] i=len(a) j=0;sum1=0 for j in range(i): #a[j]=raw_input("please input a number:") sum1+=int(a[j]) # j+=1 print sum1
用户输入
#!/usr/bin/env python a=[1,2,3,4,5] i=len(a) j=0;sum1=0 for j in range(i): a[j]=raw_input("please input a number:") sum1+=int(a[j]) print sum1
注明:元组不支持修改
2-9
#!/usr/bin/env python a=[23,32,43,443,5] i=len(a) j=0;sum1=0 for j in range(i): #a[j]=raw_input("please input a number:") sum1+=int(a[j]) print float(sum1)/float(i)
2-10
#!/usr/bin/env python #coding:utf-8 while True: a=int(raw_input("please input a number:")) if a>=1 and a<=100: print "%d is good" % (a) break else : print "mistake,retry!" continue
2-11
#!/usr/bin/env python #coding:utf-8 def Menu(): print "================================" print "[1]get sum from 5 numbers" print "[2]get average from 5 numbers" print "[X]Exit" def Sum(): list=[1,2,3,4,5] sum=0 for i in xrange(len(list)): list[i]=raw_input("input a %d Number:" %(i+1)) sum+=int(list[i]) i+=1 return sum def Aver(): sum=Sum() return sum/5 while True: Menu() choice = raw_input("please choice one:") if choice == '1': SUM=Sum() print "sum is :%d" % SUM print continue elif choice == '2': Average = Aver() print "Average is:%f" % Average continue elif choice == 'X': break else: print "wrong choose,please again!" continue
2-12
(a)>>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'i', 'k']
显示每个值
>>> __builtins__ <module '__builtin__' (built-in)> >>> __doc__ >>> __name__ '__main__' >>> __package__ >>> a [23, 32, 43, 54, 73] >>> b 54 >>> i 2 >>> k '5'
(b)
(c)
(d)
2-13
(a)>>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'i', 'k']>>> import sys
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'i', 'k', 'sys']
>>> dir(sys)
['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_traceback', 'exc_type', 'exc_value', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
(b)
>>> sys.api_version 1013 >>> sys.platform 'win32'
(c)
>>> sys.exit()
2-14
>>> print -2*4+3**2 1 >>> print (-2*4)+(3**2) 1
没有错
2-15
#!/usr/bin/env python #coding:utf-8 i=0 j=3 a=0;b=0;c=0 a=raw_input("a=") b=raw_input("b=") c=raw_input("c=") if a >b: if b>c: print "%s>%s>%s" %(a,b,c) elif c>b: print "%s>%s>%s" %(a,c,b) elif b>a: if a>c: print "%s>%s>%s" %(b,a,c) elif c>a: print "%s>%s>^%s" % (b,c,a) elif c>b: if b>a: print "%s>%s>%s" %(c,b,a) elif a>b: print "%s>%s>%s" %( c , b ,a) elif a == b: if a > c: print "%s=%s>%s" %(a,b,c) elif a<c: print "%s>%s=%s" %(c,a,b) elif a == c: if a>b: print "%s=%s>%s" %(a,c,b) elif b>a: print "%s>%s=%s" %(b,a,c) elif b == c: if b>a: print "%s=%s>%s" % (b,c,a) elif a>b: print "%s>%s=%s" %(a,b,c) else : print "%s=%s=%s" %(a,b,c)
2-16
#!/usr/bin/env python filename=raw_input("Enter file name:") fobj=open(filename,'r') for eachLine in fobj: print eachLine, fobj.close()
相关文章推荐
- Eclipse ADT->Android Studio
- 7--栈的定义及实现
- 搞懂$.each和$(selector).each
- 堆栈堆栈
- C++primer知识点(一)
- ora11g listener.ora
- Android telnet RPi 2B
- BAT 批处理获取系统时间
- Unity mesh texture开启 read 会增加内存畅想
- iOS开发入门
- openstack mitaka 完整安装详细文档(亲测,花了3天时间)
- 《快学Scala》第四章习题解答
- 2016"百度之星" - 资格赛(Astar Round1) 1004
- hdu1058 && hdu3199
- iOS简单对象的文件存取
- QT-悬浮对话框
- 搭建高可用mongodb集群(四)—— 分片
- 优先队列priority queue
- 菜单的属性与封装
- Python:输出系统所有的PID