चर#

परिचय#

वेरिएबल डेटा को स्टोर करते हैं और आपको अपने प्रोग्राम में इसका पुन: उपयोग और हेरफेर करने की अनुमति देते हैं। पायथन एक डायनेमिकली टाइप्ड भाषा है, जिसका अर्थ है कि आपको किसी वेरिएबल के प्रकार को स्पष्ट रूप से घोषित करने की आवश्यकता नहीं है। इसके बजाय, इसे दिए गए मान के आधार पर प्रकार स्वचालित रूप से अनुमानित हो जाता है। उदाहरण के लिए:

angle = 90           # angle is an integer
dist = "Distance: "  # dist is a string
steps = 2.5          # steps is a float

पायथन स्ट्रॉन्गली टाइप्ड भी है, जिसका अर्थ है कि आप असंगत प्रकारों पर स्पष्ट रूप से रूपांतरण किए बिना कोई ऑपरेशन नहीं कर सकते। उदाहरण के लिए:

red_disks = 2                       # red_disks is an integer
green_disks = "4"                      # green_disks is a string
result = red_disks + green_disks  # Creates a TypeError

ऐसी समस्याओं को हल करने के लिए, यदि उपयुक्त हो, तो आपको प्रकारों को स्पष्ट रूप से परिवर्तित करना होगा:

red_disks = 2     # red_disks is an integer
green_disks = "4" # green_disks is a string

# Convert green_disks to an integer before adding
result = int(green_disks) + red_disks

यह API पायथन में आम तौर पर इस्तेमाल होने वाले वेरिएबल प्रकारों के बारे में बताता है। यह पूरी सूची नहीं है, लेकिन इसमें उन प्रकारों को शामिल किया गया है जिनका उपयोग आप व्यवहार में सबसे अधिक करेंगे।

  • स्थानीय चर – फ़ंक्शन के अंदर घोषित और केवल उसी दायरे में उपयोग किया जाता है; अस्थायी या पृथक मानों के लिए सबसे अच्छा।

  • ग्लोबल वैरिएबल – किसी भी फ़ंक्शन के बाहर घोषित किया जाता है और पूरे प्रोजेक्ट में उपयोग किया जाता है; फ़ंक्शंस के बीच डेटा साझा करने के लिए अच्छा है।

  • पूर्णांक – गिनती, दूरी या दशमलव के बिना किसी भी चीज़ के लिए उपयोग की जाने वाली पूर्ण संख्याएँ।

  • फ्लोट – दशमलव बिंदु वाली संख्याएँ, सटीक माप या गणना के लिए उपयोगी।

  • स्ट्रिंग – पाठ मान, संदेशों, लेबलों या पठनीय आउटपुट प्रदर्शित करने के लिए उपयोग किया जाता है।

  • बूलियन – True या False तर्क और निर्णय लेने के लिए मान।

  • NoneType – पायथन में किसी मान की अनुपस्थिति को दर्शाता है।

  • रेंज – स्वचालित रूप से संख्याओं के अनुक्रम उत्पन्न करता है, जो आमतौर पर लूप में उपयोग किया जाता है।

  • सूची – वस्तुओं का एक परिवर्तनीय संग्रह; वस्तुओं या सेंसर रीडिंग जैसे मूल्यों के समूहों को संग्रहीत करने के लिए अच्छा है।

  • 2डी सूची – सूचियों की एक सूची; पंक्तियों, ग्रिड या तालिका जैसे डेटा का प्रतिनिधित्व करने के लिए आदर्श।

  • टपल – मानों का एक निश्चित क्रम जिसे बदला नहीं जा सकता; समूहीकृत, अपरिवर्तनीय डेटा के लिए उपयोगी।

किसी वेरिएबल को घोषित करना और उसे असाइन करना#

एक वेरिएबल बनाने के लिए, बस = ऑपरेटर का उपयोग करके नाम को एक मान असाइन करें:

distance = 100

किसी वेरिएबल का नामकरण करते समय, निम्नलिखित नियमों का पालन करना आवश्यक है:

  • नाम में विशेष वर्ण (जैसे विस्मय चिह्न) नहीं हो सकते।

  • नाम की शुरुआत किसी संख्या से नहीं हो सकती।

  • नाम में रिक्त स्थान नहीं हो सकते।

  • यह नाम VEXcode में आरक्षित शब्द नहीं हो सकता (उदाहरण के लिए, Drivetrain)।

Local Variables#

स्थानीय चर (लोकल वेरिएबल) किसी फ़ंक्शन या कोड ब्लॉक के अंदर परिभाषित किए जाते हैं। वे केवल उसी फ़ंक्शन या ब्लॉक के दायरे में ही पहुंच योग्य होते हैं और उसके बाहर दिखाई नहीं देते।

