Python classic interview question one
1. What is the difference between python2 and python? List at least five
- The print statement in Python 2 is replaced by the print() function in Python 3. In Python 3, the object to be output must be enclosed in parentheses;
- Python 2 has an ASCII-based str() type, which can be converted to a unicode type through a separate unicode() function, but there is no byte type.
In Python 3, finally there are Unicode (utf-8) strings, and two byte classes: bytes and bytearrays.
- There are xrange() and range() in Python2.x. xrange() is a lazy mechanism. If you only loop once, it is recommended to use range(). For this, range() will create multiple lists in memory, which is expensive. .
There is only range() in python3, and range has a new contains method. The contains method can effectively speed up the "search" of integers and booleans in Python 3.x.
- Exception handling, you must use'as' to handle in python3.x, and you don't need to use it in python2.x.
- In python2.x, the .Next() function can be used as an attribute of the function, or as a function alone;
Only functions can be used in python3.x, use. Next() will trigger attributeError.
- Use raw_input() to parse user input in python2.x, and use input() to parse in python3.x.
2. What are the built-in data types of python
- Integer --int
- Boolean--bool
- String --str
- List --list
- Tuple - tuple
- Dictionary --dict
3. Ways to improve the efficiency of python operation
- Use generators because you can save a lot of memory.
- Loop code optimization to avoid excessive repetitive code execution.
- The core module uses Cython PyPy etc. to improve efficiency.
- Multi-process, multi-thread, coroutine.
- Multiple if elif conditional judgments can put the most likely condition to happen first, which can reduce the number of program judgments and improve efficiency.
**4. What is PEP8? **
"Python Enhancement Proposal #8" (Python Enhancement Proposal #8) is also called PEP8, a style guide he compiled for the python code format.
**5. What tools can help debug or do static analysis? **
- PyChecker is a static analysis tool that not only reports errors in the source code, but also reports the type and complexity of errors.
- Pylint is another tool to check whether a module meets the code standard.
**6. How to convert tuple and list in Python? **
Just use tuple and list functions directly, type() can determine the type of the object.
**7. Please write a piece of Python code to delete duplicate elements in a list. **
Use the set function
set(list)
Use dictionary functions
a=[1,2,4,2,4,5,6,5,7,8,9,0]
b={}
b=b.fromkeys(a)
c=list(b.keys())print(c)
**8. How to generate random numbers in Python? **
The module used to generate random numbers in Python is random, which needs to be imported before use. For example:
- random.random(): Generate a random floating point number between 0-1
- random.randint(a,b): Generate an integer between [a,b], including a,b
- random.uniform(a,b): Generate floating-point numbers between [a,b]
- random.randrange(a,b,step): In the specified set (a,b), take a random number based on step, without b
- random.choice(sequence): randomly select an element from a specific sequence, where the sequence can be a list of characters, tuples, etc.
**9. What is pass in Python? **
pass is a statement that will not be executed in Python. In complex sentences, if a place needs to be left blank temporarily, it is often used as a placeholder.
10. String formatting: the difference between% and .format
The format function of the string is very flexible and powerful. It can accept an unlimited number of parameters, and the position can be out of order, and there are more powerful format qualifiers (such as: padding, alignment, precision, etc.).