Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

01 Lists in Python Class

Uploaded by

Nikhilesh Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

01 Lists in Python Class

Uploaded by

Nikhilesh Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python In Hindi List-01

Lists in Python
❖ Introduction to Lists
❖ Creating a List
❖ list() function
❖ Nested Lists The CS Pundit
Lists in Python
1. Introduction to Lists
2. Creating and Accessing List Elements
3. Understanding List Mutability
4. List Operations: Concatenation, Repetition, Membership, Slicing
5. Traversing a List
6. List Methods: append, clear, copy, count, extend, index, insert, pop, remove, reverse, sort
7. Built-In Functions for Lists: list(), len(), sorted(), min(), max(), sum(), any(), all(), enumerate()
8. Working with Nested Lists
9. Copying and Comparing Lists
10. Using Lists as Function Arguments
11. List Manipulation and Programming: This includes both manipulating lists and creating
list-based programs.
12. And More : This is not an exhaustive list, and there are many more aspects of Python lists to explore
and understand.
Introduction to Lists in Python
Python lists are a built-in data type that can store a sequence of items. Each item, or element, in a list can be of any data
type, such as integers, floats, strings, tuples, or even other lists. This versatility makes lists a powerful tool for grouping
together a mix of data types.

Key characteristics of Lists in Python include:

1. Order: Lists maintain the order of elements based on their insertion order.
2. Mutability: Unlike strings and tuples, lists can change their elements in place without creating a new list.
3. Duplication: Lists can contain duplicate values.
4. Zero-Based Indexing: List indices start from 0, like string indices.
5. Heterogeneity: Lists can store objects of different types.
6. Dynamism: Lists can grow or shrink dynamically, supporting the addition, insertion, and removal of elements.
7. Nesting: Lists can contain other lists, allowing for complex data structures like lists of lists.
8. Traversal: Lists support iteration, allowing traversal using a loop to perform operations on each element.

In Python, Lists, Sets, and Dictionaries are known as collection data types, while Lists, Tuples, and Strings are sequence
data types. Notably, lists are unique as they fall into both categories. They are often referred to as containers or collections
because they can hold a variable number of objects. These objects are enclosed in square brackets `[]` and separated by
commas `,`, making lists both a collection and a sequence, and thus particularly versatile.
Creating a List
Creating an Empty List
An empty list is a list that contains no elements. This is useful when you need a list, but you don't have any
elements to put in it yet.

empty_list = []

Creating a List with Items

The simplest way to create a list is to place the items (elements) inside square brackets [], separated by
commas.

b= ['NCERT', 'CBSE']
print(b) #Output : ['NCERT', 'CBSE']
Creating a List with Different Types of Items
A list in Python can contain items of different types. For example, a list can contain a mix of integers,
strings, floats, and booleans.
>>>['Class', 11]
list() function in python
We can create a Python list by using list() function. Below are the ways by which we can use list() function
in Python:

● To create a list from a string


● To create a list from a tuple
● To create a list from set and dictionary
● To create a list using range() function
● Taking user input as a list

Creating a List from a String


The `list()` function can be used to create a list from a string. Each character in the string becomes a separate
item in the list.

s='List in Python'
char_list=list(s)
char_list
['L', 'i', 's', 't', ' ', 'i', 'n', ' ', 'P', 'y', 't', 'h', 'o', 'n']
list() function in python
Creating a List from a Tuple
The `list()` function can also be used to create a list from a tuple.
>>>t=('computer science','information practices')
>>>list(t)
['computer science', 'information practices']

Creating a List from a Set


The `list()` function can also be used to create a list from a set.
>>>set1={'cs','ip'}
>>>list(set1)
['cs', 'ip']

Creating a List from a Dictionary


The `list()` function can also be used to create a list from a set.
dict1={'board': 'cbse','class': 11, 'chapter': 9}
list(dict1)
['board', 'class', 'chapter']
list() function in python
Creating List using Range Function

You can create a list from a range using the range() function combined with the list() function.

>>>list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>list(range(10,13))
[10, 11, 12]

Create List from user input


list1 = list(input("Please Enter List Elements: "))
Please Enter List Elements: Python Lists
list1
['P', 'y', 't', 'h', 'o', 'n', ' ', 'L', 'i', 's', 't', 's']
Creating Nested List - lists in lists
Creating a Nested List
A list can contain other lists. This is known as a nested list. You can use it to represent complex structures.

2D list in python
>>>list2D = ['python', ['class', 11], ['class',12]]
>>>list2D
['python', ['class', 11], ['class', 12]]

3D list in python
>>>list3D=['python',['class 11',['cs','chapter 9'],['ip','chapter 4 ']]]
>>>list3D
['python', ['class 11', ['cs', 'chapter 9'], ['ip', 'chapter 4 ']]]
Join Telegram Channel For Free PDFs & PPTs
Telegram Channel : https://t.me/TheCSPundit
Youtube Channel : @TheCSPundit

You might also like