[python入门笔记--代码加详细注释]Programming with Mosh
原创 还有五个代码
最后发布于2019-12-03 22:43:02
阅读数 28 收藏
发布于2019-12-03 22:43:02
展开
- emoji converter(知识点:字典,映射)
-
-
words=message.
split(
' ')
-
--执行--
-
-
[
'I',
'love',
'python',
'so',
'much']
-
-
-
[
'I l',
've y',
'u s',
' much']
-
-
words = message.split(
' ')
-
-
-
-
-
-
-
output += emojis.get(word,word)+
" "
-
--执行--(不是很理解为什么:)前必须加空格才能输出正确结果?)
-
-
-
-
-
-
-
-
- 函数(将上述例子写成函数,注意return的位置)
-
def emoji_converter(message):
-
words = message.split(
' ')
-
-
-
-
-
-
-
output += emojis.get(word,word)+
" "
-
-
-
-
print(emoji_converter(message))
class **(class关键字,**名字的首字母需大写)
关于类和对象的博主原话:with this class we defined a new type,with this new type we can create new objects,so an object is a instance of a class,a class simply defines the blue print or the template for creating objects.
类创建一个新对象,并将其返回,这样我们就可以存储该对象在变量中,及point1 = Point()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
构造函数(A construction is a function that gets called at the time of creating an object)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
print(f
"Hi,I am {self.name}")
-
-
-
john = Person(
"John Smith")
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
右键→New→File→**.py
-
-
-
-
-
from utils
import find_max
-
numbers=[
2,
3,
42,
2,
1]
-
-
-
-
-
-
-
-
- 右键→New→Directory→**(包的名字)→右键→New→__init__.py
- 右键→New→Python package(则会自动添加__init__.py)
应用:2000人参加转发抽奖,随机抽取其中3个欧皇
Python视频来源:https://www.youtube.com/watch?v=_uQrJ0TkZlc