Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
वेरिएबल्स और प्रकार
Python पूरी तरह से ऑब्जेक्ट ओरिएंटेड है और "स्टैटिकली टाइप्ड" नहीं है। आपको उनका उपयोग करने से पहले वेरिएबल्स की घोषणा करने की ज़रूरत नहीं है, या उनके प्रकार की घोषणा करने की ज़रूरत नहीं है। Python में हर वेरिएबल एक ऑब्जेक्ट होता है।
यह ट्यूटोरियल कुछ मूल प्रकार के वेरिएबल्स पर चर्चा करेगा।
Numbers
Python दो प्रकार के नंबरों का समर्थन करता है - integers (पूर्णांक) और floating point numbers (दशमलव)। (यह complex numbers का भी समर्थन करता है, जिन्हें इस ट्यूटोरियल में नहीं समझाया जाएगा)।
एक integer को परिभाषित करने के लिए, निम्नलिखित सिंटैक्स का उपयोग करें:
myint = 7
print(myint)
एक floating point number को परिभाषित करने के लिए, आप निम्नलिखित में से किसी एक संकेतन का उपयोग कर सकते हैं:
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
Strings
Strings को या तो सिंगल क्वोट या डबल क्वोट के साथ परिभाषित किया जा सकता है।
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
दोनों के बीच का अंतर यह है कि डबल क्वोट्स का उपयोग करने से अपॉस्ट्रोफ्स को शामिल करना आसान हो जाता है (जबकि सिंगल क्वोट्स का उपयोग करने पर यह स्ट्रिंग समाप्ति का कारण बन सकता है)
mystring = "Don't worry about apostrophes"
print(mystring)
Strings को परिभाषित करने के लिए अतिरिक्त विभिन्नताएँ हैं जो कैरिज रिटर्न्स, बैक्सलैशेज़ और यूनिकोड कैरेक्टर्स जैसी चीजों को शामिल करना आसान बनाती हैं। ये इस ट्यूटोरियल के दायरे से बाहर हैं, लेकिन Python डॉक्यूमेंटेशन में कवर की गई हैं।
सरल ऑपरेटर नंबरों और स्ट्रिंग्स पर निष्पादित किए जा सकते हैं:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
असाइनमेंट एक से अधिक वेरिएबल्स पर "एक ही समय में" एक ही लाइन पर किया जा सकता है, जैसे
a, b = 3, 4
print(a, b)
नंबर्स और स्ट्रिंग्स के बीच ऑपरेटर को मिलाना समर्थित नहीं है:
# This will not work!
one = 1
two = 2
hello = "hello"
print(one + two + hello)
Exercise
इस अभ्यास का लक्ष्य एक स्ट्रिंग, एक integer, और एक floating point number बनाना है। स्ट्रिंग का नाम mystring
होना चाहिए और उसमें "hello" शब्द होना चाहिए। floating point number का नाम myfloat
होना चाहिए और उसमें 10.0 अंक होना चाहिए, और integer का नाम myint
होना चाहिए और उसमें 20 अंक होना चाहिए।
# change this code
mystring = None
myfloat = None
myint = None
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
# change this code
mystring = "hello"
myfloat = 10.0
myint = 20
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
test_object('mystring', incorrect_msg="Don't forget to change `mystring` to the correct value from the exercise description.")
test_object('myfloat', incorrect_msg="Don't forget to change `myfloat` to the correct value from the exercise description.")
test_object('myint', incorrect_msg="Don't forget to change `myint` to the correct value from the exercise description.")
test_output_contains("String: hello",no_output_msg= "Make sure your string matches exactly to the exercise desciption.")
test_output_contains("Float: 10.000000",no_output_msg= "Make sure your float matches exactly to the exercise desciption.")
test_output_contains("Integer: 20",no_output_msg= "Make sure your integer matches exactly to the exercise desciption.")
success_msg("Great job!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!