概述
正好在复习期中考试,顺带就把课上做的笔记,还有老师写的一些比较tricky问题的代码po上来供大家参考,这样一来二去,自己也算是复习了一遍ლ(╹◡╹ლ) and 希望我周六能考一个好成绩( •̀ .̫ •́ )✧
问题1: Find the sum of all the multiples of 3 or 5 below 1000.
计算1000以下能被3或5整除的数字的和。
#代码:
tot_sum = 0
for i in range(1,1000):
if (i%3 == 0 or i%5 == 0):
tot_sum += i
print(tot_sum)
问题2: Write a function that accepts a sub-total from a restaurant bill, with an optional tip value defaulting to 18% and optional tax value defaulting to 10%, returning the final bill. Test the function with the default values and with 20% tip and 10.5% tax。
自动计算营业额的公式
def total_bill(sub_total,tip=0.18,tax=0.10):
return sub_total*(1+tip+tax)
问题3:Consider a stock with a price today of $p. For n consecutive days, it follows a path with two components: a drift of $c/day and a uniformly-distributed random variable of +/- $v/day. Write a function to calculate the series of prices and plot them.
import numpy as np
import matplotlib.pyplot as plt
series_values = list()
def series_calc(stock_price,no_days,drift,v):
days_X = list()
days_X.append(0)
series_values.append(stock_price)
for ii in range (0,no_days):
days_X.append(ii+1)
randomV=np.random.uniform(low=-v,high=v,size=None)
stock_price += (drift + randomV)
series_values.append(stock_price)
plt.plot(days_X,series_values)
plt.show()
series_calc(100,1000,0.1,10.0)
问题4 : Write a program to generate a dictionary that contains (n,n*n) as key-value pairs from keys ranging from 0 to m.
dict1={}
m=10
for n in range(0,m):
dict1[n]=n*n
print(dict1)
问题5:Write a program that asks a user for a long string containing multiple words. Print back the original string, but with the words in backwards order.
把一串文字用倒序方式呈现出来
sentence = "hello there"
words = sentence.split()
sentence_rev = " ".join(reversed(words))
print(sentence_rev)
问题6:Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
对斐波那契数列求和,只求偶数的和。
tot_sum = 0
def fib(n):
ex4 = list()
a, b = 1, 1
while b < n:
ex4.append(b)
a, b = b, a+b
return ex4
ex4_fib = fib(4000000)
print(ex4_fib)
for i in ex4_fib:
if i%2 == 0:
tot_sum += i
print(tot_sum)
最后
以上就是怕黑滑板为你收集整理的python期中考试知识点_python 中的一些小练习 【3】 ლ(╹◡╹ლ)的全部内容,希望文章能够帮你解决python期中考试知识点_python 中的一些小练习 【3】 ლ(╹◡╹ლ)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复