The latest Python interview questions in 2020 (1): Python Basics

Autumn maple 2020-11-16 10:29:47
latest python interview questions python


1. What is? Python? Use Python What are the benefits ?

There are as many kinds of computer programming languages as there are in the world , for example Python、R、C、C++、C#、Java、JavaScript、Go、SHELL、PHP and MATLAB etc. , among ,Python  It's an interpretive type 、 object-oriented 、 Advanced programming language for dynamic data types .Python  You don't need to define variable types when writing , At run time, variable types are forced to be fixed , It's a strongly typed language .Python  No need to compile , It can be run directly in the interpreter environment .

Python  This programming language , from  Guido van Rossum  On 1989 At the end of the year , The first public release was released on  1991  year ,Python  Source code follows  GPL(GNU General Public License)  agreement .Python  There are objects 、 modular 、 Threads 、 process 、 Exception handling and automatic memory management ,Python  The language is simple 、 convenient 、 Easy to expand , And there are many self-contained modules and open source ,Python  It can be applied to multiple platforms , Include Windows、Linux and Mac Operating system, etc . On the whole ,Python  It has the following characteristics :

  1. Python  It's an interpretative language , therefore ,Python  Code doesn't need to be compiled before it runs .
  2. Python  Is a dynamically typed language , When you declare variables , You don't need to specify the type of the variable .
  3. Python  Suitable for object-oriented programming , Because it supports defining classes by composition and inheritance .
  4. To write  Python  The code is easier , But it's usually slower than compiled languages .
  5. Python  A wide range of uses , Often used as   Glue language , Help other languages and components improve their health .
  6. Programmers use  Python  You can focus on the design of algorithms and data structures , Instead of dealing with the underlying details .
  7. If you like to program, you can add some Q Group 867067945, You can get free learning materials when you join the group ! The group is full of friends who want to learn programming

2. Python What are the common keywords in ?

Python  Keywords cannot use them as any identifier names , Keywords are case sensitive .Python  The standard library of provides a  keyword  modular , You can output all keywords of the current version , As shown below :





3. Python 2.x and Python 3.x What are the main differences between versions ?

Python Of 3.x The version is relative to Python Early versions , It's a big upgrade . In order not to put too much burden on the system , Python 3.x No consideration was given to downward compatibility in the design , namely Python3.x and Python2.x It's not compatible . Many are aimed at early Python None of the version designed programs can be found in Python 3.x On normal execution . In order to take care of the existing procedures ,Python 2.6 As a transitional version , Basically used Python 2.x The grammar and the library , Also consider to Python 3.x Migration , Support section Python3.x The syntax and functions of .

Up to date Python It is recommended to use Python 3.x The syntax of the version .Python 3.x The main changes are as follows :

If you like to program, you can add some Q Group 867067945, You can get free learning materials when you join the group ! The group is full of friends who want to learn programming

(1) print function

stay Python 3.x in ,print  The sentence is gone , In its place  print  function . stay Python 2.6 and Python 2.7 in , The following three forms are equivalent :








(2) Unicode

Python 2.x By default  ASCII  code , therefore Python 2.x Chinese is not supported by default , And variable name can only be used in English .Python 3.x By default  UTF-8  The coding , therefore Python 3.x The default is to support Chinese , And variable naming can use Chinese . If in Python No header information specified in the file  # -*-coding:utf-8-*-., So in Python 2.x By default  ASCII  code , stay Python 3.x By default  UTF-8  code . because Python 3.x The source file defaults to  UTF-8  code , This makes the following code legal :



 Python 2.x:



 Python 3.x:




(3) Division operations

Python The division in is very high-end compared with other languages , It has a very complicated set of rules .Python Division in has two operators :/ and //. stay Python 2.x in , about / Algorithm , The result of dividing an integer is an integer , It will completely ignore the decimal part , Floating point division preserves the decimal part , Get the result of a floating point number . stay Python 3.x in ,/ Division doesn't do that anymore , The result of dividing integers will also be floating point numbers .




  And for  //  division , This division is called  floor  division , It will automatically perform a... On the result of division  floor  operation , stay Python 2.x and Python 3.x Is the same .



  It should be noted that ,//  It's not about abandoning the decimal part , But to perform  floor  operation . If you want to intercept the integer part , Then you need to use  math  Modular  trunc  function .




(4) Octal literal means

stay Python 3.x in , There is only one way to represent octal literals , That's it  0o1000, and Python 2.x Medium  01000  The form is no longer available .





(5) Inequality operator

stay Python 2.x in , It's not equal to   There are two ways to write , Namely  !=  and  <>  stay Python 3.x Removed from  <>  Only  !=  A way of writing .

(6) Removed repr expression ``

stay Python 2.x Back quotation marks are equivalent to  repr  function ( Returns the string Format ) The role of . stay Python 3.x Removed from `` This kind of writing , Only use  repr  function , The purpose of this is to make the code look clearer .




