You are on page 1of 88

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..

… Author , Eng/ Mahmoud Soliman 1


‫جذول انًحتويات‬

‫انفصم األول ‪ :‬يقذية في أَواع انبياَات ‪3.................................... Introduction to Data types‬‬

‫انفصم انثاَي ‪ :‬انتحول بيٍ أَواع انبياَات ‪13....................... Type Conversion & Type Casting‬‬

‫انفصم انثانث ‪ :‬األرقاو ‪11................. ................................ ................................ Numbers‬‬

‫انفصم انزابع ‪ :‬انسالسم ‪22.................. ................................ ................................ Strings‬‬

‫انفصم انخايس ‪ :‬انقوائى ‪31..................... ................................ ................................ Lists‬‬

‫انفصم انسادس ‪ :‬انتعذيذ أو انًجًوعات ‪44................................. ................................ Tuples‬‬

‫انفصم انسابع ‪ :‬انتشكيالت أو انًجًوعات ‪66................................... ................................ Sets‬‬

‫انفصم انثايٍ ‪ :‬انقايوس ‪22............................................ ................................ Dictionary‬‬

‫انفصم انثانث ‪ :‬انقيى انًُطقية ‪13......................................... ................................ Booleans‬‬

‫انخاتًة ‪12 ................................................................................................‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪2‬‬
‫ميحرلا نمحرلا هللا‬ ‫بسم‬
‫انطزيق إنى انبزيجة بهغة انبايثوٌ‬
‫‪The Way to Python programming‬‬

‫انذرس األول ‪ :‬يقذية في أَواع انبياَات‬


‫)‪(Introduction to Data types‬‬
‫‪ ‬لجً اٌجذأ ف‪ ٟ‬أٔ‪ٛ‬اع اٌج‪١‬بٔبد ‪٠‬غت أْ ٕٔ‪ ٖٛ‬أْ أ‪ٚ‬ي دسع‪ ٓ١‬ف‪٘ ٟ‬ذزا اٌغذضت رذُ دساعذز‪ ُٙ‬فذ‪ ٟ‬اٌغذضت ا ‪ٚ‬ي‬
‫‪ٌٚ‬ىٓ ِٓ ثبة االعززوبس ع‪١‬زُ عشدُ٘ ِشح أخش‪ ٜ‬ف‪٘ ٟ‬زا اٌغضت صُ ٔم‪ َٛ‬ثذساعخ وً ٔ‪ٛ‬ع ثبٌزفص‪. ً١‬‬

‫‪ ‬ف‪٘ ٟ‬زا اٌذسط عززؼشف ػٍ‪ ٝ‬أٔ‪ٛ‬اع اٌج‪١‬بٔبد اٌّخزٍفخ اٌز‪ّ٠ ٟ‬ىٕه اعزخذاِ‪ٙ‬ب ف‪.Python ٟ‬‬

‫طب ٌٍغب‪٠‬خ ‪ٌٚ‬ىٕٗ ِ‪ ُٙ‬عذًا ِزبػ فذ‪ ٟ‬عّ‪١‬ذغ ٌغذبد اٌجشِغذخ رمش‪٠‬جًذب ‪٘ٚ‬ذ‪ِ ٛ‬ذب ‪٠‬غذّ‪ٝ‬‬ ‫دػ‪ٔٛ‬ب ٕٔبلش ِف‪ًِ ٛٙ‬ب ثغ‪ً ١‬‬
‫ػذذب ِذذٓ اٌج‪١‬بٔذذبد اٌزذذ‪ّ٠ ٟ‬ىٕذذه‬
‫أٔذذ‪ٛ‬اع اٌج‪١‬بٔذذبد ‪ٚ data types‬وّذذب ‪٠‬شذذ‪١‬ش اعذذّٗ ‪ّ٠‬ضذذً ٔذذ‪ٛ‬ع اٌج‪١‬بٔذذبد ٔ‪ً ٛ‬‬
‫ِؼبٌغز‪ٙ‬ب ثبعزخذاَ ثشٔذبِظ اٌىّج‪١‬ذ‪ٛ‬رش اٌخذب ثذه‪ .‬فمذذ ‪٠‬ىذ‪ٔ ْٛ‬ذ‪ٛ‬ع اٌج‪١‬بٔذبد سلّ‪ً١‬ذب ‪ numeric‬أ‪ٔ ٚ‬صذ‪١‬ب ً‬
‫‪ Strings‬أ‪ ٚ‬أثغذذذ‪ً٠‬ب ‪ alphanumeric‬أ‪ ٚ‬ػشذذش‪ً٠‬ب ‪ decimal‬ئٌ ذ ‪ .‬دػٕذذب ٔذذذع ثشِغذذخ اٌىّج‪١‬ذذ‪ٛ‬رش عبٔج ذب ً‬
‫ٌجؼط اٌ‪ٛ‬لذ ‪ٔٚ‬أخز ثؼط ا ِضٍخ اٌغ‪ٍٙ‬خ ‪:‬‬
‫‪ِ ‬ضبي ‪ 1‬إلظبفخ ػذد‪ ٓ٠‬صؾ‪١‬ؾ‪ّ٠ 20 & 10 ٓ١‬ىٓ اٌم‪١‬بَ ثٗ ثجغبغخ ػٍ‪ ٝ‬إٌؾ‪ ٛ‬اٌزبٌ‪: ٟ‬‬
‫‪10 + 10‬‬
‫اٌّضبي ا ‪ٚ‬ي ‪٠‬زؼبًِ ِغ ا ػذاد اٌصؾ‪١‬ؾخ ‪. whole numbers‬‬

‫‪ِ ‬ضبي ‪ 2‬إلظبفخ ػذد‪ ٓ٠‬ػشش‪ّ٠ 10.50 & 20.50 ٓ١٠‬ىٓ اٌم‪١‬بَ ثٗ ثجغبغخ ػٍ‪ ٝ‬إٌؾ‪ ٛ‬اٌزبٌ‪: ٟ‬‬
‫‪10.50 + 20.50‬‬
‫اٌّضبي اٌضبٔ‪٠ ٟ‬زؼبًِ ِغ ا ػذاد اٌؼشش‪٠‬خ ‪. decimal numbers‬‬

‫‪ِ ‬ضبي ‪ٌ 3‬زغغ‪ِ ً١‬ؼٍ‪ِٛ‬بد اٌطبٌت ف‪ ٟ‬دفزش ِالؽظبد ا‪٢‬ر‪ٕ٘ ٟ‬ب ٔ‪ٛ‬د رغغ‪ ً١‬اٌّؼٍ‪ِٛ‬بد اٌزبٌ‪١‬خ ‪:‬‬
‫‪Name:‬‬
‫‪Class:‬‬
‫‪Section:‬‬
‫‪Age:‬‬
‫‪Sex:‬‬

‫ً‬
‫عغال ‪ٚ record‬اؽذًا ٌٍطبٌت ‪ٚ‬فمًب ٌٍّزطٍجبد اٌّؾذدح ف‪ ٟ‬اٌغذ‪ٚ‬ي اٌغبثك ‪:‬‬ ‫دػٕب ٔعغ‬
‫‪Name: Zara Ali‬‬
‫‪Class: 6th‬‬
‫‪Section: J‬‬
‫‪Age: 13‬‬
‫‪Sex: F‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪3‬‬
‫‪ ‬اٌّضبي اٌضبٌش ‪٠‬زؼبًِ ِغ ِض‪٠‬ظ ِٓ اٌج‪١‬بٔبد اٌّخزٍفخ ‪ّ٠‬ىٓ أْ ٔ‪ٛ‬ظؾ‪ٙ‬ب ػٍ‪ ٝ‬إٌؾ‪ ٛ‬اٌزبٌ‪: ٟ‬‬
‫عب عٍغٍخ ٔص‪١‬خ ‪. String‬‬ ‫‪ o‬اعُ اٌطبٌجخ "‪ ٛ٘ "Zara Ali‬عٍغٍخ ِٓ ا ؽشف رغّ‪ ٝ‬أ‪ً ٠‬‬
‫‪ o‬فصذً اٌطذالة "‪٠ "6th‬زىذ‪ِ ْٛ‬ذٓ ِذض‪٠‬ظ ِذٓ ػذذد صذؾ‪١‬ؼ ‪ٚ whole number‬عٍغذٍخ ‪ِ string‬ذٓ‬
‫ؽشف‪٠ٚ ٓ١‬غّ‪٘ ٝ‬زا اٌّض‪٠‬ظ ا ثغذ‪ ٞ‬اٌشلّ‪. alphanumeric ٟ‬‬
‫‪ o‬لغُ اٌطالة "‪ ٛ٘ "J‬ؽشف ‪ٚ‬اؽذ ‪. single character‬‬
‫‪ o‬ػّش اٌطالة "‪ ٛ٘ "13‬سلُ ‪ٚ‬اؽذ صؾ‪١‬ؼ ‪. whole number‬‬
‫‪ٛٔ o‬ع اٌطالة "‪ ٛ٘ "F‬ؽشف ‪ٚ‬اؽذ ‪. single character‬‬

‫‪ ‬ث‪ٙ‬زٖ اٌطش‪٠‬مخ أدسوٕب أٔٗ ف‪ ٟ‬ؽ‪١‬برٕب اٌ‪١ِٛ١‬خ إٔٔب ٕٔزؼبًِ ِغ أٔ‪ٛ‬اع ِخزٍفخ ِٓ اٌج‪١‬بٔبد ِضً ‪:‬‬
‫‪ o‬اٌغالعً ‪. strings‬‬
‫‪ o‬ا ؽشف ‪. characters‬‬
‫‪ o‬ا سلبَ اٌصؾ‪١‬ؾخ ‪ whole numbers‬أ‪ ٚ‬ا ػذاد اٌصؾ‪١‬ؾخ )‪. (integers‬‬
‫‪ o‬ا سلبَ اٌؼشش‪٠‬خ ‪ decimal numbers‬أ‪ ٚ‬أسلبَ اٌفبصٍخ اٌؼبئّخ )‪.(floating point numbers‬‬
‫‪ٚ‬ثبٌّضذً وذذً ل‪ّ١‬ذذخ ‪ value‬فذذ‪ٌٙ python ٟ‬ذذب ٔذذ‪ٛ‬ع ث‪١‬بٔذذبد ‪ٔٚ‬ظذ ًذشا ْ وذذً شذذ‪ٟ‬ت فذذ‪ ٟ‬ثشِغذذخ ‪Python‬‬
‫ػجبسح ػٓ وبئٓ ‪ object‬فذاْ أٔذ‪ٛ‬اع اٌج‪١‬بٔذبد ٘ذ‪ ٟ‬فذ‪ ٟ‬اٌ‪ٛ‬الذغ فئذبد ‪ٚ classes‬اٌّزغ‪١‬ذشاد ‪variables‬‬
‫اٌز‪ٔ ٟ‬غزخذِ‪ٙ‬ب ٘‪ ٟ‬ػجبسح ػٓ ِض‪ ً١‬أ‪ّٛٔ ٚ‬صط ‪( instance‬وبئٓ ‪٘ ِٓ ) object‬زٖ اٌفئبد ‪. classes‬‬
‫ػٕذِب ٔىزت ثشٔبِظ وّج‪ٛ١‬رش ٌّؼبٌغخ أٔ‪ٛ‬اع ِخزٍفخ ِٓ اٌج‪١‬بٔبد ٔؾزبط ئٌ‪ ٝ‬رؾذ‪٠‬ذذ ٔذ‪ٛ‬ع اٌج‪١‬بٔذبد اٌّذشاد‬
‫رخض‪ٕ٠‬ذذٗ ث‪ٛ‬ظذذ‪ٛ‬ػ ؛ ‪ٚ‬ئال فذذاْ اٌىّج‪١‬ذذ‪ٛ‬رش ال ‪٠‬ف‪ٙ‬ذذُ و‪ ١‬ذ ‪ّ٠‬ىذذٓ ئعذذشات ػٍّ‪١‬ذذبد ِخزٍفذذخ ػٍذذ‪ ٝ‬رٍذذه اٌج‪١‬بٔذذبد‬
‫اٌّؾذدح ‪ٚ‬رغذزخذَ ٌغذبد اٌجشِغذخ اٌّخزٍفذخ وٍّذبد سئ‪١‬غذ‪١‬خ ‪ِ keywords‬خزٍفذخ ٌزؾذ‪٠‬ذذ أٔذ‪ٛ‬اع ث‪١‬بٔذبد‬
‫ِخزٍفذذذخ‪ .‬فىذذذً ٌغذذذخ ثشِغ‪١‬ذذذخ ٌ‪ٙ‬ذذذب ‪ syntax‬خذذذب ث‪ٙ‬ذذذب ‪ٌٙٚ‬ذذذب وٍّذذذبد ِؾغذذذ‪ٛ‬صح ‪ٚ‬سئ‪١‬غذذذ‪١‬خ خبصذذذخ ث‪ٙ‬ذذذب‬
‫‪ٚ keywords‬سغُ ٘زا ف‪ٕٙ‬ذبن ٌغذبد ِضذً ٌغذخ ‪ٌ python‬غذخ رو‪١‬ذخ ال رؾزذبط ئٌذ‪ ٝ‬رؾذ‪٠‬ذذ ٔذ‪ٛ‬ع اٌج‪١‬بٔذبد‬
‫‪. data types‬‬
‫‪ٕ٘ ‬بن فئبد ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔبد داخً ٌغخ ثب‪٠‬ض‪٘ ِٓ ْٛ‬زٖ ا ٔ‪ٛ‬اع ا‪٢‬ر‪: ٟ‬‬
‫‪ : Python Numbers )1‬ا سلبَ داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ِجبششح ‪.‬‬
‫‪ : Python Strings )2‬اٌغالعً داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ث‪ ' ' ٓ١‬أ‪ " " ٚ‬أ‪''' ''' ٚ‬‬
‫‪ : Python List )3‬اٌم‪ٛ‬ائُ داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ث‪[ , , , ] ٓ١‬‬
‫‪ : Python Tuple )4‬اٌزؼذ‪٠‬ذ داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ث‪) , , , ( ٓ١‬‬
‫‪ : Python Set )5‬اٌّغّ‪ٛ‬ػخ داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ث‪{ , , , } ٓ١‬‬
‫‪ : Python Dictionary )6‬اٌم‪ٛ‬ائُ داخً ٌغخ ثب‪٠‬ض‪ٚ ْٛ‬رىزت ث‪{key:"value", key:"value",… } ٓ١‬‬
‫‪ : Python boolean )7‬اٌم‪ ُ١‬إٌّطم‪١‬خ داخً ٌغخ ثب‪٠‬ض‪ ٝ٘ٚ ْٛ‬ئِب ‪ True‬أ‪. False ٚ‬‬

‫عٕم‪ َٛ‬ثأخز ٔظشح عذش‪٠‬ؼخ ػٍذ‪ ٝ‬وذً ٔذ‪ٛ‬ع صذُ عذٕم‪ َٛ‬ثشذشػ وذً ٔذ‪ٛ‬ع فذ‪ ٟ‬دسط ِغذزمً ٌذٗ ‪ٚ‬عذٕزؼّك فذ‪ٟ‬‬
‫خصبئص ‪ٚ‬د‪ٚ‬اي وً ٔ‪ٛ‬ع ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔبد ‪.‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪4‬‬
: ْٛ‫ض‬٠‫ ا سلبَ داخً ٌغخ ثب‬: Python Numbers ً‫ال‬ٚ‫أ‬
: ٟ‫ر‬٢‫ْ ا‬ٛ‫ض‬٠‫دح داخً اٌجب‬ٛ‫ع‬ٌّٛ‫رٕذسط رؾذ فئخ أسلبَ ا‬
. int class ‫رُؼشف ثـ‬ٚ Integers numbers ‫ؾخ‬١‫ أسلبَ ا ػذاد اٌصؾ‬
.float class ‫رُؼشف ثـ‬ٚ floating point numbers ‫ أسلبَ اٌفبصٍخ اٌؼبئّخ‬/ ‫خ‬٠‫ ا سلبَ اٌؼشش‬
. complex class ‫رُؼشف ثـ‬ٚ complex numbers ‫ا سلبَ اٌّشوجخ‬ٚ 

.‫ّخ‬١‫ اٌم‬ٚ‫ش أ‬١‫ب اٌّزغ‬ٙ١ٌ‫ ئ‬ّٟ‫ٕز‬٠ ٟ‫ اٌز‬class ‫ اٌـ‬ٚ‫ ٌّؼشفخ اٌفئخ أ‬type)( ‫ّىٕٕب اعزخذاَ داٌخ‬٠ 
.ٓ١‫ ِؼ‬class ٌٝ‫ ئ‬ّٟ‫ٕز‬٠ object ٓ‫ ٌٍزؾمك ِّب ئرا وبْ اٌىبئ‬isinstance)( ‫زُ اعزخذاَ اٌذاٌخ‬٠ 

a = 5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))

Output
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True

.‫دح فمػ ثبٌزاوشح اٌّزبؽخ‬ٚ‫ ِؾذ‬ٟٙ‫ي ف‬ٛ‫ غ‬ٞ‫ْ ِٓ أ‬ٛ‫ّىٓ أْ رى‬٠ Integers ‫ؾخ‬١‫ ا ػذاد اٌصؾ‬
َ‫ا سلذب‬ٚ ‫ؼ‬١‫اٌؼذد صؾ‬ٚ .ٞ‫ سلُ ػشش‬15 ٝ‫أخز ؽز‬٠ floating-point number ‫ سلُ اٌفبصٍخ اٌؼبئّخ‬
. ٞ‫ سلُ ػشش‬ٛ٘ 1.0 ‫ّٕب‬١‫ؼ ث‬١‫ ػذد صؾ‬ٛ٘ 1 ً‫خ فّضال‬٠‫ٌخ ثٕمبغ ػشش‬ٛ‫ْ ِفص‬ٛ‫خ رى‬٠‫اٌؼشش‬
real ٟ‫مذ‬١‫ اٌغذضت اٌؾم‬ٛ‫ ٘ذ‬x ‫ذش‬١‫ ؽ‬x + yj ‫غخ‬١‫ثذخ ثبٌصذ‬ٛ‫ ِىز‬complex numbers ‫ ا ػذاد اٌّشوجذخ‬
. imaginary part ٍٟ١‫ اٌغضت اٌزخ‬y ٚ part

: ‫ ِضبي‬
>>> a = 1234567890123456789
>>> a
1234567890123456789
>>> b = 0.1234567890123456789123456789
>>> b
0.12345678901234568
>>> c = 1+2j
>>> c
(1+2j)

. ‫خ‬٠‫ ثؼذ اٌؼالِخ اٌؼشش‬ٞ‫ سلُ ػشش‬15 ِٓ ‫أخز أوزش‬٠ ‫ ٔٗ ال‬b ٞ‫ش اٌؼشش‬١‫ الؽع أٔٗ رُ لطغ اٌّزغ‬
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 5
‫صبٔ‪١‬ب ً ‪ : Python Strings‬اٌغالعً داخً ٌغخ ثب‪٠‬ض‪: ْٛ‬‬
‫اٌغٍغٍخ ‪ ٝ٘ String‬ػجبسح ػٓ عٍغٍخ ِٓ أؽشف اٌزشِ‪١‬ض ‪٠ٚ Unicode‬زُ رّض‪ٍٙ١‬ب وب‪٢‬ر‪: ٟ‬‬
‫‪ٌ ‬زّض‪ ً١‬اٌغالعً إٌص‪١‬خ ٌغطش ‪ٚ‬اؽذ ٔغذزخذَ ػالِذبد االلزجذبط اٌّفذشدح ‪ٚ single quotes‬سِض٘ذب‬
‫٘‪ ( ' ' ) ٛ‬أ‪ ٚ‬ػالِبد االلزجبط اٌّضد‪ٚ‬عخ ‪ٚ double quotes‬سِض٘ب ٘‪. ( " " ) ٛ‬‬
‫‪ٌ ‬زّض‪١‬ذذً اٌغالعذذً إٌصذذ‪١‬خ ِزؼذذذادح ا عذذطش ٔغذذزخذَ ػالِذذبد االلزجذذبط اٌضالص‪١‬ذذخ ‪triple quotes‬‬
‫‪ٚ‬سِض٘ب ٘‪ ( """ """ ) ٛ‬أ‪. ( ''' ''' ) ٚ‬‬

‫‪#Single-line strings‬‬
‫"‪s = "This is a string‬‬
‫)‪print(s‬‬

‫‪#Multi-line strings‬‬
‫‪s = '''A multiline‬‬
‫'''‪string‬‬
‫)‪print(s‬‬

‫‪Output‬‬
‫‪This is a string‬‬
‫‪A multiline‬‬
‫‪string‬‬

‫‪ّ٠ ‬ىٓ اعزخذاَ ػبًِ اٌزمط‪١‬غ ‪ِ slicing operator‬غ اٌغالعً ‪ٚ strings‬سِض٘ب ٘ذ‪ ] [ ٛ‬العذزخشاط‬
‫ػٕصش أ‪ِ ٚ‬غّ‪ٛ‬ػخ ِٓ اٌؼٕبصش ِٓ اٌغٍغٍخ ‪. string‬‬

‫'!‪s = 'Hello world‬‬

‫'‪# s[4] = 'o‬‬


‫)]‪print("s[4] = ", s[4‬‬

‫'‪# s[6:11] = 'world‬‬


‫)]‪print("s[6:11] = ", s[6:11‬‬

‫‪Output‬‬
‫‪s[4] = o‬‬
‫= ]‪s[6:11‬‬ ‫‪world‬‬

‫‪ ‬اٌف‪ٙ‬شط ] ‪ : [ index‬اٌم‪ّ١‬خ اٌز‪ٔ ٟ‬عؼ‪ٙ‬ب داخً ] [ ‪ ٝ٘ٚ‬ػجبسح ػٓ سلذُ اٌف‪ٙ‬ذشط اٌذز‪٠ ٞ‬ؼجذش ػذٓ ل‪ّ١‬ذخ‬
‫ِ‪ٛ‬ظغ أ‪ِ ٚ‬ىبْ اٌؾشف ‪ .‬فّضالً ]‪ ٛ٘ [4‬ػجبسح ػٓ ل‪ّ١‬خ اٌشلُ ‪ 4‬اٌّ‪ٛ‬ع‪ٛ‬د ف‪ ٟ‬اٌغٍغٍخ ‪.‬‬
‫‪٠ : index ‬جذأ ِٓ ‪ٌ zero‬زٌه ِ‪ٛ‬ظغ ؽشف '‪ٚ 0 ٛ٘ 'h‬ؽشف '‪٘ٚ 1 ٛ٘ 'e‬ىزا ‪.‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪6‬‬
‫‪ ‬اٌغالعذذً ‪ strings‬غ‪١‬ذذش لبثذذً ٌٍزغ‪١١‬ذذش ‪ immutable‬ثّؼٕذذ‪ ٝ‬أٔذذٗ ال ‪ّ٠‬ىذذٓ رغ‪١١‬ذذش ل‪ّ١‬ذذخ ػٕبصذذش اٌغٍغذذٍخ‬
‫الؽع اٌّضبي اٌزبٌ‪: ٟ‬‬
‫'!‪s = 'Hello world‬‬
‫‪# Generates error‬‬
‫‪# Strings are immutable in Python‬‬
‫'‪s[5] ='d‬‬

‫‪Output‬‬
‫‪Traceback (most recent call last):‬‬
‫>‪File "<string>", line 11, in <module‬‬
‫‪TypeError: 'str' object does not support item assignment‬‬

‫صبٌضب ً ‪ : Python Lists‬اٌم‪ٛ‬ائُ داخً ٌغخ ثب‪٠‬ض‪: ْٛ‬‬


‫اٌمبئّذذخ ‪ : list‬اٌمبئّذذخ ٘ذذ‪ ٟ‬عٍغذذٍخ ِشرجذذخ ِذذٓ اٌؼٕبصذذش‪ .‬ئٔذذٗ أؽذذذ أٔذذ‪ٛ‬اع اٌج‪١‬بٔذذبد ا وضذذش اعذذزخذا ًِب فذذ‪ٟ‬‬
‫‪ِ ٛ٘ٚ Python‬شْ ٌٍغب‪٠‬خ‪ .‬ال ‪ٍ٠‬ضَ أْ رى‪ ْٛ‬وبفخ اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌمبئّخ ِٓ ٔفظ إٌ‪ٛ‬ع ‪.‬‬

‫ٌإلػذذالْ ػذذٓ لبئّذذخ ‪ list‬أِذذش عذذ‪ِٚ ًٙ‬جبشذذش ‪٠‬ذذزُ رٌذذه ػذذٓ غش‪٠‬ذذك ‪ٚ‬ظذذغ اٌؼٕبصذذش داخذذً ألذذ‪ٛ‬اط [ ]‬
‫‪ِٚ‬فص‪ٛ‬ي ث‪ّٕٙ١‬ب ثفبصٍخ ‪ٚ Comma‬سِض٘ب ( ‪ ) ,‬وب‪٢‬ر‪: ٟ‬‬
‫]'‪a = [1, 2.2, 'python‬‬

‫‪ّ٠ ‬ىذذٓ اعذذزخذاَ ػبِذذً اٌزمط‪١‬ذذغ ‪ِ slicing operator‬ذذغ اٌمذذ‪ٛ‬ائُ ‪ٚ lists‬سِض٘ذذب ٘ذذ‪ ] [ ٛ‬العذذزخشاط‬
‫ػٕصش أ‪ِ ٚ‬غّ‪ٛ‬ػخ ِٓ اٌؼٕبصش ِٓ اٌمبئّخ ‪. list‬‬

‫]‪a = [5,10,15,20,25,30,35,40‬‬

‫‪# a[2] = 15‬‬


‫)]‪print("a[2] = ", a[2‬‬

‫]‪# a[0:3] = [5, 10, 15‬‬


‫)]‪print("a[0:3] = ", a[0:3‬‬

‫]‪# a[5:] = [30, 35, 40‬‬


‫)]‪print("a[5:] = ", a[5:‬‬

‫‪Output‬‬
‫‪a[2] = 15‬‬
‫]‪a[0:3] = [5, 10, 15‬‬
‫]‪a[5:] = [30, 35, 40‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪2‬‬
‫‪ ‬اٌم‪ٛ‬ائُ ‪ lists‬لبثٍخ ٌٍزغ‪١١‬ش ثّؼٕ‪ ٝ‬أٔٗ ‪ّ٠‬ىٓ رغ‪١١‬ش ل‪ّ١‬خ ػٕبصش اٌمبئّخ الؽع اٌّضبي اٌزبٌ‪: ٟ‬‬
‫]‪a = [1, 2, 3‬‬
‫‪a[2] = 4‬‬
‫)‪print(a‬‬

‫‪Output‬‬
‫]‪[1, 2, 4‬‬

‫ساثؼب ً ‪ : Python Tuples‬اٌم‪ٛ‬ائُ داخً ٌغخ ثب‪٠‬ض‪: ْٛ‬‬


‫اٌّغّ‪ٛ‬ػذذذخ ‪٘ Tuple‬ذذذ‪ ٛ‬رغٍغذذذً ِشرذذذت ِذذذٓ اٌؼٕبصذذذش ِضذذذً اٌمبئّذذذخ ‪ . list‬االخذذذزالف اٌ‪ٛ‬ؽ‪١‬ذذذذ ٘ذذذ‪ ٛ‬أْ‬
‫اٌّغّ‪ٛ‬ػبد رٍه غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش ال ‪ّ٠‬ىٓ رؼذ‪ ً٠‬اٌّغّ‪ٛ‬ػبد ‪ tuples‬اٌز‪ ٟ‬رُ ئٔشبؤ٘ب ِشح ‪ٚ‬اؽذح‪.‬‬
‫رُغذذزخذَ ‪ Tuples‬فذذ‪ ٟ‬ؽّب‪٠‬ذذخ اٌج‪١‬بٔذذبد ِذذٓ اٌىزبثذذخ ‪ٚ write-protect data‬ػذذبدح ً ِذذب رىذذ‪ ْٛ‬أعذذشع ِذذٓ‬
‫اٌم‪ٛ‬ائُ ‪ lists‬ؽ‪١‬ش ال ‪ّ٠‬ىٓ رغ‪١١‬ش٘ب د‪ٕ٠‬بِ‪١‬ى‪ً١‬ب‪.‬‬
‫ٌإلػالْ ػٓ اٌّغّ‪ٛ‬ػخ ‪ Tuple‬أِش ع‪ِٚ ًٙ‬جبشش ‪٠‬زُ رٌه ػٓ غش‪٠‬ك ‪ٚ‬ظغ اٌؼٕبصش داخً ألذ‪ٛ‬اط (‬
‫) ‪ِٚ‬فص‪ٛ‬ي ث‪ّٕٙ١‬ب ثفبصٍخ ‪ٚ Comma‬سِض٘ب ( ‪ ) ,‬وب‪٢‬ر‪: ٟ‬‬

‫)‪t = (5,'program', 1+3j‬‬

‫‪ّ٠ ‬ىذذٓ اعذذزخذاَ ػبِذذً اٌزمط‪١‬ذذغ ‪ِ slicing operator‬ذذغ اٌمذذ‪ٛ‬ائُ ‪ٚ lists‬سِض٘ذذب ٘ذذ‪ ] [ ٛ‬العذذزخشاط‬
‫ػٕصش أ‪ِ ٚ‬غّ‪ٛ‬ػخ ِٓ اٌؼٕبصش ِٓ اٌّغّ‪ٛ‬ػخ ‪. tuple‬‬

‫)‪t = (5,'program', 1+3j‬‬

‫'‪# t[1] = 'program‬‬


‫)]‪print("t[1] = ", t[1‬‬

‫))‪# t[0:3] = (5, 'program', (1+3j‬‬


‫)]‪print("t[0:3] = ", t[0:3‬‬

‫‪Output‬‬
‫‪t[1] = program‬‬
‫))‪t[0:3] = (5, 'program', (1+3j‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪1‬‬
: ٍٟ٠ ‫ػخ وّب‬ّٛ‫ّخ ػٕبصش اٌّغ‬١‫ش ل‬١١‫ّىٓ رغ‬٠ ٗٔ‫ أ‬ٕٝ‫ش ثّؼ‬١١‫ش لبثٍٗ ٌٍزغ‬١‫ غ‬tuple ‫ػخ‬ّٛ‫ اٌّغ‬
t = (5,'program', 1+3j)
# Generates error - Tuples are immutable
t[0] = 10

Output
Traceback (most recent call last):
File "test.py", line 11, in <module>
t[0] = 10
TypeError: 'tuple' object does not support item assignment

: ْٛ‫ض‬٠‫ػخ داخً ٌغخ ثب‬ّٛ‫ ِغ‬ٚ‫ٍخ أ‬١‫ رشى‬: Python Set ً ‫خبِغب‬
‫ذذذت‬١‫شذذذزشغ رشر‬٠ ‫ال‬ٚ ‫ذذذذح‬٠‫ذذذش ِشرجذذذخ ِذذذٓ اٌؼٕبصذذذش اٌفش‬١‫ػذذذخ غ‬ّٛ‫ ِغ‬ٟ‫ ٘ذذذ‬Set ‫ػذذذخ‬ّٛ‫ اٌّغ‬ٚ‫ٍخ أ‬١‫اٌزشذذذى‬
. Set ‫ػخ‬ّٛ‫ ِغ‬ٟ‫دح ف‬ٛ‫ع‬ٌّٛ‫اٌؼٕبصش ا‬
{ } ‫اط‬ٛ‫ظغ اٌؼٕبصش داخذً ألذ‬ٚ ‫ك‬٠‫زُ رٌه ػٓ غش‬٠ ‫ِجبشش‬ٚ ًٙ‫ أِش ع‬Set ‫ػخ‬ّٛ‫ٌإلػالْ ػٓ اٌّغ‬
: ٟ‫ر‬٢‫ ) وب‬, ( ‫سِض٘ب‬ٚ Comma ‫ّب ثفبصٍخ‬ٕٙ١‫ي ث‬ٛ‫ِفص‬ٚ
a = {5,2,3,1,4}

# printing set variable


print("a = ", a)

# data type of variable a


print(type(a))

Output
a = {1, 2, 3, 4, 5}
<class 'set'>

. sets ٓ١‫ػز‬ّٛ‫ ِغ‬ٍٝ‫ػ‬intersection ‫اٌزمبغغ‬ٚ union ‫بد ِؾذدح ِضً االرؾبد‬١ٍّ‫ّىٕٕب ئعشات ػ‬٠ 

: ٍٟ٠ ‫زُ ؽزف اٌزىشاساد وّب‬٠ ‫ ؽبٌخ اٌزىشاس‬ٟ‫ف‬ٚ unique values ‫ذح‬٠‫ُ فش‬١‫ب ل‬ٌٙ ‫ػبد‬ّٛ‫ اٌّغ‬
a = {1,2,2,3,3,3}
print("a = ",a)

Output
a = {1, 2, 3}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 4
‫‪ ‬ال ‪ّ٠‬ىذٓ اعذزخذاَ ػبِذً اٌزمط‪١‬ذغ [ ] ‪ِ slicing operator‬ذغ اٌمذ‪ٛ‬ائُ ‪ ْ sets‬اٌّغّ‪ٛ‬ػذخ ‪٘ set‬ذ‪ٟ‬‬
‫ِغّ‪ٛ‬ػخ غ‪١‬ش ِشرجخ فٍ‪١‬ظ ٌٍف‪ٙ‬شعخ ‪ indexing‬أ‪ِ ٞ‬ؼٕ‪ ٝ‬فٍزٌه ػبًِ اٌزمط‪١‬غ [] ال ‪٠‬ؼًّ وّب ‪: ٍٟ٠‬‬

‫}‪>>> a = {1,2,3‬‬
‫]‪>>> a[1‬‬
‫‪Traceback (most recent call last):‬‬
‫‪File "<string>", line 301, in runcode‬‬
‫>‪File "<interactive input>", line 1, in <module‬‬
‫‪TypeError: 'set' object does not support indexing‬‬

‫عبدعب ً ‪ :Python dictionary‬اٌمبِ‪ٛ‬ط داخً ٌغخ ثب‪٠‬ض‪: ْٛ‬‬


‫اٌمذذبِ‪ٛ‬ط ‪٘ dictionary‬ذذ‪ ٟ‬ػجذذبسح ػذذٓ ِغّ‪ٛ‬ػذذخ غ‪١‬ذذش ِشرجذذخ ِذذٓ ص‪ٚ‬عذذ‪ِ ٓ١‬ذذشرج‪ّ٘ ٓ١‬ذذب ص‪ٚ‬ط اٌّفزذذبػ‬
‫‪ٚ‬اٌم‪ّ١‬خ ا‪ِ ٚ‬ب ‪٠‬ؼشف ثـ ‪. key-value pairs‬‬

‫‪٠‬ذذزُ اعذذزخذاِٗ ثشذذىً ػذذبَ ػٕذذذِب ِذذغ اٌىّ‪١‬ذذخ اٌ‪ٙ‬بئٍذذخ ِذذٓ اٌج‪١‬بٔذذبد رذذُ رؾغذذ‪ ٓ١‬اٌمذذ‪ٛ‬اِ‪١‬ظ‪dictionary‬‬
‫العذذزشعبع اٌج‪١‬بٔذذبد ثغذذ‪ٌٛٙ‬خ ‪ٚ‬ثطش‪٠‬مذذخ عذذش‪٠‬ؼخ ‪ٌٚ‬ذذزٌه ‪٠‬غذذت أْ ٔؼذذشف اٌّفزذذبػ ‪ key‬العذذزشداد اٌم‪ّ١‬ذذخ‬
‫‪ٌٚ value‬ىٓ ٌ‪١‬ظ اٌؼىظ ‪.‬‬

‫‪ّ٠‬ىٓ أْ ‪٠‬ى‪ ْٛ‬اٌّفزبػ ‪ٚ‬اٌم‪ّ١‬خ ِٓ أ‪ٛٔ ٞ‬ع ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔبد ا‪٢‬خش‪. ٜ‬‬

‫ٌإلػالْ ػٓ اٌمذ‪ٛ‬اِ‪١‬ظ ‪ dictionary‬أِذش عذ‪ِٚ ًٙ‬جبشذش ‪٠‬ذزُ رٌذه ػذٓ غش‪٠‬ذك ‪ٚ‬ظذغ اٌؼٕبصذش داخذً‬
‫ألذذذ‪ٛ‬اط } { ‪ِٚ‬فصذذذ‪ٛ‬ي ث‪ّٕٙ١‬ذذذب ثفبصذذذٍخ ‪ٚ Comma‬سِض٘ذذذب ( ‪ٔٚ ) ,‬فصذذذً ثذذذ‪ ٓ١‬وذذذً ‪key-value‬‬
‫ثبٌٕمطز‪ ٓ١‬اٌشئغ‪١‬ز‪ٚ colon ٓ١‬سِض٘ب ( ‪ ) :‬وب‪٢‬ر‪: ٟ‬‬

‫}‪d = {1:'value','key':2‬‬
‫))‪print(type(d‬‬

‫;)]‪print("d[1] = ", d[1‬‬

‫;)]'‪print("d['key'] = ", d['key‬‬

