[python入门笔记--代码加详细注释]Programming with Mosh
发布于2019-12-03 22:43:02
- emoji converter(知识点:字典,映射)
- message = input(">")#用户输入消息(先右边再写左边是个好习惯)
- words=message.split(' ')#用空格将字符串分割(split里面的单引号或者双引号没多大影响)
- print(words)
--执行--
- >I love python so much
- ['I', 'love', 'python', 'so', 'much']
- #将空格改为o
- >I love you so much
- ['I l', 've y', 'u s', ' much']
- message = input(">")#用户输入消息
- words = message.split(' ')#用空格将字符串分割
- emojis = {
- ":)":"😁",
- ":(":"😔"
- }#字典
- output = ""#定义一个输出变量
- for word in words:
- output += emojis.get(word,word)+" "#get函数:字典中的映射,而不是字典中的原样输出,为什么最后加个空格?
- print(output)
--执行--(不是很理解为什么:)前必须加空格才能输出正确结果?)
- >Good morning :)
- Good morning 😁
- >I am sad :(
- I am sad 😔
-
- 错误:
- >I am sad:(
- I am sad:(
- 函数(将上述例子写成函数,注意return的位置)
- def emoji_converter(message):
- words = message.split(' ')
- emojis = {
- ":)":"😁",
- ":(":"😔"
- }
- output = ""
- for word in words:
- output += emojis.get(word,word)+" "
- return output
-
- message = input(">")
- 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()
- class Point:
- def move(self):
- print("move")
-
- def draw(self):
- print("draw")
-
-
- point1 = Point()#像调用一个函数一样的调用它
- point1.x = 10
- point1.y = 20
- print(point1.x)
- point1.draw()
-
- point2 = Point#有了一个对象就会有另外一个对象!
- point2.x=1
- print(point2.x)
构造函数(A construction is a function that gets called at the time of creating an object)
- class Point:
- def __init__(self,x,y):#在self之后添加两个额外的参数
- self.x = x
- self.y = y#init用来初始化对象
- def move(self):
- print("move")
-
- def draw(self):
- print("draw")
-
-
- point = Point(10,20)
- print(point.x)
- class Person:
- def __init__(self,name):
- self.name = name
- def talk(self):
- print(f"Hi,I am {self.name}")#动态的添加人名
-
-
- john = Person("John Smith")
- john.talk()
- bob = Person("bob wei")
- bob.talk()
- 继承(inheritance)
- class Mammal:
- def walk(self):
- print("walk")
-
-
- class Dog(Mammal):
- pass
-
-
- class Cat(Mammal):
- def be_annoying(self):
- print("annoying")
-
-
- Dog1 = Dog()
- Dog1.walk()#继承父类
- Cat1 = Cat()
- Cat1.be_annoying()#自己define
- 模块(module)
右键→New→File→**.py
- (app.py)
- # import utils
- # numbers=[2,3,42,2,1]
- # print(utils.find_max(numbers))#两种表示都可
- from utils import find_max
- numbers=[2,3,42,2,1]
- print(find_max(numbers))
- (utils.py)
- def find_max(numbers):
- max=numbers[0]
- for number in numbers:
- if number > max:
- max=number
- return max
- 包(package)
- 右键→New→Directory→**(包的名字)→右键→New→__init__.py
- 右键→New→Python package(则会自动添加__init__.py)
- Python内置模块
应用:2000人参加转发抽奖,随机抽取其中3个欧皇
Python视频来源:https://www.youtube.com/watch?v=_uQrJ0TkZlc
- emoji converter(知识点:字典,映射)
- message = input(">")#用户输入消息(先右边再写左边是个好习惯)
- words=message.split(' ')#用空格将字符串分割(split里面的单引号或者双引号没多大影响)
- print(words)
来源:CSDN
作者:qq_41314786
链接:https://blog.csdn.net/qq_41314786/article/details/104261752