Basics Python interview question
Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991.
Key features of Python include its clear and concise syntax, dynamic typing, automatic memory management, extensive standard library, and support for multiple programming paradigms like procedural, object-oriented, and functional programming.
Python is interpreted, which means the Python code is executed line by line, rather than being compiled into machine code beforehand. An interpreter reads and executes the code directly, which allows for easier debugging and portability.
Python 2 and Python 3 are two major versions of Python. Python 3 introduced some backward-incompatible changes, including improved Unicode support, print function, integer division, and syntax enhancements. Python 2 reached its end of life on January 1, 2020, and Python 3 is the recommended version for new projects.
In Python, comments are created using the '#' symbol. Anything after the '#' on the same line is considered a comment and is ignored by the Python interpreter.
PEP 8 is the Python Enhancement Proposal that provides guidelines for writing clean and readable Python code. Following PEP 8 is essential to maintain a consistent coding style and improve code readability for yourself and other developers.
In Python, variables are declared without explicit type declarations. You can assign a value to a variable directly, and Python will infer its type. For example:
makefile
Copy code
x = 10 # x is an integer name = "John" # name is a string
Python supports various data types, including int (integer), float (floating-point number), str (string), bool (boolean), list, tuple, set, dictionary, and more.
You can use built-in functions like int(), float(), str(), list(), tuple(), set(), dict(), etc., to convert one data type to another.
Python uses indentation (whitespace at the beginning of lines) to define blocks of code, such as loops and functions. Proper indentation is crucial in Python and is used instead of curly braces or similar constructs found in other languages.
Recommended by LinkedIn
In Python, you can use a try-except block to catch and handle exceptions. Code that might raise an exception is placed inside the try block, and the corresponding exception-handling code is placed in the except block.
Lists, tuples, and dictionaries are Python data structures. Lists are ordered, mutable collections of elements. Tuples are similar to lists but immutable. Dictionaries are unordered collections of key-value pairs.
You can access elements in a list using their index (e.g., my_list[0]). For dictionaries, you can access values using keys (e.g., my_dict['key']).
A Python function is a block of reusable code that performs a specific task. You define a function using the def keyword, followed by the function name, parameters (if any), and a block of code within indentation.
The '==' operator is used to check if two variables have the same value, while the 'is' operator is used to check if two variables refer to the same object in memory.
You can import modules in Python using the import statement, followed by the module name. For example, import math.
When you use import module, you need to prefix the functions or classes from that module with the module name (e.g., module.function()). With from module import *, you can directly use functions or classes without the prefix.
You can use the open() function to open a file, read its contents using read() or write to it using write() methods, and finally close the file using close().
The range() function generates a sequence of numbers within a specified range. It can be used in for loops or to create lists of numbers.
Python provides various types of loops, such as the for loop and the while loop. The for loop is used to iterate over elements in a sequence, and the while loop continues executing as long as a condition is true.