# 不明白 # def print_nums(x): # for i in range(x): # print(i) # return # print_nums(10) # ------------------------ # rest=4+0、rest=4+1、rest=4+2、rest=4+3 # def func(x): # res = 0 # for i in range(x): # res += i # return res # # print(func(4)) #不输出字符长度 # coding: utf-8 # msg = "Hello world!" # file = open("newfile.txt", "w") # amount_written = file.write(msg) # print(amount_written) # file.close() #不理解(squares[8] = 84:为啥插入到index0,而squares[5] = 54 插入到最后) # squares = {1: 1, 2: 4, 3: "error", 4: 16,} # squares[8] = 84 # squares[3] = 9 # print(squares) #不理解 # primes = {1: 2, 2: 3, 4: 7, 7:17} # print(primes[primes[4]]) # 不理解: # fib = {1: 1, 2: 1, 3: 2, 4: 3} # print(fib.get(4, 0) + fib.get(7, 5)) # print(fib.get(4, 0) ) # 谨记:元组可以在没有括号的情况下创建,只需用逗号分隔值(元组比列表快,但是元组不能改变。) # my_tuple = "one", "two", "three" # print(my_tuple[0]) # 为啥结果是[49, 36] # sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # print(sqs[7:5:-1]) # evens=[i**2 for i in range(10) if i**2 % 2 == 0] # for i in range(10): 为啥不是0,4,8,12,16 # if i ** 2 % 2 == 0: # evens=i ** 2 # print(evens) # 0,1,2,3,4,5,6,7,8,9 0,2,4,6,8, # 字符串函数(作为笔记 记录下来) # join - 以另一个字符串作为分隔符连接字符串列表。 # split 方法与 join 相反,把一个字符串转换成一个列表。 print(", ".join(["spam", "eggs", "ham"])) #打印 "spam, eggs, ham" print("spam, eggs, ham".split(", ")) #打印 "['spam', 'eggs', 'ham']" # 要将数字四舍五入到一定的小数位数,请使用 round 。 # enumerate 函数可以用来同时迭代列表的值和索引。 # 不懂(为啥返回2) # nums = [-1, 2, -3, 4, -5] # if all([abs(i) < 3 for i in nums]): # print(1) # else: # print(2)