[python入门笔记--代码加详细注释]Programming with Mosh

六月ゝ 毕业季﹏ 提交于 2020-02-11 17:11:31

[python入门笔记--代码加详细注释]Programming with Mosh

  • emoji converter(知识点:字典,映射)
  1. message = input(">")#用户输入消息(先右边再写左边是个好习惯)
  2. words=message.split(' ')#用空格将字符串分割(split里面的单引号或者双引号没多大影响)
  3. print(words)

--执行--

  1. >I love python so much
  2. ['I', 'love', 'python', 'so', 'much']
  3. #将空格改为o
  4. >I love you so much
  5. ['I l', 've y', 'u s', ' much']

 

  1. message = input(">")#用户输入消息
  2. words = message.split(' ')#用空格将字符串分割
  3. emojis = {
  4. ":)":"😁",
  5. ":(":"😔"
  6. }#字典
  7. output = ""#定义一个输出变量
  8. for word in words:
  9. output += emojis.get(word,word)+" "#get函数:字典中的映射,而不是字典中的原样输出,为什么最后加个空格?
  10. print(output)

--执行--不是很理解为什么:)前必须加空格才能输出正确结果?)

  1. >Good morning :)
  2. Good morning 😁
  3. >I am sad :(
  4. I am sad 😔
  5. 错误:
  6. >I am sad:(
  7. I am sad:(
  •  函数(将上述例子写成函数,注意return的位置)
  1. def emoji_converter(message):
  2. words = message.split(' ')
  3. emojis = {
  4. ":)":"😁",
  5. ":(":"😔"
  6. }
  7. output = ""
  8. for word in words:
  9. output += emojis.get(word,word)+" "
  10. return output
  11. message = input(">")
  12. 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()

  1. class Point:
  2. def move(self):
  3. print("move")
  4. def draw(self):
  5. print("draw")
  6. point1 = Point()#像调用一个函数一样的调用它
  7. point1.x = 10
  8. point1.y = 20
  9. print(point1.x)
  10. point1.draw()
  11. point2 = Point#有了一个对象就会有另外一个对象!
  12. point2.x=1
  13. print(point2.x)

构造函数(A construction is a function that gets called at the time of creating an object)

  1. class Point:
  2. def __init__(self,x,y):#在self之后添加两个额外的参数
  3. self.x = x
  4. self.y = y#init用来初始化对象
  5. def move(self):
  6. print("move")
  7. def draw(self):
  8. print("draw")
  9. point = Point(10,20)
  10. print(point.x)
  1. class Person:
  2. def __init__(self,name):
  3. self.name = name
  4. def talk(self):
  5. print(f"Hi,I am {self.name}")#动态的添加人名
  6. john = Person("John Smith")
  7. john.talk()
  8. bob = Person("bob wei")
  9. bob.talk()
  • 继承(inheritance)
  1. class Mammal:
  2. def walk(self):
  3. print("walk")
  4. class Dog(Mammal):
  5. pass
  6. class Cat(Mammal):
  7. def be_annoying(self):
  8. print("annoying")
  9. Dog1 = Dog()
  10. Dog1.walk()#继承父类
  11. Cat1 = Cat()
  12. Cat1.be_annoying()#自己define

  • 模块(module)

右键→New→File→**.py

  1. (app.py)
  2. # import utils
  3. # numbers=[2,3,42,2,1]
  4. # print(utils.find_max(numbers))#两种表示都可
  5. from utils import find_max
  6. numbers=[2,3,42,2,1]
  7. print(find_max(numbers))

 

  1. (utils.py)
  2. def find_max(numbers):
  3. max=numbers[0]
  4. for number in numbers:
  5. if number > max:
  6. max=number
  7. return max
  • 包(package)
  1. 右键→New→Directory→**(包的名字)→右键→New→__init__.py
  2. 右键→New→Python package(则会自动添加__init__.py)
  •  Python内置模块

应用:2000人参加转发抽奖,随机抽取其中3个欧皇 

Python视频来源:https://www.youtube.com/watch?v=_uQrJ0TkZlc

  • emoji converter(知识点:字典,映射)
  1. message = input(">")#用户输入消息(先右边再写左边是个好习惯)
  2. words=message.split(' ')#用空格将字符串分割(split里面的单引号或者双引号没多大影响)
  3. print(words)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!