def show_local():
    # This variable only exists inside this function
    message = "I'm local!" 
    console.print(message)

def main():
    show_local()

# Start threads — Do not delete
start_thread(main)

स्थानीय चरों का उपयोग आमतौर पर अस्थायी मानों को संग्रहीत करने के लिए किया जाता है जो केवल किसी विशिष्ट फ़ंक्शन या प्रोग्राम के भाग के भीतर ही प्रासंगिक होते हैं।

Global Variables#

ग्लोबल वेरिएबल किसी भी फंक्शन या ब्लॉक के बाहर परिभाषित किए जाते हैं। इन्हें प्रोग्राम में कहीं भी एक्सेस और रीड किया जा सकता है, जिसमें फंक्शन के अंदर का भाग भी शामिल है।

ध्यान दें: एक ग्लोबल वेरिएबल का उपयोग आपके प्रोजेक्ट में कहीं भी किया जा सकता है। यह मददगार हो सकता है, लेकिन इससे बग्स को ढूंढना भी मुश्किल हो सकता है। यदि मान बदलता है, तो आपको यह देखने के लिए प्रोजेक्ट के कई हिस्सों की जांच करनी पड़ सकती है कि यह कहाँ बदला है। लोकल वेरिएबल का उपयोग तब करें जब केवल एक फ़ंक्शन को मान की आवश्यकता हो।

# The variable is defined outside a function
message = "I'm global!"  

def show_global():
    # You can access 'message' inside a function
    console.print(message)
    console.new_line()

def main():
    show_global()
    # And you can access 'message' outside a function
    console.print(message)

# Start threads — Do not delete
start_thread(main)

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

# Define the global variable
count = 0 

def increase_count():
    # Use the global keyword to let you modify the
    # global variable
    global count
    count = count + 1
    console.print("Count: {}".format(count))

def main():
    increase_count()
    console.new_line()
    increase_count()

# Start threads — Do not delete
start_thread(main)

डेटा के प्रकार#

पायथन वैरिएबल विभिन्न प्रकार के डेटा को स्टोर कर सकते हैं, जिनमें से प्रत्येक अलग-अलग उपयोग के मामलों के लिए उपयुक्त होता है। नीचे सबसे अधिक उपयोग किए जाने वाले प्रकार दिए गए हैं:

Integer#

एक पूर्णांक धनात्मक या ऋणात्मक संख्या होती है।

# Build Used: Super Code Base 2.0
def main():
    distance = 100

    # Move the robot forward for the variable value in mm
    drivetrain.drive_for(FORWARD, distance, MM)

    # Add to the variable and move forward the new value, 
    # for 200mm total
    wait(1, SECONDS)
    distance = distance + 100
    drivetrain.drive_for(FORWARD, distance, MM)

# Start threads — Do not delete
start_thread(main)

Float#

एक फ्लोट एक दशमलव बिंदु वाली संख्या होती है।

def main():
    # Store a value with decimal points
    raw_value = 0.88

    # Print the decimal value as a percentage
    console.print(raw_value * 100, "%")

# Start threads — Do not delete
start_thread(main)

String#

स्ट्रिंग अक्षरों का एक क्रम है, जिसका उपयोग आमतौर पर टेक्स्ट के लिए किया जाता है।

def main():
    # Set the variable to a string then print the string
    message = "Ready!"
    console.print(message)

# Start threads — Do not delete
start_thread(main)

ध्यान दें: किसी भी स्ट्रिंग को हमेशा एक जैसे उद्धरण चिह्नों के भीतर ही रखना चाहिए, चाहे वे एकल (’) हों या दोहरे (”)। आप दोनों में से किसी भी शैली का उपयोग कर सकते हैं, लेकिन शुरुआती और अंतिम चिह्न एक जैसे होने चाहिए।

Boolean#

एक बूलियन True या False मानों का प्रतिनिधित्व करता है।

def main():
    # Set the state of the variable
    delivered = False

    # Print different messages depending on the Boolean.
    if delivered:
        console.print("Package delivered!")
    else: 
        console.print("Delivering...")

# Start threads — Do not delete
start_thread(main)

बूलियन मानों को प्रोजेक्ट के किसी भी चरण में बदला जा सकता है।

def main():
    # Print the value of the delivered variable
    delivered = True
    console.print(delivered)
    console.new_line()
    wait(2,SECONDS)

    # Clear the screen and print the value of the variable again
    console.clear()
    delivered = False
    console.print(delivered)

# Start threads — Do not delete
start_thread(main)

NoneType#

NoneType पाइथन में किसी मान की अनुपस्थिति को दर्शाता है।

