【Python】 Python编程基础练习100题学习记录第四期(31~40)

1.此为GitHub项目的学习记录,记录着我的思考,代码基本都有注释。
2.可以作为Python初学者巩固基础的绝佳练习,原题有些不妥的地方我也做了一些修正。
3.建议大家进行Python编程时使用英语。
4.6~17题为level1难度,18-22题为level3难度,其余都为level1难度。
项目名称:
100+ Python challenging programming exercises for Python 3

#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function that can accept an integer number as input and print the "It is an even number"
if the number is even, otherwise print "It is an odd number".
"""

'''Use % operator to check if a number is even or odd.'''


def checkNumber(a):
    a = int(a)
    if a % 2 == 0:
        print('It is a even number.')
    elif a % 2 != 0:
        print('It is an odd number.')
    else:
        pass


checkNumber(7)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function which can print a dictionary
where the keys are numbers between 1 and 3 (both included) and the values are square of keys.
"""

'''
Hints:
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
'''


def dicprac(a,b):
    """打印一个字典,键为1个整数,值为键所对应整数的平方"""
    a = int(a)
    b = int(b)
    values = {}         # 创建空字典
    for i in range(a, b):
        values[i] = i ** 2  # 创建键值对
    print(values)


dicprac(1, 21)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-

"""
Define a function which can generate a dictionary
where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
The function should just print the values only.
"""

'''Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get power of a number. 
Use range() for loops. Use keys() to iterate keys in the dictionary. 
Also we can use item() to get key/value pairs.'''


def dicprac2(a, b):
    """只打印一个字典的值,键为1个整数,值为键所对应整数的平方"""
    a = int(a)
    b = int(b)
    values = {}  # 创建空字典
    for i in range(a, b):
        values[i] = i ** 2  # 创建键值对
#        print(values[i])   # 方法1:索引,只打印值

#    for (k, v) in values.items():      # 方法2:用items(),只打印值
#        print(v)

    for k in values.keys():         # 用keys(),只打印键
        print(k)


dicprac2(1, 21)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Define a function which can generate and print a list
where the values are square of numbers between 1 and 20 (both included).
"""

'''Use ** operator to get power of a number. 
Use range() for loops. 
Use list.append() to add values into a list.'''


def listprac(a, b):
    """打印一个列表,列表里的值为所给范围数字的平方"""
    a = int(a)
    b = int(b)
    values = []         # 创建一个空列表
    for i in range(a, b):    # for循环添加值
        values.append(i ** 2)   # 值为平方
    print(values)


listprac(1, 21)     # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included).
Then the function needs to print the first 5 elements in the list.
"""

'''
Hints:
Use ** operator to get power of a number.
Use range() for loops. 
Use list.append() to add values into a list. 
Use [n1:n2] to slice a list
'''


def listprac(a, b):
    """打印一个列表,列表里的值为所给范围数字的平方的前五个"""
    a = int(a)
    b = int(b)
    values = []  # 创建一个空列表
    for i in range(a, b):  # for循环添加值
        values.append(i ** 2)  # 值为平方
    print(values[0:5])         # 切片,打印前五个数


listprac(1, 21)  # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included).
Then the function needs to print the last 5 elements in the list.
"""

'''
Hints:
Use ** operator to get power of a number. 
Use range() for loops. 
Use list.append() to add values into a list. 
Use [n1:n2] to slice a list
'''


def listprac(a, b):
    """打印一个列表,列表里的值为所给范围数字的平方的最后五个"""
    a = int(a)
    b = int(b)
    values = []  # 创建一个空列表
    for i in range(a, b):  # for循环添加值
        values.append(i ** 2)  # 值为平方
    print(values[-5:])  # 切片,打印最后五个数


listprac(1, 21)  # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included).
Then the function needs to print all values except the first 5 elements in the list.
"""

'''
Hints:
Use ** operator to get power of a number. 
Use range() for loops. 
Use list.append() to add values into a list. 
Use [n1:n2] to slice a list
'''


def listprac(a, b):
    """打印一个列表,列表里的值为除了所给范围数字的平方的前五个的其他所有值"""
    a = int(a)
    b = int(b)
    values = []  # 创建一个空列表
    for i in range(a, b):  # for循环添加值
        values.append(i ** 2)  # 值为平方
    print(values[5:])  # 切片,打印除前五个数之外的其他所有数


listprac(1, 21)  # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Define a function which can generate and print a tuple
where the value are square of numbers between 1 and 20 (both included).
"""

'''
Hints:
Use ** operator to get power of a number. 
Use range() for loops. 
Use list.append() to add values into a list. 
Use tuple() to get a tuple from a list.
'''


def tupleprac(a, b):
    """打印一个元组,元组里的值为除了所给范围数字的平方"""
    a = int(a)
    b = int(b)
    values = []  # 创建一个空列表
    for i in range(a, b):  # for循环添加值
        values.append(i ** 2)  # 值为平方
    print(tuple(values))  # 将列表转为元组并打印


tupleprac(1, 21)  # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
With a given tuple (1,2,3,4,5,6,7,8,9,10),
write a program to print the first half values in one line and the last half values in one line.
"""

'''
Hints:
Use [n1:n2] notation to get a slice from a tuple.
'''


def tupleprac(a, b):
    """打印一个元组,元组里的值为除了所给范围数字的平方"""
    a = int(a)
    b = int(b)
    values = []  # 创建一个空列表
    for i in range(a, b):  # for循环添加值
        values.append(i ** 2)  # 值为平方
#    print(tuple(values))  # 将列表转为元组并打印
    print(tuple(values[0:10]))  # 打印元组的前一半
    print(tuple(values[11:20])) # 打印元组的后一半


tupleprac(1, 21)  # 调用函数
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-


"""
Write a program to generate and print another tuple
whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).
"""

'''
Hints:
Use "for" to iterate the tuple Use tuple() to generate a tuple from a list.
'''
list_practice = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]     # 先将这些数字放进列表里
print(tuple(list_practice))                         # 转换为元组
even_nums = []                                       # 创建一个空列表
for i in list_practice:                             # for循环找到列表里的偶数
    if i % 2 == 0:
        even_nums.append(i)                          # 将找到的偶数加进列表里
print(tuple(even_nums))                              # 转换为元组打印出来

# 源代码
"""tp = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
li = list()
for i in tp:
    if tp[i] % 2 == 0:
        li.append(tp[i])

tp2 = tuple(li)
print(tp2)"""

第四期先到这儿,共十期。第五期出来后我会把链接加进来,方便大家学习。

“自主学习,快乐学习”

再附上Python常用标准库供大家学习使用:
Python一些常用标准库解释文章集合索引(方便翻看)

“学海无涯苦作舟”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值