‫‪Output‬‬
‫>'‪<class 'dict‬‬
‫‪d[1] = value‬‬
‫‪d['key'] = 2‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪16‬‬
‫ٔغزخذَ اٌّفزبػ العزشداد اٌم‪ّ١‬خ اٌّؼٕ‪١‬خ‪ٌٚ .‬ىٓ ٌ‪١‬ظ اٌؼىظ الؽع ِب ‪: ٍٝ٠‬‬
‫}‪d = {1:'value','key':2‬‬

‫‪# Generates error‬‬


‫;)]‪print("d[2] = ", d[2‬‬

‫‪Output‬‬
‫‪Traceback (most recent call last):‬‬
‫>‪File "<string>", line 9, in <module‬‬
‫‪KeyError: 2‬‬

‫عبثؼب ً ‪ :Python boolean‬اٌم‪ ُ١‬إٌّطم‪١‬خ داخً ٌغخ ثب‪٠‬ض‪: ْٛ‬‬


‫اٌم‪ ُ١‬إٌّطم‪١‬خ ‪ ٟ٘ boolean‬ػجبسح ل‪ ُ١‬رغزخذَ ٌٍزؾمك ِٓ ل‪ّ١‬خ اٌزؼج‪١‬ش ‪٠ٚ expression‬ى‪ ْٛ‬إٌذبرظ‬
‫ئؽذ‪ ٜ‬ل‪ّ١‬ز‪ ٓ١‬ئِب ل‪ّ١‬خ ‪ True‬أ‪ ٚ‬ل‪ّ١‬خ ‪. False‬‬

‫‪٠‬ى‪ ْٛ‬اٌزؼج‪١‬ش صؾ‪١‬ؾب ً ‪ True‬ف‪ ٟ‬ؽبٌخ رؾمك اٌششغ أ‪ ٚ‬اٌزؼج‪١‬ش ‪.‬‬

‫‪٠‬ى‪ ْٛ‬اٌزؼج‪١‬ش خطأ ‪ False‬ف‪ ٟ‬ؽبٌخ ػذَ رؾمك اٌششغ أ‪ ٚ‬اٌزؼج‪١‬ش ‪.‬‬

‫)‪print(10 > 9‬‬

‫)‪print(10 == 9‬‬

‫)‪print(10 < 9‬‬

‫‪Output‬‬
‫‪True‬‬
‫‪False‬‬
‫‪False‬‬

‫رً‬ ‫إلى هنا نكون قد أخذنا نظرة سرٌعة على أنواع البٌانات ‪ ،‬وسوف نقوم بشرر للرا اونرواع باللل رٌ‬
‫الدروس القادمة ‪ ،‬سنقوم اآلن بأخذ نظرة سرٌعة على اللحوٌ بٌن أنواع البٌانات ‪.‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪11‬‬
Conversion between data types ‫بٔبد‬١‫اع اٌج‬ٛٔ‫ٓ أ‬١‫ً ث‬٠ٛ‫اٌزؾ‬
str() ٚ float() ٚ int() ً‫ع ِضذ‬ٌٕٛ‫ً ا‬٠ٛ‫اي رؾ‬ٚ‫بٔبد اٌّخزٍفخ ثبعزخذاَ د‬١‫اع اٌج‬ٛٔ‫ٓ أ‬١‫ً ث‬٠ٛ‫ّىٕٕب اٌزؾ‬٠
. ‫ّخ‬ٌّٙ‫اي ا‬ٚ‫ رٌه ِٓ اٌذ‬ٌٝ‫ِب ئ‬ٚ

: float ٌٝ‫ ئ‬integer ‫بٔبد‬١‫ع ث‬ٛٔ ِٓ ً٠ٛ‫ ِضبي رؾ‬


>>> float(5)
5.0

.‫ؼ‬١‫ت لشة سلُ صؾ‬٠‫زُ اٌزمش‬٠ : integer ٌٝ‫ ئ‬float ‫بٔبد‬١‫ع ث‬ٛٔ ِٓ ً٠ٛ‫ ِضبي رؾ‬
>>> float(10.6)
10

. ‫ اٌصفش‬ٌٝ‫غؼٍٗ ألشة ئ‬٠ٚ ‫ّخ‬١‫مطغ اٌم‬١‫ ع‬

. compatible values ‫افمخ‬ٛ‫ُ ِز‬١‫ ل‬ٍٝ‫ آخش٘ب ػ‬ٌٝ‫ ئ‬string ِٓ ً٠ٛ‫ اٌزؾ‬ٞٛ‫ؾز‬٠ ْ‫غت أ‬٠ ‫ ِضبي‬
>>> float('2.5')
2.5
>>> str(25)
'25'
>>> int('1p')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1p'

. ٜ‫ أخش‬ٌٝ‫ ئ‬sequence ‫خ‬١ٌ‫ً ِٓ ِززب‬٠ٛ‫ّىٕٕب رؾ‬٠ ‫ ِضبي‬


>>> set([1,2,3])
{1, 2, 3}
>>> tuple({5,6,7})
(5, 6, 7)
>>> list('hello')
['h', 'e', 'l', 'l', 'o']

.‫ ًعب‬ٚ‫ْ وً ػٕصش ص‬ٛ‫ى‬٠ ْ‫غت أ‬٠ ٓ‫ٌى‬ٚ dictionary ‫ط‬ِٛ‫ً ِٓ لب‬٠ٛ‫ّىٕٕب رؾ‬٠ ‫ ِضبي‬
>>> dict([[1,2],[3,4]])
{1: 2, 3: 4}
>>> dict([(3,26),(4,44)])
{3: 26, 4: 44}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 12
‫ انتحول بيٍ أَواع انبياَات‬: ‫انذرس انثاَي‬
( Type Conversion & Type Casting )
ً‫ذ‬٠ٛ‫اعذزخذاِبد رؾ‬ٚ Type Conversion ‫ع‬ٛ‫ذً إٌذ‬٠ٛ‫ رؾ‬ٝ‫ عذززؼشف ػٍذ‬ٟ‫ّذ‬١ٍ‫ ٘ذزا اٌجشٔذبِظ اٌزؼ‬ٟ‫ فذ‬
.Python ٟ‫ع ف‬ٌٕٛ‫ا‬

. ‫اع‬ٛٔ ‫ٓ ٘زٖ ا‬١‫ً ث‬٠ٛ‫خ اٌزؾ‬١‫ف‬١‫ اٌزؼشف و‬ٍٝ‫ْ ٌٍزؼشف ػ‬٢‫ ا‬ٟ‫بٔبد ٔأر‬١‫اع اٌج‬ٛٔ‫ أ‬ٍٝ‫ثؼذ أْ رؼشفٕب ػ‬

‫ع‬ٌٕٛ‫ً ا‬٠ٛ‫ رؾ‬: Type Conversion 


‫بٔذبد آخذش‬١‫ع ث‬ٛٔ ٌٝ‫ ئٌ ) ئ‬integer , string , float( ً‫اؽذ ِض‬ٚ ‫بٔبد‬١‫ع ث‬ٛٔ ‫ّخ‬١‫ً ل‬٠ٛ‫خ رؾ‬١ٍّ‫ ػ‬ٛ٘
: ‫ع‬ٌٕٛ‫ً ا‬٠ٛ‫ػبْ ِٓ رؾ‬ٛٔ Python ٟ‫عذ ف‬ٛ٠ٚ
ّٟٕ‫ً اٌع‬٠ٛ‫ اٌزؾ‬ٚ‫ أ‬ّٟٕ‫ع اٌع‬ٌٕٛ‫ً ا‬٠ٛ‫ رؾ‬: Implicit Type Conversion ‫ي‬ٚ ‫ع ا‬ٌٕٛ‫ ا‬
‫بٔذبد آخذش‬١‫ع ث‬ٛ‫ ٔذ‬ٌٝ‫اؽذ ئ‬ٚ ‫بٔبد‬١‫ع ث‬ٛٔ ً٠ٛ‫ًب ثزؾ‬١‫ رٍمبئ‬Python َٛ‫ رم‬ّٟٕ‫ع اٌع‬ٌٕٛ‫ً ا‬٠ٛ‫ رؾ‬ٟ‫ف‬
.َ‫ رذخً ِٓ اٌّغزخذ‬ٞ‫ أ‬ٌٝ‫خ ال رؾزبط ئ‬١ٍّ‫٘زٖ اٌؼ‬ٚ

ٝ‫بٔذبد ا ػٍذ‬١‫ع اٌج‬ٛ‫ ٔذ‬ٝ‫ ئٌذ‬int ً‫بٔذبد ا لذ‬١‫ع اٌج‬ٛ‫ذً ٔذ‬٠ٛ‫ ثزؾ‬Python َٛ‫ذش رمذ‬١‫ ِضبالً ؽ‬ٜ‫ٔب ٔش‬ٛ‫ دػ‬
.‫بٔبد‬١‫ ٌزغٕت فمذاْ اٌج‬float
num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

Output
datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23


datatype of num_new: <class 'float'>

ٟ‫ُ فذ‬ٙ‫ٔعذؼ‬ٚ float ‫ع‬ٛ‫ذش ِذٓ ٔذ‬١‫ِزغ‬ٚ int ‫ع‬ٛ‫ذش ِذٓ ٔذ‬١‫ٓ ِزغ‬٠‫ش‬١‫ اٌّضبي اٌغبثك ٔالؽع ػٕذ عّغ ِزغ‬ٟ‫ ف‬
َٛ‫ رمذ‬Python ْ flaot ‫بٔذبد‬١‫ع ث‬ٛ‫ ٔذ‬ٝ‫ ػٍذ‬ٞٛ‫ؾزذ‬٠ num_new ‫ذذ‬٠‫ش اٌغذ‬١‫ّخ اٌّزغ‬١‫ذ فاْ ل‬٠‫ش عذ‬١‫ِزغ‬
. ‫بٔبد‬١‫بٔبد أوجش ٌزغٕت فمذاْ اٌج‬١‫اع ث‬ٛٔ‫ أ‬ٌٝ‫بٔبد ا صغش ئ‬١‫اع اٌج‬ٛٔ‫ً أ‬٠ٛ‫ثزؾ‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 13
ْ ‫بٔذبد‬١‫ اٌج‬ٟ‫ْ ٕ٘بن فمذ ف‬ٛ‫ى‬١‫ فغ‬integer ‫ع‬ٛٔ ِٓ ْ‫وب‬ٚ float ‫ذ‬٠‫ش اٌغذ‬١‫ع اٌّزغ‬ٛٔ ْٛ‫ى‬٠ ُ‫ز‬٠ ٌُ ‫ ئرا‬
. ‫ؾخ‬١‫ّخ صؾ‬١‫ ألشة ل‬ٌٝ‫ؾخ عزمشة اٌشلُ ئ‬١‫ّخ اٌصؾ‬١‫اٌم‬
: ‫ب‬ٙ‫خ اٌزؼبًِ ِؼ‬١‫ف‬١‫و‬ٚ Python ً‫ داخ‬int ٚ string ‫ ِضبالً ئظبفخ‬ٜ‫ٔب ٔش‬ٛ‫ دػ‬
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str:",type(num_str))

print(num_int+num_str)

Output
Data type of num_int: <class 'int'>
Data type of num_str: <class 'str'>

Traceback (most recent call last):


File "python", line 7, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

‫ ِذٓ اإلخذشاط ؽصذٍٕب‬ٜ‫ وّذب ٔذش‬.num_str ٚ num_int ٓ٠‫ذش‬١‫ ِزغ‬١‫ اٌّضذبي اٌغذبثك ػٕذذِب ٔعذ‬ٟ‫ ف‬
.‫ف‬ٚ‫ ِضذً ٘ذزٖ اٌظذش‬ٟ‫ ف‬ّٟٕ‫ً اٌع‬٠ٛ‫ اعزخذاَ اٌزؾ‬ٍٝ‫غذ لبدسح ػ‬١ٌ ْٛ‫ض‬٠‫ ْ ٌغخ ثب‬TypeError ٍٝ‫ػ‬
.‫ؼ‬٠‫ً اٌصش‬٠ٛ‫فخ ثبعُ اٌزؾ‬ٚ‫ال اٌّؼش‬ٌّٛ‫اع ِٓ ا‬ٛٔ ‫زٖ ا‬ٌٙ ً‫ب ؽ‬ٙ٠‫ ٌذ‬Python ْ‫ِغ رٌه فا‬ٚ

‫ؼ‬٠‫ً اٌصش‬٠ٛ‫ اٌزؾ‬ٚ‫ؼ أ‬٠‫ع اٌصش‬ٌٕٛ‫ً ا‬٠ٛ‫ رؾ‬: Explicit Type Conversion ٟٔ‫ع اٌضب‬ٌٕٛ‫ ا‬
‫بٔذذبد‬١‫ع اٌج‬ٛ‫ ٔذذ‬ٝ‫بٔذذبد اٌىذذبئٓ ئٌذذ‬١‫ع ث‬ٛ‫ذذً ٔذذ‬٠ٛ‫ْ ثزؾ‬ِٛ‫َ اٌّغذذزخذ‬ٛ‫مذذ‬٠ ‫ؼ‬٠‫ع اٌصذذش‬ٛ‫ذذً إٌذذ‬٠ٛ‫ رؾ‬ٟ‫فذذ‬
‫ رٌذه إلعذشات‬ٝ‫ِذب ئٌذ‬ٚ str)( ٚ float)( ٚ int)( ً‫ظبئ اٌّؾذدح ِغجمًب ِض‬ٌٛ‫ ٔغزخذَ ا‬.‫ة‬ٍٛ‫اٌّط‬
. ‫بٔبد‬١‫ع اٌج‬ٌٕٛ ‫ؼ‬٠‫ً صش‬٠ٛ‫رؾ‬

‫ذش‬١١‫ رغ‬ٟ‫ٍمذ‬٠ َ‫" ْ اٌّغذزخذ‬typecasting" ‫ظ‬١‫عذب اعذُ اٌزٍجذ‬


ً ٠‫ً أ‬٠ٛ‫ع ِٓ اٌزؾ‬ٌٕٛ‫ ٘زا ا‬ٍٝ‫ُطٍك ػ‬٠
.‫بٔبد اٌىبئٕبد‬١‫ع ث‬ٛٔ ٍٝ‫ ػ‬casts changes

: ٟ‫ر‬٢‫ع وب‬ٌٕٛ‫ العزخذاَ ٘زا ا‬Syntax ‫ شىً ثٕبت اٌغٍّخ‬


<required_datatype>(expression)

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 14
.expression ‫ش‬١‫ثخ ثذاخً اٌزؼج‬ٍٛ‫بٔبد اٌّط‬١‫ع اٌج‬ٛٔ ‫ٓ داٌخ‬١١‫ك رؼ‬٠‫ ػٓ غش‬Typecasting ًّ‫ّىٓ ػ‬٠ 

: ‫ؼ‬٠‫ً اٌصش‬٠ٛ‫ك اٌزؾ‬٠‫ ػٓ غش‬int ٚ string ‫ ئظبفخ‬ٍٝ‫ ِضبي ػ‬


num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)


print("Data type of the sum:",type(num_sum))

Output
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579


Data type of the sum: <class 'int'>

‫ذخ‬١ٍّ‫ إلعذشات ػ‬int)( ‫ (ألذً) ثبعذزخذاَ داٌذخ‬int ‫ع‬ٛٔ ٌٝ‫) ئ‬ٍٝ‫ (أػ‬string ِٓ num_str ً٠ٛ‫ لّٕب ثزؾ‬
ٓ٠‫ ئظذذذبفخ ٘ذذذز‬Python ‫غ‬١‫ رغذذذزط‬int ‫ؼ‬١‫ّذذذخ ػذذذذد صذذذؾ‬١‫ ل‬ٝ‫ ئٌذذذ‬num_str ً‫ذذذ‬٠ٛ‫ثؼذذذذ رؾ‬ٚ ‫اٌغّذذذغ‬
.ٓ٠‫ش‬١‫اٌّزغ‬

ً‫ذ‬٠ٛ‫ذب فّذضال ئرا أسدٔذب رؾ‬ٙ‫ذً اٌخبصذخ ث‬٠ٛ‫ذب داٌذخ اٌزؾ‬ٌٙ َ‫ب ٔغذزخذ‬ٙ١ٌ‫ً ئ‬٠ٛ‫ّخ اٌّشاد اٌزؾ‬١‫ أْ اٌم‬ٝ‫ ال رٕغ‬
‫ثبٌّضذذً ئرا أسدٔذذب‬ٚ int)( ‫ داٌذذخ‬ٝ‫٘ذذ‬ٚ ‫ذذب‬ٙ١ٌ‫ذذً ئ‬٠ٛ‫ة اٌزؾ‬ٛ‫ فإٔذذب ٔغذذزخذَ داٌذذخ اٌّطٍذذ‬int ٝ‫ ئٌذذ‬string ٓ‫ِذذ‬
. ... ‫٘ىزا‬ٚ str)( ‫ داٌخ‬ٝ٘ٚ ‫ب‬ٙ١ٌ‫ً ئ‬٠ٛ‫ة اٌزؾ‬ٍٛ‫ فإٔب ٔغزخذَ داٌخ اٌّط‬string ٌٝ‫ ئ‬int ِٓ ً٠ٛ‫رؾ‬

.‫بٔبد آخش‬١‫ع ث‬ٛٔ ٌٝ‫بٔبد ئ‬١‫ع ث‬ٛٔ ِٓ object ٓ‫ً اٌىبئ‬٠ٛ‫ رؾ‬ٛ٘ Type Conversion ً٠ٛ‫ع اٌزؾ‬ٛٔ 

ٟ‫بٔبد فذ‬١‫ فمذاْ اٌج‬Python ‫رزغٕت ٌغخ‬ٚ ْٛ‫ض‬٠‫اعطخ ِزشعُ ثب‬ٛ‫ًب ث‬١‫ رٍمبئ‬ّٟٕ‫ع اٌع‬ٌٕٛ‫ً ا‬٠ٛ‫زُ ئعشات رؾ‬٠ 
. ّٟٕ‫ً اٌع‬٠ٛ‫اٌزؾ‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 15
‫عب اعُ ‪ Type Casting‬ؽ‪١‬ش ‪٠‬زُ رؾ‪ ً٠ٛ‬أٔ‪ٛ‬اع ث‪١‬بٔبد اٌىبئٕذبد‬‫‪ُ٠ ‬طٍك ػٍ‪ ٝ‬رؾ‪ ً٠ٛ‬إٌ‪ٛ‬ع اٌصش‪٠‬ؼ أ‪ً ٠‬‬
‫ثبعزخذاَ ‪ٚ‬ظبئ ِؾذدح ِغجمًب ث‪ٛ‬اعطخ اٌّغذزخذَ ‪ٚ‬فذ‪ Type Casting ٟ‬لذذ ‪٠‬ؾذذس فمذذاْ ٌٍج‪١‬بٔذبد‬
‫ٕٔب ٔفشض ‪ٔٚ‬غجش اٌىبئٓ ػٍ‪ٛٔ ٝ‬ع ث‪١‬بٔبد ِؼ‪.ٓ١‬‬

‫‪ٛٔ : Data Type & class Type ‬ع اٌج‪١‬بٔبد ‪ٛٔٚ‬ع اٌفئخ‬
‫وذً ٔذ‪ٛ‬ع ِذذٓ ٘ذزٖ ا ٔذذ‪ٛ‬اع ‪ٕ٠‬زّذ‪ ٟ‬ئٌذذ‪ ٝ‬فئذخ ‪ class‬رخصذذ‪ٙ‬ب ؽ‪١‬ذش ‪٠‬ذذزُ رؼش‪٠‬ذ وذذً ٔذ‪ٛ‬ع ػذذٓ غش‪٠‬ذك اٌفئذذخ‬
‫‪ class‬اٌخب ثٗ فّضالً ‪:‬‬
‫‪ٛٔ ‬ع اٌج‪١‬بٔبد ‪ٕ٠ int‬زّ‪ ٟ‬ئٌ‪ int class ٝ‬أ‪٠ ٞ‬زُ رؼش‪٠‬ف‪ٙ‬ب ِٓ خالي ‪.int class‬‬
‫‪ٛٔ ‬ع اٌج‪١‬بٔبد ‪ٕ٠ float‬زّ‪ ٟ‬ئٌ‪ float class ٝ‬أ‪٠ ٞ‬زُ رؼش‪٠‬ف‪ٙ‬ب ِٓ خالي ‪.float class‬‬
‫‪ٛٔ ‬ع اٌج‪١‬بٔبد ‪ٕ٠ complex‬زّ‪ ٟ‬ئٌ‪ complex class ٝ‬أ‪٠ ٞ‬زُ رؼش‪٠‬ف‪ٙ‬ب ِٓ خالي ‪.complex class‬‬

‫‪ ‬و‪١‬ف‪١‬خ ِؼشفخ ٔ‪ٛ‬ع اٌىبئٓ ف‪ٕ٘ : Python ٟ‬بن داٌز‪ّ٘ ٓ١‬ب‬


‫‪: type)( function ‬‬
‫داٌخ ثزشعغ ٔ‪ٛ‬ع اٌىبئٓ ‪ object type‬اٌ ُّؾذد ‪.‬‬
‫‪ Synax :‬‬ ‫)‪type(object, bases, dict‬‬

‫‪ : Parameter Values ‬ل‪ ُ١‬اٌّؼبِالد اٌز‪ّ٠ ٟ‬ىٓ رّش‪٠‬ش٘ب‬


‫‪ : object‬اٌىبئٓ اٌّطٍ‪ٛ‬ة اٌزؾمك ِٕٗ ‪.‬‬ ‫‪‬‬
‫‪٠ : bases‬ؾذد اٌفئبد ا عبع‪١‬خ (‪. )Optional‬‬ ‫‪‬‬
‫‪٠ : dict‬ؾذد ‪ِ namespace‬غ رؼش‪ ٠‬اٌفئخ (‪. )Optional‬‬ ‫‪‬‬

‫>'‪<class 'int‬‬
‫‪: isinstance)( function ‬‬
‫داٌخ ٌٍزؾمك ِّب ئرا وبْ اٌىبئٓ ‪ٕ٠ object‬زّ‪ ٟ‬ئٌ‪ ٝ‬فئخ ‪ِ class‬ؼ‪ٚ ٓ١‬ئال ٘زشعغ ‪. False‬‬
‫‪ Synax :‬‬ ‫)‪isinstance(object , type‬‬

‫‪ : Parameter Values ‬ل‪ ُ١‬اٌّؼبِالد اٌز‪ّ٠ ٟ‬ىٓ رّش‪٠‬ش٘ب‬


‫‪ : object ‬اٌىبئٓ اٌّطٍ‪ٛ‬ة اٌزؾمك ِٕٗ ‪.‬‬
‫‪ٛٔ : type ‬ع اٌفئخ اٌز‪٠ ٟ‬زُ اٌزؾمك ػٍ‪ٙ١‬ب ‪ّ٠ٚ‬ىٓ أْ ‪٠‬ى‪ِ ْٛ‬غّ‪ٛ‬ػخ ِٓ أٔ‪ٛ‬اع اٌفئبد ‪tuple of types classes‬‬
‫‪ٚ‬ئرا وبٔذ اٌّؼبًِ اٌضبٔ‪ tuple ٟ‬فغزؼ‪١‬ذ ٘زٖ اٌذاٌخ ‪ True‬ئرا وبْ اٌىبئٓ أؽذ ا ٔ‪ٛ‬اع اٌّ‪ٛ‬ع‪ٛ‬دح ف‪. tuple ٟ‬‬

‫ثؼط ا ِضٍخ ػٍ‪ ٝ‬داٌخ )(‪ٚ type‬داٌخ )(‪isinstance‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪16‬‬
a=30 #integer
b=30.4 #float
c=5j #complex
d= True #boolean
e="Mahmoud Soliman" #String
f='M' #String
g=["mahmoud","Mohammed","Abd El-Atty" ,"Soliman"] #list
h=("mahmoud","Mohammed","Abd El-Atty" ,"Soliman") #tuple
i={"mahmoud","Mohammed","Abd El-Atty" ,"Soliman"} #set
j={"firstName" :"Mahmoud" ,"secondName":"Mohammed" ,"lastName" : "Soliman"} #dictionary

print("This is Exmples of type() function ")


print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))
print(type(h))
print(type(i))
print(type(j))

print("\n" + "This is Exmples of isinstance() function ")


print(isinstance(a,int))
print(isinstance(b,int))
print(isinstance(c,(int,str,bool)))
print(isinstance(c,(int,str,bool,complex)))

output
This is Exmples of type() function
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>

This is Exmples of isinstance() function


True
False
False
True

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 12
‫انذرس انثانث ‪ :‬األرقاو‬
‫) ‪( Numbers‬‬
‫‪ ‬فذذ‪٘ ٟ‬ذذزا اٌجشٔذذبِظ اٌزؼٍ‪ّ١‬ذذ‪ ٟ‬عذذززؼشف ػٍذذ‪ ٝ‬ا سلذذبَ ‪ numbers‬اٌّخزٍفذذخ اٌّغذذزخذِخ فذذ‪Python ٟ‬‬
‫‪ٚ‬و‪١‬ف‪١‬ذذذذخ اٌزؾ‪٠ٛ‬ذذذذً ‪ِ conversion‬ذذذذٓ ٔذذذذ‪ٛ‬ع ئٌذذذذ‪ ٝ‬آخذذذذش ‪ٚ‬اٌؼٍّ‪١‬ذذذذبد اٌؾغذذذذبث‪١‬خ ‪mathematical‬‬
‫‪ operations‬اٌّذػ‪ِٛ‬خ ف‪ٌ ٟ‬غخ ‪. Python‬‬

‫‪ : Numbers‬ا سلبَ‬
‫ٕ٘بن صالصخ أٔ‪ٛ‬اع سلّ‪١‬خ ف‪ ٟ‬ثب‪٠‬ض‪: ْٛ‬‬
‫‪ : int ‬ا سلبَ اٌصؾ‪١‬ؾخ ‪ِ Integers numbers‬ضً ‪ 5‬أ‪. -5 ٚ‬‬
‫٘‪ ٛ‬أ‪ ٞ‬ػذد صؾ‪١‬ؼ ِ‪ٛ‬عت أ‪ ٚ‬عبٌت ثذ‪ ْٚ‬وغ‪ٛ‬س ػشش‪٠‬خ ثط‪ٛ‬ي غ‪١‬ش ِؾذ‪ٚ‬د ‪.‬‬
‫)"‪print("integer data types‬‬
‫‪x1=5 ; y1=-5 ; z1= 36783902987463562‬‬
‫))‪print(type(x1‬‬
‫))‪print(type(y1‬‬
‫))‪print(type(z1‬‬

‫‪output‬‬
‫‪integer data types‬‬
‫>'‪<class 'int‬‬
‫>'‪<class 'int‬‬
‫>'‪<class 'int‬‬

‫‪ : float ‬ا سلبَ اٌؼشش‪٠‬خ ‪ِ floating numbers‬ضً ‪ 5.4‬أ‪-5.4 ٚ‬‬


‫٘‪ ٛ‬أ‪ ٞ‬سلُ ػشش‪ِٛ ٞ‬عت أ‪ ٚ‬عبٌت ‪٠ٚ‬أخذز ِذٓ سلذُ ‪ 1‬ئٌذ‪ 15 ٟ‬سلذُ ػشذش‪ٚ ٞ‬ئرا رذُ ئدخذبي أوضذش‬
‫ِٓ ‪ 15‬سلُ ػشش‪ ٞ‬ع‪١‬زُ ؽزف اٌم‪ ُ١‬اٌؼشش‪٠‬خ اٌضائذح ‪.‬‬
‫)"‪print( "float data types‬‬
‫‪x2= 1.10 ; y2 = 1.0 ; z2 = -35.59‬‬
‫))‪print(type(x2‬‬
‫))‪print(type(y2‬‬
‫))‪print(type(z2‬‬

‫‪output‬‬
‫‪float data types‬‬
‫>'‪<class 'float‬‬
‫>'‪<class 'float‬‬
‫>'‪<class 'float‬‬

‫‪ ‬ث‪ّٕ١‬ب ‪ّ٠‬ىٓ أْ رى‪ ْٛ‬ا ػذاد اٌصؾ‪١‬ؾخ ثأ‪ ٞ‬غ‪ٛ‬ي فاْ االسلبَ اٌؼشش‪٠‬خ رى‪ ْٛ‬ل‪ ُ١‬دل‪١‬مخ فمػ ؽز‪ٝ‬‬
‫‪ 15‬سلُ ػشش‪ ٞ‬ثؼذ اٌؼالِخ (اٌّىبْ اٌغبدط ػشش غ‪١‬ش دل‪١‬ك)‪.‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪11‬‬
.10 ‫ح‬ٛ‫ ل‬ٌٝ‫" ٌإلشبسح ئ‬e" ‫خ ثؾشف‬١ٍّ‫ أسلب ًِب ػ‬Float ْٛ‫عب أْ رى‬
ً ٠‫ّىٓ أ‬٠ٚ
print("float data types in values e ")
x3 = 35e3 ; y3 = 12E4 ; z3 = -87.7e100
print(type(x3))
print(type(y3))
print(type(z3))

output
float data types in values e
<class 'float'>
<class 'float'>
<class 'float'>

-2-5j ٚ‫ أ‬2+5j ً‫ ِض‬complex numbers ‫ ا سلبَ اٌّشوجخ‬: complex 


‫ش‬١‫ ؽ‬x + yj ٌٟ‫ب ثبٌشىً اٌزب‬ٙ‫غز‬١‫زُ وزبثخ ص‬٠ٚ ٍٟ١‫" وغضت رخ‬j" ‫ؼجش ػٕٗ ثؾشف‬٠ ُ‫ سل‬ٞ‫ أ‬ٛ٘
. imaginary part ٍٟ١‫ اٌغضت اٌزخ‬y ٚ real part ٟ‫م‬١‫ اٌغضت اٌؾم‬ٛ٘ x

print("complex data types")


x4 = 3+5j ; y4 = 5j ; z4 = -5j
print(type(x4))
print(type(y4))
print(type(z4))

output
complex data types
<class 'complex'>
<class 'complex'>
<class 'complex'>

: ‫خ‬١‫ر‬٢‫اي ا‬ٚ‫ آخش ثبعزخذاَ اٌذ‬ٌٝ‫ع ئ‬ٛٔ ِٓ ً٠ٛ‫ّىٕه اٌزؾ‬٠ : Type Conversion
. int ‫ع‬ٛٔ ٌٝ‫ ئ‬object ‫ع‬ٛٔ ٞ‫ً ِٓ أ‬٠ٛ‫ ٌٍزؾ‬: int(object) ‫داٌخ‬ 
. float ‫ع‬ٛٔ ٌٝ‫ ئ‬object ‫ع‬ٛٔ ٞ‫ً ِٓ أ‬٠ٛ‫ ٌٍزؾ‬: float(object) ‫داٌخ‬ 
. complex ‫ع‬ٛٔ ٌٝ‫ ئ‬object ‫ع‬ٛٔ ٞ‫ً ِٓ أ‬٠ٛ‫ ٌٍزؾ‬: complex(object) ‫داٌخ‬ 
. string ‫ع‬ٛٔ ٌٝ‫ ئ‬object ‫ع‬ٛٔ ٞ‫ً ِٓ أ‬٠ٛ‫ ٌٍزؾ‬: str(object) ‫داٌخ‬ 

‫ّذخ‬١‫ ل‬ٟ‫ ئٌذ‬5.9 ‫ّذخ‬١‫ذً اٌم‬٠ٛ‫بٔذبد فّذضالً ػٕذذ رؾ‬١‫ اٌج‬ٟ‫ؾذس فمذذ فذ‬٠ ْ‫ فا‬int ٌٝ‫ ئ‬float ِٓ ً٠ٛ‫ ػٕذ اٌزؾ‬
. 0.9 ‫ّخ‬١‫زُ رغبً٘ ل‬٠ ٗٔ ‫ش‬١‫س ؽ‬ٛ‫ّخ اٌىغ‬١‫زُ فمذ ل‬٠ٚ 5 ‫ْ إٌبرظ‬ٛ‫ى‬١‫ ع‬int ‫ؾخ‬١‫صؾ‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 14
. ‫ع سلُ آخش‬ٛٔ ٌٝ‫ ئ‬complex ‫ً ا سلبَ اٌّشوجخ‬٠ٛ‫ّىٕه رؾ‬٠ ‫ ال‬
x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:


print("convert from int to float:")
a = float(x)
print(a)
print(type(a))

#convert from float to int:


print("\n"+"convert from float to int:")
b = int(y)
print(b)
print(type(b))

#convert from int to complex:


print("\n"+"convert from int to complex:")
c = complex(x)
print(c)
print(type(c))

#convert from int to complex:


print("\n"+"convert from int to string:")
c = str(x)
print(c)
print(type(c))

output
convert from int to float:
1.0
<class 'float'>

convert from float to int:


2
<class 'int'>

convert from int to complex:


(1+0j)
<class 'complex'>
convert from int to string:
1
<class 'str'>

ٟ‫مذ‬١‫ّخ اٌغضت اٌؾم‬١‫ب ٌم‬ٙ‫ص‬١‫زُ رخص‬١‫ ع‬int ُ‫ّخ اٌشل‬١‫ فاْ اٌم‬complex ٌٝ‫ ئ‬int ِٓ ً٠ٛ‫ الؽع ػٕذ اٌزؾ‬
ٞٚ‫ رغذب‬ٟ‫ٍذ‬١‫ّذخ اٌغذضت اٌزخ‬١‫صذجؼ ل‬٠ ٌٟ‫ثبٌزب‬ٚ )0j) ‫ّخ‬١‫ ثبٌم‬ٍٟ١‫عغ اٌغضت اٌزخ‬١‫ع‬ٚ real number
. 1 + 0j ‫ْ إٌبرظ‬ٛ‫ى‬١‫ ع‬complex ‫ّخ‬١‫ ل‬ٌٟ‫ ئ‬1 ‫ّخ‬١‫ً اٌم‬٠ٛ‫صفش فّضالً ػٕذ رؾ‬
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 26
‫‪ : Python Decimal‬اٌزؼبًِ ِغ ا سلبَ اٌؼشش‪٠‬خ داخً ٌغخ اٌجبص‪ْٛ‬‬
‫رم‪ َٛ‬فئخ ا سلذبَ اٌؼشذش‪٠‬خ ‪ class float‬اٌّذذِظ فذ‪ Python ٟ‬ثذجؼط اٌؾغذبثبد اٌزذ‪ ٟ‬لذذ رذ٘شذٕب‪ٔ .‬ؼٍذُ‬
‫عّ‪١‬ؼًب أْ ِغّ‪ٛ‬ع ‪٠ 2.2 ٚ 1.1‬غب‪ٌ 3.3 ٞٚ‬ىٓ ٌغخ اٌجب‪٠‬ض‪ ْٛ‬غ‪١‬ش ِ‪ٛ‬افك ٌ‪ٙ‬زا إٌبرظ ‪.‬‬
‫‪x= (1.1 + 2.2) == 3.3‬‬
‫)‪print (x‬‬

‫‪output‬‬
‫‪False‬‬

‫‪ِ ‬ب اٌز‪ ٞ‬ؽذس ؟‬


‫‪ ‬ارعؼ أْ ا سلبَ اٌؼشش‪٠‬خ ‪٠ float‬زُ رٕف‪١‬ز٘ب ف‪ ٟ‬أع‪ٙ‬ذضح اٌىّج‪١‬ذ‪ٛ‬رش وىغذ‪ٛ‬س صٕبئ‪١‬ذخ ‪binary fractions‬‬
‫ْ اٌىّج‪ٛ١‬رش ال ‪٠‬ف‪ ُٙ‬ع‪ ٜٛ‬إٌظبَ اٌضٕبئ‪ٌٙ binary (0 and 1) ٟ‬زا اٌغجت ال ‪ّ٠‬ىٓ رخذض‪ِ ٓ٠‬ؼظذُ‬
‫اٌىغ‪ٛ‬س اٌؼشش‪٠‬خ ‪ decimal fractions‬اٌز‪ٔ ٟ‬ؼشف‪ٙ‬ب ثذلخ ف‪ ٟ‬ع‪ٙ‬بص اٌىّج‪ٛ١‬رش اٌخب ثٕب‪.‬‬

‫وشلُ ػشش‪ ٞ‬ع‪١‬ؼط‪٘ ٟ‬زا ‪٘ٚ 0.33333333...‬ذ‪ ٛ‬لذ‪ ُ١‬غ‪٠ٛ‬ذً ثذال‬ ‫‪ٌٕ ‬أخز ِضبال ال ‪ّ٠‬ىٕٕب رّض‪ ً١‬اٌىغش‬
‫ؽذ‪ٚ‬د ‪ّ٠ٚ infinitely long‬ىٕٕب فمػ رمش‪٠‬جٗ‪.‬‬

‫‪ ‬ارعذذذذذذذؼ أْ اٌىغذذذذذذذش اٌؼشذذذذذذذش‪ 0.1 ٞ‬عذذذذذذذ‪ٕ١‬زظ ػٕذذذذذذذٗ وغذذذذذذذش صٕذذذذذذذبئ‪ ٟ‬غ‪٠ٛ‬ذذذذذذذً ثذذذذذذذال ؽذذذذذذذذ‪ٚ‬د ٌذذذذذذذـ‬
‫‪٠ٚ 0.000110011001100110011...‬خذذضْ ع‪ٙ‬ذذبص اٌىّج‪١‬ذذ‪ٛ‬رش ٌذذذ‪ٕ٠‬ب ػذذذدًا ِؾذذذ‪ٚ‬دًا ِٕذذٗ فمذذػ‪٘ٚ .‬ذذزا‬
‫ع‪١‬مبسة ‪ 0.1‬فمػ ‪ٌٚ‬ىٕٗ ٌٓ ‪٠‬ى‪ِ ْٛ‬زغب‪ً٠ٚ‬ب أثذًا‪ ِٓٚ .‬صُ فاْ ٘زا ٘ذ‪ ٛ‬اٌؾذذ ِذٓ أع‪ٙ‬ذضح اٌىّج‪١‬ذ‪ٛ‬رش ٌذذ‪ٕ٠‬ب‬
‫‪١ٌٚ‬ظ خطأ ف‪.Python ٟ‬‬
‫‪x= 1.1 + 2.2‬‬
‫)‪print (x‬‬

‫‪output‬‬
‫‪3.3000000000000003‬‬

‫لللغل ر علررى هررذ المشرركلة ‪ٌ ،‬مكننررا اسررلخدام ‪ decimal module‬اللررً لررألً م ر ‪ . Python‬بٌنمررا‬
‫إلى ‪ 51‬رقم عشرًٌا ‪ ،‬رنن ‪decimal module‬‬ ‫لحلوي ‪floating-point numbers‬على دقة ل‬
‫للمل بدقة ٌمكن للمسلخدم ضبطها‪.‬‬
‫‪import decimal‬‬
‫)‪print(0.1‬‬
‫))‪print(decimal.Decimal(0.1‬‬
‫‪output‬‬
‫‪0.1‬‬
‫‪0.1000000000000000055511151231257827021181583404541015625‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪21‬‬
‫‪ ‬رُغزخذَ ٘زٖ ‪ module‬ػٕذذِب ٔش‪٠‬ذذ ئعذشات ؽغذبثبد ػشذش‪٠‬خ وّذب رؼٍّٕذب فذ‪ ٟ‬اٌّذسعذخ‪ .‬وّذب أٔذٗ ‪٠‬ؾذبفع‬
‫ػٍ‪ ٝ‬اٌّغض‪ٔ . significance ٜ‬ؼٍُ أْ ‪ 25.50‬وغُ ٘‪ ٟ‬أوضش دلخ ِذٓ ‪ 25.5‬وغذُ ٔ‪ٙ‬ذب رؾزذ‪ ٞٛ‬ػٍذ‪ٝ‬‬
‫سلّ‪ ٓ١‬ػشش‪ِ ٓ١ِّٙ ٓ١٠‬مبسٔخ ثشلُ ػشش‪ٚ ٞ‬اؽذ ‪.‬‬
‫‪from decimal import Decimal as D‬‬

‫))'‪print(D('1.1') + D('2.2‬‬
‫))'‪print(D('1.2') * D('2.50‬‬

