למד טקסטים של פיתון תוך חמש דקות

קורס התרסקות בנושא טיפוס-טקסט והסבה מסוג בפייתון באופן מאוד לא-מילולי

TypeCasting

תהליך המרת סוג נתונים אחד לסוג נתונים אחר נקרא Typecasting או Type כפייה או Conversion Type .

הנושאים עליהם אתמקד במאמר זה הם:

  1. המרה מסוג מרומז
  2. המרה מסוג מפורש
  3. יתרונות
  4. חסרונות

המרה מסוג מרומז

כאשר המרת הסוג מתבצעת באופן אוטומטי על ידי המתורגמן ללא התערבות המתכנת, סוג זה של המרה מכונה המרה מסוג מרומז .

תוכנית לדוגמא:

myInt = 143 # Integer value.myFloat = 1.43 # Float value.
myResult = myInt + myFloat # Sum result
print("datatype of myInt:",type(myInt))print("datatype of myFloat:",type(myFloat))
print("Value of myResult:",myResult)print("datatype of myResult:",type(myResult))

תְפוּקָה:

התפוקה של התוכנית לעיל תהיה:

datatype of myInt: datatype of myFloat: Value of myResult: 144.43datatype of myResult: 

בתכנית לעיל,

  • אנו מוסיפים שני משתנים myInt ו- myFloat, ושומרים את הערך ב- myResult.
  • נבחן את סוג הנתונים של כל שלושת האובייקטים בהתאמה.
  • בפלט אנו יכולים לראות את סוג הנתונים של myInt הוא an integer, את סוג הנתונים של myFloat הוא a float.
  • כמו כן, אנו יכולים לראות כי ל- myFloat יש floatסוג נתונים מכיוון שפייתון ממיר סוג נתונים קטן יותר לסוג נתונים גדול יותר כדי למנוע אובדן נתונים.

סוג המרה זה נקרא המרה מסוג Implicit (או) UpCasting .

המרה מסוג מפורש

בהמרה מסוג מפורש, משתמשים ממירים את סוג הנתונים של אובייקט לסוג הנתונים הנדרש. אנו משתמשים בפונקציות מובנות מוגדרות מראש כמו:

  1. int ()
  2. לָצוּף()
  3. מורכב()
  4. בול ()
  5. str ()

התחביר להמרה מסוג מפורש הוא:

(required_datatype)(expression)

סוג זה של המרה נקרא המרה מסוג מפורש (או) DownCasting .

המרה אינטנסיבית

אנו יכולים להשתמש בפונקציה זו כדי להמיר ערכים מסוגים אחרים ל- int.

לדוגמה:

>>> int(123.654)123
>>>int(False)0
>>> int("10")10
>>> int("10.5")ValueError: invalid literal for int() with base 10: '10.5'
>>> int("ten")ValueError: invalid literal for int() with base 10: 'ten'
>>> int("0B1111")ValueError: invalid literal for int() with base 10: '0B1111'
>>> int(10+3j)TypeError: can't convert complex to int

הערה:

  1. אינך יכול להמיר סוג נתונים מורכב ל- int
  2. אם ברצונך להמיר סוג מחרוזת לסוג int, מילולית המחרוזת חייבת להכיל את הערך ב- Base-10

המרה לצוף

פונקציה זו משמשת להמרת כל סוג נתונים למספר נקודה צפה.

לדוגמה:

>>> float(10) 10.0
>>> float(True)1.0
>>> float(False)0.0
>>> float("10")10.0
>>> float("10.5")10.5
>>> float("ten")ValueError: could not convert string to float: 'ten'
>>> float(10+5j)TypeError: can't convert complex to float
>>> float("0B1111")ValueError: could not convert string to float: '0B1111'

הערה:

  1. אתה יכול להמיר סוג מורכב לערך סוג צף.
  2. אם ברצונך להמיר סוג מחרוזת לסוג float, מילולית המחרוזת חייב להכיל את הערך בבסיס 10.

המרה מורכבת

פונקציה זומשמש להמרת מספרים ממשיים למספר מורכב (אמיתי, דמיוני).

טופס 1: מורכב (x)

אתה יכול להשתמש בפונקציה זו כדי להמיר ערך בודד למספר מורכב עם חלק x אמיתי וחלק 0 דמיוני.

לדוגמה:

>>> complex(10)10+0j
>>> complex(10.5)10.5+0j
>>> complex(True)1+0j
>>> complex(False)0+0j
>>> complex("10")10+0j
>>> complex("10.5")10.5+0j
>>> complex("ten")ValueError: complex() arg is a malformed string

טופס 2: מורכב (x, y)

אם אתה רוצה להמיר את X ו- Y למספר מורכב כך ש- X יהיה חלק אמיתי ו- Y יהיה חלק דמיוני.

לדוגמה:

>>> complex(10,-2)10-2j
>>> complex(True, False)1+0j

המרה בוליאנית

פונקציה זו משמשת להמרת כל סוג נתונים לסוג נתונים בוליאני בקלות. זהו סוג הנתונים הגמיש ביותר בפייתון.

לדוגמה:

>>> bool(0)False
>>> bool(1)True
>>> bool(10)True
>>> bool(0.13332)True
>>> bool(0.0)False
>>> bool(10+6j)True
>>> bool(0+15j)True
>>> bool(0+0j)False
>>> bool("Apple")True
>>> bool("")False
הערה: בעזרת פונקציית bool תוכלו להמיר כל סוג של סוג נתונים לבוליאני והפלט יהיה - עבור כל הערכים הוא יפיק True למעט 0, 0 + 0j ולמחרוזת ריקה.

המרת מחרוזות

פונקציה זו משמשת להמרת כל סוג לסוג מחרוזת.

לדוגמה:

>>> str(10)'10'
>>> str(10.5)'10.5'
>>> str(True)'True'
>>> str(False)'False'
>>> str(10+5j)'10+5j'
>>> str(False)'False'

תוכנית לדוגמא:

integer_number = 123 # Intstring_number = "456" # String
print("Data type of integer_number:",type(integer_number))print("Data type of num_str before Type Casting:",type(num_str))
string_number = int(string_number)print("Data type of string_number after Type Casting:",type(string_number))
number_sum = integer_number + string_number
print("Sum of integer_number and num_str:",number_sum)print("Data type of the sum:",type(number_sum))

תְפוּקָה:

כאשר אנו מריצים את התוכנית לעיל הפלט יהיה:

Data type of integer_number: Data type of num_str before Type Casting: Data type of string_number after Type Casting: Sum of integer_number and num_str: 579Data type of the sum: 

בתכנית לעיל,

  • אנו מוסיפים מחרוזת_מספר ומשתנה שלם_מספר.
  • המירו מחרוזת_מספר ממחרוזת (גבוהה יותר) לסוג שלם (תחתון) בעזרת int()פונקציה לביצוע הוספה.
  • After converting string_number to an integer value Python adds these two variables.
  • We got the number_sum value and data type to be an integer.

Advantages Of Typecasting

  1. More convenient to use

Disadvantages Of Typecasting

  1. More complex type system
  2. Source of bugs due to unexpected casts

I covered pretty much everything that is required to perform any type of typecasting operation in Python3.

Hope this helped you learn about Python Typecasting in a quick and easy way.

If you liked this article please click on the clap and leave me your valuable feedback.