This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1 | |
| numbers = [-4, -3, -2, -1, 0, 2, 4, 6] | |
| negative_and_zero_numbers = [n for n in numbers if n <= 0] | |
| print(negative_and_zero_numbers) | |
| # 2 | |
| list_of_lists = [[[1, 2, 3]], [[4, 5, 6]], [[7, 8, 9]]] | |
| flattened_list = [num for level1 in list_of_lists for level2 in level1 for num in level2] | |
| print(flattened_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import string | |
| import random | |
| # 1 | |
| # 多个字符中生成指定数量的随机字符序列(list): | |
| # random.sample('字符', number) # number means 指定数量 | |
| # 注意:random.sample('字符', number) 生成的是 list | |
| def random_user_id(): | |
| user_id = ''.join(random.sample(string.ascii_letters + string.digits, 6)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import cmath | |
| # 1 | |
| def add_two_number(num1, num2): | |
| total = num1 + num2 | |
| return total | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1 | |
| n = 0 | |
| while n <= 10: | |
| print(n) | |
| n += 1 | |
| # 2 | |
| print() | |
| n = 10 | |
| while n >= 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If Condition | |
| # In python and other programming languages, | |
| # the key word if we use to check if a condition is true and to execute the block code. | |
| # Remember the indentation (缩进) after the colon (冒号). | |
| # syntax | |
| # if condition: | |
| # this part of code runs for true conditions | |
| a = 3 | |
| if a > 0: | |
| print('A is a positive number') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A dictionary is a collection of unordered, modifiable(mutable) paired (key: value) data type. | |
| # Creating a Dictionary | |
| # To create a dictionary we use curly brackets, {}. | |
| # syntax | |
| empty_dict = {} | |
| # Dictionary with data values | |
| dct = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} | |
| person = { | |
| 'first_name': 'Fangyu', | |
| 'last_name': 'Qu', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # sets | |
| it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'} | |
| A = {19, 22, 24, 20, 25, 26} | |
| B = {19, 22, 20, 25, 26, 24, 28, 27} | |
| age = [22, 19, 24, 25, 26, 24, 25, 24] | |
| # 1 | |
| print(len(it_companies)) | |
| # 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1 | |
| # method 1 | |
| empty_tuple = () | |
| print(empty_tuple) | |
| # method 2 | |
| empty_tuple = tuple() | |
| print(empty_tuple) | |
| # 2 & 3 | |
| sisters = ('Alice', 'Cici') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1 | |
| # method 1 | |
| lst = [] | |
| # method 2 | |
| lst = list() | |
| # 2 | |
| lst = ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou'] | |
| # 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The most common escape characters: | |
| # \n: new line | |
| # \t: Tab (means 8 spaces) | |
| # \\: Back slash (反斜杠,slash:斜杠) | |
| # \': Single quote (') | |
| # \": Double quote (") | |
| print('I hope everyone is enjoying the Python Challenge.\nAre you ?') # \n: line break(换行符) | |
| print('Days\tTopics\tExercises') |