Python | Data Types | List & It's Conversion By. Shivansh Sir

 PYTHON PROGRAMMING

PYTHON | DATA TYPES | LIST

Python के list data type में एक से ज्यादा items होते है | List ये एक mutable data type है | इन data type के items की values change की जा सकती है | Python में 'List' ये data structure होता है | List ये Elements का sequence होता है और mutable(changeable) होता है | Python के list में जो elements होते है उसे 'items' कहते है | List के हर item को comma(,) से separate किया जाता है और पूरे items को square brackets([]) के अन्दर close किया जाता है | List में mixed data types भी इस्तेमाल किये जा सकते है |

Source Code 

list = [1, "Hello", 5.6, (1, "Hii")]

for i in list:

print(i)

Output 

1

Hello

5.6

(1, 'Hii')

PYTHON | DATA TYPES CREATING LIST

List के आखिरी में semi-colon(;) दे या ना दे इससे कोई फर्क नहीं पड़ता है |

list1 = [1, 2, 3, 4, 5] #Integer List

list2 = [1.5, 2.4, 3.8, 4.4, 5.7]; #Float List

list3 = [1, 2, "Hello", 4.5]; #Mixed Data Types List

list4 = [1, 2, [1, 2], 4.5] #Nested List

PYTHON | DATA TYPES LIST POSITIVE INDEXING

हर Programming Language में index की शुरुआत '0' से होती है उसी तरह से Python में List के पहले item का index '0' होता है |

strList = ["H", "e", "l", "l", "o"]


PYTHON | DATA TYPES | LIST NEGATIVE INDEXING
Python में List के आखिरी index '-1' से शुरू होता है |
PYTHON | DATA TYPES ACCESSING LIST
Index से List के items को access किया जा सकता है |
Syntax
list[index]
Source Code
list = [1, 2, "H", 2.8]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
Output
1
2
H
2.8

PYTHON | DATA TYPES | INVALID INDEXING
अगर invalid index दिया जाता है तो 'indexError' का exception आ जाता है |
Source Code
list = [1, 2, "H", 2.8]
print(list[10])
Output
print(list[10])
IndexError: list index out of range

PYTHON | DATA TYPES ACCESSING NEXTED LIST
Source Code
list = [1, 2, "Hello", ["R", "U"]]
print(list[3][0]) 
Output
R
print(list[3][1]) 
Output
U

PYTHON | DATA TYPES | LIST | CONVERSION INTO SUB-LIST & LIST SLICING
Colon(:) के left में sub-List कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है | Slicing के लिए colon को 'slicing operator' कहा जाता है |
Syntax
list[start:end]
Source Code
list = [1, 2, "H", 2.8]
print(list[0:1]) # [1]
print(list[0:2]) # [1, 2]
print(list[1:3]) # [2, 'H', 2.8]
print(list[3:1]) #[]
Output
[1]
[1, 2]
[2, 'H', 2.8]
[]
Note :- अगर colon(:) के left side का index invalid होता है तो blank list ([]) return होती है और colon(:) के right side का index invalid होता है तो List के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank list ([]) return होता है |
Example
list = [1, 2, "H", 2.8]
print(list[0:1]) #Output - [1]
print(list[0:2]) #Output - [1, 2]
print(list[1:3]) #Output - [2, 'H']
print(list[1:15]) #Output - [2, 'H', 2.8]
print(list[10:4]) #Output - []

CHECK LENGTH OF ITEM
Source Code
list = [1, 2, "H", 2.8]
print (len(list))
Output
4

CONCATENATION
Source Code
list = [1, 2, "H", 2.8] + ["Hello", [2, 8]]
print (list)
Output
[1, 2, 'H', 2.8, 'Hello', [2, 8]]

REPETITION
Source Code
list = [1, 2, "H", 2.8] * 3
print (list)
Output
[1, 2, 'H', 2.8, 1, 2, 'H', 2.8, 1, 2, 'H', 2.8]

ITERATING LIST USING FOR LOOP
Source Code
list = [1, 2, "H", 2.8]
for i in list:
    print(i)
Output
1
2
H
2.8

ADD ITEM
List में item(s) को add करने के लिए functions की जरुरत पड़ती है | List में एक item add करने के लिए 'append()' function का इस्तेमाल किया जाता है और एक से ज्यादा items add करने के लिए 'extend()' function का इस्तेमाल किया जाता है |
Source Code
list = [1, 2, "H", 2.8]
list.append("E")
print(list)
Output
[1, 2, 'H', 2.8, 'E']

Source Code
list = ["P", "y", "t"]
list.extend(["h", "o", "n"])
print(list)
Output
['P', 'y', 't', 'h', 'o', 'n']

CHANGE ITEM
Assignment Operator और index की मदद से List की values को change किया जा सकता है |
Source Code
list = ['P', 'y', 't', 'h', 'o', 'n']
list[2] = "p"
list[0] = "C"
print(list)
Output
['C', 'y', 'p', 'h', 'o', 'n']

DELETING ITEMS
List के item को delete करने के लिए 'del' operator का इस्तेमाल किया जाता है |

Source Code
list = ['P', 'y', 't', 'h', 'o', 'n']
del list[2]
del list[0]
print(list)
Output
['y', 'h', 'o', 'n']

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir

Python | Loop | For Loop By. Shivansh Sir