<jupyter_start><jupyter_text>Notes for Using Python for Research(HarvardX PH526x)  Part 3: Manipulating Objects(对象操作) 1. Dynamic Typing(动态类型)x = 31. 创建对象(object) 32. 创建变量名(variable) x3. 插入从变量名(variable)到对象(object)之间的引用一个变量是给定对象的一个引用。 2. Copies(拷贝)1. Shallow Copy(浅拷贝)2. Deep Copy(深拷贝) 3. Statements(语句)Compound statements(复合语句) 如 if 语句。 4. For and While Loops(For 和 While 循环) 5. List Comprehensions(列表推导式/解析式/生成式)1. fast(快)2. elegant(简洁) 6. Reading and Writing Files(读写文件) 7. Introduction to Functions(函数介绍)Functions are devices for grouping statements在运行到 def 语句之间，函数是不存在的。def 语句创建一个对象，并将它赋给一个名字。 8. Writing Simple Functions(写简单函数) 9. Common Mistakes and Errors(常见错误)1. Not reading or understanding error messages.2. Forgetting that dictionaries are unordered.3. Trying to do an operation not supported by the object.4. Trying to access an object in the wrong way.5. Trying to modify an immutable object.6. Trying to operate on objects of different type7. Improper indentation.  Dynamic Typing 部分的代码<jupyter_code># immutable object
x = 3
y = x
y = y - 1
x, y
# mutable object
L1 = [2,3,4]
L2 = L1
L1[0] = 24
L1, L2
# object identity
L = [1,2,3]
M = [1,2,3]
L == M, L is M, id(L), id(M), id(L) == id(M)<jupyter_output><empty_output><jupyter_text>List Comprehension 部分的代码<jupyter_code>numbers = range(10)
squares = []
for number in numbers:
    square = number ** 2
    squares.append(square)

squares2 = [number**2 for number in numbers]
squares, squares2<jupyter_output><empty_output><jupyter_text>Reading and Writing Files 部分的代码<jupyter_code>filename = "input.txt"
for line in open(filename):
    print(line)

for line in open(filename):
    line = line.rstrip().split(" ")
    print(line)

F = open("output.txt", "w")
F.write("Python\n")
F.close()<jupyter_output><empty_output><jupyter_text>Introduction to Functions 部分的代码<jupyter_code>def add(a, b):
    mysum = a + b
    return mysum

add(12, 15)
def add_and_sub(a, b):
    mysum = a + b
    mydiff = a - b
    return mysum, mydiff

add_and_sub(15, 20)
def modify(mylist):
    mylist[0] *= 10
L = [1,3,5,7,9]
modify(L)
L<jupyter_output><empty_output><jupyter_text>Writing Simple Functions 部分的代码<jupyter_code>def intersect(s1, s2):
    res = []
    for x in s1:
        if x in s2:
            res.append(x)
    return res

intersect([1,2,3,4,5], [3,4,5,6,7])
def password(length):
    import random
    pwd = str()
    characters = "abcdefghijklmnopqrstuvwxyz" + "<PASSWORD>"
    for i in range(length):
        pwd += random.choice(characters)
    return pwd

password(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12236121970289289, 'fraction_numerical': 0.03401094605160281, 'mean_word_length': 4.526997840172786, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '102415', 'license': 'MulanPSL-1.0', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1097, 'n_words': 269, 'path': 'python_notes/using_python_for_research_ph256x_harvard/week1/3manipulating_objects.ipynb', 'repo_name': 'ZaynChen/notes', 'size': '2558'}