Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the most common Python interview questions?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the most common Python interview questions". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the most common Python interview questions.

What are the characteristics and advantages of Q 1:Python?

As an entry-level programming language, Python has the following main features and advantages:

Can be explained

Have dynamic characteristics

object-oriented

Concise and simple

Open source

Have strong community support

Q2: what is the difference between a deep copy and a shallow copy?

A: deep copy is copying one object to another, which means that if you make a change to a copy of an object, it will not affect the original object. In Python, we use the function deepcopy () to perform a deep copy and import the module copy, as follows:

> import copy > b=copy.deepcopy (a)

A shallow copy copies a reference from one object to another, so if we change it in the copy, it will affect the original object. We use the function function () to perform a shallow copy, using the following:

> > b=copy.copy (a)

Q 3. What is the difference between lists and tuples?

A: the main difference between the two is that lists are mutable and tuples are immutable. For example, it looks like this:

> mylist= [1 most recent call last 3] > mylist [1] = 2 > > mytuple= > > mytuple [1] = 2Traceback (most recent call last): File ", line 1, in mytuple [1] = 2

The following error occurs:

TypeError: 'tuple' object does not support item assignment

For more information about lists and tuples, you can see here:

Https://data-flair.training/blogs/python-tuples-vs-lists/

Q4 to Q20 are all basic Python interview questions for beginners, but experienced people can also look at these questions and review the basic concepts. Q 4. Explain the ternary operator in Python

Unlike Clover, we don't have?: in Python, but we do have this:

[on true] if [expression] else [on false]

If the expression is True, the statement in [on true] is executed. Otherwise, execute the statement in [on false].

Here is how to use it:

> min=an if a > min

Running result:

2 > > print ("Hi") if a > > import copy > help (copy.copy)

The running result is:

Help on function copy in module copy:copy (x) Shallow copy operation on arbitrary Python objects.See the module's _ doc__ string for more info.

The Dir () function is also a Python built-in function. When the dir () function takes no parameters, it returns a list of variables, methods, and defined types in the current scope; when with parameters, it returns a list of properties and methods of parameters.

The following example shows how to use dir:

> dir (copy.copy)

The running result is:

['_ _ annotations__','_ _ call__','_ _ class__','_ _ closure__','_ _ code__','_ _ defaults__','_ _ delattr__','_ _ dict__','_ _ dir__','_ _ doc__','_ eq__','_ format__','_ _ ge__','_ _ get__' '_ _ getattribute__',' _ _ globals__','_ _ gt__','_ _ hash__','_ _ init__','_ _ init_subclass__','_ _ kwdefaults__','_ _ le__','_ _ lt__','_ _ module__','_ name__','_ ne__','_ new__','_ _ qualname__' '_ _ reduce__',' _ _ reduce_ex__','_ _ repr__','_ _ setattr__','_ _ sizeof__','_ _ str__','_ subclasshook__'] Q 10. When exiting Python, do you want to free up all memory?

The answer is No. Modules that circularly reference other objects or objects referenced from the global namespace are not completely released when Python exits.

In addition, the memory reserved by the C library will not be released.

Q 11. What is a monkey patch?

Dynamically modify a class or module during operation.

> class A: def func (self): print ("Hi") > def monkey (self): print "Hi, monkey" > m.A.func = monkey > a = m.A () > > a.func ()

The running result is:

What is the dictionary in Hi, MonkeyQ 12. Python?

A dictionary is something that is not found in programming languages such as C++ and Java. It has key-value pairs.

> roots= {25 roots 5, 16 14, 9 3, 4 4, 1 1} > > type (roots) > > roots [9]

The running result is:

three

The dictionary is immutable, and we can also create it with a deduction.

> roots= {Xerox for x in range (550mm)} > roots

Running result:

{25: 5, 16: 4, 9: 3, 4: 2, 1: 1} Q 13. Please explain the meaning of using args and * kwargs

When we don't know how many arguments to pass to a function, for example, we pass a list or tuple to, we use * args.

> def func (* args): for i in args: print (I) > > func (3, 2, 1, 1, 4, 7)

The running result is:

32147

Use * * kwargs to collect keyword parameters when we don't know how many keyword parameters to pass.

> def func (* * kwargs): for i in kwargs: print (iMagnekwargs [I]) > func (axiom 1, Benz2, and 7)

The running result is:

A.1b.2c.7Q 14. Please write a Python logic and calculate the number of uppercase letters in a file > import os > os.chdir ('C:\\ Users\\ lifei\\ Desktop') > with open ('Today.txt') as today: count=0 for i in today.read (): if i.isupper (): count+=1print (count)

Running result:

26Q 15. What is a negative index?

Let's first create a list like this:

> mylist= >

A negative index is different from a positive index in that it starts from the right.

> mylist [- 3]

Running result:

six

It can also be used for slices in the list:

> mylist [- 6RV Murray 1]

Results:

[3, 4, 5, 6, 7] Q 16. How to disrupt the elements of a list by operating in place?

To do this, we import the shuffle () function from the random module.

> from random import shuffle > shuffle (mylist) > mylist

Running result:

[3, 4, 8, 0, 5, 7, 6, 2, 1] Q 17. Explain the join () and split () functions in Python

Join () allows us to add the specified character to the string.

>', '.join (' 12345')

Running result:

'1pyrrine, 2pyrrine, 3pyrrine, 4pr 5'

Split () allows us to split a string with a specified character.

>

Running result:

['1x, '2mm,' 3mm, '4pm,' 5'] Q 18. Is Python case sensitive?

If you can distinguish between identifiers such as myname and Myname, it is case-sensitive. Which means it cares about uppercase and lowercase. We can try it with Python:

> myname='Ayushi' > MynameTraceback (most recent call last): File "", line 1, in

Running result:

MynameNameError: name' Myname' is not defined

As you can see, NameError appears here, so Python is case-sensitive.

Q 19. How long can identifiers in Python be?

In Python, identifiers can be of any length. In addition, we must follow the following rules when naming identifiers:

You can only underscore or start with a letter in A-Z/a-z

You can use A-Z/a-z/0-9 for the rest.

Case sensitive

Keywords cannot be used as identifiers, and the following keywords are common in Python:

Q 20. How do I remove leading spaces from a string?

The leading space in a string is the space that appears before the first non-space character in the string. We use the method Istrip () to remove it from the string.

> 'Ayushi' .lstrip ()

Results:

'Ayushi'

As you can see, the string has both leading and suffix characters, and a call to Istrip () removes leading spaces. If we want to remove suffix spaces, we use the rstrip () method.

> 'Ayushi' .rstrip ()

Results:

Ayushi' Thank you for your reading, the above is the content of "what are the most common Python interview questions". After the study of this article, I believe you have a deeper understanding of what the most common Python interview questions are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report