• 013-python基础-课堂练习


    一、练习一

    1.1 使用while循环输入 1 2 3 4 5 6 8 9 10

    方法一:

    1 for i in range(1,11):
    2     print(i)

    方法二:

    1 i = 1
    2 while i <= 10:
    3     print(i)
    4     i += 1

     1.2 求1-100的所有数的和

    1 i = 1
    2 i_sum = 0
    3 while i <= 100:
    4     i_sum = i + i_sum
    5     i += 1
    6 print(i_sum)

     1.3 输出 1-100 内的所有奇数

    1 i = 1
    2 while i <= 100:
    3     mod = i % 2      # "%"用于求余数
    4     if mod == 1: print(i)
    5     i += 1

    1.4 输出 1-100 内的所有偶数

    1 i = 1
    2 while i <= 100:
    3     mod = i % 2      # "%"用于求余数
    4     if mod == 0: print(i)
    5     i += 1

    1.5 求1-2+3-4+5 ... 99的所有数的和

     方法一:

     1 i = 1
     2 m = 0
     3 while i <= 100:
     4     mod = i % 2
     5     if mod == 0:
     6         z = -i
     7     else:
     8         z = i
     9     m = m + z
    10     i += 1
    11 print(m)

    方法二:

     1 i = 1
     2 m = 0
     3 flag = True
     4 while i <= 100:
     5     if flag is True:
     6         m = m + i
     7         flag = False
     8     else:
     9         m = m - i
    10         flag = True
    11     i += 1
    12 print(m)

    二、练习二

    2.1、元素分类

    有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
    即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

    方法一:

    1 ll = [11,22,33,44,55,66,77,88,99,]
    2 dict = {"k1":[],"k2":[]}
    3 for i in ll:
    4     if i > 66:
    5         dict["k1"].append(i)
    6     else:
    7         dict["k2"].append(i)
    8 print(dict)

    方法二:

     1 ll = [11,22,33,44,55,66,77,88,99,]
     2 a = []
     3 b = []
     4 
     5 for i in ll:
     6     if i >= 66:
     7          a.append(i)
     8     else:
     9          b.append(i)
    10 c = {"key1":a,"key2":b}
    11 print(c)
    2.2、查找
    查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
        li = ["alec", " aric", "Alex", "Tony", "rain"]
        tu = ("alec", " aric", "Alex", "Tony", "rain") 
        dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
     
    1 li = ["alec", " aric", "Alex", "Tony", "rain"]
    2 for i in li:
    3     b = i.strip()
    4     if b.startswith("a") or b.startswith("A") and b.endswith("c"):
    5         print(b)
    1 tu = ("alec", " aric", "Alex", "Tony", "rain")
    2 for i in tu:
    3     b = i.strip()
    4     if b.startswith("a") or b.startswith("A") and b.endswith("c"):
    5         print(b)
    1 dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
    2 for key in dic:
    3     b = dic[key].strip()
    4     if b.startswith("a") or b.startswith("A") and b.endswith("c"):
    5         print(b)
  • 相关阅读:
    在 Laravel 5.1 中使用 Pjax
    在 iOS 中实现方法链调用
    利用 WireShark 深入调试网络请求
    设计一个健壮的后台下载
    设计一个健壮的大型文件下载系统
    iOS开发基础知识:Core Animation(核心动画)
    开发 Swift 和 Objective-C 混编的 Framework
    protobuf3 iOS 接入 protobuf
    iOS之ProtocolBuffer搭建
    iOS10 推送必看(基础篇)
  • 原文地址:https://www.cnblogs.com/chhphjcpy/p/6064729.html
Copyright © 2020-2023  润新知