基础python代码

 

看完这段代码可以让我瞬间想起python的语法

 

# Open a file, read each line, and print it out
for line in open('file.txt'):
    print line

# Create a file and write to it
file = open("text.txt","w")
file.write("test line\n")
file.close()

# Create a small dictionary of names and ages and manipulate
family = {'Megan':13, 'Elise':8, 'Marc':6}
# result in 8
print family['Elise']

# Remove the key/value pair
del family['Elise']

# Create a list and a function that doubles its input. Map the 
# function to each of the elements of the list (creating a new list as a result)
arr = [1, 2, 3, 4, 5]
def double(x):return x*x
print map(double,arr)

# Create a class, inherit by another, and then instantiate it and invoke its methods
class Simple:
    def __init__(self, name):
        self.name = name;
    def hello(self):
        print self.name+"says hi."

class Simple2(Simple):
    def goodbye(self):
        print self.name+"says goodbye."

me = Simple2("Tim")
me.hello()
me.goodbye()
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值