whileTrue: city = input('Please input city name:') if city == 'quit': break else: print(f'I would like to go {city}') current_number = 0 while current_number <= 10: if current_number % 2 == 0: continue else: print(current_number) current_number += 1
# 这样修改函数不会影响原来的列表 name = ['Tom','Bob'] a = name[:]
# 调用函数时若不给形参指定参数则会使用默认值,注意有默认值的参数一定要放在后面 defmy_pet(pet_name,animal_type='dog'): print(f'I have a {animal_type.title()}') print(f'{animal_type.title()}\'s name is {pet_name}') my_pet(pet_name='Wangcai') print('-'*20) my_pet(animal_type='cat',pet_name='Wangcai') # 按照顺序赋予变量可以省略形参数的名字 print('-'*20) my_pet('wangcai','pig')
student = get_formatted_name('yaowei','wang') print(student) print('-'*20) s = get_formatted_name('yaowei','wang','ss') print(s)
# 传入任意数量的实参数,*args,*让python创建了一个名为args的空元组,所有接收到的变量都会被封存到里面。 defmake_pizza(*args): print('The following toppings are required in a pizza') for topping in toppings: print(f"- {topping}") components = ['mushroom','tomato','cheese'] make_pizza(components)
# 传入任意数量的实参数,还有实参在一起;*arg表示任意数量的实际参数 defmake_pizza(size,*args): print(f'The pizza is {size} inches') print('The following toppings are required in a pizza') for topping in args: print(f"- {topping}") components = ['mushroom','tomato','cheese'] make_pizza('7',components)
# 类的首字母要大写,切后面没有括号,类要采用驼峰命名法 # 类中的函数成为方法,__init__是一种特殊方法 # self.name可以被类中的所用方法访问,其被称为属性 classDog: '''我第一次写的小狗的类''' def__init__(self,name,age): self.name = name self.age = age defsit(self): print(f'{self.name} is sitting!') defrolling(self): print(f'{self.name} is rolling over!') my_dog = Dog(name='Wangcai',age='6') # 上面等价于my_dog = Dog('Wangcai','6') my_dog.sit()
# 一个经典的汽车类例子 classCar: def__init__(self,make,model,year): self.make = make self.model = model self.year = year self.odometer = 0 defget_name(self): print(f'{self.make}{self.model}{self.year}') defread_odometer(self): print(f'This car is {self.odometer} miles on it') defupdate_odometer(self,mileage): if mileage > self.odometer: self.odometer = mileage else: print('You can not roll back the odometer!') defincrease_odometer(self,miles): if miles > 0: self.odometer += miles else: print('You can not roll back the odometer!') classElectriccar(Car):#括号中要写父类的名字,下面有super def__init__(self,make,model,year): super().__init__(make,model,year) self.battery = Battery() classBattery: def__init__(self,battery_size=75): self.battery_size=battery_size defdescrib_battery(self): print(f'The battery size is {self.battery_size} inches') defget_range(self): if self.battery_size == 75: range = 100 else: range = None print(f'The car will run {range} miles') my_car = Car('benz','A6','2020') my_car.get_name() my_car.read_odometer() # 可以直接访问属性的类 print(my_car.odometer)
第十章:文件和异常
读取文件
with关键词之后不需要关闭文件;
1 2 3 4 5 6 7
with open('pi.txt') as f: contents = f.read() print(contents) # 一行行的读取 with open('pi.txt') as f: contents = f.readlines() print(contents)
try: print(5/0) except ZeroDivisionError: print("You can not divede zero") # except后面可以指定特定的错误类型也可以不指定 defdivision(i): try: print(5/i) except : # print(f"{i} can not divede") pass for i in [1,2,3,0,5]: division(i) # 可以用pass直接跳过错误 >>> 5.0 2.5 1.6666666666666667 1.0