python3学习案例

2020-01-03 10:35:04 浏览数 (1)

代码语言:javascript复制
"""
请打印出
1024 * 768 = ***
"""
shu = 1024 * 768
print("1024 * 768 = %d" %shu)

"""
请打印出以下变量的值:
# -*- coding: utf-8 -*-
n = 123
f = 456.789
s1 = 'Hello, world'
s2 = 'Hello, 'Adam''
s3 = r'Hello, "Bart"'
s4 = r'''Hello,
Lisa!'''
"""
n = 123
f = 456789 / 1000
s1 = "'Hello, World'"
s2 = "'Hello, \'Adam\''"
s3 = "r'Hello, "Bart"'"
s4 = 'r'''''Hello,nLisa!''''
print('n=', n, 'nf=', f, 'ns1=', s1, 'ns2=', s2, 'ns3=', s3, 'ns4=', s4)

"""
×××的成绩从去年的72分提升到了今年的85分,请计算×××成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位:
"""
zuo_nain = 72 / 100
jin_nain = 85 / 100
r = (jin_nain - zuo_nain) * 100
print("提升%.1f%%" %r )

"""
请用索引取出下面list的指定元素:
# -*- coding: utf-8 -*-
L = [
    ['Apple', 'Google', 'Microsoft'],
    ['Java', 'Python', 'Ruby', 'PHP'],
    ['Adam', 'Bart', 'Lisa']
]
# 打印Apple:
print(?)
# 打印Python:
print(?)
# 打印Lisa:
print(?)
"""
L = [
    ['Apple', 'Google', 'Microsoft'],
    ['Java', 'Python', 'Ruby', 'PHP'],
    ['Adam', 'Bart', 'Lisa']]
print(L[0][0])
print(L[1][1])
print(L[2][2])

"""
×××身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮×××计算他的BMI指数,并根据BMI指数:
低于18.5:过轻
18.5-25:正常
25-28:过重
28-32:肥胖
高于32:严重肥胖
用if-elif判断并打印结果:
"""
s = input("高》:")
height = float(s)
a = input("重》:")
weight = float(a)

BMI = weight / height ** 2
if BMI < 18.5:
    print("过轻")
elif BMI < 25:
    print("正常")
elif BMI < 28:
    print("过重")
elif BMI < 32:
    print("肥胖")
elif BMI > 32:
    print("严重肥胖")

"""
请利用循环依次对list中的每个名字打印出Hello, xxx!:
L = ['Bart', 'Lisa', 'Adam']
"""
L = ['Bart', 'Lisa', 'Adam']
for i in L:
    print("Hello, %s" % i)

"""
实现将列表:['a','a','b','a','b','c']输出为字典:{'a':3,'b':2,'c':1}
"""
str_list = ['a', 'a', 'b', 'a', 'b', 'c']
st_set = set(['a', 'a', 'b', 'a', 'b', 'c'])
dic = {}
for i in st_set:
    cont = str_list.count(i)
    dic[i] = cont
print(dic)

"""
请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串:
"""
n1 = 233
n2 = 1000
print(hex(n1), hex(n2))

"""
请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2 bx c=0 的两个解。
计算平方根可以调用math.sqrt()函数
"""
import math
def quadratic(a, b, c):
    b2 = b ** 2 - 4*a*c
    if not isinstance(a   b   c, (int, float)):
        raise TypeError('error type')
    if b2 >= 0:
        ma_x1 = math.sqrt(b ** 2 - 4*a*c)
        x1 = (-b - ma_x1) / (2 * a)
        x2 = (-b   ma_x1) / (2 * a)
        print("x1=%.2f ,x2=%.2f" % (x1, x2))
    else:
        print("无解")
quadratic(1, 5, 5)

"""
以下函数允许计算两个数的乘积,请稍加改造,变成可接收一个或多个数并计算乘积:
def product(x, y):
    return x * y
"""
def product(*arges):
    if len(arges) == 0:
        raise TypeError('参数不能为空, 否则没有意义!')
    s = 1
    for i in arges:
        if not isinstance(i, (int, float)):
            raise TypeError('error type')
        s = i * s
    #return s
    print(s)
product(10, 25)

