sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » List Data Type

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

Creating List in Python

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

List Positive Indexing

हर Programming Language में index की शुरुआत '0' से होती है उसी तरह से Python में List के पहले item का index '0' होता है |
strList = ["H", "e", "l", "l", "o"]
Python में 'Tuple' ये data structure होता है | Tuple ये Elements का sequence होता है और immutable(unchangeable) होता है |
Python में List और Tuple एक जैसे ही होते है लेकिन List के items को change या उसके items delete किये जा सकते है और Tuple के items को change या उसके items delete नहीं किये जा सकते है | Tuples सिर्फ read किये जा सकते है |
Python के tuple में जो elements होते है उसे 'items' कहते है |
Tuple के हर item को comma(,) से seperate किया जाता है और पूरे items को parenthesis(()) के अन्दर close किया जाता है | Tuple में mixed data types भी इस्तेमाल किये जा सकते है |

Creating Tuple in Python

Tuple के आखिरी में semi-colon(;) दे या ना दे इससे कोई फर्क नहीं पड़ता है |
tuple1 = (1, 2, 3, 4, 5) #Integer Tuple
tuple2 = (1.5, 2.4, 3.8, 4.4, 5.7); #Float Tuple
tuple3 = (1, 2, "Hello", 4.5); #Mixed Data Types Tuple
tuple4 = (1, 2, (1, 2), 4.5) #Nested Tuple

Tuple Without Parenthesis(())

Tuples बिना parenthesis के भी हो सकते है |
tuple = "H", "e", "l", "l", "o"
print(type(tuple))
#Output : <class 'tuple'>

Tuple Positive Indexing

हर Programming Language में index की शुरुआत '0' से होती है उसी तरह से Python में Tuple के पहले item का index '0' होता है |
strTuple = ("H", "e", "l", "l", "o")
strTuple
ItemsNumberitem1item2item3item4item5
Index01234
Items"H""e""l""l""o"

Tuple Negative Indexing

Python में Tuple के आखिरी index '-1' से शुरू होता है |
strTuple = ("H", "e", "l", "l", "o")
strTuple
ItemsNumberitem1item2item3item4item5
Index-5-4-3-2-1
Items"H""e""l""l""o"

Accesing Tuple

index से Tuple के items को access किया जा सकता है |
Syntax :
tuple(index)
Source Code :
tuple = (1, 2, "H", 2.8)
print(tuple[0])
print(tuple[1])
print(tuple[2])
print(tuple[3])
Output :
1
2
H
2.8

Invalid Indexing

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

Accessing Nested Tuple

tuple = (1, 2, "Hello", ("R", "U"))

print(tuple[3][0]) #Output : R
print(tuple[3][1]) #Output : U

Tuples Cannot Change But Reassign

tuple को change नहीं कर सकते लेकिन उसे re-assign किया जा सकता है |
tuple = (1, 2, 3, 4, 5)
print(tuple)
#Output : (1, 2, 3, 4, 5)

tuple = (6, 7, 8, 9, 10)
print(tuple)
#Output : (6, 7, 8, 9, 10)

Deleting Tuple

'del' Operator से पूरा tuple delete किया जा सकता है लेकिन tuple के एक-एक item को delete नहीं किया जा सकता |
tuple = (1, 2, 3, 4, 5)
del tuple[0]
#Output :
#    del tuple[0]
#TypeError: 'tuple' object doesn't support item deletion
tuple = (1, 2, 3, 4, 5)
del tuple
print(tuple)
#Output : <class 'tuple'>

Convert Tuple to Sub-Tuple or Tuple Slicing

colon(:) के left में sub-Tuple कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है |
Slicing के लिए colon को 'slicing operator' कहा जाता है |
Syntax :
tuple(start:end)
Source Code :
tuple = (1, 2, "H", 2.8)
print(tuple[0:1]) # (1)
print(tuple[0:2]) # (1, 2)
print(tuple[1:3]) # (2, 'H', 2.8)
print(tuple[3:1]) #()
Output :
(1)
(1, 2)
(2, 'H', 2.8)
()

अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |
For Example,
tuple = (1, 2, "H", 2.8)
print(tuple[0:1]) # (1)
print(tuple[0:2]) # (1, 2)
print(tuple[1:3]) # (2, 'H')
print(tuple[1:15]) # (2, 'H', 2.8)
print(tuple[10:4]) # ()

