string_in_triple_quotes = '''Hello, Python! I believe that ‘BOXUE’ will let me become stronger and stronger. However, only “hard work” makes me better than before.'''
当字符串本身需要引号时,就使用另外的引号形式将其包裹,防止歧义
数字通过类型转换来定义
1 2
number = 1145141919810 number_to_string = str(number)
list1 = [] # This is an empty list, no any value list2 = list() # a way to create a list
number_list = [8, 6, 5, 7, 2] # This is a list filled with numbers mixed_list = [1, 'one', 2, "two", 3] # This is a list with mixed value consist of number and strings
final_list = [number_list, mixed_list] # This is a list consists of two different list
empty = () # create an empty tuple number_tuple = (1, 2, 3) # use the elements to create a tuple directly mix = tuple([1, 2, "Three"]) # use the list to pass its value to create a tuple
# 1. create the tuple empty = () # create an empty tuple number_tuple: tuple[int, int, int] = (1, 2, 3) # use the elements to create a tuple directly mix = tuple([1, 2, "Three"]) # use the list to pass its value to create a tuple
# 2. how to use the tuple # print the tuple print(number_tuple)
# add some tuples print(number_tuple * 2)
# find some elements in the tuples print(number_tuple[0]) print(number_tuple[0:2]) print(number_tuple[0:3:2]) print(3in number_tuple)
# get length of the tuple print(len(number_tuple))
# get the maximum of the tuple print(max(number_tuple))
# get the minium of the tuple print(min(number_tuple))
# if there is a specific element? print(3in number_tuple)
# delete the whole tuple del number_tuple
Python中的Dictionary
又称哈希表,指用某个具体的值而不是位置来指代另一个值,前者称为key,后者为value
创建Dictionary
1 2 3 4 5 6 7 8 9 10 11
# 1.1 Create an empty dictionary empty_dictionary = {}
# 1.2 Create a dictionary directly user = {'email': '[email protected]', 'password': '1145141919810', 'ID': 1}
# 1.3 use the keyword dict() to create(not recommended) another_user = dict({'email': 'aoi@'})
# 1.4 use another dictionary to extend it user.update({'address': 'Tokyo, Japan'})
元素删除
1 2 3 4 5 6 7
# 2. delete the key and value in the dictionary del user['password'] # del user # of course it can also delete the whole dictionary print(user)
# Caution: the key must be read-only, so only the value, tuple and dictionary could be the key. # List can't be the key!
其他方法
1 2 3
print(user.values()) # get all the values in the dictionary print(user.keys()) # get all the keys in the dictionary print('email'in user) # if there is a key like this in the dictionary?
# for num in [1, 2, 3, 4]: # print(num) for num inrange(1, 5): if num == 2: print(num) break else: print("Error!") user = {'id': 10, "Name": "Leo"} for info in user: print(info) # info means the keys of the dictionary print(user[info])
value = 1
while value <= 10: if value % 2 == 0: value += 1 continue
print(value) value += 1
Python中的Comprehension
一个for循环配合if的特殊用法,简化代码用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# About the list comprehension
numbers = [i for i inrange(1, 5) if i % 2 == 0] print(numbers)
strings = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flatten = [value for key in strings for value in key] print(flatten)
# So how to handle these errors? try: 3/0 except ZeroDivisionError: print("Divided by Zero!") except NameError: print("Invalid Name!") else: print("No Errors.") finally: print("Clean up actions.")
from Employee import Employee from Person import Person
mars = Employee('Mars', 30, 11) eleven = Employee('Mars', 30, 11) print(mars == eleven) # if you want this value is true, you need to define the attribute "__eq__" # print(Employee.__counter)
print(mars.__repr__()) print(mars.__str__()) # str use the repr, which include more information about developer # str include more information about user, it can be written.