16
2018
09

python暑期专业实训-Day02代码参考

00_year.py

-------------------------------------


#1.输入一个年份

#2.判断是否为闰年

    #2.1. 条件1

    #2.2  条件2


#and 表示并且  Fasle and True

#or 表示或者  Fasle or True

#!= 表示不等于

def year(a):

    if a%4==0 and a%100 != 0:

        print("{}是闰年".format(a))

    elif a%400 == 0:

        print("{}是闰年".format(a))

    else:

        print("{}不是闰年".format(a))


def year1(a):

    if a%4==0 and a%100 != 0:

        print("{}是闰年".format(a))

    if a%400 == 0:

        print("{}是闰年".format(a))



year1(1956)

-------------------------------------

01_student_system.py

-------------------------------------

#1.学生信息保存在字典里面

#2.所有的学生信息放在列表中


#while True

    #3.打印提示

    #4.用户输入

    #5.拿到用户输入的结果

    #6.根据结果选择要做的事情,即选择要调用的函数

        #函数1.展示全部学生信息

        #函数2.搜索一个学生

        #函数3.增加一个学生

        #函数4.修改一个学生

        #函数5.删除一个学生

    #7.用户退出,break


student_list = [{"name":"xiaohong","age":18,"stu_num":10000}]


def print_info():

    print("*"*20)  #3.打印提示

    print("欢迎来到学生信息管理系统")

    print("1.展示全部学生")

    print("2.搜索一个学生")

    print("3.增加一个学生")

    print("4.修改一个学生")

    print("5.删除一个学生")

    print("6.退出信息系统")

    print("*"*20)

    user_input = input(">>>>请选择序号:") #4.用户输入

    return user_input


def show_all_stu(): #展示所有的学生

    for stu in student_list:

        print(stu)


def search_stu(): #搜索学生

    user_input_name = input(">>>>请输入学生的名字:")

    stu_exist = False

    for stu in student_list:

        if stu["name"] == user_input_name:

            stu_exist = True  #如果学生存在,就让stu_exist变为True

            print(stu)

    if stu_exist == False:#  if not stu_exist  #if stu_exist !=True

        print(">>>>您要搜索的学生不存在")


def add_stu():

    stu_name = input("请输入要添加的学生姓名:")

    stu_age = input("请输入要添加的学生年龄:")

    stu_num = input("请输入要添加的学生学号:")

    new_stu = {"name":stu_name,"age":stu_age,"stu_num":stu_num}

    student_list.append(new_stu)

    print("学生:{}信息添加成功".format(stu_name))


def modify_stu_info():

    stu_name = input("请输入要修改的学生姓名:")

    stu_exist = False

    for stu in student_list:

        if stu["name"] == stu_name:

            stu_exist = True

            stu_age = input("请输入修改后的年龄:")

            stu_num = input("亲输入修改后的学号:")

            stu["age"] = stu_age

            stu["stu_num"] = stu_num

            print("学生:{}信息更新成功".format(stu_name))

    if not stu_exist:

        print(">>>>您要修改的学生不存在")


def delete_stu_info():

    stu_name = input("请输入要删除的学生姓名:")

    stu_exist = False

    for stu in student_list:

        if stu["name"] == stu_name:

            stu_exist = True

            student_list.remove(stu)

            print("学生:{}信息删除成功".format(stu_name))

    if not stu_exist:

        print(">>>>您要删除的学生不存在")


def main():

    while True:

        user_input = print_info()  #5.拿到用户输入的结果

        if user_input  in ["1","2","3","4","5","6"]:

            # print(user_input)

            if user_input == "1":  #展示所有的学生信息

                show_all_stu()

            elif user_input == "2": #搜索一个学生

                search_stu()

            elif user_input == "3": #增加一个学生

                add_stu()

            elif user_input == "4": #修改一个学生

                modify_stu_info()

            elif user_input == "5": #删除一个学生

                delete_stu_info()

            elif user_input == "6": #退出

                print(">>>>再见")

                break

        else:

            print("不好意思,你输入错误,请重新输入")


if __name__ == "__main__":

    main()

-------------------------------------

02_if.py

-------------------------------------