‫‪output‬‬
‫‪3.3‬‬
‫‪3.000‬‬

‫‪ ‬الؽع ا صفبس ‪ zeros‬اٌالؽمخ ف‪ ٟ‬اٌّضبي اٌغبثك ‪.‬‬

‫‪ ‬لذ ٔزغبتي ٌّبرا ال ٔطجك ‪ Decimal‬وً ِشح ثذالً ِٓ ‪ float‬؟ اٌغذجت اٌشئ‪١‬غذ‪٘ ٟ‬ذ‪ ٛ‬اٌىفذبتح ‪٠‬غذت أْ‬
‫‪٠‬زُ رٕف‪١‬ز ػٍّ‪١‬بد ‪ Floating point‬ثشىً أعشع ِٓ ػٍّ‪١‬بد ‪.Decimal‬‬

‫‪ِ ‬ز‪٠ ٝ‬زُ اعزخذاَ ‪ Decimal‬ثذالً ِٓ ‪ float‬؟ ٔغزخذَ ثشىً ػبَ ‪ Decimal‬ف‪ ٟ‬اٌؾبالد اٌزبٌ‪١‬خ ‪:‬‬
‫‪ o‬ػٕذِب ٔغش‪ ٞ‬رطج‪١‬مبد ِبٌ‪١‬خ ‪ financial applications‬رؾزبط ئٌ‪ ٝ‬رّض‪ decimal ً١‬دل‪١‬ك‪.‬‬
‫‪ o‬ػٕذِب ٔش‪٠‬ذ اٌزؾىُ ف‪ِ ٟ‬غز‪ ٜٛ‬اٌذلخ اٌّطٍ‪ٛ‬ثخ ‪. control the level of precision‬‬
‫‪ o‬ػٕذِب ٔش‪٠‬ذ رطج‪١‬ك ِف‪ِٛ َٛٙ‬اظغ ‪ٚ‬أِبوٓ اٌؼالِخ اٌؼشش‪٠‬خ اٌّ‪ّٙ‬خ‪.‬‬

‫‪ : Python Fractions‬اٌزؼبًِ ِغ اٌىغ‪ٛ‬س اٌؼشش‪٠‬خ داخً ٌغخ اٌجبص‪ْٛ‬‬


‫ر‪ٛ‬فش ٌغخ ‪ Python‬ػٍّ‪١‬بد رزعذّٓ أػذذادًا وغذش‪٠‬خ ‪ِ fractional numbers‬ذٓ خذالي ‪ٚ‬ؽذذح اٌىغذ‪ٛ‬س‬
‫اٌخبصخ ث‪ٙ‬ب ‪ . fractions module‬وّب ٔؼٍُ أْ اٌىغذش ‪٘ fraction‬ذ‪ ٛ‬ػجذبسح ػذٓ ثغذػ ‪numerator‬‬
‫‪ِٚ‬مبَ ‪ٚ denominator‬والّ٘ب ػذد صؾ‪١‬ؼ‪ .‬رؾز‪٘ ٞٛ‬زٖ اٌ‪ٛ‬ؽذح ػٍذ‪ ٝ‬دػذُ ٌؾغذبة اٌؼذذد إٌّطمذ‪ٟ‬‬
‫‪.rational number arithmetic‬‬

‫‪ّ٠ ‬ىٕٕب ئٔشبت ‪ Fraction objects‬ثطشق ِخزٍفخ‪ .‬دػ‪ٔٛ‬ب ٍٔم‪ٔ ٟ‬ظشح ػٍ‪.ُٙ١‬‬
‫‪import fractions‬‬

‫))‪print(fractions.Fraction(1.5‬‬
‫))‪print(fractions.Fraction(5‬‬
‫))‪print(fractions.Fraction(1,3‬‬

‫‪output‬‬
‫‪3/2‬‬
‫‪5‬‬
‫‪1/3‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪22‬‬
ٝ‫شعغ رٌذه ئٌذ‬٠ٚ .‫خ‬٠‫ش اٌؼبد‬١‫ ثؼط إٌزبئظ غ‬ٍٝ‫ لذ ٔؾصً ػ‬float ‫خ‬٠‫ أصٕبت ئٔشبت اٌىغش ِٓ أسلبَ ػشش‬
. ً ‫ وّب رّذ ِٕبلشزٗ عبثمب‬ٟ‫خ اٌضٕبئ‬٠‫ش اٌىبًِ ٌالسلبَ اٌؼشش‬١‫ً غ‬١‫اٌزّض‬

ً ٠‫ أ‬string ‫ وذذبئٓ ثغٍغذذٍخ‬ٚ‫ذذً أ‬١‫ ثأشذذبت ِض‬Fraction ‫غذذّؼ ٌٕذذب‬٠ ‫ ٌؾغذذٓ اٌؾذذع‬
‫ذذبس‬١‫ اٌخ‬ٛ‫٘ذذزا ٘ذذ‬ٚ .‫عذذب‬
. decimal numbers ‫خ‬٠‫اٌّفعً ػٕذ اعزخذاَ ا سلبَ اٌؼشش‬
import fractions

# As float
# Output: 2476979795053773/2251799813685248
print(fractions.Fraction(1.1))

# As string
# Output: 11/10
print(fractions.Fraction('1.1'))

output
2476979795053773/2251799813685248
11/10

.‫ رٌه‬ٍٝ‫ ثؼط ا ِضٍخ ػ‬ٍٟ٠ ‫ّب‬١‫ف‬ٚ .‫خ‬١‫بد ا عبع‬١ٍّ‫غ اٌؼ‬١ّ‫بٔبد ع‬١‫ع اٌج‬ٛٔ ‫ذػُ ٘زا‬٠ٚ 
from fractions import Fraction as F

print(F(1, 3) + F(1, 3))


print(1 / F(5, 6))
print(F(-3, 10) > 0)
print(F(-3, 10) < 0)

output
2/3
6/5
False
True

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 23
‫‪ :Python Mathematics‬اٌزؼبًِ ِغ اٌؾغبثبد أ‪ ٚ‬اٌش‪٠‬بظ‪١‬بد داخً ٌغخ اٌجبص‪ْٛ‬‬
‫رذذذ‪ٛ‬فش ٌغذذذخ ‪ٚ Python‬ؽذذذذاد ‪ِ modules‬ضذذذً اٌش‪٠‬بظذذذ‪١‬بد ‪ٚ math‬اٌؼشذذذ‪ٛ‬ائ‪١‬خ ‪ٌ random‬زٕف‪١‬ذذذز‬
‫ؽغذذذذبثبد س‪٠‬بظذذذذ‪١‬خ ِخزٍفذذذذخ ِضذذذذً ػٍذذذذُ ؽغذذذذبة اٌّضٍضذذذذبد ‪ٚ trigonometry‬ػٍذذذذُ اٌٍ‪ٛ‬غبس‪٠‬زّذذذذبد‬
‫‪ٚ logarithms‬ػٍُ االؽزّبالد ‪ٚ probability‬ػٍُ اإلؽصبت ‪ statistics‬ئٌ ‪.‬‬

‫‪ٕ٘ ‬ذذبن اٌىض‪١‬ذذش ِذذٓ اٌذذذ‪ٚ‬اي ‪ٚ‬اٌخذذ‪ٛ‬ا اٌزذذ‪ّ٠ ٟ‬ىٕٕذذب اعذذزخذاِ‪ٙ‬ب داخذذً ‪ٚ‬ؽذذذح ‪ٌٍٚ math‬زؼذذشف ػٍذذ‪٘ ٝ‬ذذزٖ‬
‫اٌذ‪ٚ‬اي ‪ٚ‬اٌخ‪ٛ‬ا ‪ّ٠‬ىٕه اٌذخ‪ٛ‬ي ػٍ‪٘ ٝ‬زا اٌشاثػ ‪. www.programiz.com‬‬

‫‪import math‬‬

‫)‪print(math.pi‬‬
‫))‪print(math.cos(math.pi‬‬
‫))‪print(math.exp(10‬‬
‫))‪print(math.log10(1000‬‬
‫))‪print(math.sinh(1‬‬
‫))‪print(math.factorial(6‬‬

‫‪output‬‬
‫‪3.141592653589793‬‬
‫‪-1.0‬‬
‫‪22026.465794806718‬‬
‫‪3.0‬‬
‫‪1.1752011936438014‬‬
‫‪720‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪24‬‬
ٟ‫ائ‬ٛ‫ اٌشلُ اٌؼش‬:Random Number
‫ ٌىذٓ ٌغذخ‬random number ٟ‫ائ‬ٛ‫ إلٔشذبت سلذُ ػشذ‬random() ‫ داٌذخ‬ٝ‫ ػٍذ‬Python ‫ ٌغذخ‬ٞٛ‫ال رؾز‬
َ‫ب إلٔشذبت أسلذب‬ِٙ‫ّىٓ اعزخذا‬٠ random ّٝ‫ رغ‬built-in module ‫خ ِذِغخ‬١‫ؽذح ّٔط‬ٚ ‫ب‬ٙ‫ ث‬Python
: random number ‫خ‬١‫ائ‬ٛ‫ػش‬

. ‫ب‬ِٙ‫ لجً اعزخذا‬import ‫شاد‬١‫ب اعز‬ٌٙ ًّ‫غت أْ ٔؼ‬٠ module ‫ؽذح‬ٚ ٞ‫ العزخذاَ أ‬
:randrange)( function 
. ‫ اٌ ُّؾذد‬object type ٓ‫ع اٌىبئ‬ٛٔ ‫داٌخ ثزشعغ‬
 Synax : randrange(start, stop, step)

‫ش٘ب‬٠‫ّىٓ رّش‬٠ ٟ‫ُ اٌّؼبِالد اٌز‬١‫ ل‬: Parameter Values 


. )Optional ‫خ‬٠‫بس‬١‫ّخ اخز‬١‫ ل‬ٝ٘ٚ( 0 ٝ٘ ‫خ‬١‫ّخ االفزشاظ‬١‫اٌم‬ٚ ‫ظغ ٌٍجذت‬ِٛ ‫ؾذد‬٠ ‫ؼ‬١‫ ػذد صؾ‬: start 
. )Required( ‫خ‬٠‫ب‬ٌٕٙ‫ظغ ا‬ِٛ ‫ؾذد‬٠ ‫ؼ‬١‫ ػذد صؾ‬: stop 
. )Optional ‫خ‬٠‫بس‬١‫ّخ اخز‬١‫ ل‬ٝ٘ٚ( 1 ٝ٘ ‫خ‬١‫ّخ االفزشاظ‬١‫اٌم‬ٚ ‫بدح‬٠‫ؾذد ِمذاس اٌض‬٠ ‫ؼ‬١‫ ػذد صؾ‬: step 

import random

x=random.randrange(1,10,1)
print("The random number is : ", x)

x=random.randrange(1,10,1)
print("The random number is : ", x)

x=random.randrange(1,10,1)
print("The random number is : ", x)

output
The random number is : 6
The random number is : 1
The random number is : 2

. ‫خ ِخزٍفخ‬١‫ائ‬ٛ‫ذ أسلبَ ػش‬١ٌٛ‫زُ ر‬٠ ‫ وً ِشح‬ٟ‫ الؽع ف‬


ٓ‫ذش ِذ‬١‫ اٌىض‬ٝ‫ٌٍزؼذشف ػٍذ‬ٚ ‫خ‬١‫ائ‬ٛ‫ذ أسلبَ ػش‬١ٌٛ‫ ٌز‬random module ً‫ داخ‬ٜ‫شح آخش‬١‫اي وض‬ٚ‫ ٕ٘بن د‬
. www.w3schools.com/ ‫ ٘زا اٌشاثػ‬ٍٝ‫ي ػ‬ٛ‫ّىٕه اٌذخ‬٠ ‫اي‬ٚ‫٘زٖ اٌذ‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 25
َ‫ أٔظّخ ا سلب‬: number systems
: َ‫ٕ٘بن أوضش ِٓ ٔظبَ ٌٍزؼبًِ ِغ ا سلب‬
ٞ‫ ٔظبَ ا سلبَ اٌؼشش‬: decimal number system 
. 10 ٜٚ‫ ٘زا إٌظبَ رغب‬ٟ‫ ف‬base ‫ّخ ا عبط‬١‫ل‬ٚ َٛ٠ ً‫ب و‬ٙ‫ ٔزؼبًِ ِؼ‬ٟ‫٘زا ٔظبَ ا سلبَ اٌز‬

ٟ‫ ٔظبَ ا سلبَ اٌضٕبئ‬: binary number system 


. 2 ٜٚ‫ ٘زا إٌظبَ رغب‬ٟ‫ ف‬base ‫ّخ ا عبط‬١‫ل‬ٚ ْٛ‫زؼبًِ ِؼٗ اٌّجشِغ‬٠ ٞ‫٘زا ٔظبَ ا سلبَ اٌز‬

‫ ػشش‬ٟ‫ ٔظبَ اٌغذاع‬: hexadecimal number system 


. 16 ٜٚ‫ ٘زا إٌظبَ رغب‬ٟ‫ ف‬base ‫ّخ ا عبط‬١‫ل‬ٚ ْٛ‫زؼبًِ ِؼٗ اٌّجشِغ‬٠ ٞ‫٘زا ٔظبَ ا سلبَ اٌز‬

ٟٔ‫ ٔظبَ ا سلبَ اٌضّب‬: octal number system 


. 8 ٜٚ‫ ٘زا إٌظبَ رغب‬ٟ‫ ف‬base ‫ّخ ا عبط‬١‫ل‬ٚ ْٛ‫زؼبًِ ِؼٗ اٌّجشِغ‬٠ ٞ‫٘زا ٔظبَ ا سلبَ اٌز‬

: ٟ‫ر‬٢‫ظغ ثبدئخ لجً ٘زا اٌشلُ وب‬ٚ ‫ً ٘زٖ ا سلبَ ِٓ خالي‬١‫ّىٕٕب رّض‬٠ ْٛ‫ض‬٠‫ ثب‬ٟ‫ ف‬

Number System Prefix

Binary '0b' or '0B'

Octal '0o' or '0O'

Hexadecimal '0x' or '0X'

# Output: 107
print(0b1101011)

# Output: 253 (251 + 2)


print(0xFB + 0b10)

# Output: 13
print(0o15)

output
107
253
13

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 26
‫انذرس انزابع ‪ :‬انسالسم (انُصوص)‬
‫) ‪( Strings‬‬
‫‪ ‬عذذززؼٍُ فذذ‪٘ ٟ‬ذذزا اٌجشٔذذبِظ اٌزؼٍ‪ّ١‬ذذ‪ ٟ‬ئٔشذذبت عالعذذً فذذ‪ٚ Python ٟ‬رٕغذذ‪١‬م‪ٙ‬ب ‪ٚ‬رؼذذذ‪ٍٙ٠‬ب ‪ٚ‬ؽذذزف‪ٙ‬ب ‪ٚ‬عذذ‪ٛ‬ف‬
‫رزؼشف ‪ٚ‬أ‪٠‬عب ػٍ‪ ٝ‬ػٍّ‪١‬بد ‪ٚٚ‬ظبئ اٌغٍغٍخ اٌّخزٍفخ‪.‬‬

‫‪ : Strings‬اٌغالعً أ‪ ٚ‬اٌغالعً إٌص‪١‬خ‬


‫٘ذذ‪ ٝ‬ػجذذبسح ػذذٓ عٍغذذٍخ ِذذٓ اٌؾذذش‪ٚ‬ف أ‪ ٚ‬أؽذذشف اٌزشِ‪١‬ذذض ‪ٚ Unicode‬اٌزذذ‪ ٟ‬رُؾذذبق ثؼالِذذبد االلزجذذبط‬
‫اٌّفذشدح ‪ٚ single quotes‬سِض٘ذب ٘ذ‪ ( ' ' ) ٛ‬أ‪ ٚ‬ػالِذبد االلزجذبط اٌّضد‪ٚ‬عذخ ‪double quotes‬‬
‫‪ٚ‬سِض٘ب ٘‪ ( " " ) ٛ‬أ‪ ٚ‬ػالِبد االلزجبط اٌضالص‪١‬خ ‪ triple quotes‬فذ‪ ٟ‬ؽبٌذخ وزبثذخ اٌغٍغذٍخ وضذش ِذٓ‬
‫عطش ‪ٚ‬سِض٘ب ٘‪ ( """ """ ) ٛ‬أ‪. ( ''' ''' ) ٚ‬‬

‫)"‪print("Hello‬‬
‫)'‪print('Hello‬‬
‫)'''‪print('''Hello‬‬
‫)"""‪print("""Hello‬‬

‫‪name = """Hello‬‬
‫""" ‪Mahmoud‬‬
‫)‪print(name‬‬

‫‪output‬‬
‫‪Hello‬‬
‫‪Hello‬‬
‫‪Hello‬‬
‫‪Hello‬‬
‫‪Hello‬‬
‫‪Mahmoud‬‬

‫‪ : Strings are Arrays‬اٌغالعً رزؼبًِ ِبٍِخ اٌّصف‪ٛ‬فبد‬


‫ِضً ٌغبد اٌجشِغخ اٌشبئؼخ ا خش‪ ٜ‬فاْ اٌغالعً ‪ strings‬داخً ٌغخ ‪ Python‬ػجبسح ػٓ ِصف‪ٛ‬فبد‬
‫‪ِ arrays‬ذذٓ اٌجب‪٠‬ذذذ ‪ bytes‬رّضذذً أؽذذشف ‪ِٚ Unicode‬ذذغ رٌذذه ال رؾزذذ‪ٌ ٞٛ‬غذذخ ‪ Python‬ػٍذذ‪ٔ ٝ‬ذذ‪ٛ‬ع‬
‫ث‪١‬بٔذذبد ؽشفذ‪ٌٚ character data type ٟ‬ىٕ‪ٙ‬ذذب رزؼبِذذً ِذذغ اٌؾذذشف اٌفذذشد‪single character ٞ‬‬
‫وغٍغٍخ ‪ِ string‬مذاس غ‪ٌٙٛ‬ب ‪. 1‬‬

‫‪ ‬و‪١‬ف‪١‬خ اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ا ؽشف أ‪ ٚ‬اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح داخً عٍغٍخ؟‬


‫‪ّ٠ ‬ىٕٕذذب اٌ‪ٛ‬صذذ‪ٛ‬ي ئٌذذ‪ ٝ‬ا ؽذذشف اٌفشد‪٠‬ذذخ أ‪ِ ٚ‬غّ‪ٛ‬ػذذخ ِذذٓ ا ؽذذشف ثبعذذزخذاَ اٌف‪ٙ‬شعذذخ ‪ٚ index‬ػبِذذً‬
‫اٌزمط‪١‬غ ( ‪٠ ) :‬جذأ اٌف‪ٙ‬شط ِٓ ‪. 0‬‬

‫‪ ‬اٌف‪ٙ‬شط ] ‪ : [ index‬اٌم‪ّ١‬خ اٌز‪ٔ ٟ‬عؼ‪ٙ‬ب داخً ] [ ‪ ٝ٘ٚ‬ػجبسح ػٓ سلذُ اٌف‪ٙ‬ذشط اٌذز‪٠ ٞ‬ؼجذش ػذٓ ل‪ّ١‬ذخ‬
‫ِ‪ٛ‬ظغ أ‪ِ ٚ‬ىبْ اٌؾشف فّضالً ]‪ ٛ٘ [4‬ػجبسح ػٓ ل‪ّ١‬خ اٌشلُ ‪ 4‬اٌّ‪ٛ‬ع‪ٛ‬د ف‪ ٟ‬اٌغٍغٍخ ‪.‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪22‬‬
‫ش‬١‫شذذ‬٠ ‫ذذش‬١‫ ؽ‬string ‫ داخذذً اٌغٍغذذٍخ‬negative indexing ‫خ‬١‫شعذذخ اٌغذذٍج‬ٙ‫ ثبٌف‬Python ‫ رغذذّؼ ٌغذذخ‬
‫اٌؼٕصذذذذذذذذذذذش‬ ٝ‫ ] ئٌذ‬-1 [ ‫شط‬ٙ‫اٌف‬
-2 [ ‫ش‬١‫ش‬٠ٚ ‫ذذذذش ٌٍغٍغذذذذٍخ‬١‫ا خ‬
‫ذذذذذذذذذذذذذذش‬١‫خ‬٢‫ا‬ ً‫ اٌؼٕصذذش لجذذ‬ٝ‫] ئٌذذ‬
. ‫٘ىزا‬ٚ

‫ ػٕبصذش اٌغٍغذٍخ‬ٝ‫ي ئٌذ‬ٛ‫صذ‬ٌٍٛ ] [ ٛ‫سِض٘ذب ٘ذ‬ٚ Square brackets ‫اط اٌّشثؼذخ‬ٛ‫ذزُ اعذزخذاَ ا لذ‬٠ 
‫ ] العذزخشاط‬: [ ٛ٘ ‫سِض٘ب‬ٚ slicing operator ‫غ‬١‫اط اٌّشثؼخ ِغ ػبًِ اٌزمط‬ٛ‫ّىٓ اعزخذاَ ا ل‬٠ٚ
. string ‫ػخ ِٓ اٌؼٕبصش ِٓ داخً اٌغٍغٍخ‬ّٛ‫ِغ‬
#Accessing string characters in Python
str = 'programiz'
print('str = ', str)

#first character
print('str[0] = ', str[0])

#last character
print('str[-1] = ', str[-1])

#slicing 2nd to 5th character


print('str[1:5] = ', str[1:5])

#Negative Indexing
print('str[-5:-2] = ', str[-5:-2])

#slicing 6th to 2nd last character


print('str[5:-2] = ', str[5:-2])

#slicing From the Start


print('str[:5] = ', str[:5])

#slicing to the End


print('str[2:] = ', str[2:])

output
str = programiz
str[0] = p
str[-1] = z
str[1:5] = rogr
str[-5:-2] = ram
str[5:-2] = am
str[:5] = progr
str[2:] = ogramiz

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 21
ّٝ‫غذ‬٠ ‫س خطذأ‬ٚ‫ ؽذذ‬ٝ‫ رٌذه ئٌذ‬ٞ‫إد‬١‫ عذ‬index ‫ذشط‬ٙ‫ ؽشف خذبسط ٔطذبق اٌف‬ٌٝ‫ي ئ‬ٛ‫ص‬ٌٛ‫ٌخ ا‬ٚ‫ ػٕذ ِؾب‬
.‫ ًؾب‬١‫ ػذدًا صؾ‬index ْٛ‫ى‬٠ ْ‫غت أ‬٠ ٌٟ‫ثبٌزب‬ٚ IndexError
my_string = 'programiz'
# index must be in range
my_string[15]

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 4, in <module>
my_string[15]
IndexError: string index out of range

.TypeError ٌٝ‫ رٌه ئ‬ٞ‫إد‬١‫ ع‬ٜ‫اع أخش‬ٛٔ‫ أ‬ٚ‫ أ‬floats َ‫ٌخ اعزخذا‬ٚ‫ ػٕذ ِؾب‬
my_string = 'programiz'
# index must be in range
my_string[15]

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
my_string[5.4]
TypeError: string indices must be integers

‫ّذخ ػٕبصذش اٌغٍغذٍخ ثؼذذ‬١‫ش ل‬١١‫ّىٓ رغ‬٠ ‫ أٔٗ ال‬ٕٝ‫ ثّؼ‬immutable ‫ش‬١١‫ش لبثً ٌٍزغ‬١‫ غ‬strings ً‫ اٌغالع‬
: ٌٟ‫ ِخزٍفخ ٌٕفظ االعُ الؽع اٌّضبي اٌزب‬string ٓ١١‫ّىٕٕب ئػبدح رؼ‬٠ ‫ٌىٕٕب ثجغبغخ‬ٚ ‫ٓ اٌغٍغٍخ‬١١‫رؼ‬
my_string = 'programiz'
my_string[5] = 'a'

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 2, in <module>
my_string[5] = 'a'
TypeError: 'str' object does not support item assignment

. ً ‫ٕٗ ِغجمب‬١‫خ رغزخذَ ٌؾزف وبئٓ رُ رؼ‬١‫ وٍّخ أعبع‬ٝ٘ : del keyword 

string ‫ ئصاٌخ ا ؽشف ِٓ عٍغٍخ‬ٚ‫ّىٕٕب ؽزف أ‬٠ ‫ ال‬


my_string = 'programiz'
del my_string[5]

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 2, in <module>
del my_string[5]
TypeError: 'str' object doesn't support item deletion
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 24
. del ‫خ‬١‫ّىٓ ؽزف اٌغٍغٍخ ثبٌىبًِ ثبعزخذاَ اٌىٍّخ ا عبع‬٠ ٓ‫ ٌى‬
my_string = 'programiz'
del my_string
print(my_string)

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
print(my_string)
NameError: name 'my_string' is not defined

ْٛ‫ض‬٠‫ اٌغٍغٍخ داخً ٌغخ اٌجب‬ٍٝ‫بد ػ‬١ٍّ‫ ػ‬: Python String Operations
‫اؽذذح‬ٚ ‫ب‬ٍٙ‫غؼ‬٠ ‫ ِّب‬String ً‫ اٌغالع‬ٍٝ‫ّىٓ ئعشاؤ٘ب ػ‬٠ ٟ‫ اٌز‬Operations ‫بد‬١ٍّ‫ذ ِٓ اٌؼ‬٠‫ٕ٘بن اٌؼذ‬
. ٍٟ٠ ‫ّب‬١‫ب ف‬ٙ١ٍ‫ عٕزؼشف ػ‬Python ٟ‫بٔبد اعزخذا ًِب ف‬١‫اع اٌج‬ٛٔ‫ِٓ أوضش أ‬

ً‫خ عّغ اٌغالع‬١ٍّ‫ ػ‬: Strings Concatenation : ً‫ال‬ٚ‫ أ‬


: ْ‫غزخذِب‬٠ ٓ١ٍِ‫ٕ٘بن ِؼب‬ٚ ‫اؽذح‬ٚ ‫ عٍغٍخ‬ٟ‫ أوضش ف‬ٚ‫ٓ أ‬١‫ظُ عٍغٍز‬ٚ ‫ب عّغ‬ٙ١‫زُ ف‬٠ ‫خ‬١ٍّ‫ ػ‬ٝ٘
. ً ‫ٓ ِؼب‬١‫عّغ عٍغٍز‬ٚ ‫ ٌذِظ‬: ) + ( ‫ ِؼبًِ اٌغّغ‬o
. ‫ ٌزىشاس اٌغٍغٍخ ػذد ِٓ اٌّشاد‬: ) * ( ‫ ِؼبًِ اٌعشة‬o
# Python String Operations
str1 = 'Hello'
str2 ='World!'
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)

output
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello

: ) ( ‫اط‬ٛ‫ّىٕٕب اعزخذاَ ا ل‬١‫س ِخزٍفخ ف‬ٛ‫ عط‬ٟ‫ ئرا أسدٔب سثػ اٌغالعً ف‬
# two string literals together
str1= 'Hello ''World!1'
print(str1)
# using parentheses
str2 = ('Hello '
'World!2')
print(str2)

output
Hello World!1
Hello World!2
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 36
ً‫خ رىشاس اٌغالع‬١ٍّ‫ ػ‬: Strings Iterating : ً ‫ب‬١ٔ‫ صب‬
ً‫س ػجذذش ا ؽذذشف داخذذ‬ٚ‫ّىٕٕذذب اٌّذذش‬١‫ ف‬arrays ‫فبد‬ٛ‫ ػجذذبسح ػذذٓ ِصذذف‬Strings ً‫ٔظذ ًذشا ْ اٌغالعذذ‬
. for ‫ ِضً ؽٍمخ‬Looping ‫خ‬٠‫اٌغٍغٍخ ثبعزخذاَ اٌؾٍمخ اٌزىشاس‬
# Iterating through a string
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')

output
3 letters found

strings ً‫ داخً اٌغالع‬looping ٍٟ‫ ِضبي آخش ػ‬


for x in "banana":
print(x)

output
b
a
n
a
n
a

ً‫ داخً اٌغالع‬ٛ‫خ اخزجبس ػع‬١ٍّ‫ ػ‬: Strings Membership Test : ً ‫ صبٌضب‬


‫دح‬ٛ‫عذ‬ِٛ ‫ذخ‬١‫خ داخذً اٌغٍغذٍخ ٌؼّذً اخزجذبس ِذب ئرا وبٔذذ اٌغٍغذٍخ اٌفشػ‬٠ٛ‫ّىٓ اعزخذاَ ِؼذبِالد اٌؼعذ‬٠
: ‫ٓ الخزجبس ػٕصش داخً عٍغٍخ ّ٘ب‬١‫ٕ٘بن وٍّز‬ٚ ‫ أَ ال ؟‬string ‫داخً عٍغٍخ‬
.‫ اٌغٍغٍخ‬ٟ‫ٓ ف‬١‫ ؽشف ِؼ‬ٚ‫د ػجبسح أ‬ٛ‫ع‬ٚ ِٓ ‫خ رغزخذَ ٌٍزؾمك‬١‫ وٍّخ أعبع‬ٝ٘ : in keyword 
.‫ اٌغٍغٍخ‬ٟ‫ٓ ف‬١‫د ؽشف ِؼ‬ٛ‫ع‬ٚ َ‫خ رغزخذَ ٌٍزؾمك ِٓ ػذ‬١‫ وٍّخ أعبع‬ٝ٘ : not in keyword 
txt1 = "The best things in life are free!"
print("free" in txt1)
print("Mahmoud" not in txt1)

output
True
True

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 31
‫خ‬٠ٛ‫ ِغ ِؼبِالد اٌؼع‬if ‫ّىٓ اعزخذاَ عٍّخ‬٠ 
txt1 = "The best things in life are free!"
if "Mahmoud" not in txt1:
print("Mahmoud is not Found.")

txt2 = "The best things in life are free!"


if "free" in txt2:
print("Yes, 'free' is present.")

output
Mahmoud is not Found.
Yes, 'free' is present.

ْٛ‫ض‬٠‫اي ٌٍزؼبًِ ِغ اٌغٍغٍخ داخً ٌغخ اٌجب‬ٚ‫ اٌذ‬: Python String Functions
: ‫ب‬ِٕٙ String ً‫ اٌغالع‬ٍٝ‫ّىٓ ئعشاؤ٘ب ػ‬٠ ٟ‫ اٌز‬Functions ‫اي‬ٚ‫ذ ِٓ اٌذ‬٠‫ٕ٘بن اٌؼذ‬

. )ٓ‫ ثزشعغ ػذد أؽشف اٌغٍغٍخ (اٌىبئ‬ٞ‫ي اٌغٍغٍخ أ‬ٛ‫ي ؛ ٌّؼشفخ غ‬ٛ‫ داٌخ اٌط‬: Len() function 
a = "Hello, World!"
print("Length of string is : ",len(a))

output
Length of string is : 13

‫ذذغ‬١ّ‫ّذذخ ع‬١‫ل‬ٚ index ‫ذذشط‬ٙ‫ ف‬ٝ‫ ػٍذذ‬ٞٛ‫ؾزذذ‬٠ ‫ داٌذذخ اٌؼذذذد؛ إلسع ذبع رؼذذذاد‬: enumerate() function 
. iteration ‫ذًا ٌٍزىشاس‬١‫ْ ٘زا ِف‬ٛ‫ى‬٠ ْ‫ّىٓ أ‬٠ٚ . pairs ‫اط‬ٚ‫ اٌغٍغٍخ وأص‬ٟ‫اٌؼٕبصش ف‬
str = 'cold'

# enumerate()
list_enumerate = list(enumerate(str))
print('list(enumerate(str) = ', list_enumerate)

output
list(enumerate(str) = [(0, 'c'), (1, 'o'), (2, 'l'), (3, 'd')]
len(str) = 4

. upper case ‫شح‬١‫ف وج‬ٚ‫ ؛ ٌغؼً اٌغٍغٍخ ؽش‬ٍٞٛ‫ داٌخ اٌؼ‬: upper() function 
. lower case ‫شح‬١‫ف صغ‬ٚ‫ ؛ ٌغؼً اٌغٍغٍخ ؽش‬ٍٟ‫ داٌخ اٌغف‬: lower() function 
.upper case ‫شح‬١‫ف وج‬ٚ‫ اٌغٍغٍخ ؽش‬ٟ‫ي ؽشف ِٓ وٍّخ ف‬ٚ‫ داٌخ اٌّمبي؛ ٌغؼً أ‬: title() function 

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 32
a = "my name is mahmoud soliman"
print(a.upper())
print(a.lower())
print(a.title())

output
MY NAME IS MAHMOUD SOLIMAN
my name is mahmoud soliman
My Name Is Mahmoud Soliman

‫خ‬٠‫ ثذا‬ٟ‫دح ف‬ٛ‫ع‬ٌّٛ‫ ا‬Whitespace ‫عبت‬١‫ئصاٌخ اٌّغبفخ اٌج‬ٚ ‫ذ ؛ ٌؾزف‬٠‫ داٌخ اٌزغش‬: strip() function 
. ‫ب‬ٙ‫ز‬٠‫ب‬ٙٔ ٟ‫ ف‬ٚ‫اٌغٍغٍخ أ‬
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

output
Hello, World!

. ٖ‫ ثؼذ‬ٚ‫ أ‬ٍٟ‫دح لجً إٌص اٌفؼ‬ٛ‫ع‬ٌّٛ‫ اٌّغبفخ ا‬ٟ٘ ‫عبت‬١‫ اٌّغبفخ اٌج‬: Whitespace 

Whitespace ‫عذذبت‬١‫ذذً اٌّغذذبفخ اٌج‬٠‫رض‬ٚ ‫ داٌذذخ ثزؾذذزف‬ٝ‫ داٌذذخ االعذذزجذاي ؛ ٘ذذ‬: replace() function 
. ‫ب‬ٙ‫ز‬٠‫ب‬ٙٔ ٟ‫ ف‬ٚ‫خ اٌغٍغٍخ أ‬٠‫ ثذا‬ٟ‫دح ف‬ٛ‫ع‬ٌّٛ‫ا‬
a = "Hello, World!"
print(a.replace("H", "J"))

output
Jello, World!

‫ّخ اٌ ُّشاد‬١‫ ( اٌم‬old value ‫ّخ‬٠‫ّخ اٌمذ‬١‫ اٌم‬ٝ٘ٚ ‫ي‬ٚ ‫ٓ اٌّؼبًِ ا‬١ٍِ‫ب ِؼب‬ٌٙ‫ ثّٕشس خال‬replace ‫ داٌخ‬
. ) ‫ب‬ٌٙ‫زُ اعزجذا‬١‫ ع‬ٟ‫ّخ اٌز‬١‫ ( اٌم‬new value ‫ذح‬٠‫ّخ اٌغذ‬١‫ اٌم‬ٝ٘ٚ ٟٔ‫اٌّؼبًِ اٌضب‬ٚ ) ‫ب‬ٌٙ‫اعزجذا‬

‫ االٔشطبس‬ٚ‫ داٌخ االٔمغبَ أ‬: split() function 


. separator ً‫ً ٌٍفبص‬١‫ ِض‬ٍٝ‫خ ئرا ػضشد ػ‬١‫ عالعً فشػ‬ٌٝ‫خ ئ‬١‫ داٌخ رمغُ اٌغٍغٍخ ا عبع‬ٝ٘
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

output
['Hello', ' World!']

. ) - ( ٚ‫ ) أ‬, ( ٚ‫ ) أ‬. ( ْٛ‫ى‬٠ ‫فّضالً لذ‬ ٛ‫ٓ إٌص‬١‫فصً ث‬٠ ‫ شئ‬ٞ‫ْ أ‬ٛ‫ى‬٠ ‫ اٌفبصً لذ‬: separator 

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 33
ْٛ‫ض‬٠‫ك اٌغٍغٍخ داخً ٌغخ اٌجب‬١‫ رٕغ‬:Python String Formatting
: ٟ‫ر‬٢‫ ا‬ٍٝ‫عٕزؼشف ػ‬ٚ ْٛ‫ض‬٠‫ اٌّخزٍفخ داخً ٌغخ اٌجب‬Formatting ‫ك‬١‫ذ ِٓ أشىبي اٌزٕغ‬٠‫ٕ٘بن اٌؼذ‬
. format() function ‫ك‬١‫ داٌخ اٌزٕغ‬: ً‫ال‬ٚ‫ أ‬
. Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ رغٍغً ا‬ٚ‫ف أ‬ٚ‫ ؽش‬: ً ‫ب‬١ٔ‫ صب‬
. Raw String sequence َ‫ عٍغٍخ اٌخب‬ٚ‫ ؽشف اٌخبَ أ‬: ً ‫ صبٌضب‬
. Old style formatting ُ٠‫ك إٌّػ اٌمذ‬١‫ رٕغ‬: ً ‫ ساثؼب‬

. ً‫ اٌغالع‬ٟ‫ك ؛ إلدساط ا سلبَ ف‬١‫ داٌخ اٌزٕغ‬: format() function : ً‫ال‬ٚ‫ أ‬
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))

