Python একটি ডাইনামিক এবং স্ট্রং টাইপের প্রোগ্রামিং ভাষা। এটি টাইপ হিন্ট-এর মাধ্যমে ডাক টাইপিং এবং গ্র্যাজুয়াল টাইপিং দুটোই ব্যবহার করে।
Python অনেক ভিন্ন প্রোগ্রামিং স্টাইল সমর্থন করলেও, অভ্যন্তরীণভাবে Python-এ সবকিছুই একটি অবজেক্ট। এর মধ্যে রয়েছে সংখ্যা, স্ট্রিং, অ্যারে এবং এমনকি ফাংশনও।
ট্র্যাক ধরে এগোতে এগোতে এই সবকিছুর মানে নিয়ে আমরা আরও গভীরে যাব।
এই প্রথম অনুশীলনীতে Python ভাষার ৪টি প্রধান বৈশিষ্ট্য পরিচয় করিয়ে দেওয়া হয়:
১. নাম অ্যাসাইনমেন্ট (ভ্যারিয়েবল এবং কনস্ট্যান্ট),
২. ফাংশন (def কিওয়ার্ড এবং return কিওয়ার্ড),
৩. কমেন্ট, এবং
৪. ডকস্ট্রিং।
সাধারণভাবে, Python ট্র্যাকের কনটেন্ট, টেস্ট এবং অ্যানালাইজার টুলিং Python কোড স্টাইলের জন্য PEP 8 এবং PEP 257-এ বর্ণিত স্টাইল রীতিগুলো মেনে চলে, এর সঙ্গে বাড়তি (জোরালো) পরামর্শ হলো, এক অক্ষরের কোনো ভ্যারিয়েবল নাম থাকবে না এবং "_" নামে কোনো ভ্যারিয়েবলও থাকবে না।
Python ট্র্যাকে ভ্যারিয়েবল সবসময় snake_case-এ লেখা হয়, আর কনস্ট্যান্ট লেখা হয় SCREAMING_SNAKE_CASE-এ।
প্রোগ্রামাররা অ্যাসাইনমেন্ট = অপারেটর ব্যবহার করে যেকোনো ধরনের অবজেক্টের সাথে নাম (যেগুলোকে ভ্যারিয়েবল-ও বলা হয়) বাইন্ড করতে পারেন: <name> = <value>।
একটি নাম তার জীবদ্দশায় ভিন্ন ভিন্ন মানে (ভিন্ন ভিন্ন অবজেক্ট টাইপে) পুনরায় অ্যাসাইন (বা রি-বাইন্ড) করা যায়।
>>> my_first_variable = 1 #<-- my_first_variable bound to an integer object of value one.
>>> my_first_variable = 2 #<-- my_first_variable re-assigned to integer value 2.
>>> print(type(my_first_variable))
<class 'int'>
>>> print(my_first_variable)
2
>>> my_first_variable = "Now, I'm a string." #<-- You may re-bind a name to a different object type and value.
>>> print(type(my_first_variable))
<class 'str'>
>>> my_first_variable = 'You can call me "str".' #<-- Strings can be declared using single or double quote marks.
>>> print(my_first_variable)
You can call me "str".
কনস্ট্যান্ট হলো এমন নাম, যেগুলো একটি প্রোগ্রামে একবারই অ্যাসাইন করার কথা।
এগুলো মডিউল (ফাইল) পর্যায়ে ডিফাইন করা উচিত, এবং সাধারণত প্রোগ্রামের সব ফাংশন ও ক্লাস থেকে দেখা যায়।
SCREAMING_SNAKE_CASE ব্যবহার করা বোঝায় যে নামটি পুনরায় অ্যাসাইন করা উচিত নয়, বা এর মান পরিবর্তন করা উচিত নয়।
def কিওয়ার্ড একটি ফাংশন ডেফিনিশন শুরু করে।
প্রতিটি ফাংশনে () বন্ধনীর ভেতরে শূন্য বা তার বেশি আনুষ্ঠানিক প্যারামিটার থাকতে পারে, তারপর একটি : কোলন।
ফাংশনের বডি-র স্টেটমেন্টগুলো def-এর পরের লাইন থেকে শুরু হয় এবং অবশ্যই একটি ব্লকে ইনডেন্ট করা থাকতে হবে।
# The body of a function is indented by 2 spaces, & prints the sum of the numbers.
def add_two_numbers(number_one, number_two):
total = number_one + number_two
print(total)
>>> add_two_numbers(3, 4)
7
# Inconsistent indentation in your code blocks will raise an error.
>>> def add_three_numbers_misformatted(number_one, number_two, number_three):
... result = number_one + number_two + number_three # This was indented by 4 spaces.
... print(result) #this was only indented by 3 spaces
...
...
File "<stdin>", line 3
print(result)
^
IndentationError: unindent does not match any outer indentation level
ফাংশন return কিওয়ার্ডের মাধ্যমে স্পষ্টভাবে একটি মান বা অবজেক্ট রিটার্ন করে:
# Function definition on first line, explicit return used on final line.
>>> def add_two_numbers(number_one, number_two):
return number_one + number_two
# Calling the function in the Python shell returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7
# Assigning the function call to a variable and printing it
# will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
11
যেসব ফাংশনে return-এর পরে স্পষ্ট কোনো এক্সপ্রেশন থাকে না, তারা অন্তর্নিহিতভাবে None অবজেক্ট রিটার্ন করে।
None-এর বিস্তারিত পরের একটি অনুশীলনীতে আলোচনা করা হবে।
এই অনুশীলনী ও ব্যাখ্যার উদ্দেশ্যে, None হলো একটি প্লেসহোল্ডার, যা কিছুই না বা null বোঝায়:
# This function will return `None`
def square_a_number(number):
square = number * number
return # <-- note that this return is not followed by an expression
# Calling the function in the Python shell appears
# to not return anything at all.
>>> square_a_number(2)
>>>
# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(square_a_number(2))
None
যেসব ফাংশনে return বাদ দেওয়া হয়, তারাও অন্তর্নিহিতভাবে None অবজেক্ট রিটার্ন করে।
এর মানে হলো, আপনি যদি কোনো ফাংশনে return ব্যবহার না করেন, Python আপনার হয়ে None অবজেক্ট রিটার্ন করবে।
# This function omits a return keyword altogether.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
>>> add_two_numbers(5, 7)
>>> print(add_two_numbers(5, 7))
None
# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None
নামের পরে () লিখে ফাংশন কল বা ইনভোক করা হয়।
ক্লাস বা মডিউলের ভেতরে ডিফাইন করা ফাংশন কল করার সময় ডট (.) নোটেশন ব্যবহার করা হয়।
>>> def raise_to_power(number, power):
... return number ** power
...
>>> raise_to_power(3,3) # <--Invoking the function with the arguments 3 and 3.
27
# A mismatch between the number of parameters and the number of arguments will raise an error.
>>> raise_to_power(4,)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: raise_to_power() missing 1 required positional argument: 'power'
# Calling methods or functions in classes and modules.
>>> start_text = "my silly sentence for examples."
>>> str.upper(start_text) # <--Calling the upper() method from the built-in str class on start_text.
'MY SILLY SENTENCE FOR EXAMPLES.'
# Importing the math module
>>> import math
>>> math.pow(2,4) # <--Calling the pow() function from the math module.
16.0
Python-এ কমেন্ট শুরু হয় # দিয়ে, যা স্ট্রিংয়ের অংশ নয়, এবং লাইন শেষ হওয়ার সাথে শেষ হয়।
অন্য অনেক প্রোগ্রামিং ভাষার মতো নয়, Python মাল্টি-লাইন কমেন্ট চিহ্ন সমর্থন করে না।
কমেন্ট ব্লকের প্রতিটি লাইন # অক্ষর দিয়ে শুরু হতে হবে।
ফাংশন বডির প্রথম স্টেটমেন্টটি ঐচ্ছিকভাবে একটি ডকস্ট্রিং হতে পারে, যা সংক্ষেপে ফাংশন বা অবজেক্টের উদ্দেশ্য তুলে ধরে। ডকস্ট্রিং ঘোষণা করা হয় তিনটি ডাবল কোট (""") দিয়ে, যা কোড ব্লকের মতো একই স্তরে ইনডেন্ট করা থাকে:
# An example from PEP257 of a multi-line docstring
# reformatted to use Google style non-type hinted docstrings.
# Some additional details can be found in the Sphinx documentation:
# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#getting-started
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword Arguments:
real (float): The real part of the number (default 0.0)
imag (float): The imaginary part of the number (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zero
ডকস্ট্রিং Sphinx-এর মতো স্বয়ংক্রিয় ডকুমেন্টেশন টুল পড়ে নেয়, এবং ফাংশন, মেথড বা ক্লাসের নামের উপর বিশেষ অ্যাট্রিবিউট .__doc__ কল করে এগুলো পাওয়া যায়।
ডকস্ট্রিংয়ের সাধারণ রীতিগুলো PEP257-এ দেওয়া আছে, তবে সঠিক ফরম্যাট প্রকল্প ও দল অনুযায়ী ভিন্ন হয়।
Exercism-এর কনসেপ্ট অনুশীলনীগুলো টাইপ হিন্ট ছাড়া কোডের জন্য Google স্টাইল অনুসরণ করার চেষ্টা করে।
ডকস্ট্রিং হালকা ইউনিট টেস্ট হিসেবেও কাজ করতে পারে, যা পরের একটি অনুশীলনীতে আলোচনা করা হবে।
# An example on a user-defined function using a Google style docstring.
>>> def raise_to_power(number, power):
"""Raise a number to an arbitrary power.
Parameters:
number (int): The base number.
power (int): The power to raise the base number to.
Returns:
int: The number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.
"""
return number ** power
...
# Calling the .__doc__ attribute of the function and printing the result.
>>> print(raise_to_power.__doc__)
Raise a number to an arbitrary power.
Parameters:
number (int): The base number.
power (int): The power to raise the base number to.
Returns:
int: The number raised to the specified power.
Takes a number and raises it to the specified power, returning the result.
আপনার প্রিয় রান্নার বই থেকে চমৎকার একটি লাসাগনা রান্না করতে সাহায্য করার জন্য আপনি কিছু কোড লিখবেন।
আপনার পাঁচটি কাজ আছে, প্রত্যেকটিই আপনার রেসিপি রান্নার সঙ্গে সম্পর্কিত।
স্টাব ফাইলে আমরা আপনার জন্য প্রথম ফাংশন ডেফিনিশনটি শুরু করে দিয়েছি, তবে বাকি ফাংশন ডেফিনিশনগুলো আপনাকেই লিখতে হবে। যেকোনো কনস্ট্যান্টও আপনাকে নিজেই ডিফাইন করতে হবে। স্টাব ফাইলের #TODO কমেন্ট লাইনগুলো মনোযোগ দিয়ে পড়ুন। কোনো কাজ শেষ হয়ে গেলে ওই TODO কমেন্টটি মুছে ফেলুন।
EXPECTED_BAKE_TIME কনস্ট্যান্টটি ডিফাইন করুন, যা বোঝায় লাসাগনাটি ওভেনে কত মিনিট বেক হতে হবে।
আপনার রান্নার বই অনুযায়ী লাসাগনাটি ৪০ মিনিট ওভেনে থাকা উচিত:
>>> print(EXPECTED_BAKE_TIME)
40
bake_time_remaining() ফাংশনটি সম্পূর্ণ করুন। এটি আর্গুমেন্ট হিসেবে লাসাগনাটি এখন পর্যন্ত ওভেনে কত মিনিট ছিল তা নেয় এবং EXPECTED_BAKE_TIME কনস্ট্যান্টের ভিত্তিতে রিটার্ন করে লাসাগনাটিকে আর কত মিনিট বেক করতে হবে।
>>> bake_time_remaining(30)
10
preparation_time_in_minutes() ফাংশনটি ডিফাইন করুন। এটি আর্গুমেন্ট হিসেবে লাসাগনায় আপনি যে number_of_layers যোগ করতে চান তা নেয় এবং সেগুলো তৈরি করতে আপনার কত মিনিট লাগবে তা রিটার্ন করে।
ধরে নিন প্রতিটি স্তর প্রস্তুত করতে ২ মিনিট লাগে।
>>> def preparation_time_in_minutes(number_of_layers):
...
...
>>> preparation_time_in_minutes(2)
4
elapsed_time_in_minutes() ফাংশনটি ডিফাইন করুন, যা আর্গুমেন্ট হিসেবে দুটি প্যারামিটার নেয়:
number_of_layers (লাসাগনায় যোগ করা স্তরের সংখ্যা)elapsed_bake_time (লাসাগনাটি ইতিমধ্যে ওভেনে বেক হতে যত মিনিট লেগেছে)।এই ফাংশনটি রিটার্ন করবে রান্নাঘরে রান্না করতে আপনার মোট কত মিনিট লেগেছে: স্তর সাজানোর প্রস্তুতির সময় + লাসাগনাটি ওভেনে বেক হতে যত সময় লেগেছে।
>>> def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
...
...
>>> elapsed_time_in_minutes(3, 20)
26
রেসিপিটি আবার দেখে নিন এবং ফাংশন ডকস্ট্রিং আকারে "নোট" যোগ করুন।
def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
"""Calculate the elapsed cooking time.
Parameters:
number_of_layers (int): The number of layers in the lasagna.
elapsed_bake_time (int): Time the lasagna has been baking in the oven.
Returns:
int: The total time elapsed (in minutes) preparing and baking.
This function takes two integers representing the number of lasagna
layers and the time already spent baking the lasagna. It calculates
the total elapsed minutes spent cooking (preparing + baking).
"""
Exercism-এ সাইন আপ করুন, Python ট্র্যাকের 17টি কনসেপ্ট146টি অনুশীলনী আর সত্যিকারের মানুষের মেন্টরিং দিয়ে শিখুন ও দক্ষ হয়ে উঠুন, সম্পূর্ণ বিনামূল্যে।