money = input("请输入你口袋里的钱:")


money = int(money)


if money>25:  #money>25的时候执行里面的内容

    print("今晚吃鸡")


if 10<money <=25: #money大于10小于等于25的时候执行内部的语句

    print("吃鱼")

    print("1")


print("2")  #注意语法,什么叫做在叫做在if内部,什么不在


if 0<money<=21:  #money大于0小于等于21的时执行里面的语句

    print("吃蔬菜")


if money == 0:   #money等于0的时候执行其中的语句

    print("不吃")

-------------------------------------

02_open_file.py

-------------------------------------

f = open("function1.py","r")

content = f.read()

f.close()


print(content)

-------------------------------------

02_write_file.py

-------------------------------------

f = open("a.txt","a")

f.write("hello\nworld")

f.close()

-------------------------------------

03_if_else.py

-------------------------------------

money = 20


if money>18:

    print("吃饭")


else:   #eles不需要条件,表示其他的条件都不满足的时候就执行else其中的语句

    print("吃不起")

-------------------------------------

03_write_dict.py

-------------------------------------

import json

a = {"xiaohong":20,"xiaogang":21,"xiaoming":18}

str_a = json.dumps(a)

f = open("dict.txt","w")

f.write(str_a)

f.close()

-------------------------------------

04_if_elif_elif_else.py

-------------------------------------

money = 20


if money>30:

    print(1)


elif 20<=money<=30:

    print(2)


elif  10<money<=20:   #可以有多个elif

    print(3)


else:  #不需要条件,表示上面都不满足的时候执行他

    print(4)

-------------------------------------

04_read_dict.py

-------------------------------------

import json


f = open("dict.txt","r")

content = f.read()

f.close()

content = json.loads(content)

print(content)

print(type(content))

-------------------------------------

05_for_in.py

-------------------------------------

a = [1,2,3,4,5,6,7,8,9,10]

for i in a:  #循环,i挨个获取a里面的内容,获取一个之后执行其中的语句,执行完了,a取下一个值

    print("现在执行第{}次循环".format(i))   #pep8规范


print("*"*10)

-------------------------------------

05_try_json.py

-------------------------------------

import json