(7) Open file

stay Python 2.x Use in file(…) perhaps open(…) , And in the Python 3.x Can only be used in open(…)

(8) data type

Python 3.x In addition to the long type , Now there is only one type of integer int, But it behaves like 2.x Version of long identical . And new bytes type , Corresponding to 2.x Version of the octet string , Define a bytes The method of literal measure is as follows :




 str Objects and bytes Object can be used  .encode()  and  .decode()  Methods transform each other .




(9) input and raw_input function

stay Python 2.x in ,raw_input All input data is treated as a string , The return value is of string type . and input Input must be a legal Python expression , Format and Python The code in is the same , The return value is the same as the input data type . If it is Python 2.x edition , that input and raw_input You can use . If it is Python 3.x edition , Then only use input. therefore , It is recommended to use input function .

If you like to program, you can add some Q Group 867067945, You can get free learning materials when you join the group ! The group is full of friends who want to learn programming

(10) map、filter and reduce

stay Python2.x Input in interactive mode map and filter, You can see that the two types are built-in function( Built in functions )



  The result types they output are all lists :



  But in Python3.x Changes in :



  First they go from functions to classes , secondly , Their return result also changes from a list to an iteratable object , You can try to use next Function to do manual iteration :




For the more high-end reduce function , It's in Python3.x China no longer belongs to built-in function, It was moved to functools Modules .

(11) xrange function

xrange You can generate sequences . stay Python3.x It's cancelled xrange function , While using range Instead of xrange function . stay Python 3.x Call in xrange The function will report an error .

stay Python2.x in xrange() Function usage and range() The function is exactly the same , The difference is that it doesn't generate a list , It's a generator .




4. Python What are the common operators in ?

Python The operators supported by the language include arithmetic operators 、 Compare ( Relationship ) Operator 、 Assignment operator 、 An operator 、 Logical operators 、 Member operators and identity operators .

If you like to program, you can add some Q Group 867067945, You can get free learning materials when you join the group ! The group is full of friends who want to learn programming

(1) Python Arithmetic operator

Hypothetical variables a by 10, Variable b by 21, The functions of various arithmetic operators are shown in the table below :




(2) Python Comparison operator

The following hypothetical variables a by 10, Variable b by 20:




All comparison operators return 1 Said really , return 0 Said the false . This is different from special variables True and False Equivalent . Be careful : The uppercase and lowercase meanings of these variable names are different , Distinguish .

(3) Python Assignment operator

The following hypothetical variables a by 10, Variable b by 20:




(4) Python An operator

Bitwise operators calculate numbers as binary .Python The bitwise algorithm in is as follows :

Variables in the following table a by 60、b by 13, The binary format is as follows :







(5) Python Logical operators

Python Language supports logical operators , The following hypothetical variables a by 10,b by 20:




priority : not>and>or
about a or b Come on , If a It's true , Then the value is a, Otherwise b; about a and b Come on , If a It's true , Then the value is b, Otherwise a. for example :





(6) Python member operator

In addition to some of the above operators ,Python Member operators are also supported in and not in





(7) Python Identity operator

Identity operators are used to compare the storage units of two objects .





5. Operator is And == What's the difference? ?

Python The object in contains three elements :id、type、value. among ,id Used to uniquely identify an object ,type Identify the type of object ,value Is the value of the object .

is Judge a Whether the object is b object , Used to determine whether two variable reference objects are the same , It's through id To determine the . == Judge a Whether the value of the object and b The values of objects are equal , It's through value To determine the . for example :




6. Python What data types are there ? What are variable types and immutable types ?

Python The inherent data types are divided into variable and immutable types .

Variable data type   Indicates that the value of a variable is allowed to change , If the value of the corresponding variable of the data type is changed , Then the memory space will not be reallocated . That is to say, if we do  append  or  +=  After operation , Just changed the value of the variable , Instead of creating a new object , The memory address of the object referenced by the variable does not change . For different objects with the same value , In memory, there are different objects , That is, each object has its own address , It is equivalent to saving multiple copies of objects with the same value in memory , There is no reference count , It's a real object . Variable data types include : list (list)、 aggregate (set) and Dictionaries (dic).

Immutable data types   Indicates that the value of a variable is not allowed to change , If the value of the corresponding variable of the data type is changed , Then the memory space will be reallocated , So I created a new object , Immutable data types include : character string (str)、 Tuples (tuple) And numbers (number).

For the same value in memory , There is only one object in memory , There will be a reference count inside to record how many variables refer to the object . When the value of the variable corresponding to the immutable data type changes, its memory address will change , After the value of variable data type corresponding variable is changed, its memory address will not change . The following is an experimental demonstration of variable and immutable data types .

(1) integer





You can find , When the data changes , The memory address of the variable has changed , Then an integer is an immutable data type .

