INTERVIEW QUESTIONS
1) What is Python?
Ans- 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. Python supports multiple programming paradigms, including procedural,
object-oriented, and functional programming. It has a large and active community
that contributes to its extensive library ecosystem, making it suitable for various
applications such as web development, data analysis, machine learning, artificial
intelligence, automation, scientific computing, and more. Python's syntax is
designed to be clear and concise, emphasizing readability and reducing the cost of
program maintenance.
2) Explain the print, Id, input, type ?
Ans- 1. **print()**: The `print()` function in Python is used to display the
specified message or value on the screen (stdout). It takes one or more arguments
and prints them to the standard output device (usually the console). For example:
```python
print("Hello, world!")
```
This will output:
```
Hello, world!
```
2. **id()**: The `id()` function in Python returns the identity (memory address) of
an object. It takes an object as an argument and returns a unique identifier for
that object. For example:
```python
x = 5
print(id(x))
```
This will output a memory address where the integer value `5` is stored.
3. **input()**: The `input()` function in Python is used to prompt the user to
enter a value from the keyboard. It takes an optional prompt message as an argument
and returns the user's input as a string. For example:
```python
name = input("Enter your name: ")
print("Hello,", name)
```
This will prompt the user to enter their name, and then it will greet them
using the entered name.
4. **type()**: The `type()` function in Python is used to get the type of an
object. It takes one argument and returns the type of that object. For example:
```python
x = 5
print(type(x))
```
This will output:
```
<class 'int'>
```
indicating that `x` is of type `int`.
3) What is type of casting?
Ans- Type casting, also known as type conversion, is the process of converting one
data type into another. In Python, type casting can be done using various built-in
functions to convert data from one type to another.
These functions enable you to convert data from one type to another, which is
useful for various programming tasks such as data manipulation, input/output
handling, and more.
4) What is identifiers and rules?
Ans- In Python, identifiers are names used to identify variables, functions,
classes, modules, or any other objects. Identifiers are case-sensitive and can
consist of letters (both lowercase and uppercase), digits, and underscores.
However, they must follow certain rules:
1. **Valid Characters**: Identifiers can include letters (both uppercase and
lowercase), digits (0-9), and underscores (_), but they cannot start with a digit.
2. **Case-Sensitivity**: Python is case-sensitive, meaning uppercase and lowercase
letters are considered different. For example, `my_variable` and `My_Variable` are
different identifiers.
3. **Reserved Keywords**: Identifiers cannot be the same as Python's reserved
keywords, which are words reserved for specific purposes within Python. For
example, you cannot use words like `if`, `else`, `for`, `while`, `class`, `def`,
`import`, etc., as identifiers.
4. **Length**: Identifiers can be of any length, but it's recommended to keep them
meaningful and concise for better code readability.
5. **Convention**: While not enforced by the Python interpreter, there are
conventions for naming identifiers to make code more readable:
- Use lowercase letters for variable names and function names.
- Use uppercase letters for constants (values that don't change during program
execution).
- Use underscores to separate words in multi-word identifiers (e.g.,
`my_variable`, `function_name`).
Here are some examples of valid identifiers:
- `variable_name`
- `myVariable`
- `function_name`
- `Class_Name`
- `_variable`
- `_private_variable`
- `CONSTANT_VALUE`
5) Can we use keywords as an identifiers ? if no ? why
Ans- No, in Python, you cannot use keywords as identifiers. Keywords are reserved
for specific purposes within the language and cannot be used as variable names,
function names, or any other identifiers.
If you try to use a keyword as an identifier, Python will raise a syntax error
because it expects the keyword to be used for its intended purpose rather than as a
user-defined name.
For example, consider the keyword `if`. It's used for conditional statements in
Python:
```python
if condition:
# do something
```
If you try to use `if` as a variable name:
```python
if = 10 # This will raise a syntax error
```
Python will raise a syntax error because `if` is a reserved keyword and cannot be
used as an identifier.
6) How many keyword in python ?
Ans- In Python there are 35 keywords in Python. These keywords are reserved for
specific purposes and cannot be used as identifiers (variable names, function
names, etc.) in Python code. Here is the list of keywords:
```plaintext
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
7) What is indexing?
Ans- Indexing in Python refers to the process of accessing individual elements
within a data structure, such as a list, tuple, string, or any other sequence type.
Each element in a sequence has a unique position called an index. Indexing allows
you to retrieve specific elements from the sequence using their position.
In Python, indexing is zero-based, which means the first element in the sequence
has an index of 0, the second element has an index of 1, and so on. Negative
indexing is also supported, where -1 represents the last element, -2 represents the
second last element, and so forth.
Here's how indexing works in Python:
1. **Accessing Elements by Positive Index**: You can access elements by specifying
their positive index within square brackets (`[]`). For example:
```python
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10 (first element)
print(my_list[2]) # Output: 30 (third element)
```
8) What is slicing?
Ans- In Python, slicing refers to the process of extracting a portion of a sequence
(such as a list, tuple, or string) by specifying a range of indices. It allows you
to create a new sequence containing elements from the original sequence within the
specified range.
The syntax for slicing is as follows:
```python
sequence[startindex:endindex:stepvalue]
```
- `start`: The index representing the start of the slice (inclusive). If omitted,
slicing starts from the beginning of the sequence.
- `end`: The index representing the end of the slice (exclusive). If omitted,
slicing continues to the end of the sequence.
- `step` (optional): The step or increment for the slice. It specifies the stride
of the slice, i.e., how many elements to skip. If omitted, the default value is 1.
9) What is str and what are methods of str?
Ans- In Python, `str` is a built-in data type used to represent strings, which are
sequences of characters enclosed within single quotes (`'`) or double quotes (`"`).
Strings are immutable, meaning once created, their contents cannot be changed.
Here are some examples of string literals:
```python
string1 = 'Hello, world!'
string2 = "Python is awesome"
Python's `str` class provides various methods for working with strings. Some common
methods of the `str` class include:
- `lower()`: Converts the string to lowercase.
- `upper()`: Converts the string to uppercase.
- `capitalize()`: Capitalizes the first character of the string.
- `title()`: Capitalizes the first character of each word in the string.
- `replace()`: Replaces occurrences of a substring with another substring.
- `index()`, `rindex()`: Like `find()`, but raise an error if the substring is
not found.
- `count()`: Counts the occurrences of a substring within the string.
- `isalpha()`, `isdigit()`, `isalnum()`, `isspace()`: Checks if the string
contains only alphabetic characters, digits, alphanumeric characters, or whitespace
characters, respectively.
- `islower()`, `isupper()`, `istitle()`: Checks if the string is in lowercase,
uppercase, or title case, respectively.