"""
请编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A、B、C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的方法,例如:
"""
def move(n, a, b, c):
    if n < 0:
        product("请输入大于0")
    elif n == 1:
        print(a, '-->', c)
    else:
        move(n - 1, a, c, b)   # 1, A, B,  C
        move(1, a, b, c)       #1,  A, B,  C
        move(n - 1, b, a, c)   #1,  A, B,  C
move(2, 'A', 'B', 'C')

"""
利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
"""
def trim(s):
    if s[:1] == ' ':
        s = s[1:]
        print(s)
    elif s[-1:] == ' ':
        s = s[:-1]
        print(s)
    elif s[:1] == ' ' and s[-1:] == ' ':
        s = s[1:-1]
        print(s)
trim(" abc2" )

"""
请使用迭代查找一个list中最小和最大值,并返回一个tuple:
"""
def suh(L):
    if not isinstance(L, (list, tuple)):
        raise TypeError('param must be a list')
    max_L = min_L = L[0]
    for i in L:
        if max_L < i:
            max_L = i
        elif min_L > i:
            min_L = i
    print(min_L)
    print(max_L)
suh([8, 2, 4, 5])

"""
如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:
>>> L = ['Hello', 'World', 18, 'Apple', None]
>>> [s.lower() for s in L]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
AttributeError: 'int' object has no attribute 'lower'
使用内建的isinstance函数可以判断一个变量是不是字符串:

>>> x = 'abc'
>>> y = 123
>>> isinstance(x, str)
True
>>> isinstance(y, str)
False
请修改列表生成式,通过添加if语句保证列表生成式能正确地执行:

# -*- coding: utf-8 -*-
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = ??
"""
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [s.lower() for s in L1 if isinstance(s, str)]
print(L2)

"""
杨辉三角定义如下:
          1
         / 
        1   1
       /  / 
      1   2   1
     /  /  / 
    1   3   3   1
   /  /  /  / 
  1   4   6   4   1
 /  /  /  /  / 
1   5   10  10  5   1

把每一行看做一个list,试写一个generator,不断输出下一行的list:
# 期待输出:
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# [1, 6, 15, 20, 15, 6, 1]
# [1, 7, 21, 35, 35, 21, 7, 1]
# [1, 8, 28, 56, 70, 56, 28, 8, 1]
# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
"""
def triangles(n):
    L = [1]
    m = 0
    while n > m:
        yield L
        p = [L[a]   L[a   1] for a in range(len(L) - 1)]
        #print(p)
        L = [1]   p   [1]
        m  =1
for i in triangles(11):
    print(i)

"""
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']:
"""
def normalize(name):
    name = name.lower()
    name = name.title()
    return name
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)

"""
Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积:
"""
from functools import reduce
def prod(G):
    def fn(x, y):
        return x * y
    return reduce(fn, G)   # def f(): retuen x*y  reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
print("3 * 5 * 7 * 9 =", prod([3, 5, 7, 9]))

"""
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456:
"""
def str2float(s):
    for i in range(len(s)):
        if s[i] == '.':
            n = i

    def num1(x, y = 0):
        return x * 10   y

    Digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

    def digit(x):
        return Digits[x]

    return reduce(num1, map(digit, s[ : n]))   reduce(num1, map(digit, s[n   1 : ])) / pow(10, (len(s) - n - 1))
