python之阶乘的小例子】的更多相关文章

现在自己写阶乘是这个样子的 def f(x): return x * f(x-1) if x >1 else 1 后来无意中看到耗子的一篇<Python程序员的进化>的文章, 感脚这个代码可以改成这个样子 f = lambda x: x*f(x-1) if x > 1 else 1 顿感哇哈哈我绝对写过第一种 看到这段代码,偶有种不懂觉厉的感脚,虽然看起来很难读的样子,仔细看看又试验了一下,不错不错,是很酷的样子,标记一下 def fact(x, acc=1): if x: ret…
1.任务:简单测试局域网中的网络是否连接,ip范围:192.168.2.101到192.168.2.200. python 实现代码: import subprocess cmd="cmd.exe" begin=101 end=200 while begin<end: p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) p.st…
temp = input("请输入1到100之间的数字:") num = int(temp) if 1 <= num <= 100:                         # 这一点给赞 省得用 and 或 &了 print('你妹好漂亮^_^')                    # tab键,还有":" else: print('你大爷好丑T_T')                     # tab键   还有":&…
In [57]: name = ('Tome','Rick','Stephon') In [58]: age = (45,23,55) In [59]: for a,n in zip (name,age): ....: print a,n ....: Tome 45Rick 23Stephon 55 In [60]:…
class Song(): def __init__(self,lyrics): self.lyrics=lyrics def sing_a_song(self): for line in self.lyrics: print(line) happy=Song(["Happy birthday to you", "Happy birthday to you"]) happy.sing_a_song() class Song(object): def __init__…
[Spark][Hive][Python][SQL]Spark 读取Hive表的小例子$ cat customers.txt 1 Ali us 2 Bsb ca 3 Carls mx $ hive hive> > CREATE TABLE IF NOT EXISTS customers( > cust_id string, > name string, > country string > ) > ROW FORMAT DELIMITED FIELDS TERMI…
[Python]Python 使用 for 循环的小例子: In [7]: for i in range(5): ...: print "xxxx" ...: print "yyyy" ...: xxxxyyyyxxxxyyyyxxxxyyyyxxxxyyyyxxxxyyyy…
[python]python 遍历一个list 的小例子: mlist=["aaa","bbb","ccc"]for ss in enumerate(mlist): print ss 验证一下运行结果: In [34]: mlist=["aaa","bbb","ccc"] In [35]: for ss in enumerate(mlist): ....: print ss ....:…
Python,while循环小例子--猜拳游戏(三局二胜) import random all_choice = ['石头', '剪刀', '布'] prompt = '''(0)石头 (1)剪刀 (2)布 请选择(0\1\2)''' # 人的计分板 pwin = 0 # 计算机的计分板 cwin = 0 # 人和计算机都没有赢够两次则继续 while pwin < 2 and cwin < 2: # 人的选择在前,计算机随机选择在后,组成小列表,把所有人赢的情况再放到大列表中 win_lis…
告别枯燥,60秒学会一个Python小例子.奔着此出发点,我在过去1个月,将平时经常使用的代码段换为小例子,分享出来后受到大家的喜欢. 一.基本操作 1 链式比较 i = 3print(1 < i < 3)  # Falseprint(1 < i <= 3)  # True 2 不用else和if实现计算器 from operator import * def calculator(a, b, k):    return {        '+': add,        '-':…