ভেরিয়েবল#

ভূমিকা#

ভেরিয়েবল ডেটা সংরক্ষণ করে এবং আপনাকে আপনার প্রোগ্রাম জুড়ে এটি পুনরায় ব্যবহার ও পরিবর্তন করার সুযোগ দেয়। পাইথন একটি ডাইনামিক্যালি টাইপড ভাষা, যার অর্থ হলো আপনাকে কোনো ভেরিয়েবলের টাইপ স্পষ্টভাবে ঘোষণা করার প্রয়োজন নেই। এর পরিবর্তে, এতে নির্ধারিত মানের উপর ভিত্তি করে টাইপটি স্বয়ংক্রিয়ভাবে অনুমান করা হয়। উদাহরণস্বরূপ:

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

এই এপিআইটি পাইথনের সাধারণ ভেরিয়েবল টাইপগুলো ব্যাখ্যা করে। যদিও এটি একটি সম্পূর্ণ তালিকা নয়, তবে এতে সেইসব টাইপ অন্তর্ভুক্ত রয়েছে যা আপনি বাস্তবে সবচেয়ে বেশি ব্যবহার করার সম্ভাবনা রাখেন।

  • লোকাল ভেরিয়েবল – একটি ফাংশনের ভিতরে ঘোষণা করা হয় এবং শুধুমাত্র সেই স্কোপের মধ্যেই ব্যবহৃত হয়; অস্থায়ী বা বিচ্ছিন্ন মানের জন্য সবচেয়ে ভালো।

  • গ্লোবাল ভেরিয়েবল – যেকোনো ফাংশনের বাইরে ঘোষণা করা হয় এবং পুরো প্রোজেক্ট জুড়ে ব্যবহৃত হয়; ফাংশনগুলোর মধ্যে ডেটা শেয়ার করার জন্য ভালো।

  • পূর্ণসংখ্যা – গণনা, দূরত্ব বা দশমিক ছাড়া যেকোনো কিছুর জন্য ব্যবহৃত পূর্ণ সংখ্যা।

  • ফ্লোট – দশমিক বিন্দুযুক্ত সংখ্যা, যা নির্ভুল পরিমাপ বা গণনার জন্য উপযোগী।

  • স্ট্রিং – টেক্সট ভ্যালু, যা মেসেজ, লেবেল অথবা পাঠযোগ্য আউটপুট দেখানোর জন্য ব্যবহৃত হয়।

  • বুলিয়ান – True অথবা False মান।

  • NoneType – পাইথনে কোনো মানের অনুপস্থিতি বোঝায়।

  • রেঞ্জ – স্বয়ংক্রিয়ভাবে সংখ্যার ক্রম তৈরি করে, যা সাধারণত লুপে ব্যবহৃত হয়।

  • তালিকা – আইটেমের একটি পরিবর্তনযোগ্য সংগ্রহ; বস্তু বা সেন্সর রিডিংয়ের মতো মানের গোষ্ঠী সংরক্ষণের জন্য ভালো।

  • 2D তালিকা – তালিকার তালিকা; সারি, গ্রিড, বা টেবিলের মতো ডেটা উপস্থাপনের জন্য আদর্শ।

  • Tuple – মানগুলির একটি স্থির ক্রম যা পরিবর্তন করা যায় না; গোষ্ঠীভুক্ত, অপরিবর্তনশীল ডেটার জন্য উপযোগী।

ভেরিয়েবল ঘোষণা এবং নির্ধারণ করা#

একটি ভেরিয়েবল তৈরি করতে, = অপারেটরটি ব্যবহার করে নামটিকে একটি মান দিয়ে দিন:

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

ঐচ্ছিক। শুরুর মানটি অন্তর্ভুক্তিমূলক (অনুক্রমটি এখান থেকে শুরু হয়)। ডিফল্ট মান হলো ০।

stop

স্টপ ভ্যালুটি এক্সক্লুসিভ (ধারাটি এই সংখ্যার আগে থেমে যায়)।

step

ঐচ্ছিক। ধাপটি নির্ধারণ করে প্রতিটি সংখ্যা কতটা বাড়বে (বা কমবে)। ডিফল্ট মান হলো ১।

# 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#

একটি ২ডি লিস্ট, বা লিস্টের লিস্ট, সাধারণত গ্রিড, টেবিল বা ম্যাট্রিক্স উপস্থাপন করতে ব্যবহৃত হয়। প্রতিটি সাব-লিস্ট একটি সারি বা ডেটার একটি নির্দিষ্ট গ্রুপকে প্রতিনিধিত্ব করে।

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)

বর্গাকার বন্ধনী () এর পরিবর্তে প্রথম বন্ধনী [] ব্যবহার করে টাপল তৈরি করা হয়।