output
My name is John, and I am 36

ْٛ‫ش رى‬١‫ اٌغٍغٍخ ؽ‬ٟ‫ب ف‬ٙ‫رعؼ‬ٚ ‫ب‬ٙ‫م‬١‫رٕغ‬ٚ ‫ش٘ب‬٠‫ رُ رّش‬ٟ‫ُ اٌّؼبِالد اٌز‬١‫ ل‬format() ‫ رأخز اٌذاٌخ‬
. } { ‫ اٌشِض‬ٝ٘ placeholders ‫اٌؼٕبصش إٌبئجخ‬
‫ب‬ٌٙ‫زُ اعزجذا‬٠ ‫ٍخ‬٠‫ي ثذ‬ٛ‫ ؽم‬ٚ‫فخ {} وؼٕبصش ٔبئجخ أ‬ٛ‫اط ِؼم‬ٛ‫ أل‬ٍٝ‫ك ػ‬١‫ عالعً اٌزٕغ‬ٞٛ‫ رؾز‬
: ‫ اٌؼٕبصش إٌبئجخ‬ٟ‫ب ف‬ٙ‫ظؼ‬ٚ ُ‫ز‬٠ٚ ‫د ِٓ اٌّؼبِالد‬ٚ‫ش ِؾذ‬١‫ ػذدًا غ‬format() ‫ رأخز اٌذاٌخ‬
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

output
I want 3 pieces of item 567 for 49.95 dollars.

: ‫ؾخ‬١‫ اٌؼٕبصش إٌبئجخ اٌصؾ‬ٟ‫ظغ اٌّؼبِالد ف‬ٚ ِٓ ‫} ٌٍزأوذ‬0{ ‫شط‬ٙ‫ّىٕه اعزخذاَ أسلبَ اٌف‬٠ 
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

output
I want to pay 49.95 dollars for 3 pieces of item 567.

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 34
. Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ رغٍغً ا‬ٚ‫ف أ‬ٚ‫ ؽش‬: ً ‫ب‬١ٔ‫ صب‬
. ‫ عٍغٍخ‬ٟ‫خ ف‬١ٔٛٔ‫ش لب‬١‫ف رغزخذَ إلدساط أؽشف غ‬ٚ‫ ؽش‬ٝ٘

) ' ' ) ‫ّىٕٕب اعزخذاَ ػالِذبد االلزجذبط اٌّفذشدح‬٠ ‫ " فال‬What's there ‫ ئرا أسدٔب غجبػخ ٔص ِضً " ؟‬
ٗ‫ذش أْ اٌذٕص ٔفغذ‬١‫ ثٕذبت اٌغٍّذخ ؽ‬ٟ‫ٕزظ خطذأ فذ‬١‫عذخ ) " " ( ْ ٘ذزا عذ‬ٚ‫ ػالِذبد االلزجذبط اٌّضد‬ٚ‫أ‬
. ‫عخ‬ٚ‫ِضد‬ٚ ‫ ػالِبد الزجبط ِفشدح‬ٍٝ‫ ػ‬ٞٛ‫ؾز‬٠
print("He said, "What's there?"")

output
C:\Users\Hoda_PC>python c:/Users/Hoda_PC/Desktop/example.py
File "c:\Users\Hoda_PC\Desktop\example.py", line 1
print("He said, "What's there?"")
^
SyntaxError: invalid syntax

ِٓ ً‫ ثذال‬ٚ‫ ) ''' ''' ( أ‬ٚ‫خ ) """ """ ( أ‬١‫ّىٓ اعزخذاَ ػالِبد االلزجبط اٌضالص‬٠ ‫ ٘زٖ اٌّشىٍخ‬ٍٝ‫ ٌٍزغٍت ػ‬
. Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ّىٕٕب اعزخذاَ رغٍغالد ا‬٠ ‫رٌه‬

( \ ) ٛ‫سِض٘ذب ٘ذ‬ٚ backslash ‫ ثشذشغخ ِبئٍذخ ٌٍخٍذ‬Escape Sequence ‫ة‬ٚ‫ذش‬ٌٙ‫جذذأ رغٍغذً ا‬٠ 
‫خ‬١‫ذً عٍغذٍخ ٔصذ‬١‫اؽذذح ٌزّض‬ٚ ‫ فّذضالً ئرا اعذزخذِٕب ػالِذخ الزجذبط‬. ٗ‫ذذ ئدساعذ‬٠‫ رش‬ٞ‫ػخ ثبٌؾشف اٌز‬ٛ‫ِزج‬
‫ اٌؾذبي ِذغ ػالِذبد االلزجذبط‬ٛ‫وّذب ٘ذ‬ٚ ‫غ ػالِبد االلزجبط اٌّفذشدح داخذً اٌغٍغذٍخ‬١ّ‫ ع‬ٟ‫غت رخط‬١‫ف‬
. ‫ً إٌص اٌغبثك‬١‫بَ ثزٌه ٌزّض‬١‫خ اٌم‬١‫ف‬١‫عخ الؽع و‬ٚ‫اٌّضد‬
print('''He said, "What's there?"''') # using triple quotes

print('He said, "What\'s there?"') # escaping single quotes

print("He said, \"What's there?\"") # escaping double quotes

output
He said, "What's there?"
He said, "What's there?"
He said, "What's there?"

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 35
Python ‫ب ٌغخ‬ّٙ‫ رذػ‬ٟ‫ اٌز‬Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫غ رغٍغالد ا‬١ّ‫ لبئّخ ثغ‬ٍٟ٠ ‫ّب‬١‫ ف‬

Escape Sequence Description


\\ Backslash
\' Single quote
\" Double quote
\a ASCII Bell
\b ASCII Backspace
\f ASCII Formfeed
\n ASCII Linefeed - newline
\r ASCII Carriage Return
\t ASCII Horizontal Tab
\v ASCII Vertical Tab
\ooo Character with octal value ooo
\xHH Character with hexadecimal value HH

print("C:\\Python32\\Lib")

print('It\'s alright.')

print("He said, \"What's there?\"")

print("Hello\nWorld!")

print("Hello\fWorld!")

print("Hello\rWorld!")

print("Hello\tWorld!")

print("Hello \bWorld!")

print("\110\145\154\154\157")

print("\x48\x65\x6c\x6c\x6f")

output
It's alright.
He said, "What's there?"
Hello
World!
Hello♀World!
World!
Hello World!
HelloWorld!
Hello
Hello?"

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 36
. Raw String sequence َ‫ عٍغٍخ اٌخب‬ٚ‫ ؽشف اٌخبَ أ‬: ً ‫ صبٌضب‬
. Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ف ا‬ٚ‫ف رغزخذَ ٌزغبً٘ ؽش‬ٚ‫ ؽش‬ٝ٘

َ‫ذب‬١‫ ٌٍم‬.‫ داخذً عٍغذٍخ‬Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ رغبً٘ رغٍغالد ا‬ٟ‫بْ لذ ٔشغت ف‬١‫ ثؼط ا ؽ‬ٟ‫ ف‬
ٞ‫زُ رغب٘ذً أ‬١‫عذ‬ٚ raw string َ‫ أٔذٗ عٍغذٍخ خذب‬ٟ‫ؼٕذ‬٠ ‫ ٘ذزا‬.‫ أِذبَ اٌغٍغذٍخ‬R ٚ‫ أ‬r ‫ظذغ‬ٚ ‫ّىٕٕذب‬٠ ‫ثذزٌه‬
.ٍٗ‫ة ثذاخ‬ٚ‫رغٍغً ٘ش‬
print("This is \x61 \ngood example")

print("\n")

print(r"This is \x61 \ngood example")

output
This is a
good example

This is \x61 \ngood example

. Old style formatting ُ٠‫ك إٌّػ اٌمذ‬١‫ رٕغ‬: ً ‫ ساثؼب‬


‫ذزا‬ٙ‫ذبَ ث‬١‫ ٌٍم‬% ً‫غذزخذَ ػبِذ‬٠ ‫ش‬١‫ ؽ‬C ‫ ٌغخ‬ٟ‫ اٌّغزخذَ ف‬sprintf() ‫ك اٌذاٌخ‬١‫شجٗ رٕغ‬٠ ُ٠‫ك لذ‬١‫ رٕغ‬ٛ٘
. ‫ك‬١‫ع ِٓ اٌزٕغ‬ٌٕٛ‫ا‬

َ‫ذب‬١‫ ٌٍم‬.‫ داخذً عٍغذٍخ‬Escape Sequence ‫ة‬ٚ‫ش‬ٌٙ‫ رغبً٘ رغٍغالد ا‬ٟ‫بْ لذ ٔشغت ف‬١‫ ثؼط ا ؽ‬ٟ‫ ف‬
ٞ‫زُ رغب٘ذً أ‬١‫عذ‬ٚ raw string َ‫ أٔذٗ عٍغذٍخ خذب‬ٟ‫ؼٕذ‬٠ ‫ ٘ذزا‬.‫ أِذبَ اٌغٍغذٍخ‬R ٚ‫ أ‬r ‫ظذغ‬ٚ ‫ّىٕٕذب‬٠ ‫ثذزٌه‬
.ٍٗ‫ة ثذاخ‬ٚ‫رغٍغً ٘ش‬
x = 12.3456789
print('The value of x is %3.2f' %x)

print('The value of x is %6.2f' %x)

print('The value of x is %7.2f' %x)

print('The value of x is %15.4f' %x)

output
The value of x is 12.35
The value of x is 12.35
The value of x is 12.35
The value of x is 12.3457

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 32
‫انذرس انخايس ‪ :‬انقوائى (انًصفوفات ‪)Arrays‬‬
‫) ‪( Lists‬‬
‫‪ ‬عززؼٍُ فذ‪٘ ٟ‬ذزا اٌجشٔذبِظ اٌزؼٍ‪ّ١‬ذ‪ ٟ‬ئٔشذبت اٌمذ‪ٛ‬ائُ فذ‪ٚ Python ٟ‬رمغذ‪ ُ١‬اٌمبئّذخ ‪ٚ‬ئظذبفخ اٌؼٕبصذش أ‪ٚ‬‬
‫ئصاٌز‪ٙ‬ب ِٕ‪ٙ‬ب ‪ِٚ‬ب ئٌ‪ ٝ‬رٌه ‪.‬‬

‫‪ : lists‬اٌم‪ٛ‬ائُ أ‪ ٚ‬اٌّصف‪ٛ‬فبد ‪( Arrays‬وّب ف‪ٌ ٟ‬غبد اٌجشِغخ ا‪٢‬خش‪)ٜ‬‬


‫٘‪ ٝ‬ػجبسح ػٓ عٍغٍخ ِشرجخ ِٓ اٌؼٕبصش ؽ‪١‬ش رُغزخذَ ٌزخض‪ ٓ٠‬ػٕبصش ِزؼذدح ف‪ِ ٟ‬زغ‪١‬ش ‪ٚ‬اؽذ‪.‬‬

‫رمذَ ‪ِ Python‬غّ‪ٛ‬ػخ ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔذبد اٌّشوجذخ اٌزذ‪٠ ٟ‬شذبس ئٌ‪ٙ١‬ذب غبٌجًذب ثبٌزغٍغذالد ‪. sequences‬‬
‫ػب عذًا اٌّغزخذِخ ف‪ٚ .Python ٟ‬رزّ‪١‬ذض‬ ‫‪ٚ‬اٌمبئّخ ‪ٚ ٟ٘ list‬اؽذح ِٓ أوضش أٔ‪ٛ‬اع اٌج‪١‬بٔبد اعزخذا ًِب ‪ٚ‬رٕ‪ً ٛ‬‬
‫اٌم‪ٛ‬ائُ ثأٔ‪ٙ‬ب ِشٔخ ٌٍغب‪٠‬خ وّب ال ‪ٍ٠‬ضَ أْ رى‪ ْٛ‬وبفخ اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌمبئّخ ِٓ ٔفظ إٌ‪ٛ‬ع ‪.‬‬

‫اٌم‪ٛ‬ائُ ‪ٚ ٟ٘ lists‬اؽذح ِٓ ‪ 4‬أٔ‪ٛ‬اع ث‪١‬بٔبد ِعّٕخ ف‪ٚ Python ٟ‬اٌز‪ ٟ‬رُغذزخذَ ٌزخذض‪ِ ٓ٠‬غّ‪ٛ‬ػذخ ِذٓ‬
‫اٌج‪١‬بٔبد ‪ٚ‬اٌضالصخ ا خذش‪٘ ٜ‬ذ‪ٚ Dictionary ٚ Set ٚ Tuple ٟ‬وٍ‪ٙ‬ذب راد خصذبئص ‪ٚ‬اعذزخذاِبد‬
‫ِخزٍفخ ‪٠ٚ‬شبس ئٌ‪٘ ٝ‬زٖ ا ٔ‪ٛ‬اع ا سثؼخ ثبعُ اٌزغٍغالد ‪. sequences‬‬

‫‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬ػٍ‪ ٝ‬أ‪ ٞ‬ػذد ِٓ اٌؼٕبصش ‪ٚ‬لذ رى‪ ِٓ ْٛ‬أٔذ‪ٛ‬اع ِخزٍفذخ (‪ integer‬أ‪ floatٚ‬أ‪string ٚ‬‬
‫عذب ػٍذ‪ ٝ‬لبئّذخ ‪ list‬أخذش‪ ٜ‬وؼٕصذش ‪ٚ‬رغذّ‪ٝ‬‬‫أ‪ ٚ‬أ‪ٛٔ ٞ‬ع آخش ) وّب ‪ّ٠‬ىذٓ أْ رؾزذ‪ ٞٛ‬اٌمبئّذخ ‪ list‬أ‪ً ٠‬‬
‫لبئّخ ِزذاخٍخ ‪. nested list‬‬

‫ٌإلػذذالْ ػذذٓ لبئّذذخ ‪ list‬أِذذش عذذ‪ِٚ ًٙ‬جبشذذش ‪٠‬ذذزُ رٌذذه ػذذٓ غش‪٠‬ذذك ‪ٚ‬ظذذغ اٌؼٕبصذذش داخذذً ألذذ‪ٛ‬اط [ ]‬
‫‪ِٚ‬فص‪ٛ‬ي ث‪ّٕٙ١‬ب ثفبصٍخ ‪ٚ Comma‬سِض٘ب ( ‪ ) ,‬وب‪٢‬ر‪: ٟ‬‬
‫‪# empty list‬‬
‫][ = ‪my_list‬‬
‫)‪print(my_list‬‬

‫‪# list of integers‬‬


‫]‪my_list = [1, 2, 3‬‬
‫)‪print(my_list‬‬

‫‪# list with mixed data types‬‬


‫]‪my_list = [1, "Hello", 3.4‬‬
‫)‪print(my_list‬‬

‫‪# nested list‬‬


‫]]'‪my_list = ["mouse", [8, 4, 6], ['a‬‬
‫)‪print(my_list‬‬

‫‪output‬‬
‫][‬
‫]‪[1, 2, 3‬‬
‫]‪[1, 'Hello', 3.4‬‬
‫]]'‪['mouse', [8, 4, 6], ['a‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪31‬‬
‫‪ٚ : List Items‬ؽذاد اٌمبئّخ أ‪ ٚ‬ػٕبصش اٌمبئّخ‬
‫ػٕبصش اٌمبئّخ ٌ‪ٙ‬ب صالس خصبئص ‪:‬‬
‫‪ o‬ػٕبصش ِف‪ٙ‬شعخ ‪indexing‬‬
‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ اٌؼٕصش ا ‪ٚ‬ي ثٗ ف‪ٙ‬شط [‪ ]0‬اٌؼٕصش اٌضبٔ‪ ٟ‬ثٗ ف‪ٙ‬شط [‪٘ٚ ]1‬ىزا ‪.‬‬

‫‪ o‬ػٕبصش ِشرجخ ‪ordered‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ اٌؼٕبصش ٌ‪ٙ‬ب رشر‪١‬ت ِؾذد ‪ٌٚ‬ذٓ ‪٠‬زغ‪١‬ذش ٘ذزا اٌزشر‪١‬ذت‪ٚ .‬ئرا لّذذ ثاظذبفخ ػٕبصذش‬
‫عذ‪٠‬ذح ئٌ‪ ٝ‬اٌمبئّخ فغ‪١‬زُ ‪ٚ‬ظغ اٌؼٕبصش اٌغذ‪٠‬ذح ف‪ٙٔ ٟ‬ب‪٠‬خ اٌمبئّخ‪.‬‬

‫‪ِ ‬الؽظخ ‪ٕ٘ :‬بن ثؼط د‪ٚ‬اي ٌٍمبئّخ عزغ‪١‬ش اٌزشر‪١‬ت ‪ٌٚ‬ىٓ ثشىً ػبَ ٌٓ ‪٠‬زغ‪١‬ش رشر‪١‬ت اٌؼٕبصش‪.‬‬

‫‪ o‬ػٕبصش لبثٍخ ٌٍزغ‪١١‬ش ‪changeable‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ‪ّ٠‬ىٓ رغ‪١١‬ش ‪ٚ‬ئظبفخ ‪ٚ‬ئصاٌخ اٌؼٕبصش ف‪ ٟ‬اٌمبئّخ ثؼذ ئٔشبئ‪ٙ‬ب ‪.‬‬

‫‪ o‬ػٕبصش رغّؼ ثم‪ِ ُ١‬ىشسح ‪duplicate values‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬اٌم‪ٛ‬ائُ ػٍ‪ ٝ‬ػٕبصش ثٕفظ اٌم‪ّ١‬خ ٔ‪ٙ‬ب ػٕبصش ِف‪ٙ‬شعخ ‪.‬‬

‫‪ ‬و‪١‬ف‪١‬خ اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ا ؽشف أ‪ ٚ‬اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح داخً عٍغٍخ؟‬


‫‪ّ٠ ‬ىٕٕذذب اٌ‪ٛ‬صذذ‪ٛ‬ي ئٌذذ‪ ٝ‬ا ؽذذشف اٌفشد‪٠‬ذذخ أ‪ِ ٚ‬غّ‪ٛ‬ػذذخ ِذذٓ ا ؽذذشف ثبعذذزخذاَ اٌف‪ٙ‬شعذذخ ‪ٚ index‬ػبِذذً‬
‫اٌزمط‪١‬غ ( ‪٠ ) :‬جذأ اٌف‪ٙ‬شط ِٓ ‪. 0‬‬

‫‪ ‬اٌف‪ٙ‬شط ] ‪ : [ index‬اٌم‪ّ١‬خ اٌز‪ٔ ٟ‬عؼ‪ٙ‬ب داخً ] [ ‪ ٝ٘ٚ‬ػجبسح ػٓ سلذُ اٌف‪ٙ‬ذشط اٌذز‪٠ ٞ‬ؼجذش ػذٓ ل‪ّ١‬ذخ‬
‫ِ‪ٛ‬ظغ أ‪ِ ٚ‬ىبْ اٌؾشف فّضالً ]‪ ٛ٘ [4‬ػجبسح ػٓ ل‪ّ١‬خ اٌشلُ ‪ 4‬اٌّ‪ٛ‬ع‪ٛ‬د ف‪ ٟ‬اٌغٍغٍخ ‪.‬‬

‫‪ ‬رغّؼ ٌغخ ‪ Python‬ثبٌف‪ٙ‬شعخ اٌغٍج‪١‬خ ‪ negative indexing‬داخً اٌمبئّخ ‪ list‬ؽ‪١‬ذش ‪٠‬شذ‪١‬ش اٌف‪ٙ‬ذشط‬
‫[ ‪ ] -1‬ئٌ‪ ٝ‬اٌؼٕصش ا خ‪١‬ش ٌٍغٍغٍخ ‪٠ٚ‬ش‪١‬ش [ ‪ ] -2‬ئٌ‪ ٝ‬اٌؼٕصش لجً ا‪٢‬خ‪١‬ش ‪٘ٚ‬ىزا ‪.‬‬

‫‪٠ ‬ذزُ اعذزخذاَ ا لذ‪ٛ‬اط اٌّشثؼذخ ‪ٚ Square brackets‬سِض٘ذب ٘ذ‪ٌٍٛ ] [ ٛ‬صذ‪ٛ‬ي ئٌذ‪ ٝ‬ػٕبصذش اٌمبئّذخ‬
‫‪ّ٠ٚ‬ىٓ اعزخذاَ ا ل‪ٛ‬اط اٌّشثؼخ ِغ ػبًِ اٌزمط‪١‬غ ‪ٚ slicing operator‬سِض٘ب ٘‪ ] : [ ٛ‬العذزخشاط‬
‫ِغّ‪ٛ‬ػخ ِٓ اٌؼٕبصش ِٓ داخً اٌمبئّخ ‪. list‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪34‬‬
# List indexing
my_list = ['p', 'r', 'o', 'b', 'e']

# Output: p
print("my_list[0] is : " , my_list[0])

# Output: o
print("my_list[2] is : " , my_list[2])

# Output: e
print("my_list[4] is : " , my_list[4])

# Output: b
print("my_list[-2] is : " , my_list[-2])

# Nested List
n_list = ["Happy", [2, 0, 1, 5]]

# Nested indexing
print("n_list[0][1] is : " , n_list[0][1])

print("n_list[1][3] is : ",n_list[1][3])

# List slicing in Python

my_list = ['p','r','o','g','r','a','m','i','z']

# elements 3rd to 5th


print(my_list[2:5])

# elements beginning to 4th


print(my_list[:-5])

# elements 6th to end


print(my_list[5:])

# elements beginning to end


print(my_list[:])

output
my_list[0] is : p
my_list[2] is : o
my_list[4] is : e
my_list[-2] is : b
n_list[0][1] is : a
n_list[1][3] is : 5
['o', 'g', 'r']
['p', 'r', 'o', 'g']
['a', 'm', 'i', 'z']
['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 46
ّٝ‫غذ‬٠ ‫س خطذأ‬ٚ‫ ؽذذ‬ٝ‫ رٌذه ئٌذ‬ٞ‫إد‬١‫ عذ‬index ‫ذشط‬ٙ‫ ؽشف خذبسط ٔطذبق اٌف‬ٌٝ‫ي ئ‬ٛ‫ص‬ٌٛ‫ٌخ ا‬ٚ‫ ػٕذ ِؾب‬
.‫ ًؾب‬١‫ ػذدًا صؾ‬index ْٛ‫ى‬٠ ْ‫غت أ‬٠ ٌٟ‫ثبٌزب‬ٚ IndexError

my_list = ['p', 'r', 'o', 'b', 'e']


# Error! Only integer can be used for indexing
print(my_list[7])

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
print(my_list[7])
IndexError: list index out of range

.TypeError ٌٝ‫ رٌه ئ‬ٞ‫إد‬١‫ ع‬ٜ‫اع أخش‬ٛٔ‫ أ‬ٚ‫ أ‬floats َ‫ٌخ اعزخذا‬ٚ‫ ػٕذ ِؾب‬

my_list = ['p', 'r', 'o', 'b', 'e']


# Error! Only integer can be used for indexing
print(my_list[4.0])

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 7, in <module>
print(my_list[4.0])
TypeError: list indices must be integers or slices, not float

‫ً ػٕبصش اٌمبئّخ‬٠‫ رؼذ‬: Modify list Items


‫ػذخ‬ّٛ‫ اٌّغ‬ٚ‫ أ‬string ‫ ػىذظ اٌغٍغذٍخ‬ٝ‫ذش ػٕبصذش٘ب ػٍذ‬١١‫ّىذٓ رغ‬٠ ٗ‫ أٔذ‬ٟٕ‫ؼ‬٠ ‫ش ِّب‬١١‫ائُ لبثٍخ ٌٍزغ‬ٛ‫اٌم‬
.) = ( ‫ص‬١‫ػخ ِٓ اٌؼٕبصش ٔغزخذَ ػبًِ اٌزخص‬ّٛ‫ ِغ‬ٚ‫ش ػٕصش أ‬١١‫ٌزغ‬ٚ tuple
# Correcting mistake values in a list
odd = [2, 4, 6, 8]

# change the 1st item


odd[0] = 1
print(odd)

# change 2nd to 4th items


odd[1:4] = [3, 5, 7]
print(odd)

output
[1, 4, 6, 8]
[1, 3, 5, 7]

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 41
‫ ئظبفخ ػٕبصش اٌمبئّخ‬: add list Items
. ‫ػخ ِٓ اٌؼٕبصش ٌٍمبئّخ‬ّٛ‫ ِغ‬ٚ‫اي رغزخذَ إلظبفخ ػٕصش أ‬ٚ‫ د‬3 ‫ٕ٘بن‬
. ‫خ اٌمبئّخ‬٠‫ب‬ٙٔ ٌٝ‫ داٌخ اإلٌؾبق ؛ إلظبفخ ػٕصش ئ‬: append () function 
. ‫شط ِؾذد‬ٙ‫ ف‬ٟ‫ داٌخ اإلدخبي ؛ إلدساط ػٕصش لبئّخ ف‬: insert() function 
. ‫خ‬١ٌ‫ اٌمبئّخ اٌؾب‬ٌٝ‫ ئ‬ٜ‫ داٌخ اإلِذاد ؛ إلٌؾبق ػٕبصش ِٓ لبئّخ أخش‬: extend() function 

‫ وذذبئٓ لبثذذً ٌٍزىذذشاس‬ٞ‫ّىٕذذه ئظذذبفخ أ‬٠ ٓ‫ٌىذذ‬ٚ ‫ائُ فمذذػ‬ٛ‫ ثذذبٌم‬extend)( ‫ أْ رٍؾذذك‬ٞ‫س‬ٚ‫ظ ِذذٓ اٌعذذش‬١‫ ٌذذ‬
.) dictionaries sets tuples(
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

thislist = ["apple", "banana", "cherry"]


thislist.insert(1, "orange")
print(thislist)

thislist = ["apple", "banana", "cherry"]


tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)

thislist = ["apple", "banana", "cherry"]


thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)

output
['apple', 'banana', 'cherry', 'orange']
['apple', 'orange', 'banana', 'cherry']
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
['apple', 'banana', 'cherry', 'kiwi', 'orange']

‫ ئصاٌخ ػٕبصش اٌمبئّخ‬: remove list Items


. ‫ػخ ِٓ اٌؼٕبصش ِٓ اٌمبئّخ‬ّٛ‫ ِغ‬ٚ‫ئصاٌخ ػٕصش أ‬ٚ ‫اي رغزخذَ ٌؾزف‬ٚ‫ د‬3 ‫ٕ٘بن‬
.‫ داٌخ اإلصاٌخ ؛ إلصاٌخ اٌؼٕصش اٌّؾذد‬: remove() function 
.‫شط اٌّؾذد‬ٙ‫ داٌخ اإلصاٌخ ؛ إلصاٌخ اٌف‬: pop() function 
. ‫ داٌخ اٌّغؼ ؛ ٌّغؼ ػٕبصش اٌمبئّخ‬: clear() function 

.‫ش‬١‫ً اٌؼٕصش ا خ‬٠‫ب عزض‬ٙٔ‫ فا‬pop() ‫ ٌٍذاٌخ‬index ‫شط‬ٙ‫ ئرا ٌُ رؾذد اٌف‬
.ٜٛ‫ ِؾز‬ٍٝ‫ ػ‬ٞٛ‫ب ال رؾز‬ٕٙ‫دح ٌى‬ٛ‫ع‬ِٛ ‫ رظً اٌمبئّخ‬ٌٟ‫ثبٌزب‬ٚ ‫غ اٌمبئّخ‬٠‫َ ثزفش‬ٛ‫ رم‬clear() ‫ اٌذاٌخ‬
.ًِ‫ الصاٌخ اٌمبئّخ ثبٌىب‬ٚ‫ أ‬pop() ‫شط اٌّؾذد ِضً داٌخ‬ٙ‫ ٌؾزف اٌف‬del ‫خ‬١‫ّىٓ اعزخذاَ اٌىٍّخ ا عبع‬٠ 
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 42
. ً ‫ٕٗ ِغجمب‬١‫خ رغزخذَ ٌؾزف وبئٓ رُ رؼ‬١‫ وٍّخ أعبع‬ٝ٘ : del keyword 
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
print(my_list) # Output: ['r', 'o', 'b', 'l', 'e', 'm']

# Output: 'o'
print(my_list.pop(1))
print(my_list) # Output: ['r', 'b', 'l', 'e', 'm']

# Output: 'm'
print(my_list.pop())
print(my_list) # Output: ['r', 'b', 'l', 'e']

my_list.clear()
print(my_list) # Output: []

# Deleting list items


my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
del my_list[2] # delete one item
print(my_list)

# delete multiple items


del my_list[1:5]
print(my_list)
del my_list # delete entire list

output
['r', 'o', 'b', 'l', 'e', 'm']
o
['r', 'b', 'l', 'e', 'm']
m
['r', 'b', 'l', 'e']
[]
['p', 'r', 'b', 'l', 'e', 'm']
['p', 'm']

: ٟ‫ر‬٢‫ خطأ وب‬ٟ‫ؼط‬١‫ب ع‬ٙ‫ الؽع ػٕذ غجبػخ اٌذاٌخ ثؼذ ؽزف‬
my_list = ['p','r','o','b','l','e','m']
del my_list # delete entire list
print(my_list) # Error: List not defined

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 7, in <module>
print(my_list)
NameError: name 'my_list' is not defined

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 43
ْٛ‫ض‬٠‫ اٌمبئّخ داخً ٌغخ اٌجب‬ٍٝ‫بد ػ‬١ٍّ‫ ػ‬: Python List Operations
: ٟ‫ر‬٢‫ب ا‬ِٕٙ List ‫ اٌمبئّخ‬ٍٝ‫ّىٓ ئعشاؤ٘ب ػ‬٠ ٟ‫ اٌز‬Operations ‫بد‬١ٍّ‫ذ ِٓ اٌؼ‬٠‫ٕ٘بن اٌؼذ‬

ُ‫ائ‬ٛ‫خ عّغ اٌم‬١ٍّ‫ ػ‬: Lists Concatenation : ً‫ال‬ٚ‫ أ‬


: ْ‫غزخذِب‬٠ ٓ١ٍِ‫ٕ٘بن ِؼب‬ٚ ‫اؽذح‬ٚ ‫ لبئّخ‬ٟ‫ أوضش ف‬ٚ‫ٓ أ‬١‫ظُ لبئّز‬ٚ ‫ب عّغ‬ٙ١‫زُ ف‬٠ ‫خ‬١ٍّ‫ ػ‬ٝ٘
. ً ‫ٓ ِؼب‬١‫عّغ لبئّز‬ٚ ‫ ٌذِظ‬: ) + ( ‫ ِؼبًِ اٌغّغ‬o
. ‫ ٌزىشاس لبئّخ ػذد ِٓ اٌّشاد‬: ) * ( ‫ ِؼبًِ اٌعشة‬o
# Concatenating and repeating lists
odd = [1, 3, 5]

print(odd + [9, 7, 5])

print(["re"] * 3)

output
[1, 3, 5, 9, 7, 5]
['re', 're', 're']

ُ‫ائ‬ٛ‫خ رىشاس اٌم‬١ٍّ‫ ػ‬: Lists Iterating : ً ‫ب‬١ٔ‫ صب‬


‫س ػجذش اٌؼٕبصذش داخذً اٌمبئّذخ‬ٚ‫ّىٕٕذب اٌّذش‬١‫ ف‬arrays ‫فبد‬ٛ‫ ػجبسح ػٓ ِصف‬Lists ُ‫ائ‬ٛ‫ظشا ْ اٌم‬ ً ٔ
. for ‫ ِضً ؽٍمخ‬Looping ‫خ‬٠‫ثبعزخذاَ اٌؾٍمخ اٌزىشاس‬
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)