temp_str = '''{"message": "success", "data": {"pc_feed_focus": [{"title": "\u6cf0\u56fd\u60bc\u5ff5\u5148\u738b\u666e\u5bc6\u84ec\u901d\u4e16\u4e00\u5468\u5e74 \u6c11\u4f17\u75db\u54ed\u6d41\u6d95\u60b2\u4f24\u4e0d\u5df2", "display_url": "/group/6476284682694770958/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3efc00015ef61bd59793", "has_image": true, "group_id": 6476284682694770958, "media_url": "http://toutiao.com/m5784742177"}, {"title": "\u5976\u7238\u5976\u5988\u548c\u201c\u6eda\u6eda\u201d\u7684\u6e29\u99a8\u65f6\u523b \u8fd9\u79cd\u6574\u5929\u88ab\u62b1\u5927\u817f\u7684\u5de5\u4f5c\u8fd8\u62db\u4eba\u5417", "display_url": "/group/6476661517630521613/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3f0000028b49efb750fd", "has_image": true, "group_id": 6476661517630521613, "media_url": "http://toutiao.com/m52307864703"}, {"title": "\u738b\u529b\u5b8f\u5316\u8eab\u5341\u8db3\u5976\u7238 \u81ea\u66dd\u6bcf\u665a\u5531\u6447\u7bee\u66f2\u54c4\u5973\u513f\u5165\u7761", "display_url": "/group/6476596750801961486/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3f0000029e48818f96b2", "has_image": true, "group_id": 6476596750801961486, "media_url": "http://toutiao.com/m50266454509"}, {"title": "\u82f1\u8d856\u5927\u8c6a\u5f3a\u51fa\u6218\u51b7\u95e8\u8fed\u7206 \u84dd\u519b\u8001\u53f8\u673a\u7ffb\u8f66\u67aa\u624b\u5c06\u7b2c\u56db\u62f1\u624b\u76f8\u8ba9", "display_url": "/group/6476823272394621197/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3e83001d1069922b1865", "has_image": true, "group_id": 6476823272394621197, "media_url": "http://toutiao.com/m1564293928517634"}, {"title": "\u7f8e\u56fd\u201c\u5bc6\u6b47\u6839\u201d\u6838\u6f5c\u8247\u62b5\u8fbe\u97e9\u56fd\u91dc\u5c71", "display_url": "/group/6476295974415253774/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3f000002a53197ce328f", "has_image": true, "group_id": 6476295974415253774, "media_url": "http://toutiao.com/m5784742177"}, {"title": "\u738b\u4e3d\u5764\u201c\u638c\u63b4\u201d\u9ad8\u4ee5\u7fd4 \u803f\u76f4girl\u5f53\u573adiss", "display_url": "/group/6476378421324054798/", "has_video": false, "image_url": "//p1.pstatp.com/large/3eff0002ddd80c819c3d", "has_image": true, "group_id": 6476378421324054798, "media_url": "http://toutiao.com/m50092288407"}, {"title": "Selina\u59b9\u59b9\u4efb\u5bb9\u8431\u8981\u5f53\u5317\u6f02\uff0c\u5979\u7684\u5185\u5fc3\u4f4f\u7740\u4e00\u53ea\u201c\u5c0f\u91ce\u517d\u201d\u4e28\u4e13\u8bbf", "display_url": "/group/6475216013678871054/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3f000000303ddc1dac0f", "has_image": false, "group_id": 6475216013678871054, "media_url": "http://toutiao.com/m50266454509"}, {"title": "\u4e2d\u56fd\u9996\u6b21\u66dd\u5149\u6b7c15\u8230\u8f7d\u673a\uff1a\u4f7f\u7528\u56fd\u4ea7\u7535\u78c1\u5f39\u5c04\u5668\u8d77\u98de\u6210\u529f", "display_url": "/group/6475921837753631246/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3efd0008478ac3f4d442", "has_image": false, "group_id": 6475921837753631246, "media_url": "http://toutiao.com/m4087561186"}, {"title": "\u6700\u5f3a\u7ae5\u989c\u5f20\u5a1c\u62c9\u4e0a\u7ebf \u643a\u624b\u5b59\u6d69\u4fca\u751c\u871c\u6c14\u6c1b\u6ea2\u51fa\u5c4f\u5e55~", "display_url": "/group/6475921668145938702/", "has_video": false, "image_url": "//p9.pstatp.com/origin/3efd00084767b74b0980", "has_image": true, "group_id": 6475921668145938702, "media_url": "http://toutiao.com/m5738017030"}, {"title": "E\u795e\u9a7e\u5230\uff01\u9648\u5955\u8fc5\u65b0\u6b4c\u97f3\u4e50\u4f1a\u5fc3\u60c5\u7206\u597d\uff0c\u624b\u821e\u8db3\u8e48\u641e\u602a\u4e0d\u505c~", "display_url": "/group/6475901594457080077/", "has_video": false, "image_url": "//p3.pstatp.com/origin/3e8200187999129ae0cd", "has_image": true, "group_id": 6475901594457080077, "media_url": "http://toutiao.com/m5738017030"}]}}'''

temp_dict = json.loads(temp_str)

# print(temp_dict)


#希望把temp_dict写入本地

f = open("touitao.txt","w",encoding="utf-8")

f.write(json.dumps(temp_dict,ensure_ascii=False,indent=2))

f.close()

-------------------------------------

06_range.py

-------------------------------------

# my_list = range(100)  #生成一个包含0-99的100个数字的可迭代对象,可以使用for in 循环遍历

my_list = list(range(100))   #编程一个列表


for i in my_list:

    print("当前的i是{}".format(i))

-------------------------------------

07_while.py

-------------------------------------


number_a = 1

while number_a<10:  #while后面接条件,表什么时候里面的内容执行,什么时候不执行

    print("当前a是{}".format(number_a))

    # number_a = number_a+1

    number_a += 1  #当前一行和上一行的相同,意思相同


print("程序结束")

-------------------------------------

08_odd.py

-------------------------------------

a = 1

sum = 0  #和最开始的为0

