Displaying Information Professionally in Python
The Complete Python Bootcamp: Go from Zero to Hero - Part 08
গত লেখায় আমরা Type Conversion সম্পর্কে শিখেছি। এখন আমরা User-এর কাছ থেকে Input নিতে পারি। সেই Input প্রয়োজন অনুযায়ী Number-এ রূপান্তর করতে পারি।
কিন্তু একটি গুরুত্বপূর্ণ প্রশ্ন এখনো বাকি আছে।
আমরা User-কে তথ্য দেখাব কীভাবে?
ধরুন আপনি
একটি Banking Application তৈরি করছেন।
অথবা একটি Student Management System।
অথবা একটি E-Commerce Website।
সব ক্ষেত্রেই User-কে তথ্য দেখাতে হবে।
Programming-এর ভাষায় এই তথ্য প্রদর্শনের প্রক্রিয়াকে বলা হয় Output।
Python-এ Output দেখানোর জন্য সবচেয়ে বেশি ব্যবহৃত Function হলো:
print()Your First Output
চলুন একটি সহজ উদাহরণ দেখি।
print("Hello Python")Output:
Hello Pythonএখানে Python Screen-এ Text Display করেছে। এটাই Output-এর সবচেয়ে সহজ উদাহরণ।
Printing Variables
আমরা শুধু Text না, Variable-এর Value-ও Display করতে পারি।
name = "Hasan"
print(name)Output:
HasanDisplaying Multiple Values
name = "Hasan"
country = "Bangladesh"
print(name, country)Output:
Hasan Bangladeshখেয়াল করুন। Python স্বয়ংক্রিয়ভাবে দুটি Value-এর মাঝে Space যোগ করেছে।
Using the sep Parameter
ধরুন আমরা Space-এর পরিবর্তে অন্য Separator ব্যবহার করতে চাই।
print("Python", "Java", "C++", sep=" | ")Output:
Python | Java | C++এখানে:
sepমানে Separator
Using the end Parameter
সাধারণত print() শেষে নতুন লাইনে চলে যায়।
print("Hello")
print("World")Output:
Hello
Worldকিন্তু আমরা চাইলে এটি পরিবর্তন করতে পারি।
print("Hello", end=" ")
print("World")Output:
Hello WorldIntroducing f-Strings
Programming-এ সবচেয়ে জনপ্রিয় Output Formatting Technique হলো:
f-stringউদাহরণ:
name = "Hasan"
print(f"Hello {name}")Output:
Hello HasanWhy Are f-Strings Useful?
ধরুন:
name = "Hasan"
country = "Bangladesh"আমরা লিখতে পারি:
print(f"My name is {name} and I live in {country}.")Output:
My name is Hasan and I live in Bangladesh.এটি Readable, Professional এবং Modern Python Style।
Real-Life Example
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}!")
print(f"Next year you will be {age + 1} years old.")Input:
Hasan
28Output:
Hello Hasan!
Next year you will be 29 years old.Common Beginner Mistakes
Forgetting the f
Wrong:
name = "Hasan"
print("Hello {name}")Output:
Hello {name}Correct:
print(f"Hello {name}")Output:
Hello HasanPractice Time
Exercise 1
name = input("Enter your name: ")
print(f"Welcome {name}")Exercise 2
country = input("Enter your country: ")
print(f"You are from {country}")Exercise 3
age = int(input("Enter your age: "))
print(f"After 5 years you will be {age + 5}")