python tutorial - Python interview questions and answers for experienced - learn python - python programming
python interview questions :121
Print in terminal with colors using Python?
Learn python - python tutorial - terminal - python examples - python programs
python interview questions :122
How to get line count cheaply in Python?
You need to get a line count of a large file (hundreds of thousands of lines) in python.
python interview questions :123
What's the list comprehension?
Which one has higher precedence in Python? - NOT, AND , OR
- AND is higher precedence than OR, AND will be evaluated first, "True" will be printed out.
- Then, how about this one?
- NOT has first precedence, then AND, then OR.
Learn python - python tutorial - precedence - python examples - python programs
One line, probably pretty fast:
python interview questions :125
What is the use of enumerate() in Python?
- Using enumerate() function you can iterate through the sequence and retrieve the index position and its corresponding value at the same time.
python interview questions :126
How do you perform pattern matching in Python? Explain
- Regular Expressions/REs/ regexes enable us to specify expressions that can match specific “parts” of a given string. For instance, we can define a regular expression to match a single character or a digit, a telephone number, or an email address, etc.
- The Python’s “re” module provides regular expression patterns and was introduce from later versions of Python 2.5. “re” module is providing methods for search text strings, or replacing text strings along with methods for splitting text strings based on the pattern defined.
python interview questions :127
What is a Class? How do you create it in Python?
- A class is a blue print/ template of code /collection of objects that has same set of attributes and behaviour.
- To create a class use the keyword class followed by class name beginning with an uppercase letter. For example, a person belongs to class called Person class and can have the attributes (say first-name and last-name) and behaviours / methods (say showFullName()). A Person class can be defined as:
Note: whenever you define a method inside a class, the first argument to the method must be self (where self - is a pointer to the class instance). self must be passed as an argument to the method, though the method does not take any arguments.
python interview questions :128
What are Exception Handling? How do you achieve it in Python?
- Exception Handling prevents the codes and scripts from breaking on receipt of an error at run -time might be at the time doing I/O, due to syntax errors, data types doesn’t match. Generally it can be used for handling user inputs.
- The keywords that are used to handle exceptions in Python are:
- try - it will try to execute the code that belongs to it. May be it used anywhere that keyboard input is required.
- except - catches all errors or can catch a specific error.
python interview questions :129
What are Accessors, mutators, @property?
- Accessors and mutators are often called getters and setters in languages like “Java”. For example, if x is a property of a user-defined class, then the class would have methods called setX() and getX().
- Python has an @property “decorator” that allows you to ad getters and setters in order to access the attribute of the class.
python interview questions :130
Differentiate between .py and .pyc files?
- Both .py and .pyc files holds the byte code. “.pyc” is a compiled version of Python file.
- This file is automatically generated by Python to improve performance.