পাইথন প্রোগ্রামিং/চলক ও স্ট্রিং

উইকিবই থেকে


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

চলক[সম্পাদনা]

চলক (ইংরেজি: Variable) কিছু একটা মান সংরক্ষণ করে রাখে যে মান পরিবর্তন হতে পারে। সহজভাবে বললে চলক হলো একটা বাক্স যেখানে আপনি কিছু রাখতে পারবেন। আপনি চলকে সকল ধরণের জিনিসই রাখতে পারেন, কিন্তু আপাতত আমরা চলকে শুরু সংখ্যা রাখবো।

lucky = 7
print (lucky)
7

এই কোডে একটি চলক তৈরি করা হয়েছে যার নাম lucky এবং সেখানে 7 সংখ্যাটি অ্যাসাইন করা হয়েছে, অর্থাৎ সংরক্ষণ করা হয়েছে। যখন আমরা পাইথনকে বলি lucky চলকে কী সংরক্ষণ করা আছে তখন পাইথন এই সংখ্যাটি আবার আমাদের ফেরত দেয়।

চলকের ভিতরে কী থাকবে তা আমরা পরিবর্তন করতে পারি। যেমন:

changing = 3                                   
print (changing)
3

changing = 9
print (changing)
9

different = 12
print (different)
12
print (changing)
9

changing = 15
print (changing)
15

এখানে আমরা changing নামে একটি চলক তৈরি করেছি এবং সেখানে 3 রেখে পরীক্ষা করে দেখেছি ঠিকমতো মান রাখা হয়েছে কিনা। এরপরে আমরা changing-এ 9 রেখেছি এবং আবার জানতে চেয়েছি কী সংরক্ষণ করে রাখা হয়েছে। পাইথন 3 মুছে ফেলেছে এবং সেখানে 9 সংরক্ষণ করেছে। তারপরে আমরা আরও একটি ভেরিয়েবল different তৈরি করেছি এবং সেখানে 12 রেখেছি। এখন আমাদের কাছে differentchanging নামের দুইটি স্বাধীন চলক রয়েছে এবং এরা দুইটি ভিন্ন মান সংরক্ষণ করে রেখেছে। একটি মান পরিবর্তন অন্যটিকে প্রভাবিত করছে না।

আপনি চাইলে একটি চলকের মান অন্য চলকে অ্যাসাইন করতে পারেন:

red = 5
blue = 10
print (red, blue)
5 10

yellow = red
print (yellow, red, blue)
5 5 10

red = blue
print (yellow, red, blue)
5 10 10

এই কোডটি বুঝতে চাইলে মনে রাখুন যে চলকের নাম সবসময় সমান চিহ্নের (অ্যাসাইনমেন্ট অপারেটর) বামে থাকে আর তার মান না ভ্যালু সমান চিহ্নের ডানে থাকে। শুরুতে নাম, তারপরে মান।

আমরা শুরুতে ডিক্লেয়ার করেছি যে red এর ভ্যালু 5 এবং blue এর ভ্যালু 10। আপনি হয়তো লক্ষ্য করেছেন যে আপনি print কে একাধিক আর্গুমেন্ট দিতে পারেন। এটি সবগুলো আর্গুমেন্ট স্পেস দ্বারা আলাদা করে একই লাইনে প্রিন্ট করবে। আমাদের আশানুরূপভাবেই পাইথন বলেছে যে red সংরক্ষণ করে রেখেছে 5 এবং blue রেখেছে 10

এবারে আমরা ৩য় চলক yellow তৈরি করলাম। এর মান ঠিক করার সময় আমরা পাইথনকে বলেছি red এর ভ্যালু যা রয়েছে yellow এর ভ্যালুও তাই ই হবে। (মনে রাখবেন: বামে নাম আর ডানে তার মান।) পাইথন জানে যে red এর মান 5, তখন এটি yellow এর মানও 5 সেট করে দেয়।

এবার আমরা red চলকটি নিলাম এবং সেখানে blue এর মান সেট করলাম। দ্বীধায় পড়বেন না — বাম নাম এবং ডানে তার মান। পাইথন blue এর মান খোঁজ করে এবং দেখে তা 10। তাই, পাইথন red-এর পুরাতন মান (5) মুছে ফেলে এবং সেখানে 10 সেট করে দেয়। এই অ্যাসাইনমেন্টের পরে পাইথন দেখালো যে yellow হলো 5, red হলো 10 আর blue হলো 10

But didn't we say that yellow should be whatever value red is? The reason that yellow is still 5 when red is 10, is because we only said that yellow should be whatever red is at the moment of the assignment. After Python has figured out what red is and assigned that value to yellow, yellow doesn't care about red any more. yellow has a value now, and that value is going to stay the same no matter what happens to red.

