Data type वह अलग अलग value होती है जो की किसी variable के अंदर store होती है | ये वैल्यू अलग-अलग प्रकार की होती है जैसे की कोई String या फिर कोई नंबर आदि |
पाइथन मे पांच ( 5 ) Standard data types होते है|
- Numbers
- String
- List
- Tuple
- Set
- Dictionary
1.Numbers
Number डाटा टाइप numeric value जैसे की int , float आदि को स्टोर करता है | पाइथन 2.x मे हमारे पास 4 number data type थे लेकिन पाइथन 3.x मे केवल तीन ही नंबर डाटा टाइप है |
- int
- float
- complex number
# int data type
x=56
print (type(x))
<class 'int'>
# float data type
x=556.4545
print (type(x))
<class 'float'>
# Complex data type
x=5+8j
print (type(x))
<class 'complex'>
2. String
String अलग -अलग characters का कलेक्शन होता है ये characters सिंगल कोट्स या डबल कोट्स के अंदर लिखे जाते है पाइथन Unicode characters को support करता है |
(string are collection of characters inside single and double quotes. python support Unicode characters.)
# string - str data type
a = "10"
print (a)
print (type(a))
OUTPUT :
10
<class 'str'>
Concatenate the String
दो या दो से अधिक string को आपस मे जोड़ना |
दो या दो से अधिक string को concatenate करने के लिए + operator का प्रयोग किया जाता है
name1 = "the"
name2 = "technical"
name3 = "notes.com"
full_name = name1+name2+name3
print (full_name)
print (type (full_name) )
OUTPUT :
thetechnicalnotes.com
<class 'str'>
Note :- किसी string के साथ आप किसी numbers को concatenate नहीं कर सकते | ऐसा करने पे type error आती है
name = "thetechnicalnotes.com"
full_name = name + 2019
print (full_name)
OUTPUT :
Type Error
अगर हमे किसी स्ट्रिंग के साथ कोई number जोड़ना (concatenate) है तो उस नंबर को हम डबल या सिंगल कोट्स के अंदर लिखेंगे जिससे की वो नंबर String की तरह काम करेगा | या str() function का प्रयोग करके हम किसी नंबर को स्ट्रिंग के साथ जोड़ सकते है
name = "thetechnicalnotes.com"
full_name = name + "2019"
print (full_name)
OUTPUT :
thetechnicalnotes.com2019
String Repetition
अगर हमे किसी स्ट्रिंग को दो या दो से अधिक बार repeat करना हो हो उसके लिए repetition ( * )
operator का प्रयोग करते है|
name = "thetechnicalnotes.com "
print (name*2)
OUTPUT :
thetechnicalnotes.com thetechnicalnotes.com
String Slicing
syntax :- [start arguments : stop arguments ]
- अगर आपको किसी स्ट्रिंग के कुछ characters को प्रिंट करना चाहते हो तो आप slice operator का प्रयोग कर सकते हो|
name = "thetechnicalnotes.com "
print (name[0:3])
print (name[3:6])
OUTPUT :
the
tec
- अगर आपको किसी स्ट्रिंग का कोई एक characters को प्रिंट करना हो तो
name = "thetechnicalnotes.com "
print (name[3])
print (name[13])
OUTPUT :
t
o
Python string methods :- आगे भी पढ़े|
3. List
पाइथन मे list , array की तरह होते है | list mutable(changeable) होता है |
एक सिंगल list डाटा टाइप जैसे int , string और ऑब्जेक्ट को contain करके रखता है|
list , stacks और queues को implement करने का एक उपयोगी तरीका है |
पाइथन list मे जो elements होते है उन्हें [items] कहते है |
# list data type
Z= [10]
print (type(Z))
OUTPUT :
<class 'list'>
Creating List
लिस्ट मे जो item होते है उन्हें हम square brackets([]) के अंदर लिख़ते है और प्रत्येक item को comma(,) से separate किया जाता है |
list1 = ["hello","python","india"] #string list
list2 = [15, 24, 38, 54] #Integer list
list3 = [5.5, 4.8, 9.8, 4.4, 5.7]; #Float list
list4 = [1, 28, "python", 45.5]; #Mixed Data Types list
list5 = [21, [15, 2], 4.5] #Nested list
python list in hindi :- आगे भी पढ़े |
4. Tuples
पाइथन tuples भी पाइथन list की तरह Elements का sequence होता है लेकिन tuples immutable (unchangeable ) होता है | पाइथन list की तरह python tuples मे जो elements होते है उन्हें [items] कहते है | tuples को हम केवल read कर सकते है |
Creating Tuples
Tuples मे जो item होते है उन्हें हम parenthesis(( )) के अंदर लिख़ते है और प्रत्येक item को comma(,) से separate किया जाता है |
जैसे की :-
tuple1 = ("hii","python","welcome") #string list
tuple2 = (155, 624, 89, 74) #Integer list
tuple3 = (45.5, 4.82, 59.8, 40.4, 25.7); #Float list
tuple4 = (5, 28, "python", 4.5); #Mixed Data Types list
tuple5 = (26, (256, 9), 24.5) #Nested list
- tuple को हम update नहीं कर सकते |
- tuple को हम delete कर सकते है लेकिन अगर हम tuple के individual item / elements को हम डिलीट नहीं कर सकते |
tuple = (1, 28, "python", 45.5)
del tuple
print (tuple)
output :- <class 'tuple'>
# tuple are delete .
आगे भी पढ़े | Python tuple in Hindi
5. Set
- Python Set, unordered item का collection होता है।
- Set, duplicate वैल्यू को remove करता है।
- Set , Mutable होता है अर्थात हम set में change (add और remove) कर सकते है।
- Set , indexing और slicing को support नहीं करता।
- Set , Hashing के concept को follow करता है।
Creating set
set1 ={"tech","computer",25,96,25.63}
print(set1)
print(type(set1))
OUTPUT
{'computer', 96, 'tech', 25, 25.63}
<class 'set'>
# Empty Set
a= set()
print(a)
print(type(a))
OUTPUT
set()
<class 'set'>
आगे पढ़े। Python set in Hindi
6. Dictionary
- Dictionaries key:value के pairs में data value को स्टोर करता है।
- Dictionaries, curly brackets {} के साथ लिखे जाते है जिसमे key और values होते है।
- Dictionaries mutable होती है अर्थात इसमें changes कर सकते है।
- Dictionaries duplicate आइटम को allow नहीं करता।
- Dictionaries में स्टोर item एक order में define होते है। इस order को change नहीं कर सकते।
नोट : Python version 3.7 में dictionaries, ordered को support करता है। जबकि Python 3.6 और इससे पहले के version में dictionaries में स्टोर आइटम unordered होते थे।
Create dictionary
Dictionary एक साथ multiple key : value के pairs को create करती है ये पेयर्स curly brackets {key : value} के साथ लिखे जाते है। और प्रत्येक key को कोलन (:) दवारा उसके value से separate किया जाता है।
student={1:"ram", 2:"ram", 3:"krishna", 4:"sita", 5:"arjun"}
print(student)
OUTPUT
{1: 'ram', 2: 'ram', 3: 'krishna', 4: 'sita', 5: 'arjun'}
आगे पढ़े। Python Dictionary in hindi