पायथन सिंटैक्स

पायथन सिंटैक्स

यह लेख पायथन सिंटैक्स को समझाता है।

YouTube Video

पायथन में सिंटैक्स

इन्डेंटेशन

पायथन कोड के ब्लॉक को परिभाषित करने के लिए इन्डेंटेशन का उपयोग करता है। कई अन्य भाषाओं की तरह जो कोड ब्लॉक्स परिभाषित करने के लिए कर्ली ब्रेसेस {} का उपयोग करती हैं, पायथन इन्डेंटेशन का उपयोग करता है। सामान्यतः, चार स्पेस के इन्डेंटेशन का उपयोग किया जाता है, लेकिन टैब्स का भी उपयोग किया जा सकता है। हालांकि, आपको एक ही फाइल में स्पेस और टैब्स को मिलाने से बचना चाहिए।

1x = 5
2if x > 0:
3    print("Positive")
4else:
5    print("Non-positive")

टिप्पणियाँ

सिंगल-लाइन टिप्पणी

पायथन में, टिप्पणियाँ # से शुरू होती हैं। लाइन के अंत तक का सब कुछ एक टिप्पणी के रूप में माना जाता है।

1# This is a comment
2print("Hello, World!")  # This is also a comment

डॉक्युमेंटेशन स्ट्रिंग (Docstring)

docstring एक स्ट्रिंग है जिसका उपयोग कोड का वर्णन करने के लिए किया जाता है, जिसे तीन डबल कोट्स """ या तीन सिंगल कोट्स ''' के साथ लिखते हैं। यह मुख्य रूप से मॉड्यूल, क्लास, और फ़ंक्शन्स का विवरण प्रस्तुत करने के लिए उपयोग किया जाता है।

1def greet(name):
2    """
3    This function displays a greeting to the specified name.
4
5    Parameters:
6        name (str): The name of the person to greet.
7    """
8    print(f"Hello, {name}!")

Docstring को help() फ़ंक्शन का उपयोग करके देखा जा सकता है।

1help(greet)

टिप्पणियों के लिए सर्वोत्तम प्रथाएँ

टिप्पणियों के लिए सर्वोत्तम प्रथाओं में निम्नलिखित बिंदु शामिल हैं:।

 1# Good example
 2# Validate user input and display a personalized message
 3
 4# Ask for the user's name
 5name = input("Enter your name: ")
 6
 7# Ask for the user's age and convert it to an integer
 8age = int(input("Enter your age: "))
 9
10# Check eligibility based on age
11if age >= 18:
12    # Inform the user that they can register
13    print(f"Welcome, {name}! You are eligible to register.")
14else:
15    # Inform the user that they are underage
16    print(f"Sorry, {name}. You must be at least 18 years old to register.")
17
18# Bad example
19# Store the user's name in the variable 'name'
20name = input("Enter your name: ")
21
22# Convert the input to an integer and store it in 'age'
23age = int(input("Enter your age: "))
24
25# Check if the user is greater than or equal to 18
26if age >= 18:
27    # Print a welcome message
28    print(f"Welcome, {name}! You are eligible to register.")
29else:
30    # Print a rejection message
31    print(f"Sorry, {name}. You must be at least 18 years old to register.")
  • स्पष्ट और संक्षिप्त रहें टिप्पणियों का उपयोग कोड के इरादे को स्पष्ट रूप से समझाने के लिए किया जाता है।

  • कोड के अर्थ को दोहराने से बचें ऐसे कोड पर टिप्पणी करने से बचें जो स्वयं स्पष्ट हो।

  • संगति बनाए रखें टीम विकास में, टिप्पणी शैली और प्रारूप में एकरूपता बनाए रखना महत्वपूर्ण है।

वैरिएबल्स और डेटा प्रकार

पायथन में, एक वैरिएबल को घोषित करते समय प्रकार निर्दिष्ट करने की आवश्यकता नहीं होती है। प्रकार असाइनमेंट के समय स्वतः निर्धारित होता है।

1x = 10        # Integer
2y = 3.14      # Floating-point number
3name = "Alice"  # String (text)
4is_active = True  # Boolean value (True or False)

कंडीशनल स्टेटमेंट्स

कंडीशनल स्टेटमेंट्स if, elif (else if), और else का उपयोग करती हैं।

1x = 0
2if x > 0:
3    print("Positive")
4elif x == 0:
5    print("Zero")
6else:
7    print("Negative")

लूप्स

पायथन for लूप और while लूप प्रदान करता है, प्रत्येक का उपयोग अलग-अलग तरीकों से किया जाता है।

for वक्तव्य

for वक्तव्य का उपयोग आम तौर पर लिस्ट या ट्यूपल के तत्वों को पुनरावृत्त करने के लिए किया जाता है।

1fruits = ["apple", "banana", "cherry"]
2for fruit in fruits:
3    print(fruit)

while वक्तव्य

while वक्तव्य तब तक लूप को दोहराता है जब तक कि शर्त सही हो।

1count = 0
2while count < 5:
3    print(count)
4    count += 1

फ़ंक्शन परिभाषित करना

पायथन में, फ़ंक्शन्स को def कीवर्ड का उपयोग करके परिभाषित किया जाता है।

1def greet(name):
2    print(f"Hello, {name}!")
3
4greet("Alice")

क्लासेस परिभाषित करना

पाइथन में, आप class कीवर्ड का उपयोग करके क्लासेज़ को परिभाषित कर सकते हैं, जो ऑब्जेक्ट-ओरियंटेड प्रोग्रामिंग को सक्षम बनाता है।

1class Dog:
2    def __init__(self, name):
3        self.name = name
4
5    def bark(self):
6        print("Woof!")
7
8dog = Dog("Fido")
9dog.bark()

मॉड्यूल और आयात

पायथन में, import कीवर्ड का उपयोग मॉड्यूल को आयात करने और मौजूदा कोड तक पहुंचने के लिए किया जाता है।

1# Importing the sqrt function from the math module
2from math import sqrt
3
4result = sqrt(16)
5print(result)  # Output: 4.0

त्रुटियाँ और अपवाद प्रबंधन

पायथन की try-except संरचना त्रुटियों और अप्रत्याशित स्थितियों को संभालती है।

1# Catching a division-by-zero error example
2try:
3    result = 10 / 0
4except ZeroDivisionError:
5    print("Cannot divide by zero.")

निष्कर्ष

पायथन का मूल सिंटैक्स बहुत सरल और अत्यंत पठनीय है। ये मूल बातें पाइथन कोड लिखने के लिए आवश्यक तत्व हैं।

आप हमारे YouTube चैनल पर Visual Studio Code का उपयोग करके ऊपर दिए गए लेख के साथ आगे बढ़ सकते हैं। कृपया YouTube चैनल को भी देखें।

YouTube Video