python3学习案例
2020-12-13 02:57
标签:sum 转换 字符串类型 create 分析 out tor inpu print python3学习案例 标签:sum 转换 字符串类型 create 分析 out tor inpu print 原文地址:https://blog.51cto.com/13399294/2411674"""
请打印出
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 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 ‘, 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:
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 "
上一篇:俄罗斯方块:win32api开发
下一篇:某C++面试题