设为首页 加入收藏

TOP

46 Simple Python Exercises 1-5题(一)
2017-10-10 08:27:28 】 浏览:1763
Tags:Simple Python Exercises 1-5

会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码。

This is version 0.45 of a collection of simple Python exercises constructed (but in many cases only found and collected) by Torbjörn Lager (torbjorn.lager@ling.gu.se). Most of them involve characters, words and phrases, rather than numbers, and are therefore suitable for students interested in language rather than math.

  1. Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)
#Define a function max() that takes two numbers as
# arguments and returns the largest of them.
# Use the if-then-else construct available in Python.
# (It is true that Python has the max() function built in,
# but writing it yourself is nevertheless a good exercise
def max(number1, number2):
    if number1>number2:
        return number1
    else:
        return number2
print(max(1,2))
print(max(3,2))
print(max(2,2))

 

  1. Define a function max_of_three() that takes three numbers as arguments and returns the largest of them
#Define a function max_of_three() that takes three numbers
# as arguments and returns the largest of them
def max(number1, number2):
    if number1>number2:
        return number1
    else:
        return number2
def max_of_three(number1,number2,number3):
    if max(number1,number2)>number3:
        return max(number1,number2)
    else:
        return number3
print(max_of_three(1,2,3))
print(max_of_three(2,2,3))
print(max_of_three(1,3,3))
print(max_of_three(3,2,3))
print(max_of_three(2,3,2))
print(max_of_three(1,3,2))
print(max_of_three(1,3,3))
print(max_of_three(3,2,1))
print(max_of_three(3,3,1))
print(max_of_three(3,2,2))
print(max_of_three(2,2,2))

 

  1. Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)
#Define a function that computes the length of a given list or string.
# (It is true that Python has the len() function built in,
# but writing it yourself is nevertheless a good exercise.)
def len_a(words):
    i=0
    for word in words:
        i=i+1
    return i
print(len_a([1,2,3,'num','a']))
print(len_a('asdfghjkl'))
print(len_a('hjfjsd,fdf'))

 

  1. Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
#Write a function that takes a character (i.e. a string of length 1)
# and returns True if it is a vowel, False otherwise.
def vowel_t(string):
    vowel = ['a', 'e', 'i', 'o', 'u']
    if string in vowel:
        return True
    else:
        return False
print(vowel_t('a'))
print(vowel_t('b'))

 

  1. Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
#  Write a function translate() that will translate a text into "rövarspråket"
# (Swedish for "robber's language"). That is,
# double every consonant and place an occurrence of "o" in between.
# For example, translate("this is fun") should return the string "tothoh
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python读取与写入csv格式文件 下一篇python 机器学习 决策树

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目