Skip to content

Instantly share code, notes, and snippets.

@ChuckleQu
ChuckleQu / 13_Day_Exercise.py
Created September 16, 2020 14:10
13_Day_List_Comprehension
# 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)
@ChuckleQu
ChuckleQu / 12_Day_Exercise_1_2.py
Created September 16, 2020 14:09
12_Day_Modules
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))
@ChuckleQu
ChuckleQu / 11_Day_Exercise.py
Created September 16, 2020 14:08
11_Day_Functions
import math
import cmath
# 1
def add_two_number(num1, num2):
total = num1 + num2
return total
@ChuckleQu
ChuckleQu / 10_Day_Exercise.py
Created September 16, 2020 14:08
10_Day_Loops
# 1
n = 0
while n <= 10:
print(n)
n += 1
# 2
print()
n = 10
while n >= 0:
@ChuckleQu
ChuckleQu / 09_Day_Conditionals.py
Created September 16, 2020 14:07
09_Day_Conditionals
# 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')
@ChuckleQu
ChuckleQu / 08_Day_Dictionaries.py
Last active September 16, 2020 14:05
08_Day_Dictionaries
# 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',
@ChuckleQu
ChuckleQu / 07_Day_Exercise.py
Created September 16, 2020 14:01
07_Day_Sets
# 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
@ChuckleQu
ChuckleQu / 06_Day_Exercise.py
Created September 16, 2020 14:01
06_Day_Tuple
# 1
# method 1
empty_tuple = ()
print(empty_tuple)
# method 2
empty_tuple = tuple()
print(empty_tuple)
# 2 & 3
sisters = ('Alice', 'Cici')
@ChuckleQu
ChuckleQu / 05_Day_Exercise_Level_1.py
Last active September 16, 2020 13:59
05_Day_Lists
# 1
# method 1
lst = []
# method 2
lst = list()
# 2
lst = ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou']
# 3
# 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')