টীকা:
The interplay between different variables in Python is, in fact, more complex than explained here. The above example works with integer numbers and with all other basic data types built into Python; the behavior of lists and dictionaries (you will encounter these complex data types later) is entirely different, though. You may read the chapter on data types for a more detailed explanation of what variables really are in Python and how their type affects their behavior. However, it is probably sufficient for now if you just keep this in mind: whenever you are declaring variables or changing their values, you always write the name of the variable on the left of the equals sign (the assignment operator), and the value you wish to assign to it on the right.

স্ট্রিং[সম্পাদনা]

A 'string' is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example, "hello" is a string. It is five characters long — h, e, l, l, o. Strings can also have spaces: "hello world" contains 11 characters: 10 letters and the space between "hello" and "world". There are no limits to the number of characters you can have in a string — you can have anywhere from one to a million or more. You can even have a string that has 0 characters, which is usually called an "empty string."

There are three ways you can declare a string in Python: single quotes ('), double quotes ("), and triple quotes ("""). In all cases, you start and end the string with your chosen string declaration. For example:

>>> print ('I am a single quoted string')
I am a single quoted string
>>> print ("I am a double quoted string")
I am a double quoted string
>>> print ("""I am a triple quoted string""")
I am a triple quoted string

You can use quotation marks within strings by placing a backslash directly before them, so that Python knows you want to include the quotation marks in the string, instead of ending the string there. Placing a backslash directly before another symbol like this is known as escaping the symbol.

>>> print ("So I said, \"You don't know me! You'll never understand me!\"")
So I said, "You don't know me! You'll never understand me!"
>>> print ('So I said, "You don\'t know me! You\'ll never understand me!"')
So I said, "You don't know me! You'll never understand me!"
>>> print ("""The double quotation mark (\") is used to indicate direct quotations.""")
The double quotation mark (") is used to indicate direct quotations.

If you want to include a backslash in a string, you have to escape said backslash. This tells Python that you want to include the backslash in the string, instead of using it as an escape character. For example:

>>> print ("This will result in only three backslashes: \\ \\ \\")
This will result in only three backslashes: \ \ \

As you can see from the above examples, only the specific character used to quote the string needs to be escaped. This makes for more readable code.

To see how to use strings, let's go back for a moment to an old, familiar program:

>>> print("Hello, world!")
Hello, world!

Look at that! You've been using strings since the very beginning!

You can add two strings together using the + operator: this is called concatenating them.

>>> print ("Hello, " + "world!")
Hello, world!

Notice that there is a space at the end of the first string. If you don't put that in, the two words will run together, and you'll end up with Hello,world!

You can also repeat strings by using the * operator, like so:

>>> print ("bouncy " * 5)
bouncy bouncy bouncy bouncy bouncy 
>>> print ("bouncy " * 10)
bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy

The string bouncy gets repeated 5 times in the 1st example and 10 times in the 2nd.

If you want to find out how long a string is, you use the len() function, which simply takes a string and counts the number of characters in it. (len stands for "length.") Just put the string that you want to find the length of inside the parentheses of the function. For example:

>>> print (len("Hello, world!"))
13

Strings and Variables[সম্পাদনা]

Now that you've learned about variables and strings separately, let's see how they work together.

Variables can store much more than just numbers. You can also use them to store strings! Here's how:

question = "What did you have for lunch?"
print (question)
What did you have for lunch?

In this program, we are creating a variable called question, and storing the string "What did you have for lunch?" in it. Then, we just tell Python to print out whatever is inside the question variable. Notice that when we tell Python to print out question, there are no quotation marks around the word question: this tells Python that we are using a variable, not a string. If we put in quotation marks around question, Python would treat it as a string, as shown below:

question = "What did you have for lunch?"
print ("question")
question

Let's try something different. Sure, it's all fine and dandy to ask the user what they had for lunch, but it doesn't make much difference if they can't respond! Let's edit this program so that the user can type in what they ate.

question = "What did you have for lunch?"
print (question)
answer = raw_input() #You should use "input()" in python 3.x, because python 3.x doesn't have a function named "raw_input".

print ("You had " + answer + "! That sounds delicious!")

To ask the user to write something, we used a function called raw_input(), which waits until the user writes something and presses enter, and then returns what the user wrote. Don't forget the parentheses! Even though there's nothing inside of them, they're still important, and Python will give you an error if you don't put them in. You can also use a different function called input(), which works in nearly the same way. We will learn the differences between these two functions later.

মন্তব্য:
In Python 3.x raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it without the trailing newline. It raises EOFError if the input is terminated prematurely (e.g. by pressing Ctrl+D). To get the old behavior of input(), use eval(input()).

In this program, we created a variable called answer, and put whatever the user wrote into it. Then, we print out a new string, which contains whatever the user wrote. Notice the extra space at the end of the "You had " string, and the exclamation mark at the start of the "! That sounds delicious!" string. They help format the output and make it look nice, so that the strings don't all run together.

Combining Numbers and Strings[সম্পাদনা]

Take a look at this program, and see if you can figure out what it's supposed to do.

print ("Please give me a number: ")
number = raw_input()

plusTen = number + 10
print ("If we add 10 to your number, we get " + plusTen)

This program should take a number from the user, add 10 to it, and print out the result. But if you try running it, it won't work! You'll get an error that looks like this:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print "If we add 10 to your number, we get " + plusTen
TypeError: cannot concatenate 'str' and 'int' objects

What's going on here? Python is telling us that there is a TypeError, which means there is a problem with the types of information being used. Specifically, Python can't figure out how to reconcile the two types of data that are being used simultaneously: integers and strings. For example, Python thinks that the number variable is holding a string, instead of a number. If the user enters 15, then number will contain a string that is two characters long: a 1, followed by a 5. So how can we tell Python that 15 should be a number, instead of a string?

Also, when printing out the answer, we are telling Python to concatenate together a string ("If we add 10 to your number, we get ") and a number (plusTen). Python doesn't know how to do that—it can only concatenate strings together. How do we tell Python to treat a number as a string, so that we can print it out with another string?

Luckily, there are two functions that are perfect solutions for these problems. The int() function will take a string and turn it into an integer, while the str() function will take an integer and turn it into a string. In both cases, we put what we want to change inside the parentheses. Therefore, our modified program will look like this:

print ("Please give me a number:",)
response = raw_input()

number = int(response) 
plusTen = number + 10

print ("If we add 10 to your number, we get " + str(plusTen))

মন্তব্য:
Another way of doing the same is to add a comma after the string part and then the number variable, like this:

print ("If we add 10 to your number, we get ", plusTen)

or use special print formatting like this:

print ("If we add 10 to your number, we get %s" % plusTen)

which alternative can be written this way, if you have multiple inputs:

plusTwenty = number + 20
print ("If we add 10 and 20 to your number, we get %s and %s" % (plusTen, plusTwenty))

or use format()

print ("If we add 10 to your number, we get {0}".format(plusTen))

That's all you need to know about strings and variables! We'll learn more about types later.

শেখা ফাংশনসমূহের তালিকা[সম্পাদনা]

  • print(): আউটপুটের তথ্য প্রিন্ট করে।
  • input() অথবা raw_input(): ব্যবহারকারীর কাছে ইনপুট চায় এবং তা রিটার্ন করে (লক্ষ্য করুন যে ৩.ক সংস্করণে raw_input() নেই, তার পরিবর্তে input() ব্যবহার করুন)।
  • len(): স্ট্রিং-এর দৈর্ঘ্য (অক্ষর সংখ্যা) রিটার্ন করে।
  • str(): অবজেক্টকে স্ট্রিং এ রূপান্তর করে।
  • int(): একটি স্ট্রিং বা সংখ্যা দিলে একটি পূর্ণসংখ্যা রিটার্ন করে।

টীকা:

  1. input and raw_input function accept a string as parameter. This string will be displayed on the prompt while waiting for the user input.
  2. The difference between the two is that raw_input accepts the data coming from the input device as a raw string, while input accepts the data and evaluates it into python code. This is why using input as a way to get a user string value returns an error because the user needs to enter strings with quotes.

It is recommended to use raw_input at all times and use the int function to convert the raw string into an integer. This way we do not have to bother with error messages until the error handling chapter and will not make a security vulnerability in your code.

অনুশীলনী[সম্পাদনা]

  1. একটি প্রোগ্রাম লিখুন যেটি ব্যবহারকারীকে একটি স্ট্রিং দিতে বলবে এবং তারপরে সেই স্ট্রিং-এর দৈর্ঘ্য প্রিন্ট করবে।
  2. ব্যবহারকারীকে একটি স্ট্রিং এবং একটি সংখ্যা দিতে বলবে এবং সেই সংখ্যাকবার ঐ স্ট্রিং প্রিন্ট করবে (উদাহরণস্বরূপ, যদি স্ট্রিংটি হয় hello এবং সংখ্যাটি হয় 3 তাহলে প্রোগ্রাম hello hello hello প্রিন্ট করবে)।
  3. যদি আপনি কোনো সংখ্যা ইনপুট চান এবং কেউ ভুলে একটি শব্দ ইনপুট দেয় তাহলে কী হবে? চেষ্টা করে দেখুন।

সমাধান

Quiz