output
apple
banana
cherry

index ‫شط‬ٙ‫ك اٌف‬٠‫ ػٓ غش‬looping ًّ‫ّىٓ ػ‬٠ 


thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])

output
apple
banana
cherry

.‫لبثً ٌٍزىشاس ِٕبعت‬ ٍِ ‫ إلٔشبت‬len)( ‫ اٌذاٌخ‬ٚ range)( ‫زُ اعزخذَ اٌذاٌخ‬٠ ‫ الؽع‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 44
‫‪ِ ‬ضبي ػٍ‪While Loop ٝ‬‬
‫]"‪thislist = ["apple", "banana", "cherry‬‬
‫‪i = 0‬‬
‫‪while i < len(thislist):‬‬
‫)]‪print(thislist[i‬‬
‫‪i = i + 1‬‬

‫‪output‬‬
‫‪apple‬‬
‫‪banana‬‬
‫‪cherry‬‬

‫‪ّ٠ ‬ىٕه رىشاس ػٕبصش اٌمبئّخ ثبعزخذاَ ؽٍمخ ‪.while‬‬


‫‪ ‬اعزخذَ اٌذاٌخ ()‪ٌ len‬زؾذ‪٠‬ذ غ‪ٛ‬ي اٌمبئّخ ‪.‬‬
‫‪٠ ‬جذأ اٌؼذ ِٓ اٌف‪ٙ‬شط ‪٠ٚ 0‬غزّش ثبٌّش‪ٚ‬س ػٍ‪ ٝ‬وً ػٕبصش اٌمبئّخ‪.‬‬
‫‪ ‬رزوش ص‪٠‬بدح اٌف‪ٙ‬شط ثّمذاس ‪ 1‬ثؼذ وً رىشاس‪.‬‬

‫‪ ‬صبٌضب ً ‪ : Looping Using List Comprehension :‬اٌزىشاس ثبعزخذاَ ف‪ ُٙ‬اٌمبئّخ‬


‫‪ٛ٠‬فش ‪ List Comprehension‬ألصش ص‪١‬غخ ٌٍزىشاس ػجش اٌم‪ٛ‬ائُ ‪:‬‬
‫]"‪thislist = ["apple", "banana", "cherry‬‬
‫]‪[print(x) for x in thislist‬‬

‫‪output‬‬
‫‪apple‬‬
‫‪banana‬‬
‫‪cherry‬‬

‫‪ : Python List Comprehension‬لبئّخ اٌف‪ ُٙ‬داخً ٌغخ اٌجب‪٠‬ض‪ْٛ‬‬


‫٘‪ ٝ‬غش‪٠‬مخ ر‪ٛ‬فش ألصش ‪ syntax‬ػٕذِب رش‪٠‬ذ ئٔشبت لبئّخ عذ‪٠‬ذح ثٕب ًت ػٍ‪ ٝ‬ل‪ ُ١‬لبئّخ ِ‪ٛ‬ع‪ٛ‬دح‪.‬‬
‫‪ Synax :‬‬ ‫]‪newlist = [expression for item in iterable if condition == True‬‬

‫‪ :expression ‬اٌزؼج‪١‬ش ٘‪ ٛ‬اٌؼٕصش اٌؾبٌ‪ ٟ‬ف‪ ٟ‬اٌزىشاس ‪ٌٚ‬ىٕٗ أ‪ً ٠‬‬


‫عب إٌز‪١‬غخ ‪ٚ‬اٌز‪ّ٠ ٟ‬ىٕه ِؼبٌغز‪ٙ‬ب لجً أْ ‪ٕ٠‬ز‪ٟٙ‬‬
‫ث‪ٙ‬ب ا ِش وؼٕصش لبئّخ ف‪ ٟ‬اٌمبئّخ اٌغذ‪٠‬ذح ‪.‬‬
‫‪ : iterable ‬اٌم‪ّ١‬خ اٌمبثٍخ ٌٍزىشاس ‪ّ٠‬ىٓ أْ ‪٠‬ى‪ ْٛ‬اٌؼٕصش اٌمبثً ٌٍزىشاس أ‪ ٞ‬وبئٓ لبثً ٌٍزىشاس ِضً ‪ list‬أ‪ٚ‬‬
‫‪ tuple‬أ‪ set ٚ‬ئٌ ِٓ اٌزغٍغالد ‪.‬‬
‫‪ : condition ‬اٌششغ ‪٠‬شجٗ ػبًِ اٌزصف‪١‬خ ‪ filter‬اٌز‪ ٞ‬ال ‪٠‬مجً ع‪ ٜٛ‬اٌؼٕبصش اٌز‪٠ ٟ‬زُ رم‪ّٙ١١‬ب ئٌ‪.True ٝ‬‬

‫‪ٕ٘ ‬فشض أْ ٌذ‪ٕ٠‬ب ِغّ‪ٛ‬ػخ ِذٓ اٌف‪ٛ‬اوذٗ داخذً لبئّذخ ‪ٔٚ list‬ش‪٠‬ذذ لبئّذخ عذ‪٠‬ذذح رؾزذ‪ ٞٛ‬فمذػ ػٍذ‪ ٝ‬اٌفبو‪ٙ‬ذخ‬
‫اٌز‪٠ ٟ‬ؾز‪ ٞٛ‬اعّ‪ٙ‬ب ػٍ‪ ٝ‬ؽشف "‪."a‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪45‬‬
‫ ِذغ اخزجذبس اٌشذشغ‬for ‫ذه وزبثذخ ػجذبسح‬١ٍ‫ٓ ػ‬١‫زؼ‬١‫ عذ‬List Comprehension َ‫ ؽبٌذخ ػذذَ اعذزخذا‬ٟ‫ ف‬
: ٟ‫ر‬٢‫ب وب‬ٍٙ‫ ثذاخ‬condition

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = []

for x in fruits:
if "a" in x:
newlist.append(x)

print(newlist)

output
['apple', 'banana', 'mango']

: ٟ‫ر‬٢‫اؽذ فمػ وب‬ٚ ‫بَ ثىً رٌه ثغطش‬١‫ّىٕه اٌم‬٠ List Comprehension َ‫ ؽبٌخ اعزخذا‬ٟ‫ ف‬

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x for x in fruits if "a" in x]

print(newlist)

output
['apple', 'banana', 'mango']

: Condition List Comprehension ٍٝ‫ ػ‬1 ‫ ِضبي‬

#Condition The condition is like a filter that only accepts the items that valuate to
True.
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if x != "apple"]
print(newlist)

#The condition is optional and can be omitted:


fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits]
print(newlist)

output
['banana', 'cherry', 'kiwi', 'mango']
['apple', 'banana', 'cherry', 'kiwi', 'mango']

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 46
: iterable List Comprehension ٍٝ‫ ػ‬2 ‫ ِضبي‬

#The iterable can be any iterable object, like a list, tuple, set etc.
newlist = [x for x in range(10)]
print(newlist)

#Same example, but with a condition:


newlist = [x for x in range(10) if x < 5]
print(newlist)

output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]

: expression List Comprehension ٍٝ‫ ػ‬3 ‫ ِضبي‬

#The expression is the current item in the iteration, but it is also the outcome,
#which you can manipulate before it ends up like a list item in the new list:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x.upper() for x in fruits]
print(newlist)

#Set all values in the new list to 'hello':


fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = ['hello' for x in fruits]
print(newlist)

#The expression can also contain conditions, not like a filter,


# but as a way to manipulate the outcome:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x if x != "banana" else "orange" for x in fruits]
print(newlist)

output
['APPLE', 'BANANA', 'CHERRY', 'KIWI', 'MANGO']
['hello', 'hello', 'hello', 'hello', 'hello']
['apple', 'orange', 'cherry', 'kiwi', 'mango']

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 42
ْٛ‫ض‬٠‫ائُ داخً ٌغخ اٌجب‬ٛ‫اي اٌم‬ٚ‫ د‬: Python list methods
‫اي‬ٚ‫ػذذخ ِذذٓ اٌذذذ‬ّٛ‫ع ذب ً ِغ‬٠‫ٕ٘ذذبن أ‬ٚ list ‫ رزؼبِذذً ِذذغ اٌمبئّذذخ‬ٟ‫اي اٌزذذ‬ٚ‫ػذذخ ِذذٓ اٌذذذ‬ّٛ‫ ِغ‬ٝ‫رؼشفٕذذب ػٍذذ‬
Python ‫فشح ِغ وبئٕبد اٌمبئّخ داخً ٌغخ ثشِغخ‬ٛ‫ اٌّز‬methods ‫ؼشف ثبٌطشق‬٠ ‫ ِب‬ٚ‫ أ‬functions
list.method() ‫ذذك‬٠‫ذذب ػذذٓ غش‬ٙ١ٌ‫ي ئ‬ٛ‫صذذ‬ٌٛ‫ّىٕذذه ا‬٠ٚ lists ُ‫ائ‬ٛ‫ اٌمذذ‬ٝ‫ّىذذٓ ئعشاؤ٘ذذب ػٍذذ‬٠ ٟ‫اٌزذذ‬
: ٍٟ٠ ‫ّب‬١‫ب ف‬ٙ‫ أغٍج‬ٍٝ‫عٕزؼشف ػ‬
. )ٓ‫ي ػٕبصش اٌمبئّخ (اٌىبئ‬ٛ‫ي ؛ ٌّؼشفخ غ‬ٛ‫ داٌخ اٌط‬: len() function 
.‫ اٌمبئّخ‬ٟ‫شط اٌؼٕصش اٌّؾذد ف‬ٙ‫شط ؛ ٌّؼشفخ ف‬ٙ‫ داٌخ اٌف‬: index() function 
.‫ اٌمبئّخ‬ٟ‫ب اٌؼٕصش اٌّؾذد ف‬ٙ١‫ش ف‬ٙ‫ظ‬٠ ٟ‫ داٌخ اٌؼذد ؛ ٌّؼشفخ ػذد اٌّشاد اٌز‬: count() function 
.ٌٟ‫ رٕبص‬ٚ‫ أ‬ٞ‫ت رصبػذ‬١‫ت ػٕبصش اٌمبئّخ ثزشر‬١‫ داٌخ اٌفشص ؛ ٌزشر‬: sort() function 
. ‫ػىظ ػٕبصش اٌمبئّخ‬ٚ ‫ داٌخ اٌؼىظ ؛ ٌمٍت‬: reverse() function 
.‫ ِٓ اٌمبئّخ‬ٜ‫ داٌخ إٌغ ؛ ٌؼًّ ٔغخخ آخش‬: copy() function 
# Python list methods
my_list = [3, 8, 1, 6, 0, 8, 4]

print(len(my_list))

print(my_list.index(8))

print(my_list.count(8))

my_list.sort()
print(my_list)

my_list.reverse()
print(my_list)

# shallow copy using the slicing syntax

# mixed list
list = ['cat', 0, 6.7]
# copying a list using slicing
new_list = list[:]
# Adding an element to the new list
new_list.append('dog')

# Printing new and old list


print('Old List:', list)
print('New List:', new_list)

output
7
1
2
[0, 1, 3, 4, 6, 8, 8]
[8, 8, 6, 4, 3, 1, 0]
Old List: ['cat', 0, 6.7]
New List: ['cat', 0, 6.7, 'dog']

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 41
‫انذرس انسادس ‪ :‬انًجًوعات (انتعذيذ)‬
‫)‪(Tuples‬‬
‫‪ ‬عذذززؼٍُ فذذ‪٘ ٟ‬ذذزا اٌجشٔذذبِظ اٌزؼٍ‪ّ١‬ذذ‪ِ ٟ‬ذذب ٘ذذ‪ ٛ‬اٌزؼذ‪٠‬ذذذ ‪ Tuples‬فذذ‪ٚ Python ٟ‬و‪١‬ف‪١‬ذذخ ئٔشذذبئ‪ٙ‬ب ‪ِٚ‬زذذ‪ٝ‬‬
‫رغزخذِ‪ٙ‬ب ‪ٚ‬اٌذ‪ٚ‬اي اٌّخزٍفخ اٌز‪٠ ٟ‬غت أْ رى‪ ْٛ‬ػٍ‪ ٝ‬دسا‪٠‬خ ث‪ٙ‬ب‪.‬‬

‫‪ : Tuples‬اٌّغّ‪ٛ‬ػبد أ‪ ٚ‬اٌزؼذ‪٠‬ذ (وّب ‪٠‬طٍك ػٍ‪ٙ١‬ب ف‪ ٟ‬اٌؾغبة)‬


‫٘‪ ٝ‬ػجبسح ػٓ عٍغٍخ ِشرجخ ِٓ اٌؼٕبصش ؽ‪١‬ش رُغزخذَ ٌزخض‪ ٓ٠‬ػٕبصش ِزؼذدح ف‪ِ ٟ‬زغ‪١‬ش ‪ٚ‬اؽذ‪.‬‬

‫رمذَ ‪ِ Python‬غّ‪ٛ‬ػخ ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔذبد اٌّشوجذخ اٌزذ‪٠ ٟ‬شذبس ئٌ‪ٙ١‬ذب غبٌجًذب ثبٌزغٍغذالد ‪. sequences‬‬
‫‪ٚ‬اٌّغّ‪ٛ‬ػذذخ ‪٘ tuple‬ذذ‪ٚ ٟ‬اؽذذذح ِذذٓ أٔذذ‪ٛ‬اع اٌج‪١‬بٔذذبد اٌّغذذزخذِخ فذذ‪ٚ .Python ٟ‬رزّ‪١‬ذذض اٌّغّ‪ٛ‬ػذذخ ثأٔ‪ٙ‬ذذب‬
‫ِشٔخ ٌٍغب‪٠‬خ وّب ال ‪ٍ٠‬ضَ أْ رى‪ ْٛ‬وبفخ اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌمبئّخ ِٓ ٔفظ إٌ‪ٛ‬ع ‪.‬‬

‫اٌّغّ‪ٛ‬ػخ ‪ٚ ٟ٘ tuple‬اؽذح ِٓ ‪ 4‬أٔ‪ٛ‬اع ث‪١‬بٔبد ِعّٕخ ف‪ٚ Python ٟ‬اٌز‪ ٟ‬رُغزخذَ ٌزخض‪ِ ٓ٠‬غّ‪ٛ‬ػذخ‬
‫ِٓ اٌج‪١‬بٔبد ‪ٚ‬اٌضالصخ ا خش‪ٚ Dictionary ٚ Set ٚ lists ٟ٘ ٜ‬وٍ‪ٙ‬ب راد خصبئص ‪ٚ‬اعذزخذاِبد‬
‫ِخزٍفخ ‪٠ٚ‬شبس ئٌ‪٘ ٝ‬زٖ ا ٔ‪ٛ‬اع ا سثؼخ ثبعُ اٌزغٍغالد ‪. sequences‬‬

‫‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬ػٍ‪ ٝ‬أ‪ ٞ‬ػذد ِٓ اٌؼٕبصش ‪ٚ‬لذ رى‪ ِٓ ْٛ‬أٔذ‪ٛ‬اع ِخزٍفذخ (‪ integer‬أ‪ floatٚ‬أ‪string ٚ‬‬
‫عذذذب ػٍذذذ‪ِ ٝ‬غّ‪ٛ‬ػذذذخ ‪ tuple‬أخذذذش‪ٜ‬‬
‫أ‪ ٚ‬أ‪ٔ ٞ‬ذذذ‪ٛ‬ع آخذذذش ) وّذذذب ‪ّ٠‬ىذذذٓ أْ رؾزذذذ‪ ٞٛ‬اٌّغّ‪ٛ‬ػذذذخ ‪ tuple‬أ‪ً ٠‬‬
‫وؼٕصش ‪ٚ‬رغّ‪ِ ٝ‬غّ‪ٛ‬ػخ ِزذاخٍخ ‪. nested tuple‬‬

‫ٌإلػالْ ػٓ ِغّ‪ٛ‬ػخ ‪ tuple‬أِش ع‪ِٚ ًٙ‬جبشش ‪٠‬زُ رٌه ػٓ غش‪٠‬ك ‪ٚ‬ظغ اٌؼٕبصش داخذً ألذ‪ٛ‬اط ( )‬
‫‪ِٚ‬فص‪ٛ‬ي ث‪ّٕٙ١‬ب ثفبصٍخ ‪ٚ Comma‬سِض٘ب ( ‪ ) ,‬وب‪٢‬ر‪: ٟ‬‬
‫‪# Empty tuple‬‬
‫)( = ‪my_tuple‬‬
‫)‪print(my_tuple‬‬

‫‪# Tuple having integers‬‬


‫)‪my_tuple = (1, 2, 3‬‬
‫)‪print(my_tuple‬‬

‫‪# tuple with mixed datatypes‬‬


‫)‪my_tuple = (1, "Hello", 3.4‬‬
‫)‪print(my_tuple‬‬

‫‪# nested tuple‬‬


‫))‪my_tuple = ("mouse", [8, 4, 6], (1, 2, 3‬‬
‫)‪print(my_tuple‬‬

‫‪output‬‬
‫)(‬
‫)‪(1, 2, 3‬‬
‫)‪(1, 'Hello', 3.4‬‬
‫))‪('mouse', [8, 4, 6], (1, 2, 3‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪44‬‬
ً ٠‫ّىٓ أ‬٠ 
.tuple packing ‫ػخ‬ّٛ‫ُؼشف ثبعُ رؼجئخ اٌّغ‬٠ٚ ‫اط‬ٛ‫ْ اعزخذاَ ا ل‬ٚ‫ػخ ثذ‬ّٛ‫عب ئٔشبت ِغ‬
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible


a, b, c = my_tuple

print(a) # 3
print(b) # 4.6
print(c) # dog

output
(3, 4.6, 'dog')
3
4.6
dog

ً ‫اؽذذ‬ٚ ‫ػخ ثؼٕصذش‬ّٛ‫ؼذ ئٔشبت ِغ‬٠ 


‫ٓ ال‬١‫عذ‬ٛ‫ٓ ل‬١‫اؽذذ ثذ‬ٚ ‫د ػٕصذش‬ٛ‫عذ‬ٚ ْ ‫ت ؛‬ٟ‫أِذشا صذؼجًب ثؼذط اٌشذ‬
‫ي ػٕصذش ٌإلشذبسح‬ٚ‫ ) ثؼذذ أ‬, ( ‫ فبصٍخ‬ٌٝ‫ٌزٌه عٕؾزبط ئ‬ٚ tuple ‫ظ‬١ٌٚ string ٓ‫ؼجش ػ‬٠ ‫ف‬ٛ‫ع‬ٚ ٟ‫ىف‬٠
.tuple ‫ػخ‬ّٛ‫ب ِغ‬ٙٔ‫ أ‬ٌٝ‫ئ‬
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element


my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>

output
<class 'str'>
<class 'tuple'>
<class 'tuple'>

ً ٠‫ّىٓ أ‬٠ 
‫ إلٔشذبت‬tuple)( ‫ ثذـ‬ّٝ‫غذ‬٠ ٞ‫اٌز‬ٚ constructor ‫ اٌ ُّٕشئ‬ٚ‫ أ‬constructor ‫ذ‬١‫عب اعزخذاَ اٌ ُّش‬
. ‫ذ٘ب‬٠‫ ٔش‬ٞ‫ػخ اٌز‬ّٛ‫ٓ اٌّغ‬١‫ع‬ٛ‫ٓ ل‬١‫ب ث‬ٍٙ‫ٔىزت ثذاخ‬ٚ tuple)( ‫ش ٔىزت‬١‫ ؽ‬tuple ‫ػخ‬ّٛ‫ِغ‬
thistuple = tuple(("apple", "banana", "cherry"))
print(thistuple)

output
('apple', 'banana', 'cherry')

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 56
‫‪ٚ : Tuple Items‬ؽذاد اٌّغّ‪ٛ‬ػخ أ‪ ٚ‬ػٕبصش اٌّغّ‪ٛ‬ػخ‬
‫ػٕبصش اٌمبئّخ ٌ‪ٙ‬ب صالس خصبئص ‪:‬‬
‫‪ o‬ػٕبصش ِف‪ٙ‬شعخ ‪indexing‬‬
‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ اٌؼٕصش ا ‪ٚ‬ي ثٗ ف‪ٙ‬شط [‪ ]0‬اٌؼٕصش اٌضبٔ‪ ٟ‬ثٗ ف‪ٙ‬شط [‪٘ٚ ]1‬ىزا ‪.‬‬

‫‪ o‬ػٕبصش ِشرجخ ‪ordered‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ اٌؼٕبصش ٌ‪ٙ‬ب رشر‪١‬ت ِؾذد ‪ٌٚ‬ذٓ ‪٠‬زغ‪١‬ذش ٘ذزا اٌزشر‪١‬ذت‪ٚ .‬ئرا لّذذ ثاظذبفخ ػٕبصذش‬
‫عذ‪٠‬ذح ئٌ‪ ٝ‬اٌمبئّخ فغ‪١‬زُ ‪ٚ‬ظغ اٌؼٕبصش اٌغذ‪٠‬ذح ف‪ٙٔ ٟ‬ب‪٠‬خ اٌمبئّخ‪.‬‬

‫‪ِ ‬الؽظخ ‪ٕ٘ :‬بن ثؼط د‪ٚ‬اي ٌٍمبئّخ عزغ‪١‬ش اٌزشر‪١‬ت ‪ٌٚ‬ىٓ ثشىً ػبَ ٌٓ ‪٠‬زغ‪١‬ش رشر‪١‬ت اٌؼٕبصش‪.‬‬

‫‪ o‬ػٕبصش غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش ‪unchangeable‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ال ‪ّ٠‬ىٓ رغ‪١١‬ش ‪ٚ‬ئظبفخ ‪ٚ‬ئصاٌخ اٌؼٕبصش ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ ثؼذ ئٔشبئ‪ٙ‬ب ‪.‬‬

‫‪ o‬ػٕبصش رغّؼ ثم‪ِ ُ١‬ىشسح ‪duplicate values‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬اٌّغّ‪ٛ‬ػخ ػٍ‪ ٝ‬ػٕبصش ثٕفظ اٌم‪ّ١‬خ ٔ‪ٙ‬ب ػٕبصش ِف‪ٙ‬شعخ ‪.‬‬

‫‪ ‬و‪١‬ف‪١‬خ اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ا ؽشف أ‪ ٚ‬اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح داخً اٌّغّ‪ٛ‬ػخ ‪ Tuple‬؟‬
‫‪ّ٠ ‬ىٕٕذذب اٌ‪ٛ‬صذذ‪ٛ‬ي ئٌذذ‪ ٝ‬ا ؽذذشف اٌفشد‪٠‬ذذخ أ‪ِ ٚ‬غّ‪ٛ‬ػذذخ ِذذٓ ا ؽذذشف ثبعذذزخذاَ اٌف‪ٙ‬شعذذخ ‪ٚ index‬ػبِذذً‬
‫اٌزمط‪١‬غ ( ‪٠ ) :‬جذأ اٌف‪ٙ‬شط ِٓ ‪. 0‬‬

‫‪ ‬اٌف‪ٙ‬شط ] ‪ : [ index‬اٌم‪ّ١‬خ اٌز‪ٔ ٟ‬عؼ‪ٙ‬ب داخً ] [ ‪ ٝ٘ٚ‬ػجبسح ػٓ سلذُ اٌف‪ٙ‬ذشط اٌذز‪٠ ٞ‬ؼجذش ػذٓ ل‪ّ١‬ذخ‬
‫ِ‪ٛ‬ظغ أ‪ِ ٚ‬ىبْ اٌؾشف فّضالً ]‪ ٛ٘ [4‬ػجبسح ػٓ ل‪ّ١‬خ اٌشلُ ‪ 4‬اٌّ‪ٛ‬ع‪ٛ‬د ف‪ ٟ‬اٌغٍغٍخ ‪.‬‬

‫‪ ‬رغّؼ ٌغخ ‪ Python‬ثبٌف‪ٙ‬شعذخ اٌغذٍج‪١‬خ ‪ negative indexing‬داخذً اٌّغّ‪ٛ‬ػذخ ‪ Tuple‬ؽ‪١‬ذش ‪٠‬شذ‪١‬ش‬


‫اٌف‪ٙ‬شط [ ‪ ] -1‬ئٌ‪ ٝ‬اٌؼٕصش ا خ‪١‬ش ٌٍغٍغٍخ ‪٠ٚ‬ش‪١‬ش [ ‪ ] -2‬ئٌ‪ ٝ‬اٌؼٕصش لجً ا‪٢‬خ‪١‬ش ‪٘ٚ‬ىزا ‪.‬‬

‫‪٠ ‬زُ اعزخذاَ ا ل‪ٛ‬اط اٌّشثؼخ ‪ٚ Square brackets‬سِض٘ب ٘‪ٌٍٛ ] [ ٛ‬صذ‪ٛ‬ي ئٌذ‪ ٝ‬ػٕبصذش اٌّغّ‪ٛ‬ػذخ‬
‫‪ّ٠ٚ‬ىذذذٓ اعذذذزخذاَ ا لذذذ‪ٛ‬اط اٌّشثؼذذذخ ِذذذغ ػبِذذذً اٌزمط‪١‬ذذذغ ‪ٚ slicing operator‬سِض٘ذذذب ٘ذذذ‪] : [ ٛ‬‬
‫العزخشاط ِغّ‪ٛ‬ػخ ِٓ اٌؼٕبصش ِٓ داخً اٌّغّ‪ٛ‬ػخ ‪.Tuple‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪51‬‬
# Accessing tuple elements using indexing

my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3]) # 's'
print(n_tuple[1][1]) # 4

# Accessing tuple elements using slicing

my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th

# Output: ('r', 'o', 'g')


print(my_tuple[1:4])

# elements beginning to 2nd

# Output: ('p', 'r')


print(my_tuple[:-7])

# elements 8th to end

# Output: ('i', 'z')


print(my_tuple[7:])

# elements beginning to end

# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])

output
p
t
s
4
('r', 'o', 'g')
('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 52
ّٝ‫غذ‬٠ ‫س خطذأ‬ٚ‫ ؽذذ‬ٝ‫ رٌذه ئٌذ‬ٞ‫إد‬١‫ عذ‬index ‫ذشط‬ٙ‫ ؽشف خذبسط ٔطذبق اٌف‬ٌٝ‫ي ئ‬ٛ‫ص‬ٌٛ‫ٌخ ا‬ٚ‫ ػٕذ ِؾب‬
.‫ ًؾب‬١‫ ػذدًا صؾ‬index ْٛ‫ى‬٠ ْ‫غت أ‬٠ ٌٟ‫ثبٌزب‬ٚ IndexError

my_tuple = ('p','e','r','m','i','t')
# TypeError: list indices must be integers, not float
my_tuple[2.0]

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 5, in <module>
my_tuple[2.0]
TypeError: tuple indices must be integers or slices, not float

.TypeError ٌٝ‫ رٌه ئ‬ٞ‫إد‬١‫ ع‬ٜ‫اع أخش‬ٛٔ‫ أ‬ٚ‫ أ‬floats َ‫ٌخ اعزخذا‬ٚ‫ ػٕذ ِؾب‬

my_tuple = ('p','e','r','m','i','t')
# IndexError: list index out of range
print(my_tuple[6])

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
print(my_tuple[6])
IndexError: tuple index out of range

‫ػخ‬ّٛ‫ً ػٕبصش اٌّغ‬٠‫ رؼذ‬: Modify Tuple Items


‫ذش ػٕبصذش٘ب ثّغذشد‬١١‫ّىذٓ رغ‬٠ ‫ أٔذٗ ال‬ٟ‫ؼٕذ‬٠ ‫ ِّذب‬list ‫ ػىظ اٌمبئّخ‬ٍٝ‫ش ػ‬١١‫ش لبثٍخ ٌٍزغ‬١‫ػخ غ‬ّٛ‫اٌّغ‬
.‫ب‬ٙ‫ص‬١‫رخص‬
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])

# TypeError: 'tuple' object does not support item assignment


my_tuple[1] = 9

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 6, in <module>
my_tuple[1] = 9
TypeError: 'tuple' object does not support item assignment

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 53
ٖ‫ذذش ػٕبصذذش‬١١‫ّىٓ رغ‬١‫ فذذ‬list ‫ذذش ِضذذً اٌمبئّذذخ‬١١‫بٔذذبد لبثذذً ٌٍزغ‬١‫ع ث‬ٛ‫ ٔفغذذٗ ٔذذ‬ٛ‫ٌىذذٓ ئرا وذذبْ اٌؼٕصذذش ٘ذذ‬ٚ 
.)ٓ١١‫ُ ِخزٍفخ (ئػبدح اٌزؼ‬١‫ػخ ٌم‬ّٛ‫ٓ ِغ‬١١‫عب رؼ‬ ً ٠‫ّىٕٕب أ‬٠ ‫ وّب‬.‫اٌّزذاخٍخ‬
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])

# However, item of mutable element can be changed


my_tuple[3][0] = 9 # Output: (4, 2, 3, [9, 5])
print(my_tuple)

# Tuples can be reassigned


my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)

output
(4, 2, 3, [9, 5])
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

‫ لبئّذخ‬ٝ‫ػذخ ئٌذ‬ّٛ‫ذً اٌّغ‬٠ٛ‫ذك رؾ‬٠‫ ػذٓ غش‬ٛ٘ٚ tuple ‫ػخ‬ّٛ‫ اٌّغ‬ٍٝ‫ُ ػ‬١‫ً اٌم‬٠‫ّىٕه ِٓ رؼذ‬٠ ً٠‫ ٕ٘بن ثذ‬
.‫ػخ‬ّٛ‫ ِغ‬ٌٝ‫ً اٌمبئّخ ئ‬٠ٛ‫ئػبدح رؾ‬ٚ ‫ش اٌمبئّخ‬١١‫رغ‬ٚ
#this is a tuple
x = ("apple", "banana", "cherry")

#convert tuple to list


y = list(x)

#modify list
y[1] = "kiwi"

#convert list to tuple


x = tuple(y)

#print the new tuple


print(x)

output
('apple', 'kiwi', 'cherry')

‫ػخ‬ّٛ‫ ئظبفخ ػٕبصش اٌّغ‬: add tuple Items


ٝ‫ذش ػٍذ‬١١‫ش لبثٍخ ٌٍزغ‬١‫ػخ غ‬ّٛ‫ب ؛ ْ اٌّغ‬ٙ١ٌ‫ّىٕه ئظبفخ ػٕبصش ئ‬٠ ‫ ال‬tuple ‫ػخ‬ّٛ‫ثّغشد ئٔشبت اٌّغ‬
.‫ب‬ٙ‫ص‬١‫ش ػٕبصش٘ب ثّغشد رخص‬١١‫ّىٓ رغ‬٠ ‫ أٔٗ ال‬ٟٕ‫ؼ‬٠ ‫ ِّب‬list ‫ػىظ اٌمبئّخ‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 54
‫)"‪thistuple = ("apple", "banana", "cherry‬‬
‫‪thistuple.append("orange") # This will raise an error‬‬
‫)‪print(thistuple‬‬

‫‪output‬‬
‫‪Traceback (most recent call last):‬‬
‫>‪File "c:\Users\Hoda_PC\Desktop\example.py", line 2, in <module‬‬
‫‪thistuple.append("orange") # This will raise an error‬‬
‫'‪AttributeError: 'tuple' object has no attribute 'append‬‬

‫‪ٕ٘ ‬بن ثذ‪ّ٠ ً٠‬ىٕه ِٓ ئظبفخ اٌم‪ ُ١‬ػٍ‪ ٝ‬اٌّغّ‪ٛ‬ػخ ‪ ٛ٘ٚ tuple‬رّب ًِب ِضً اٌؾً اٌجذ‪ٌ ً٠‬زغ‪١١‬ذش اٌّغّ‪ٛ‬ػذخ‬
‫‪ّ٠‬ىٕه رؾ‪ٍٙ٠ٛ‬ب ئٌذ‪ ٝ‬لبئّذخ ‪ٚ‬ئظذبفخ اٌؼٕبصذش ػذٓ غش‪٠‬ذك د‪ٚ‬اي ئظذبفخ اٌؼٕبصذش ػٍذ‪ ٝ‬اٌمبئّذخ اٌزذ‪ ٟ‬رذُ‬
‫دساعز‪ٙ‬ب ف‪ ٟ‬اٌفصً اٌغبثك ‪ٚ‬رؾ‪ٍٙ٠ٛ‬ب ِشح أخش‪ ٜ‬ئٌ‪ِ ٝ‬غّ‪ٛ‬ػخ‪.‬‬
‫‪ : append () function ‬داٌخ اإلٌؾبق ؛ إلظبفخ ػٕصش ئٌ‪ٙٔ ٝ‬ب‪٠‬خ اٌمبئّخ ‪.‬‬
‫‪ : insert() function ‬داٌخ اإلدخبي ؛ إلدساط ػٕصش لبئّخ ف‪ ٟ‬ف‪ٙ‬شط ِؾذد ‪.‬‬
‫‪ : extend() function ‬داٌخ اإلِذاد ؛ إلٌؾبق ػٕبصش ِٓ لبئّخ أخش‪ ٜ‬ئٌ‪ ٝ‬اٌمبئّخ اٌؾبٌ‪١‬خ ‪.‬‬
‫)"‪thistuple = ("apple", "banana", "cherry‬‬
‫)‪y = list(thistuple‬‬
‫)"‪y.append("orange‬‬
‫)‪thistuple = tuple(y‬‬

‫)‪print(thistuple‬‬

‫‪output‬‬
‫)'‪('apple', 'banana', 'cherry', 'orange‬‬

‫‪ : remove tuple Items‬ئصاٌخ ػٕبصش اٌّغّ‪ٛ‬ػخ‬


‫اٌّغّ‪ٛ‬ػبد غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش ٌذزا ال ‪ّ٠‬ىٕذه ئصاٌذخ اٌؼٕبصذش ِٕ‪ٙ‬ذب ‪ٌٚ‬ىذٓ ‪ّ٠‬ىٕذه اعذزخذاَ ٔفذظ اٌؾذً‬
‫اٌجذذذ‪ ً٠‬اٌذذز‪ ٞ‬اعذذزخذِٕبٖ ٌزغ‪١١‬ذذش ػٕبصذذش اٌّغّ‪ٛ‬ػذذخ ‪ٚ‬ئظذذبفز‪ٙ‬ب ‪ٚ‬ال رٕغذذ‪ ٝ‬أْ رغذذزخذَ د‪ٚ‬اي اٌخبصذذخ‬
‫ثبٌؾزف اٌمبئّخ اٌز‪ ٟ‬رُ دساعز‪ٙ‬ب ف‪ ٟ‬اٌفصً اٌغبثك ‪.‬‬
‫‪ : remove () function ‬داٌخ اإلصاٌخ ؛ إلصاٌخ اٌؼٕصش اٌّؾذد‪.‬‬
‫‪ : pop() function ‬داٌخ اإلصاٌخ ؛ إلصاٌخ اٌف‪ٙ‬شط اٌّؾذد‪.‬‬
‫‪ : clear() function ‬داٌخ اٌّغؼ ؛ ٌّغؼ ػٕبصش اٌمبئّخ ‪.‬‬