print("str2float('123.456') =", str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
    print('测试成功!')
else:
    print('测试失败!')

"""
假设我们用一组tuple表示学生名字和成绩:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
请用sorted()对上述列表分别按名字排序:
"""
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

def by_name(t):
    m = t[0]
    return m

def by_score(t):
    n=-t[1]
    return n

L1 = sorted(L, key=by_score)
L2 = sorted(L, key=by_name)
print(L1)
print(L2)

"""
回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数:
"""
def is_palindrome(n):
    c = 0
    for i in range(len(str(n))):
        if str(n)[i] != str(n)[-i-1]:
            c = c 1
    return c == 0

#测试
output = filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
    print('测试成功!')
else:
    print('测试失败!')

"""
def count():
    fs = []
    for i in range(1, 4):
        def f():
             return i*i
        fs.append(f)
    return fs
f1 = count()[0]
print(f1())
不加括号函数不执行。
利用闭包返回一个计数器函数,每次调用它返回递增整数:
"""
def createCounter():
    f = []
    def counter():
        f.append(counter)
        return len(f)
    return counter
# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
    print('测试通过!')
else:
    print('测试失败!')

"""
请用匿名函数改造下面的代码:
def is_odd(n):
    return n % 2 == 1

L = list(filter(is_odd, range(1, 20)))
print(L)
"""
L = list(filter(lambda n:n % 2 ==1 , range(1, 20)))
print(L)

"""
请设计一个decorator,它可作用于任何函数上,并打印该函数的执行时间:
"""
import time, functools
def metric(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kw):
        start = time.time()
        re = fn(*args, **kw)
        end = time.time()
        print('%s executed in %s ms' % (fn.__name__, end-start))
        return fn(*args, **kw)
    return wrapper

# 测试
@metric
def fast(x, y):
    time.sleep(0.0012)
    return x   y;
@metric
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z;
f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
    print('测试失败!')
elif s != 7986:
    print('测试失败!')

"""
请把下面的Student对象的gender字段对外隐藏起来,用get_gender()和set_gender()代替,并检查参数有效性:
class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender
"""
class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.__gender = gender
    def get_gender(self):
        return self.__gender
    def set_gender(self, gender):
        self.__gender = gender

bart = Student('Bart', 'male')
print(bart.name, bart.get_gender())
if bart.get_gender() != 'male':
    print('测试失败!')
else:
    bart.set_gender('female')
    if bart.get_gender() != 'female':
        print('测试失败!')
    else:
        print('测试成功!')

"""
为了统计学生人数,可以给Student类增加一个类属性,每创建一个实例,该属性自动增加:
"""
class Student(object):
    count = 0
    def __init__(self, name):
        self.name = name
        Student.count =1
# 测试:
if Student.count != 0:
    print('测试失败!')
else:
    bart = Student('Bart')
    if Student.count != 1:
        print('测试失败!')
    else:
        lisa = Student('Bart')
        if Student.count != 2:
            print('测试失败!')
        else:
            print('Students:', Student.count)
            print('测试通过!')

"""
请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution:
"""
class Screen(object):
    @property
    def width(self):
        return self._width

    @width.setter
    def width(self, width_value):
        self._width = width_value

    @property
    def height(self):
        return  self._height
    @height.setter
    def height(self, height_value):
        self._height = height_value

    @property
    def resolution(self):
        return self._width * self._height

# 测试:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
    print('测试通过!')
else:
    print('测试失败!')

""""
运行下面的代码,根据异常信息进行分析,定位出错误源头,并修复:
from functools import reduce

def str2num(s):
    return int(s)

def calc(exp):
    ss = exp.split(' ')
    ns = map(str2num, ss)
    return reduce(lambda acc, x: acc   x, ns)

def main():
    r = calc('100   200   345')
    print('100   200   345 =', r)
    r = calc('99   88   7.6')
    print('99   88   7.6 =', r)

main()
"""
from functools import reduce
def str2num(s):
    s = s.strip()
    if s.replace('.','').isdigit():
        if s.count('.') == 0:
            return int(s)
        else:
            return float(s)
    else:
        raise ValueError('input error')

def calc(exp):
    ss = exp.split(' ')
    print('ss=',ss)
    ns = map(str2num, ss)
    return reduce(lambda acc, x: acc   x, ns)

def main():
    r = calc('100   200   345')
    print('100   200   345 =', r)
    r = calc('99   88   7.6')
    print('99   88   7.6 =', r)

main()

"""
请将本地一个文本文件读为一个str并打印出来:
"""
#写入
fpath_wrig = r'C:UsersAdministratorDesktopLUOZIX.txt'
with open(fpath_wrig, 'w') as f:
    w = f.write("woshiwshuin wozainaer n woyaoshfuu")

#追加
fpath_wria = r'C:UsersAdministratorDesktopLUOZIX.txt'
with open(fpath_wria, 'a') as f:
    w = f.write("n1234455")

#1查看
fpath = r'C:UsersAdministratorDesktopLUOZIX.txt'
with open(fpath, 'r') as f:
    s = f.read()
    print(s)

#2查看
with open(fpath, 'r') as fs:
    ss = fs.readlines()
    for x in ss:
        print(x)

0 人点赞