Pass parameters to the program
import sys
print(sys.argv)
operation result:
list comprehension
The so-called list comprehension refers to the lightweight loop to create a list
1. The basic way
2. Use if in the loop
3. 2 for loops
4. 3 for loops
interview
- In Python, what is the difference between a class and an object? How does an object access the methods of a class? What do you do when you create an object? For
example, an animal is a class, and a dog is an object. You can use object.methods to access methods in a class. Object creation and initialization - Please write a piece of Python code to group the elements in a list, for example [1,2,3,...100] becomes [[1,2,3],[4,5,6]....]
a=[b[x:x+3] for x in ranger(1,101)] - Please write a piece of Python code to delete duplicate elements in a list
1 Use set to do it
2 Open a list to judge - Design and implement traversing directories and subdirectories, grab .pyc files
#设计实现遍历目录与子目录,抓取.pyc 文件 import os def getFiles(dir, suffix): res = [] for root, dirs, files in os.walk(dir): for filename in files: name, suf = os.path.splitext(filename) if suf == suffix: res.append(os.path.join(root, filename)) print(res) getFiles("./", '.py')
- Write a function, given a parameter n, generate an array containing n elements with values 1~n, the elements are in random order, but the values are not repeated
import random def random_list(n): list =random.sample(range(1,n+1),n) return list print(random_list(6))
- Swap the values of the a and b variables without using other variables
a=a+b; a=ab; a=ab; - How to set a global variable
global in a function to declare