‫‪ ‬ئرا ٌُ رؾذد اٌف‪ٙ‬شط ‪ٌٍ index‬ذاٌخ )(‪ pop‬فأ‪ٙ‬ب عزض‪ ً٠‬اٌؼٕصش ا خ‪١‬ش‪.‬‬
‫‪ ‬اٌذاٌخ )(‪ clear‬رم‪ َٛ‬ثزفش‪٠‬غ اٌمبئّخ ‪ٚ‬ثبٌزبٌ‪ ٟ‬رظً اٌمبئّخ ِ‪ٛ‬ع‪ٛ‬دح ٌىٕ‪ٙ‬ب ال رؾز‪ ٞٛ‬ػٍ‪ِ ٝ‬ؾز‪.ٜٛ‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪55‬‬
.ًِ‫ الصاٌخ اٌمبئّخ ثبٌىب‬ٚ‫ أ‬pop() ‫شط اٌّؾذد ِضً داٌخ‬ٙ‫ ٌؾزف اٌف‬del ‫خ‬١‫ّىٓ اعزخذاَ اٌىٍّخ ا عبع‬٠ 
#Convert the tuple into a list, remove "apple", and convert it back into a tuple:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
print(thistuple)

output
('banana', 'cherry')

: ٟ‫ر‬٢‫ خطأ وب‬ٟ‫ؼط‬١‫ب ع‬ٙ‫ الؽع ػٕذ غجبػخ اٌذاٌخ ثؼذ ؽزف‬
#The del keyword can delete the tuple completely:
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 11, in <module>
print(thistuple) #this will raise an error because the tuple no longer exists
NameError: name 'thistuple' is not defined

ْٛ‫ض‬٠‫ػخ داخً ٌغخ اٌجب‬ّٛ‫ اٌّغ‬ٍٝ‫بد ػ‬١ٍّ‫ ػ‬: Python Tuple Operations
: ٟ‫ر‬٢‫ب ا‬ِٕٙ Tuple ‫ػخ‬ّٛ‫ اٌّغ‬ٍٝ‫ّىٓ ئعشاؤ٘ب ػ‬٠ ٟ‫ اٌز‬Operations ‫بد‬١ٍّ‫ذ ِٓ اٌؼ‬٠‫ٕ٘بن اٌؼذ‬

ٓ١‫ػز‬ّٛ‫خ عّغ ِغ‬١ٍّ‫ ػ‬: Tuple Concatenation : ً‫ال‬ٚ‫ أ‬


: ْ‫غزخذِب‬٠ ٓ١ٍِ‫ٕ٘بن ِؼب‬ٚ ‫اؽذح‬ٚ ‫ػخ‬ّٛ‫ ِغ‬ٟ‫ أوضش ف‬ٚ‫ٓ أ‬١‫ػز‬ّٛ‫ظُ ِغ‬ٚ ‫ب عّغ‬ٙ١‫زُ ف‬٠ ‫خ‬١ٍّ‫ ػ‬ٝ٘
. ً ‫ٓ ِؼب‬١‫ػز‬ّٛ‫عّغ ِغ‬ٚ ‫ ٌذِظ‬: ) + ( ‫ ِؼبًِ اٌغّغ‬o
. ‫ػخ ػذد ِٓ اٌّشاد‬ّٛ‫ ٌزىشاس ِغ‬: ) * ( ‫ ِؼبًِ اٌعشة‬o
# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))

# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",) * 3)

output
(1, 2, 3, 4, 5, 6)
('Repeat', 'Repeat', 'Repeat')

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 56
‫ػخ‬ّٛ‫خ رىشاس اٌّغ‬١ٍّ‫ ػ‬: Tuple Iterating : ً ‫ب‬١ٔ‫ صب‬
ً‫س ػجذذش اٌؼٕبصذذش داخذذ‬ٚ‫ّىٕٕذذب اٌّذذش‬١‫ ف‬arrays ‫فبد‬ٛ‫ػجذذبسح ػذذٓ ِصذذف‬Tuple ‫ػذذخ‬ّٛ‫ٔظذ ًذشا ْ اٌّغ‬
. for ‫ ِضً ؽٍمخ‬Looping ‫خ‬٠‫ػخ ثبعزخذاَ اٌؾٍمخ اٌزىشاس‬ّٛ‫اٌّغ‬
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)

output
apple
banana
cherry

index ‫شط‬ٙ‫ك اٌف‬٠‫ ػٓ غش‬looping ًّ‫ّىٓ ػ‬٠ 


thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
print(thistuple[i])

output
apple
banana
cherry

.‫لبثً ٌٍزىشاس ِٕبعت‬ ٍِ ‫ إلٔشبت‬len)( ‫ اٌذاٌخ‬ٚ range)( ‫زُ اعزخذَ اٌذاٌخ‬٠ ‫ الؽع‬

While Loop ٍٝ‫ ِضبي ػ‬


thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
print(thistuple[i])
i = i + 1

output
apple
banana
cherry

.while ‫ّىٕه رىشاس ػٕبصش اٌمبئّخ ثبعزخذاَ ؽٍمخ‬٠ 


. ‫ي اٌمبئّخ‬ٛ‫ذ غ‬٠‫ ٌزؾذ‬len)( ‫ اعزخذَ اٌذاٌخ‬
.‫ وً ػٕبصش اٌمبئّخ‬ٍٝ‫س ػ‬ٚ‫غزّش ثبٌّش‬٠ٚ 0 ‫شط‬ٙ‫جذأ اٌؼذ ِٓ اٌف‬٠ 
.‫ ثؼذ وً رىشاس‬1 ‫شط ثّمذاس‬ٙ‫بدح اٌف‬٠‫ رزوش ص‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 52
‫ػخ‬ّٛ‫ داخً ِغ‬ٛ‫خ اخزجبس ػع‬١ٍّ‫ ػ‬: Tuple Membership Test : ً ‫ صبٌضب‬
‫ذذخ‬١‫ػذذخ اٌفشػ‬ّٛ‫ػذذخ ٌؼّذذً اخزجذذبس ِذذب ئرا وبٔذذذ اٌّغ‬ّٛ‫خ داخذذً اٌّغ‬٠ٛ‫ّىذذٓ اعذذزخذاَ ِؼذذبِالد اٌؼعذذ‬٠
: ‫ػخ ّ٘ب‬ّٛ‫ٓ الخزجبس ػٕصش داخً ِغ‬١‫ٕ٘بن وٍّز‬ٚ ‫ أَ ال ؟‬Tuple ‫ػخ‬ّٛ‫دح داخً ِغ‬ٛ‫ع‬ِٛ
. ‫ػخ‬ّٛ‫ اٌّغ‬ٟ‫ٓ ف‬١‫د ؽشف ِؼ‬ٛ‫ع‬ٚ ِٓ ‫خ رغزخذَ ٌٍزؾمك‬١‫ وٍّخ أعبع‬: in keyword 
. ‫ػخ‬ّٛ‫ اٌّغ‬ٟ‫ٓ ف‬١‫د ؽشف ِؼ‬ٛ‫ع‬ٚ َ‫خ رغزخذَ ٌٍزؾمك ِٓ ػذ‬١‫ وٍّخ أعبع‬: not in keyword 
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)

# In operation
print('a' in my_tuple)
print('b' in my_tuple)

# Not in operation
print('g' not in my_tuple)

output
True
False
True

ْٛ‫ض‬٠‫ػخ داخً ٌغخ اٌجب‬ّٛ‫اي اٌّغ‬ٚ‫ د‬: Python tuple methods


: ُ٘ tuple ‫ػخ‬ّٛ‫ رزؼبًِ ِغ اٌّغ‬ٟ‫اي اٌز‬ٚ‫ػخ ِٓ اٌذ‬ّٛ‫ٕ٘بن ِغ‬
. )ٓ‫ػخ (اٌىبئ‬ّٛ‫ي ػٕبصش اٌّغ‬ٛ‫ي ؛ ٌّؼشفخ غ‬ٛ‫ داٌخ اٌط‬: len() function 
.‫ػخ‬ّٛ‫ اٌّغ‬ٟ‫شط اٌؼٕصش اٌّؾذد ف‬ٙ‫شط ؛ ٌّؼشفخ ف‬ٙ‫ داٌخ اٌف‬: index() function 
.‫ػخ‬ّٛ‫ اٌّغ‬ٟ‫ب اٌؼٕصش اٌّؾذد ف‬ٙ١‫ش ف‬ٙ‫ظ‬٠ ٟ‫ داٌخ اٌؼذد؛ ٌّؼشفخ ػذد اٌّشاد اٌز‬: count() function 

my_tuple = ('a', 'p', 'p', 'l', 'e',)

print(len(my_tuple)) # Output: 5
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3

output
5
2
3

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 51
‫‪١ِّ : Advantages of Tuple over List‬ضاد اٌّغّ‪ٛ‬ػخ ػٓ اٌمبئّخ داخً ٌغخ اٌجب‪٠‬ض‪ْٛ‬‬
‫ٔظشا ْ ‪ tuple‬رشجٗ اٌم‪ٛ‬ائُ ‪ lists‬رّب ًِب ‪٠‬ذزُ اعذزخذاَ والّ٘ذب فذ‪ِٛ ٟ‬الذ ِّبصٍذخ‪ِٚ .‬ذغ رٌذه ٕ٘ذبن‬‫ً‬
‫ِّ‪١‬ضاد ِؼ‪ٕ١‬خ ػٕذ رطج‪١‬ك اٌّغّ‪ٛ‬ػخ ‪ tuple‬ثخالف اٌمبئّخ ‪ list‬ف‪ّ١‬ب ‪ ٍٟ٠‬ثؼط اٌّضا‪٠‬ب اٌشئ‪١‬غ‪١‬خ ‪:‬‬
‫‪ٔ ‬غزخذَ ثشىً ػبَ اٌّغّ‪ٛ‬ػبد ٔ‪ٛ‬اع اٌج‪١‬بٔبد غ‪١‬ش اٌّزغبٔغخ (اٌّخزٍفخ) ‪ٚ‬اٌم‪ٛ‬ائُ ٔ‪ٛ‬اع اٌج‪١‬بٔبد‬
‫اٌّزغبٔغخ (اٌّزشبث‪ٙ‬خ)‪.‬‬

‫ٔظشا ْ ‪ tuple‬غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش فاْ اٌزىشاس ػجش ‪٠ tuple‬ى‪ ْٛ‬أعشع ِٕٗ ِغ ‪ٌٚ list‬زٌه‬
‫ً‬ ‫‪‬‬
‫ٕ٘بن رؾغٓ غف‪ ١‬ف‪ ٟ‬ا دات‪.‬‬

‫‪ّ٠ ‬ىٓ اعزخذاَ اٌّغّ‪ٛ‬ػبد اٌز‪ ٟ‬رؾز‪ ٞٛ‬ػٍ‪ ٝ‬ػٕبصش غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش وّفزبػ ‪ٌٕٛ key‬ع اٌج‪١‬بٔبد‬
‫اٌمبِ‪ٛ‬ط ‪ٌٚ dictionary‬ىٓ ٘زا غ‪١‬ش ِّىٓ ِغ اٌم‪ٛ‬ائُ‪.‬‬

‫‪ ‬ئرا وبٔذ ٌذ‪٠‬ه ث‪١‬بٔبد ال رزغ‪١‬ش فاْ رٕف‪١‬ز٘ب ثصفز‪ٙ‬ب ‪ tuple‬ع‪١‬عّٓ أٔ‪ٙ‬ب رظً ِؾّ‪١‬خ ظذ اٌىزبثخ‪.‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪54‬‬
‫انذرس انسابع ‪ :‬انتشكيهة (انًجًوعة)‬
‫)‪(Sets‬‬
‫‪ ‬عذذززؼٍُ فذذ‪٘ ٟ‬ذذزا اٌجشٔذذبِظ اٌزؼٍ‪ّ١‬ذذ‪ ٟ‬عذذززؼٍُ وذذً شذذ‪ٟ‬ت ػذذٓ رشذذى‪١‬الد أ‪ِ ٚ‬غّ‪ٛ‬ػذذبد ‪ sets‬داخذذً ٌغذذخ‬
‫اٌجب‪٠‬ض‪ ْٛ‬؛ عززؼٍُ و‪٠ ١‬زُ ئٔشبؤ٘ب ‪ٚ‬و‪١‬ف‪١‬خ ئظبفخ أ‪ ٚ‬ئصاٌخ اٌؼٕبصش ِٕ‪ٙ‬ذب ‪ٚ‬عّ‪١‬ذغ اٌؼٍّ‪١‬ذبد اٌزذ‪٠ ٟ‬ذزُ‬
‫ئعشاؤ٘ب ػٍ‪ ٝ‬رشى‪١‬الد ‪ sets‬ف‪.Python ٟ‬‬

‫‪ : Sets‬اٌزشى‪١‬الد (اٌّغّ‪ٛ‬ػبد)‬
‫٘‪ ٝ‬ػجبسح ػٓ عٍغٍخ غ‪١‬ش ِشرجخ ِٓ اٌؼٕبصش ؽ‪١‬ش رُغزخذَ ٌزخض‪ ٓ٠‬ػٕبصش ِزؼذدح ف‪ِ ٟ‬زغ‪١‬ش ‪ٚ‬اؽذ‪.‬‬

‫رمذَ ‪ِ Python‬غّ‪ٛ‬ػخ ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔذبد اٌّشوجذخ اٌزذ‪٠ ٟ‬شذبس ئٌ‪ٙ١‬ذب غبٌجًذب ثبٌزغٍغذالد ‪. sequences‬‬
‫‪ٚ‬اٌزشى‪ٍ١‬خ ‪ٚ ٟ٘ set‬اؽذح ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔبد اٌّغزخذِخ فذ‪ٚ .Python ٟ‬رزّ‪١‬ذض اٌّغّ‪ٛ‬ػذخ ثأٔ‪ٙ‬ذب ِشٔذخ‬
‫وّب ال ‪ٍ٠‬ضَ أْ رى‪ ْٛ‬وبفخ اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌمبئّخ ِٓ ٔفظ إٌ‪ٛ‬ع ‪.‬‬

‫اٌزشى‪ٍ١‬خ ‪ٚ ٟ٘ Set‬اؽذح ِٓ ‪ 4‬أٔ‪ٛ‬اع ث‪١‬بٔبد ِعّٕخ ف‪ٚ Python ٟ‬اٌز‪ ٟ‬رُغزخذَ ٌزخذض‪ِ ٓ٠‬غّ‪ٛ‬ػذخ ِذٓ‬
‫اٌج‪١‬بٔبد ‪ٚ‬اٌضالصخ ا خذش‪٘ ٜ‬ذ‪ٚ Dictionary ٚ Set ٚ Tuple ٟ‬وٍ‪ٙ‬ذب راد خصذبئص ‪ٚ‬اعذزخذاِبد‬
‫ِخزٍفخ ‪٠ٚ‬شبس ئٌ‪٘ ٝ‬زٖ ا ٔ‪ٛ‬اع ا سثؼخ ثبعُ اٌزغٍغالد ‪. sequences‬‬

‫‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬ػٍ‪ ٝ‬أ‪ ٞ‬ػذد ِٓ اٌؼٕبصش ‪ٚ‬لذ رى‪ ِٓ ْٛ‬أٔذ‪ٛ‬اع ِخزٍفذخ (‪ integer‬أ‪ floatٚ‬أ‪string ٚ‬‬
‫أ‪ ٚ‬أ‪ٔ ٞ‬ذذ‪ٛ‬ع آخذذش ) ٌىذذٓ ال ‪ّ٠‬ىذذٓ أْ رؾزذذ‪ sets ٞٛ‬ػٍذذ‪ ٝ‬ػٕبصذذش لبثٍذذخ ٌٍزغ‪١١‬ذذش ِضذذً اٌمذذ‪ٛ‬ائُ ‪ lists‬أ‪ٚ‬‬
‫اٌزشذذى‪١‬الد ‪ sets‬أ‪ ٚ‬اٌمذذ‪ٛ‬اِ‪١‬ظ ‪ dictionaries‬وؼٕبصذذش ثذذذاخٍ‪ٙ‬ب ‪ٌٚ‬ىذذٓ ‪ّ٠‬ىذذٓ أْ رمجذذً ثذذذاخٍ‪ٙ‬ب ػٍذذ‪ٝ‬‬
‫ِغّ‪ٛ‬ػخ ‪ tuple‬وؼٕصش ‪.‬‬

‫ٌإلػالْ ػٓ اٌزشى‪ٍ١‬خ ‪ Set‬أِش ع‪ِٚ ًٙ‬جبشش ‪٠‬زُ رٌذه ػذٓ غش‪٠‬ذك ‪ٚ‬ظذغ اٌؼٕبصذش داخذً ألذ‪ٛ‬اط } {‬
‫‪ِٚ‬فص‪ٛ‬ي ث‪ّٕٙ١‬ب ثفبصٍخ ‪ٚ Comma‬سِض٘ب ( ‪ ) ,‬أ‪ ٚ‬ػٓ غش‪٠‬ك اٌ ُّش‪١‬ذ )(‪ set‬وب‪٢‬ر‪: ٟ‬‬
‫‪# Different types of sets in Python‬‬
‫‪# set of integers‬‬
‫}‪my_set = {1, 2, 3‬‬
‫)‪print(my_set‬‬

‫‪# set of mixed datatypes‬‬


‫}‪my_set = {1.0, (1, 3), "Hello", (1, 2, 3) , True‬‬
‫)‪print(my_set‬‬

‫‪output‬‬
‫}‪{1, 2, 3‬‬
‫}'‪{1.0, (1, 2, 3), (1, 3), 'Hello‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪66‬‬
: ٟ‫ر‬٢‫ وب‬set() ‫ذ‬١‫ك اٌ ُّش‬٠‫ ػٓ غش‬set ‫ٍخ‬١‫ وؼجبسح ػٓ رشى‬list ‫ص لبئّخ‬١‫ّىٓ رخص‬٠ 
# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list


# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

output
{1, 2, 3, 4}
{1, 2, 3}

‫ٓ } { ال‬٠‫ٓ ٘ذز‬١‫اؽذذ ثذ‬ٚ ‫د ػٕصش‬ٛ‫ع‬ٚ ْ ‫ت ؛‬ٟ‫أِشا صؼجًب ثؼط اٌش‬ ً ‫اؽذ‬ٚ ‫ػخ ثؼٕصش‬ّٛ‫ؼذ ئٔشبت ِغ‬٠ 
َ‫ اعذذذذزخذا‬ٝ‫ٌذذذذزٌه عذذذذٕؾزبط ئٌذذذذ‬ٚ set ‫ظ‬١‫ٌذذذذ‬ٚ dictionary ‫بٔذذذذبد‬١‫ع اٌج‬ٛ‫ؼجذذذذش ػذذذذٓ ٔذذذذ‬٠ ‫ف‬ٛ‫عذذذذ‬ٚ ٟ‫ىفذذذذ‬٠
. set ‫ٍخ فبسغخ‬١‫ش ػٓ رشى‬١‫ذٌٍزؼج‬١‫اٌ ُّش‬
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a


print(type(a))

# initialize a with set()


a = set()

# check data type of a


print(type(a))

output
<class 'dict'>
<class 'set'>

ً ٠‫ّىذذٓ أ‬٠ 
‫ إلٔشذذبت‬set)( ‫ ثذذـ‬ّٝ‫غذذ‬٠ ٞ‫اٌذذز‬ٚ constructor ‫ اٌ ُّٕشذذئ‬ٚ‫ أ‬constructor ‫ذ‬١‫عذذب اعذذزخذاَ اٌ ُّشذذ‬
. ‫ذ٘ب‬٠‫ ٔش‬ٞ‫ اٌز‬set ‫ٍخ‬١‫ٓ اٌزشى‬١‫ع‬ٛ‫ٓ ل‬١‫ب ث‬ٍٙ‫ٔىزت ثذاخ‬ٚ set)( ‫ش ٔىزت‬١‫ ؽ‬set ‫ٍخ‬١‫رشى‬
thisset = set({"apple", "banana", "cherry"})
print(thisset)

output
{'banana', 'apple', 'cherry'}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 61
‫‪ٚ : Set Items‬ؽذاد اٌزشى‪ٍ١‬خ أ‪ ٚ‬ػٕبصش اٌزشى‪ٍ١‬خ‬
‫ػٕبصش اٌمبئّخ ٌ‪ٙ‬ب صالس خصبئص ‪:‬‬
‫‪ o‬ػٕبصش غ‪١‬ش ِف‪ٙ‬شعخ ‪unindexed‬‬
‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ال ‪ّ٠‬ىٓ اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ػٕبصش ‪ set‬ثبٌشع‪ٛ‬ع ئٌ‪ ٝ‬ف‪ٙ‬شط ‪ index‬أ‪ ٚ‬اٌّفزبػ ‪.key‬‬

‫‪ o‬ػٕبصش غ‪١‬ش ِشرجخ ‪unordered‬‬


‫‪٘ٚ‬ذذزا ‪٠‬ؼٕذذ‪ ٟ‬أْ اٌؼٕبصذذش اٌّ‪ٛ‬عذذ‪ٛ‬دح فذذ‪ ٟ‬اٌزشذذى‪ٍ١‬خ ‪ٌ set‬ذذ‪١‬ظ ٌ‪ٙ‬ذذب رشر‪١‬ذذت ِؾذذذد‪ّ٠ٚ .‬ىذذٓ أْ رظ‪ٙ‬ذذش‬
‫ػٕبصذذش اٌّغّ‪ٛ‬ػذذخ ثزشر‪١‬ذذت ِخزٍذذ فذذ‪ ٟ‬وذذً ِذذشح رغذذزخذِ‪ٙ‬ب ‪ٌٚ‬ذذزٌه ال ‪ّ٠‬ىذذٓ اإلشذذبسح ئٌ‪ٙ١‬ذذب‬
‫ثبٌف‪ٙ‬شط ‪ index‬أ‪ ٚ‬اٌّفزبػ ‪.key‬‬

‫‪ o‬ػٕبصش غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش ‪unchangeable‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ال ‪ّ٠‬ىٓ رغ‪١١‬ش اٌؼٕبصش ثؼذ ئٔشبت اٌّغّ‪ٛ‬ػخ‪.‬‬

‫‪ِ ‬الؽظخ ‪ :‬ثّغشد ئٔشبت ‪ set‬ال ‪ّ٠‬ىٕه رغ‪١١‬ش ػٕبصش٘ب ‪ٌٚ‬ىٓ ‪ّ٠‬ىٕه ئظبفخ ػٕبصش عذ‪٠‬ذح‪.‬‬

‫‪ o‬ػٕبصش ال رغّؼ ثم‪ِ ُ١‬ىشسح ‪unduplicated values‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ال ‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬اٌزشى‪ٍ١‬خ ػٍ‪ ٝ‬ػٕبصش‪ ٓ٠‬ثٕفظ اٌم‪ّ١‬خ ‪.‬‬

‫‪ ‬و‪١‬ف‪١‬خ اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ا ؽشف أ‪ ٚ‬اٌؼٕبصش اٌّ‪ٛ‬ع‪ٛ‬دح داخً اٌّغّ‪ٛ‬ػخ ‪ Tuple‬؟‬
‫‪ ‬ال ‪ّ٠‬ىٕٕب اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ػٕبصش اٌزشى‪ٍ١‬خ ‪ set‬ثذبٌشع‪ٛ‬ع ئٌذ‪ ٝ‬اٌف‪ٙ‬شعذخ ‪ٚ index‬ػبِذً اٌزمط‪١‬ذغ ( ‪٠ ) :‬جذذأ‬
‫اٌف‪ٙ‬ذذشط ِذذٓ ‪ 0‬وّذذب دسعذذٕب فذذ‪ ٟ‬اٌّغّ‪ٛ‬ػذذبد ا‪٢‬خذذش‪ٌٚ . ٜ‬ىذذٓ ‪ّ٠‬ىٕذذه ئعذذشات رىذذشاس خذذالي ػٕبصذذش‬
‫اٌّغّ‪ٛ‬ػخ ثبعزخذاَ ؽٍمخ ‪. for‬‬
‫}"‪thisset = {"apple", "banana", "cherry‬‬
‫‪for x in thisset:‬‬
‫)‪print(x‬‬

‫‪output‬‬
‫‪banana‬‬
‫‪apple‬‬
‫‪cherry‬‬

‫‪ّ٠ ‬ىٕه اخزجبس ِب ئرا وبٔذ ٕ٘بن ل‪ّ١‬خ ِؾذدح ِ‪ٛ‬ع‪ٛ‬دح ف‪ set ٟ‬ثبعزخذاَ اٌىٍّخ ا عبع‪١‬خ ‪.in‬‬

‫}"‪thisset = {"apple", "banana", "cherry‬‬


‫)‪print("banana" in thisset‬‬

‫‪output‬‬
‫‪True‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪62‬‬
ّٝ‫غذ‬٠ ‫س خطذأ‬ٚ‫ ؽذذ‬ٝ‫ رٌذه ئٌذ‬ٞ‫إد‬١‫ عذ‬index ‫ذشط‬ٙ‫ذك اٌف‬٠‫ ػٕصذش ػذٓ غش‬ٝ‫ي ئٌذ‬ٛ‫ص‬ٌٛ‫ٌخ ا‬ٚ‫ ػٕذ ِؾب‬
. ‫ش ِشرجخ‬١‫غ‬ٚ ‫شعخ‬ٙ‫ش ِف‬١‫ب ػٕبصش غ‬ٙٔ ‫ ؛‬TypeError

thisset = {"apple", "banana", "cherry"}


print(thisset[1])

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 2, in <module>
print(thisset[1])
TypeError: 'set' object is not subscriptable

‫ٍخ‬١‫ً ػٕبصش اٌزشى‬٠‫ رؼذ‬: Modify Set Items


.‫ب‬ٙ‫ص‬١‫ش ػٕبصش٘ب ثّغشد رخص‬١١‫ّىٓ رغ‬٠ ‫ أٔٗ ال‬ٟٕ‫ؼ‬٠ ‫ش ِّب‬١١‫ش لبثٍخ ٌٍزغ‬١‫ٍخ غ‬١‫اٌزشى‬
thisset = {"apple", "banana", "cherry"}
print(thisset[1]=="Grape")

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
print(thisset[1]=="Grape")
TypeError: 'set' object is not subscriptable

‫ٍخ‬١‫ ئظبفخ ػٕبصش ٌٍزشى‬: add set Items


: ٓ١‫ك اٌذاٌز‬٠‫ػخ ِٓ اٌؼٕبصش ػٓ غش‬ّٛ‫ ِغ‬ٚ‫ّىٕٕب ئظبفخ ػٕصش أ‬٠
. ‫ٍخ‬١‫ اٌزشى‬ٌٝ‫اؽذ ئ‬ٚ ‫ داٌخ اإلظبفخ ؛ إلظبفخ ػٕصش‬: add() function 
. ‫ٍخ‬١‫ اٌزشى‬ٌٝ‫ش ؛ إلظبفخ ػذح ػٕبصش ئ‬٠‫ داٌخ اٌزؾذ‬: update() function 
#To add one item to a set
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)

#To add items from another set into the current set
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)

output
{'banana', 'orange', 'cherry', 'apple'}
{'papaya', 'mango', 'banana', 'pineapple', 'cherry', 'apple'}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 63
dictionaries ٚ‫ أ‬tuples ٚ‫ أ‬lists ً‫ وذذبئٓ لبثذذً ٌٍزىذذشاس ِضذذ‬ٞ‫ أ‬update() ‫ّىذذٓ أْ رأخذذز اٌذاٌذذخ‬٠ 
.‫زُ رغٕت اٌزىشاساد‬٠ ‫غ اٌؾبالد‬١ّ‫ ع‬ٟ‫ٌىٓ ف‬ٚ ‫ب‬ٌٙ ‫وّؼبِالد‬
# add list to set
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)

# add list and set


my_set = {1, 3}
my_set.update([4, 5], {1, 6, 8})
print(my_set)

output
{'cherry', 'kiwi', 'apple', 'orange', 'banana'}
{1, 3, 4, 5, 6, 8}

‫ٍخ‬١‫ ئصاٌخ ػٕبصش اٌزشى‬: remove set Items


: ٓ١‫ك اٌذاٌز‬٠‫ػخ ِٓ اٌؼٕبصش ػٓ غش‬ّٛ‫ ِغ‬ٚ‫ّىٕٕب ؽزف ػٕصش أ‬٠
.‫ داٌخ اإلصاٌخ ؛ إلصاٌخ اٌؼٕصش اٌّؾذد‬: remove() function 
.‫رغبً٘ اٌؼٕصش اٌّؾذد‬ٚ ‫ داٌخ اٌزغبً٘ ؛ إلصاٌخ‬: discard() function 
. ‫ٍخ‬١‫ اٌزشى‬ٟ‫ش ف‬١‫خ‬٢‫ داٌخ اإلصاٌخ ؛ إلصاٌخ اٌؼٕصش ا‬: pop() function 
. ‫ٍخ‬١‫ داٌخ اٌّغؼ ؛ ٌّغؼ ػٕبصش اٌزشى‬: clear() function 
. ‫ٍخ وبٍِخ‬١‫ داٌخ اٌؾزف ؛ ٌؾزف اٌزشى‬: del() function 
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)

# discard an element
my_set.discard(4) # Output: {1, 3, 5, 6}
print(my_set)

# remove an element
my_set.remove(6) # Output: {1, 3, 5}
print(my_set)

# clear my_set
my_set.clear() # Output: set()
print(my_set)

output
{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
set()
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 64
ٓ‫ذش ِشرجذخ ٌذزٌه ٌذ‬١‫ػذخ غ‬ّٛ‫ ِغ‬ٝ‫ ٘ذ‬set ‫ٍخ‬١‫ٌىٓ رزوش أْ اٌزشى‬ٚ ‫ش‬١‫ً اٌؼٕصش ا خ‬٠‫ عزض‬pop() ‫ اٌذاٌخ‬
ْ‫ف الؽذع أ‬ٚ‫ش اٌؼٕصش اٌّؾز‬١١‫زُ رغ‬١‫د ع‬ٛ‫ز اٌى‬١‫زُ رٕف‬٠ ‫ وً ِشح‬ٟ‫ف‬ٚ ٗ‫ رزُ ئصاٌز‬ٞ‫رؼشف اٌؼٕصش اٌز‬
.ٗ‫ رّذ ئصاٌز‬ٞ‫ اٌؼٕصش اٌز‬ٟ٘ pop)( ‫ّخ ئسعبع اٌذاٌخ‬١‫ل‬
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)

# pop an element
print("the removed element is : ",my_set.pop())

# initialize my_set
# Output: set of unique elements
my_set = set("HelloWorld")
print(my_set)

# pop an element
print("the removed element is : ",my_set.pop())

output
{1, 3, 4, 5, 6}
the removed element is : 1
{'o', 'l', 'd', 'H', 'W', 'r', 'e'}
the removed element is : o

: ٟ‫ر‬٢‫ ا‬ٛ٘ discard() ‫اٌذاٌخ‬ٚ remove() ‫ٓ اٌذاٌخ‬١‫ اٌفشق ث‬


. ‫د‬ٛ‫ع‬ِٛ ‫ش‬١‫س خطأ ئرا وبْ اٌؼٕصش اٌّشاد ئصاٌزٗ غ‬ٛٙ‫ ظ‬ٌٝ‫ ئ‬ٞ‫ فغزإد‬remove() ‫ اٌذاٌخ‬
# remove my_set
my_set = {1, 3, 4, 5, 6}
my_set.remove(2)
print(my_set)

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 3, in <module>
my_set.remove(2)
KeyError: 2

. ‫د‬ٛ‫ع‬ِٛ ‫ش‬١‫س خطأ ئرا وبْ اٌؼٕصش اٌّشاد ئصاٌزٗ غ‬ٛٙ‫ ظ‬ٌٝ‫ ئ‬ٞ‫ ال رإد‬discard() ‫ اٌذاٌخ‬
# discard my_set
my_set = {1, 3, 4, 5, 6}
my_set.discard(2)
print(my_set)

output
{1, 3, 4, 5, 6}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 65
: ٟ‫ر‬٢‫ خطأ وب‬ٟ‫ؼط‬١‫ب ع‬ٙ‫ ثؼذ ؽزف‬set ‫ الؽع ػٕذ غجبػخ‬
#this will raise an error because the set no longer exists
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 4, in <module>
print(thisset)
NameError: name 'thisset' is not defined

ْٛ‫ض‬٠‫ٍخ داخً ٌغخ اٌجب‬١‫ اٌزشى‬ٍٝ‫بد ػ‬١ٍّ‫ ػ‬: Python Set Operations
‫اٌزمذذبغغ‬ٚ union ‫خ ِضذذً االرؾذذبد‬١‫بظذذ‬٠‫ػذذخ س‬ّٛ‫ذذبد ِغ‬١ٍّ‫ إلعذذشات ػ‬Sets ‫الد‬١‫ّىذذٓ اعذذزخذاَ اٌزشذذى‬٠
‫ّىٕٕذذب‬٠ٚ symmetric difference ً‫اٌفذذشق اٌّزّبصذذ‬ٚ difference ‫االخذذزالف‬ٚ intersection
. ‫خ‬١‫ر‬٢‫اي ا‬ٚ‫ اٌذ‬ٚ‫اًِ أ‬ٛ‫ك اٌؼ‬٠‫بَ ثزٌه ػٓ غش‬١‫اٌم‬

ٓ١‫ رشىٍز‬ٚ‫ٓ أ‬١‫ػز‬ّٛ‫خ ارؾبد ِغ‬١ٍّ‫ ػ‬: Set Union : ً‫ال‬ٚ‫ أ‬
ً‫دح داخذذذ‬ٛ‫عذذذ‬ٌّٛ‫ذذذغ اٌؼٕبصذذذش ا‬١ّ‫ ع‬ٝ‫ي ػٍذذذ‬ٛ‫ذذذخ ٌٍؾصذذذ‬١ٍّ‫ ػ‬ٝ‫٘ذذذ‬
: ‫ك‬٠‫زُ رٌه ػٓ غش‬٠ٚ sets ٓ١‫ػز‬ّٛ‫ِغ‬
: ) | ( ‫ ِؼبًِ االرؾبد‬o
.ٓ١‫ػز‬ّٛ‫غ اٌؼٕبصش داخً اٌّغ‬١ّ‫ ع‬ٍٟ‫ ػ‬ٜٛ‫ذح رؾز‬٠‫ عذ‬set ‫ ثزشعغ‬: union() ‫ داٌخ‬o
.1 ‫ػخ‬ّٛ‫ داخً اٌّغ‬2 ‫ػخ‬ّٛ‫ اٌّغ‬ٟ‫دح ف‬ٛ‫ع‬ٌّٛ‫ ثزُذسط اٌؼٕبصش ا‬: update() ‫ داٌخ‬o
# Set union method

set1 = {1, 2, 3, 4, 5} o
set2 = {4, 5, 6, 7, 8}

# use | operator
print(set1 | set2)

# use union() method


set3 = set1.union(set2)
print(set3)

# use update() method


set1.update(set2)
print(set1)

output
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 66
‫‪ ‬صبٔ‪١‬ب ً ‪ : Set Intersection :‬ػٍّ‪١‬خ رمبغغ ِغّ‪ٛ‬ػز‪ ٓ١‬أ‪ ٚ‬رشىٍز‪ٓ١‬‬
‫٘‪ ٝ‬ػٍّ‪١‬خ ٌٍؾص‪ٛ‬ي ػٍ‪ ٝ‬اٌؼٕبصذش اٌّشذزشوخ ثذ‪ِ ٓ١‬غّذ‪ٛ‬ػز‪sets ٓ١‬‬
‫‪٠ٚ‬زُ رٌه ػٓ غش‪٠‬ك ‪:‬‬
‫‪ِ o‬ؼبًِ اٌزمبغغ ( & ) ‪:‬‬
‫‪ o‬داٌخ )( ‪ : intersection‬ثزشعغ ‪ set‬عذ‪٠‬ذح رؾز‪ ٜٛ‬ػٍ‪ ٟ‬اٌؼٕبصش اٌّشزشوخ ث‪ِ ٓ١‬غّ‪ٛ‬ػز‪.ٓ١‬‬
‫‪ o‬داٌخ )( ‪ : intersection_update‬ثزؾزفع ثبٌؼٕبصش اٌّىشسح اٌّ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬وال اٌّغّ‪ٛ‬ػز‪. ٓ١‬‬
‫‪# Set union method‬‬
‫‪‬‬
‫}‪set1 = {1, 2, 3, 4, 5‬‬ ‫‪o‬‬
‫}‪set2 = {4, 5, 6, 7, 8‬‬

‫‪# use & operator‬‬


‫)‪print(set1 & set2‬‬

‫‪# use intersection() method‬‬


‫)‪set3 = set1.intersection(set2‬‬
‫)‪print(set3‬‬

‫‪# use intersection_update() method‬‬


‫)‪set1.intersection_update(set2‬‬
‫)‪print(set1‬‬

‫‪output‬‬
‫}‪{4, 5‬‬
‫}‪{4, 5‬‬
‫}‪{4, 5‬‬

‫‪ ‬صبٌضب ً ‪ : Set Difference :‬ػٍّ‪١‬خ اٌفشق ث‪ِ ٓ١‬غّ‪ٛ‬ػز‪ ٓ١‬أ‪ ٚ‬رشى‪١‬ز‪ٓ١‬‬


‫٘ذذذذ‪ ٝ‬ػٍّ‪١‬ذذذذخ اٌؾصذذذذ‪ٛ‬ي ػٍذذذذ‪ِ ٝ‬غّ‪ٛ‬ػذذذذخ ِذذذذٓ اٌؼٕبصذذذذش اٌّ‪ٛ‬عذذذذ‪ٛ‬دح أؽذذذذذ‬
‫اٌّغّ‪ٛ‬ػز‪١ٌٚ ٓ١‬غذ ِ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ ا‪٢‬خش‪٠ٚ ٜ‬زُ رٌه ػٓ غش‪٠‬ك ‪:‬‬
‫‪ِ o‬ؼبًِ اٌفشق ( ‪: ) -‬‬
‫‪ o‬داٌخ االرؾبد )(‪ : difference‬ثزشعغ ‪ set‬عذ‪٠‬ذح رؾز‪ ٜٛ‬ػٍ‪ ٟ‬اٌفشق ث‪ ٓ١‬اٌّغّ‪ٛ‬ػز‪.ٓ١‬‬
‫‪ o‬داٌذذخ االرؾذذبد )(‪ : difference_update‬ثزؾصذذً ػٍذذ‪ ٝ‬اٌؼٕبصذذش اٌّ‪ٛ‬عذذ‪ٛ‬دح فذذ‪ ٟ‬أؽذذذ اٌّغّذذ‪ٛ‬ػز‪ٓ١‬‬
‫‪١ٌٚ‬غذ ِ‪ٛ‬ع‪ٛ‬دح ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ ا‪٢‬خش‪.ٜ‬‬

‫‪ٔ ‬غزخذَ ٘ذزٖ اٌؼجذبسح اٌش‪٠‬بظذ‪١‬خ (‪ٌٍ A)A-B‬ؾصذ‪ٛ‬ي ػٍذ‪ِ ٝ‬غّ‪ٛ‬ػذخ اٌؼٕبصذش اٌّ‪ٛ‬عذ‪ٛ‬دح فمذػ فذ‪A ٟ‬‬
‫‪١ٌٚ‬غذ ِ‪ٛ‬ع‪ٛ‬دح ‪. B‬‬

‫‪ٔ ‬غذزخذَ ٘ذزٖ اٌؼجذذبسح اٌش‪٠‬بظذ‪١‬خ (‪ٌٍ B)B-A‬ؾصذذ‪ٛ‬ي ػٍذ‪ِ ٝ‬غّ‪ٛ‬ػذذخ اٌؼٕبصذش اٌّ‪ٛ‬عذذ‪ٛ‬دح فمذػ فذذ‪B ٟ‬‬
‫‪١ٌٚ‬غذ ِ‪ٛ‬ع‪ٛ‬دح ‪. A‬‬
‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪62‬‬
# Set union method
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8} o
# set of elements that are only in set1 but not in set2
print(set1 - set2) # use - operator
set3 = set1.difference(set2) # use difference() method
print(set3)
set1.difference_update(set2) # use difference_update() method
print(set1)