while a<=100:

    if a%2==0:  #判断a是否为偶数

        sum = sum+a #用和加上每一个偶数

    a = a+1  #让a加上1


print("sum为{}".format(sum))

-------------------------------------

09_sum100.py

-------------------------------------

a = 1

sum = 0

while a<=100:

    if sum>100:

        break  #当sum大于100的时候,后面的循环就不在执行了

    sum = sum+a

    # if sum>100:

    #     break

    a = a+1




print("1-100中前n个数的和刚好大于100的第一个数是{}".format(sum))



for i in range(100):

    if i>10:

        break  #i大于10的时候,停止循环

    print(i)


for i in rage(10):  #当前prin(i)是不会有任何的现象的,第一次就break了

    break

    print(i)

-------------------------------------

10_continue.py

-------------------------------------

for i in range(10):

    if i == 5:

        continue  #跳出了i=5的这次的循环,继续选择i=6的结果

    print(i)

-------------------------------------

11_even_sum.py

-------------------------------------

sum = 0

for i in range(1,101):

    if i%2 == 0: #当前的条件成立表示i是偶数

        continue

    print(i)

    sum = sum + i


print("1-100内的所有的奇数的和市{}".format(sum))

-------------------------------------

12_try_def.py

-------------------------------------

def print_helloworld():  #定义一个函数,需要有def关键字,函数名,()和冒号,换行后下面的内容是函数体

    print("hello,world")

    print("are you ok")


# print_helloworld()

# print_helloworld()

for i in range(100):

    print_helloworld()

-------------------------------------

13_try_def_2.py

-------------------------------------

def print_my_word(a): #带一个参数的函数

    print("this is {}".format(a))


print_my_word("ARE YOU OK")

print_my_word(["1","2"])

-------------------------------------

14_get_volume.py

-------------------------------------

def get_volume(a):

    # print("正方体边长为{}的体积为:".format(a),a*a*a)

    print("正方体边长为{}的体积为:{}".format(a,a*a*a))



def get_volume2(a,b,c):  #长方体的体积 #带多个参数的函数

    print("长方体边长分别为{},{},{}的体积为:{}".format(a,b,c,a*b*c))


c = get_volume(10)  #c为调用这个函数的结果,但是当前函数并没有任何的结果,所以c为None

print(c)

get_volume2(10,20,30)

-------------------------------------

15_get_volume2.py

-------------------------------------

def get_volume(a):

    # print("正方体边长为{}的体积为:".format(a),a*a*a)

    v1 = a*a*a

    print("正方体边长为{}的体积为:{}".format(a,v1))

    return v1  #有返回值的函数



def get_volume2(a,b,c):  #长方体的体积

    v2 = a*b*c

    v3 = v2+100

    print("长方体边长分别为{},{},{}的体积为:{}".format(a,b,c,v2))

    return v2,v3  #遇到return,return后面的程序都不会在执行  #有多个返回值的函数

    # return v3

    # print("*"*100)


v1 = get_volume(100)

print(v1)

v2,v3 = get_volume2(10,20,30)

print(v2,v3)


if v1>v2:

    print("正方体的体积大")

elif v1<v2:

    print("长方体的体积大")

else:

    print("两个体积相等")

-------------------------------------

16_temp.py

-------------------------------------

def func():

    for i in range(3):

        return i


def func2():

    d = func()  #函数的嵌套调用

    print(d)


# d = func()

# print(d)

func2()

-------------------------------------

17_gobal_v.py

-------------------------------------

a = 100


def test1():

    a = 200 #定义了一个局部变量a=200,没有修改全局变量a

    print("局部变量a的值是",a)


def test2():  #修改全局变量a =300

    global a  #声明a是一个全局变量

    print("修改之前a是",a)

    a = 300

    print("修改之前a是",a)


def test3():

    print("这是test3函数,a是",a)


test1()

test2()

test3()


#只有在函数中才有全局变量局部变量一说,这里的j,在下一个循环中就是之前的j的值

for i in range(100):

    j = i

    # print(i)


for i in range(3):

    print("this is j",j)

    print(i)

-------------------------------------

a.txt

-------------------------------------

hello

