1. What are the Data Structures?
Data Structures allow us to store and organize data efficiently. This will allow us to easily access and perform operations on the data.
In Python, there are four built-in data structures
- List
- Tuple
- Set
- Dictionary
2. What is a list?
The List is the most versatile python data structure that holds an ordered sequence of items.
Creating a List
A List can be created by enclosing elements within [square] brackets where each item is separated by a comma.
Code
Output
3. Explain a few List Methods?
Append
Adds an element to the end of the list.
Syntax
Code
Output
Extend
Adds all the elements of a sequence to the end of the list.
Syntax
Code
Output
Insert
Inserts the given element at the specified index.
Syntax
Code
Output
Pop
Removes the last item from the list.
Syntax
Code
Output
Remove
Removes the first matching element from the list.
Syntax
Code
Output
Clear
Removes all the items from the list.
Syntax
Code
Output
Index
Returns the index of the first occurrence of the specified item in the list.
Syntax
Code
Output
Count
Returns the number of elements with the specified value.
Syntax
Code
Output
Sort
Sorts the items of a list in ascending ascending order. The
Syntax
Code
Output
Sorted
Sorts the items of a list in ascending ascending order. The
Syntax
Code
Output
Code
Output
4. What is the difference between append() and extend()?
append | extend |
---|---|
.append() takes a single element as an argument | .extend() takes an iterable as an argument (list, tuple, dictionaries, sets, strings) |
.append() adds a single element to the end of the list | .extend() can add multiple individual elements to the end of the list |
5. How to use the split() method?
The
Syntax:
Example
Code
Output
If no separator is specified, the default separator is whitespace.
Using separator
Example
Code
Output
6. How to use the join() method?
The
Syntax:
Example
Code
Output
7. What is List Slicing?
Obtaining a part of a list is called List Slicing.
Syntax:
- end_indexis not included in the slice.
Code
Output
Extended Slicing
Similar to string extended slicing, we can extract alternate items from the list using the step.
Syntax:
Code
Output
8. What is Negative Indexing?
Using a negative index returns the nth item from the end of the list.
The last item in the list can be accessed with the index
Accessing List Items with Negative Index
Example-1
Code
Output
Example-2
Code
Output
Slicing With Negative Index
You can also specify negative indices while slicing a List.
Code
Output
Out of Bounds Index
While slicing, the index can go beyond the size of the list.
Code
Output
9. How to reverse a List?
Reversing a list using
The
Code
Output
Reversing a list using list slicing:
A list can be reversed using extended slicing.
Syntax:
Code
Output
10. Explain about functions in Python?
A function is a block of reusable code to perform a specific action. Functions help us in using existing code without writing it every time we need it.
A function can be defined using a keyword
Code
Output
Function Arguments
We can pass values to a function using Argument.
Code
Input
Output
Providing default values
Default values indicate that the function argument will take that value if no argument value is passed during the function call.
Example
Code
Input
Output
11. What is Recursion?
A function calling itself is called Recursion
Let's understand recursion with a simple example of multiplying N numbers