Python练习题 Day3
practice 1
编写Python程序,判断给定的数字是偶数还是奇数,并向用户输出判断结果。
def even_or_odd(numb):
if numb % 2 == 0:
return '该数字为偶数'
else:
return '该数字为奇数'
print(even_or_odd(-9))
print(even_or_odd(444))
practice 2
编写一个Python程序来计算列表中出现多少个4。
def count4(lst):
count = 0
for i in lst:
if i == 4:
count += 1
return count
print(count4([1,2,4,4,4,4,5,6,7,4,4,5]))
practice 3
编写Python程序获取给定字符串的前2位字符并输出n次。如果长度小于2,则返回n次整个字符串。
方法一:
def bac_str_fs(str1, n):
if len(str1) > 2:
return n*str1[0:2]
else:
return n*str1
print(bac_str_fs('abcacb', 3))
print(bac_str_fs('ab', 3))
print(bac_str_fs('a', 3))
方法二:
def substring_copy(str, n):
flen = 2
if flen > len(str):
flen = len(str)
substr = str[:flen]
result = ""
for i in range(n):
result = result + substr
return result
print(substring_copy('abcdef', 2))
print(substring_copy('p', 3))
practice 4
编写一个Python程序来测试一个通过的字母是否是元音
def is_vowel(char):
all_vowel = 'aeiou'
if char in all_vowel:
print('该字母为元音字母')
else:
print('该字母不是元音字母')
is_vowel('a')
is_vowel('c')
practice 5
编写一个Python程序来检查指定的值是否包含在一组值中
测试数据 :
3 -> [1, 5, 8, 3] : True
-1 -> [1, 5, 8, 3] : False
def is_group_member(group_data, n):
for value in group_data:
if n == value:
return True
return False # 这里与for对齐,若value不在group_data里,则直接退出循环,返回false
print(is_group_member([1, 5, 8, 3], 3))
print(is_group_member([1, 5, 8, 3], -1))
practice 6
编写一个Python程序,由给定的整数列表得出一个与列表数字对应的图,组成图片的字符可以自定义
如:[2,5,3,7]
@@
@@@@@
@@@
@@@@@@@
def histogram(items):
for n in items:
output = ''
times = n
while times > 0:
output += '*'
times = times - 1
print(output)
histogram([3, 4, 5, 6])
practice 7
编写Python程序将列表中的所有元素连接成一个字符串并返回它
def lst_str(lst1):
str1 = ''
n = 0
while n < len(lst1):
for i in lst1:
str1 += i
n += 1
return str1
print(lst_str(['s', '2', '3', 'b']))
print(lst_str(['s', 'b', '6', 'c']))
practice 8
编写一个Python程序以相同的顺序打印给定数字列表中的所有偶数,如果序列中有任何数字出现在237之后,则停止打印。 :
数字列表例子
numbers = [
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958,743, 527
]
numbers = [
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958,743, 527
]
for x in numbers:
if x == 237:
print(x)
break
elif x % 2 == 0:
print(x)
practice 9
编写一个Python程序输出一个包含color_list_1中所有不在color_list_2中的颜色的集合
测试数据:
color_list_1 = set([“White”, “Black”, “Red”])
color_list_2 = set([“Red”, “Green”])
期望输出 :
{‘Black’, ‘White’}
方法一:
def not_in(color_list_1, color_list_2):
color_list_3 = []
for str1 in color_list_1:
if str1 not in color_list_2:
color_list_3.append(str1)
return color_list_3
print(not_in(["White", "Black", "Red"], ["Red", "Green"]))
方法二:
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
print(color_list_1.difference(color_list_2))
practice 10
编写一个Python程序,接受三角形的底和高,并计算面积。
def triangle_area(a, h):
area = 0.5 * a * h
return area
print(triangle_area(3, 5))
print(triangle_area(6, 7))
坚持