worldhello

world

-------------------------------------

dict.txt

-------------------------------------

{"xiaohong": 20, "xiaogang": 21, "xiaoming": 18}

-------------------------------------

function1.py

-------------------------------------


def my_func():

    print("hello,world!")


if __name__ == "__main__":

    print("this is function1...")

-------------------------------------

temp1.py

-------------------------------------

# import function1

from function1 import my_func


# function1.my_func()

my_func()

-------------------------------------

touitao.txt

-------------------------------------

{

  "message": "success",

  "data": {

    "pc_feed_focus": [

      {

        "title": "泰国悼念先王普密蓬逝世一周年 民众痛哭流涕悲伤不已",

        "display_url": "/group/6476284682694770958/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3efc00015ef61bd59793",

        "has_image": true,

        "group_id": 6476284682694770958,

        "media_url": "http://toutiao.com/m5784742177"

      },

      {

        "title": "奶爸奶妈和“滚滚”的温馨时刻 这种整天被抱大腿的工作还招人吗",

        "display_url": "/group/6476661517630521613/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3f0000028b49efb750fd",

        "has_image": true,

        "group_id": 6476661517630521613,

        "media_url": "http://toutiao.com/m52307864703"

      },

      {

        "title": "王力宏化身十足奶爸 自曝每晚唱摇篮曲哄女儿入睡",

        "display_url": "/group/6476596750801961486/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3f0000029e48818f96b2",

        "has_image": true,

        "group_id": 6476596750801961486,

        "media_url": "http://toutiao.com/m50266454509"

      },

      {

        "title": "英超6大豪强出战冷门迭爆 蓝军老司机翻车枪手将第四拱手相让",

        "display_url": "/group/6476823272394621197/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3e83001d1069922b1865",

        "has_image": true,

        "group_id": 6476823272394621197,

        "media_url": "http://toutiao.com/m1564293928517634"

      },

      {

        "title": "美国“密歇根”核潜艇抵达韩国釜山",

        "display_url": "/group/6476295974415253774/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3f000002a53197ce328f",

        "has_image": true,

        "group_id": 6476295974415253774,

        "media_url": "http://toutiao.com/m5784742177"

      },

      {

        "title": "王丽坤“掌掴”高以翔 耿直girl当场diss",

        "display_url": "/group/6476378421324054798/",

        "has_video": false,

        "image_url": "//p1.pstatp.com/large/3eff0002ddd80c819c3d",

        "has_image": true,

        "group_id": 6476378421324054798,

        "media_url": "http://toutiao.com/m50092288407"

      },

      {

        "title": "Selina妹妹任容萱要当北漂,她的内心住着一只“小野兽”丨专访",

        "display_url": "/group/6475216013678871054/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3f000000303ddc1dac0f",

        "has_image": false,

        "group_id": 6475216013678871054,

        "media_url": "http://toutiao.com/m50266454509"

      },

      {

        "title": "中国首次曝光歼15舰载机:使用国产电磁弹射器起飞成功",

        "display_url": "/group/6475921837753631246/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3efd0008478ac3f4d442",

        "has_image": false,

        "group_id": 6475921837753631246,

        "media_url": "http://toutiao.com/m4087561186"

      },

      {

        "title": "最强童颜张娜拉上线 携手孙浩俊甜蜜气氛溢出屏幕~",

        "display_url": "/group/6475921668145938702/",

        "has_video": false,

        "image_url": "//p9.pstatp.com/origin/3efd00084767b74b0980",

        "has_image": true,

        "group_id": 6475921668145938702,

        "media_url": "http://toutiao.com/m5738017030"

      },

      {

        "title": "E神驾到!陈奕迅新歌音乐会心情爆好,手舞足蹈搞怪不停~",

        "display_url": "/group/6475901594457080077/",

        "has_video": false,

        "image_url": "//p3.pstatp.com/origin/3e8200187999129ae0cd",

        "has_image": true,

        "group_id": 6475901594457080077,

        "media_url": "http://toutiao.com/m5738017030"

      }

    ]

  }

}

-------------------------------------

请同学们参考练习,务必亲自敲代码,脚踏实地的编程!

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。