codecademy练习记录--Learn Python(70%)
- ##############################################################################
# codecademy python 5.5
# Define a function factorial that takes an integer x as input.
# Calculate and return the factorial of that number.
# def digit_sum(x):
# mul = 1
# for i in range(1,x+1):
# mul = i*mul
# print(mul)
# digit_sum(6)
##############################################################################
# codecademy python 5.6
# Define a function called is_prime that takes a number x as input.
# For each number n from 2 to x - 1, test if x is evenly divisible by n.
# If it is, return False.
# If none of them are, then return True.
# def is_prime(x):
# for i in range(2,x):
# if x%i ==0:
# return True
# else:
# return False
# is_prime(100)
##############################################################################
# codecademy python 5.7
# Define a function called reverse that takes a string textand returns that string in reverse.
# For example: reverse("abcd") should return "dcba".
# You may not use reversed or [::-1] to help you with this.
# You may get a string containing special characters (for example, !, @, or #).
# def Read(str):
# return str[::-1]
# print(Read('abc'))
##############################################################################
# codecademy python 5.8
# Define a function called anti_vowel that takes one string, text,
# as input and returns the text with all of the vowels removed.
# For example: anti_vowel("Hey You!") should return "Hy Y!".
# Don't count Y as a vowel. Make sure to remove lowercase and uppercase vowels.
# import re
# def anti_vowel(str):
# print(re.sub('[aeiou]','',str))
# anti_vowel('Hey You!')
##############################################################################
# codecademy python 5.9
# Define a function scrabble_score that takes a string word as input
# and returns the equivalent scrabble score for that word.
# Assume your input is only one word containing no spaces or punctuation.
# As mentioned, no need to worry about score multipliers!
# Your function should work even if the letters you get are uppercase, lowercase, or a mix.
# Assume that you're only given non-empty strings.
# 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}
# word = str(input()).lower()
# def scrabble_score(word):
# sum = 0
# for item in word:
# sum = score[item]+sum
# print(sum)
# scrabble_score(word)
##############################################################################
# codecademy python 5.10
# Write a function called censor that takes two strings, text and word, as input.
# It should return the text with the word you chose replaced with asterisks. For example:
# censor("this hack is wack hack", "hack")
# should return:
# "this **** is wack ****"
# Assume your input strings won't contain punctuation or upper case letters.
# The number of asterisks you put should correspond to the number of letters in the censored word.
# text = 'this hack is wack hack'
# word = 'hack'
# def censor(text,word):
# num = 0
# for i in word:
# num +=1
# print(text.replace(word,'*'*num))
# censor(text,word)
##############################################################################
# codecademy python 5.11
# Define a function called count that has two arguments called sequence and item.
# Return the number of times the item occurs in the list.
# For example: count([1, 2, 1, 1], 1) should return 3 (because 1 appears 3 times in the list).
# There is a list method in Python that you can use for this, but you should do it the long way for practice.
# Your function should return an integer.
# The item you input may be an integer, string, float, or even another list!
# Be careful not to use list as a variable name in your code—it's a reserved word in Python!
# def count(sequence,item):
# sum = 0
# for i in sequence:
# if i == item:
# sum+=1
# return sum
##############################################################################
# codecademy python 5.12
# Define a function called purify that takes in a list of numbers,
# removes all odd numbers in the list, and returns the result.
# For example, purify([1,2,3]) should return [2].
# Do not directly modify the list you are given as input;
# instead, return a new list with only the even numbers.
# def purify(x):
# li = []
# for i in x:
# if i %2 ==0:
# li.append(i)
# return li
# print(purify([1,2,3,4]))
##############################################################################
# codecademy python 5.13
# Define a function called product that takes a list of integers as input and
# returns the product of all of the elements in the list.
# For example: product([4, 5, 5]) should return 100 (because 4 * 5 * 5 is 100).
# Don't worry about the list being empty.
# Your function should return an integer.
# def product(x):
# mul = 1
# for i in x:
# mul = i*mul
# return mul
# print(product([12,4,3]))
##############################################################################
# codecademy python 5.14
# Write a function remove_duplicates that takes in a list and removes elements of the list that are the same.
# For example: remove_duplicates([1, 1, 2, 2]) should return [1, 2].
# Don't remove every occurrence, since you need to keep a single occurrence of a number.
# The order in which you present your output does not matter.
# So returning [1, 2, 3] is the same as returning [3, 1, 2].
# Do not modify the list you take as input! Instead, return a new list.
# def remove_duplicates(li):
# li1 = []
# for i in li:
# if i not in li1:
# li1.append(i)
# return li1
# print(remove_duplicates([1,1,2,2,3,3,4]))
##############################################################################
# codecademy python 5.15
# Write a function called median that takes a list as an input
# and returns the median value of the list. For example: median([1, 1, 2]) should return 1.
# The list can be of any size and the numbers are not guaranteed to be in any particular order. Make sure to sort it!
# If the list contains an even number of elements, your function should return the average of the middle two.
# def median(li):
# li = sorted(list(li))
# print(li)
# num = 0
# for i in li:
# num +=1
# if (num-1)%2==0:
# return li[int(num/2)]
# else:
# return ((li[int((num/2-0.5)-1)]+li[int((num/2-0.5)+1)]))/2
# print(median([6,2,4,8,9,1,2,3,3,7]))
codecademy练习记录--Learn Python(70%)的更多相关文章
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 记录:python读取excel文件
由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...
- 学 Python (Learn Python The Hard Way)
学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...
- 工作记录之 [ python请求url ] v s [ java请求url ]
背景: 模拟浏览器访问web,发送https请求url,为了实验需求需要获取ipv4数据包 由于不做后续的内容整理(有内部平台分析),故只要写几行代码请求发送https请求url列表中的url即可 开 ...
- 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes
This tutorial is available as a short ebook. The e-book features extra content from follow-up posts ...
- Python basic (from learn python the hard the way)
1. How to run the python file? python ...py 2. UTF-8 is a character encoding, just like ASCII. 3. ro ...
- 记录一些python内置函数
整理一些内置函数,平时用得比较少,但是时不时遇上,记录一下吧(嘻嘻(●'◡'●)) 1.help() 查看模块or函数的帮助文档 help(pandas) #模块 Help on package pa ...
随机推荐
- Day 10 函数
函数 1.什么是函数? 函数就是具备某一功能的工具,事先将工具准备好就是函数的定义,遇到应用场景拿来就用就是函数的调用 2.为何用函数? 如果不使用函数,写程序会遇到这三个问题 1.程序冗长 2.程序 ...
- 【JavaScript框架封装】公共框架的封装
/* * @Author: 我爱科技论坛 * @Time: 20180706 * @Desc: 实现一个类似于JQuery功能的框架 // 公共框架 // 种子模块:命名空间.对象扩展.数组化.类型的 ...
- [luogu3237 HNOI2014] 米特运输 (树形dp)
传送门 Description 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题. D星上有N个城市,我们将其顺序编号为1到N, ...
- 4.1、Ansible模块
ansible-doc -l 列出所有模块 ansible-doc 模块名 查看模块的help说明 ansible-doc -s module_name:获取指定模块的使用信息 ***文 ...
- 探索Python的多态是怎么实现的
多态是指通过基类的指针或者引用,在运行时动态调用实际绑定对象函数的行为. 对于其他如C++的语言,多态是通过在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来 ...
- ORM对象关系型映射的用法
ORM对象关系型映射的用法 -- Django模型 1.什么是ORM关系型映射 ORM 全拼Object-Relation Mapping. 中文意为 对象-关系映射. 主要实现模型对象到关系数据库数 ...
- C/C++ 图像二进制存储与读取
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792 在深度学习时,制作样本数 ...
- structs中通过LabelValueBean构建下拉列表
Action类中增加列表 List<LabelValueBean> list = new ArrayList<LabelValueBean>(); list.add(new L ...
- rails 修改数据库之后注意修改controller
rails 修改数据库之后注意修改controller 在view中进行修改之后,注意修改controller中的内容: 这样才可以进行参数的传递:
- 用PHP去实现静态化
我们在PHP站点开发过程中为了站点的推广或者SEO的须要,须要对站点进行一定的静态化,这里设计到什么是静态页面,所谓的静态页面.并非页面中没有动画等元素,而是指网页的代码都在页面中,即不须要再去执行P ...