Python Interview Questions 2

1. What is Python?
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

2. What is the purpose of PYTHONPATH environment variable?
PYTHONPATH - It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

3. What is the purpose of PYTHONSTARTUP environment variable?
PYTHONSTARTUP - It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.

4. Is python a case sensitive language?
Yes! Python is a case sensitive programming language.

5. What are the supported data types in Python?

Python has five standard data types −
Numbers
String
List
Tuple
Dictionary

6. What are tuples in Python?
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

7. What is the difference between tuples and lists in Python?
The main differences between lists and tuples are − Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

8.What are Python's dictionaries?
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

9.How will you create a dictionary in python?
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

10.How will you get all the keys from the dictionary?
Using dictionary.keys() function, we can get all the keys from the dictionary object.
print dict.keys()   # Prints all the keys

11.How will you get all the values from the dictionary?
Using dictionary.values() function, we can get all the values from the dictionary object.
print dict.values()   # Prints all the values

12. How will you convert a string to an int in python?
int(x [,base]) - Converts x to an integer. base specifies the base if x is a string.

13.How will you convert a string to a long in python?
long(x [,base] ) - Converts x to a long integer. base specifies the base if x is a string.

14.How will you convert a string to a float in python?
float(x) − Converts x to a floating-point number.

15.How will you convert a object to a string in python?
str(x) − Converts object x to a string representation.

16.How will you convert a object to a regular expression in python?
repr(x) − Converts object x to an expression string.

17.How will you convert a String to an object in python?
eval(str) − Evaluates a string and returns an object.

18.How will you convert a string to a tuple in python?
tuple(s) − Converts s to a tuple.

19.How will you convert a string to a list in python?
list(s) − Converts s to a list.

20.How will you convert a string to a set in python?
set(s) − Converts s to a set.

No comments:

Post a Comment