• python核心编程 第二章练习


      1 #encoding=utf-8
      2 from __future__ import division  #除法总是会返回真实的商
      3 from __future__ import unicode_literals #运行时中文编码正确
      4 #练习1 
      5 string = 'hello,world'
      6 intA = 5
      7 print 'string is %s,intA is %d' % (string,intA)
      8 
      9 #练习2
     10 print 1 + 2 * 4
     11 
     12 #练习3
     13 a = 9
     14 b = 3
     15 print a+b,a-b,a*b, a/b,a%b,a**b
     16 
     17 #练习4
     18 print 'please emter string:'
     19 c = raw_input()
     20 print  c
     21 intB = int(raw_input())
     22 print 'intB is %d' % intB
     23 
     24 #练习5
     25 i = 0
     26 while i < 11:
     27     print i
     28     i += 1  #python 没有++
     29 
     30 for j in range(11):
     31     print j
     32 
     33 #练习6   有问题   a = 输入的数据,后续程序并不是
     34 #a = 3
     35 a = raw_input()
     36 print a
     37 
     38 if a > 0:
     39     print 'a > 0'
     40 elif a == 0 :
     41     print 'a == 0'
     42 else :
     43     print 'a < 0'
     44 
     45 #问题7  
     46 string = raw_input()
     47 strlen = len(string)
     48 i = 0
     49 while i < strlen:
     50     print string[i]
     51     i += 1
     52 #for 接受可迭代对象(例如序列或迭代器)作为其参数,
     53 #每次迭代其中一个元素。与传统计数器循环不太一样
     54 for j in string:
     55     print j
     56 
     57 #问题8
     58 aList = [1, 2, 3, 4, 5]    #列表[]
     59 sum = 0
     60 i = 0
     61 while i < len(aList):
     62     sum += aList[i]
     63     i += 1
     64 print 'sum while is : %d' % sum
     65 for j in aList:
     66     sum += j
     67 print 'sum for is : %d' % sum
     68 
     69 aTuple = (1, 2, 3, 4, 5)    #元组()
     70 sum = 0
     71 i = 0
     72 while i < len(aTuple):
     73     sum += aTuple[i]
     74     i += 1
     75 print 'sum while is : %d' % sum
     76 for j in aTuple:
     77     sum += j
     78 print 'sum for is : %d' % sum
     79 
     80 sum = 0
     81 j = 0
     82 while j < 5:
     83     i = int(raw_input())
     84     sum += i
     85     j += 1
     86 print sum
     87 
     88 #问题9
     89 aList = [1, 2, 3, 4]    #列表[]
     90 sum = 0
     91 i = 0
     92 while i < len(aList):
     93     sum += aList[i]
     94     i += 1
     95 float(sum)
     96 float(len(aList))
     97 
     98 print sum
     99 print float(10 / 4)
    100 print 'average is %f' % (sum/(len(aList)))
    101 
    102 问题10
    103 python 逻辑and or not
    104 a = int(raw_input())
    105 while a > 100 or  a < 1:
    106     print 'input error,again:'
    107     a = int(raw_input())
    108 print 'sucess %d' % a
    109 
    110 #问题11
    111 print '--------------菜单项------------'
    112 print '(1)取五个数的和'
    113 print '(2) 取五个数的差'
    114 print '(X)退出'
    115 select = raw_input()
    116 
    117 while select != 'x':
    118     select = int(select)
    119     if select == 1:
    120         print '求和'
    121         sum = 0
    122         j = 0
    123         while j < 5:
    124             i = int(raw_input())
    125             sum += i
    126             j += 1
    127         print sum
    128     elif select == 2 :
    129         print '求均值'
    130         sum = 0
    131         j = 0
    132         while j < 5:
    133             i = int(raw_input())
    134             sum += i
    135             j += 1
    136         print float(sum/5)
    137     else :
    138         print '选择错误'
    139     print '--------------再次进入菜单项------------'
    140     print '(1)取五个数的和'
    141     print '(2) 取五个数的差'
    142     print '(X)退出'
    143     select = raw_input()
    144 
    145 #问题15
    146 # print 'please enter three numbers'
    147 # a = int(raw_input())
    148 # b = int(raw_input())
    149 # c = int(raw_input())
    150 # if a > b:
    151 #     temp = a
    152 #     a = b
    153 #     b = temp
    154 # if b > c:
    155 #     temp = b
    156 #     b = c
    157 #     c = temp
    158 # if a > b:
    159 #     temp = a
    160 #     a = b
    161 #     b = temp
    162 # print '%d < %d < %d' %(a,b,c)
    163 
    164 #问题16
    165 filename = raw_input('enter file name:')
    166 fobj = open(filename,'r')
    167 for x in fobj:
    168     print x;
    169 fobj.close()
  • 相关阅读:
    Git标签
    Git管理修改和撤销修改
    Git删除文件
    Git解决冲突
    Git的stash功能
    Git管理分支
    Git的多人协作模式
    Git相关指令
    LC7 整数翻转 + LC9 回文数
    LC1 两数之和
  • 原文地址:https://www.cnblogs.com/lovely7/p/5718148.html
Copyright © 2020-2023  润新知