Python Base Week jobs
1. Two ways to execute a python script
脚本前面直接指定解释器在脚本开始前声明解释器
2. Briefly describe the relationship between bits and bytes
每一个字节占用八个比特位
3, briefly describe the relationship between ASCII, Unicode, utf-‐8, GBK
utf--‐8 <-- unicode <-- gbk <-- ascii 按此方向兼容
4., please write "Li Jie" the number of digits that are encoded by utf-‐8 and GBK respectively
"李杰" 占用utf -8 占6字节 , gbk 占用4字节
What are 5.python single-line comments and multiline comments used separately?
python 单行注释 用# ,多行注释用‘‘‘‘‘‘
6. What are the considerations for declaring variables?
可读,字母数字下划线组成,第一个字符不能是数字,语句用语不可作为变量名[‘and‘, ‘as‘, ‘assert‘, ‘break‘,‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘,‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
8. How do I see the address of a variable in memory?
命令 id(变量名) 查看内存地址
9. When executing a python program. What is the role of the PYc file?
pyc 文件python文件是经过解释器编译后的文件,下次运行的话则不需要再次编译。
10. Write code
A. Implement user input user name and password, when the user name is seven and the password is 123, the display login is successful, otherwise the login failed!
user_name = input("用户名")user_passwd = input("密码")if user_name == "seven" and user_passwd == "123": print("logon success!")else: print("logon failure!")
B. Implement user input user name and password, when the user name is seven and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
count =0while True: count += 1 user_name = input("user_name:") user_passwd = input("passworld:") if user_name == "seven" and user_passwd == "123": print("logon success!") elif count>= 3 :break else: print("logon failure!")
C. Implement user input user name and password, when the user name is seven or Alex and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
count =0while True: count += 1 user_name = input("user_name:") user_passwd = input("passworld:") if user_name == "seven" or user_name == "alex" and user_passwd == "123": print("logon success!") elif count>= 3 :break else: print("logon failure!")
11. Write code
A. Using a while loop to implement output 2-3+4-5+6...+100 and
number= 1sum = 0while number <= 99 : number +=1 if number %2 == 1: sum -= number else : sum += numberprint (sum)
B. Using the For loop and range for output 1-2+3-4+5...+99 and
number =0sum = 0for i in range(1,100): number += 1 if number % 2 == 1: sum += number else: sum -= numberprint(sum)print(number)
C. Using a while loop to implement output 1,2,3,4,5,7,8,9,11,12
number= 0while number <= 11 : number +=1 if number ==6 or number == 10: continue else : print(number)
D. Using a while loop to implement all the odd numbers within the output 1-‐100
number= 0while number <= 99 : number +=1 if number%2 == 1: print(number)
E. Using a while loop to implement all the even numbers within the output 1-‐100
number= 0while number < 99 : number +=1 if number%2 == 0: print(number)
12. Write the binary representations of the 5,10,32,7 separately
128 64 32 16 8 4 2 15 0 0 0 0 0 1 0 110 0 0 0 0 1 0 1 032 0 0 1 0 0 0 0 07 0 0 0 0 0 1 1 1
13. Describe the relationship between objects and classes.
14. The following two variables are available, please briefly describe what is the relationship between N1 and N2?
n1 = 123n2 = 123n1和n2共用同一个内存地址
15. The following two variables are available, please briefly describe what is the relationship between N1 and N2?
n1 = 123456n2 = 123456没有关系
16. The following two variables are available, please briefly describe what is the relationship between N1 and N2?
n1 = 123456n2 = n1n1和n2都指向同一个内存地址
17. If there is a variable n1=5, use the provided method of int to get the minimum number of bits that the variable can represent
3个
18. What are the Boolean values?
true 和 false 真和假 0和1
19. Read the code and write out the results
a = "alex"b = a.capitalize()print(a)print(b)请写出输出结果:alex Alex
20, write code, there are the following variables, please follow the requirements to achieve each function
Name = "AleX"
a. 移除 name 变量对应的值两边的空格,并输入移除有的内容name = " aleX"name.strip()print(name)b. 判断 name 变量对应的值是否以 "al" 开头,并输出结果name = " aleX"print(name.startswith("al"))c. 判断 name 变量对应的值是否以 "X" 结尾,并输出结果name = " aleX"print(name.endswith("X"))d. 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果name = " aleX"list_name = list(name)list_name[list_name.index("l")]= "L"name = "".join(list_name)print(list_name)print (name)e. 将 name 变量对应的值根据 “l” 分割,并输出结果。name = " aleX"print(name.split("l"))f. 请问,上一题 e 分割之后得到值是什么类型?分隔之后的类型是列表g. 将 name 变量对应的值变大写,并输出结果name = " aleX"print(name.upper())h. 将 name 变量对应的值变小写,并输出结果name = " aleX"name = name.capitalize()print(name)i. 请输出 name 变量对应的值的第 2 个字符?name = " aleX"print(name[1])j. 请输出 name 变量对应的值的前 3 个字符?name = " aleX"print(name[0:3])k. 请输出 name 变量对应的值的后 2 个字符?name = " aleX"print(name[-2],name[-1])l. 请输出 name 变量对应的值中 “e” 所在索引位置?name = " aleX"print(name.index("e"))
21. Can strings be iterated? Can I use a for loop for each element?
可以name ="aleX"for i in name : print (i)
22, use the code to achieve: the use of an underscore the list of each element of the mosaic into a string,
Li = [' Alex ', ' Eric ', ' Rain ']
li = [‘alex‘, ‘eric‘, ‘rain‘]print(li[0]+"_"+li[1]+"_"+li[2])
22, write code, such as the following table, as required to achieve each function
Li = [' Alex ', ' Eric ', ' Rain ']a. Calculate the list length and output Li = [' Alex ', ' Eric ', ' Rain ']print (Len (LI)) b. The list is appended with the element "seven" and the output is added after the list li = [' Alex ', ' Eric ', ' Rain ']li.append ("seven") print (LI) c. Please insert the element "Tony" in the 1th position of the list and output the added list li = [' Alex ', ' Eric ', ' Rain ']li.insert (0, "Tony") print (LD. Please modify the element in the 2nd position of the list to "Kelly" and output the modified list of li = [' Alex ', ' Eric ', ' Rain ']li [1] = "kally" Print (LI) E. Please remove the list of elements "Eric" and output the modified list li = [' Alex ', ' Eric ', ' Rain ']li.remove ("Eric") print (LI) F. Delete the 2nd element in the list and output the value of the deleted element and the list after deleting the element li = [' Alex ', ' Eric ', ' Rain ']li.pop (1) print (LI) G. Please delete the 3rd element in the list and output the list after the delete element li = [' Alex ', ' Eric ', ' Rain ']print (Li.pop (2)) print (LI) H. Please delete the 2nd to 4th element in the list and output the list after deleting the element li = [' Alex ', ' Eric ', ' Rain ', ' seven ']del li [1:4]print (LI) i. Please invert all the elements of the list and output the inverted list of li = [' Alex ', ' Eric ', ' Rain ', ' Seven ']li.reverse () print (LI) J. Please use the index of the for, Len, range output List li = [' Alex ', ' Eric ', ' Rain ', ' seven ']for I in range (Len (LI)): print (i) K. Please use the Enumrate output list element and ordinal (ordinal starting from 100) Li = [' Alex ', ' Eric ', ' rain']for i,j in Enumerate (li,100): Print (I,J) L. Use the For loop to output all the elements of the list li = [' Alex ', ' Eric ', ' Rain ', ' seven ']for i in Li:print (i)
23, write code, such as the following table, please follow the functional requirements to achieve each function
Li = ["Hello", ' Seven ', ["Mon", ["H", "Kelly"], ' all '], 123, 446]
A. Please output "Kelly"
a.li = ["hello", ‘seven‘, ["mon", ["h", "kelly"], ‘all‘], 123, 446]print(li[2][1][1])
B. Please use the index to find the ' all ' element and modify it to "all"
b.li = ["hello", ‘seven‘, ["mon", ["h", "kelly"], ‘all‘], 123, 446]li[2][2] = "ALL"print(li)
24, write code, have the following tuple, according to the requirements of each function
Tu = (' Alex ', ' Eric ', ' Rain ')
a. 计算元组长度并输出tu = (‘alex‘, ‘eric‘, ‘rain‘)print(len(tu))b. 获取元组的第 2 个元素,并输出tu = (‘alex‘, ‘eric‘, ‘rain‘)print(tu[1])c. 获取元组的第 1--‐2 个元素,并输出tu = (‘alex‘,‘eric‘,‘rain‘)print(tu[0:2])d. 请使用 for 输出元组的元素tu = (‘alex‘,‘eric‘,‘rain‘)for i in tu : print(i)e. 请使用 for、len、range 输出元组的索引tu = (‘alex‘,‘eric‘,‘rain‘)for i in range(len(tu)) : print(i)f. 请使用 enumrate 输出元祖元素和序号(序号从 10 开始)tu = (‘alex‘, ‘eric‘, ‘rain‘)for i,j in enumerate(tu,10): print(i,j)
25, there are the following variables, please implement the required functions
tu = ("alex", [11, 22, {"k1": ‘v1‘, "k2": ["age", "name"], "k3": (11,22,33)}, 44])a. 讲述元祖的特性元素不可更改b. 请问 tu 变量中的第一个元素 “alex” 是否可被修改?不可以c. 请问 tu 变量中的"k2"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”k2 对应的值是列表类型.可以修改tu = ("alex",[11,22,{"k1":‘v1‘,"k2":["age","name"],"k3":(11,22,33)},44])tu[1][2]["k2"].append("seven")print(tu)d. 请问 tu 变量中的"k3"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”"k3"对应的值的类型是元组,不可修改
26. Dictionaries
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}a. 请循环输出所有的 keydic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}for i in dic : print (i)b. 请循环输出所有的 valuedic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}for i in dic : print (dic[i])c. 请循环输出所有的 key 和 valuedic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}for i in dic : print (i,dic[i])d. 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}dic["k4"] = "v4"print (dic)e. 请在修改字典中 “k1” 对应的值为 “alex”,输出修改后的字典dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}dic["k1"] = "alex"print (dic)f. 请在 k3 对应的值中追加一个元素 44,输出修改后的字典dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}dic["k3"].append(44)print (dic)g. 请在 k3 对应的值的第 1 个位置插入个元素 18,输出修改后的字典dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}dic["k3"].insert(0,18)print (dic)
27. Conversion
a. 将字符串 s = "alex" 转换成列表s = "alex"s =[s]print(s)b. 将字符串 s = "alex" 转换成元祖s = "alex"s =(s)print(s)b. 将列表 li = ["alex", "seven"] 转换成元组li = ["alex", "seven"]s=(li[0],li[1])print(s)c. 将元祖 tu = (‘Alex‘, "seven") 转换成列表tu = (‘Alex‘, "seven")s = []for i in tu : s.append(i)print(s)d. 将列表 li = ["alex", "seven"] 转换成字典且字典的 key 按照 10 开始向后递增li = ["alex", "seven"]s={}count =10for i in li : s[count] = i count += 1print(s)
27. transcoding
n = "老男孩"a. 将字符串转换成 utf--‐8 编码的字节,并输出,然后将该字节再转换成 utf--‐8 编码字符串,再输出n = "老男孩"print(n)print( n.encode("utf--8"))print( n.encode("utf--8").decode())a. 将字符串转换成 gbk 编码的字节,并输出,然后将该字节再转换成 gbk 编码字符串,再输出n = "老男孩"print(n)print( n.encode("gbk"))print( n.encode().decode())
28, for all the numbers within the 1‐100 and
sum = 0for i in range(101): sum += iprint(sum)
29. Element classification
有如下值集合 [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个 key 中,将小于 66 的值保存至第二个 key 的值中。即: {‘k1‘: 大于 66 的所有值, ‘k2‘: 小于 66 的所有值}number = set([11,22,33,44,55,66,77,88,99,90])dic = {"k1":[],"k2":[]}for i in number : if i > 66: dic["k1"].append(i) elif i < 66: dic ["k2"].append(i)print (number)print (dic)
30. Shopping Cart
Functional requirements: Require the user to enter total assets, for example: 2000 display a list of items, let the user select items according to the serial number, add shopping cart purchase, if the total amount of goods is greater than the total assets, the account balance is insufficient, otherwise, the purchase success. Goods = [{"Name": "Computer", "Price": 1999}, {"name": "Mouse", "Price": ten}, {"name": "Yacht", "Price": 20}, {"Name": "Beauty", "price": 998},]goods = [{"Name": "Computer", "Price": 1999}, {"name": "Mouse", "Price": ten}, {"Name": " Yacht "," Price ": +}, {" name ":" Beauty "," price ": 998},]salary =" 1 "Print ("----welcome! ----") while type (salary)! = Type (1): try:salary = Int (input (" Enter your Ralary: ")) Except:print (" Wrong type! ") set = salary = Int (salary) print ("You can buy these items:") while True:for key in range (1,len (goods) +1): Print (Key, ".", goods[key-1]["name"],goods[key-1]["Price"]) Choice = input ("Please input the serial number of goods you want:" if choice = = "Q": Break try:choice = Int (choice) except:print ("Wrong type!") Continue if choice < 1 or choice >len (goods): print ("Product not found!") Continue else: Salary = salary-goods[choice-1]["Price"] Print (Salary)
Python Base Week job