def main():
    # No task is assigned yet
    current_task = None

    # Check if a task is assigned
    if current_task is None:
        console.print("No task!")
    else:
        console.print("Task: {}".format(current_task))

# Start threads — Do not delete
start_thread(main)

Range#

एक रेंज संख्याओं का एक क्रम है, जिसका उपयोग आमतौर पर लूप में संख्यात्मक अनुक्रम उत्पन्न करने के लिए किया जाता है। इसका प्रारूप इस प्रकार है:

range(start, stop, step)

अवयव

विवरण

start

यह वैकल्पिक है। प्रारंभिक मान शामिल है (अनुक्रम यहीं से शुरू होता है)। डिफ़ॉल्ट मान 0 है।

stop

स्टॉप वैल्यू एक्सक्लूसिव है (अनुक्रम इस संख्या से पहले रुक जाता है)।

step

यह वैकल्पिक है। यह चरण निर्धारित करता है कि प्रत्येक संख्या कितनी बढ़ेगी (या घटेगी)। डिफ़ॉल्ट मान 1 है।

# Build Used: Super Code Base 2.0
def main():
    # Drive and turn 4 times to move in a square
    for index in range(4):
        drivetrain.drive_for(FORWARD, 100, MM)
        drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

def main():
    # Count by 2 starting at 1 and ending before 12
    for index in range(1, 12, 2): 
        # Print the values on the screen with each loop
        console.print(index)
        console.new_line()

# Start threads — Do not delete
start_thread(main)

List#

एक सूची (लिस्ट) में कई आइटम एक ही वेरिएबल में स्टोर होते हैं। आप इसमें आइटम जोड़ सकते हैं, हटा सकते हैं, बदल सकते हैं या अपने प्रोजेक्ट में प्रत्येक आइटम का अलग-अलग उपयोग कर सकते हैं। सूचियाँ तब उपयोगी होती हैं जब आप संबंधित मानों को एक साथ रखना चाहते हैं, जैसे सेंसर रीडिंग, रंग या ऑब्जेक्ट के नाम। सूचियाँ वर्गाकार कोष्ठकों [] ) का उपयोग करके बनाई जाती हैं।

def main():
    # Define a list of colors
    colors = ["Red", "Green", "Blue", "Purple"]

    # Repeat for the number of items in the colors list
    for index in colors:
        # Print each color in order
        console.print(index)
        console.new_line()

# Start threads — Do not delete
start_thread(main)

किसी सूची में आइटम जोड़ने के लिए append का उपयोग करें।

def main():
    # Define a list of colors
    colors = ["Red", "Green", "Blue", "Purple"]

    # Append a new color to the list
    colors.append("Yellow")

    # Repeat for the number of items in the colors list
    for index in colors:
        # Print each color in order
        console.print(index)
        console.new_line()

# Start threads — Do not delete
start_thread(main)

2D List#

एक 2D सूची, या सूचियों की सूची, आमतौर पर ग्रिड, टेबल या मैट्रिक्स को दर्शाने के लिए उपयोग की जाती है। प्रत्येक उपसूची एक पंक्ति या डेटा के एक विशिष्ट समूह का प्रतिनिधित्व करती है।

def main():
    # Assign the values in the matrix 2D list
    matrix = [
        ["A", 1, "Red"],
        ["B", 2, "Orange"],
        ["C", 3, "Yellow"]
    ]
    # Loop through each row
    for row in matrix: 
        # Loop through each column in the row 
        for element in row: 
            console.print(element, ", ")
        console.new_line()

# Start threads — Do not delete
start_thread(main)

आप 2D सूची के भीतर विशिष्ट तत्वों या यहां तक ​​कि पूरी उपसूचियों को भी संशोधित कर सकते हैं:

def main():
    # Assign the values in the matrix 2D list
    matrix = [
        ["A", 1, "Red"],
        ["B", 2, "Orange"],
        ["C", 3, "Yellow"]
    ]
    # Modify the color (in column 2) in row 0
    matrix[0][2] = "Blue"

    # Print the modified row from the matrix 2D list 
    console.print(matrix[0])

# Start threads — Do not delete
start_thread(main)

Tuple#

टपल एक प्रकार का ऐसा क्रम है जिसमें आइटम एक बार बन जाने के बाद बदले नहीं जा सकते। इनका उपयोग अक्सर संबंधित मानों को एक साथ समूहित करने के लिए किया जाता है।

def main():
    # Define a tuple
    set_1 = (100, "Left")

    # Print the tuple
    console.print(set_1)

# Start threads — Do not delete
start_thread(main)

टुपल्स को वर्ग कोष्ठक () के बजाय कोष्ठक [] उपयोग करके बनाया जाता है।