# set of elements that are only in set2 but not in set1


print(set2 - set1) # use - operator
set3 = set2.difference(set1) # use difference() method
print(set3)
set2.difference_update(set1) # use difference_update() method
print(set2)

output
{1, 2, 3}
{1, 2, 3}
{1, 2, 3}
{4, 5, 6, 7, 8}
{4, 5, 6, 7, 8}
{4, 5, 6, 7, 8}

ٓ١‫ رشىٍز‬ٚ‫ٓ أ‬١‫ػز‬ّٛ‫خ اٌفشق اٌّّبصً ٌّغ‬١ٍّ‫ ػ‬: Set Symmetric Difference : ً ‫ ساثؼب‬
sets ٓ١‫ػز‬ٛ‫ ِغّذ‬ٟ‫دح فذ‬ٛ‫عذ‬ِٛ ‫ذش‬١‫غ اٌؼٕبصذش اٌغ‬١ّ‫ ع‬ٍٝ‫ي ػ‬ٛ‫خ ٌٍؾص‬١ٍّ‫ ػ‬ٝ٘
: ‫ك‬٠‫زُ رٌه ػٓ غش‬٠ٚ .)‫ّب (ثبعزضٕبت اٌزمبغغ‬ٙ١ٍ‫ و‬ٟ‫ظ ف‬١ٌ ٓ‫ٌى‬ٚ
: ) ^ ( ‫ ِؼبًِ االرؾبد‬o
.ٓ١‫ػز‬ّٛ‫ اٌّغ‬ٟ‫دح ف‬ٛ‫ع‬ِٛ ‫ش‬١‫ رؾزفع ثبٌؼٕبصش اٌغ‬: symmetric_difference() ‫ داٌخ‬o
.ٓ١‫ػز‬ّٛ‫ اٌّغ‬ٟ‫دح ف‬ٛ‫ع‬ِٛ ‫ش‬١‫ رؾزفع ثبٌؼٕبصش اٌغ‬: symmetric_difference_update () ‫ داٌخ‬o

# Set symmetric_difference method


set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# use | operator
print(set1 ^ set2)
# use symmetric_difference() method
set3 = set1.symmetric_difference(set2)
print(set3)
# use symmetric_difference update() method
set1.symmetric_difference_update(set2)
print(set1)

output
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 61
‫‪ ‬خبِغب ً ‪ : Set Iterating :‬ػٍّ‪١‬خ رىشاس اٌزشى‪ٍ١‬خ أ‪ ٚ‬اٌّغّ‪ٛ‬ػخ‬
‫ظشا ْ اٌزشى‪ٍ١‬خ أ‪ ٚ‬اٌّغّ‪ٛ‬ػخ ‪ Set‬ػجذبسح ػذٓ ِصذف‪ٛ‬فبد ‪ arrays‬ف‪ّ١‬ىٕٕذب اٌّذش‪ٚ‬س ػجذش اٌؼٕبصذش‬ ‫ٔ ً‬
‫داخً اٌّغّ‪ٛ‬ػخ ثبعزخذاَ اٌؾٍمخ اٌزىشاس‪٠‬خ ‪ِ Looping‬ضً ؽٍمخ ‪. for‬‬
‫‪for letter in set("apple"):‬‬
‫)‪print(letter‬‬

‫‪output‬‬
‫‪p‬‬
‫‪a‬‬
‫‪e‬‬
‫‪l‬‬

‫‪ ‬ال ‪ّ٠‬ىٓ ػًّ ؽٍمخ رىشاس‪٠‬خ ػٓ غش‪٠‬ك ‪ ْ while‬اٌّغّ‪ٛ‬ػخ ‪ set‬ال رذػُ اٌف‪ٙ‬شعخ ‪. index‬‬

‫‪ ‬عبدعب ً ‪ : Set Membership Test :‬ػٍّ‪١‬خ اخزجبس ػع‪ ٛ‬داخً اٌزشى‪ٍ١‬خ أ‪ ٚ‬اٌّغّ‪ٛ‬ػخ ‪set‬‬
‫‪ّ٠‬ىذذٓ اعذذزخذاَ ِؼذذبِالد اٌؼعذذ‪٠ٛ‬خ داخذذً اٌّغّ‪ٛ‬ػذذخ ٌؼّذذً اخزجذذبس ِذذب ئرا وبٔذذذ اٌّغّ‪ٛ‬ػذذخ اٌفشػ‪١‬ذذخ‬
‫ِ‪ٛ‬ع‪ٛ‬دح داخً ِغّ‪ٛ‬ػخ ‪ Set‬أَ ال ؟ ‪ٕ٘ٚ‬بن وٍّز‪ ٓ١‬الخزجبس ػٕصش داخً رشى‪ٍ١‬خ ّ٘ب ‪:‬‬
‫‪ : in keyword ‬وٍّخ أعبع‪١‬خ رغزخذَ ٌٍزؾمك ِٓ ‪ٚ‬ع‪ٛ‬د ػٕصش ِؼ‪ ٓ١‬ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ‪.‬‬
‫‪ : not in keyword ‬وٍّخ أعبع‪١‬خ رغزخذَ ٌٍزؾمك ِٓ ػذَ ‪ٚ‬ع‪ٛ‬د ػٕصش ِؼ‪ ٓ١‬ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ‪.‬‬
‫‪# in keyword in a set‬‬
‫‪# initialize my_set‬‬
‫)"‪my_set = set("apple‬‬

‫‪# check if 'a' is present‬‬


‫‪# Output: True‬‬
‫)‪print('a' in my_set‬‬

‫‪# check if 'p' is present‬‬


‫‪# Output: False‬‬
‫)‪print('p' not in my_set‬‬

‫‪output‬‬
‫‪True‬‬
‫‪False‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪64‬‬
ْٛ‫ض‬٠‫ داخً ٌغخ اٌجب‬set ‫ػخ‬ّٛ‫اي خبصخ ثبٌّغ‬ٚ‫ د‬: Python Set methods
: ‫ٕخ‬١‫بَ ِؼ‬ِٙ ‫ز‬١‫ ٌزٕف‬Set ‫ػخ‬ّٛ‫ اٌّغ‬ٚ‫ٍخ أ‬١‫ رزؼبًِ ِغ اٌزشى‬ٟ‫اي اٌز‬ٚ‫ػخ ِٓ اٌذ‬ّٛ‫ٕ٘بن ِغ‬
. ٓ‫ ثزشعغ ػذد ػٕبصش اٌىبئ‬ٞ‫ػخ أ‬ّٛ‫ي اٌّغ‬ٛ‫ي ؛ ٌّؼشفخ غ‬ٛ‫ داٌخ اٌط‬: len() function 
. set ‫ػخ‬ّٛ‫ ؛ ٌّؼشفخ أوجش ػٕصش داخً اٌّغ‬ٜ‫ّخ اٌىجش‬١‫ داٌخ اٌم‬: max() function 
. set ‫ػخ‬ّٛ‫ ؛ ٌّؼشفخ أصغش ػٕصش داخً اٌّغ‬ٜ‫ّخ اٌصغش‬١‫ داٌخ اٌم‬: min() function 
. set ‫ػخ‬ّٛ‫ع وً اٌؼٕبصش داخً اٌّغ‬ّٛ‫ داٌخ اٌغّغ ؛ ٌّؼشفخ ِغ‬: sum() function 
‫ػذذخ (ال رفذذشص‬ّٛ‫ذذذح ِذذٓ ػٕبصذذش اٌّغ‬٠‫ذذت ؛ رُشعذذغ لبئّذذخ ِشرجذذخ عذ‬١‫ داٌذذخ اٌزشر‬: sorted() function 
.) ‫ب‬ٙ‫ػخ ٔفغ‬ّٛ‫اٌّغ‬
number = {3, 2, 8, 5, 10, 6}

print("The length of set is:", len(number))


print("The largest number is:", max(number))
print("The smallest number is:", min(number))
print("The Sum of set is:", sum(number))
print("The Sorted set is:", sorted(number))

output
The length of set is: 6
The largest number is: 10
The smallest number is: 2
The Sum of set is: 34
The Sorted set is: [2, 3, 5, 6, 8, 10]

https://www.w3schools.com ‫ ٘زا اٌشاثػ‬ٍٝ‫اي ثبٌظغػ ػ‬ٚ‫ذ ِٓ اٌذ‬٠‫ اٌّض‬ٍٝ‫ّىٕه اٌزؼشف ػ‬٠ 


. https://www.programiz.com ‫ ٘زا اٌشاثػ‬ٚ‫أ‬

‫ػخ اٌ ُّغّذح‬ّٛ‫ اٌّغ‬: Python Frozenset


unchangeable ‫ذذش ػٕبصذذش٘ب‬١١‫ّىذذٓ رغ‬٠ ‫ٌىذذٓ ال‬ٚ set ‫ػذذخ‬ّٛ‫ذذب خصذذبئص اٌّغ‬ٌٙ ‫ذذذح‬٠‫ فئذذخ عذ‬ٟ‫٘ذذ‬
ٟ‫ ٘ذ‬frozensets ْ‫ فا‬immutable lists ‫ائُ صبثزخ‬ٛ‫ ػجبسح ػٓ ل‬tuple ْ‫ٓ أ‬١‫ ؽ‬ٟ‫ب ف‬ٕٙ١١‫ثّغشد رؼ‬
. immutable sets ‫ش‬١١‫ش لبثٍخ ٌٍزغ‬١‫ػبد غ‬ّٛ‫ِغ‬

unhashable ‫ذش لبثٍذخ ٌٍزغضئذخ‬١‫ْ غ‬ٛ‫ذب رىذ‬ٙٔ‫ فا‬mutable ‫ذش‬١١‫ْ لبثٍخ ٌٍزغ‬ٛ‫ رى‬ٟ‫ اٌز‬sets ‫ػبد‬ّٛ‫اٌّغ‬
‫ لبثٍذخ‬frozensets ْ‫ فا‬ٜ‫خ أخش‬١‫ ِٓ ٔبؽ‬.dictionary keys ‫ط‬ِٛ‫ؼ لب‬١‫ب وّفبر‬ِٙ‫ّىٓ اعزخذا‬٠ ‫ٌزا ال‬
. dictionary keys ‫ط‬ِٛ‫ؼ ٌٍمب‬١‫ب وّفبر‬ِٙ‫ّىٓ اعزخذا‬٠ ‫ٌزا‬ٚ hashable ‫ٌٍزغضئخ‬

isdisjoint() ٚ intersection() ٚ difference() ٚ copy() ‫اي وذـ‬ٚ‫ذ ِٓ اٌذذ‬٠‫ع اٌؼذ‬ٌٕٛ‫ذػُ ٘زا ا‬٠
ُ‫ذذب ال رذذذػ‬ٕٙ‫ٌى‬ٚ union)( ٚ symmetric_difference() ٚ issuperset() ٚ issubset() ٚ
. immutable ‫ش‬١١‫ش لبثٍخ ٌٍزغ‬١‫ب غ‬ٙٔ add() ‫ئظبفخ اٌؼٕبصش ِضً اٌذاٌخ‬ٚ ‫اي ؽزف‬ٚ‫د‬
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 26
frozenset)( ‫ ثبعزخذاَ اٌذاٌخ‬Frozensets ‫ػبد‬ّٛ‫ّىٓ ئٔشبت ِغ‬٠ 
# Frozensets
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])

print(A)
print(A | B)
print(A & B)
print(A - B)
print(A ^ B)

output
frozenset({1, 2, 3, 4})
frozenset({1, 2, 3, 4, 5, 6})
frozenset({3, 4})
frozenset({1, 2})
frozenset({1, 2, 5, 6})

Frozensets ‫ اٌؾزف ِغ‬ٚ‫اي اإلظبفخ أ‬ٚ‫ّىٓ اعزخذاَ د‬٠ ‫ ال‬

# Frozensets
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])

C=A.add(3)
print(C)

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 6, in <module>
C=A.add(3)
AttributeError: 'frozenset' object has no attribute 'add'

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 21
‫انذرس انثايٍ ‪ :‬انقايوس‬
‫)‪(Dictionary‬‬
‫‪ ‬عززؼٍُ ف‪٘ ٟ‬زا اٌجشٔبِظ اٌزؼٍ‪ِ ّٟ١‬ب ٘‪ ٛ‬اٌمبِ‪ٛ‬ط ‪ Dictionary‬فذ‪ٚ Python ٟ‬و‪١‬ف‪١‬ذخ ئٔشذبئٗ ‪ٚ‬و‪١‬ف‪١‬ذخ‬
‫اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ٚ ٗ١‬ئظبفزٗ ‪ٚ‬ئصاٌزٗ ِٓ اٌؼٕبصش ‪ٚ‬اٌذ‪ٚ‬اي اٌّخزٍفخ اٌز‪٠ ٟ‬غت أْ رى‪ ْٛ‬ػٍ‪ ٝ‬دسا‪٠‬خ ث‪ٙ‬ب‪.‬‬

‫‪ : Dictionary‬اٌمبِ‪ٛ‬ط‬
‫٘‪ ٝ‬ػجبسح ػٓ عٍغٍخ غ‪١‬ش غ‪١‬ش ِشرجخ ِٓ ص‪ٚ‬ع‪ِ ٓ١‬شرج‪ّ٘ ٓ١‬ب ص‪ٚ‬ط اٌّفزبػ ‪ٚ‬اٌم‪ّ١‬خ أ‪ِ ٚ‬ب ‪٠‬ؼذشف ثذـبعُ‬
‫‪. key-value pairs‬‬

‫رمذَ ‪ِ Python‬غّ‪ٛ‬ػخ ِٓ أٔ‪ٛ‬اع اٌج‪١‬بٔذبد اٌّشوجذخ اٌزذ‪٠ ٟ‬شذبس ئٌ‪ٙ١‬ذب غبٌجًذب ثبٌزغٍغذالد ‪. sequences‬‬
‫‪ٚ‬اٌمبِ‪ٛ‬ط ‪٘ dictionary‬ذ‪ٚ ٟ‬اؽذذح ِذٓ أٔذ‪ٛ‬اع اٌج‪١‬بٔذبد اٌّغذزخذِخ فذ‪ٚ .Python ٟ‬رغذزخذَ اٌمذ‪ٛ‬اِ‪١‬ظ‬
‫ثشىً ػبَ ِذغ اٌىّ‪١‬ذخ اٌ‪ٙ‬بئٍذخ ِذٓ اٌج‪١‬بٔذبد ‪ٚ‬رذُ رؾغذ‪ ٓ١‬اٌمذ‪ٛ‬اِ‪١‬ظ ‪ dictionary‬العذزشعبع اٌج‪١‬بٔذبد‬
‫ثغ‪ٌٛٙ‬خ ‪ٚ‬ثطش‪٠‬مخ عش‪٠‬ؼخ ‪.‬‬

‫‪٠‬غذذت أْ ٔؼذذشف اٌّفزذذبػ ‪ key‬العذذزشداد اٌم‪ّ١‬ذذخ ‪ٌٚ value‬ىذذٓ ٌذذ‪١‬ظ اٌؼىذذظ ‪ّ٠ٚ‬ىذذٓ أْ ‪٠‬ىذذ‪ ْٛ‬اٌّفزذذبػ‬
‫‪ٚ‬اٌم‪ّ١‬خ ِٓ أ‪ٛٔ ٞ‬ع ‪.‬‬

‫اٌمذذبِ‪ٛ‬ط ‪٘ dictionary‬ذذ‪ٚ ٟ‬اؽذذذح ِذذٓ ‪ 4‬أٔذذ‪ٛ‬اع ث‪١‬بٔذذبد ِعذذّٕخ فذذ‪ٚ Python ٟ‬اٌزذذ‪ ٟ‬رُغذذزخذَ ٌزخذذض‪ٓ٠‬‬
‫ِغّ‪ٛ‬ػذذذذخ ِذذذذٓ اٌج‪١‬بٔذذذذبد ‪ٚ‬اٌضالصذذذذخ ا خذذذذش‪٘ ٜ‬ذذذذ‪ٚ tuple ٚ Set ٚ lists ٟ‬وٍ‪ٙ‬ذذذذب راد خصذذذذبئص‬
‫‪ٚ‬اعزخذاِبد ِخزٍفخ ‪٠ٚ‬شبس ئٌ‪٘ ٝ‬زٖ ا ٔ‪ٛ‬اع ا سثؼخ ثبعُ اٌزغٍغالد ‪. sequences‬‬

‫‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬ػٍ‪ ٝ‬أ‪ ٞ‬ػذد ِٓ اٌؼٕبصش ‪ٚ‬لذ رى‪ ِٓ ْٛ‬أٔذ‪ٛ‬اع ِخزٍفذخ (‪ integer‬أ‪ floatٚ‬أ‪string ٚ‬‬
‫عذب‬
‫أ‪ ٚ‬أ‪ٛٔ ٞ‬ع آخش ) وّب ‪ّ٠‬ىٓ أْ ‪٠‬ؾز‪ ٞٛ‬اٌمبِ‪ٛ‬ط ‪ dictionary‬ػٍ‪ ٝ‬لبِ‪ٛ‬ط ‪ dictionary‬آخش أ‪ً ٠‬‬
‫‪٠ٚ‬غّ‪ ٝ‬ف‪٘ ٟ‬زٖ اٌؾبٌخ ثبعُ ‪. nested dictionary‬‬

‫ث‪ّٕ١‬ب ‪ّ٠‬ىٓ أْ رى‪ ْٛ‬اٌم‪ ِٓ values ُ١‬أ‪ٛٔ ٞ‬ع ث‪١‬بٔبد وّب ‪ّ٠‬ىذٓ رىشاس٘ذب فذاْ اٌّفزذبػ ‪٠ key‬غذت أْ‬
‫‪٠‬ىذذ‪ِ ْٛ‬ذذٓ إٌذذ‪ٛ‬ع اٌضبثذذذ ‪ِ immutable type‬ضذذً (عٍغذذٍخ ‪ string‬أ‪ ٚ‬سلذذُ ‪ number‬أ‪ِ ٚ‬غّ‪ٛ‬ػذذخ‬
‫‪ِ tuple‬غ ػٕبصش غ‪١‬ش لبثٍخ ٌٍزغ‪١١‬ش ) ‪٠ٚ‬غت أْ رى‪ ْٛ‬ل‪ ُ١‬اٌّفزبػ ‪ key‬فش‪٠‬ذح ‪.‬‬

‫ٌإلػالْ ػٓ اٌمذ‪ٛ‬اِ‪١‬ظ ‪ dictionary‬أِذش عذ‪ِٚ ًٙ‬جبشذش ‪٠‬ذزُ رٌذه ػذٓ غش‪٠‬ذك ‪ٚ‬ظذغ اٌؼٕبصذش داخذً‬
‫ألذذذ‪ٛ‬اط } { ‪ِٚ‬فصذذذ‪ٛ‬ي ث‪ّٕٙ١‬ذذذب ثفبصذذذٍخ ‪ٚ Comma‬سِض٘ذذذب ( ‪ٔٚ ) ,‬فصذذذً ثذذذ‪ ٓ١‬وذذذً ‪key-value‬‬
‫ثبٌٕمطز‪ ٓ١‬اٌشئغ‪١‬ز‪ٚ colon ٓ١‬سِض٘ب ( ‪ ) :‬وب‪٢‬ر‪: ٟ‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪22‬‬
# empty dictionary
my_dict = {}
print(my_dict)

# dictionary with integer keys


my_dict = {1: 'apple', 2: 'ball'}
print(my_dict)

# dictionary with nested dictionary


my_dict = {
'fruit': 'apple',
'number':{'one':1 ,'two':2}
}
print(my_dict)

# dictionary with mixed keys


my_dict = {'name': 'John', 1: [2, 4, 3]}
print(my_dict)

print("\n")
#nested dictionary
myfamily = {"child1" : {"name" : "Emil","year" : 2004},
"child2" : {"name" : "Tobias", "year" : 2007},
"child3" : { "name" : "Linus", "year" : 2011 }
output
{}
{1: 'apple', 2: 'ball'}
{'fruit': 'apple', 'number': {'one': 1, 'two': 2}}
{'name': 'John', 1: [2, 4, 3]}

{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007},
'child3': {'name': 'Linus', 'year': 2011}}

ً ٠‫ّىٓ أ‬٠ 
‫ إلٔشذبت‬dict)( ‫ ثذـ‬ّٝ‫غذ‬٠ ٞ‫اٌذز‬ٚ constructor ‫ اٌ ُّٕشذئ‬ٚ‫ أ‬constructor ‫ذ‬١‫عب اعذزخذاَ اٌ ُّشذ‬
. ‫ذ٘ب‬٠‫ ٔش‬ٞ‫ػخ اٌز‬ّٛ‫ٓ اٌّغ‬١‫ع‬ٛ‫ٓ ل‬١‫ب ث‬ٍٙ‫ٔىزت ثذاخ‬ٚ dict)( ‫ش ٔىزت‬١‫ ؽ‬dictionary ‫ط‬ِٛ‫لب‬
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
print(my_dict)

# from sequence having each item as a pair


my_dict = dict([(1,'apple'), (2,'ball')])
print(my_dict)

output
{1: 'apple', 2: 'ball'}
{1: 'apple', 2: 'ball'}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 23
‫‪ٚ : dictionary Items‬ؽذاد اٌمبِ‪ٛ‬ط أ‪ ٚ‬ػٕبصش اٌمبِ‪ٛ‬ط‬
‫ػٕبصش اٌمبئّخ ٌ‪ٙ‬ب صالس خصبئص ‪:‬‬
‫‪ o‬ػٕبصش غ‪١‬ش ِف‪ٙ‬شعخ ‪unindexed‬‬
‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ ال ‪ّ٠‬ىٓ اٌ‪ٛ‬ص‪ٛ‬ي ٌٍؼٕبصش ػٓ غش‪٠‬ك اٌف‪ٙ‬شط ‪. index‬‬

‫‪ o‬ػٕبصش غ‪١‬ش ِشرجخ ‪unordered‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أْ اٌؼٕبصش ٌ‪١‬ظ ٌ‪ٙ‬ب رشر‪١‬ت ِؾذد ‪ٚ‬ال ‪ّ٠‬ىٕه اٌشع‪ٛ‬ع ئٌ‪ ٝ‬ػٕصش ثبعزخذاَ ف‪ٙ‬شط‪.‬‬

‫‪ o‬ػٕبصش لبثٍخ ٌٍزغ‪١١‬ش ‪changeable‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ‪ّ٠‬ىٓ رغ‪١١‬ش ‪ٚ‬ئظبفخ ‪ٚ‬ئصاٌخ اٌؼٕبصش ف‪ ٟ‬اٌمبِ‪ٛ‬ط ثؼذ ئٔشبئ‪ٙ‬ب ‪.‬‬

‫‪ o‬ػٕبصش ال رغّؼ ثم‪ِ ُ١‬ىشسح ‪unduplicate values‬‬


‫‪٘ٚ‬زا ‪٠‬ؼٕ‪ ٟ‬أٔٗ ال ‪ّ٠‬ىٓ أْ رؾز‪ ٞٛ‬اٌّغّ‪ٛ‬ػخ ػٍ‪ِ ٝ‬فزبؽ‪ ٓ١‬ثٕفظ االعُ‪.‬‬

‫‪ : Access Dictionary Items‬اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ػٕبصش اٌمبِ‪ٛ‬ط‬


‫‪ّ٠‬ىٕه اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬ػٕبصش اٌمبِ‪ٛ‬ط ثأوضش ِٓ غش‪٠‬مخ وب‪٢‬ر‪: ٟ‬‬
‫‪ ‬ػٓ غش‪٠‬ك ا ل‪ٛ‬اط اٌّشثؼخ ‪ٔٚ Square brackets‬ىزت ثذاخٍ‪ٙ‬ب اعُ اٌّفزبػ وب‪٢‬ر‪. ]key [ ٟ‬‬
‫‪ ‬ػٓ غش‪٠‬ك اٌذاٌخ )(‪ٔٚ get‬ىزت ثذاخٍ‪ٙ‬ب اعُ اٌّفزبػ وب‪٢‬ر‪. get(key) ٟ‬‬
‫‪# get vs [] for retrieving elements‬‬
‫}‪my_dict = {'name': 'Jack', 'age': 26‬‬
‫)]'‪print(my_dict['name‬‬ ‫‪# Output: Jack‬‬
‫))'‪print(my_dict.get('age‬‬ ‫‪# Output: 26‬‬

‫‪output‬‬
‫‪Jack‬‬
‫‪26‬‬

‫‪ ‬ئرا اعزخذِٕب اٌذاٌخ )(‪ get‬فغزشعغ اٌم‪ّ١‬خ ‪ None‬ئرا ٌُ ‪٠‬زُ اٌؼض‪ٛ‬س ػٍ‪ ٝ‬اٌّفزبػ‪.‬‬
‫‪# get vs [] for retrieving elements‬‬
‫}‪my_dict = {'name': 'Jack', 'age': 26‬‬
‫))'‪print(my_dict.get('address‬‬

‫‪output‬‬
‫‪None‬‬

‫‪ ‬ال ‪ّ٠‬ىٕٕب اٌ‪ٛ‬ص‪ٛ‬ي ئٌ‪ ٝ‬اٌؼٕبصش ثبعزخذاَ اٌف‪ٙ‬شعخ ‪ٚ index‬ػبِذً اٌزمط‪١‬ذغ ( ‪ ْ ) :‬اٌمذبِ‪ٛ‬ط ال ‪٠‬ذذػُ‬
‫اٌف‪ٙ‬شط ‪. index‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪24‬‬
.‫ط‬ِٛ‫ اٌمب‬ٟ‫د ِفزبػ ف‬ٛ‫ع‬ٚ َ‫ ؽبٌخ ػذ‬ٟ‫ ف‬KeyError ‫زُ سفغ‬١‫اط اٌّشثؼخ [ ] فغ‬ٛ‫ ئرا اعزخذِٕب ا ل‬
my_dict = {'name': 'Jack', 'age': 26}
print(my_dict['address'])

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 9, in <module>
print(my_dict['address'])
KeyError: 'address'

. dictionary ‫ط‬ِٛ‫ داخً اٌمب‬keys ‫ؼ‬١‫غ اٌّفبر‬١ّ‫ؼ ؛ ٌّؼشفخ ع‬١‫ داٌخ اٌّفبر‬: keys() function 
. dictionary ‫ط‬ِٛ‫ داخً اٌمب‬values ُ١‫غ اٌم‬١ّ‫ُ ؛ ٌّؼشفخ ع‬١‫ داٌخ اٌم‬: values() function 
. dictionary ‫ط‬ِٛ‫ داخذً اٌمذب‬items ‫ذغ اٌؼٕبصذش‬١ّ‫ داٌخ اٌؼٕبصش ؛ ٌّؼشفخ ع‬: items() function 
. list ‫ لبئّخ‬ٟ‫ ف‬tuples ‫ػبد‬ّٛ‫ شىً ِغ‬ٟ‫ش ف‬ٙ‫ظ‬٠ dictionary ‫ط‬ِٛ‫ اٌمب‬ٟ‫ ف‬item ‫وً ػٕصش‬

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

# return a list of all the keys in the dictionary.


x = thisdict.keys()
print(x)

# return a list of all the values in the dictionary.


x = thisdict.values()
print(x)

# return each item in a dictionary, as tuples in a list.


x = thisdict.items()
print(x)

output
dict_keys(['brand', 'model', 'year'])
dict_values(['Ford', 'Mustang', 1964])
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 25
‫ط‬ِٛ‫ً ػٕبصش اٌمب‬٠‫ رؼذ‬: Modify dictionary Items
َ‫دح ثبعذذزخذا‬ٛ‫عذذ‬ٌّٛ‫ّذذخ اٌؼٕبصذذش ا‬١‫ذذش ل‬١١‫ رغ‬ٚ‫ذذذح أ‬٠‫ّىٕٕذذب ئظذذبفخ ػٕبصذذش عذ‬٠ ٞ‫ذذش أ‬١١‫ظ لبثٍذذخ ٌٍزغ‬١ِ‫ا‬ٛ‫اٌمذذ‬
.‫ذخ‬١ٌ‫ّذخ اٌؾب‬١‫ش اٌم‬٠‫زُ رؾذذ‬١‫دًا ثبٌفؼذً فغذ‬ٛ‫عذ‬ِٛ key ‫الػ أٔٗ ئرا وذبْ اٌّفزذبػ‬ٚ .)=( ‫ص‬١‫ػبًِ اٌزخص‬
.‫ط‬ِٛ‫ اٌمب‬ٌٝ‫) ئ‬key : value( ‫ذ‬٠‫ط عذ‬ٚ‫زُ ئظبفخ ص‬١‫ فغ‬key ‫د اٌّفزبػ‬ٛ‫ع‬ٚ َ‫ ؽبٌخ ػذ‬ٟ‫ف‬ٚ
# Changing and adding Dictionary Elements
my_dict = {'name': 'Jack', 'age': 26}

# update value
my_dict['age'] = 27

#Output: {'age': 27, 'name': 'Jack'}


print(my_dict)

# add item
my_dict['address'] = 'Downtown'

# Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}


print(my_dict)

output
{'name': 'Jack', 'age': 27}
{'name': 'Jack', 'age': 27, 'address': 'Downtown'}

‫ط‬ِٛ‫ ئظبفخ ػٕبصش ٌٍمب‬: add dictionary Items


‫ذذك اٌذاٌذذخ‬٠‫ػذذٓ غش‬ٚ‫ّذذخ ٌذذٗ أ‬١‫ٓ ل‬١‫ذذ‬١‫رؼ‬ٚ ‫ذذذ‬٠‫ذذشط ِفزذذبػ عذ‬ٙ‫ط ثبعذذزخذاَ ف‬ِٛ‫ اٌمذذب‬ٝ‫ذذزُ ئظذذبفخ ػٕصذذش ئٌذذ‬٠
‫ ؽبٌذخ‬ٟ‫ فذ‬. ‫ش٘ذب وّؼذبِالد ٌٍذاٌذخ‬٠‫ذزُ رّش‬٠ ٟ‫ط ثبٌؼٕبصذش اٌزذ‬ِٛ‫ش اٌمذب‬٠‫ رؾذ‬ٍٝ‫ رؼًّ ػ‬ٟ‫ اٌز‬update()
‫ط‬ِٛ‫شٖ ػجبسح ػذٓ لذب‬٠‫زُ رّش‬٠ ٞ‫ْ اٌّؼبًِ اٌز‬ٛ‫غت أْ رى‬٠ .‫د اٌؼٕصش عززُ ئظبفخ اٌؼٕصش‬ٛ‫ع‬ٚ َ‫ػذ‬
. )key : value) ‫اط‬ٚ‫لبثال ٌٍزىشاس ِغ أص‬ ً ‫ وبئًٕب‬ٚ‫ أ‬dictionary

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)

thisdict.update({"color": "'blue"})
print(thisdict)

output
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'blue'}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 26
‫ط‬ِٛ‫ ئصاٌخ ػٕبصش اٌمب‬: remove dictionary Items
: ‫ط‬ِٛ‫ٕ٘بن ػذح غشق إلصاٌخ اٌؼٕبصش ِٓ اٌمب‬
. ‫ داٌخ اإلصاٌخ ؛ إلصاٌخ اٌؼٕصش ثبعُ اٌّفزبػ اٌ ُّؾذد‬: pop() function 
. ‫ داٌخ ئصاٌخ اٌؼٕصش ؛ إلصاٌخ آخش ػٕصش ُِذسط‬: popitem() function 
. ‫ط‬ِٛ‫ داٌخ اٌّغؼ ؛ ٌّغؼ ػٕبصش اٌمب‬: clear() function 

. TypeError ٟ‫ب عزؼط‬ٙٔ‫ فا‬pop() ‫ ٌٍذاٌخ‬index ‫شط‬ٙ‫ ئرا ٌُ رؾذد اٌف‬


.ٜٛ‫ ِؾز‬ٍٝ‫ ػ‬ٞٛ‫ؾز‬٠ ‫دح ٌىٕٗ ال‬ٛ‫ع‬ِٛ ‫ط‬ِٛ‫ظً اٌمب‬٠ ٌٟ‫ثبٌزب‬ٚ ‫ط‬ِٛ‫غ اٌمب‬٠‫َ ثزفش‬ٛ‫ رم‬clear() ‫ اٌذاٌخ‬

.ًِ‫ط ثبٌىب‬ِٛ‫ الصاٌخ اٌمب‬ٚ‫ً اٌؼٕصش ثبعُ اٌّفزبػ اٌّؾذد أ‬٠‫ ٌزض‬del ‫خ‬١‫ّىٓ اعزخذاَ اٌىٍّخ ا عبع‬٠ 
# Removing elements from a dictionary
# create a dictionary
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

# remove a particular item, returns its value


print(squares.pop(4)) # Output: 16
print(squares) # Output: {1: 1, 2: 4, 3: 9, 5: 25}

# remove an arbitrary item, return (key,value)


print(squares.popitem()) # Output: (5, 25)
print(squares) # Output: {1: 1, 2: 4, 3: 9}

# remove all items


squares.clear()
print(squares) # Output: {}

output
16
{1: 1, 2: 4, 3: 9, 5: 25}
(5, 25)
{1: 1, 2: 4, 3: 9}
{}

: ٟ‫ر‬٢‫ خطأ وب‬ٟ‫ؼط‬١‫ب ع‬ٙ‫ط ثؼذ ؽزف‬ِٛ‫ الؽع ػٕذ غجبػخ اٌمب‬
thisdict = {"brand": "Ford","model": "Mustang","year": 1964}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.

