tg-me.com/kaizonova/898
Last Update:
Python3 Programming #post_7
### تحويل البيانات في بايثون (Python Casting)
#### تحديد نوع المتغير
في بعض الأحيان، قد ترغب في تحديد نوع معين لمتغير. يمكن القيام بذلك باستخدام التحويل. بايثون هي لغة موجهة للكائنات، وبالتالي تستخدم الفئات (Classes) لتعريف أنواع البيانات، بما في ذلك أنواعها الأساسية.
يتم تنفيذ التحويل في بايثون باستخدام دوال الإنشاء (Constructor Functions) التالية:
- **int()
** - تقوم بإنشاء عدد صحيح (Integer) من قيمة صحيحة، أو عدد عشري (مع إزالة الأجزاء العشرية)، أو من سلسلة نصية بشرط أن تمثل السلسلة عددًا صحيحًا.
- **float()
** - تقوم بإنشاء عدد عشري (Float) من قيمة صحيحة أو سلسلة نصية بشرط أن تمثل السلسلة عددًا عشريًا أو عددًا صحstr()*str()
** - تقوم بإنشاء سلسلة نصية (String) من مجموعة واسعة من أنواع البيانات مثل النصوص، الأعداد الصحيحة، والأعداد العشرية.
#### الأعداد الصحيحة (Integers):gers):**
x = int(1) # x سيكون 1
y = int(2.8) # y سيكون 2
z = int("3") # z سيكون 3
**الأعداد العشرية (Floats):**
x = float(1) # x سيكون 1.0
y = float(2.8) # y سيكون 2.8
z = float("3") # z سيكون 3.0
w = float("4.2") # w سيكون 4.2
**السلاسل النصية (Strings):**
x = str("s1") # x سيكون 's1'
y = str(2) # y سيكون '2'
z = str(3.0) # z سيكون '3.0'
#### تمرين:
ما هي نتيجة الكود التالي؟
print(int(35.88))
- 35
- 35.88
-Python CastingPython Casting**
#### Specify a Variable Type
There may be times when you want to specify a particular type for a variable. This cacastingsing **casting**. Python is an object-oriented language, and as such, it uses classes to define data types, including its primitive types.
Casting in Python is done using the following constructoint()ns:
- **
int()
** - constructs an integer from an integer literal, a float literal (by removing all decimals), or a string literal (as long as the string represents a wfloat()).- **
float()
** - constructs a float from an integer literal, a float literal, or a string literal (as long as the string represents a float orstr()er).- **
str()
** - constructs a string from a wide range of data types, including strings, integer literals, and float literals.Integers::
**Integers:**
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
**Floats:**
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
**Strings:**
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
#### Exercise:
What will be the result of the following code?
print(int(35.88))
- 35
- 35.88
- 36
:)
BY Kaizonova
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/kaizonova/898