第一节
1 介绍了Python的一种内置方法type(x),用来判断x的类型,比如type(5)是int,type("asf")是string等等
2 练习:写一个函数为is_int,x作为参数,判断x是否为整数,但是要注意的是如果x的小数点全部为0那么x也认为是整数比如7.0
def is_int(x):
if type(x) == int or x == int(x):
return True
else:
return False
第二节
1 练习:写一个函数为digit_sum(),n作为参数,求n所有位数的和
def digit_sum(n):
sum = 0
while n > 0:
sum += n%10
n /= 10
return sum
第三节
1 练习:写一个函数factorial,参数为x,求x的所有位数的乘积
def factorial(x):
sum = 1
while x > 0:
sum *= x
x -= 1
return sum
第四节
1 写一个函数is_prime,参数是x,判断x是否为质数
def is_prime(x):
if x <= 1:
return False
for i in range(2,x):
if x%i == 0:
return False
return True
第五节
1 首先介绍了我们可以使用[::-1]来反转一个列表,一个字符串,比如my_list = [1,2,3,4],那么my_list[::-1]就是为[4,3,2,1]
2 练习:写一个函数为reverse,参数是一个字符串text,把text逆向的串输出,比如"abcd"应该输出为"dcba"
def reverse(text):
length = len(text)
str = ""
while length > 0:
length -= 1
str += text[length]
return str
第六节
1 练习:写一个函数为anti_vowel,参数是字符串text,要求把所有是元音的字母全部去掉然后返回
def anti_vowel(text):
str = ""
length = len(text)
for i in range(length):
x = text.lower()
if x != "a" and x != "i" and x != "o" and x != "u" and x != "e":
str += text
return str
第七节
1 练习:写一个函数为scrabble_score参数为字符串word,根据提供的字典,求出word的得分
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score(word):
sum = 0
length = len(word)
for i in range(length):
x = word.lower()
sum += score[x]
return sum
第八节
1 练习:写一个函数为censor,参数为两个字符串text和word,要求把text里面出现的word换成对应长度的"*"串
比如text为"hack is hack"word为"hack",那么输出为"**** is ****"
def censor(text , word):
len_text = len(text)
len_word = len(word)
str = ""
i = 0
while i < len_text:
isOk = True
pos = 0
for j in range(i,i+len_word):
if j >= len_text or text[j] != word[pos]:
isOk = False
break
else:
pos += 1
if isOk:
for j in range(len_word):
str += "*"
i += len_word
else:
str += text
i += 1
return str
第九节
1 练习:写一个函数为purify,参数是一个列表,要求把列表中的所有奇数删除,然后返回这个列表
def purify(my_list):
while True:
isOk = False;
for num in my_list:
if num%2:
my_list.remove(num)
isOk = True
break
if isOk == False:
break
return my_list
第十节
1 练习:写一个函数remove_duplicates,参数是一个列表,要求把所有的相同的元素去掉,返回一个新的列表
def remove_duplicates(my_list):
my_list.sort()
res = []
length = my_list
i = 0
for num in my_list:
if (num in res):
continue
else:
res.append(num)
return res
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com