python-basics-practice and interview questions

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

  1. 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
  2. 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)]
  3. 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
  4. 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')
  5. 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))
  6. Swap the values ​​of the a and b variables without using other variables
    a=a+b; a=ab; a=ab;
  7. How to set a global variable
    global in a function to declare

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173775&siteId=291194637