and | 与
or | 或
not | 非
!= | 不等于
== | 等于
>= | 大于等于
< = | 小于等于
True | 真
False | 假
这一节是对ex27的巩固,意在增加对逻辑关系的理解
这一节是为了理解if语句的格式
people = 20
cats = 30
dogs = 15
if people < cats:
print("Too many cats! The world is doomed!")
if people > cats:
print("Not many cats! The world is saved!")
if people > dogs:
print("The world is dry!")
这一节意在理解 else、elif 和 if 之间的联系
people = 30
cars = 40
trucks = 15
if cars > people:
print("We should take the cars.")
elif cars < people:
print("We should not take the cars.")
else:
print("We can't decide.")
通过 if、 else 和 elif 来设计一个小游戏,加深对 if 、else、 elif 的理解
熟悉列表的格式和作用
这里注意,数字在列表不需要加 ‘ ’ ,但文字和字母需要
for i in range(1, 3) # 在这里只循环2次而非三次,即实际上到2就停了
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
#This first kind of for-loop through a list
for number in the_count:
print(f"This is count {number}")
#same as above
for fruit in fruits:
print(f"A fruit of type :{fruit}")
# also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it
for i in change:
print(f"I got {i}")
对while循环格式的理解
i = 0
numbers = []
while i < 6:
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now",numbers)
print(f"At the bottom i is {i}")
知道编程中元素的排序下标为 0、1、2、3、4、5、6、7、8
通过编写一个游戏加深对函数的理解
def gold_room():
print("This room is full of gold. How much do you take?")
choice = input("> ")
if "0" in choice or "1" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
注意,python中以缩进来划分代码块
知道如何调试代码
这里可以用 print()语句进行调试
熟悉一些对列表的基本操作
A.split(‘’):将语句A切片
A.pop() : 弹出列表元素
A.append() : 将A中加入元素
print(‘–‘.join(A)) : 打印时在A中元素之间打印加上–
print(’#’.join(stuff[3:5]) : 打印下标为3和下标为4的元素,并且中间用#隔开
ten_things = "Apples Orange Crows Telephone Light Sugar"
print("Wait there are not 10 things in that list. Let's fix that.")
stuff = ten_things.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Gril", "Boy"]
while len(stuff) != 10:
next_one = more_stuff.pop()
print("Adding:",next_one)
stuff.append(next_one)
print(f"There are {len(stuff)} items now.")
print("There we go:",stuff)
print("There's do something with stuff.")
print(stuff[1])
print(stuff[-1])
print(stuff.pop())
print('_____'.join(stuff))
print('#'.join(stuff[3:5]))
列表中通过数字的下标找到元素
things = [‘a’, ‘b’, ‘c’, ‘d’]
其中print(things[1]) 打印出 b 元素
字典则是类似于显示中的字典
states = {
‘Oregon’: ‘OR’,
‘Florida’: ‘FL’,
‘California’: ‘CA’,
‘New York’: ‘NY’,
‘Michigan’: ‘MI’,
}
其中 print(states[Oregon]) 打印出 OR 元素,即一一对应
模块
定义:
def apple():
print("I am apples!")
我可以通过以下方法进行调用
import stuff
stuff.apple()
不仅可以调用其中函数,还可以调用其中的参量
类
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print(line)
以上代码就定义了一个叫 Song 的类
对象
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
以上的代码声明了一个对象
对象和模块差不多
例:上面可以通过 happy_bday.sing_me_a_song() 来调用函数
定义:
一些概念:
将以上的概念和定义记忆理解
通过编写一些代码加深对对象、类及从属关系的理解
即写一个游戏,加深理解
继承
定义:
隐式继承
在父类里定义了一个函数但没有在子类中定义时的隐式继承
class Parent(object):
def implicit(self):
print("PARENT implicit()")
class Child(Parent):
pass
dad = Parent()
son = Child()
dad.implicit()
son.implicit()
这个歌时候的打印结果应该是
PARENT implicit()
PARENT implicit()
显式覆盖
要使子类中的函数有不同的行为,这种情况下隐式继承是做不到的。这时需要在子类中定义一个与父类中同名的函数即可。
class Parent(object):
def override(self):
print("PARENT override()")
class Child(Parent):
def altered(self):
print("CHILD override()")
dad = Parent()
son = Child()
dad.override()
son.override()
结果是
PARENT override()
PARENT override()
在运行前或运行后替换
即想要修改函数,又想要父类中的函数,首先要像上面一样进行覆盖,之后调用super()函数进行调用父类的函数
class Parent(object):
def altered(self):
print("PARENT altered()")
class Child(Parent):
def altered(self):
print("Child")
super(Child, self).altered()
print("Child 2 ")
dad = Parent()
son = Child()
dad.altered()
son.altered()
结果是
PARENT altered()
Child
PARENT altered()
Child 2
从结果中我们可以看到,函数super()使得回到了Child的父类中调用了altered()函数进行打印
意在巩固代码能力