Python Interview Questions Part 3
Here you can find the most frequently asked Python Interview Questions (Part 3) by many multi-national companies.
21) How can you copy an object in Python?
To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.
22) What is negative index in Python?
Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index, and so forth. For negative index, (-1) is the last index, and (-2) is the second last index and so forth.
23) How you can convert a number to a string?
In order to convert a number into a string, use the inbuilt function str(). If you want an octal or hexadecimal representation, use the inbuilt function oct() or hex().
24) What is the difference between Xrange and range?
Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.
25) What is module and package in Python?
In Python, the module is the way to structure the program. Each Python program file is a module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules. A package can have modules or subfolders.
26) Mention what are the rules for local and global variables in Python?
Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.
Global variables: Those variables that are only referenced inside a function are implicitly global.
27) How can you share global variables across modules
To share global variables across modules within a single program, create a special module. Import the config module in all modules of your application. The module will be available as a global variable across modules.
28) Explain how can you make a Python Script executable on Unix?
To make a Python Script executable on Unix, you need to do two things, Script file’s mode must be executable and the first line must begin with # ( #!/usr/local/bin/python)
29) Explain how to delete a file in Python?
By using a command os.remove (filename) or os.unlink(filename)
30) Explain how can you generate random numbers in Python?
To generate random numbers in Python, you need to import command as import random.random() This returns a random floating-point number in the range [0,1).
This article contains the most frequently asked Python Interview Questions (Part 3) by many multi-national companies. Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.