print("Hello Python world!") # 变量 message="Hello" print(message) message="Hello Python world!" print(message)# 引号:可以是单,可以是双 message='I told my friend, "Python is my favorite language!"' print(message) message="I told my friend, 'Python is my favorite language!' " print(message)# 大小写函数 name="my friend" print(name.title()) print(name.upper()) print(name.lower())# 拼接 first_name = "ada" last_name = "lovelace" full_name = first_name + " " + last_name print(full_name) print("Hello, " + full_name.title() + "!")#使用制表符或换行符来添加空白 print("\tPython\tPython") print("Languages:\n\tPython\n\tC\n\tJavaScript")#空白处理 print('空白处理') favorite_language = ' python ' print(favorite_language) print(favorite_language.lstrip()) print(favorite_language.rstrip()) print(favorite_language.strip())message = "One of Python's strengths is its diverse community." print(message)#数字计算 print((2 + 3) * 4) print(3**2) print(0.2+0.1) print(3 * 0.1) print(3/2) print(3.0/2)#数字转换字符串 age = 23 message = "Happy " + str(age) + "rd Birthday!" print(message)#在Python中,用方括号([] )来表示列表,并用逗号来分隔其中的元素。 bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) print(bicycles[0]) print(bicycles[0].title()) print(bicycles[-2]) message = 'My first bicycle was a ' + bicycles[0].title() + "." print(message) #修改列表元素 motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) motorcycles[0] = 'ducati' print(motorcycles) motorcycles.append('ducati') print(motorcycles) del motorcycles[0] print(motorcycles);print('方法pop() 可删除列表末尾的元素') #方法pop() 可删除列表末尾的元素 motorcycles.pop() popped_motorcycle = motorcycles.pop() print(motorcycles) print(popped_motorcycle) first_owned = motorcycles.pop(0) print('The first motorcycle I owned was a ' + first_owned.title() + '.') print(motorcycles) motorcycles = ['honda', 'yamaha', 'suzuki'] first_owned = motorcycles.pop(0) print('The first motorcycle I owned was a ' + first_owned.title() + '.') #根据值删除元素 print('根据值删除元素') motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) too_expensive='ducati' motorcycles.remove('ducati') print(motorcycles) print("\nA " + too_expensive.title() + " is too expensive for me.")print("使用方法 使 sort() 对列表进行永久性排序") cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars)cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort(reverse=True) print(cars)cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.reverse() print(cars)print("使用方法 使 sorted() 对列表进行临时性排序") cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) print(sorted(cars)) print(cars)print("len") cars = ['bmw', 'audi', 'toyota', 'subaru'] print(len(cars))print("列表超出范围") motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[0]) print(motorcycles[-1])
print("for循环遍历整个列表") magicians = ['alice', 'david', 'carolina'] for magician in magicians:print(magician)magicians = ['alice', 'david', 'carolina'] for magician in magicians:print(magician.title() + ", that was a great trick!")print("I can't wait to see your next trick, " + magician.title() + ".\n") print("Thank you, everyone. That was a great magic show!")#函数range() 让Python从你指定的第一个值开始数,并在到达你指定的第二个值 后停止,因此输出不包含第二个值(这里为5)。 print("创建数值列表") for value in range(1,5): print(value) numbers = list(range(1,6)) print(numbers) even_numbers = list(range(2,11,2)) print(even_numbers)squares = [] for value in range(1,11):squares.append(value**2)print(squares)digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print(min(digits)) print(max(digits)) print(sum(digits))print("使用列表的一部分") players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[0:3]) players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[2:]) print("遍历切片") players = ['charles', 'martina', 'michael', 'florence', 'eli'] print("Here are the first three players on my team:") for player in players[:3]:print(player.title())my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] my_foods.append('cannoli') friend_foods.append('ice cream') print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)print("元组") dimensions = (200, 50) print(dimensions[0]) print(dimensions[1])dimensions = (200, 100) for dimension in dimensions:print(dimension)####################### 第 5 章 if 语句 ################################ print("##############第 5 章章 if 语句##############") cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars:if car == 'bmw': print(car.upper())else:print(car.title())car = 'bmw' car == 'bmw' print(car == 'bmw')answer = 17 if answer != 42: print("That is not the correct answer. Please try again!")requested_toppings = ['mushrooms', 'onions', 'pineapple'] print('mushrooms' in requested_toppings)age = 12 if age < 4: print("Your admission cost is $0.") elif age < 18: print("Your admission cost is $5.") else:print("Your admission cost is $10.")