'''
1、求偶数元素的和[1,2,1,2,3,3,6,5,8]
b1 = 0
a1 = [1,2,1,2,3,3,6,5,8]
for i in a1:
if i%2 == 0:#当i除以2余数为0,偶数
b1 += i
print(b1)
==============================
2、写代码:计算 1 - 2 + 3 - 4 + ... + 99 中除了88以外所有数的总和?
a1 = 0
for i in range(1,100):
if i%2 == 0: # 当i除以2余数为0,偶数
if i == 88:
continue
else:
a1 -= i
else:
a1 += i
print(a1)
==============================
3、求1+2+3+4+……+100的和.
a1 = 0
for i in range(1,101): #[1,2,3,...,100]
a1 += i
print(a1)
==============================
4、斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,特别指出:第0项是0,第1项是第一个1。从第三项开始,每一项都等于前两项之和。
计算索引为10的斐波那契数列对应的值
思路:
nterms = 5
n1 = 0
n2 = 1
n3 = n1 + n2
n4 = n2 +n3
if nterms == 1:
print(n1)
elif nterms == 2:
print(n2)
elif nterms == 3:
n3 = n1 + n2
print(n3)
elif nterms == 4:
n4 = n2 + n3
print(n4)
elif nterms == 5:
n5 = n3 + n4
print(n5)
...
==============================
方法:
weizhi = int(input("请输入斐波那契数列对应的索引位置:>>"))
n1 = 0
n2 = 1
count = 2
if weizhi == 1:
print(n1,",")
elif weizhi == 2:
print(n1,",",n2,",")
else:
print(n1,",",n2,end=" , ")
while count < weizhi:
n3 = n1 + n2
print(n3,end=" , ")
n1 = n2
n2 = n3
count +=1
==============================
5、打印菱形小星星
*
***
*****
*******
*********
***********
***********
*********
*******
*****
***
*
思路:
第一行 空格3 *1 (即*个数为7-3*2)
第二行 空格2 *3 (即*个数为7-2*2)
第三行 空格1 *5 (即*个数为7-1*2)
第四行 空格0 *7
第五行 空格1 *5
第六行 空格2 *3
第七行 空格3 *1
方法:
n = 7
for i in range(-int(n/2),int(n/2)+1):
print(" "*abs(i),"*"*abs(n-abs(i)*2))
==============================
'''