output
Traceback (most recent call last):
File "c:\Users\Hoda_PC\Desktop\example.py", line 7, in <module>
print(thisdict) #this will cause an error because "thisdict" no longer exists.
NameError: name 'thisdict' is not defined

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 22
ْٛ‫ض‬٠‫ط داخً ٌغخ اٌجب‬ِٛ‫ اٌمب‬ٍٝ‫بد ػ‬١ٍّ‫ ػ‬: Python dictionary Operations
: ٟ‫ر‬٢‫ ا‬dictionary ‫ط‬ِٛ‫ اٌمب‬ٍٝ‫ّىٓ ئعشاؤ٘ب ػ‬٠ ٟ‫ اٌز‬Operations ‫بد‬١ٍّ‫ذ ِٓ اٌؼ‬٠‫ٕ٘بن اٌؼذ‬

‫ػخ‬ّٛ‫خ رىشاس اٌّغ‬١ٍّ‫ ػ‬: Tuple Iterating : ً‫ال‬ٚ‫ أ‬


ً‫س ػجذذش اٌؼٕبصذذش داخذذ‬ٚ‫ّىٕٕذذب اٌّذذش‬١‫ ف‬arrays ‫فبد‬ٛ‫ػجذذبسح ػذذٓ ِصذذف‬Tuple ‫ػذذخ‬ّٛ‫ٔظذ ًذشا ْ اٌّغ‬
. for ‫ ِضً ؽٍمخ‬Looping ‫خ‬٠‫ػخ ثبعزخذاَ اٌؾٍمخ اٌزىشاس‬ّٛ‫اٌّغ‬
# Iterating through a Dictionary
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])

output
1
9
25
49
81

‫ط‬ِٛ‫ داخً لب‬ٛ‫خ اخزجبس ػع‬١ٍّ‫ ػ‬: dictionary Membership Test : ً ‫ب‬١ٔ‫ صب‬
‫دح‬ٛ‫عذ‬ِٛ key ‫ّذخ ِفزذبػ‬١‫ط ٌؼًّ اخزجذبس ِذب ئرا وبٔذذ ل‬ِٛ‫خ داخً اٌمب‬٠ٛ‫ّىٓ اعزخذاَ ِؼبِالد اٌؼع‬٠
: ‫ػخ ّ٘ب‬ّٛ‫ٓ الخزجبس ػٕصش داخً ِغ‬١‫ٕ٘بن وٍّز‬ٚ ‫ أَ ال ؟‬dictionary ‫ط‬ِٛ‫داخً اٌمب‬
. ‫ط‬ِٛ‫ اٌمب‬ٟ‫ٓ ف‬١‫ ِؼ‬key ‫د‬ٛ‫ع‬ٚ ِٓ ‫خ رغزخذَ ٌٍزؾمك‬١‫ وٍّخ أعبع‬: in keyword 
.‫ط‬ِٛ‫ اٌمب‬ٟ‫ٓ ف‬١‫ ِؼ‬key ‫د‬ٛ‫ع‬ٚ َ‫خ رغزخذَ ٌٍزؾمك ِٓ ػذ‬١‫ وٍّخ أعبع‬: not in keyword 
# Membership Test for Dictionary Keys
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(1 in squares) # Output: True
print(2 not in squares) # Output: True

# membership tests for key only not value


print(49 in squares) # Output: False
# Membership Test for Dictionary Keys
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(1 in squares) # Output: True
print(2 not in squares) # Output: True

# membership tests for key only not value


print(49 in squares) # Output: False

output
True
True
False
True
True
False
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 21
ْٛ‫ض‬٠‫ط داخً ٌغخ اٌجب‬ِٛ‫اي اٌمب‬ٚ‫ د‬: Python dictionary methods
: ُ٘ dictionary ‫ط‬ِٛ‫ رزؼبًِ ِغ اٌمب‬ٟ‫اي اٌز‬ٚ‫ػخ ِٓ اٌذ‬ّٛ‫ٕ٘بن ِغ‬
. )ٓ‫ط (اٌىبئ‬ِٛ‫ي ػٕبصش اٌمب‬ٛ‫ي ؛ ٌّؼشفخ غ‬ٛ‫ داٌخ اٌط‬: len() function 
.‫ط‬ِٛ‫ اٌمب‬ٟ‫ذح ف‬٠‫ ِشرجخ عذ‬keys ‫ؼ‬١‫َ ثبسعبع لبئّخ ِفبر‬ٛ‫ت؛ رم‬١‫ داٌخ اٌزشر‬: sorted() function 

# Dictionary Built-in Functions


squares = {0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81}

# Output: 6
print(len(squares))

output
6
[0, 1, 3, 5, 7, 9]

www.programiz.com ‫ ٘زا اٌشاثػ‬ٍٝ‫ّىٕه اٌعغػ ػ‬٠ ‫اي‬ٚ‫ذ ِٓ اٌذ‬٠‫ اٌّض‬ٍٝ‫ ٌٍزؼشف ػ‬

ْٛ‫ض‬٠‫ط داخً ٌغخ اٌجب‬ِٛ‫ُ اٌمب‬ٙ‫ ف‬: Python Dictionary Comprehension


‫ط لبثذً ٌٍزىذشاس‬ِٛ‫ذ ِٓ لب‬٠‫ط عذ‬ِٛ‫ِخزصشح إلٔشبت لب‬ٚ ‫مخ‬١ٔ‫مخ أ‬٠‫ غش‬dictionary ‫ط‬ِٛ‫ُ اٌمب‬ٙ‫ؼزجش ف‬٠
)key: value( ‫ذذش‬١‫ط ِذذٓ اٌزؼج‬ٚ‫ ِذذٓ ص‬Dictionary Comprehension ْٛ‫زىذذ‬٠ ٚ Python ٟ‫فذذ‬
.}{ ‫اط اٌّزؼشعخ‬ٛ‫ داخً ا ل‬for ‫ػب ثؼجبسح‬
ً ٛ‫ِزج‬

: dictionary = {key: value for vars in iterable}

. value ً‫ّض‬٠ٚ ُ‫ِشثغ اٌشل‬ٚ key ً‫ّض‬٠ٚ ُ‫ٓ ّ٘ب اٌشل‬١‫ع‬ٚ‫ط ػجبسح ػٓ ص‬ِٛ‫ ِضبي ٌؼًّ لب‬
# Dictionary Comprehension
squares = {x: x*x for x in range(6)}
print(squares)

#This code is equivalent to


squares = {}
for x in range(6):
squares[x] = x*x
print(squares)

output
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 24
. ‫اٌّضبي اٌغبثك‬ٚ Dictionary Comprehension ‫ٓ ثٕبت عٍّخ‬١‫ؼ ِمبسٔخ ث‬١‫ظ‬ٛ‫سح ٌز‬ٛ‫ ص‬

.‫ط آخش‬ِٛ‫بٔبد ِٓ لب‬١‫ط ثبعزخذاَ ث‬ِٛ‫ُ اٌمب‬ٙ‫ّىٕٕب اعزخذاَ ف‬٠ ١‫ الؽع و‬
#item price in dollars
old_price = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5}

dollar_to_pound = 0.76
new_price = {item: value*dollar_to_pound for (item, value) in old_price.items()}
print(new_price)

output
{'milk': 0.7752, 'coffee': 1.9, 'bread': 1.9}

ُٙ‫ اعزخذاَ ف‬.‫بد‬ٙ١ٕ‫ اٌغ‬ٌٝ‫ب ئ‬ٍٙ٠ٛ‫لّٕب ثزؾ‬ٚ ‫الس‬ٚ‫ إٔٔب اعزشعؼٕب أعؼبس ا صٕبف ثبٌذ‬ٜ‫ّىٕٕب أْ ٔش‬٠ 
.‫ش‬١‫ألصش ثىض‬ٚ ‫ّخ أثغػ‬ٌّٙ‫غؼً ٘زٖ ا‬٠ ‫ط‬ِٛ‫اٌمب‬

: ٟ‫ر‬٢‫ٗ وب‬١ٌ‫ ئ‬condition ‫ك اعزخذاَ ششغ‬٠‫ط ػٓ غش‬ِٛ‫ُ اٌمب‬ٙ‫ ف‬


#If Conditional Dictionary Comprehension
original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}

even_dict = {k: v for (k, v) in original_dict.items() if v % 2 == 0}


print(even_dict)

output
{'jack': 38, 'michael': 48}

.‫ط‬ِٛ‫ُ اٌمب‬ٙ‫ ف‬ٟ‫ ف‬if ‫خ فمػ ثغجت عٍّخ‬١‫ع‬ٚ‫ّخ اٌض‬١‫ رّذ ئظبفخ اٌؼٕبصش راد اٌم‬ٜ‫ وّب ٔش‬

: ٟ‫ر‬٢‫ٗ وب‬١ٌ‫ ئ‬condition ‫ك اعزخذاَ أوضش ِٓ ششغ‬٠‫ط ػٓ غش‬ِٛ‫ُ اٌمب‬ٙ‫ ف‬

#Multiple if Conditional Dictionary Comprehension


original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}

new_dict = {k: v for (k, v) in original_dict.items() if v % 2 != 0 if v < 40}


print(new_dict)

output
{'john': 33}

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 16
‫‪ ‬ف‪٘ ٟ‬زٖ اٌؾبٌخ رّذ ئظبفخ اٌؼٕبصش راد اٌم‪ّ١‬خ اٌفشد‪٠‬خ ا لً ِٓ ‪ 44‬فمػ ئٌ‪ ٝ‬اٌمبِ‪ٛ‬ط اٌغذ‪٠‬ذ ثغجت‬
‫عًّ ‪ if‬اٌّزؼذدح ف‪ ٟ‬ف‪ ُٙ‬اٌمبِ‪ٛ‬ط‪ .‬ئٔ‪ٙ‬ب رىبفئ ‪ٚ‬رؼًّ ؽ‪١‬ش ‪٠‬غت أْ ‪٠‬ى‪ ْٛ‬وال اٌششغ‪ ٓ١‬صؾ‪١‬ؾ‪.ٓ١‬‬

‫‪ ‬ف‪ ُٙ‬اٌمبِ‪ٛ‬ط ػٓ غش‪٠‬ك اعزخذاَ اٌغًّ اٌششغ‪١‬خ ‪ if-else‬وب‪٢‬ر‪: ٟ‬‬

‫}‪original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33‬‬

‫)'‪new_dict_1 = {k: ('old' if v > 40 else 'young‬‬


‫})(‪for (k, v) in original_dict.items‬‬
‫)‪print(new_dict_1‬‬

‫‪output‬‬
‫}'‪{'jack': 'young', 'michael': 'old', 'guido': 'old', 'john': 'young‬‬

‫‪ ‬ف‪٘ ٟ‬زٖ اٌؾبٌخ ‪٠‬زُ ئٔشبت لبِ‪ٛ‬ط عذ‪٠‬ذ ػجش ف‪ ُٙ‬اٌمبِ‪ٛ‬ط‪ٚ .‬اٌؼٕبصش اٌز‪ ٟ‬رجٍغ ل‪ّ١‬ز‪ٙ‬ب ‪ 44‬أ‪ ٚ‬أوضش ٌ‪ٙ‬ب‬
‫ل‪ّ١‬خ "‪ " old‬ف‪ ٟ‬ؽ‪ ٓ١‬أْ اٌؼٕبصش ا خش‪ٌٙ ٜ‬ب ل‪ّ١‬خ "‪." young‬‬

‫‪ ‬ف‪ ُٙ‬اٌمبِ‪ٛ‬ط ػٓ غش‪٠‬ك ف‪ ُٙ‬اٌم‪ٛ‬اِ‪١‬ظ اٌّزذاخٍخ ‪: Nested Dictionary Comprehension‬‬

‫{ = ‪dictionary‬‬
‫)‪k1: {k2: k1 * k2 for k2 in range(1, 6)} for k1 in range(2, 5‬‬
‫}‬
‫)‪print(dictionary‬‬

‫‪output‬‬
‫{‬
‫‪2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10},‬‬
‫‪3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15},‬‬
‫}‪4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20‬‬
‫}‬

‫‪ ‬ف‪٘ ٟ‬زٖ اٌؾبٌخ ‪٠‬زُ ئٔشبت لبِ‪ٛ‬ط عذ‪٠‬ذ ػجش ف‪ ُٙ‬اٌمبِ‪ٛ‬ط‪ٚ .‬اٌؼٕبصش اٌز‪ ٟ‬رجٍغ ل‪ّ١‬ز‪ٙ‬ب ‪ 44‬أ‪ ٚ‬أوضش ٌ‪ٙ‬ب‬
‫ل‪ّ١‬خ "‪ " old‬ف‪ ٟ‬ؽ‪ ٓ١‬أْ اٌؼٕبصش ا خش‪ٌٙ ٜ‬ب ل‪ّ١‬خ "‪." young‬‬

‫‪ ‬وّب رش‪ ٜ‬لّٕب ثجٕبت عذ‪ٚ‬ي اٌعشة ف‪ ٟ‬لبِ‪ٛ‬ط ِزذاخً ٌألسلبَ ِٓ ‪ 2‬ئٌ‪ .4 ٝ‬وٍّب رُ اعزخذاَ ف‪ُٙ‬‬
‫اٌمبِ‪ٛ‬ط اٌّزذاخً رجذأ ٌغخ ‪ Python‬أ‪ٚ‬الً ِٓ اٌؾٍمخ اٌخبسع‪١‬خ صُ رٕزمً ئٌ‪ ٝ‬اٌؾٍمخ اٌذاخٍ‪١‬خ‪.‬‬

‫‪ٕ٘ ‬بن ثشاِظ آخش‪ِ ٜ‬ىبفئخ ٌ‪ٙ‬زا إٌ‪ٛ‬ع اٌّضبٌ‪ ٓ١‬ا‪٢‬ر‪ّ٘ ٓ١١‬ب ِضبٌ‪ِ ٓ١‬ىبفئ‪ٌٍ ٓ١‬جشٔبِظ اٌغبثك ‪.‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪11‬‬
#the base program
dictionary = {
k1: {k2: k1 * k2 for k2 in range(1, 6)} for k1 in range(2, 5)
}
print(dictionary)

#the first program. So, the above code would be equivalent to:
dictionary = dict()
for k1 in range(2, 5):
dictionary[k1] = {k2: k1*k2 for k2 in range(1, 6)}
print(dictionary)

#the second program. It can further be unfolded:


dictionary = dict()
for k1 in range(2, 5):
dictionary[k1] = dict()
for k2 in range(1, 6):
dictionary[k1][k2] = k1*k2
print(dictionary)

output
{2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15}, 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20}}
{2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15}, 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20}}
{2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15}, 4: {1: 4, 2: 8, 3: 12, 4: 16, 5: 20}}

‫ظ‬١ِ‫ا‬ٛ‫ُ اٌم‬ٙ‫ضاد ف‬١ِّ : Advantages of Using Dictionary Comprehension 


ُٙ‫ اعزخذاَ ف‬ٞ‫إد‬٠ ْ‫ّىٓ أ‬٠ ‫شا‬١‫وض‬ً ‫ط‬ِٛ‫ئخ اٌمب‬١ٙ‫خ ر‬١ٍّ‫مًٍ ِٓ ػ‬٠ ‫ط‬ِٛ‫ُ اٌمب‬ٙ‫ فاْ ف‬ٜ‫وّب ٔش‬
.‫ ًّب‬١ٍ‫ إٌّطك ع‬ٍٝ‫د ِغ اٌؾفبظ ػ‬ٛ‫س اٌى‬ٛ‫ش عط‬١‫ رمص‬ٌٝ‫د اٌخب ثٕب ئ‬ٛ‫ اٌى‬ٟ‫ط ف‬ِٛ‫اٌمب‬

‫ط‬ِٛ‫ُ اٌمب‬ٙ‫ي اعزخذاَ ف‬ٛ‫شاد ؽ‬٠‫ رؾز‬: Warnings on Using Dictionary Comprehension 
‫غذ دائ ًّب‬١ٌ ‫ب‬ٙٔ‫ب ئال أ‬ٙ‫ً لشاتر‬ٙ‫غ‬٠ ‫مخ‬١ٔ‫خ أ‬١‫ّبد ثشِغ‬١ٍ‫ط سائغ ٌىزبثخ رؼ‬ِٛ‫ُ اٌمب‬ٙ‫ اٌشغُ ِٓ أْ ف‬ٍٝ‫ػ‬
:ٌٟ‫ اٌزب‬ٛ‫ إٌؾ‬ٍٝ‫ب ػ‬ِٙ‫ٓ أصٕبت اعزخذا‬٠‫ْ ؽزس‬ٛ‫غت أْ ٔى‬٠ ‫ش‬١‫ؼ ؽ‬١‫بس اٌصؾ‬١‫االخز‬
.‫ذ ِٓ اٌزاوشح‬٠‫ٍه اٌّض‬ٙ‫رغز‬ٚ ‫بًٔب عؼً اٌشفشح رؼًّ ثشىً أثطأ‬١‫ُ أؽ‬ٕٙ‫ّى‬٠ o
.‫د‬ٛ‫خ لشاتح اٌى‬١ٍ‫ً لبث‬١ٍ‫عب رم‬
ً ٠‫ُ أ‬ٕٙ‫ّى‬٠ o

ً ‫ ػذدًا‬ٚ‫ي أْ ٔالئُ ِٕطمًب صؼجًب أ‬ٚ‫غت أال ٔؾب‬٠ 


ً‫ُ فمػ ِٓ أعً عؼ‬ٍٙ‫ط ثذاخ‬ِٛ‫ُ اٌمب‬ٙ‫شا ِٓ اٌف‬١‫وج‬
.‫ ِضً اٌؾٍمبد‬ٜ‫بس ثذائً أخش‬١‫ ٘زٖ اٌؾبالد ِٓ ا فعً اخز‬ٟ‫ ف‬.‫د ِفشدًا‬ٛ‫اٌى‬

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 12
‫انذرس انتاسع ‪ :‬انقيى انًُطقية‬
‫)‪(boolean‬‬
‫‪٠‬ذذزُ ‪ّ٠‬ض‪ٍٙ١‬ذذب ‪ٚ‬أّ٘‪١‬ز‪ٙ‬ذذب‬ ‫‪ ‬عذذززؼٍُ فذذ‪٘ ٟ‬ذذزا اٌجشٔذذبِظ اٌزؼٍ‪ّ١‬ذذ‪ِ ٟ‬ذذب ٘ذذ‪ ٟ‬اٌمذذ‪ ُ١‬إٌّطم‪١‬ذذخ ‪ٚ Boolean‬و‪١‬ذ‬
‫داخً ٌغخ ‪. python‬‬

‫‪ : boolean‬اٌم‪ ُ١‬إٌّطم‪١‬خ‬
‫٘‪ ٝ‬ػجبسح ػٓ ل‪ّ١‬ز‪٠ ٓ١‬زُ اعزخذاِ‪ٌ ُٙ‬ج‪١‬بْ ؽبٌخ اٌغٍّخ أ ٌزم‪١‬ذ‪ ُ١‬رؼج‪١‬ذش ثٕذب ًت ػٍذ‪ ٝ‬شذشغ ِؼذ‪ ٓ١‬داخذً ٌغذخ‬
‫اٌجب‪٠‬ض‪٠ٚ ْٛ‬زُ رّض‪ ً١‬اٌم‪ ُ١‬إٌّطم‪١‬خ ثأؽذ اٌم‪ّ١‬ز‪ ٓ١‬ا‪٢‬ر‪١‬ز‪: ٓ١‬‬
‫‪ : True ‬صؾ‪١‬ؼ ‪٠ٚ‬ؼجش ػٕ‪ٙ‬ب ف‪ ٟ‬اٌجشٔبِظ ثبٌشلُ ‪ِٚ 1‬ؼٕب٘ب رؾمك اٌششغ ‪.‬‬
‫‪ : False ‬خطأ ‪٠ٚ‬ؼجش ػٕ‪ٙ‬ب ف‪ ٟ‬اٌجشٔبِظ ثبٌشلُ ‪ِٚ 0‬ؼٕب٘ب ػذَ رؾمك اٌششغ ‪.‬‬

‫غبٌجًب ِب رؾزبط ف‪ ٟ‬اٌجشِغذخ ئٌذ‪ِ ٝ‬ؼشفذخ ِذب ئرا وذبْ اٌزؼج‪١‬ذش ‪ expresion‬رؼج‪١‬ذش صذؾ‪١‬ؼ أَ رؼج‪١‬ذش خطذأ‪.‬‬
‫‪ّ٠‬ىٕه رم‪ ُ١١‬أ‪ ٞ‬رؼج‪١‬ش ف‪ ٟ‬ثب‪٠‬ض‪ٚ ْٛ‬اٌؾص‪ٛ‬ي ػٍ‪ ٝ‬ئؽذ‪ ٜ‬ئعبثز‪ True ٓ١‬أ‪. False ٚ‬‬

‫ٌإلػالْ ػٓ ل‪ّ١‬خ ِٕطم‪١‬خ ‪ Boolean‬أِش ع‪ِٚ ًٙ‬جبشش ‪٠‬زُ رٌه ػٓ غش‪٠‬ك رخص‪١‬ص ئؽذ‪ ٜ‬ل‪١ّ١‬زذ‪ٓ١‬‬
‫ٌٍّزغ‪١‬ش ئِب ٔخصص اٌم‪ّ١‬خ ‪ Tue‬أ‪ ٚ‬اٌم‪ّ١‬خ ‪ False‬وب‪٢‬ر‪: ٟ‬‬
‫‪a=True‬‬
‫‪b=False‬‬

‫)‪print(a‬‬
‫)‪print(b‬‬

‫‪output‬‬
‫‪True‬‬
‫‪False‬‬

‫ػٕذذ ِمبسٔذخ ل‪ّ١‬زذ‪٠ ٓ١‬ذزُ رم‪١‬ذ‪ ُ١‬اٌزؼج‪١‬ذش ‪ٚ‬رشعذغ ‪ Python‬اإلعبثذخ إٌّطم‪١‬ذخ ‪ Boolean‬أ‪ ٞ‬ثزشعذغ ئِذب‬
‫‪ True‬أ‪. False ٚ‬‬
‫)‪print(10 > 9‬‬
‫)‪print(10 == 9‬‬
‫)‪print(10 < 9‬‬

‫‪output‬‬
‫‪True‬‬
‫‪False‬‬
‫‪False‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪13‬‬
‫شاد‬١‫اٌّزغ‬ٚ ُ١‫ُ اٌم‬١١‫ رم‬: Evaluate Values and Variables
‫ذخ‬١‫ّذخ ِٕطم‬١‫ ل‬ٝ‫ذش ئٌذ‬١‫ ِزغ‬ٚ‫ّذخ أ‬١‫ ل‬ٞ‫ذً أ‬٠ٛ‫ْ ثزؾ‬ٛ‫ض‬٠‫َ ٌغخ اٌجب‬ٛ‫ش رم‬١‫ش ؽ‬١‫ ِزغ‬ٞ‫ أ‬ٚ‫ّخ أ‬١‫ ل‬ٞ‫ُ أ‬١١‫ّىٓ رم‬٠
ُ‫ذز‬٠ٚ standard truth testing procedure ٟ‫بعذ‬١‫مذخ اٌم‬١‫ ثبعزخذاَ ئعشات اخزجذبس اٌؾم‬Boolean
ً‫ اٌّمبثذ‬ٟ‫ذه فذ‬١‫رؼط‬ٚ ‫ّذخ‬١‫ ل‬ٞ‫ُ أ‬١١‫ش رغّؼ ٌه ٘زٖ اٌذاٌخ ثزم‬١‫ ؽ‬bool() ‫ك اعزخذاَ اٌذاٌخ‬٠‫رٌه ػٓ غش‬
. False ٚ‫ أ‬True

.)False ٚ‫ أ‬True( ‫خ‬١‫ّخ ِٕطم‬١‫ ل‬ٌٝ‫ّخ ئ‬١‫ً اٌم‬٠ٛ‫خ ؛ ٌزؾ‬١‫ُ إٌّطم‬١‫ داٌخ اٌم‬: bool() function 

: bool([value])

‫ش٘ب‬٠‫ّىٓ رّش‬٠ ٟ‫ُ اٌّؼبِالد اٌز‬١‫ ل‬: Parameter Values 


. ِٕٗ ‫ة اٌزؾمك‬ٍٛ‫ اٌىبئٓ اٌّط‬: value 

bool)( ‫ّذذخ فذذاْ اٌذاٌذذخ‬١‫ذذش ل‬٠‫ ؽبٌذذخ ػذذذَ رّش‬ٟ‫فذذ‬ٚ bool)( ٝ‫ّذذخ ئٌذذ‬١‫ذذش ل‬٠‫ رّش‬ٞ‫س‬ٚ‫ظ ِذذٓ اٌعذذش‬١‫ٌذذ‬
. False ‫ّخ‬١‫عزشعغ اٌم‬

#Evaluate values
print(bool("Hello"))
print(bool(15))

#Evaluate variables
x = "Hello"
y = 15
print(bool(x))
print(bool(y))

output
True
True
True
True

True ٟ‫ رؼط‬ٟ‫ُ اٌز‬١‫ اٌم‬: True Values 


: ً‫ ِض‬ٜٛ‫ع ِٓ اٌّؾز‬ٛٔ ٍٝ‫ ػ‬ٞٛ‫ ئرا وبٔذ رؾز‬True ٌٝ‫جًب ئ‬٠‫ّخ رمش‬١‫ ل‬ٞ‫ُ أ‬١١‫زُ رم‬٠
. empty strings ‫ ِبػذا اٌغالعً اٌفبسغخ‬True ْٛ‫ رى‬strings ً‫ اٌغالع‬o
. 0 ‫ّخ‬١‫ ِبػذا اٌم‬True ْٛ‫ى‬٠ numbers َ‫ ا سلب‬o
‫ ثبعزضٕبت‬True ْٛ‫ رى‬Dictionary ٚ set ٚ tuple ٚ list ً‫ ِض‬sequences ‫ اٌّزغٍغالد‬o
. empty object ‫اٌىبئٕبد اٌفبسغخ‬
. True ‫ّخ‬١‫ اٌم‬ٚ‫ أ‬1 ‫ّخ‬١‫ اٌم‬o
The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 14
False ٟ‫ رؼط‬ٟ‫ُ اٌز‬١‫ اٌم‬: False Values 
: empty values ‫ُ اٌفبسغخ‬١‫ ثبعزضٕبت اٌم‬False ٌٝ‫ب ئ‬ّٙ١١‫زُ رم‬٠ ‫شح‬١‫ُ وض‬١‫عذ ل‬ٛ‫الغ ال ر‬ٌٛ‫ ا‬ٟ‫ف‬
. ''' ''' ٚ‫ """ """ أ‬ٚ‫ ' ' أ‬ٚ‫ اٌغالعً اٌفبسغخ ِضً " " أ‬o
. 0j ٚ‫ أ‬0.0 ٚ‫ أ‬0 ً‫ ا سلبَ اٌفبسغخ ِض‬o
. } { ٚ ] [ ٚ ) ( : ً‫ اٌّزغٍغالد اٌفبسغخ ِض‬o
. False ‫ّخ‬١‫اٌم‬ٚ None ‫ّخ‬١‫ اٌم‬o
. False ٚ‫ أ‬0 ‫ رُشعغ‬ٟ‫__ اٌز‬len__() ٚ‫ __ أ‬bool)(__ ٍٝ‫ ػ‬ٞٛ‫ رؾز‬ٟ‫ وبئٕبد اٌفئبد اٌز‬o
test = []
print(test,'is',bool(test))

test = [0]
print(test,'is',bool(test))

test = 0.0
print(test,'is',bool(test))

test = None
print(test,'is',bool(test))

test = True
print(test,'is',bool(test))

test = 'Easy string'


print(test,'is',bool(test))

output
[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

‫ْ ِٓ فئخ راد‬ٛ‫ه وبئٓ ِى‬٠‫رٌه ئرا وبْ ٌذ‬ٚ False ٌٝ‫ ٘زٖ اٌؾبٌخ ئ‬ٟ‫ وبئٓ آخش ف‬ٚ‫ّخ أ‬١‫ُ ل‬١١‫زُ رم‬٠ 
. False ٚ‫ أ‬0 ‫ رُشعغ‬ٚ‫__ أ‬len__() ‫ داٌخ‬ٚ‫ __ أ‬bool)(__ ‫داٌخ‬

class myclass():
def __len__(self):
return 0

myobj = myclass()
print(bool(myobj))

output
False

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 15
‫خ‬١‫ّخ ِٕطم‬١‫ رُشعغ ل‬ٟ‫اي اٌز‬ٚ‫ اٌذ‬: Functions can Return a Boolean
‫خ‬١‫ّخ ِٕطم‬١‫ رُشعغ ل‬ٟ‫ اٌز‬functions ‫اي‬ٚ‫ّىٓ ئٔشبت د‬٠

# create functions that returns a Boolean Value:


def myFunction() :
return True

print(myFunction())

# execute code based on the Boolean answer of a function:


def myFunction() :
return True

if myFunction():
print("YES!")
else:
print("NO!")

output
True
YES!

Boolean ‫ذخ‬١‫ّذخ ِٕطم‬١‫ رُشعذغ ل‬ٟ‫اي اٌ ُّعذّٕخ اٌزذ‬ٚ‫ذ ِٓ اٌذ‬٠‫ اٌؼذ‬ٍٝ‫عب ػ‬


ً ٠‫ أ‬Python ‫ ٌغخ‬ٞٛ‫ رؾز‬
.ٓ١‫بٔبد ِؼ‬١‫ع ث‬ٛٔ ِٓ ٓ‫ذ ِب ئرا وبْ اٌىبئ‬٠‫ب ٌزؾذ‬ِٙ‫ّىٓ اعزخذا‬٠ ٟ‫اٌز‬ٚ isinstance() ‫ِضً اٌذاٌخ‬

x = 200
print(isinstance(x, int))

output
True

The Way To Python 4 … Data types …. ‫ الطريق إلى لغة البايثون‬..… Author , Eng/ Mahmoud Soliman 16
‫انخاتًة‬

‫ئٌذذ‪ٕ٘ ٝ‬ذذب ٔىذذ‪ ْٛ‬لذذذ أز‪ٕ١ٙ‬ذذب ِذذٓ اٌغذذضت اٌشاثذذغ ‪٘ٚ‬ذذ‪ِ ٛ‬ذذب اخذذزص ثذاسعذذخ أٔذذ‪ٛ‬اع اٌج‪١‬بٔذذبد اٌزذذ‪ ٟ‬رذذذػّ‪ٙ‬ب ٌغذذخ‬
‫اٌجذب‪٠‬ض‪ٚ ْٛ‬اٌّؼذش‪ٚ‬ف ثبعذُ ‪ٚ Python data types‬فذ‪ ٟ‬اٌذذس‪ٚ‬ط اٌمبدِذخ عذ‪ٛ‬ف ٔزؼذشف ػٍذ‪ ٝ‬و‪١‬ف‪١‬ذخ‬
‫اٌزؼبًِ ِغ اٌٍّفبد ‪ files‬داخً ٌغخ اٌجب‪٠‬ض‪ٚ ْٛ‬اٌّؼش‪ٚ‬فخ ثبعُ ‪. Python File I/O‬‬

‫ِذذذذذذٓ ثذذذذذذبة ا ِبٔذذذذذذخ اٌؼٍّ‪١‬ذذذذذذخ ف‪ٙ‬ذذذذذذزا اٌىزذذذذذذبة ٘ذذذذذذ‪ ٛ‬ؽصذذذذذذ‪ٍ١‬خ ‪ٚ‬رغّ‪١‬ؼذذذذذذخ ِؼٍ‪ِٛ‬ذذذذذذبد ِذذذذذذٓ ِ‪ٛ‬لذذذذذذغ‬
‫‪ٚ /https://www.programiz.com‬رُ عشد وذً دس‪ٚ‬ط ٘ذزٖ اٌغٍغذٍخ ثذٕفظ رغٍغذً ٘ذزا اٌّ‪ٛ‬لذغ ‪.‬‬
‫وّب االعزؼبٔخ ثّ‪ٛ‬لغ ‪. https://www.w3schools.com‬‬

‫٘ذذذزا اٌىزذذذبة ٘ذذذ‪ ٛ‬صذذذذلخ عبس‪٠‬ذذذخ ػذذذٓ ٔفغذذذ‪ٚ ٟ‬أ٘ذذذً ث‪١‬زذذذ‪ٚ ٟ‬أؽجذذذبث‪ٚ ٟ‬أعذذذأي أْ ‪٠‬زمجذذذً ٘ذذذزا اٌؼّذذذً‬
‫‪٠ٚ‬غٍؼذذذٗ خبٌصذذذب ً ٌ‪ٛ‬ع‪ٙ‬ذذذٗ اٌىذذذش‪ٚ . ُ٠‬وّذذذب ‪ٚ‬ظذذذؼزٗ ثذذذ‪ ٓ١‬أ‪٠‬ذذذذوُ ٌزٕذذذبٌ‪ٛ‬ا ِٕذذذٗ ػٍّذذذب ً ‪ٌٚ‬ذذذ‪ ٛ‬وذذذبْ ‪٠‬غذذذ‪١‬شا ً‬
‫ف‪ّ١‬ىذذذٕىُ أ‪٠‬عذذذب ً ٔشذذذشٖ ػٍذذذ‪ ٝ‬أ‪ِٛ ٞ‬لذذذغ ‪ٚ‬رؼذذذذ‪ِ ً٠‬ؾزذذذ‪ٛ‬اٖ ثّذذذب ‪٠‬خذذذذَ إٌذذذبط ‪ٚ‬االلزجذذذبط ِٕذذذٗ أ‪٠‬عذذذب ً وّذذذب‬
‫رشبت‪ ْٚ‬ثذ‪ ْٚ‬اعزئزاْ‪.‬‬

‫‪٠ ِٓٚ‬شغت ف‪ٔ ٟ‬غخخ ‪٘ ِٓ Word‬زا اٌىزبة ف‪ّ١‬ىٕٗ أْ ‪٠‬شاعٍٕ‪ ٝ‬ػٍ‪ ٝ‬اإل‪١ّ٠‬الد ‪.‬‬

‫سؽذذذُ سعذذذالً أ٘ذذذذ‪ٌ ٜ‬ذذذ‪ ٟ‬ػ‪١‬ذذذ‪ٛ‬ث‪ ٟ‬أسعذذذ‪ِ ٛ‬ذذذٓ وذذذً شذذذخص ‪٠‬مذذذشأ ٘ذذذزا اٌىزذذذبة ئرا ‪ٚ‬لذذذ ػٕذذذذ خطذذذأ‬
‫ػٍّذذذ‪ ٝ‬أ‪ ٚ‬خطذذذأ ئِالئذذذ‪ ٟ‬أ‪ِ ٚ‬ؼٍ‪ِٛ‬ذذذخ لذذذذ ‪٠‬غذذذبت ف‪ّٙٙ‬ذذذب أْ ‪٠‬شاعذذذٍٕ‪ ٟ‬ػٍذذذ‪ ٝ‬اإل‪ّ٠‬ذذذ‪١‬الد ا‪٢‬ر‪١‬ذذذخ ٌىذذذ‪ ٝ‬ألذذذ‪َٛ‬‬
‫ثاصالؽٗ ف‪ ٟ‬اٌطجؼبد ا‪٢‬خش‪ ٜ‬وّب أسع‪ ٛ‬أال رٕغ‪ٔٛ‬ب ِٓ صبٌؼ دػ‪ٛ‬ارىُ ‪.‬‬

‫‪E-mail : mmasoliman19962020@gmail.com‬‬
‫‪E-FaceBook : Mahmoud Soliman‬‬
‫‪Youtube : https://www.youtube.com/channel/UCfZvWjnYnfxI55v4LNhBekA‬‬

‫نتحًيم جًيع انكتب انخاصة بُا يًكٍ يتابعة قُاتي عهى انتيهيجزاو يٍ خالل هذا انزابط ‪:‬‬
‫‪Telegram Channel : https://t.me/joinchat/-aC4Ps0pna5lY2M0‬‬

‫نتحًيم جًيع انكتب انخاصة بُا يًكٍ يتابعة صفحتي عهى انفيس بوك يٍ خالل هذا انزابط ‪:‬‬
‫‪Public Page: Help-me-page‬‬

‫‪Mahmoud Soliman‬‬

‫‪The Way To Python 4 … Data types ….‬‬ ‫‪ ..… Author , Eng/ Mahmoud Soliman‬الطريق إلى لغة البايثون‬ ‫‪12‬‬
The Way To Python 4 … Data types …. ‫ الطريق إلى برمجة بلغة البايثون‬..… Author , Eng/ Mahmoud 11

You might also like