EX40

1 # this goes in mystuff.py
2 def apple():
3     print "I AM APPLES!"
4 
5 # this is just a variable
6 tangerine = "Living reflection of a dream."
 # 以上资料保存为mystuff.py
1 import mystuff
2 
3 mystuff.apple()
4 print mystuff.tangerine
# 以上资料保存为ex401.py(名字可以随意选取,这个文件的主要目的在于用import mystuff来进入第一个文件内
1 mystuff = {'apple': "I AM APPLES!"}
2 print mystuff['apple']
# 单独一个文件,跟第1、2无关联,保存文件ex40.py

上图为mystuff文件内容

 

 

1、[] 中括号,代表List列表数据类型; {} 大括号,代表dict字典数据类型,用冒号:分开键&值,用逗号,分开组

2、

1 mystuff['apple'] # get apple from dict
2 mystuff.apple() # get apple from the module
3 mystuff.tangerine # same thing, it's just a variable

第一个是直接从字典内抽取apple键;第二是从模块中抽取apple键,第三同理,只不过它是个变量

 

 1 class Song(object):
 2     
 3     def __init__(self, lyrics):
 4         self.lyrics = lyrics
 5     def sing_me_a_song(self):
 6         for line in self.lyrics:
 7             print line
 8             
 9 happy_baby = Song(["Happy birthday to you",
10                     "I don't want to get sued",
11                     "So I'll stop right there"])
12                     
13 bulls_on_parade = Song(["They rally around the family",
14                         "With pockets full of shells"])
15                         
16 happy_baby.sing_me_a_song()
17 
18 bulls_on_parade.sing_me_a_song()

1、__init__:这个下行线是由_两个组成

此章节完全没有学懂,先过,后面重新再看

 

转载于:https://www.cnblogs.com/LevenLau/p/6407831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值