Python基础:让小明的故事讲给你听,包括条件循环语句、字典元祖和列表

Python基础:让小明的故事讲给你听,包括条件循环语句、字典元祖和列表

小明的故事,让你清楚Python基础。包括Python变量类型、运算符、if条件语句、for循环语句、while循环语句、break语句、coninue语句、列表、元祖、字典、日期和时间、函数、文件I/O等基础知识

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
'''
@File  : Basic.py
@Author: MyName
@Date  : 2019/02/01 13:24
@Desc  : Python基础,包括Python变量类型、运算符、if条件语句、for循环语句、while循环语句
        break语句、coninue语句、列表、元祖、字典、日期和时间、函数、文件I/O等基础知识
'''
# Python标识符由字母/数字/下划线组成,不能以数字开头,区分字母大小写
import math
import time
import calendar

localtime = time.asctime(time.localtime(time.time()))
print("当前本地时间为 :", localtime)
cal = calendar.month(2019, 2)
print("2019年02月日历(猪年)")
print(cal)

# Python有5个标准的数据类型:List(列表)/ Tuple(元组)/ Dictionary(字典)/ Numbers(数字)/ String(字符串)
classmate = ['Tom', 'Marry', 'Jack', 'Jan']     #创建列表
num_mate = len(classmate)
num_lucky = tuple( range(100) )                 #创建元组
MyScore = {'语文':98, '数学':100, '英语':95}    #创建字典

name = str(input('Please enter name about person : '))  # name = 小明
flag = True
while flag :
    age = int(input("Please enter a number about age : "))
    if age <= 0 :
        flag = True
        print('年龄数值必须大于0,请重新输入!\n')
    elif age > 12 :
        flag = True
        print('小学生的年龄通常情况下在12岁以下,请重新输入!\n')
    else :
        flag = False
        if age >= 6 :
            print('My name is ',name, ', ', age, 'years old')
            print('我在读小学!\n')
        elif age >= 3 :
            print("My name is %s, %d years old" % (name, age))      #字符串格式化输出打印
            print("我在读幼儿园!\n")
        else :
            print("""
            我在读早教班!\n
            """)

print('I\'m smart, 我知道全班同学的名字,竖着叫:')
for name in classmate :
    print('Hello,', name)
print('\n')

print('I\'m smart, 我知道全班同学的名字,横着喊:')
for name in classmate :
    print(name, end=" ")
print('\n')

print('我会心算1到10的乘法: ')
product = 1
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] :
    product = i * product
print('那么1 * 2 * 3 * … * 10 = ', product, '\n')

print('我也会心算1到n的加法: ')
Sum = 0
n = int(input("如果n等于: "))
for i in range(n+1) :
    Sum = Sum + i
print('那么1 + 2 + 3 + … + %d + %d = %d \n' % (n-1, n, Sum))

print('或者心算n之内所有奇数相加: ')
Sum = 0
n_o = int(input("如果n等于: "))
n = n_o
if (n % 2 == 1) :
    while n > 0 :
        Sum += n
        n -= 2
    print('那么1 + 3 + … + %d = %d \n' % (n_o,Sum))
else :
    n = n -1
    while n > 0 :
        Sum += n
        n -= 2
    print('那么1 + 3 + … + %d = %d \n' % (n_o - 1, Sum))

print('我还会写出三位数n以内的数字: ')
n = int(input("如果n等于: "))
i = 1
while i <= n :
    if i > 20 :     #当i = 21时,条件满足,执行break语句
        break       #break语句会结束当前循环,提前退出循环
    print(i, end=" ")
    i = i + 1
print('End')

print('既然这样,那我就写出20以内的奇数: ')
i = 0
while i < 20 :
    i = i + 1
    if i % 2 != 1 :    #如果n是偶数,执行continue语句
        continue       #continue语句会直接继续下一轮循环,后续的print语句不会执行
    print(i)
print('End \n')

print('那你数学成绩期末考了多少分?')
print('数学考了',MyScore['数学'], '分,厉害吧!\n')

print('那你体育成绩期末考了多少分?')
print('体育成绩还没录入系统,给老师说下录入系统里。\n')
MyScore['体育'] = 99
print('好了,这下全部成绩都能在系统中查询到:', MyScore)
SumScore = sum(MyScore.values())
print('总成绩为:', SumScore)
max_Score = max( zip(MyScore.values(),MyScore.keys()) )
print('最高成绩及其科目:',max_Score)
min_Score = min(MyScore.items(), key=lambda x: x[1])
print('最低成绩及其科目:',min_Score)
def dict_Avg( Dict ) :
    Len = len( Dict )
    Sum = sum( Dict.values() )
    Avg = Sum / Len
    return Avg
# 将自定义函数保存为Dict_avg.py
# from Dict_avg import dict_Avg
print('平均分为:', dict_Avg( MyScore ))

运行结果如下
Python基础:让小明的故事讲给你听,包括条件循环语句、字典元祖和列表

  • 致谢
    若对大家有用,感谢点赞或评论;若有不足或补充之处,也感谢大家评论进行指正,后期我将对本文进行补充完善。相信这是互相进步的开始