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 interview questions for Python?

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

Share

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

This article mainly introduces "what are the most common interview questions in Python". In daily operation, I believe many people have doubts about the most common interview questions in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the most common interview questions in Python?" Next, please follow the editor to study!

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

Of course, in fact, the advantages of Python are much more than that.

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) to copy the code

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 Traceback 3] > mylist [1] = 2 > > mytuple= > mytuple [1] = 2 Traceback (most recent call last): File ", line 1, in mytuple [1] = 2

The following error occurs:

TypeError: 'tuple' object does not support item assignment

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:

Hi, Monkey

Q 12. What is the dictionary in 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:

3 2 1 4 7

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.1 b.2 c.7

Q 14. Please write a Python logic to 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+=1 print (count)

Running result:

twenty-six

Q 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, 4pence5'

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

>

Running result:

['1percent,' 2percent, '3percent,' 4percent,'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' > Myname Traceback (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:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

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?

Leading spaces in a string are spaces that appear in front of * * non-space characters in a 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'

Q 21 to Q 35 are advanced Python interview questions for those with Python experience.

Q 21. How to convert a string to lowercase?

We use the lower () method.

> > 'AyuShi'.lower ()

Results:

'ayushi'

You can convert it to uppercase using the upper () method.

> > 'AyuShi'.upper ()

Results:

'AYUSHI'

In addition, use the isupper () and islower () methods to check whether the character spring is all uppercase or lowercase.

> 'AyuShi'.isupper () False >' AYUSHI'.isupper () True > 'ayushi'.islower () True >' @ yu$hi'.islower () True >'@ YU$HI'.isupper () True

Then, characters like @ and $satisfy both uppercase and lowercase.

Istitle () can tell us whether a string is in title format.

> 'The Corpse Bride'.istitle () True

Q 22. What is the pass statement in Python?

When writing code in Python, sometimes you may not have figured out how to write the function, but only write the function declaration, but in order to ensure that the syntax is correct, you must enter something, in which case, we will use the pass statement.

Def func (* args): pass >

Similarly, the break statement allows us to jump out of the loop.

> for i in range (7): if iTunes 3: break print (I)

Results:

0 1 2

The continue statement allows us to skip to the next loop.

> for i in range (7): if iTunes 3: continue print (I)

Results:

0 1 2 4 5 6

Q 23. What is the closure in Python?

When a nested function references a value in its outer region, the nested function is a closure. The meaning is that this value will be recorded.

> def A (x): def B (): print (x) return B > A (7) ()

Results:

seven

Q 24. Explain the / /,% and * * operators in Python

The / / operator performs floor division (integer division down), which returns the integer portion of the division result. >

Divisible here returns 3.5.

Similarly, the exponentiation operation is performed. Ab returns a to the power of b.

> 2. 10 1024

* *,% performs modular operation and returns the remainder of division.

> 13% 7 6 > 3.5% 1.5 0.5

Q 25. How many operators are there in Python? Explain the arithmetic operator.

In Python, we have seven operators: arithmetic operator, relational operator, assignment operator, logical operator, bit operator, member operator, and identity operator.

We have seven arithmetic operators that allow us to perform arithmetic operations on numeric values:

1. Plus sign (+), which adds two values

> 7: 8 / 15

two。 Minus sign (-), minus * * values minus the second value

7-8-1

3. Multiplication sign (*), which multiplies two values

> 7: 8 / 56

4. Divide the sign (/) by dividing the second value by * values

> 7 >

5. Take divisible, modular, and exponentiated operations downward, see the previous question.

Q 26. Explain the relational operators in Python

The relational operator is used to compare two values.

1. The less than sign (> > 'hi') returns True if the value on the left is large.

> > 1.1 > 2.2 > 3.3 True

3. The less than or equal sign (> > 3.0 =) returns True if the value on the left is greater than or equal to the value on the right.

> True > = False True

Equals sign (= =), or returns True if the values on both sides of the symbol are equal.

> {1pm 3pm 2pm 2} = = {1pm 2pm 3} True

Does not equal the sign (! =), returns True if the values on both sides of the symbol are not equal.

> trueworthy 0.1 True > false setting 0.1 True

Q 27. Explain the assignment operator in Python

This is an important interview question in the Python interview.

We put all the arithmetic operators and assignment symbols together to show:

> > Amur 7 > Amur 7 > a 8 > Amur 1 > A7 > Amur 2 > a 14 > a Rue 2 > a 7.0 > A > 49 > > a 49 > an apace 3 > > a 16.0 > a% balance 4 > a 0.0.

Q 28. Explain the logical operators in Python

There are three logical operators in Python: and,or,not.

> False and TrueFalse > 7 > > not 2 False

Q 29. Explain the member operator in Python

We can confirm whether one value is a member of another through the member operators' in' and 'not in',.

> 'me' in' disappointment' True > 'us' not in' disappointment' True

Q 30. Explain the identity operator in Python

This is also a question often asked in Python interviews.

We can confirm whether the two values are the same through the identity operators' is' and'is not',.

> 10 is' 10' False > True is not False True

Q 31. Talk about bit operators in Python

The operator operates on values in binary bits.

And (&), bitwise and operator: two values that participate in the operation, if both corresponding bits are 1, the result of that bit is 1, otherwise it is 0

> > 0b110 & 0b010 2

two。 Or (|), bitwise or operator: as long as one of the corresponding two binary bits is 1, the resulting bit is 1.

> 3 | 2 / 3

3. XOR (^), bitwise XOR operator: when two corresponding binary bits are different, the result is 1

> > 3 ^ 2 1

4. Take the inverse (~), take the inverse operator by bit: reverse each binary bit of the data, that is, change 1 to 0 and 0 to 1

> 2-3

5. The left displacement (1) shifts all the binaries on the left side of the Operand to the right, and the number on the right specifies the number of bits to be moved.

> > 4 > > 2 1

Q 32. How to use M-ary numbers in Python?

In Python, we can use binary, octal, and hexadecimal in addition to decimal.

Binary numbers consist of 0 and 1, and we use the 0b or 0B prefix to represent binary numbers.

> > int (0b1010) 10

two。 Use the bin () function to convert a number to its binary form.

> bin (0xf) '0b1111'

3. The octal number consists of the number 0-7, with the prefix 0o or 0O for the octal number.

> oct (8) '0o10'

4. The hexadecimal number consists of the number 0-15, with the prefix 0x or 0X for the hexadecimal number.

> hex (16) '0x10' > hex (15) '0xf'

Q 33. How do I get a list of all the keys in the dictionary?

Use keys () to get all the keys in the dictionary

> mydict= {'axiaqiaqizhul 1dongbaozi 2Magneticallycantilevered VOL3LINGRAPHING 5} > mydict.keys () dict_keys (['ahe,' baked, 'crested,' e'])

Q 34. Why not recommend the underscore as the beginning of the identifier

Because Python does not have the concept of private variables, it is agreed that a variable is declared private at the beginning of a quick underscore. So if you don't want the variable to be private, don't start with an underscore.

Q 35. How to declare multiple variables and assign values?

There are two ways:

> > a=b=c=3 # This assigns 3 to 4, and 5 This assigns 3 to 4, a=b=c=3 # This assigns 3 to, and c

Q 36. What is the de-encapsulation of tuples?

First, let's take a look at de-encapsulation:

> mytuple=3,4,5 > mytuple (3pm 4je 5)

This encapsulates 3Jing 4pr 5 into a tuple mytuple.

Now let's unwrap these values into the variable x _ meno y _ Z:

> x+y+z >

Get the result 12.

At this point, the study of "what are the most common interview questions for Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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