কম্পিউটার টিউটোরিয়াল

পাইথনে নামপাই টাইপ প্রোমোশন নিয়ম প্রয়োগ করে ফলাফল টাইপ ফেরত দিন

numpy.result_type() মেথড নামপাই টাইপ প্রোমোশন (type promotion) নিয়ম প্রয়োগ করে আর্গুমেন্টগুলোর ফলাফল টাইপ ফেরত দেয়। নামপাই-তে টাইপ প্রোমোশন C++-এর নিয়মের মতো কাজ করে, কিছু পার্থক্যের সাথে। স্কেলার এবং অ্যারে উভয় থাকলে অ্যারের টাইপ প্রাধান্য পায় এবং স্কেলারের আসল মান বিবেচনা করা হয়।

প্যারামিটার

  • operands: যেসব অপারেশনের অপারেন্ড, তাদের ফলাফল টাইপ জানতে হবে

পদ্ধতি

লাইব্রেরি ইম্পোর্ট করুন

import numpy as np

result_type() মেথড দিয়ে টাইপ প্রোমোশন যাচাই করুন

print("Numpy-এ result_type() মেথড ব্যবহার\n")
print("ফলাফল...", np.result_type(2, np.arange(4, dtype='i1')))
print("ফলাফল...", np.result_type(5, 8))
print("ফলাফল...", np.result_type('i4', 'c8'))
print("ফলাফল...", np.result_type(3.8, 8))
print("ফলাফল...", np.result_type(5, 20.7))
print("ফলাফল...", np.result_type(-8, 20.7))
print("ফলাফল...", np.result_type(10.0, -4))

সম্পূর্ণ উদাহরণ

import numpy as np

print("Numpy-এ result_type() মেথড ব্যবহার\n")
print("ফলাফল...", np.result_type(2, np.arange(4, dtype='i1')))
print("ফলাফল...", np.result_type(5, 8))
print("ফলাফল...", np.result_type('i4', 'c8'))
print("ফলাফল...", np.result_type(3.8, 8))
print("ফলাফল...", np.result_type(5, 20.7))
print("ফলাফল...", np.result_type(-8, 20.7))
print("ফলাফল...", np.result_type(10.0, -4))

আউটপুট

Numpy-এ result_type() মেথড ব্যবহার
ফলাফল... int8
ফলাফল... int64
ফলাফল... complex128
ফলাফল... float64
ফলাফল... float64
ফলাফল... float64
ফলাফল... float64

ব্যাখ্যা:

  • 2 (int) + int8 array → অ্যারে টাইপ প্রাধান্য পায় → int8
  • 5, 8 (পাইথন int) → ডিফল্ট int64
  • 'i4' (int32) + 'c8' (complex64) → complex128
  • 3.8 (float) + 8 (int) → float64
  • 5 (int) + 20.7 (float) → float64
  • -8 (int) + 20.7 (float) → float64
  • 10.0 (float) + -4 (int) → float64
  1. পাইথন প্যান্ডাস - ইন্ডেক্স অবজেক্ট থেকে সাপেক্ষ ফ্রিকোয়েন্সি ফেরত দেওয়া

  2. পাইথন প্যান্ডাস - Timedelta অবজেক্ট থেকে সেকেন্ড ফেরত দেওয়া

  3. পাইথন প্যান্ডাস - Timedelta অবজেক্ট থেকে ন্যানোসেকেন্ড ফেরত দিন

  4. পাইথন প্যান্ডাস - Timedelta অবজেক্ট থেকে মাইক্রোসেকেন্ড ফেরত দিন