加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python day2

发布时间:2020-12-20 12:48:49 所属栏目:Python 来源:网络整理
导读:Today is the second day of leaning Python. 1.For Loop ?? for i in range(): print() 2.break and continue ?? break : To jump out of this loop ?? continue : To jump out of the current loop 3.list? [] ?? a = [1,2,3,4,5]???? a[1:4]? Everying fr

Today is the second day of leaning Python.

1.For Loop

?? for i in range():
  print()

2.break and continue

?? break : To jump out of this loop

?? continue : To jump out of the current loop

3.list? []

?? a = [1,2,3,4,5]???? a[1:4]? Everying from the first to the fourth,but not the fourth.

????????????????????????????? a[1:]??? from the first to the last

????????????????????????????? a[1: :2] This 2 represents the step size.(2,4) if it‘s -2? represents in the opposite direction

????????????????????????????? a.append(‘data‘)? a.insert(index,‘data‘)

????????????????????????????? a.remove(‘data‘)? a.pop(index) If no input index,it means that the last item is deleted.

????????????????????????????? a.count(‘data‘) ? ? a.extend(b)? Add b to a ? ? a.index(‘data‘)? Find index???

????????????????????????????? a.sort()?? Sort lists from samll to large??? a.reverse()?? Reverse the contents of the list

4.tuple? ()

?? a=(1,5)????? Tuples are readable and unchangeable.

5.Format output

?? print ("----%s----" % variable)

6.Determine whether the user input is a number?

??? if variable.digit():?? If it‘s number,return True? Else return False

7.dictionary

?? dic={"name":"Mr","age":22,"sex":"man"}??? Key-value pairs

?? Key is Immutable? Changeless . It could be a string,a number or a tuple

?? Value is Varibale . it could be a list,a string,a number

?? The contents of the dictionary are disordered.? Access accordng to Key.

?

??

8.Shopping test? (source code)

    salary = input("请输入您的工资:")
    if salary.isdigit():
      salary = int(salary)
    shopping_car = []
    shop = [[‘iphone6‘,6000],
        [‘mac book‘,80],
        [‘coffee‘,35],
        [‘bicycle‘,1500],
        [‘computer‘,4000,]]
    while True:
      for i,v in enumerate(shop,1):
      print(i,v)
      choose = input("请输入你要购买的物品编号(退出q):")
      if choose.isdigit():
        choose = int(choose)
        if choose>0 and choose<len(shop):
          p_item = shop[choose-1]
          if p_item[1] < salary:
            salary -= p_item[1]
            shopping_car.append(p_item)
          else:
            print("余额不足,还剩%s元钱" % salary)
            print(p_item)
        else:
          print("商品编码不存在")
      elif choose == ‘q‘:
        print("---------购买的商品--------")
        for i in shopping_car:
          print(i)
          print("您还剩%s元钱" % salary)
          break
      else:
      print("金钱不足")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读