(2) character string




You can find , When the data changes , The memory address of the variable has changed , Then a string is an immutable data type .

(3) Tuples

You can put a list in the element of the tuple , Change the value of the list to see if the tuple is mutable or immutable .




You can find , Although tuple data changes , But the memory address hasn't changed , But we can't judge tuple as variable data type . Changed the value of the list in the tuple , But because lists are variable data types , So although the values are changed in the list , But the address of the list hasn't changed , The value of the address of the list in the tuple does not change , So it means that the tuple has not changed . therefore , You can think of tuples as immutable data types .

(4) aggregate

Sets are often used for de duplication and relational operations , Sets are unordered . Use braces or set() Function to create a collection . It should be noted that , If you want to create an empty collection , Then you have to use set() instead of {}, The latter is used to create empty dictionaries . Curly braces can't create a collection of elements that contain dictionaries and lists .




You can find , Although the aggregate data changes , But the memory address doesn't change , This shows that the set is a variable data type .

(5) list

The list is Python One of the basic data types in , There are also data types similar to lists in other languages , for example js Middle is called array , It is a [] The enclosed , Each element is separated by commas , It can store all kinds of data types .





You can find , Although the list data changes , But the memory address doesn't change , It can be seen that the list is also a variable data type .

(6) Dictionaries

The dictionary is Python The only mapping type in , Use key value pairs (key-value) In the form of data storage .Python Yes key By hash function operation , According to the result of the calculation value Storage address , So the dictionary is out of order . But in Python3.6 After version , Dictionaries start out in order , This is the new version feature . Dictionary key Values can be integers 、 Immutable objects such as strings or tuples , But it can't be a list 、 aggregate 、 Variable objects such as dictionaries .




You can find , Although the dictionary data has changed , But the memory address doesn't change , Thus, dictionaries are also variable data types .

7. list 、 Tuples 、 aggregate 、 What are the differences between dictionaries ?

list (list): It's a variable length data storage container , The corresponding data can be obtained by subscript index .
Tuples (tuple): Fixed length immutable sequential containers , High access efficiency , Suitable for storing some constant data , It can be used as a key to a dictionary .
aggregate (set): disorder , Elements only appear once , It can automatically remove the weight .
Dictionaries (dict): Of variable length hash Dictionary container . The storage mode is key value pair , The corresponding value can be obtained through the corresponding key ,key Support for multiple types .key Must be immutable and unique . The detailed differences between them are shown in the table below :




8. What's the difference between a list and a tuple ?

Sequence is Python The most basic data structure in . Each element in the sequence is assigned a numeric index , The index of the first element is 0, The index of the second element is 1, And so on .Python Yes 6 Built in types of sequences , But the most common are lists and tuples .

Array in Python Is called a list , Lists and tuples are Python Important built-in sequences in , Both lists and tuples have indexes 、 Slicing, etc . Generally speaking , Lists can replace tuples . however , To limit certain elements , You'll use tuples . The main differences between lists and tuples are as follows :

  1. The list can be modified , Tuples cannot be modified . If you want to add and delete elements , You can only use lists . Tuples are called read-only lists , That is, data can be queried , But it can't be modified . If tuples are defined , Its length and content are fixed , Cannot be modified , That is, tuples cannot be updated 、 Add and delete operations .
  2. List with  []  Express , Tuple use  ()  Express .
  3. A list cannot be used as a dictionary key, And tuples can .
  4. If there is only one element in the tuple , Then you need to put a comma after the element .

9. Python What are the common data type conversion functions in ?

Python Provides built-in functions that convert variables or values from one type to another , Built in functions encapsulate various conversion functions , You can cast a type using the target type keyword , for example ,int The function converts a numeric string into an integer in a mathematical format . Common type conversion functions are as follows :




10. list 、 Tuples 、 How to convert sets and dictionaries to each other ?







11. The real question

  1. 1 or 2、1 and 2、1 < (2==2)、1 < 2 == 2 What to output respectively ?

  2. What is the result of running the following code ? 




  1. use 4、9、2、7 Four Numbers , have access to +、-、*、 and /, Use each number once , Let the result of the expression be 24, What is an expression ?

  2. any() and all() What's the difference ?

  3. Python What elements in it are false ?

  4. stay Python Is there a ternary operator in “?:” ?

  5. How to declare multiple variables and assign values ?

  6. Do the following procedures report errors ?




  1. a = (1,),b = (1),c = (“1”) What types of data are they ?

  2. Use for Loop through the list separately 、 Tuples 、 Dictionaries and collections .

  3. 99 What's the octal representation of ?

  4. Please write decimal to binary 、 octal 、 Hexadecimal program .

  5. If you like to program, you can add some Q Group 867067945, You can get free learning materials when you join the group ! The group is full of friends who want to learn programming


本文为[Autumn maple]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database