name ='Make'
age =18
isHave =Falseprint(age,name,isHave)
003 输入和输出
full_name =input('What is your name? \n')print('Hi '+ full_name)
004 输入和输出类型
birth =int(input('Birth year: '))
age =2020- birth
print(type(age))print(age)
005 单引号双引号三引号的用法 字符串 多种下标取法
coures ="Python's for Beginners"print(coures)
coures ='Python for "Beginners"'print(coures)
coures ='''
Hi oldbai
see you
'''print(coures)
coures ='Python for Beginners'print(coures[0])print(coures[-1])print(coures[0:3])print(coures[0:])print(coures[:6])print(coures[:])
coures ='Jennifer'print(coures[1:-1])
006 一种简便的打印
first ='John'
last ='Smith'
msg = f'{first} [{last}] is a coder'print(msg)
007 字符串的多种方法
course ='Python for Beginners'print(len(course))print(course.upper())print(course.lower())print(course.title())print(course.find('O'))print(course.find('o'))print(course.find('Beginners'))print(course.replace('Beginners','OldBai'))print('Python'in course)print(course)
008 除法 整除 次方
print(10/3)print(10//3)print(10**3)
x =10
x = x +3print(x)
x +=3print(x)
009 运算优先级
x =10+3*2
y =10+3*2**2print(x,y)
010 引入数学方法
import math
print(math.ceil(2.9))print(math.floor(2.9))
x =2.9print(round(x))
x =-2.9print(abs(x))
011 if的使用
is_hot =True
is_hot =False# is_cold = True
is_cold =Falseif is_hot:print("It's a hot day")print("Drink plentf of water")elif is_cold:print("You are so cold")print("Wear warm clothes")else:print("It's a good day")print("Have a good time")print("From oldBai")
weight =int(input('Weight: '))
unit =input('(L)bs or (K)g: ')if unit.upper()=="L":
converted = weight *0.45print(f"You are {converted} kilos")else:
converted = weight /0.45print(f"You are {converted} pounds")
014 while循环
i =1while i<=5:print('*'*i)
i=i+1print("Done")
secret_num =9
guess_count =0
guess_limit =3while guess_count<guess_limit:
guess =int(input('Guess: '))
guess_count +=1if guess == secret_num:print('You Win')breakelse:print('You should again!')else:print('Sorry you failed')print("End")
015 while循环
command =""
started =FalsewhileTrue:
command =input("> ").lower()if command =="start":if started:print("Car is already started!")else:print("Car started...")
started =Trueelif command =="stop":ifnot started:print("Car is already stopped!")else:print("Car stopped...")elif command =="help":print("""
------->start - to start the Car
------->stop - to stop the Car
------->quit - to quit
""")elif command =="quit":print("You quit the Car")breakelse:print("Sorry , I don't understand that!")
016 for循环的多种遍历
for item in'Python':print(item)for item in['Mosh','John','Sarah']:print(item)for item in[1,2,3,4,5]:print(item)for item inrange(10):print(item)for item inrange(11,15):print(item)for item inrange(5,10,2):print(item)
prices =[10,20,30]
total =0for price in prices:
total += price
print(f"Total:{total}")
017 嵌套循环
for x inrange(4):for y inrange(4):print(f'({x},{y})')
num =[5,2,5,2,2]for item in num:
output =''for count inrange(item):
output +='x'print(output)
018 使用数组的方法
names =['John','Bob','Mosh','Sarah','Mary']print(names)
number =[3,6,9,12,15,1,11]max= number[0]for iten in number:if iten >max:max= iten
print(max)print(max(number))
019 二维数组
matrix =[[1,2,3],[4,5,6],[7,8,9]]print(matrix)print(matrix[0][0])for row in matrix:for item in row:print(item)
message =input(">")
words = message.split(' ')print(words)
emojis ={":)":"😊",":(":"😒"}
output =""for word in words :
output += emojis.get(word, word)+" "print(output)
defsquare(number):return number * number
result = square(3)print(result)
030 用函数完成表情引用
defemoji_converter(message):
words = message.split(" ")
emjis ={":)":"😊",":(":"😒"}
output =""for word in words :
output += emjis.get(word,word)+" "return output
message =input(">")print(emoji_converter(message))
031 处理错误
try:
age =int(input('Age: '))
income =20000
risk = income / age
print(age)except ZeroDivisionError:print('Age cannot be 0')except ValueError:print('Invalid value')
032 python 的 类
classPoint:defmove(self):print("move")defdraw(self):print("draw")
point = Point()
point.x =10
point.y =20print(point.x)print(point.y)
point.draw()
point.move()
033 构造函数
classPoint:def__init__(self,x,y):
self.x = x
self.y = y
defmove(self):print("move")defdraw(self):print("draw")
point = Point(10,20)
point.x =11print(point.x)
034 类的练习
classPerson:def__init__(self,name):
self.name = name
deftalk(self):print(f"Hi I'm {self.name}")
john = Person("John Smith")print(john.name)
john.talk()
035 继承
classMammal:defwalk(self):print("walk")classDog(Mammal):pass#路径语句classCat(Mammal):defbark(self):print("bark")
dog = Dog()
dog.walk()
cat = Cat()
cat.walk()
cat.bark()