chanduthedev
4 min readSep 12, 2023

Python interview Questions for beginners!!

  1. What is lambda function?
  • Lambda functions are also called anonymous functions
  • Anonymous functions are functions with no name
  • Lambda functions should be in one line, it won’t support multiple lines
python lambda function example

2. What are list, set, tuple and dictionary in python?

List, set, tuple and map are python in-built data types.

  • List:
    -
    List are used to store multiple items in one variable.
    - List is mutable which means list can be changeable.
    - Items can be accessed using index
    - Items can add, delete from the list
    - Duplicate elements are allowed in the list
    - List is represented with square brackets([“sample”, “list”])
  • Set:
    -
    Set is same as list, but only difference is set wont allow duplicate elements
    - Set also can be editable/modifiable like List
    - Set is represented with curly braces({“sample”, “set”})
  • Tuple:
    - Tuple also used to store multiple values in one variable
    -
    Tuple is immutable(not editable/modifiable)
    - Items can be accessed using index similar to List/Set
    - Tuple is represented with braces((“sample”, “tuple”))
  • Dictionary:
    - Dictionary used to store key:value kind of data
    - This is also mutable datatype(can edit/modify data)
    - Dictionary will not allow duplicate keys, duplicate values allowed
    - Dictionary represented in curly braces like set({“key1”:”value”}).

— Except Tuple, remaining three data types list, set and dictionary are editable,

3. What is slicing in python?

- Slicing is retrieving/extracting part from the object like string/list/tuple.
- Slicing allows to access range of items by specifying start, end and step values.
- Example:
- “chanduthedev”[1:8:2], where 1 is starting index, 8 is end index and 2 is step value.
- Extracted string is ‘hnuh’, Because, starting from index 1, add 2 to to the index and it reaches 8.
- Using slicing we can easily reverse a string like “chanduthedev”[::-1]

4. What is the difference between str[:-1] vs str[-1]?
- str[:-1] is to delete last element in the list
- str[-1] is to get last element in the list
- Above two statements using concept called slicing in python

5. How to reverse a string in python?
- To reverse a string in python is very easy
- By using slicing, easily reverse the string
- Assume str is a string, str[::-1] will reverse the string.

6. What are mutable and immutable objects in Python?
- String, Tuple and Numbers are immutable objects, which are non-editable
- List, Set and Dictionary are mutable objects, which are editable

7. Why do we need immutable objects?
- Immutability is very helpful when passing objects as function parameters
- Immutability will not allow to change the original value of the parameter
- Immutability will help when object shared between multiple processes
- One of the use case is, if username and password is passing between functions, these should be immutable and no one will allow to change these values if they set as immutable

8. What is the difference between List and tuple?
- List and tuple works almost same. only difference is List is editable and tuple is not editable.

9. What is list comprehension?
- List comprehension is a shortest way to create new list
- List comprehension is pythonic way of iterating over list of elements and creating a new list.

Python list comprehension syntax and example
Python list comprehension syntax and example

10. What is enumerate in python?
- enumerate() is python built-in function that allows to keep track of iteration number from the iterators(list, set etc.)
- enumerate() object contains two items, one is counter always start with zero and second one is value from the iterator at the location of the counter
- While iterating over the list/set, instead of using range(len(iterator)), its advisable to use enumerate()

Python built-in function enumerate() syntax and example
Python built-in function enumerate() syntax and example

11. What is zip in python?
- zip() function will combine/zip items from the multiple list and make a iterator of tuple of corresponding position items from the given lists
- zip() returns zip object of iterators, we need to manually convert into list/set iterator
- zip() function is very useful when multiple lists of equal length want to iterate at once

zip() python built-in function example
zip() python built-in function syntax and example

No responses yet