വേരിയബിളുകൾ#

ആമുഖം#

വേരിയബിളുകൾ ഡാറ്റ സംഭരിക്കുകയും നിങ്ങളുടെ പ്രോഗ്രാമിലുടനീളം അത് വീണ്ടും ഉപയോഗിക്കാനും കൈകാര്യം ചെയ്യാനും നിങ്ങളെ അനുവദിക്കുന്നു. പൈത്തൺ ഒരു ഡൈനാമിക്കലി ടൈപ്പ് ചെയ്ത ഭാഷയാണ്, അതായത് ഒരു വേരിയബിളിന്റെ തരം വ്യക്തമായി പ്രഖ്യാപിക്കേണ്ടതില്ല. പകരം, അതിന് നൽകിയിരിക്കുന്ന മൂല്യത്തെ അടിസ്ഥാനമാക്കി തരം സ്വയമേവ അനുമാനിക്കപ്പെടുന്നു. ഉദാഹരണത്തിന്:

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 – പൈത്തണിൽ ഒരു മൂല്യത്തിന്റെ അഭാവത്തെ പ്രതിനിധീകരിക്കുന്നു.

  • ശ്രേണി – ലൂപ്പുകളിൽ സാധാരണയായി ഉപയോഗിക്കുന്ന സംഖ്യകളുടെ ശ്രേണികൾ യാന്ത്രികമായി സൃഷ്ടിക്കുന്നു.

  • ലിസ്റ്റ് – മാറ്റാവുന്ന ഇനങ്ങളുടെ ഒരു ശേഖരം; വസ്തുക്കൾ അല്ലെങ്കിൽ സെൻസർ റീഡിംഗുകൾ പോലുള്ള മൂല്യങ്ങളുടെ ഗ്രൂപ്പുകൾ സംഭരിക്കുന്നതിന് നല്ലതാണ്.

  • 2D ലിസ്റ്റ് – ലിസ്റ്റുകളുടെ ഒരു ലിസ്റ്റ്; വരികൾ, ഗ്രിഡുകൾ അല്ലെങ്കിൽ പട്ടിക പോലുള്ള ഡാറ്റയെ പ്രതിനിധീകരിക്കുന്നതിന് അനുയോജ്യം.

  • ട്യൂപ്പിൾ – മാറ്റാൻ കഴിയാത്ത മൂല്യങ്ങളുടെ ഒരു നിശ്ചിത ശ്രേണി; ഗ്രൂപ്പുചെയ്‌തതും മാറ്റമില്ലാത്തതുമായ ഡാറ്റയ്ക്ക് ഉപയോഗപ്രദമാണ്.

ഒരു വേരിയബിൾ പ്രഖ്യാപിക്കുകയും നിയോഗിക്കുകയും ചെയ്യുന്നു#

ഒരു വേരിയബിൾ സൃഷ്ടിക്കാൻ, = ഓപ്പറേറ്റർ ഉപയോഗിച്ച് ഒരു പേരിന് ഒരു മൂല്യം നൽകുക:

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)

ചതുര ബ്രാക്കറ്റുകൾ () ന് പകരം [] എന്ന ബ്രാക്കറ്റ് ഉപയോഗിച്ചാണ് ട്യൂപ്പിളുകൾ സൃഷ്ടിക്കുന്നത്.