Check Length of Tuple's Items

tuple = (1, 2, "H", 2.8)
print (len(tuple))

#Output : 4

Tuple Concatenation

tuple = (1, 2, "H", 2.8) + ("Hello", (2, 8))
print (tuple)

#Output : (1, 2, 'H', 2.8, 'Hello', (2, 8))

Tuple Repetition

tuple = (1, 2, "H", 2.8) * 3
print (tuple)

#Output : (1, 2, 'H', 2.8, 1, 2, 'H', 2.8, 1, 2, 'H', 2.8)

Iterating Tuple Items using For Loop

tuple = (1, 2, "H", 2.8)
for i in tuple:
    print(i)

#Output :
#1
#2
#H
#2.8

Tuple Functions in Python

Tuple FunctionDescription
all()sequence के सभी items True होते है तो ये तो ये True return करता है |
any()sequence का एक या सभी items True होते है तो ये तो ये True return करता है |
enumerate()दिए गए start से index और उसकी value की pair return करता है |
len()दिए गए tuple की length; number में return करता है |
max()दिए गए tuple से max value को return करता है |
min()दिए गए tuple से min value को return करता है |
sorted()दिए गए sequence को sort करके return करता है |
sum()दिए गए sequence के items को add करके उनका sum return करता है |
tuple()sequence को tuple में convert करता है |

strList ItemsNumberitem1item2item3item4item5 Index01234 Items"H""e""l""l""o"

List Negative Indexing

Python में List के आखिरी index '-1' से शुरू होता है |
strList = ["H", "e", "l", "l", "o"]
strList
ItemsNumberitem1item2item3item4item5
Index-5-4-3-2-1
Items"H""e""l""l""o"

Accesing 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

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

Accessing Nested List

list = [1, 2, "Hello", ["R", "U"]]

print(list[3][0]) #Output : R
print(list[3][1]) #Output : U

Convert List to Sub-List or 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]
[]

अगर colon(:) के left side का index invalid होता है तो blank list([]) return होती है और colon(:) के right side का index invalid होता है तो List के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank list([]) return होता है |
For Example,
list = [1, 2, "H", 2.8]
print(list[0:1]) # [1]
print(list[0:2]) # [1, 2]
print(list[1:3]) # [2, 'H']
print(list[1:15]) # [2, 'H', 2.8]
print(list[10:4]) # []

Check Length of List's Items

list = [1, 2, "H", 2.8]
print (len(list))

#Output : 4

List Concatenation

list = [1, 2, "H", 2.8] + ["Hello", [2, 8]]
print (list)

#Output : [1, 2, 'H', 2.8, 'Hello', [2, 8]]

List Repetition

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 Items using For Loop

list = [1, 2, "H", 2.8]
for i in list:
    print(i)

#Output :
1
2
H
2.8

Add Item(s) in List

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

#Output : [1, 2, 'H', 2.8, 'E']
list = ["P", "y", "t"]
list.extend(["h", "o", "n"])
print(list)

#Output : ['P', 'y', 't', 'h', 'o', 'n']

Change Items in List

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

#Output : ['C', 'y', 'p', 'h', 'o', 'n']

Deleting Items in List

List के item को delete करने के लिए 'del' operator का इस्तेमाल किया जाता है |
list = ['P', 'y', 't', 'h', 'o', 'n']
del list[2]
del list[0]
print(list)

#Output : ['y', 'h', 'o', 'n']

List Functions in Python

List FunctionDescription
len()दिए गए list की length; number में return करता है |
max()दिए गए list से max value को return करता है |
min()दिए गए list से min value को return करता है |
list()sequence को list में convert करता है |

List Methods in Python

List MethodDescription
append()list पर एक ही item जोड़ने के लिए किया जाता है |
clear()list को clear करता है |
copy()list की copy बनाता है |
count()दिए हुए item के list में आये occurrences return करता है |
extend()दिए गए list पर sequence को जोड़ता है |
index()दिए गए item की index return करता है |
insert()दिए गए valid index पर item को insert करता है |
pop()दिए गए index का item return किया जाता है |
remove()दिए गए item को remove करता है |
reverse()दिए गए list को reverse करता है |
sort()दिए गए sequence को sort करके return करता है |

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply