Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
402 views

Byte of Python Arabic

لغة بايثون

Uploaded by

makkawi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
402 views

Byte of Python Arabic

لغة بايثون

Uploaded by

makkawi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

2008

Byte of Python
‫ة
 ن‬
Swaroop C H


‫ أف‬:
Kspersky0
www.linuxac.org
‫ه ﻝ ﻝ اﻝ‬
‫آب ‪Byte Of Python‬‬

‫ ‪Swaroop C H :‬‬


اب‪www.byteofpython.info‬‬

‫اار ر
 ‪Version 1.20‬‬

‫ ق ا ‪Copyright © 2003-2005 Swaroop C H‬‬

‫ا
ـ‬

‫  ا ت‬

‫  ‪ ! -‬ه"ا اب ‪ -‬درس ری& ‪  -‬اب ‪ -‬ا!


ا)!( ‪ -‬ﺵوط ا‪ – -.‬ا
اح ‪ -‬أ ر ی‪23‬‬
‫ا‪56 4‬‬

‫** 
‬
‫‪9‬ی‪ 8‬ن ه( واة <; ا<>ت ا<< ا( ی! ا‪A‬د@ء أﻥ‪ 9 !3 5‬آ‪ : C‬ا‪ FGH‬وا ة‪.‬إﻥ‪.> 5‬ﺝة‬
‫<!‪ LH‬و<!‪ )  N<@ 6M‬اء ‪،‬وا‪  P‬ا‪P‬ه ا!‪ Q‬ا‪H‬ﻥ ‪. R‬‬
‫ی‪5‬ف اب ا‪ <Q (6 ;@G N‬ه"‪ T‬ا<> اا‪ UQL‬ویی; آ‪ 4‬إﻥ‪3‬ز ا‪ P‬ر ‪ @G9‬و‪ – 2Q W C9‬و‪N6‬‬
‫ا ا
' ه ‪ ) Z (8 Y6‬و آ<; ا‪.3 H‬‬

‫**  ها ا ب؟‬

‫ه"ا اب ‪ 98!9‬د‪ >< (!<Q C‬ا‪9 3 H‬ی‪ 8‬ن‪ .‬وه( ‪5G‬ف أ)) ا!‪ . LH‬وه( ‪4‬ة <!‪ 3 H‬ذوي‬
‫ا]‪H‬ة آ";‪ ،‬وا‪5‬ف ذ; @!  ه أﻥ‪ U‬آ‪ @ U6Q ;<@  C‬أﺝ‪^5‬ة ا!‪  H‬ه آ‪ _4 4‬ا!<‪4‬ت‬
‫ا` ﺙ ی!; أن ‪9 <Q‬ی‪ 8‬ن ه"ا اب وإاذا آن ی; ‪H.‬ة ‪ @ HG‬ا‪ ، 3 H‬ی!; أی‪ b‬ان ‪<Q‬‬
‫‪9‬ی‪ 8‬ن ه"ا اب‬

‫إذا آ‪H. 2 c‬ة ‪ G6 ،3 H9 HG‬ن ‪9 !5‬وﺝ‪ U‬ا‪d.A‬ف ‪9 9‬ی‪ 8‬ن و> ا‪ 3 H‬ا!‪ <b4‬ی;‬
‫ أ‪ c‬ا‪ b‬ء @<‪ N‬ا‪ 8‬ه"‪ T‬ا‪6d.A‬ت ‪.‬‬
‫@<‪ N‬ا‪ W‬ذ; ( ‪9 ، eG9 UH‬ی‪ 8‬ن @!
ی‪ ) 2‬ف `‪ > YH‬ا‪ 3 H‬ا!‪ <b4‬ی; ☺☺ !!‬

‫**درس ﺕر!!‬

‫‪ (6‬أول ا‪9  P‬أت ‪9‬ی‪ 8‬ن @  ا‪ c3‬إ‪9 N‬ﻥ ‪H cH8 R‬ﻥ ‪"، (3‬ا ا)‪ cQg‬أن أﺝ‪ CQ‬ا‪d5) cH8‬‬
‫وآن @<( ا‪.A‬ر ‪9 9‬ی‪ 8‬ن و ‪9‬ل ‪ ،‬أ‪.Qt H 4<W‬‬
‫و
!‪@h9 c‬دة ا‪ (6 iMH‬ﺵ‪ H‬اﻥﻥ‪8@ N c‬ت ‪  N<@ 6`9‬ل إیی; إس رای! ﻥ ذ; ا‪5‬آ ا!‪C3H‬‬
‫‪،‬وا!‪ 5‬ر ی< ‪ @ 56‬آ أ‪9 cMH‬ی‪ 8‬ن ه( > ا‪ 3 H‬ا!‪ HHM‬ی‪.U‬‬
‫وآ"; اآ‪ c4‬أن أ‪ PyQt 4<W‬ﺝة ﺝا ‪!9‬رﻥ ‪
;" Perl-Qt‬رت أن ‪9‬ی‪ 8‬ن ه( ا<> ا] ‪(9‬‬
‫‪Q9‬ه ‪9‬أت ا‪ @ iMH‬آب ﺝ ‪9 > (6‬ی‪ 8‬ن‪ .‬و(  أﺝ أی ‪ ، !!5‬و
 وﺝت ‪ kQ9‬ا‪ 2‬ـ ‪O'Reilly‬‬
‫و‪ 5‬آﻥ‪ c‬إ  ‪9‬ه‪ m‬ا‪>< !8‬ی‪ ،‬أو ‪ UH‬إ‪  N‬آ‪  H‬ت أ
ب آ ﻥ‪ 5‬اﺝ ‪.‬‬
‫و‪ (9‬ا‪ c53‬إ‪ N‬ا ﺙ‪ nL‬ا( ﺝءت ‪9‬ی‪ 8‬ن‪ ،‬و ذ; آﻥ‪`] c‬ة ﺝا و>ة‪ ،‬و
 أ@‪6 (g‬ة ﺝة‬
‫و
 أ ( ا‪ i 5Q C Q‬آن ي ‪H.‬ة ‪ ،3 H9 HG‬و‪9 @ W 5‬ی‪ 8‬ن‪ ،‬و‪ ،<!   5‬و‪W‬‬
‫ ‪LH!< !Ld‬‬
‫‪ ) Q9‬أﺵ‪ 5‬أول ء ( ‪9‬ی‪ 8‬ن
!‪  .o cH89 c‬زی‪ Q‬ردهت ‪Red Hat 9 Linux ،‬‬
‫‪ ،‬وآ‪ c‬أ‪  2Q‬ل أه  ‪ ، 56‬وآ‪ c‬أزداد إﺙرة و‪36‬ة ‪g.‬ت ( ‪6‬ة آ‪ 9‬دة @ ‪9‬ی‪ 8‬ن‬
‫و
 ‪9‬أت ا‪ C<9 9‬ا`‪M4‬ت‪،‬و‪) 5‬ی‪ Q‬أ‪ cMH‬ﺙ‪d‬ﺙ ‪ F M4‬ی< ‪Q9،‬ه ‪ cMH‬ﺝدا ‪L6 C!@ (6‬ة‬
‫أآ‪ N<@ H‬ﺵ‪ C‬آب‬
‫و‪ Q9‬ا‪Q‬ی إ@دة ا‪9‬ت‪ ،‬أ‪ < (6 YH‬آ ﻥ‪ U‬ﺝ‪4 Q‬ا ‪9 > <Q (6‬ی‪ 8‬ن ‪ ،‬وأﻥ أأ ‪ C‬أن ی ن ه"ا‬
‫اب ‪G‬ه! ( و‪ !3! M‬ا!`در ا!‪ 4‬‬

‫وه"ا اب ‪9‬أ آ!‪md‬ت ﺵ]` @ ‪9‬ی‪ 8‬ن و(  ز‪ c‬أﻥ‪ (6 U6 m‬ﻥ‪ p4‬ا
‪ - c‬ر‪ W‬أﻥ( ‪U6 c"9‬‬
‫ا‪ 8‬ا‪  - 53‬ن أآ‪ @ A H
8‬ا‪.q‬ی ‪.‬‬
‫و ‪d.‬ل اوح ا‪ !3! M‬ا!`ر ا!‪ 4‬ح‪ c< ،‬ا‪ 8‬ا‪
A‬ات ا‪H‬ءة‪ ،‬وا‪A‬ﻥدات وردود ‪CQ6‬‬
‫‪ .‬ااء ! )@ﻥ( آ‪8‬ا @<‪ GM N‬ه"ا اب ‪G!M‬‬

‫**ﺡ  ا ب‬

‫ه"ا اب زال


 ا‪ i C!Q‬إن ا‪ 8‬ا‪ `4‬ل > ‪!)9‬ار و‪ ، GM‬و ذ; ‪h6‬ن اب
 ﻥ‪Rb‬‬
‫آ‪8‬ا‪ ،‬و) ن ‪QG‬ا ‪9 <Q‬ی‪ 8‬ن ‪  5G9‬ه"ا اب‪.‬‬
‫ ‪ ;<b6‬أ‪H.‬ﻥ( إن وﺝت أي ﺝ^ء ‪ (6‬ه"ا اب ‪ YM W‬أو ‪ 54 W‬م‬
‫أآ‪ 8‬ا‪ `4‬ل ‪ wxpython Twisted,:C8 ، <HG eg. 5‬ور‪Boa Constructor N !9‬‬

‫** ا " ا ! ‬
‫ا!
ا)!( ‪"5‬ا اب ه ‪http://www.byteofpython.info‬‬
‫و ‪d.‬ل ا!
ی!;
اء اب آ ‪H C9 d‬ﺵ أو ^ی‪ .o C‬إار <ب‪ ،‬وآ"; إر)ل ا!‪md‬ت‬
‫(‬

‫**(!وط ا !‪#$%‬‬

‫‪.‬ه"ا اب ‪ !9 -.‬ﺝ‪ 2‬ر‪ `.‬ا‪9‬اع ا‪ W  Q‬ا‪3‬ری ﺵ‪ UH‬ا!رآ‬


‫‪The Creative Commons Attribution- NonCommercial -ShareAlike License‬‬

‫أ)) ؛ ; ا‪M‬ی ‪ (6‬ﻥ‪ &G‬و زی ‪ ،‬و@ض اب‪ !F ،‬أﻥ; ‪ 2G‬ا‪ .( Cb4‬ا د ه( أﻥ‪ A U‬ی!; ا)]ام‬
‫اب ‪WP‬اض ‪3‬ری ‪9‬ون إذن ( ‪.‬‬
‫; ا‪M‬ی ‪ (6‬ا‪Q‬ی‪ C‬وا‪H‬ء @<‪ N‬ه"ا ا‪ ،C!Q‬ﺵی‪ g‬أن  م ‪ d@ Z 9‬ت وا‪ C MZ‬ا>ات وإار ا‪C!Q‬‬
‫ا!‪Q‬ل ‪ cM‬ﻥ‪ p4‬ا‪ `.‬آ! ‪ (6‬ه"ا اب‬
‫اءة ا‪ -‬ا ‪ C‬ا‪ `.‬ا‪^9 
Cb6 Creative Commons <P‬یرة
ا‪`.‬‬
‫أو ‪ 56  5G‬ا‪ N ]G‬إﻥ‪ U‬ی ﺝ ‪
!9‬ﺵی‪ ;Mb e‬ح ا‪.`.‬‬
‫**ا!اح‬

‫ ‪ c"9‬ا‪ 8‬ا‪ CQ3 53‬ه"ا اب ‪4‬ا و ‪


N<@ !M‬ر ا ن‪ .‬و ‪ ،‬إذا وﺝت ‪ kQ9‬ا! اد‬
‫‪ G W‬او ‪ ،MM W‬أو ‪M9 FGH9‬ﺝ ا‪ ، GM N‬اﺝء أ‪ iM9 ،(><9‬أ! @!‪ C‬ا‪d‬ت‬
‫( @ ‪F‬ی‪<swaroop@byteofpython.info>. n‬‬ ‫ا!)‪ .H‬ی!; ا  ل إ ‪v‬‬

‫‪*$+ !$,- ./ 01‬‬

‫هك ‪F‬ین ‪H‬ء `! ا‪H‬ﻥ ‪ R‬أه ه ﺝ‪ gG9 5<Q‬ﺝا ‪ i‬إﻥ‪ Z 9 5‬ح و ‪ @ d9‬ب وأ  ا‪.P‬ى‬
‫‪ 546‬ا‪ A iM9 Q‬ی‪ 59 Yb‬أوﺝ‪ U‬ا` ر ‪( C. A. R. Hoare ) ..‬‬
‫ا‪3‬ح ‪ (6‬ا‪M‬ة ‪ A‬ی‪ U6 5‬ا"آء وا! ه‪9 H‬ر  ی‪ 56 5‬اآ^ وا!‪98‬ة ) ‪( C. W. Wendte‬‬
‫ا ‪ 02,‬ا‪4‬ول‬
‫ا
‬

‪ !L‬ا!‪ M‬یت‪:‬‬
‫   ‪Introduction................................................................................................................‬‬
‫ !^ات ‪9‬ی‪ 8‬ن‪Features of Python ............................................................................................‬‬
‫ <]‪Summary ..................................................................................................................-‬‬
‫!ذا ‪9 p‬ل ؟‪Why not Perl? ...............................................................................................‬‬
‫ ذا ی ل ا!‪ 3 H‬ن‪What Programmers Say ..............................................................................‬‬

‫
ــــ‬
‫* ‪9‬ی‪ 8‬ن ه( واة <; ا<>ت ا<< ا( ی! ا‪A‬د@ء أﻥ‪ gG9 5‬و
ی @<‪ )  N‬اء‪ ) .‬ن ‪G‬ورا‬
‫و ‪4‬ﺝ| ‪ (6‬آ ه( )‪ <5‬وآ^ ‪ C (6‬ا!< ‪!9‬رﻥ اآ‪ 2‬وأ))ت أی > ‪5<@ C!Q 3 9‬‬

‫ا
 ا ! ‪67 $‬ن ه ‪:‬‬

‫‪9‬ی‪ 8‬ن ه( واة >ت ا‪ <5) 3 H‬ا‪


،<Q‬ی‪ .‬و‪ M‬ي ‪49‬ءة @ ا!‪ G‬ى و‪ N<@ gG9‬هآ‪ C‬ا‪H‬ﻥت‬
‫و‪ C!Q Q6 5‬ا‪ 3 H‬ا‪. L‬‬
‫أﻥ

ا@ ‪9‬ی‪ 8‬ن ودی  ا‪ ،56 9‬ﺝ‪ H‬إ‪ N‬ﺝ‪G4 QHF 2‬ه‪9 CQ3 ،‬ی‪ 8‬ن > ‪3 H 8‬‬
‫ا‪9G‬ت و)@ ‪ g‬یا‪ (6 nHg‬ا‪Q‬ی ا!‪A3‬ت @<‪ mQ N‬ا!`ت ‪.‬‬
‫)ﻥ
} ‪ mQ‬ه"‪ T‬ا‪!G‬ت ‪^!9‬ی ا‪ (6 C`4‬ا‪ G‬ا(‪.‬‬

‫‪:‬ﺡ‪:9‬‬
‫‪ W" Guido van Rossum‬یو ‪6‬ن رو)م “  > ‪9‬ی‪ 8‬ن أ‪ 5<@ n<F‬ذ; ا‪ Q9 )A‬رؤی‪|5 Z@ U‬‬
‫اذا@ ا‪H‬ی‪g‬ﻥ ‪)" )9‬ك ﻥ( <‪ 9Q8‬ا‪Lg‬ة"" ‪ "Monty Python's Flying Circus‬و
ل إﻥ‪ C8 5‬ا‪9Q8‬‬
‫ا( ‪ C‬ا‪ M‬اﻥت >"ى @<‪F @ 5‬ی‪ 4` n‬ﺝ‪G‬ه ‪4A9‬ف  ‪ ، 5‬و)‪. 5M‬‬

‫ ‪=$‬ات < ﺏ‪6‬ن‪:‬‬

‫**ﺏ‪:?$1‬‬
‫‪9‬ی‪ 8‬ن > ‪ Q9P gG9‬ا‪M‬ود‪ .‬إن
اءة ‪9‬ﻥ ‪ R‬ﺝ ‪H‬ی‪ 8‬ن ید ی‪
UH‬اء ة ا<> اﻥ<^ی @<‪ N‬ا‪ W‬أﻥ‪5‬‬
‫إﻥ‪^<3‬ی ر !‬
‫‪ QHF‬ه"ا ا‪ )A‬ا!‪QG‬ر ‪H‬ی‪ 8‬ن ه وا أ@‪ m‬أ)ار
‪ .5‬إﻥ‪ U‬ی‪ ; Y‬اآ^ @<‪ C N‬ا!< أآ‪ 8‬‬
‫ا<> ﻥ‪. 5G4‬‬

‫** *‪ -‬ا ‪:@-A‬‬


‫آ! )ون‪9 ،‬ی‪ 8‬ن )‪>< <5‬ی ‪H‬أ ‪ <Q (6 59‬ا‪9 .3 H‬ی‪ 8‬ن ‪ M‬ي اآ‪ <5) 2‬و@دی‪ ،‬آ! )‪ nH‬ذآ‪. T‬‬

‫**ﺡ!ة و‪,‬ﺡ ا ‪ 2‬ر‪:‬‬


‫‪9‬ی‪ 8‬ن ه( ‪8‬ل !`‪ (Free/Libré and Open Source Soft-ware) FLOSS Y<g‬ا‪H‬ا ‪ R‬ا‪M‬ة وا!‪ 4‬‬
‫ا!`ر‪.‬‬
‫‪HQ9‬رات ‪ ،gG9‬ی!; ‪M9‬ی  زی ﻥ‪ &G‬ه"‪ T‬ا‪3 H‬ت‪ ،‬و
اءة ﺵ‪4‬ة ا!`ر‪ ،‬و  م ‪ kQH9‬ا>ات @<‪5‬‬
‫وا)]ام أﺝ^اء ‪3 9 (6 5‬ت ة ﺝیة‪ ،‬وأﻥ‪Q c‬ف أﻥ‪ U‬ی!; أن ‪ CQ4‬ه"‪ T‬ا‪P‬ﺵء‪.‬‬
‫ی م ‪ 54‬م `‪H N<@ FLOSS Y<g‬أ ا!‪ !3‬ا"ي یرك ‪ (6‬ا!‪.6Q‬‬
‫ه"ا وا أ)‪H‬ب آ ن ‪9‬ی‪ 8‬ن ﺝة ﺝا ‪P -‬ﻥ‪  
U‬إﻥؤه و‪d. !G C9 5GM‬ل ا!‪ !3‬ا"ي یی‬
‫‪ e6‬أن یى ‪9‬ی‪ 8‬ن أ‪.Cb6‬‬
:‫ى‬1 ‫ ا‬A$+‫ ر‬/!‫** < ﺏ‬
5 ]G‫ إدارة ا"اآة ا( ی‬C8 ‫ ى‬G!‫ د
 ا‬C49 nb‫ ا‬N‫ا إ‬9‫ج أ‬M A ،‫ ن‬8‫ی‬9 (6 R ‫ا‬H‫ ا‬2  @
.&‫ ا‬،;3 ‫ﻥ‬9

:  **
.‫ی ا!`ت‬Q‫( ا‬N<@ C!Q 5<Q‫!  )أي  ﺝ‬M > ‫ ن‬8‫ی‬9 CQ‫  ﺝ‬،‫  ا!`ر‬4!‫ ا‬R ‫ا‬H‫ ا‬QHg ‫ا‬m‫ﻥ‬
N<@ ‫ ذ; أي >ات‬2<g‫ ا!`ت دون أن ی‬T"‫ أي ه‬N<@ C!Q ‫ أن‬5!‫ ن ی‬8‫ی‬9 ><9 R ‫ا‬9 UQ`  C‫آ‬
.‫م‬m< . ‫ أي ا@!دیت‬23 ‫ی‬4‫ ا‬U6 !9 
‫ د‬c‫ إذا آ‬.‫ق‬dF‫ا‬
.
: ‫ت‬2D ‫ ا‬E‫ ه‬F-G ‫ن‬6‫ ام ﺏ‬I ‫ ا‬JD 
Linux, Windows, FreeBSD, Macintosh, Solaris,
OS/2, Amiga, AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX,
VMS, Psion, Acorn RISC OS, VxWorks, PlayStation, Sharp Zaurus,
windows ce pocketpc! (4‫  ا‬H!‫ ا‬N‫و‬

: ‫!ة‬1, < **
‫ ﺵ| اح‬2<g‫**وذ; ی‬
><‫ ا‬N‫إ‬C++ ‫ أو‬C .><‫ `ر ا‬5<‫ ی‬M ‫ أو ی‬C++ ‫ أو‬C C8 !‫ ﺝ‬/Q!3 > (6 ‫  ب‬R ‫ﻥ‬9
‫)]ام ا!ﺝ ]< ا]رات‬9 (binary code i.e. 0s and 1s) "‫ ا د‬L‫زك "ﺙ‬5‫ ﺝ‬5!<‫ا( ی‬
(6 ‫أ‬H‫ ا"اآة وی‬N‫ إ‬2<`‫ اص ا‬R ‫ﻥ‬H‫& ا‬G9 linker/loader ‫ ی م‬،R ‫ﻥ‬H‫ @ ><; ا‬.‫<!ت‬Q‫وا‬
. U<>

‫ﺵة آ د‬H R ‫ﻥ‬H‫ ا‬C>‫ ﺵ‬e6 .(L8‫ ا د ا‬N‫! إ‬3‫ا‬/!‫ اﺝ‬N‫ج إ‬M A ،‫ى‬.‫ ﻥ أ‬،‫ ن‬8‫ی‬9
<P‫ ا<> ا‬N‫ ﺙ یﺝ ه"ا إ‬bytecodes N!G‫ ی‬e)‫ و‬C‫ ﺵ‬N‫ة ا!`ر إ‬4‫ ل ﺵ‬M‫ ن ی‬8‫ی‬9 ‫ن‬h6 ،<.‫ دا‬.‫ا!`ر‬
n<9 Q ‫ @<; أن‬p U‫ إﻥ‬i ‫ ن‬8‫ی‬9 ‫ ا)]ام‬89 C5)P‫ ا‬CQ3‫ ی‬،
‫( ا ا‬6 ،‫ ه"ا‬C‫ آ‬.U<>‫ ﺙ ی‬،‫زك‬53
]‫ ن ا‬8‫ی‬9 R ‫ا‬9 CQ3‫ ی‬b‫ ا& وه"ا أی‬،&‫ ا‬،5<!M‫ و‬e9‫ت ا‬H M ‫ أو اآ‬،R ‫ﻥ‬H‫! ا‬3 ‫ﻥ‬
! C!Q‫ه ی‬Q9‫ و‬،.‫  أ‬H!‫ز آ‬5‫ ﺝ‬N<@ ;9 ‫ ن ا]ص‬8‫ی‬9 R ‫ﻥ‬9 &G‫د ﻥ‬3 ;!‫ ی‬iM9 ، !M 8‫; أآ‬9

Object Oriented K‫ ا ﺝ‬$D‫** < آ‬

object-/5‫ ا! ﺝ‬L‫ ا‬3 H‫ وآ"; ا‬procedure-oriented/5‫ ا! ﺝ‬L‫ اﺝا‬3 H‫ ن @ ا‬8‫ی‬9
‫ ر  ل‬M!‫ ی‬R ‫ﻥ‬H‫ن ا‬h6 procedure-oriented/5‫ ا! ﺝ‬L‫ اﺝا‬3 H‫( ا<>ت ا( @ ا‬46 .oriented
‫ن‬h6 ، L‫ ا‬3 H‫( >ت ا‬6‫ و‬.5 ‫ ی! إ@دة ا)]ا‬R ‫ا‬H‫ ا‬Qg
‫ ) ى‬cG (‫اﺝاءات أو اوال ا‬
‫ ن
ی ﺝا و‬8‫ی‬9 >‫ و‬.L† ‫ﻥت وا‬H‫ ا‬9 !6 !3 (‫ ا‬objects/‫ت‬L‫ ر  ل ا‬M!‫ ی‬R ‫ﻥ‬H‫ا‬
C8 ‫ة‬H‫<>ت آ‬9 5‫] @ رﻥ‬9‫ و‬،{Object-Oriented Programming} oop C!Q gGH ‫ی‬g9
. 6‫ أو ﺝ‬C++

Extensible ‫ اد‬: -‫**ﺏ‬


A  ‫ ارز‬. g‫ ا‬kQ9 ;‫ ﺝا أو ی أن ی ن ی‬Q‫ )ی‬C!Q ‫ ا د‬Qg
N‫( ﺝ ) إ‬6 c‫إذا آ‬
. ;9 ‫ ن ا]ص‬8‫ی‬9 R ‫ﻥ‬9 U ]G ‫ه‬Q9‫ و‬C++ ‫أو‬C ><9 ;3 ‫ﻥ‬9 ‫^ء‬3‫ ه"ا ا‬9‫ ی!; آ‬، 4 ‫ ن‬

Embeddable $ M- -‫**ﺏ‬


. ;3 ‫ﻥ‬9 ( ]G! 'scripting' ‫ء
رات ال‬g@ C/C++ R ‫ا‬9 !Z ‫ ن‬8‫ی‬9 !b ;!‫ی‬

Extensive Libraries A1 ‫ا‬/-N ‫ت ا‬7 ‫**ا‬


regular :56 !9 ‫دی‬Q‫ﺵء ا‬P‫ ]< ا‬C!@ N<@ ‫)@ك‬،‫ "آ‬.
‫( ا ا‬6 !]Z H )‫ ن ا‬8‫ی‬9 H
expressions, documentation generation, unit testing, threading, databases, web browsers,
CGI, ftp, email, XML, XML-RPC, HTML, WAV files, cryptography, GUI (graphical
.‫ ن‬8‫ی‬9 cH8‫! أی! ی‬L‫ ه"ا ح دا‬C‫ آ‬، ‫ "آ‬.‫م‬m‫ ا‬N<@ !Q (‫ﺵء ا‬P‫ه ا‬W‫ و‬user interfaces), Tk
.'Batteries Included' (6Z‫ ا‬M‫ریت ا‬g9).‫ ن‬8‫ی‬9 4G<6 (6 N!G‫وه"ا  ی‬

:C8 ‫ دة‬3‫ى @ ا‬.P‫ت ا! @ ا‬H!‫ی ا‬Q‫ت ا)؛  ﺝ ا‬H!< HG9 ‫ ذ;؛‬2‫ ﺝﻥ‬N‫إ‬
. 8‫ وا‬8‫ وا‬Python Imaging Library, Twisted, wxPython

‫ﺹ‬:%
R ‫ا‬9 9‫ آ‬CQ3 (‫داء وا!^ات ا‬P‫ ا‬G R‫ ^ی‬9 !3 M‫( ا‬6 5‫ إﻥ‬.  ‫ة و
ی‬8 > ‫ ن‬8‫ی‬9
.Q!‫  وا‬5G‫ ا‬C‫ آ‬9 !3 ‫ ن‬8‫ی‬9

‫!ل؟‬$‫ ﺏ‬Q$ ‫ ذا‬


.‫ <>ی‬HQ‫  ا!`ر ﺵ‬4 ‫ة‬G4 3 9 > ‫ى‬.P‫ ه( ا‬HQ ‫ل‬9 ،dQ6 ‫ف‬Q A c‫إذا آ‬
! ! ;G49 ‫ال‬G‫ @ ه"ا ا‬cH‫
 أﺝ‬c‫! آ‬9‫ ر‬،‫ل‬9 (6 H‫ آ‬R ‫ﻥ‬9 9‫ آ‬c‫ ; وو‬nH) ‫إذا‬
‫آت‬5‫ت وا‬9G‫ ا`>ة وا‬R ‫ا‬H‫( ا‬6 ‫ع‬H‫ وه ی‬،‫< @   ن >ة‬5) U3 ‫ا‬9 ‫ل‬9 ‫ن‬h6 ،‫ى‬.‫رة أ‬HQ9‫و‬
.C!Q‫ز ا‬3‫ﻥ‬
93
‫ث وا‬M‫ وأﻥ أ‬،H‫ أآ‬R ‫ا‬9 9‫( آ‬6 ‫ء‬H‫د ا‬3!9 ‫ ﺝ ة‬R ‫ا‬H‫ ا‬T"‫ ه‬YH`  ‫ أی ل؛ )@ن‬N<@‫و‬
! ‫( یه‬6 ‫ل‬9 ><9 ‫ة‬H‫ آ‬R ‫ا‬9 9‫آ‬
8‫( أآ‬9‫ و‬9‫( ا‬6 C5)‫ وأ‬، Z‫ و‬8‫ وأآ‬،C5)‫آ أ‬9 (‫ ن ه‬8‫ی‬9 N<@ R ‫ا‬H‫ن ا‬h6 ،‫ل‬9 ‫!رﻥ‬9‫و‬
.‫ و<`ﻥ‬54< <9

‫أ‬9‫! أ‬L‫ا‬6 ،3 ‫ﻥ‬9 cH‫ و( آ<! آ‬،@  ‫ ر‬P  ‫ أ))( ی‬C9 5 ‫)]ا‬9 ‫ل وأ
م‬9 ><9 23Q ‫أﻥ‬
.( HG9 ‫ ﺝا‬QHF cMH‫ أ‬5‫ إﻥ‬i ‫ ن؛‬8‫ی‬9 (6 4‫ا‬
.‫ك‬5‫( ا‬M‫ واة ﺝ‬5‫ار واة )و‬W N<@ 5‫ أﻥ‬Q‫ و‬،‫آت‬5‫ ا>ات وا‬H‫د آ‬Q ‫ل‬9 > cQb.
. ‫"ا‬59 n<Q ‫ت‬GM ‫ﺝاء أي‬h‫ ی‬c 
5‫و أﻥ‬H‫ ی‬A <H!‫ ا‬٦ ‫ل‬9 ‫^ن أن إارة‬M!‫و ا‬
the Comprehensive Perl ) CPAN ‫]! ـ‬b‫ ا‬H!‫ل ه( ا‬9 (6 59 Q‫  ﺝا وا( أﺵ‬5‫ا!^ة ا  ا‬
modules/‫ وات‬R‫ه ﺝ! ^ی‬، )A‫{وآ! ی ( ا‬H‫ ا‬N<@ ‫ل‬H C ‫رﺵ ا‬P‫( }ا‬Archive Network
‫) ب‬M‫ ا‬U9 ‫ي ﺵ(ء ی!; ام‬9 <!@ U9 ‫ ی!; ام‬-- n!Q‫ وا‬3M< ‫ا‬m‫ ﻥ‬CQ< C‫ "ه‬FGH9 ‫ل وه‬9
5  C!@ U‫ ن ه أﻥ‬8‫ی‬9 @ ! 8‫ل أآ‬9 ‫ت‬H CQ3 (‫ب ا‬H)P‫ أ ا‬. modules/‫ ا ات‬T"‫)]ام ه‬9
. comp.lang.python
(6 ‫ ن‬8‫ی‬9 N‫ل إ‬9 ^<‫ ! دی‬C‫ ﻥ‬C!@ ‫<( أ
ح‬Q‫ و‬.‫ ن‬8‫ی‬9 89 ‫ ل‬F‫ أ‬c

‫ ن‬8‫ی‬9 C8  ! 5!!` ‫د‬Q!‫ ا‬٦‫ل‬9 > C‫ آ‬C>9 ‫ ه( `!!  م‬Parrot virtual machine ‫آ";؛‬
. Tcl ‫ و‬PHP‫ و‬Ruby C8 ‫ى‬.P‫ة ا‬G4!‫وآ"; ا<>ت ا‬
،CHG!‫( ا‬6 ‫ ن‬8‫ی‬9 C.‫ل دا‬9 ‫ ا)]ام ﺝ! وات‬N<@ ‫!  ن
درا‬9‫ ; ه"ا أﻥ; ر‬HG9 ;‫ ذ‬UQ‫  ی‬
e6 <@ ‫ أی ل؛‬N<@ .‫ ن ا ی‬8‫ی‬9 > ‫ﺵاك‬A9 CPAN Q‫( ا‬6 H ‫ أ
ى‬C‫( آ‬6 Cb6P‫; ذ; ا‬M!) ‫و"ا‬
.‫ث‬M)  ‫ وﻥى‬m‫أن ﻥ‬

‫ن‬/!7 ‫*ذا 
ل ا‬
.‫ن‬6‫ ﺏ‬G { Eric S. Raymond} ESR ‫ل‬8 ‫آ أ‬5‫!ء ا‬m@ U ‫ أن أ  ی‬5!‫! ا‬9‫ر‬
b‫زار{ وه أی‬H‫ وا‬L‫' }ارا‬The Cathedral and the Bazaar' ‫  آب‬:Eric S. Raymond •
. 4!‫ ا!`در ا‬Y<g` Z‫ ا"ي و‬-]‫ا‬
3 H‫ ن ه( > ا‬8‫ی‬9 cMH‫  أ‬Python has become his favorite programming language/ " :‫
ل‬
.‫ ن‬8‫ی‬9 (6 (‫ ا‬g. N‫( أو‬6 ( (M‫ ا‬5<!‫ ا! ه( ا‬T"‫ ه‬HQ‫< ي " و‬b4!‫ا‬
"c++><9 4‫' و "ا‬Thinking in Java' '6‫<> ﺝ‬9 4‫ة 'ا‬5‫ ا‬2‫ وه  ا‬:Bruce Eckel •
(‫ ن ه‬8‫ی‬9 ‫!  ن‬9‫ ر‬U‫ إﻥ‬:‫ وی ل‬.‫ ن‬8‫ی‬9 ‫ إﻥﺝ‬8‫ أآ‬U<Q‫ هك >
 ﺝ‬p : ‫' ی ل‬Thinking in C++'
complete interview < ‫< ا‬9!‫ ا
أ ا‬.R H!< HG9 C5)‫ ر أ‬P‫ ا‬CQ‫ ﺝ‬N<@ ^‫ا<> ا ة ا( آ‬
.C4‫!^ی ا‬
Guido van ‫ )ﺵا ل‬C‫( ﺝ ﺝ‬6 iMH‫ و ی ﺝ دة ا‬Lisp >  U‫ﻥ‬9 ‫وف ﺝا‬Q ‫ ه‬: Peter Norvig •
( ;‫ ذ‬N‫ إ‬U‫ ﺵر‬Rossum
‫ل‬d.
‫( ا ا‬6 Y‫ ه"ا ا`ی‬nM‫ ی!; ا‬،Google ‫^أ‬3‫ ی‬A ‫! ﺝ^ءا‬L‫ دا‬c‫ ن آﻥ‬8‫ی‬9 ‫ إن‬:‫ی ل‬
()5 CH
56Q ‫< ب‬g!‫رف ا‬Q!‫! ا‬L
(6 ‫ ن‬8‫ی‬9 > cQZ‫ وا( و‬. Google Jobs M4 (6 m‫ا‬
.‫ت‬3 H‫ا‬
userlinux.‫ و وع‬opensource.org ‫ ل‬G)!‫ ه أ ا‬: Bruce Perens •
G6!‫ ن ا‬8‫ی‬9 ‫ب‬Z 
‫و‬. ‫دی‬Q QL9  @ p )
Q‫  زی‬C!Q ‫ هف‬Userlinux
.Userlinux CH
 @ ‫ ا( ) ن‬GL‫ ا‬3 H‫ > ا‬YH` (9‫ل ورو‬9 c<Q6 !<8

‫ﻥـ‬6 ‫ ا‬0‫ــ‬2, ‫ا‬


‫ــن‬6‫ ﺏ‬.$‫ـ‬2ْDَ‫ﺕ‬
‫ یت‬M!‫ﺝول ا‬
d]‫ ا‬- ‫] ( ویوز‬G! - -Linux/BSD ( ]G!
،FreeBSD C8 BSD ‫م‬m‫ أَو ﻥ‬،{‫ك ه‬ َ ‫ َر‬.‫ ا‬Z} ‫ورا أَو ﻥری; أَو‬6 C8 p Q‫  زی‬C
ُ !QGَ c‫إذا آ‬
. ; m‫ ﻥ‬N<@ H8 ‫ ن‬8‫ی‬9 ‫ أن ی ن‬H‫ا!ل آ‬
‫ أَو‬console C8 )‫ ا ل‬R ‫ﻥ‬9 Y6‫ ا‬،َp Q‫  زی‬N<@ ‫ @ك‬CQ49 ‫ ن ﺝ دا‬8‫ی‬9 ‫ر  إذا آن‬Hِ.A
 P‫ ه"ا ا‬C.‫( وأد‬Gnome terminal
$ python -V
Python 2.3.4

.;‫م ی‬m‫ إ@ادات ا‬N<@ ‫ ; ا@!دا‬HG9 <]‫ وذ; ی‬shell (6 ‫ا!ﺵ‬/iM!‫  ا‬d@ (‫ ه‬$
e6 ^ ‫"ا ا‬59 iM!‫ ا‬N‫ "; ) ف أﺵ إ‬$
.‫ ن‬8‫ی‬9 cH8 ‫ @<; أن‬U‫ﻥ‬h6 A‫ وإ‬،Td@‫  أ‬P‫( ا‬6 ‫وض‬Q ‫  ه‬C8 ِ]G‫< ت @ إارة ا‬Q!‫ ا‬kQ9 c‫إذا رأی‬

:T"‫ ه‬C8 )‫ ر‬N<@ c<` ‫ إذا‬،‫ أی ل‬N<@


$ python -V
bash: python: command not found

.C
َ !M U ‫ ﺝًا‬QHG!‫ وه"ا ا‬،‫ @ك‬H8 p ‫ ن‬8‫ی‬9 ‫ أن‬TQ ‫ ی ن‬M‫( <; ا‬6
;
َ ِ m‫ ﻥ‬N<@ ‫ ن‬8‫ی‬9 2`َْ ‫ین‬F ‫ك‬ َ َ ِْ@ ،ِM‫ ا‬T"‫( ه‬6.
p (6 yum C8 ،‫م‬m‫( ُء ا‬3َ (‫^م ا‬M‫ إدار ِة ا‬R
َ ‫ا‬9 ‫)]ام‬9 binary packages ْ2ّ‫• آ‬
‫م‬m‫( ﻥ‬6 pkg-add - ، p ِ  ‫ن‬H‫( دی‬6 apt-get - ،‫ ﻥراك‬p ِ  (6 urpmi - ،‫ورا‬6
.&‫ ا‬، FreeBSD
.ِ‫ی‬g‫ ا‬T"‫ْ!ل ه‬Qِْ)A c
ِ ‫ﻥﻥ‬9 A ً `ّ‫ج ا‬ ُ Mََ) :9‫ﺡ‬:ُ
.5H89 
‫زك و‬5‫ ﺝ‬N‫ إ‬5]G‫ ذ; اﻥ‬Q9‫ و‬.o ‫ ِ ْ ن‬binaries ‫ ^م‬C َ ّ!Mُ ْ‫ یُ!ُِْ; َأن‬،;‫ @ ذ‬d ً ‫ی‬9‫و‬
 ‫وا‬9 c‫ اﻥﻥ‬H‫ و) ف !ك ﺵ‬.UH8‫ و‬source code ‫ < د ا!`ري‬C‫ی‬H ‫ آ‬C!Q9 ‫ ن‬8‫ی‬9 2‫یُ!ِْ ُ َأنْ آ‬
. 2‫اآ‬

:ِ ‫ وز‬D‫م و‬9‫  ﻥ‬I1


( 2.3.4 : ]G‫ن ر
 ا‬
َ َ‫ ن ِ ْ ه"ا ا!
ِ )آ‬8‫ی‬H ِ]G‫ ﻥ‬.o Cّ!‫ و‬Python.org/download
‫^یرة‬9 

‫ >ت‬8‫!رﻥ أآ‬9 ‫ُ ﺝًا‬F >b ‫(  رة‬6 c‫ی‬9> 9.4 e6 5!3 ’<H‫ وه( ی‬.‫ ا<!ت‬T"‫ ه‬9‫ آ‬N
.‫| ا یوز‬9 N<@ H R ‫ا‬9 ‫ي‬ ّ ‫ أ‬C8 2‫ و اآ‬.‫ى‬.P‫ ا‬3 H‫ا‬
:K$7D‫ﺕ‬
ً `. ،;َ ‫ا‬4 ‫ت‬ ِ ‫ ا! ّﻥ‬T"‫ ف ی ن أ ه‬Any ] A ،6Z‫ي  ّﻥت إ‬ ّ ‫ أ‬cH8 ‫م‬Q9 ‫رَا‬. (gQَ  @
. IDLE
‫(  ر َة‬gQَ A T"‫ ه‬، Hg9 .‫!<( ویوز‬QG ِ ‫ ن‬8‫ی‬9 R ِ ‫ا‬9 C!M9 ‫ !
م‬% 70 (‫ن  ا‬ ّ ‫ة أ‬8!‫ ا‬M‫ا‬
. (Z‫ا‬6‫ ا‬C9 5ِ!m‫ أﻥ‬N<@ H8 ‫ ن‬8‫ی‬9 ُْ‫ن @ِْ َه‬ ُ َ) H‫ ی‬p (<!QG C ّ ُ‫ أن آ‬i َ< ‫آ‬
‫ وز‬D‫ ?! أوا! و‬+ ‫ن‬6‫ ل ﺏ‬A ‫ا‬
C9 PATH C‫ْ ا‬Z َ  ‫ج‬ ُ Mَ ;‫ﻥ‬h6 ،‫ أوا  ویوز‬g) ْ ِ ‫ ن‬8‫ی‬9 ‫ْ!ل‬Qِْ)‫ ا‬N<@ ‫ن
درا‬ َ َُ ‫إذا أردت أن‬
.YM
‫رة‬49 e>Z‫ ا‬،XP، 2003 ،2000 ‫  یوز‬HG9
.Control Panel -> System -> Advanced -> Environment Variables
" :6Zh9 
‫ و‬EDIT .‫' ﺙ ّ ا‬System Variables' G
(6 PATH Nّ!G!‫ ا!>ّ ِ ا‬N<@ e>Z‫ا‬
2)!‫ ا‬C‫ ا) ا‬C!Q)‫ ا‬Hg9 .‫ هك‬CQ49 ‫ ا! ب‬gG‫ی ا‬5‫( ﻥ‬6 ("" ‫ون أ
اس‬9) " C:\Python23
.;
C: \ Autoexec  ِ <!‫ ا‬N‫( إ‬
َ ‫ ا‬gG‫ف ا‬b‫ ی‬،ِ"6‫م ا ا‬m‫
 ِم ِ ْ ﻥ‬P‫& ا‬ ِ G<
.‫ َم‬m‫ ا‬C> @‫ون أ
اس ""( ﺙ أ‬9) 'PATH=%PATH%;C:\Python23'
.AUTOEXEC.NT < C ُ !QGَ‫ ی‬،NT ‫  یوز‬HG9
‫ﺹ‬:I ‫ا‬
R
َ ‫ا‬9 ‫)]ام‬9 UَHّ‫ یُ!ُِْ; َأنْ ُآ‬،;‫  @ا ذ‬.;َ ِ m‫ ﻥ‬N<@ ‫ ن‬8‫ی‬9 c َ Hّ`‫ ﺝًا أﻥ; ﻥ‬C!M!‫ ا‬p ‫م‬m HG9
.;Q‫( ُء  زی‬3َ (‫^م ا‬M‫إدار ِة ا‬
‫ن‬
َ q‫ و ا‬.U<@  9‫ و‬R ‫ﻥ‬H‫ < ا‬C!Mَ ‫ل‬d. ;"‫  آ‬5G9 ‫ ن‬8‫ی‬9 cH8 ‫ ی‬،‫م ویوز‬m HG9
.;
َ ِ m‫ ﻥ‬N<@ ‫ ن‬8‫ی‬9 c َ Hّ`‫ﻥّ; ﻥ‬9 ‫ض‬
ُ 4ََ) ،ً‫َ`َ@ِ َا‬6
.‫ ن‬8‫ی‬9 N<@ ‫ول‬P‫َ ا‬3 ‫ﻥ‬9 2 ُ ََْ) ،‫ ادم‬C`4‫ا‬
\ 6 ‫ ا‬02, ‫ا‬
F ‫و‬4‫?ات ا‬I ‫ا‬
:‫ﺝ ول ا ت‬
‫!ل < `ري‬Q)‫ ا‬- ‫ّر ا` ص‬M ‫َْر‬.‫ ا‬- interpreter prompt G4!‫ ﺵ ا‬/iM ‫!ل‬Q)‫ ا‬-  !‫ا‬
d]‫ ا‬- ‫@ة‬G N<@ ‫ُ`ُ ل‬M‫ ا‬- Executable "4< <9‫ ن ا‬8‫ی‬9 R ‫ا‬9 - C
ُ !ْQَ‫ آَ ی‬- output ‫ ا]ج‬-

:
‫*ا‬

Cّ>‫ و‬،_4M ،ُ2َْ 


َ َ‫<ّ!ُ; ه"ا آ‬Qَُ) .‫ ن‬8‫ی‬9 (6 'Hello World' ‫ ا<ي‬R ‫ﻥ‬H‫ ا‬C>‫ ﻥ‬ َ َ‫ن آ‬q‫)ََى ا‬
.‫ ن‬8‫ی‬9 R
َ ‫ا‬9
-:;
َ ِ3 ‫ﻥ‬9 C> ‫ ن‬8‫ی‬9 ‫ق ا)]ام‬
ِ F ْ ِ ‫هك اﺙن‬
( ‫ ل‬G‫ آ ﻥ‬R ‫ﻥ‬9 ‫ل أي‬d. ) interactive interpreter prompt (<@4‫ ا‬G4!‫ ا‬iM ‫**ا)]ام‬
. ‫ی‬g‫ آ< ا‬C
ُ !QGَ‫ ﻥ‬
َ َ‫ن آ‬q‫ و)ََى ا‬.‫** أَو ا)]ام < `ري‬

the interpreter prompt !1, ‫ ل ](! ا‬A ‫ا‬

. {console / terminal } ََ6َ َ`‫( ا‬6 python !<‫ آ‬99  ‫وا‬P‫ ا‬g) N<@ interpreter ‫أ‬9‫ا‬
.Hello World : @ ‫رة‬H@ R‫ َأنْ َى ا‬2 ُ ِ3َ‫ ی‬.Enter ‫ح‬ ِ 4!9 @ H 'Hello World ' :2‫ن اآ‬q‫وا‬
َ QZ‫{ إذا َو‬Dos}  ‫وا‬P‫ ا‬g) interpreter G4!‫ ا‬C> ْ‫ یُ!ُِْ; َأن‬، :‫ وز‬D‫  و‬I1
َ ّ>!‫ ا‬C‫ د‬c
.IDLE R َ ‫ﻥ‬9 C
َ !QGَ ْ‫ یُ!ُِْ; َأن‬،;‫ @ ذ‬d ً ‫ی‬9‫ و‬.Ld C9 PATH
.Integrated Development Environment ‫ ی  <ِ >ة‬g ِ|9 @ ‫رة‬H@ IDLE
Click on Start -> Programs -> Python 2.3 -> IDLE (Python GUI)
.ًb‫ أی‬IDLE ‫!< ا‬QGَ‫ َأنْ ی‬5ُِْ!ُ‫ ی‬p
ِ  <!QG .
:9‫ﺡ‬:ُ
. .prompt for entering Python statements ‫ ن‬8‫ی‬H G4!‫ ا‬R ‫ﻥ‬H‫ ا‬iM ‫ُ ل‬.ُ  ‫  >>> إﺵرة‬dQ‫ ا‬T"‫ه‬
Example 3.1. Using the python interpreter prompt ‫ ن‬8‫ی‬9 G4 ‫!ل ﺵ‬Q)‫ ا‬.3.1 ‫ل‬8

$ python
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>>

printC
ُ !QG‫ ﻥ‬M‫ ﻥ‬.single Python statement e6 c<.‫
 أد‬c‫ رًا ! وأﻥ‬6 gG‫ ا‬R َ ‫; ﻥ‬gQَ‫ ن ی‬8‫ی‬9 ‫ن‬
ّ ‫_ْ أ‬A
.U 5g@‫ أ‬value !
ّ‫ أی‬Hg (—‫ﺝ‬4 W C9)
.ِ‫ اﺵ‬N<@ ‫ رًا‬6 ُ َHْgُ T"‫" وه‬Hello World" - v َ‫ ا‬U g@‫
 أ‬M‫ ﻥ‬،‫ه‬

: ‫ن‬6‫!وج  ﺏ‬I ‫ ا‬$َ,$َ‫آ‬


َ6َ َ ‫]م‬G ‫ أَو‬IDLE C
ُ !QGَ c‫ إذا آ‬Ctrl+ d (4 N<@ e>Z‫ ا‬prompt R ‫ﻥ‬H‫<]ُُوج ﺵ ا‬
.ENTER ‫ح‬4!9 @ H Ctrl+z (4 e>Z‫ ا‬، ‫ یوز‬9  ‫وا‬P‫ ا‬g) ِ ‫(  ﺵ‬6, .BSD/LINUX
‫ص‬2D ‫َر !ر ا‬$ْ%‫ا‬
‫ ّ ِر‬M ‫ َر‬.‫ن ا‬ّ ‫ إ‬.ِ‫ت ا!`ری‬ ِ 4<!‫َ ا‬9ِ ‫ّرًا‬M ‫ج‬ ُ Mَ‫ ﻥ‬،ِ‫ت ا!`ری‬ ِ 4<!‫( ا‬6 ‫ ن‬8‫ی‬9 R ِ ‫ا‬9 9ِ Cَ‫ َأنْ ﻥ‬Cَ ْHَ

9‫( آ‬6 ‫@ ُك‬Gَُ) 3‫ّر ا‬M!‫ إن ا‬.5‫رة ا( َی‬G‫ َر آﻥ; َ] ُر ا‬M!‫ َأنْ ] َر ا‬2 ُ ِ3َ‫ ی‬.M‫( ا‬6 5  ‫أ‬
89 ‫ی أ)ع‬g9 (; َ َ6‫ل ه‬ُ ََ) ;
َ ِ‫ی‬W N‫ إ‬C ُ ِ`َ ‫@َك‬Gُ‫ َ وی‬8‫َ أآ‬M‫ ر<َ; ی‬C ُ Qْ3َ‫ وی‬، 5G9 ‫ ن‬8‫ی‬9 R ‫ا‬9
.ً‫ أ ﻥ‬8‫وأآ‬
‫ﺝ^اء‬P‫ ا‬C ّ ُ‫  ن آ‬iM9 ( 2‫رزة <اآ‬9 ‫ )أ ان‬syntax highlighting (‫))ِ ﺝًا ه‬P‫ت ا‬ ِ H<g!‫إى ا‬
.U<!@ 4‫; وَ` ّ ُر آ‬ َ َ3 ‫ﻥ‬9 ‫ ; أن َى‬NG‫ ی‬N ‫ ن‬8‫ی‬9 (6 colorized ‫ < ﻥ‬R ِ ‫ﻥ‬H< 4<]!‫ا‬
8‫ وأآ‬syntax highlighting C!Q9 ‫ ی م‬IDLE ‫ إن‬i .IDLE ‫!ل‬Q)‫ح @<; ا‬ ُ 
َ‫ أ‬،‫ ویوز‬C ُ !QGَ c‫إذا آ‬
.‫ى‬.‫! أﺵ ِء أ‬Z IDLE !Z ; َ ِ3 ‫ا‬9 C>9 ; ‫َ!ح‬G‫ ا‬C8 ِ 89
;‫ه ذ‬P‫ وا‬syntax highlighting C!Q9 ‫ ی م‬A U‫ﻥ‬P —) ‫ر‬.‫ ا‬U‫ إﻥ‬- Notepad ْC!QG A :ّ. mdُ
.ًA ‫( ِ آ! )ََى‬6 ‫ ُ ﺝًا‬5 ‫دءة ( وه‬H‫ت ا‬6G!‫ ا‬m ‫ )أي‬indentation - ˜ َ‫< َ ا‬8 ُ @َْ‫ ی‬A U‫أﻥ‬
.;‫ ذ‬Cُ َ!Q ًo ‫@ك‬G) (VIM ًb‫ )وأی‬IDLE C8 ‫ة‬3‫ّرات ا‬M!‫ا‬
،‫ا‬H. 3 H c‫ وإذا آ‬.‫ ّرِات‬M!‫ ا‬9 ‫ ِ ْ ا]رات‬8‫<ی; ا‬6 ‫؛‬tg]d ،Linux / FreeBSD C ُ !QGَ c‫إذا آ‬
ُ 4Gََ) ‫
ى و‬P‫ّرات ا‬M!‫! إﺙن ِ ْ ا‬5‫ﻥ‬9 ‫ ﺝ < ل‬A‫ و‬.Emacs ‫ أَو‬VIM C!QG ;‫! ا!آ أﻥ‬6
.‫ ن‬8‫ی‬9 N<@ ;ِ3 ‫ا‬9 َ9ِ !5!Q)‫إ‬
‫ وه( إى‬،KATE C َ !QGَ ْ‫ |" یُ!ُِْ; َأن‬،LH ِ3 H c‫ إذا آ‬.(ِ3 ‫ا‬9 2<WP VIM C ُ !Q)َ‫أﻥ ﺵ]`ً أ‬
ّ<Qَ ‫ة أن‬9 ;‫ﻥ( أَو‬h6 ،Emacs ‫ أَو‬VIM ّ<َQَ c
‫ء و‬b
(6 HW‫ را‬c‫(   إذا آ‬6 .ِ<ّb4!‫أدوا( ا‬
.QH‫( ا!ى ا‬6 ;َ ‫ا ﺝًا‬4 ;‫ن ذ‬ ُ ََُ) i !5 ‫ْ!ل أي‬Qِْ)‫ا‬
.‫رك‬. ‫ ن ود‬8‫ی‬9 ‫ّرات‬M! َ< ‫!َ ا‬L‫ ا‬m‫ اﻥ‬،‫ّر‬M!< ‫ى‬.P‫  ^ال ُی ُ ا)ِْْف ا]رات ا‬c‫إذا آ‬
.‫ ن‬8‫ی‬H (ِ<  ‫ ی‬g |9) (IDE (Integrated Development Environment ًb‫یُ!ُِْ; َأنْ َ] َر أی‬
‫ن‬h6، ‫ ن‬8‫ی‬9 (6 ‫ة‬H‫ آ‬R ِ ‫ا‬9 َ9ِ9 ‫ُْأ‬Hَ  @ .C4‫ ن <!^ی ا‬8‫ی‬9 ِ @ (‫ ا‬IDE ‫!َ ا <َ ـ‬L‫ﺵهْ ا‬
.M‫( ا‬6 ‫ َ ﺝًا‬4 ‫ن‬
َ ُ ْ‫ یُ! ُ َأن‬IDEs R ‫ا‬9
. 5)‫ َ َ و‬8‫ ن وأآ‬8‫ی‬9 R ‫ا‬9 9‫ آ‬C َ Qْ3َ‫ ی‬56 - ‫ّرا ﺝا‬M ْ.‫ رﺝ ًء ا‬،‫ى‬.‫أُآ ّ ُر  ًة أ‬

‫ ري‬2 ‫` ا‬- ‫ ام ا‬I ‫ا‬


U<>‫ و‬UH R ‫ﻥ‬9 ‫ اول‬،‫ ﺝیة‬3 9 > <Q ;<H) (6 c‫ آ<! آ‬U‫ هك < أﻥ‬.3 H‫ ا‬N‫ د ا‬Q‫ن د@ ﻥ‬q‫وا‬
Simon ‫ وآ!
ل‬.U<> @ 'Hello World' :‫ ه أن  ل‬U<Q6 ;<@  C‫ آ‬-- 'Hello World' R ‫ﻥ‬9 ‫ه‬
. 'Cb6‫ أ‬C9 ><‫< ا‬Q N<@ ;@G! 3 H‫ب ا‬9‫ر‬P ‫ ی^ة <ی‬Q 98!9 5‫ " إﻥ‬:١Cozens
helloworld.py :)9 Um4‫ ا( وا‬R ‫ﻥ‬H‫ ا‬C.‫ أد‬،‫ر‬M!‫ر ا‬.‫( ا‬6 ‫أ‬9‫ا‬
‫ ا)]ام ا!< ا!`ري‬3.2 :‫ل‬8
Example 3.2. Using a Source File

#!/usr/bin/python
# Filename : helloworld.py
print 'Hello World'

@ ‫()ی ا! ه !ن ا!< ا!`ري وا"ي ی( اب‬Source file: code/helloworld.py)
(U ;<!M
:  P‫ ا‬9‫( وآ‬Linux terminal or DOS prompt) C‫ا‬/6`‫ ا‬Y6 n‫ی‬F @ R ‫ﻥ‬H‫ ه"ا ا‬C>‫ﺵ‬
$ Python Hello World
R‫ وا‬.Ctrl+F5 Y4!‫`ر   ا‬.‫ او ا‬menu Edit -> Run Script ‫ ا)]م‬،IDLE ‫]م‬G c‫اذا آ‬
.T‫ أدﻥ‬H ‫ آ! ه‬output
"Beginning Perl" +‫ه‬,‫ب اﻝ‬$ ‫ واﻝ)ﻝ ﻝ‬perl6/parrot ‫آز وﻡ أز رواد‬$%‫ر اﻝ‬$'‫[وا ﻡ آ‬1]
Output

$ python helloworld.py
Hello World

‫( ل‬6‫و‬.‫ ن‬8‫ی‬9 (6 ; R ‫ﻥ‬9 ‫ أول‬C> (6 cM3‫  ﻥ‬-- !‫ﻥ‬5 ; ،Td@‫ أ‬H ‫ آ! ه‬R‫ ا‬N<@ c<` ‫اذا‬
!<@ .‫ى‬.‫ ة أ‬R ‫ﻥ‬H‫ ا‬C>‫ و‬Td@‫ أ‬H ‫ آ! ه‬eHb9 ‫ ا!"آ ر‬R ‫ﻥ‬H‫ ا‬9‫ آ‬N‫ یﺝ‬، g. N<@ c<`
case-sensitive ‫ف‬P‫ ا‬M )GM9 ^!‫ ن ی‬8‫ی‬9 ‫أن‬
.‫ة‬.P‫( ا‬6 P H‫ف ا‬M‫ و ا‬N‫و‬P‫( ا<! ا‬6 p >`‫ف ا‬M‫_ ا‬A -- Print ‫وي‬G A print !<‫ آ‬:‫ل‬8
 ‫ و)ى !ذا أن ه"ا أ‬-- g) C‫( آ‬6 ‫ول‬P‫ف ا‬M‫ ا‬CH
‫ت‬F‫ ت ﺵ‬d@ ‫ت او‬W‫ا‬6 ‫ آ @م وﺝ د‬،b‫أی‬
.nA c
‫( و‬6 5
:0 A `$‫*آ‬
‫ أي ﺵ(ء  ب‬--" comments - ‫<ت‬Q‫ "ا‬: ‫ ا<!ت‬T"‫ ه‬N!G‫ و‬.R ‫ﻥ‬H‫ی ا‬g) ‫( أول‬6 m‫د@ ﻥ ﻥ‬
. R ‫ﻥ‬H‫ رئ ه"ا ا‬4  ‫)س أ‬P‫( ا‬6 ‫ و ه‬n<Q ‫ ه‬# ^ ‫ ی! ا‬N<@
‫ و@  ی ن‬shebang line N!G (‫ وه‬.‫ول ه‬P‫ ا‬gG‫ ا‬.  ‫ء‬8)9 ‫<ت‬Q‫]م ا‬G‫ ی‬A ‫ ن‬8‫ی‬9
R ‫ﻥ‬H‫ أن ه"ا ا‬p‫\ی ﻥ‬p H]‫ن ه"ا ی‬h6 R ‫ﻥ‬H‫) ا‬9 @ H # # @ ‫رة‬H@ ‫ ا!< ا!`ري‬6 ‫أول‬
.R ‫ﻥ‬H‫" ا‬4 @ interpreter G4!‫ ه"ا ا‬C!Q‫ ان ی‬23‫ی‬
platform ` ‫ أي‬N<@ R ‫ﻥ‬H‫ ا‬C> !L‫ ﻥ; دا‬h9 U‫ @<! أﻥ‬.(‫ ا‬G‫( ا‬6 C`49 ‫و) ف یح ه"ا‬
. python helloworld.py : P‫ ا‬C8  ‫وا‬P‫ ا‬g) N<@ ‫ﺵة‬H interpreter G4!‫ی ا‬M ‫ل‬d.
@*
‫ رئ‬4  ‫ وه"ا أ‬-- R ‫ﻥ‬H‫( ا‬6 !5!‫ ا‬C4‫ ا‬kQ9 ‫; ح‬3 ‫ﻥ‬9 (6 (‫ﻥ‬d@  ‫<ت أ‬Q‫ا)]ام ا‬
) Q9 ;G4‫ ﻥ‬c‫ ی! أن ی ن أﻥ‬-]‫ إن ه"ا ا‬،‫ "آ‬.R ‫ﻥ‬H‫ ا‬U9 ‫  ی م‬56  5G9 !‫ ی‬iM9 ;3 ‫ﻥ‬9
!!5‫أﺵ‬
(‫ ه‬print !<‫ إن آ‬.'Hello World' :-‫@ ه"ا ا‬Hg9 e6 ‫  م‬-- Python statement (< (‫<ت ا‬Q‫ا‬
،!! n< A --string `‫< ﻥ‬G<) ‫ره‬H@9 5‫' یر ا‬Hello World'‫ و‬C Q operator @ ‫رة‬H@ CQ49
.Q9 !6 C`49 ‫ت‬M<g`!‫ ا‬T"‫( ه‬6 iMH)
Executable Python programs $,D- -‫ن ا
ﺏ‬6‫ ﺏ‬b‫ﺏ!ا‬
gG‫ ل @ ا‬b4‫ ا‬kQ9 5‫] ویوز ی‬G ‫ و
 ی ن‬p‫ ی ﻥ‬/ p ( ]G N<@ A‫ إ‬nHg‫ ی‬A ‫وه"ا‬
 P‫)]ام ا‬9 executable permission "49 Y‫ `ی‬R ‫ﻥ‬H‫ء ا‬g@‫ @< ا‬Q‫ ی‬، A‫ أو‬.R ‫ﻥ‬H‫ول ا‬A‫ا‬
.‫ ا!`ر‬R ‫ﻥ‬H‫ ا‬C> ‫ ﺙ‬Chmod

$ chmod a+x helloworld.py


$ ./helloworld.py
Hello World

‫ه‬Q9‫و‬.‫م‬m‫] ( ا‬G !3 ‫ذن‬A‫ء ا‬g@9 "4< d9


U<Q3 <!‫ ا‬Y‫]م ه >  `ری‬G‫ ی‬chmod  ‫أ‬
(6 ‫ ی‬R ‫ﻥ‬H‫ أن ه"ا ا‬N‫  إ‬/ .‫]م‬G‫ اﻥ ﻥ‬.‫ی
ا!< ا!`ري‬M n‫ی‬F @ ‫ﺵة‬H R ‫ﻥ‬H‫" ا‬4‫ﻥ ﻥ‬h6
.(M‫ ا‬C‫ا‬
Helloworld / . : M‫ ا‬N<@ U<>‫ و‬helloworld N‫! ا!< ا‬G ‫ إ@دة‬e6 ;!‫ ی‬، Q 8‫ ر أآ‬P‫ ا‬CQ3
<!‫( ا‬6 ‫ول‬A‫ ا‬gG‫( ا‬6 ‫د‬M!‫ ا‬G4!‫)]ام ا‬9 R ‫ﻥ‬H‫ ا‬C> U<@ ‫ف ان‬Q‫م ی‬m‫ن ا‬P C!Q‫ ی‬Cm)‫و‬
.‫ا!`ر‬
N<@ ‫ ی ارة‬c‫ و ذا  آ‬-- R ‫ﻥ‬H‫ر ا‬G eHb9 ‫ف‬Q c ‫  د‬R ‫ﻥ‬H‫ ا‬C> N<@ ‫ن
در‬q‫ ا‬c‫أﻥ‬
‫ر‬G (6 ‫ ا اردة‬U‫د‬A‫( واة ا‬6 R ‫ﻥ‬H‫ل ]^ی ا‬d. ;‫ ذ‬CQ4 ‫ اي ن؟ ی!; ان‬R ‫ﻥ‬H‫ ا‬C>
(6 ‫د ا اردة‬P‫ ا‬C‫( آ‬6 R ‫ﻥ‬H‫ @ ه"ا ا‬iMH‫م ی‬m‫ن ا‬h6 ،R ‫ﻥ‬9 ‫ أي‬C>9 c!
!<‫ آ‬.PATH |H‫ > ا‬
FG9 C9‫ ن و‬C‫( آ‬6  R ‫ﻥ‬H‫ ه"ا ا‬CQ3‫ ی! أن ﻥ‬.R ‫ﻥ‬H‫ ه"ا ا‬C>‫ و ﺙ ی‬PATH |H‫ر > ا‬G
.PATH ‫ر‬G!‫( ا‬6 ‫ ا اردة‬U‫د‬A‫ وا ا‬N‫& ه"ا ا!< ا!`ر إ‬G‫ﻥ‬

$ echo $PATH
/opt/mono/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin
$ cp helloworld.py /home/swaroop/bin/helloworld
$ helloworld
Hello World

 P‫ ا‬2‫ اآ‬-

n9G‫  ا‬P‫ ا‬3‫د ا‬P‫ ا‬P R ‫ﻥ‬H‫& ا‬G9 


-

T‫ر‬G ‫ﺝ "آ‬M‫ أي ن دون ا‬R ‫ﻥ‬H‫ ا‬C> ‫ن‬q‫ ی!; ا‬-

M‫ وﻥ‬shell N‫ ا) ا!>  إ‬CH


$ ^ ‫ وی ا‬echo  P‫)]ام ا‬9 PATH ‫ر‬G!‫ی! @ض > ا‬
:‫
! ه"ا ا!> ه"ا‬N‫ﺝ إ‬M9
$echo $PATH
‫]م ا"ي‬G!‫ ه ا) ا‬swaroop i PATH ‫ر‬G!‫( ا‬6 ‫د‬P‫ ه أ ا‬/home/swaroop/bin/ ‫ ﻥى أن‬M‫وﻥ‬
N<@ ;9 ‫]م ا]ص‬G!‫) ا‬Asimilar directory C‫ !ﺙ‬C‫ ) ن هك @دة د‬.‫م @ي‬m‫ ا‬N<@ U ])‫أ‬
9‫ آ‬n‫ی‬F @ ;‫ وی! أن ی ذ‬--PATH >!< ‫رك‬.‫ ا‬d‫ د‬b ‫ ی!; أن‬، ;" C‫ی‬H‫ وآ‬.‫زك‬5‫ﺝ‬
PATH=$PATH:/home/swaroop/mydir  P‫ا‬
PATH >!‫ ا‬N‫ إ‬U6Z‫ ا"ي أری إ‬C‫ ه ا‬/home/swaroop/mydir/ i
U‫ إﻥ‬.‫( أي ن‬6‫ و‬c
‫( أي و‬6 R ‫ﻥ‬H‫ ا‬C> ‫ة وی‬4 ‫ت‬9) 2 ‫ ی أن‬c‫ة ﺝا إذا آ‬4 ‫ی‬g‫ ا‬T"‫وه‬
.‫ أو اوس‬p 6F (6 5 ]G (‫وا  ا‬P‫ ا‬TW‫ أو‬cd  P‫ ا‬C8 5<8 ;G4  ‫ أوا‬n<. UH‫ی‬
.‫ ا(ء‬p4‫( ﻥ‬Q 5Q!‫ وﺝ‬nHg ‫ او‬cH‫ او )ی‬R ‫ﻥ‬9 U!G !‫ ی‬W.r.t. Python :K$7D‫ﺕ‬
‫@ة‬G!‫ ا‬N<@ ‫` ل‬M‫ا‬
4†‫ ی!; ا)]ام و‬، ‫ ن‬8‫ی‬9 (6 statement ‫ن‬9 ‫ )ی @ أي دا أو‬C9 ‫< ت‬Q N‫ﺝ إ‬M9 c‫إذا آ‬
N<Q6 .interpreter promptG4!‫`  @ ا)]ام ﺵ ا‬.‫ ﺝا و‬4 ‫ ه"ا‬. R ‫ﻥ‬H‫( ا‬6 3 !‫@ة ا‬G!‫ا‬
-‫ ﻥ‬C‫]م ]^ی آ‬G (‫وا‬str class Hg‫@ة @ ا‬G ‫ض‬Q‫  ی‬P‫ ه"ا ا‬-- help(str) C>‫ل ﺵ‬8!‫ ا‬CH)
|‫ ا‬3 H9 n<Q!‫ ا‬C`4‫( ا‬6 C`49 5‫) ﺵ‬Classes ‫ت‬Hg‫ ا‬.;3 ‫ﻥ‬9 (6 U ]G (C!3‫)ا‬
object-oriented programming 5‫ا! ﺝ‬

:9‫ﺡ‬:
‫@ة‬G!‫ <]وج ا‬q e>Z‫ا‬

‫ ا!^ی  ل‬6Q! help () ‫ ا)]م‬.‫ ن‬8‫ی‬9 (6 H‫< ت @ أي ﺵ(ء ی‬Q N<@ ‫` ل‬M‫ ی!; ا‬،C8!9‫و‬
!5G4‫@ة ﻥ‬G!‫ی ا‬F ‘ ‫ا)]ام‬
> ‫ی‬M N‫ﺝ إ‬M9 c‫ﻥ‬6 ،print C8 operator C Q @ ‫@ة‬G N<@ ‫` ل‬M‫ ا‬N‫ﺝ إ‬M9 c‫( ل آ‬6
/ p N<@  5G9 ;‫ وی! أن ی ذ‬.2) C9PYTHONDOCS environment variable |H‫ا‬
.ENV: P‫)]ام ا‬9 p‫ی ﻥ‬

$ env PYTHONDOCS=/usr/share/doc/python-docs-2.3.4/html/ python


Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help('print')

‫@ة‬G ‫ر‬bM)‫ أﻥ( اری ا‬54‫ ن أن ی‬8‫ی‬H !‫ ی‬N 'print' ‫ی‬M " ‫س‬H
A‫  ا‬d@ c ])‫_ أﻥ( ا‬d)
.‫@ أي ﺵ(ء‬HF U A (‫' وأﻥ‬print' ‫ ل‬

‫ أو‬Q‫ < زی‬HF 4<] ‫ و


  ن‬-- 3 ‫ورا‬6 (6 ‫]م‬G!‫ و ه ا‬R ‫ﻥ‬H< ‫ول‬P‫ ا!
ا‬c ])‫@<! أﻥ( ا‬
.]G‫ا‬

:d]‫ا‬
‫ن‬q‫ د@ ا‬، ‫ ن‬8‫ی‬9 ‫]م‬G c‫ن أﻥ‬q‫ ا‬. 5) C9 ‫ ن‬8‫ی‬9 R ‫ا‬9 C>‫ و‬،_4‫ و‬،9‫ آ‬N<@ ‫ن
درا‬q‫ أن  ن ا‬23‫ی‬
. ‫ ن‬8‫ی‬9 @ 8‫ه أآ‬4 <Q‫ﻥ‬
"‫ ا !اﺏــ‬0‫ـــ‬2, ‫ا‬
‫ـــت‬$  4‫ا‬
‫  ا ت‬

Literal Constants ..............................................................................................6M‫ ا‬c9‫ ا‬8‫ا‬


Numbers .....................................................................................................................‫@اد‬P‫ا‬
strings ..............................................................................................................`‫ ا‬C)dG‫ا‬
variables ...................................................................................................................‫ا!>ات‬
Identifier Naming ...............................................................................................‫ف‬Q!‫! ا‬G
Data Types ..........................................................................................................‫ﻥت‬H‫أﻥ اع ا‬
Objects .......................................................................................................................‫ت‬L‫ا‬
Output ..........................................................................................................................‫ا]ج‬
How It Works..........................................................................................................C!Q‫آ ی‬
physical lines Logical .........................................................................g!‫ ر ا!دی وا‬gG‫ا‬
Indentation ...................................................................................................................<8‫ا‬
Summary ...................................................................................................................d.

kQ9 ". ‫ ی أن‬-- ;‫ ذ‬8‫ أآ‬CQ4 ‫ ی أن‬C‫ آ"; ؟! ه‬p‫ أ‬،(4‫ ی‬A e6 'Hello World' @HF ‫إن‬
‫ وا!>ات‬c9‫ ا‬8‫)]ام ا‬9 ‫ ن‬8‫ی‬9 (6 ‫ ه"ا‬nM‫ ی! أن ﻥ‬.5 ‫ ﺵ(ء‬N<@ ‫` ل‬M‫ وا‬59 2@d‫ و ا‬،‫ت‬d.!‫ا‬
.constants and variables

Literal Constants $+! ‫ ا‬c‫اﺏ‬6 ‫ا‬

'This is string ‘ C8 <!‫ أو ﺝ‬e - 39.25 ، 23،1 ، 5 ‫د‬Q‫ ا‬: ‫( ه‬6M‫ ا‬c98‫ل ا‬8
:(‫ﻥ‬8‫  ا‬P‫ ا‬. 6M‫ ا‬5!
‫]م‬G c‫ – وأﻥ‬6 5‫ﻥ‬P literal 6M‫ ا‬N!G T"‫ وه‬."! It's a string" ‫او‬
5<‫ آ‬T"56 ،;" .> ‫ ی! أن‬A 5!
‫ن‬P constant c9‫ وه( ﺙ‬-- .o ‫ ﺵ(ء‬A‫ و‬5‫(  ذا‬6 5G4‫ ﻥ‬C8! 5‫أﻥ‬
.lliteral constants 6M‫ ا‬c9‫ ا‬8‫ ا‬5‫ﻥ‬9 5‫یر ا‬

:‫ اد‬G4‫ا‬

<4‫ أ@اد ا‬،long integers <‫ ی‬F MM ‫ أ@اد‬،integers MM ‫ أ@اد‬-:‫ع‬
ِ ‫ أﻥ ا‬Q9‫ ن أر‬8‫ی‬9 (6 ‫@اد‬P‫ا‬
.complex numbers َHّ‫ وأ@اد آ‬floating point ،!LQ‫ا‬

.e6 YM ‫ ا( ه( @د‬2 :ِMM`‫@اد ا‬P‫ ا‬N<@ <8 ‫أ‬


.H‫ أآ‬5‫ و‬MM ‫ أ@اد‬e6 (‫ ی< ه‬g‫ ا‬MM`‫@اد ا‬P‫ا‬
-4
(6 10 N‫ یُ ُ إ‬E-4.,52.3 E-4, 3.23 (‫`را‬.‫ ا‬float‫ )أَو‬floating point،/!LQ‫< ا‬4‫< أ@ا َد ا‬8 ‫أ‬
52.3*10-4 (Q52.3*E-4 ،ِM‫ ا‬T"‫ه‬

(2.3 - 4.6j) ‫( و‬-5+4j) ِHّ‫@اد ا!آ‬P‫< ا‬8 ‫أ‬


strings $2D ‫ ا‬0 :1 ‫ا‬
. ‫! @ ا<!ت‬3 ‫د‬3 ))‫ وه( أ‬.‫وف‬M‫< ا‬G<) (‫ ه‬: strings
;<@ ;" ،5H (‫ ن ا‬8‫ی‬9 R ‫ا‬9 H‫ ی‬2‫ ﺝﻥ‬C‫( آ‬6 strings ‫)]ام‬9 ‫! اﻥ; ) م‬Z‫ أن أ‬g)‫ أ‬H‫ی‬
: ‫ ن‬8‫ی‬9 (6 `‫ ا‬C!3‫]م ا‬G ‫ وإ; آ‬.(‫^ء ا‬3‫ ا‬N‫ إ‬TH‫ﻥ‬A‫ا‬

(’) ‫دة‬4!‫س ا‬H


A‫ ت ا‬d@ ‫** ا)]ام‬
C8 ‫ء‬bH‫ ا‬G!‫' ﺝ! ا‬Quote me on this' C8 ‫س‬H
‫  ا‬d@ ‫)]ام‬9 string -‫ی ا‬M ;!‫ی‬
. (‫_ آ! ه‬4M tabs ‫ت وال‬W‫ا‬4‫ا‬
: double quotes ( ") ‫س ا!^دوﺝ‬H
A‫ ت ا‬d@ ‫** ا)]ام‬
(‫ ا‬C!3‫( ا‬6 !‫ی !  آ‬g‫ ا‬p49 C!Q " " double quotes ‫س ا!^دوﺝ‬H
A‫ ت ا‬d@ !Z C!3‫ا‬
"What's your name?" ;‫ ذ‬N<@ ‫ل‬8 . single quotes ’ ’ ‫دة‬4!‫س ا‬H
A‫ ت ا‬d@ !Z

triple quotes ’’’ ’’’ ‫ﺙ‬d8‫س ا‬H


A‫  ا‬d@ ‫**ا)]ام‬
 d@ ‫ وی!; ا)]ام‬.‫ﺙ‬d8‫س ا‬H
A‫ ت ا‬d@ ‫)]ام‬9 `‫ ا‬C)dG‫ ا‬g)P‫دة ا‬Q C!‫ی ﺝ‬M ;!‫ی‬
.‫ﺙ‬d8‫س ا‬H
A‫  ا‬d@ !Z ‫ی‬M9 ‫دة وا!^دوﺝ‬4!‫س ا‬H
A‫ا‬
:‫ل‬8!‫ ا‬CH) N<@


• '''This is a multi-line string. This is the first line.
• This is the second line.
• "What's your name?," I asked.
• He said "Bond, James Bond." '''

Escape Sequence
CH) N<@ ‫!< ؟‬3‫ ا‬T"‫د ه‬M ‫ آ ; أن‬،( ' ) ‫دی‬6 ‫س‬H
‫  ا‬d@ N<@ ‫ ي‬M <!3‫ض أﻥ; ی أن  ن ا‬6‫ا‬
‫س‬H
‫  ا‬dQ9 (What's your name )‫رة‬HQ‫ ا‬T"‫ی ه‬M ;!‫ ی‬A the string is What's your name ،‫ل‬8!‫ا‬
‫س‬H
A‫  ا‬d@ CQ3 ‫ @<; أن‬Q) ‫ "ا‬.(5 ‫!< وأی‬3‫أ ا‬H ‫ أی‬i ;H‫ ن ) ف ی‬8‫ی‬9 ‫ن‬P ’ ‫دة‬4
;‫ز ذ‬3‫ ه"ا وی! إﻥ‬.(N‫و‬P‫س ا‬H
A‫  ا‬d@ @ c5‫!< اﻥ‬3‫ ن أن ا‬8‫ی‬9 Q‫ ی‬d|) -‫ی ا‬5  A ‫ا اة‬
\ ‫{ ه"ا‬-‫ی ا‬5‫ ﻥ‬CH
} ‫دة‬4!‫س ا‬H
A‫  ا‬d@ CQ‫ اﺝ‬.(‫وب‬5‫ ا‬C)d)) escape sequence N!G‫@ة  ی‬G!9
.\ backslash. 4<]‫< ا‬L!‫ ا‬F‫ ا‬N‫ إ‬UH‫'اﻥ‬--

'What\'s your name?'. :*  ‫س ﻡ( دة آ‬$%&‫   اﻝ  اﻝ "!ﻡ ا‬،‫ان‬

‫س ﻡ( د‬$%&‫ام ا‬7%‫"اي اﺱ‬What's your name?" % ‫ن‬%‫ ﺱ‬2 3 3‫ اﻝ‬01‫ ه‬%‫ ا ى ﻝ‬.
‫هك‬
‫س‬$%&A‫ام !ﻡ ا‬7%‫ اﺱ‬8‫ ﻡ> أﺝ‬escape sequence ‫ام‬7%‫ = اﺱ‬،8‫ وﻝ‬.‫س ﻡ;دوج‬$%&‫ ا‬8‫ا‬

K‫ ﻝ‬K ‫  ان‬،‫ﻝ‬1‫ آ‬.‫س ﻡ;دوﺝ‬$%&‫ﺹ  ﻡ
 "!ﻡ ا‬J‫ اﻝ  ا‬G‫* ﺡل آﻥ‬D B‫اﻝ;دوﺝ ذا‬
escape sequence \\. ‫ام‬7%‫
 ﺱ‬K‫ اﻝ‬backslash B‫ ( ذا‬7‫  اﻝ‬L‫اﻝ‬

‫ ’’’ آ ه‬-’’’ *‫س ﺙ!ﺙ‬$%&‫ام ا‬7%‫ أﺡ اﻝ ق ﺱ‬8%‫ ﺱ ان ؟ و‬B ‫  ﻥ‬3 ‫ﻡذا ﻝ أردت أن د ﺱ‬
‫ اﻝ ء ﺱ‬K%‫ ﻝ‬- \ n % ‫ اﻝ‬3 ‫ ﻝ‬escape sequence. ‫ام‬7%‫ أو  اﺱ‬0! ‫> أ‬$‫ﻡ‬
This is the first line\nThis is the second line ‫ا‬1‫ وﻡل ه‬.‫ﺝ‬
escape ‫ب‬5‫ ا‬C)d) ‫ی‬Q‫ وهك ا‬. - \t tab ‫ ه ال‬escape sequences ‫ى ـ‬.‫ة أ‬L6 ‫ هك‬.
. ‫ ه‬UQ4 ‫ه‬8‫ أآ‬N‫ إ‬e6 ‫ و( أﺵت‬sequences

N‫!<  ا‬3‫ی ا‬5‫( ﻥ‬6 backslash \ 4<]‫< ا‬L!‫ ا‬F‫ ه أن ا‬،<!3‫( ا‬6 Umd‫ﺵ(ء وا @< أن ﻥ‬
،‫ل‬8!‫ ا‬CH) N<@ .‫ ﺝی‬g) 6Z‫ون إ‬9 ‫ و‬،CH!‫ ا‬gG‫( ا‬6 ‫!ة‬G `‫< ا‬G<G‫أن ا‬
Variables ‫!ات‬$< ‫ا‬
‫ی‬F N‫ﺝ ا‬M9 M‫ وﻥ‬-- d ً ! ‫ أ ا‬Q‫ )ی‬YH`‫ ی! أن ی‬e6 literal constants 6M‫ ا‬c9‫ ا‬8‫إن ا)]ام ا‬
‫ ل‬eHb9 ‫ ا!>ات‬.‫( ا` رة‬6 ‫ ا!>ات‬5m i ‫ وه"ا‬.b‫ ای‬5 ‫( أي‬6 2@d‫< ت وا‬Q!‫  ]^ی ا‬
cG ‫ ا!>ات‬.Variable > ‫)]ام‬9 ‫ اي ی!; ان ]^ن اي ﺵ(ء‬5!
‫وت‬4 ‫ ی! ان‬U‫ﻥ‬P - 5!)‫ ا‬N<@
literal 6M‫ ا‬c9‫ ا‬8‫ ا‬pQ9 .‫< ت‬Q!‫ ا‬kQ9 ‫ ]^ن‬i ;9 ‫  ا]ص‬H!‫ زة ذاآة ا‬3M ‫) ى اﺝ^اء‬
.‫ أ)!ء‬5Lg@‫( ا‬9‫ و‬، ‫ ا!>ات‬T"‫ ه‬N‫ی  <  ل ا‬F N‫ﺝ إ‬M9 c‫ أﻥ‬،constants

Identifier Naming ‫ف‬Q!‫! ا‬G


;<@ @‫ ا ا‬kQ9 ‫ وهك‬. ‫ی ﺵ(ء‬Q NgQ ‫ت ه( أ)!ء‬6Q!‫ ا‬. ‫ت‬6Q!‫ ا‬N<@ <8 ‫ا!>ات ه( ا‬
: ‫ت‬6Q!‫! ا‬G 5@H‫ا‬

(upper or lowercase) ‫ة أو >ة‬H‫ی آ‬39A‫وف ا‬M‫ أن ی ن ا‬9 A ‫ف‬Q!‫ول ا‬P‫ف ا‬M‫**ا‬
.('_') underscores b4] F‫او ﺵ‬

F‫ و ﺵ‬، (upper or lowercase) ‫ة أو ا`>ة‬H‫وف ا‬M‫ف ی! ان  ن ا‬Q!‫ ا) ا‬9 **
.(9-0) ‫ او ار
م‬.('_') underscores b4]

cG myname‫ و‬Myname ، ‫ل‬8!‫ ا‬CH) N<@ .case-sensitive ‫ف‬P‫ ا‬M )G ‫ف‬Q!‫** أ)!ء ا‬
.‫ة‬.A‫ ا‬T"‫ى ان > ه‬.P‫( ا‬6 m >`‫ف ا‬M‫ وا‬N‫و‬P‫( ا<! ا‬6 M H‫ف ا‬M‫_ ا‬d‫ ﻥ‬.‫رﺝ واة‬9

i, __my_name, name_23 and a1b2_c3 ، :M ‫ت‬6Q ‫)!ء‬P <8 ‫ا‬


(&‫ إ‬...... ‫ي‬39‫ ف أ‬U‫ أو‬i8‫ – وا‬underscore ‫أ ب‬H‫ﻥ( ی‬8‫( –وا‬L3‫ول ف ه‬P‫ف ا‬Q!‫_ أن ا‬d‫)ﻥ‬
2things, this is spaced out and my-name : M W ‫ت‬6Q ‫)!ء‬P <8 ‫ا‬
dash - F‫ ﺵ‬N<@ ‫ ي‬M‫ ی‬i8‫ وا‬- ‫ت‬W‫ا‬6‫ت و‬6G N<@ ‫ ي‬M‫ﻥ( ی‬8‫
 – وا‬9 ‫أ‬H‫ول ی‬P‫ف ا‬Q!‫ _ أن ا‬d‫)ﻥ‬
(underscore p‫و‬

Data Types ...‫ﻥت‬$7 ‫أﻥاع ا‬


:(‫)) ه‬P‫ﻥ اع ا‬P‫ ا‬.Data Types N!G ‫ﻥت‬H‫ ا‬4<] ‫ﻥ اع‬P 
C! 5!‫ ی‬Variables ‫ا!>ات‬
‫ﻥ اع‬P‫ إﻥء ا‬4‫` ل اد  )ى آ‬4‫( ا‬6‫ و‬. CH
‫ ا( ﻥ
ه‬strings ‫ ا` ص‬C)d)‫ و‬- ‫@اد‬P‫ا‬
classes /‫ت‬Hg‫)]ام ا‬9 9 ]‫ا‬

Objects …. ‫ت‬D ‫ا‬


. ‫م‬Q‫ ا‬TQ!9 ‫ وه"ا ه ا!` د‬،object L‫ آ‬U‫ أﻥ‬N<@ R ‫ﻥ‬H‫( ا‬6 ‫]م‬G ‫ أي ﺵ(ء‬N‫ ن ی إ‬8‫ی‬9 ‫"آ أن‬
( object L‫ ) آ‬U@ ‫( ﻥ ل‬something - ‫ " ﺵ(ء‬U@ ‫ أن ﻥ ل‬AH6

Object Oriented Programming users :*‫ ا ﺝ‬$D ‫ ا‬/!7 ‫  ا‬I1 9‫ﺡ‬:**


‫@اد‬P‫ ) اء ا‬object @ ‫رة‬H@ ‫ ﺵ(ء‬C‫ إن آ‬NQ!9 5‫ ا! ﺝ‬L‫ ا‬3 H‫ل ا‬3 (6 ‫
ی‬3 9 > ‫ ن‬8‫ی‬9
. functions ‫ اوال‬N‫و‬،‫ا` ص‬،
Variables literal constants 6M‫ ا‬c9‫ ا‬8‫]م ا!>ات ا‬G‫ن آ ﻥ‬q‫)ى ا‬Save the following
. R ‫ﻥ‬H‫ ا‬C>‫( < ﺙ ﺙ ﺵ‬6 (‫ل ا‬8!‫_ ا‬4‫ا‬

How to write Python programs :‫ن‬6‫ ﺏ‬+ b‫!ا‬7 ‫ ا‬.‫` ﻥ‬$‫آ‬


: (‫ ا‬M‫ ا‬N<@ ‫ ن‬8‫ی‬9 R ‫ا‬9 C>‫_ و‬4M ‫ هك إﺝاءات
) ة‬، ‫`@ا‬6 ‫ن‬q‫ ا‬
.;‫ ی‬Cb4!‫ر ا` ص ا‬M Y6‫ ا‬.١
.(‫ل ا‬8!‫( ا‬6 ; NgQ!‫ ا‬R ‫ﻥ‬H‫ آ د ا‬C.‫ أد‬.٢
!‫ن  ن ﺝ‬9 U<@ 4‫  ا‬H‫ ا‬n<Q‫( ا‬6 ‫ ا!"آ ر‬R ‫ﻥ‬H‫( أول آ د ا‬6 H!‫) ا‬A9 <!‫_ ا‬4‫ ا‬.٣
. Py. ‫ اد‬9 U† 4M!‫ ا‬R ‫ا‬H‫ا‬
IDLE ‫ او ا)]ام‬python program.py (‫  ا‬P9 R ‫ﻥ‬H‫) ا‬9 @ H ‫ ن‬8‫ی‬9 G4 C> .٤
c
‫( و‬6 YZ ‫ آ! ه‬، executable ‫"ی‬4‫ت ا‬4<!‫ی ا‬F ‫ ا)]ام‬b‫ ی!; ای‬.R ‫ا‬H‫ ا‬C>
.n9)
Example 4.1. Using Variables and Literal constants

# Filename : var.py

i=5
print i
i=i+1
print i

s = '''This is a multi-line string.


This is the second line.'''
print s

Output

$ python var.py
5
6
This is a multi-line string.
This is the second line.

How It Works b‫!ﻥ‬7 ‫ ا‬0 G $,$‫آ‬


‫ول‬P‫ ا!> ا‬N‫ ا‬٥ U!
(i) literal constant (6 c9‫)د ﺙ‬h9 !
، A‫ أو‬.R ‫ﻥ‬H‫ ه"ا ا‬C!Q‫إ; آ ی‬
U9 ‫>( ام‬H‫ ﺵ|  ی‬Q‫ و‬H 5‫ﻥ‬P statement Y‫ `ی‬N!G‫ ی‬gG‫ ه"ا ا‬.(=) operator C Q!‫)]ام ا‬9
‫ ا"ي‬print Y‫)]ام ا`ی‬9 i !
QHF ;‫ ذ‬Q9 .٥ U!‫ ا‬i ‫ول‬P‫ ا) ا!> ا‬g9‫ ر‬، M‫ ا‬T"‫( ه‬6‫و‬،
. ‫ اﺵ‬N<@ i >!‫@
! ا‬Hg9 U‫ اه‬8‫ ی‬A (QHF C9‫ و‬e6 ‫ی م‬

. ٦ U!‫ ا‬N<@ C`M‫!


أن ﻥ‬6 U@HF ‫ ﺙ‬.(Q‫ﺙ رﺝ‬9 U‫ول و]^ی‬P‫( ا!> ا‬6 ‫ ا!]^ﻥ‬U!‫ ا‬N‫ ا‬١ 4Z‫ن أ‬h6

. U@HF ‫ ﺙ‬s >!< literal string ‫ﻥ أ)ﻥ‬6 ، C8!9‫و‬


Note for C/C++ Programmers………………………………. ++ \ /!7 9‫ﺡ‬:
Y‫ @ ﻥ ع ا`ری‬declaration ‫ن‬d@¦ ‫ هك ﺝ‬p‫ و‬.!
N‫)ده إ‬h9 e6 ‫]م‬G Variable ‫ا!>ات‬
. ]G!‫ت ا‬6Q!‫أو ا‬

Logical and Physical Lines… $


?D ‫?ر ا‬1 ‫?ر ا د وا‬1 ‫ا‬.
‫ وا‬Y‫ ن آ`ی‬8‫ی‬9 (U!54‫ )ی‬T‫( ه  یا‬g!‫ ا‬gG‫ وا‬.R ‫ﻥ‬H‫ ا‬2  @ T‫ )ا!دي( ه  ا‬N<Q4‫ ا‬gG‫ا‬
UQ n9g‫( ی‬g g) U<9‫ ی‬Physical ‫<( دي‬Q6 g) C‫! ان آ‬Z ‫ض‬4 ‫ ن‬8‫ی‬9 .single statement
Logical
(6 U‫ )آ! وﻥ‬U‫(  ذا‬6 gG‫ ا‬N<@ 9  ‫ اذا آن‬-- print 'Hello World' :(g!‫ ا‬gG‫ ا‬N<@ ‫ل‬8
.‫ دي‬g) n9g‫ ی‬b‫ ه"ا ای‬،(‫ر‬M!‫ا‬
(6  5) 8‫ ا د أآ‬CQ3‫ ! ی‬g) C single statement ‫ وا‬Y‫!ل `ی‬Q)‫ ا‬N<@ ‫ ن‬8‫ی‬9 3 ،!Z
. .‫ااءة‬
<4‫)]ام ا‬9 ‫د ه"ا ا‬M ‫ @<; ان‬،‫ دي‬g) N<@ ‫( وا‬g g) 8‫د أآ‬M ‫ ی أن‬c‫اذا آ‬
،‫ل‬8!‫ ا‬CH) N<@ .statement Y‫( أو ا`ی‬g!‫ ا‬gG‫ی ا‬5‫ ﻥ‬N‫ );( وه  ی ا‬F !‫ا‬
i=5
print i
:‫ ه"ا‬UH‫@<  آ‬4‫ ا‬p49 ‫وی ن‬
i = 5;
print i;
: (<‫ آ! ی‬U9‫ ی! آ‬C‫ ا‬p49‫و‬
i = 5; print i;
:‫ ه"ا‬N ‫أو‬
i = 5; print i
8‫ ا)]ام اآ‬.‫ دي وا‬g) C e6 ‫( وا‬g g) 9‫ آ‬N<@ cH8 ‫ ة ان‬9 (‫ﻥ( او‬6 ،;‫و ذ‬
‫
ر‬F !‫`< ا‬4‫ ا‬23 (‫ة ه‬4‫ وا‬. C‫ ی‬F (g!‫ ا‬gG‫ اذا آن ا‬A‫( ا‬g g) C ‫ دي وا‬g)
U<4‫ ا‬c‫ رأی‬N ‫ أو‬5 ]G  ‫ اﻥ‬،M‫( ا‬6 .‫(
اءة ا د‬6 c
‫ ^ی ا‬N‫ دي ا‬5‫ﻥ‬P ‫ا‬m‫ا ن ﻥ‬
. ‫ ن‬8‫ی‬H R ‫ﻥ‬9 (6 UF !‫ا‬

. gG‫!م ا‬b‫ اﻥ‬N‫ ح ا‬Z 9 ‫ ه"ا وی‬.(<‫ دی آ! ی‬g)‫ @ة أ‬N‫( ی! إ‬g g) 9‫ آ‬N<@ ‫ل‬8
s = 'This is a string. \
This continues the string.'
Print s

:R‫ ا‬gQ T"‫وه‬


This is a string. This continues the string.
:C8!9‫و‬
print \
i
print i C8 eHb9 (‫ه‬
‫ل‬M‫ وه"ا ه ا‬.( \ ) backslash <L!‫ ا‬GQ‫ ا‬F‫ ا)]ام ا‬N‫ج ا‬M A ;‫!( اﻥ‬Z ‫اض‬6‫اﻥ هك ا‬
} ‫ة‬Q3 ‫ [ أو‬square brackets] Q9 ‫ ( او‬parentheses) d‫ أ
اس ه‬9 (g!‫ ا‬gG‫]م ا‬G‫@  ی‬
2‫ @  ﻥ‬C!Q‫ ی!; أن ى ه"ا ی‬.implicit line joining (!b‫!م ا‬b‫ﻥ‬A‫ ا‬g) N!G‫وه"ا ی‬. { curly
.  ‫` ل اد‬4‫( ا‬6 L‫)]ام ا ا‬9

: Indentation
{!! <‫ وا‬2‫< وا"ی‬8‫ ا‬N!G 6‫ و‬،5H‫"ی‬5‫ت و‬W‫ا‬4‫دءة وا‬H‫ت ا‬6G!‫ ا‬m TQ Y<g` }
N!G‫وه"ا  ی‬. 5  ‫ أ‬gG‫ای ا‬9 (6 ‫ء‬bH‫ت ا‬W‫ا‬4‫ا‬، CQ49‫ و‬.‫ ن‬8‫ی‬9 (6 !5 ‫ء‬bH‫ت ا‬W‫ا‬4‫ا‬
Indentation
<8‫ ى ا‬G ‫ی‬M (6 ‫]م‬G (g!‫ا‬gG‫ای ا‬9 (6 (‫ت‬F‫ت وا‬6G!‫)))ا‬P‫ء ا‬bH‫ت ا‬W‫ا‬4‫ ا‬.
. Statements ‫ﻥت‬H‫! ا‬3 ‫ی‬M ‫]م‬G‫ ی‬T‫ور‬9 ‫ وا"ي‬،(g!‫ ا‬gG<indentation level

C‫ آ‬.indentation <8‫ ا‬p4‫ ﻥ‬5 ‫ ان ی ن‬23‫ ی‬2‫ ﺝ‬N‫ ا‬H‫ ﺝ‬G (‫ ا‬statements ‫ﻥت‬H‫( ان ا‬Q‫وه"ا ی‬
.A ‫` ل‬6 (6 blocks !‫ ار أه‬N<@ <8 ‫ و)ى أ‬.block <‫ ا‬N!G ‫ﻥت‬H‫! @ ا‬3
: ‫ل‬8!‫ ا‬CH) N<@.‫ء‬g.‫ أ‬N‫— ی! أن یدي إ‬F]‫ ا‬indentation <8‫ وه أن ا‬T‫أ  وا @<; أن "آ‬

i=5
print 'Value is', i # Error! Notice a single space at the start of the line
print 'I repeat, the value is', i

:g]‫; ه"ا ا‬gQ‫ ی‬U<> @‫و‬

File "whitespace.py", line 4


print 'Value is', i # Error! Notice a single space at the start of the line
^
SyntaxError: invalid syntax

2‫ن اآ‬9 ‫ﻥ‬H]‫ ی‬i ‫ ن‬8‫ی‬9 U‫ أﺵ إ‬g]‫ ا‬.(‫ﻥ‬8‫ ا‬gG‫ای ا‬9 (6 ‫ واة‬W‫ر‬6 6G !‫_ أن ﺙ‬A
;‫ ه"ا أﻥ‬NQ .YM`‫ ا‬U‫ ا ﺝ‬N<@ ‫  ب‬W R ‫ﻥ‬H‫ آ ن ا‬C8 .MM W R ‫ﻥ‬H‫( ه"ا ا‬6 syntax ‫ ي‬M‫ا‬
!L‫ دا‬5 ]G (‫ ا‬GL‫ء ا< ا‬8)9) ‫ﻥت‬H‫ ﺝیة ا‬block <‫ای آ‬9 FH@‫` رة ا‬9 ‫ا‬9‫ أ‬gG A
n6 (6 M‫ ا‬C8 A ‫` ل‬6 (6 5<`4 ) ‫یة‬3‫ ا‬C‫ ا)]ام ا‬56 ;!‫ت ا( ی‬AM‫ ا‬.(‫ل‬M‫ ا‬QHg9،
.. ‫ﻥت‬H‫ا‬

@$-6 ‫ ا‬0 A‫` ﺕ


م ﺏ‬$‫آ‬How to indent
<] H@ YM`‫ ا‬M‫ ا‬N<@ C!Q A 5‫ @ أﻥ‬db6 <8< ( tabs and spaces) ‫ ت‬dQ‫ ا‬3‫]م ^ی‬G A
. indentation ‫ ى‬G C ‫ت‬6G 9‫ واة او اﺙ أو أر‬tab ‫]م‬G ‫ة أن‬9 (‫ وأو‬.‫ا!`ت‬
‫ا‪ .‬أ ه"‪ T‬ا‪ 2)P‬ا‪d8‬ﺙ <‪ .<8‬وا‪A‬ه ذ; ‪ ،‬ان ]ر واة وا)]ا ‪!)9 5‬ار‪indentation .‬‬
‫  ه إ‪ A‬ا)]ام ا‪ <)A‬ب ‪. 2GM6‬‬
‫ا ‪:I‬ﺹ‬
‫ا‪q‬ن ‪ Q9‬أن رﻥ @<‪ N‬آ‪ 8‬ا‪ C4‬ا
‪ ،‬ی! ا‪A‬ﻥل ا‪^ N‬ی ا‪ P‬ر ا‪P‬ه ‪ C8‬ا‪n6 (6 M‬‬
‫ا‪H‬ﻥت ‪ .control flow statements‬آ @<‪ N‬ﺙ أﻥ; )`‪ ) !  YH‬ف ؤ‪ (6 T‬ه"ا ا‪.C`4‬‬
QI ‫ ا‬02, ‫ا‬
‫!ات‬$7A ‫ وا‬0‫ا‬A ‫ا‬
Operators and Expressions
‫ﺝ ول ا ت‬
Introduction ............................................................................................................ 
Operators ...............................................................................................................C ‫ ا‬Q‫ا‬
Operator Precedence .......................................................................................C Q‫ ا‬H)‫أ‬
Order of Evaluation............................................................................................‫ ا‬2<F
Associativity..........................................................................................................g9‫اا‬
Expressions .......................................................................................................‫ات‬HQ‫ا‬
Using Expressions.. ...................................................................................‫ات‬HQ‫ا)]ام ا‬
Summary..............................................................................................................d]‫ا‬

:
 **
.expressions ‫ات‬HQ N<@ ‫ ي‬M 599 ‫ ( ا( ) م‬logical lines) (‫ وه‬statements ‫ﻥت‬H‫ ا‬2<W‫أ‬
operators and operands ‫ وود‬C ‫ @ ا‬N‫ إ‬5!G !‫ات ی‬HQ‫ أ ا‬.٣+٢ ‫ات‬HQ‫ ا‬P eG9 ‫ل‬8!‫آ‬
+ C8 ‫ ا ز‬9 5<8! !‫ ﺵ(ء  وی‬C!Q9 ‫(  م‬U6d.‫ح و‬g‫! وا‬G‫! وا‬3‫ )آ‬4†‫ ه( و‬: Operators
. keywords/4!‫أو أ ا<!ت ا‬
@ ‫رة‬H@ ٣ ،٢ HQ M‫ ا‬T"‫( ه‬6‫ و‬.operands/‫ت‬d Q!‫ ا‬N!G ‫ﻥت‬H‫ ا‬kQ9 2<g : Operators
operands
. 5 ‫ وا)]ا‬operators :C ‫ ا‬Q‫  ]`ة  ل ا‬39 ‫ ) ف ﻥ م‬Operators

‫ل‬8!‫ ا‬CH) N<@ . ‫ﺵة‬H interpreter G4!‫)]ام ا‬9 <8 P‫( ا‬6 ‫ة‬gQ!‫ب ا!دی ا‬G ;!‫ ی‬: g$ -‫ﺕ‬
Python interpreter prompt ‫ ن‬8‫ی‬H9 ‫ ا]ص‬C@4!‫ ا‬G4!‫ ا)]م ﺵ ا‬3+2 HQ‫ر ا‬H.A

>>> 2 + 3
5
>>> 3 * 5
15
>>>
Table 5.1. Operators and their usage 5 ‫ وا)]ا‬Z‫ ای‬C ‫ ا‬Q‫ ا‬H‫ﺝول ی‬
Operator Name Explanation Examples
+ Plus Adds the two objects 3 + 5 gives 8. 'a' + 'b' gives 'ab'.
Either gives a negative
number or gives the -5.2 gives a negative number. 50 - 24
- Minus
subtraction of one number gives 26.
from the other
Gives the multiplication of
* Multiply the two numbers or returns 2 * 3 gives 6. 'la' * 3 gives 'lalala'.
the string repeated that many
Operator Name Explanation Examples
times.
** Power Returns x to the power of y 3 ** 4 gives 81 (i.e. 3 * 3 * 3 * 3)
4/3 gives 1 (division of integers
/ Divide Divide x by y gives an integer). 4.0/3 or 4/3.0 gives
1.3333333333333333
Floor
// (
9 ‫ون‬9 !G‫ ا‬R‫ ﻥ‬Q 4 // 3.0 gives 1.0
Division
Returns the remainder of the
% Modulo 8%3 gives 2. -25.5%2.25 gives 1.5 .
division
Shifts the bits of the number
to the left by the number of 2 << 2 gives 8. - 2 is represented by
bits specified. (Each number 10 in bits. Left shifting by 2 bits
<< Left Shift
is represented in memory by gives 1000 which represents the
bits or binary digits i.e. 0 and decimal 8.
1)
11 >> 1 gives 5 - 11 is represented in
Shifts the bits of the number
Right bits by 1011 which when right
>> to the right by the number of
Shift shifted by 1 bit gives 101 which is
bits specified.
nothing but decimal 5.
Bitwise
& Bitwise AND of the numbers 5 & 3 gives 1.
AND
Bit-wise
| Bitwise OR of the numbers 5 | 3 gives 7
OR
Bit-wise
^ 5 ^ 3 gives 6
XOR
Bit-wise The bit-wise inversion of x is
~ ~5 gives -6.
invert -(x+1)
Returns whether x is less than
y. All comparison operators
return 1 for true and 0 for 5 < 3 gives 0 (i.e. False) and 3 < 5
Less false. This is equivalent to the gives 1 (i.e. True). Comparisons can
<
Than special variables True and be chained arbitrarily: 3 < 5 < 7
False respectively. Note the gives True.
capitalization of these
variables' names.
5 < 3 returns True. If both operands
Greater Returns whether x is greater are numbers, they are first converted
>
Than than y to a common type. Otherwise, it
always returns False.
Less Returns whether x is less than
<= x = 3; y = 6; x <= y returns True.
Than or or equal to y
Operator Name Explanation Examples
Equal To
Greater
Returns whether x is greater
>= Than or x = 4; y = 3; x >= 3 returns True.
than or equal to y
Equal To
x = 2; y = 2; x == y returns True. x =
Compares if the objects are
== Equal To 'str'; y = 'stR'; x == y returns False. x
equal
= 'str'; y = 'str'; x == y returns True.
Not Compares if the objects are
!= x = 2; y = 3; x != y returns True.
Equal To not equal
Boolean If x is True, it returns False.
not x = True; not y returns False.
NOT If x is False, it returns True.
x = False; y = True; x and y returns
False since x is False. In this case,
x and y returns False if x is Python will not evaluate y since it
Boolean
and False, else it returns knows that the value of the
AND
evaluation of y expression will has to be false (since
x is False). This is called short-
circuit evaluation.
x = True; y = False; x or y returns
Boolean If x is True, it returns True,
or True. Short-circuit evaluation applies
OR else it returns evaluation of y
here as well.

Operator Precedence 0A ‫ ا‬$


7 ‫أ‬
!‫ب ؟‬b‫!< ا‬Q9 ‫ أم‬A‫! أو‬3‫!< ا‬Q9 ‫  م‬C‫ ه‬2 + 3 * 4 C8 9G <!@ @ HQ ;‫إذا آن ی‬
‫ب‬b‫ أن @!< ا‬TQ ;‫ – ذ‬A‫ إﺝاؤه أو‬23‫ب ی‬b‫ﻥ أن @!< ا‬H]‫ﻥ ی ی‬8‫( ا!ر) ا‬6 ‫ت‬Z‫@< ای‬
.addition operator !3‫ @!< ا‬higher precedence N<@‫ أ‬H)‫ أ‬5 multiplication

8‫ )أآ‬N<@P‫ ا‬H)P‫ إ^ا ( ﺙ ا‬C


‫ )أ‬N‫دﻥ‬P‫ ا‬H)P9 ‫ءا‬9 ‫ ن‬8‫ی‬9 (6 C Q‫ ا‬H)9 ‫ن‬9 gQ‫ول ا( ی‬3‫ا‬
.( ‫إ^ا‬
N<@P‫ ا‬C ‫ ا‬Q‫ ا‬CH
‫ول‬3‫( ا‬6 N‫دﻥ‬P‫ ا‬C ‫ ا‬Q‫ب
! ا‬GM9 ‫ ن ) م‬8‫ی‬9 ‫ن‬h6 ‫ة‬gQ!‫ات ا‬HQ‫ ا‬C.‫ا‬9 U‫ أﻥ‬TQ ;‫ذ‬
.‫ول‬3‫! ا‬L
(6

;" ،‫ ا!ل‬C‫< أﺝ‬Q‫ زیدة ا‬CH) N<@ ; 5g@‫ ن ( أ‬8‫ی‬9 (6 ‫ و ﺝ‬C‫ د‬U‫و وآﻥ‬H‫ول ا( )ی‬3‫ا‬
.U9 ;M`‫أﻥ‬
C Q< H)P‫ی ا‬M operators and operands ‫ت‬d Q!‫ا‬/‫ود‬M‫ وا‬C ‫ ا‬Q‫! ا‬3 ( ) ‫
اس‬P‫ا)]م ا‬
8‫ أآ‬C9 ‫د‬M 2 + (3 * 4) ،‫ل‬8!‫ ا‬CH) N<@ .‫( ااءة
ر ا ن‬6 C5)‫; أ‬3 ‫ﻥ‬9 CQ3‫ ح و‬Z 9
 Z‫و‬
‫ة‬L‫>( أن  ن زا‬H‫ ﻥ‬A‫ﻥ و‬dQ9‫ب و‬GM9 ‫]م‬G ‫ أن‬23‫
اس ) ( ی‬P‫ ا‬.;‫ ذ‬Q9 ‫ ﺵ(ء‬C‫ وه"ا آ‬. 2 + 3 * 4
.( (4+3)+2 C8 ) .M‫@ ا‬
Table 5.2. Operator Precedence
Operator Description
lambda Lambda Expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in Membership tests
is, is not Identity tests
<, <=, >, >=, !=, == Comparisons
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
*, /, % Multiplication, Division and Remainder
+x, -x Positive, Negative
~x Bitwise NOT
** Exponentiation
x.attribute Attribute reference
x[index] Subscription
x[index:index] Slicing
f(arguments ...) Function call
(expressions, ...) Binding or tuple display
[expressions, ...] List display
{key:datum, ...} Dictionary display
`expressions, ...` String conversion

.d‫` ل ا‬4‫( ا‬6 5‫ ) ﺵ‬5d. !‫ ا(  ﻥ‬C ‫ ا‬Q‫ا‬
- ‫ و‬+ ‫ل‬8!‫ ا‬CH) N<@ .Td@‫ أ‬H!‫ول ا‬3‫( ا‬6 !L‫( ا‬6 `‫ ا‬p4‫( ﻥ‬6 cQZ‫ و‬H)P‫( ا‬6 ‫وی‬G!‫ ا ا‬Q‫ا‬
.H)P‫ ا‬p4‫! ﻥ‬5

:Order of Evaluation
(‫ ا‬H)P‫"; إذا أردت > أ  ا‬..q‫ ا‬CH
H)‫ أ‬U C @ ‫ یر أي‬C ‫ ا‬Q‫ ا‬H)‫ن ﺝول أ‬h6 ‫؛‬Z‫ا‬6‫ا‬
<!@ CH
! !3‫ل إذا أردت أن  ن @!< ا‬8!‫ ا‬CH) N<@ ،( ) ‫
اس‬P‫ ی!; ا)]ام ا‬5! ‫ي‬3‫ی‬
. (2 + 3) * 4 : C8 ‫
اس‬P‫ات |" ی! ا)]ام ا‬HQ‫( أ ا‬6 ‫ب‬b‫ا‬

: Associativity FH‫ر‬A‫ا‬
. !‫ ا‬N‫ر إ‬G‫ ا‬e!9  H)P‫( ا‬6 ‫ ا!آ‬C ‫ ا‬Q‫ل ا‬8!‫ ا! آ‬N‫ر إ‬G‫ @دة ا‬gH!‫ ا‬C ‫ ا‬Q‫ا‬
g9‫ ا‬C!M ``]!‫ ا‬C ‫ ا‬Q‫ ا‬C8 C ‫ ا‬Q‫ ا‬kQ9 .(2 + 3) + 4 C8  + 3 + 42 ‫ل‬8!‫ ا‬CH) N<@
a = (b = c). ‫ره‬H@9 C Q a = b = c :C8 ‫ر‬G‫ ا‬N‫ا! إ‬

Expressions ...‫!ات‬$7A ‫ا‬


‫ات‬HQ‫ا)]ام ا‬
Example 5.1. Using Expressions

#!/usr/bin/python
# Filename: expression.py

length = 5
breadth = 2

area = length * breadth


print 'Area is', area
print 'Perimeter is', 2 * (length + breadth)

Output : b‫ﺕ‬D ‫ا‬

$ python expression.py
Area is 10
Perimeter is 14

: 0 A `$‫آ‬
>!‫( ا‬6 !59Z C ‫]^ی‬9 !
‫ ﺙ‬breadth >!‫ ا‬N‫ إ‬2 !‫ و ا‬Length >!‫ ا‬N‫ إ‬5 !‫}أ)ﻥ ا‬
{(area = length*breadth) (‫ آ‬i8‫ا‬
M‫ وﻥ‬.)A‫ ا‬p4‫ ﻥ‬5Lg@h9 ( breadth--length ) ‫ ا!>ات‬N‫ إ‬G ‫ ]^ن أو‬CgG!‫ ل و@ض ا‬F !

@ HQ‫ ا‬3‫]^ی ﻥ‬9 ‫ ) م‬.‫ات‬HQ‫@ة ا‬G!9 CgG!‫ ا‬eM ‫ و‬G ‫ب‬GM 5 ]G‫ﻥ‬
‫ﻥ‬h6 ،‫ﻥ‬8‫ ا‬UM‫( ا‬6  ‫ ا‬.print ‫ن‬H‫)]ام ا‬9 U@Hg9 ‫ ﺙ ﻥ م‬area (6 >!‫( ا‬6 length * breadth
.print Y‫ا`ی‬/‫ن‬H‫( ا‬62 * (length + breadth) HQ‫)]ام
! ا‬9 ‫ﺵة‬H

'Area is' 9 6G ‫د‬M‫ اﻥ  ﻥ‬W‫ ور‬.R‫@ ا‬HF (6 9‫ﻥ


 وﺝ!ل ا‬9 ‫ ن‬8‫ی‬9^!‫_ آ ی‬A ،b‫أی‬
.area >!‫وا‬
‫ی‬g‫ ا‬T"59 ‫< <اءة‬9
8‫ أآ‬R ‫ﻥ‬H‫ ا‬YH`‫ وی‬g‫ و‬m‫ ﻥ‬R‫ ﻥ‬N<@ ‫` ل‬M‫ ا‬C‫  آ"; أﺝ‬5Qb‫ ن ی‬8‫ی‬9
R H!< HG9 <5) ‫ة‬M‫ ا‬CQ3‫ ن ی‬8‫ی‬9 ‫( أن‬4‫ آ‬N<@ ‫ل‬8 ‫ ( وه"ا‬R‫( ا‬6 ‫ت‬6G!‫ ل ا‬n<9 Q‫) |"  ﻥ‬
. !!

:‫ﺹ‬:I ‫ا‬
expressions ‫ات‬HQ‫ وا‬operands ‫ت‬d Q!‫ا‬/9GM‫ود ا‬M‫ وا‬operators C ‫ ا‬Q‫ إ)]ام ا‬4‫ آ‬c‫
 رأی‬
.R ‫ﻥ‬9 ‫ أي‬Blocks C‫ آ‬N<@ ))‫( أ‬H (‫وا‬
.‫ﻥت‬H‫ا‬/ Y‫)]ام ا`ری‬9 3 ‫ا‬9 (6 5 ‫)]ا‬9 ‫! ی<( )ى آ ﻥ م‬6‫و‬
‫دس‬1 ‫ ا‬02, ‫ا‬
Control Flow

‫ﺝ ول ا ت‬
Introduction ....................................................................................................................... 
The if statement ......................................................................................................" if " ‫ن‬H‫ا‬
Using the if statement ..................................................................................." if " ‫ن‬H‫ا)]ام ا‬
How It Works ..............................................................................................................C!Q‫آ ی‬
The while .......................................................................................... " while " ‫ن‬H‫ا‬
statement
Using the while statement ......................................................................." while " ‫ن‬H‫ا)]ام ا‬
The for ........................................................................................................ " for " <M‫ا‬
loop
Using the for statement ............................................................................." for " ‫ن‬H‫ا)]ام ا‬
The break statement ..........................................................................................."break " ‫ن‬H‫ا‬
Using the break statement ........................................................................"break " ‫ن‬H‫ا)]ام ا‬
The continue statement.................................................................................." continue " ‫ن‬H‫ا‬
Using the continue statement ............................................................." continue " ‫ن‬H‫ا)]ام ا‬
Summary ...................................................................................................................d]‫ا‬

: 
 *
p4‫( ﻥ‬6 5 d‫" آ‬4‫ص ی‬d.h9 ‫ ﻥ و‬9 ‫ ن‬8‫ی‬9‫ﻥت ؛ و‬H‫ ا‬C)d) !L‫ن هك دا‬A‫ ا( رأیه ا‬R ‫ا‬H‫( ا‬6
‫ اارات وام‬kQ9 ‫ ا]ذ‬R ‫ﻥ‬H‫ ی ا‬، ‫ل‬8!‫ ا‬CH) N<@ C!Q‫ب ا‬G‫ی اﻥ‬F > ‫ ذا  اردت‬. P‫ا‬
' Good Evening ' ‫ ' أو‬Good Morning ' !<‫@ آ‬HF C8 4<]!‫ <! ا
 ا‬QH 4<] ‫ﺵء‬9
‫ن ا م ؟‬q‫ ا‬c
‫  ه ا‬N<@ ;‫ ی
 ذ‬i
if –for -while : C8 ‫ ن‬8‫ی‬9 (6 ‫ﻥت‬H‫ ا‬n6 M ‫ أداة‬N<@ ‫ة‬gG‫ ا‬N<@ 
‫ن ذ; ی‬h6 c‫ أﻥ‬c!. !‫وآ‬

: The "if" statement

‫ﻥ‬h6 A‫ وإ‬، (if-block N!G) ‫ﻥت‬H‫ آ< ا‬C>‫ﻥ ﻥ‬h6 ، true ‫ وإذا آن اط‬، ‫ اط‬nM< ‫]م‬G‫ی‬
. ‫ري‬.‫ " ا‬else " H‫ وا‬.( else-block N!G) ‫ﻥت‬9 < ‫ى‬.‫ @!< ا‬C>‫ﻥ‬
Example 6.1. Using the if statement

#!/usr/bin/python
# Filename: if.py

number = 23
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
print 'No, it is a little higher than that' # Another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that'
# you must have guess > number to reach here

print 'Done'
# This last statement is always executed, after the if statement is executed

Output

$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done

: 0 A `$‫* آ‬

>!‫ أ)ﻥ ا‬M‫ ﻥ‬.‫د ا"ي ی‬Q‫  اذا آن ه"ا ه ا‬6Q ‫]م و‬G!‫" ا]!ت ا‬.‫ﻥ ﻥ‬6 ، R ‫ﻥ‬H‫( ه"ا ا‬6
‫)]ام اا‬9 ‫]م‬G!‫"ﻥ ]! ا‬.‫ أ‬، ‫ ﺙ‬.23 ‫ أن ی ل‬T‫ اﻥ ﻥی ﻥی‬YM ‫ أي @د‬N‫" إ‬number"
raw_input ().
.(‫ ا‬C`4‫( ا‬6 5@ ‫!^ی ااءة‬9 ‫ ) م‬.5 ‫ ی! ا@دة ا)]ا‬R ‫ا‬H‫ أﺝ^اء ا‬A‫اوال  ه( إ‬
‫ت‬d.!‫ ا‬m‫ اﺵ و‬N‫ إ‬-‫@ ا‬Hg9 ‫ " وا(  م‬raw_input " 3 !‫ء ﺝ!< ﻥ` <ا ا‬g@h9 !

 (6 (‫ت وه‬d.!‫ إ اا ا‬Q enter N<@ e>b‫ أي ﺵ(ء وا‬C.‫د أن ﻥ‬3!9‫ و‬. ‫]م‬G!‫ ا‬
" >!‫( ا‬6 U‫ " ﺙ ﻥ]^ﻥ‬int " ‫)]ام‬9 YM ‫ @د‬N‫ إ‬-‫ ل ه"ا ا‬M‫ه ی‬Q9 ، -‫رة @ ﻥ‬H@ raw_input
gG ;‫ن أﻥ‬q‫  ا‬U6Q ‫<; أن‬Q@  C‫ " و آ‬class " HF @ ‫رة‬H@ " int " ‫ أن‬M‫( ا‬6 . " guess
. ( -‫ ا‬C.‫ دا‬YM ‫ @د‬N<@ ‫ ي‬M <!3‫ض أن ا‬6 N<@ ) YM ‫ @د‬N‫ إ‬-‫ ا‬C‫ ی‬M
R H‫ ی م ا‬U ‫وی‬G ‫ذا آن‬h6 . "23" T‫ﻥ‬.‫د ا"ي ا‬Q‫ ا‬9‫]م و‬G!‫ " ا‬guess " !] 9 ‫ ذ;
رﻥ‬Q9
(! (‫ﻥت ا‬H‫ ن @ ا‬8‫ی‬9 H] indentation <8< ‫ یت‬G ‫]م‬G‫_ أﻥ ﻥ‬A، <!Q‫ح ا‬39 )‫@ ر‬Hg9
tab g>Z " :‫
@ة‬N<@ cH8 ;‫ أﻥ‬N!‫ أ‬. ‫ ن‬8‫ی‬9 (6 indentation <8‫ أه! ا‬2H) H‫ ذ; ی‬. <‫ أی آ‬N‫إ‬
!‫ ذ; ؟‬CQ4) C56 . " indentation level C ‫واة‬
block <‫ن آ‬9 ‫ ن‬8‫ی‬9 N‫ ﻥ إ‬M‫ی – ﻥ‬5‫( ا‬6 colon (:) N<@ ‫ ي‬M‫ " ی‬if " ‫ن‬H‫_ أن ا‬d‫ﻥ‬
. ‫!ة‬G ‫ﻥت‬H‫ا‬
"elif " H‫ @< ه ه ا)]ام ا‬23‫  ی‬."٢٣" ‫د‬Q‫ ذ; ا‬H‫ أو أآ‬C
‫ آ ن ا]! أ‬nM9 ‫ ذ; ﻥ م‬Q9
. if-elif-else ‫ن ك‬9 C.‫ دا‬elif-else : if ‫
 ب‬d@ ‫ﻥت ذوي‬H‫ اﺙ ا‬9 !3‫ا"ي ی‬
. 9 <g!‫ ا‬indentation !‫ آ‬C<‫ وی‬C5)‫ أ‬R ‫ﻥ‬H‫ ا‬CQ3‫ذ; ی‬
‫ﻥت‬H‫ @ آ< †ة ا‬H (g!‫ ا‬gG‫ی ا‬5‫( ﻥ‬6 (:) N<@ ‫ ي‬M else ‫ و‬elif ‫ﻥت‬9 ‫  ن‬c‫ أﻥ‬23‫ی‬
(! Hg9 2)!‫< ا‬8‫) ا‬
. if ‫ ـ‬C.‫ن ا!ا‬H‫ ا‬N!G ‫ وه"ا‬if-block <‫ آ‬C.‫ى دا‬.‫ " ة أ‬if " ‫ن‬H‫ ا‬9‫* ی!; آ‬
: ‫ ه‬if ‫ن‬H‫ ا‬9‫  ی! آ‬C
‫ وأ‬. ‫ری‬.‫ " ا‬else " ‫ " و‬elif " ‫ﺝ^اء‬P‫* "آ ان ا‬

if True:
print 'Yes, it is true'

<g 2‫ ﺙ ی"ه‬، " else " ‫ " و‬elif " 5M ‫ د ا!رﺝ‬H‫ ا‬56 !9 d ‫ آ‬if ‫ن‬H‫" ا‬4 ‫ ن‬8‫ی‬9 (5‫  ی‬Q9
5<‫أ ﺙ ی‬H R ‫ﻥ‬H‫" ا‬4 (‫ ا‬GL‫ ا< ا‬M‫ ا‬T"‫( ه‬6 ‫ و‬. if ‫ﻥت‬H‫ یت آ< ا‬M (‫ن ا‬H‫ ا‬N‫إ‬
. FGH‫ ا‬N5!9 U5‫ ﺙ ی‬v ‫ و ﺙ‬R ‫ﻥ‬H‫ی ا‬5‫ ن ﻥ‬8‫ی‬9 ‫ه یى‬Q9 . print 'Done' ‫ن‬H‫ا‬

‫( ه"ا‬6 N 5md ‫ﺵء ا( @<; أن‬A‫ ا‬8‫ ا‬N‫ هك إ‬c5H‫ و ﻥ‬، ‫ ﺝا‬eG9 R ‫ﻥ‬H‫ ان ه"ا ا‬W‫ور‬
3 9 4<]9  ‫و|; اد‬P HG9 —‫ﺝ‬4 C9 gG9‫ ) و‬gG9‫ أﺵء ﺝ!< و‬5<‫ آ‬T"‫ وه‬.eGH‫ ا‬R ‫ﻥ‬H‫ا‬
‫ و) ف‬5Q  YH` ‫ ) ف‬، ;‫ ذ‬Q9 ‫ و‬، ‫ایت‬H‫ ا‬T"‫ ه‬C ‫ رآ‬YH` ‫ ; ان‬2<g‫ ( وی‬C/C++ >
. ;‫ ی‬6  5‫ﻥ‬9

3 H + + ‫ ج‬/ R< Umd


kQ9 (6‫ )و‬UG4‫ ا(ء ﻥ‬CQ4 ‫ ان‬N‫ ا‬.o ‫ن‬9 ..‫ اذا‬elif.. ‫ ی!; ا)]ام اى‬.‫ ن‬8‫ی‬9 ‫ن‬9 (6 C‫ی‬H ‫ هك أي‬p
)@G9 ;‫ ذ‬CQ4 ‫ ا)]ام ا س‬، ‫ت‬AM‫ا‬

C/C++ /!7 9‫ﺡ‬:


do it quickly) ‫ﻥت‬H‫  ی! ا)]ام ا‬. ‫ ن‬8‫ی‬9 (6 switch statement C‫ی‬H‫ن ا‬9 ‫ هك‬p
( ‫ )ی‬C9 ;‫ ذ‬C!Q ‫]م ا س‬G‫ ی‬، ‫ت‬AM‫ ا‬kQ9 (6‫ ا(ء ) و‬p4‫ ﻥ‬C!Q if..elif..else

The while statement ..... " while " ‫ن‬$7 ‫* ا‬


‫ " ه‬while " ‫ن‬H‫ ا‬.true  ‫ﻥت  دام اط‬H‫" آ< ا‬49 ‫ ; ارا وارا‬Y!G‫ " ی‬while " ‫ن‬H‫ا‬
" ‫ري وه‬.‫ ا‬9 C!‫ " ی! أن ی‬while " ‫ن‬H‫ ا‬. looping statement " n<M‫ اوی" أو ا‬N!G‫ل ! ی‬8
" else
: " while " ‫ن‬H‫ا)]ام ا‬

#!/usr/bin/python
# Filename: while.py

number = 23
running = True
while running:
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
# Do anything else you want to do here

print 'Done'

Output : b‫ﺕ‬D ‫ا‬

$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬

U!]9 ‫ظ‬4A9 U Y!G‫]م ی‬G!‫  ا!^ة ه ه أن ا‬، guessing !]‫ ا‬HQ 2Q<‫  ز ﻥ‬، R ‫ﻥ‬H‫( ه"ا ا‬6
- 9) (6 <Q6 !‫ آ‬- !] C‫ ارا وارا @ آ‬R ‫ﻥ‬H‫" ا‬4 N‫ هك ﺝ ا‬cG MM U!] ‫ إذا آن‬A‫إ‬
: " while " ‫ن‬H‫ ح ا)]ام ا‬Z 9 ‫ ه"ا یح‬.
>!‫< ا‬Q‫ وﺝ‬. while loop <M‫ ا‬C.‫ دا‬N‫" ا‬if " ‫ " و‬raw_input " ‫ن‬H‫ی; ا‬M9 !
. •
. while loop <M‫ ا‬C> CH
true N‫ " إ‬unning "
‫ﻥت‬9 <‫ل آ‬d. ‫" @!< ا!رﻥ‬4 (6 @‫ه ﺵ‬Q9‫و‬True " unning " >!‫ ا‬CQ39 !
: A‫• أو‬
. while-block
" >!‫ ه ا‬M‫ ا‬T"‫( ه‬6 ‫ اط ة ﺙﻥ و‬nM‫ ی ا‬، while-block <‫ ا‬T"‫" ه‬4 Q9 •
. "running
<‫" ا‬4 C‫ ا‬G6 A‫ وإ‬، ً‫" ﺙﻥ‬while-block"‫ن‬H‫" آ< ا‬49 ‫ ﻥ م‬،" true" U >!‫ذا آن ا‬h6 •
. (‫ن ا‬H‫ ا‬N‫ إ‬C‫ه ﻥ‬Q9‫" و‬else-block" ‫ری‬.A‫ا‬
!9‫" – ور‬False" |F. while loop <M‫( ا‬6 ‫ اط‬YH`‫ @  ی‬else block <‫" ا‬4 ‫ي‬3‫• ی‬
56 ،" while loop" <M< " else" H‫ إذا آن هك ا‬. ‫ اط‬nM‫ ا‬56 ‫ أول ة ی‬T"‫ ن ه‬
. 5 ‫ون ا]وج‬9 9P‫ ا‬N‫)!ار إ‬9 ‫ وا( ر‬while loops ‫ إذا آن ی; <ت‬A‫! إ‬L‫" دا‬4‫ی‬
‫و‬. (‫ ا ا‬N<@ 0‫ و‬1 ! A‫د‬Q ‫ه‬HQ ‫ وی!; ان‬Boolean types N!G " False" ‫" و‬True" •
.1 C8 U<Q4‫ ا‬U!‫ ه ا‬5!‫ ا‬p‫! و‬5 nM‫! ی ن اط او ا‬8 5!Q)‫ ا‬5!‫ا‬

<‫ ا‬p4‫( ﻥ‬6 5 Q9‫ﻥت ا‬H‫ ا‬Z‫ﺝ @  ی!; و‬M‫ة @ ا‬L‫ زا‬CQ49 ‫ "  ن‬else-block " •

. ‫ﺙ‬P‫ ا‬p49 while ‫ أن  م‬Q9 (while statement ‫ن‬H‫(  ا‬6 !‫)آ‬

C/C++ /!7 9‫ﺡ‬:


" while" <M< "else" H‫ ا‬N<@ ‫` ل‬M‫ ی!; ا‬U‫"آ أﻥ‬

: The for loop .... " for" 


- ‫ا‬

objects ‫ت‬L‫ ا‬Q9 <G<) ‫ل‬d. ‫ وه( ر‬.o looping (< ‫ن‬9 (‫ن ه‬H‫( ا‬6 " for"
C‫ آ‬A ‫` ل‬6 (6 C`49 C)dG‫ ا!^ی @ ا‬N<@ ‫ف‬Q)‫ و‬.UQ9 <G<) (6 `@ C‫ل آ‬d. ‫ ا"هب‬C8
.9 <g!‫ ا‬Q‫! @ ا‬3 ‫د‬3 ‫ ه‬9‫ن ه ان ا‬q‫ ا‬U6Q ;<@ 

Example 6.3. Using the for statement

#!/usr/bin/python
# Filename: for.py

for i in range(1, 5):


print i
else:
print 'The for loop is over'

Output

$ python for.py
1
2
3
4
The for loop is over

: b‫!ﻥ‬7 ‫ ا‬0 G $,$‫** * آ‬


" <.‫)]ام اا اا‬9 ‫@اد‬P‫< ا‬G<G‫ وﺵ>< ا‬. ‫@اد‬P‫< ا‬G<) @Hg9 !
‫ ؛‬R ‫ﻥ‬H‫( ه"ا ا‬6 •
"range
‫• أ)ﻥ ا!ى ا! ن ر
! )‪ (5،1‬إ‪ N‬ا!> "‪"i‬‬
‫• 
! ‪ U9‬ه أﻥ أ@‪ Tg‬ر
! "‪ !59 "5،1‬ى ی‪ <G<)  Q‬ا‪P‬ر
م ‪9‬ای ا‪Q‬د ا‪P‬ول ﺙ‬
‫ی`@ ‪ N‬ی`‪ C‬إ‪ N‬ا‪Q‬د ا‪8‬ﻥ( @<‪ CH) N‬ا!‪8‬ل ‪ (5،1) @ !3 ،‬ی‪  <G<) (gQ‬ﻥ ]‪[4،3،2،1‬‬
‫ا‪6‬ا‪ ، Z‬ی‪ ".‬ا!ى ‪ g. range‬ة ‪ 1 !9‬واذا أ@‪@ Ug‬دا ﺙ‪  8‬ن ا]‪ g‬ة ‪!9‬ار ذ; ا‪Q‬د‪N<@ ،‬‬
‫)‪ CH‬ا!‪8‬ل‪ :‬ا!‪ i} .[3،1] (gQ (2،5،1) @ !3‬أن ا!ى ‪ `M‬ر ‪ 1,5 9‬وا‪^4‬ة أو ا]‪ g‬ة ‪!9‬ار‬
‫‪.‬‬ ‫‪"{ 2‬آ ان ا!ى ی! ‪ N‬ا‪Q‬د ا‪8‬ﻥ(‪ -‬أي إﻥ‪ A U‬ی!‪ C‬ا‪Q‬د ا‪8‬ﻥ(‬
‫‪ Q9‬ذ; ر ا‪d. "for" <M‬ل ذ; ا!ى‬ ‫•‬
‫‪for i in range(1, 5):‬‬
‫‪Q‬دل ‪:‬‬
‫‪for i in range [1, 2, 3, 4]:‬‬

‫• وا( ‪ UH‬إ)د آ‪@ C‬د ) أو آ‪ ( object- L‬إ‪ N‬ا!> ‪ i‬واا ‪ (6 5‬آ‪ C‬ة ‪Q9 ،‬ه ی ‪ "4‬آ<‬
‫ا‪H‬ﻥت ‪ !
C‬ـ ‪. i‬و ‪ (6‬ه"‪ T‬ا‪ M‬ﻥ م ‪ @Hg9 e6‬ا! ‪ (6‬آ< ا‪H‬ﻥت ‪.‬‬
‫• "آ أن ا‪^3‬ء "‪ " else‬ا‪.‬ري ‪ .‬و! ی‪b‬ف ‪ 56،‬دا‪ !L‬ی‪ 6 "4‬ر اﻥ‪5‬ء ا‪" for" <M‬إ‪ A‬إذا ث‬
‫‪.‬وج `ق ‪ 9‬ا)‪ g‬ا‪H‬ن " ‪ )} "break‬ﺵ‪.{C<
Q9 U‬‬
‫• "آ ان "‪ " for‬دا‪ C.‬ا‪ <M‬ااری ‪ C!Q loop‬اي )<‪ .<G‬وه ی
‪ !L‬ا‪@P‬اد ی ><‪ 5‬‬
‫‪d.‬ل اا اا‪، " range" <.‬و @<‪ N‬ا‪ !Q‬م ی! ا)]ام أي ﻥ ع ا‪P C)dG‬ي ﻥ ع ا‪L‬ت‬
‫}أو ا‪ . !{ Q‬و) ف ﻥ م ‪9‬ح ‪ T"5‬ا‪4‬ة ‪ (6 C`49‬ا‪ `4‬ل اد  ‪.‬‬

‫‪:‬ﺡ‪C/C++/Java/C# /!7 9‬‬

‫ا ‪ + " for" 
-‬ﺏ‪6‬ن ﺕ‪ `-I‬ا‪ +:%‬ﺝر ‪ C# /!7. C/C++/ G‬ف ‪:‬ﺡ?ن ان ه‪ E‬ا ‪+ 
-‬‬
‫ﺏ‪6‬ن ‪N‬ﺏ* ‪ Java /!7 .C# + " foreach " 
-‬ف ‪:‬ﺡ‪9‬ن أ‪ M‬ان ﻥ‪ Q,‬ا ‪+ K7N‬‬
‫ا ‪7A‬رة ‪:‬‬
‫")‪ " for (int i : IntArray‬د  ‪Java 1.5‬‬
‫‪ ، C/C++ (6‬إذا اردت ان ‪9 (46، " for (int i = 0; i < 5; i++)" 2‬ی‪ 8‬ن ‪e6 2‬‬
‫" )‪ . "for i in range(0,5‬وآ! ون ‪6 ،‬ن آ‪ 9‬ا‪9 (6 <M‬ی‪ 8‬ن اآ‪ FG9 8‬وا
‪HQ C‬ا‬
‫و@‪.g]< Z‬‬

‫* ا ‪$7‬ن " ‪The break statement " break‬‬


‫‪ : break statement‬ی‪]G‬م ‪ (6‬ا]وج ا‪ <M‬ااری ‪.‬آ!‪8‬ل؛ و
 ‪ "4‬ا‪ N <M‬و  ی`‪ YH‬ﺵط‬
‫ا‪ False <M‬أو أن )<‪ <G‬ا‪  Q‬ر‪. C 9‬‬
‫ ا!‪md‬ت ا!‪ !5‬أﻥ; 
!‪]9 c‬وج < "‪ " for‬أو " ‪ "while‬؛ ‪h6‬ﻥ‪  C8!9 U‬ی ‪ "4‬آ< ا‪H‬ﻥت‬
‫ا]ص ‪9‬ــ " ‪. "else‬‬
break statement ‫ ام‬I ‫* ا‬
Example 6.4. Using the break statement

#!/usr/bin/python
# Filename: break.py

while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'

Output

$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 12
Enter something : quit
Done

: b‫!ﻥ‬7 ‫ ا‬0 G $,$‫* آ‬


.‫ ة‬C‫( آ‬6 <.!‫ ا‬length QHF ‫]م ارا وارا ﺙ‬G!‫ت ا‬d.!‫" ا‬.9 !
R ‫ﻥ‬H‫( ه"ا ا‬6 •
"quit " ‫]م ه‬G!‫ ا‬C.!‫  إذا آن ا‬-M6 ‫ل‬d. R ‫ﻥ‬H‫ 
 ا‬. F‫ ﺵ‬6 9 !

‫و‬
. R ‫ﻥ‬H‫ی ا‬5‫ ﻥ‬N‫< وا  ل إ‬M‫ ا]وج ا‬n‫ی‬F @ R ‫ﻥ‬H‫ ا‬C!@ 4
‫وأو‬
. "len" <.‫)]ام اا اا‬9 {‫ت‬6G!‫ ا‬56 !9 ‫وف‬M‫ }@د ا‬C.!‫ ا‬gG‫ ل ا‬F ‫• ی! اآف‬
. ‫ ﺝ‬C9 " for" <M‫ ا‬5 ‫ ی! ا)]ا‬break statement ‫• "آ أن‬

!! ‫ ن‬8‫ی‬H " G2" Q‫


`ة ﺵ‬
" G2's Poetic Python" : 5!)‫ و‬5H‫ی >ة
 آ‬Q‫ @ ﺵ‬g ‫ل‬8!‫( ه"ا ا‬6 c ])‫ا‬
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
: The continue statement

C‫ وی ا‬M‫< ا‬M‫( آ< ا‬6 ‫  ورد‬9 Ng]‫ن ی‬9 ‫ ن‬8‫ی‬9 ‫غ‬d9‫( إ‬6 The continue statement ‫]م‬G‫ی‬
. <M‫ار ا‬
Example 6.5. Using the continue statement

#!/usr/bin/python
# Filename: continue.py

while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...

Output

$ python continue.py
Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit

: b‫!ﻥ‬7 ‫ ا‬0 G $,$‫* آ‬

 ])‫"ا ا‬. C
P‫ ا‬N<@ ‫ أف‬3 ‫ @  آن‬e6 T‫"ﻥ‬4‫ و ﻥ‬، ‫]م‬G!‫ ا‬C.!‫< ا‬H
‫ ؛‬R ‫ﻥ‬H‫( ه"ا ا‬6 •
9 skip C!Q9 ‫ أف ؛ ﻥ م‬3 C
‫ ل أ‬g‫ذا آن ا‬h6، ‫رة‬HQ‫ ل ا‬F N<@ ‫` ل‬M< " len" <.‫اا اا‬
، ‫"ه‬4 ‫< وا( ی‬M‫( ا‬6 ‫ﻥت‬H‫ ا‬9 ‫ف‬d]9 ." continue" ‫رة‬HQ‫)]ام ا‬9 <‫( ا‬6 ‫ن ا! ﺝ د‬H‫ا‬
.‫!<ت ه‬Q‫ ا‬T‫ أي ﻥ ع ﻥی‬C!@ !‫وی‬
. ‫ ﺝ‬C9 " for" <M‫ ا‬C!Q " continue" ‫رة‬H@ ‫ﺝ_ أن‬A •

: ‫ﺹ‬:I ‫* ا‬

gH!‫ﻥت ا‬H‫ ( و ا‬if – while – for ) : ‫ﻥت‬H‫ ا‬n6 (6 M< ‫ث أدوات‬d‫ ا)]ام ﺙ‬4‫
 رأی آ‬
ً 4® ‫ ن ؛ وآ ﻥ;  و‬8‫ی‬9 (6 ‫]  @دة‬G!‫ﺝ^اء ا‬P‫ ا‬8‫ أآ‬kQ9 ;<‫و‬.( continue ‫ و‬break) 59
. functions ‫]م اوال‬G‫! ی<( ؛ )ى آ ﻥ— وﻥ‬6‫ و‬. ‫وري‬Z  ‫ أ‬5Q
"‫ﺏ‬1 ‫ ا‬02, ‫ا‬
Functions ‫ا وال‬

‫  ا ت‬

Introduction................................................................................................  
Defining a Function ........................................................................... ‫ی دا‬Q
Function Parameters ..................................................................... ‫را ات اا‬9
Using Function Parameters ................................................‫را ات اا‬9 ‫ا)]ام‬
Local Variables ........................................................................... <M!‫ا!>ات ا‬
Using Local Variables........................................................ <M!‫ا)]ام ا!>ات ا‬
Using the global statement ....................................................( !Q‫ن ا‬H‫ا)]ام ا‬
Default Argument Values..................................................... Z‫ا‬6A‫ ا‬g) ‫ا ا‬
Keyword Arguments.......................................................... g) ‫ ا‬4!‫ا<!ت ا‬
Keyword Arguments ................................................g) ‫ ا‬4!‫ا)]ام ا<!ت ا‬
The return statement......................................................................." return" ‫ن‬H
Using the “ literal ” statement ................................." literal" (6M‫ن ا‬H‫ا)]ام ا‬
Docstrings .........................................................................................‫ ن‬8‫ی‬9 nL‫وﺙ‬
using Docstrings..................................................................................................
summary.................................................................................................... d.

: 
*
;!‫ وی‬، ‫ﻥت‬H‫ء ا)  < ا‬g@h9 ; Y!G 5‫ اﻥ‬.5 ‫ ی! إ@دة ا)]ا‬R ‫ا‬H‫ ه( أﺝ^اء ا‬: ‫اوال‬
M‫وﻥ‬. ‫ ا)@ء اا‬N!G‫! ی‬9 ‫ف‬Q‫وه"ا ی‬.‫ واي @د ا!ات‬، R ‫ﻥ‬H‫( اي ن ا‬6 <‫ ا‬T"‫ا)]ام ه‬
.”range" ‫” واا‬len" ‫ اا‬C8 <.‫ اوال اا‬bQ9  ])‫
 ا‬CQ49
!9‫ وا( ر‬، () d5‫
اس ا‬P‫ف <ا ﺙ زوﺝ ا‬Q!‫) ا‬9 @ H ." def" 4!‫<! ا‬9 ‫ف اا‬Q
C ‫وره‬9 (‫ﻥت وا‬H‫ ذ; آ< ا‬2Q‫ ی‬. (:) g9 gG‫( ا‬5‫ أ)!ء ا!>ات وی‬kQ9 5Q n6‫ی‬
: dQ6 FGH‫ی ا‬W (6 ;‫ ذ‬H‫ل ا"ي ی‬8!‫ وا‬. ‫ اا‬T"‫ﺝ^ءا ه‬

: ‫ی دا‬Q
Example 7.1. Defining a function

#!/usr/bin/python
# Filename: function1.py

def sayHello():
print 'Hello World!' # block belonging to the function
# End of function

sayHello() # call the function


Output

$ python function1.py
Hello World!
: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

‫را ات‬9 ‫" أي‬. A ‫ اا‬T"‫ ه‬.Td@‫ أ‬YZ ‫ آ! ه‬2‫)]ام اآ‬9 sayHello ‫!ة‬G ‫ی دا‬M9 ‫• ﻥ م‬
!‫ ﻥ‬N ‫ <ا‬e6 Z  ‫را ات اا‬9 . ) ‫ ا‬9 ‫ن @ >ات‬d@‫  ﺝ إ‬A (9‫ و‬، {
}
.RL‫ ا‬5‫ !رﻥ‬5‫ د إ‬Q‫ وﻥ‬5 4<] 
‫!ی‬

Function Parameters  ‫*  دات ا ا‬


W ‫ ا!>ات‬UH ‫را ات‬H‫ ا‬T"‫ ه‬. ‫ اا‬T"5 ‫]م‬G 
‫ ) ى‬cG (‫ وا‬، ‫را ات‬9 ". ‫اا ی! ان‬
. ‫ اا‬C.‫ا‬9 
5 G‫ ی‬A‫ و‬، ‫@( اا‬G‫یه @  ﻥ‬M ‫أن
 ا!>ات ی‬
‫ @  ﻥ م‬. (:) g9  `4 ‫ و‬، ‫ی اا‬Q9 ]‫
اس)( ا‬P‫ زوج ا‬C.‫دة دا‬M ‫را ات‬H‫ا‬
. ‫ی‬g‫ ا‬p49 ‫ ا‬5gQ‫ ﻥ‬، ‫)@ء اا‬9
‫ اا‬C.‫ دا‬NgQ (‫! ا ا‬9 ، parameters N@ ‫ی اا‬Q (6 ‫ة‬gQ!‫)!ء ا‬P‫ أن ا‬5!‫  ا‬P‫* ا‬md *
. arguments N@

: ‫دات اا‬M ‫ا)]ام‬


Example 7.2. Using Function Parameters

#!/usr/bin/python
# Filename: func_param.py

def printMax(a, b):


if a > b:
print a, 'is maximum'
else:
print b, 'is maximum'

printMax(3, 4) # directly give literal values

x=5
y=7

printMax(x, y) # give variables as arguments

Output

$ python func_param.py
4 is maximum
7 is maximum
b‫!ﻥ‬7 ‫ ا‬0 A `$‫آ‬

‫د‬Q‫ ا‬3)‫ وا‬.( a , b) (‫را ات( ه‬9) ‫"ﻥ اﺙ ا‬.‫ أ‬i printMax )9 ‫ اا‬6@ ، ‫• ه‬
.H‫آ‬P‫د ا‬Q‫@ ا‬HF ;‫ ذ‬Q9‫ " و‬if..else " (‫ر‬HQ eG9 ‫)]ام‬9 H‫آ‬P‫ا‬
‫)]ام‬A‫( ا‬6‫ ( و‬arguments) (‫{ وه‬4’3} ‫@اد‬P‫ﺵة ا‬H ‫ض‬Q‫ ﻥ‬M‫ ﻥ‬، printMax ‫( اول ا)]ام ـ‬6 •
N‫ إ‬G x e) ‫
! ا‬printMax(x, y)CQ3.{x,y} ‫)]ام ا!>ات‬9 ‫)@ء اا‬9 ‫ ﻥ م‬، (‫ﻥ‬8‫ا‬
C‫( آ‬6 ‫ ا(ء‬p4‫ ﻥ‬C!Q The printMax ‫ اا‬. b  ‫را‬9 N‫ إ‬G y e) ‫ و
! ا‬a  ‫را‬H‫ا‬
.‫ت‬AM‫ا‬

Local Variables $- ‫!ات ا‬$< ‫* ا‬

‫ ال >ات‬P‫ي ل ا‬9 gH ‫  ن‬A ، ‫ی اا‬Q C.‫ن @ ا!>ات دا‬d@9 ‫@   م‬
‫ق‬g‫ ﻥ‬N!G‫ وه"ا  ی‬، ‫ اا‬C.‫ دا‬local <M HQ ‫ أ)!ء ا!>ات‬. ‫ی اا‬Q ‫رج‬. )A‫ ا‬p49 ‫ى‬.‫أ‬
‫ای‬9 (6 starting `‫< ا‬G<G‫( ا‬6 5@ ‫ن‬d@‫ و ی ا‬، <‫ ا‬C.‫ق دا‬g‫ ﻥ‬5 ‫ ﺝ! ا!>ات‬.>!‫ا‬
. )A‫ی ا‬Q

Example 7.3. Using Local Variables <M!‫ا)]ام ا!>ات ا‬

#!/usr/bin/python
# Filename: func_local.py

def func(x):
print 'x is', x
x=2
print 'Changed local x to', x

x = 50
func(x)
print 'x is still', x

Output

$ python func_local.py
x is 50
Changed local x to 2
x is still 50
: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

. x )‫]م
! ا‬G‫ ا( ﻥ‬N‫و‬A‫ اا وه( ا!ة ا‬T"‫( ه‬6 •
. ‫( اا‬6 U@ <@‫ ا"ي ا‬parameter ‫ ِد‬M!‫]م
! ا‬G‫ ن ی‬8‫ی‬9
(6 x !
>‫( اا ا( ی ؛ "ا @  ﻥ‬6 local <M HQ‫ ی‬x )A‫ ا‬، x N‫ إ‬2 !‫ ذ; أ)ﻥ ا‬Q9 •
. ‫س‬G ‫ أي‬d9 GL‫( ا< ا‬6 ‫دة‬M!‫ ا‬x YH`، ‫اا‬
. CQ49 p!  GL‫( ا< ا‬6 x !
‫ ﻥآ أن‬.P‫ ا‬print ‫ن‬9 (6 •

Using the global statement  A ‫ن ا‬$7 ‫ ام ا‬I ‫* ا‬


، local <M p )A‫ ن أن ا‬8‫ی‬9 <H ‫ |" @<; أن‬، ‫رج اا‬. ‫د‬M!‫) ا‬A‫ ا‬N‫اذا اردت إ)د
! ا‬
> N‫ ا)د
! ا‬CMG!‫ و ا‬.global statement ‫)]ام‬9 ;‫ ذ‬CQ4‫ ﻥ‬M‫ وﻥ‬. global ( !@ U‫و‬
. global statement ‫رج اا دون ا)]ام‬. ‫د‬M
p4‫ ﻥ‬C!M‫اض @م وﺝ د ا!> ا"ي ی‬69) ‫رج اا‬. ‫دة‬M!‫ ا!>ات ا‬C ‫ ا‬T"‫ی!; ا)]ام ه‬

رئ ه"ا‬N‫ ا‬HG9 YZ‫ وا‬W YH`‫ ی‬U‫ﻥ‬A 5H3 N>H‫ وی‬U<@ 3‫ا ﻥ‬P  P‫ ان ه"ا ا‬W .(‫ اا‬C.‫) دا‬A‫ا‬
. ‫ی ا!>ات‬Q (6 ;‫ آ! ی ﺝ ذ‬. R ‫ﻥ‬H‫ا‬
.‫( ا< ا]رﺝ‬6 ‫ف‬Q >!‫ ح أن ا‬Z 9‫ ی‬global statement ‫)]ام‬9
Example 7.4. Using the global statement

#!/usr/bin/python
# Filename: func_global.py

def func():
global x

print 'x is', x


x=2
print 'Changed global x to', x

x = 50
func()
print 'Value of x is', x

Output

$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

!
G‫ﻥ @  ﻥ‬6 ، ‫ ؛ و ه‬global ( !@ > ‫ ه‬x ‫ن‬9 ‫ن‬d@¦ ‫]م‬G‫ ی‬global statement •
.GL‫( ا< ا‬6 x !
‫]م‬G‫ @  ﻥ‬5m‫ن ه"ا ا> ی‬h6 ، ‫ اا‬C.‫ دا‬x N‫ا‬
: ‫ل‬8!‫ ا‬CH) N<@ .global ‫ن‬H‫ ا‬p4‫)]ام ﻥ‬9 ‫ وا‬global > 8‫ی اآ‬M ;!‫• ی‬
global x, y, z
Default Argument Values l - K$‫!اﺽ‬+k‫@ ا‬$
‫* ا‬

‫ اذا‬Z‫ا‬6A‫ري وا)]ام ا ا‬.‫ ا‬Z ‫ آ‬parameters 5‫دا‬M kQ9 CQ‫( ﺝ‬6 2W 
، ‫ اوال‬kQ9 (6
‫ی ا‬M ;!‫ ی‬. argument ‫@ة
 ال‬G Cb49 ;‫ وی ذ‬. ‫دات‬M!‫ ا‬T"5 ‫ ا‬6  ‫ ی‬A ‫]م‬G!‫آن ا‬
(=)  d@ ‫ـ‬9 ‫ی اا‬Q (6 parameter name ‫د‬M!‫ع ا) ا‬Hh9 ‫را ات‬H< argument C UZ‫ا‬6A‫ا‬
. Z‫ا‬6A‫! ا‬9 @ H
8‫(ء أآ‬9 ;‫ و) ف ی ﺵح ذ‬.constant 9‫>( ان ی ن ﺙ‬H‫ ی‬argument ‫ ل‬UZ‫ا‬6A‫ ا‬U!‫ن ا‬9 !<@
.‫ "آ ه"ا‬e6 ، ‫ن‬q‫ ا  ا‬.A ‫` ل‬6 (6 C`4‫ ا‬
Example 7.5. Using Default Argument Values….Z‫ا‬6A‫ا)]ام ا ا‬

#!/usr/bin/python
# Filename: func_default.py

def say(message, times = 1):


print message * times

say('Hello')
say('World', 5)

Output

$ python func_default.py
Hello
WorldWorldWorldWorldWorld
: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬
Z 6 ، !
‫ أی‬5gQ‫ واذا  ﻥ‬.‫@ ﺝ!<  @دا ا!ات آ! ﻥی‬Hg ‫]م‬G say 5!)‫• اا ا‬
. e6 ‫!< !ة واة‬3‫@ ا‬HF ‫( ه‬Z‫ا‬6A‫ا‬
. parameter ‫د‬M!‫ @د ات ا‬N‫ ا‬1 argument ‫ی
! ل‬M n‫ی‬F @ ;‫ ذ‬nM‫• ﻥ‬
‫ﻥ‬8‫( ا!ة ا‬6 .‫!< ة واة‬3‫@ ا‬Hg9 ‫ وه(  م‬e6 -‫( ا‬gQ‫ ﻥ‬، say ‫( اول ا)]ام‬6 •
‫ أﻥ‬N<@ - (‫  ) اء وا‬N<@ argument 5 ‫ و‬-‫ ا‬d‫ آ‬ULg@h9 ‫ ﻥ م‬M‫ وﻥ‬، say ‫!ل‬Q)‫ا‬
. ‫ ات‬p!. <!3‫ﻥی ار ا‬
* @* *

‫ ی! أن ی ن‬A‫ ؛ و‬Z‫ا‬6‫ ا‬argument 


NgQُ ‫ ی! أن‬parameter list ‫ی‬5‫( ﻥ‬6 (‫را ات ا‬H‫ ا‬T"‫ه‬
<Q!‫را ات ا‬H‫ ا‬2<F @ (Z‫ا‬6‫ ا‬argument ‫ون‬9  ‫را‬9 CH
(Z‫ا‬6‫ ا‬rgument  ‫را‬9 ;‫ی‬
. ‫را  اا‬9 !L
(6 5@
def func(a, " : ‫ل‬8!‫ ا‬CH) N<@ . position 5QZ‫ و‬2G ‫را ات‬H‫ ا‬N‫ إ‬G 
‫ أن‬2HG9 ;‫وذ‬
M " b=5)
. Y<` A " def func(a=5, b) " !9
: Keyword Arguments *
C !
(gQ ‫ |" ی!; ان‬، e6 5bQ9 ‫د‬M ‫ر ات وی أن‬H‫ی ا‬Q‫ اوال ا‬kQ9 ;‫اذا آن ی‬
(keyword) )A‫]ام ه"ا ا‬G‫ ﻥ‬M‫ ﻥ‬- keyword arguments N!G‫ وه"ا  ی‬-- 5!G n‫ی‬F @ ‫را ات‬H‫ا‬
. ‫ا‬9 ]‫ ا‬arguments ‫ی اـ‬M ( c
‫ ال ا‬F T ])‫ )ا"ي آ
 ا‬position Z !‫ ا‬A9
‫ اـ‬T"‫  ه‬9 ‫ﻥ>ل‬A‫ ا‬N‫( ﺝ ا‬6 G i C5)‫ ؛ ا)]ام اا ی ن أ‬N‫و‬P‫ ا‬- : ‫ ^ن‬U  P‫وذ; ا‬
. arguments
‫ى‬.‫ أ‬parameters ‫ــ‬9 ‫ وﻥ!ه‬، ‫ ا( ﻥیه آ! ﻥء‬arguments ‫ء
 <ـ‬g@‫ إ‬gG‫ﻥ ؛ أﻥ ﻥ‬8‫ا!^ة ا‬
. arguments ‫ ـ‬Z‫ا‬6‫
 ا‬N<@ ‫ ي‬M
Example 7.6. Using Keyword Arguments

#!/usr/bin/python
# Filename: func_key.py

def func(a, b=5, c=10):


print 'a is', a, 'and b is', b, 'and c is', c

func(3, 7)
func(25, c=24)
func(c=50, a=100)

Output :
$ python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬


argument C Z‫ا‬6‫ون
! ا‬9 (a)‫ وا‬parameter ‫د‬M N<@ ‫ ي‬M func 5!)‫ دا ا‬T"‫• ه‬
.{ b=5, c=10 i} argument ‫ ـ‬Z‫ا‬6‫{
 ا‬b,c} parameter 2 5< ،
5 Z‫ا‬6A‫" ا! ا‬.‫ ی‬b  ‫را‬H‫ وا‬، 3 !‫" ا‬.‫ ی‬a  ‫را‬H‫ ا‬، func(3, 7) ‫ول ؛‬P‫)]ام ا‬A‫( ا‬6 •
.١٠ !‫" ا‬.‫ ی‬c  ‫را‬H‫وا‬
‫(
س اـ‬6 Z ‫( أول‬6 ‫ ا! ا! ﺝ دة‬U a >!‫ا‬، func(25, c=24) (‫ﻥ‬8‫)]ام ا‬A‫( ا‬6 •
U!
N<@ C`M‫ ی‬b >!‫! ا‬9 24 !‫ ا‬N<@ C`M‫ ی‬c>!‫{ وا‬25=(‫ }وه‬argument
5 Z‫ا‬6A‫ا‬
‫ی ا‬M < ‫ آ‬keyword arguments  ])‫ ؛ا‬func(c=50, a=100): i8‫)]ام ا‬A‫( ا‬6 •
!
 @ c CH
a ‫ی‬M9 !
‫ أﻥ‬W‫ ر‬N<@ a CH
‫ ع‬Z c  ‫را‬H< ‫دة‬M!‫_ أن ا! ا‬A
. ‫ی اا‬Q9

:The return statement *

N<@ ‫ری أن ﻥﺝ ا! اا‬.‫ وی! ا‬. ‫ ا]وج اا‬C8 ‫ " رﺝع اا‬return" HQ‫]م ا‬G‫ی‬
.‫< ب‬g!‫ ا‬U‫ا ﺝ‬
Example 7.7. Using the literal statement

#!/usr/bin/python
# Filename: func_return.py

def maximum(x, y):


if x > y:
return x
else:
return y

print maximum(2, 3)

Output

$ python func_return.py
3

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

(‫{ وه‬2, 3} ‫ة <ا‬gQ!‫@اد ا‬P‫(  ا‬6 (‫ وه‬، ‫را ات‬H‫ ا‬H‫آ‬P‫ ا‬M‫ ﺝ  ا‬maximum ‫• اا‬
. !‫  <; ا‬Q ‫ه‬Q9‫ و‬H‫آ‬P‫د ا‬Q‫
! ا‬N<@ ‫ ر‬8Q<. if..else (‫ ه‬gG9 ‫ﻥت‬9 C!QG
. None ‫وی ـ‬G ‫ون أی
!  ن‬9 return ‫@   ن‬: md
!
‫ أي‬C!M‫ ی‬A > N‫]م ¦ﺵرة إ‬G ‫ل ؛‬8!‫ ا‬CH) N<@ . ‫م‬Q‫ ا‬C8!‫ ن ی‬8‫ی‬9 (6 ‫ص‬. ‫ ﻥ ع‬HQ None •
. None ‫
! اره‬C!M‫إذا آن ی‬
.return ‫ ا] ب‬5‫ﻥ‬9 2 ‫( دون أن‬5  @ None ‫ن‬H‫ إرﺝع ا‬N<@ !Z ‫ ي‬M ‫ دا‬C‫• آ‬
‫ن‬H‫]م ا‬G A someFunction() ‫ @   ن اا‬someFunction() @Hg9 ;‫و ی!; رؤی ذ‬
return
: (<‫آ! ی‬
def someFunction():
pass

. ‫ﻥت‬H‫ ا‬W‫ر‬6 <‫ آ‬N‫ ن  إ‬8‫ی‬9 (6 pass ‫ن‬H‫]م ا‬G‫ی‬

DocStrings *

‫ل‬d. ‫ @دة‬5‫ " وا( یر إ‬documentation strings " `‫ ا‬nL‫ ا ﺙ‬N@ ‫ ^ة أﻥ‬U ‫ ن‬8‫ی‬9
" DocStrings" `]!‫ ا‬5!)‫ا‬
، Cb6‫ أ‬C9 R ‫ﻥ‬H‫ ا‬n‫  ﺙ‬N<@ @G 5‫ إﻥ‬i 5 4G ‫ @<; أن‬23‫ ه( أداة ه  ی‬: Docstrings
. 54<  5) 8‫ أآ‬U<Q3‫و‬
!! CQ49 C!Q9 R!‫ﻥ‬H‫ @  ی م ا‬، docstring @‫ د‬N<@ ‫` ل‬M‫ ا‬N !‫ ی‬، }‫ ه‬C9‫و‬

: docstrings ‫ ام‬I ‫* ا‬
Example 7.8. Using DocStrings

#!/usr/bin/python
# Filename: func_doc.py

def printMax(x, y):


'''Prints the maximum of two numbers.
The two values must be integers.'''
x = int(x) # convert to integers, if possible
y = int(y)

if x > y:
print x, 'is maximum'
else:
print y, 'is maximum'

printMax(3, 5)
print printMax.__doc__

Output

$ python func_doc.py
5 is maximum
Prints the maximum of two numbers.
The two values must be integers..

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

N<@ b‫ أی‬nHg DocStrings ‫_ أن‬A . ‫ <ا‬docstring ‫ول <ا ه‬P‫( ا‬g!‫ ا‬gG‫ ا‬string •
. . ‫` ل‬6 (6 59 ‫ ا( ) ف ﻥ م‬classes ‫ و‬modules
(5‫ و‬capital H‫ف آ‬M9 ‫أ‬H g) ‫( أول‬6 g)P‫دة ا‬Q C!3‫ ه( أن ا‬docstring (6 QH!‫
 ا‬4A‫• ا‬
. i8‫ ا‬gG‫( ا‬6 C`4!‫!< اح ا‬39 @ H ‫رغ‬6 (‫ﻥ‬8‫ ا‬gG‫ ذ; ا‬Q9 . (.)g9
. !5!‫ اوال ا‬U"H  C‫ أﺝ‬docstrings C‫( آ‬6 
4A‫ ا‬T"‫ ه‬H ‫ة أن‬9 ;M`‫وﻥ‬
b4]!‫ ا‬F‫_ ا‬A) doc__ ‫)]ام‬9 printMax ‫ا‬9 ]‫ ا‬docstring N‫• ی! ا  ل ا‬
. ‫( اا‬name belonging__to N‫ )ا) !(_إ‬N‫ إ‬G (double underscores__ ‫ا!^دوﺝ‬
@ ‫< ا!^ی‬Q‫ و) ف ﻥ‬. b‫ اوال أی‬C!‫" وه"ا ی‬object " L‫ آ‬U‫ ﺵ(ء أﻥ‬C‫ آ‬HQ‫ ن ی‬8‫ی‬9 ‫ "آ ان‬e6 *
. " classes" ‫ت‬Hg9 n<Q!‫ ا‬C`4‫( ا‬6 "objects" ‫ت‬L‫ا‬

. CQ49 docstrings ‫ی ا)]ام‬F CQ49 c‫ أﻥ; رأی‬9d6 ، ‫ ن‬8‫ی‬9 (6 help() . c ])‫
 ا‬c‫* اذا آ‬
. n‫"ب وأﻥ‬5 ‫)< ب‬9 ; 5ZQ‫ اا و‬T"5 !!‫ ا‬doc__ ‫ر‬bM)‫د ا‬3 ‫  @<; ه‬C‫* آ‬
‫ و"آ أن‬. ;3 ‫ﻥ‬9 (6 help(printMax) : ِZ‫ أ‬e6 – Td@‫ أ‬H!‫ل اا ا‬d. ;‫* ی!; ا)ف ذ‬
. ‫@ة‬G!‫ <]وج ا‬q ‫ح‬4 e>b
‫]م‬G c‫ ة إن آ‬9 (‫ﻥ( أو‬h6 ، ‫ "ا‬.‫ی‬g‫ ا‬T"59 ;3 ‫ﻥ‬9 nL‫ ا)ﺝع ا ﺙ‬5!‫ ی‬q‫دوات ا‬P‫ا‬
@ help() ‫)]ام‬A C8!9 C!Q‫ ن ی‬8‫ی‬9 (‫ ا"ي ی‬pydoc  P‫ ا‬.5H 56W ‫ی دا‬P docstrings
. docstrings n‫ی‬F

‫ﺹ‬:% *
. 5H‫ ﺝ اﻥ‬6‫( آ‬g>‫_ أﻥ  ز  ﻥ‬d!‫ و ا‬، ‫وال‬9 <Q!‫ ا‬2‫ اﻥ‬3‫ ا‬8‫ رأی ا‬
‫! ی<( ؛ ) ف ﻥى‬6‫ و‬.  ‫)) ا‬P‫ ن ا‬8‫ی‬9 ‫وال‬9 n<Q (‫ ر ا‬P‫ ا‬mQ g>9 CQ49 !
6 ;‫ ذ‬W‫ور‬
. Python modules ‫ﻥء‬h9 ‫آ ﻥ م‬

☺☺☺
6 ‫ ا‬02, ‫ا‬
Modules

 *
‫ ذا  اردت ا@دة ا)]ام @د اوال‬.‫ی اوال ة واة‬Q n‫ی‬F @ ;3 ‫ﻥ‬9 (6 ‫ آ ی!; إ@دة ا)]ام ا د‬c‫
 رأی‬
‫ ي‬M‫)س < ی‬A‫( ا‬6 ‫ ه‬: module ‫ ا! ذج‬.modules ‫ اب ه ا!ذج‬3‫ ا‬، c!. 
!‫ ! آ‬Q‫؟ ﻥ‬5H (‫ى ا‬.A‫ ا‬R ‫ا‬H‫( ا‬6
module ‫ أن ی ا) < ا ة‬23‫ی‬، ‫ى‬.‫ ا‬R ‫ا‬9 (6 ‫ و@دة ا)]ام ه"ا ا! ذج‬.54‫ی‬Q9 c!
(‫ﺝ! اوال وا!>ات ا‬
. Py .‫ اد‬9
)‫ ن ا‬8‫ی‬9 ‫ت‬H ‫]م‬G‫ی ا( ی! أن ﻥ‬g‫ ه( ا‬T"‫ وه‬.U4†‫دة و‬4)d .o R ‫ﻥ‬9 CHِ
‫ ی! ا)اده‬C‫ا! دی‬
.^<‫ <! دی‬U)‫ت ا‬H!‫ ا)]ام ا‬4‫ ) ف ﻥى آ‬، A‫ أو‬.YM Cg9
: sys module ‫ ام‬I ‫ا‬
Example 8.1. Using the sys module

#!/usr/bin/python
# Filename: using_sys.py

import sys

print 'The command line arguments are:'


for i in sys.argv:
print i

print '\n\nThe PYTHONPATH is', sys.path, '\n'

Output

$ python using_sys.py we are arguments


The command line arguments are:
using_sys.py
we
are
arguments

The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',


'/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ر‬H.‫ إ‬NQ!9  ‫)س ه"ا یﺝ‬P‫( ا‬6 " import" HQ‫)]ام ا‬9 module sys ‫)اد‬9 !
‫ای‬H‫( ا‬6 •
]‫| ا‬H‫ ن وا‬8‫ی‬9 G4 gH 4†‫ ي و‬M‫ ی‬sys C‫ ا! دی‬. C‫ ن أﻥ ﻥی ا)]ام ذ; ا! دی‬8‫ی‬9
. U9
(6 ‫د ا! ﺝ دة‬P‫( أ ا‬6 sys.py C‫ @ ا! دی‬iMH‫ه ی‬Q9، sys ‫ رد‬G!‫ ا‬C‫ ن ا! دی‬8‫ی‬9 "4‫• @  ی‬
(6 ‫ﻥت ! ﺝ دة‬H‫ ا‬C> ‫" ی‬L@ <!‫ذا وﺝ ا‬h6 . {U‫ ﺵ‬nH)} sys.path >!‫ـ ا‬9 ]‫! ا‬L‫ا‬
. ‫!ل‬Q)d ‫ا‬Q C‫ ا! دی‬YH`‫ه ی‬Q9‫ و‬C‫! دی‬9 ]‫ ا‬GL‫ا< ا‬
'system' ‫`ر ل‬.‫' ا‬sys' ‫آ"; ا@< أن‬. C‫ ا! دی‬56 N@G‫ @ أول ة ی‬e6  6A‫ ا‬T"‫ ه‬: 9‫ﺡ‬:
{g9   !<‫ }ا) آ‬notation dotted ‫ ا)]ام‬N‫ ی إ‬module "sys" (6 "argv" >!‫ا‬
sys.argv
U‫ﻥ‬h6 ;"‫ وآ‬. ;3 ‫ﻥ‬9 (6 ‫]م‬G .o "argv" > ‫ أی‬UH‫ ی‬A )A‫)< ب أن ا‬P‫• أ !^ات ه"ا ا‬
. sys C‫) ﺝ^ءا ا! دی‬A‫ آ ن ذ; ا‬N<@ ‫ ح‬Z 9 ‫یل‬
C9‫و‬.(A ‫م‬G
‫( أ‬6 C`49 ;‫ ا` )) ﺵح ذ‬C!3‫! ا‬L
@ ‫رة‬H@ sys.argv >!‫• ا‬
arguments ‫ ال‬: d8 . ‫وا‬P‫ ا‬gG9 arguments !L
N<@ ‫ ي‬M‫ ی‬sys.argv ‫ن‬h6 ‫یا‬M 8‫أآ‬
.  ‫وا‬P‫ ا‬g) ‫]م‬G ;3 ‫ﻥ‬9 N‫ا!!رة إ‬
‫]م‬G c‫إذا آ‬IDE ‫ أوا  ال‬g) ‫د‬M ‫ی‬F @ iM9‫; ؛ ا‬3 ‫ا‬9 C>‫ و‬9 arguments. L‫ل ا ا‬d. ;3 ‫ﻥ‬H
C>9 !
6 ، using_ sys.py we are arguments N!G!‫ ن ا‬8‫ی‬9 R ‫ﻥ‬9 "4‫• وه ؛ @  ﻥ‬
N‫  !یه إ‬arguments @ ‫رة‬H@ U Q9‫ى ا‬.P‫ﺵء ا‬P‫ وا‬python  P‫ ا‬sys.spy C‫ا! دی‬
. sys.argv >!‫( ا‬6  5‫]^ی‬9 ‫ ن‬8‫ی‬9 ‫ ی م‬. R ‫ﻥ‬H‫ا‬
;" . sys.argv !L
(6 argument ‫! أول‬L‫ دا‬C!Q‫{ ا>ل ا"ي ی‬c9G‫ }ا‬R ‫ﻥ‬H‫• "آ أن ا) ا‬
‫' آـ‬are' ‫[ و‬sys.argv[1] ‫' آـ‬we' ‫[ و‬sys.argv[0] ‫' آـ‬using_sys.py' ‫ ) ن ی‬M‫ ا‬T"‫( ه‬6
.١ p‫ و‬٠ ّ Q‫أ ا‬H‫ ن ی‬8‫ی‬9 ‫_ أن‬A . [sys.argv[3] ‫' آـ‬arguments' ‫[ و‬sys.argv[2]
‫ أن أول‬md . 5 ^<‫د ا( ی ا)اد ا! دی‬P‫! أ)!ء ا‬L
N<@ ‫ ي‬M‫ ی‬sys.path >!‫• ا‬
، sys.path ‫( ه آ"; ﺝ^ء‬M‫ ا‬C‫ أن ا‬N‫  إ‬W‫ر‬4‫رة ا‬HQ‫ ا‬T"‫ – ه‬W‫ر‬6 sys.path (6 ‫رة‬H@
‫ﺵة‬H ;!‫ ی‬U‫ أﻥ‬TQ ;‫ذ‬. PYTHONPATH environment variable |H‫ > ا‬UG4‫وا"ي ه ﻥ‬
‫د‬P‫ ا‬T"‫( أ ه‬6 C‫ ا! دی‬b ‫>( @<; أن‬H6 ;" 6d. . (M‫ ا‬C‫( ا‬6 ‫ا)اد ا! دی<^ ا! ﺝ دة‬
. sys.path !L
(6 ‫ا! ﺝ دة‬
Byte-compiled .pyc ‫ت‬,-*
T"‫ أ ه‬. ‫ أ)ع‬C9 C!Q 5<Q3 CM‫ ا‬kQ9 C!Q9 ‫ ن ی م‬8‫ی‬9 >6 ‫ "ا‬، HG‫ ﻥ‬p4‫ أ  ﻥ‬C‫ا)اد ا! دی‬
‫ ل‬M‫| ا"ي ی‬H9 eH‫ ا"ي ی‬.pyc ‫ اد‬9 (‫ وه‬Byte-compiled .pyc ‫ت‬4<!9 N!G‫ق ه( إﻥء  ی‬g‫ا‬
‫]م‬G‫ ی‬.pyc ‫ اد‬9 <!‫ ن ( ه"ا ا‬8‫ی‬9 C!@ 4‫ @ آ‬U6 ‫ﺙ‬M ‫^ء ا"ي‬3‫{ ) "آ ا‬.py} 5‫ إ‬R ‫ا‬H‫ ن ا‬8‫ی‬9
<!Q‫^ء ا‬3‫ أن ا‬i @) 8‫ و) ن أآ‬-<] R ‫ﻥ‬9 ‫ﻥ‬8‫( ا!ة ا‬6 C‫@( ا! دی‬G‫@  ﻥ‬
`!‫< @ ا‬G (‫ ه‬byte-compiled ‫ت‬4<!‫ ا‬T"‫ن ه‬h6 C8!9‫ و‬. CQ49 U<!@  
C‫< ب ا)اد ا! دی‬g!‫ا‬
.pyc. ‫ت‬4< (‫  ه‬6@ 
‫"; ﻥ ن‬9‫ و‬. platform-independent

:"from..import" ‫ن‬$7 ‫* ا‬
‫ ی!; ا)]ام‬T@ ( ‫ ة‬C‫( آ‬6 .sys 9‫ آ‬23) ;3 ‫ﻥ‬9 N‫" إ‬argv" !$< ‫!اد ا‬$ ‫(!ة ا‬7 ‫إذا أردت‬
‫ ی!; ا)]ام‬sys C‫( ا! دی‬6  ]G!‫)!ء ا‬P‫ ا‬C‫ إذا اردت ا)اد آ‬." from sys import argv" ‫رة‬H@
‫رة‬H@ ‫ ا)]ام‬23 ‫ @م‬U‫ ﺝ‬9‫ و‬. ^<‫ ا! دی‬C‫ آ‬C!Q‫ وه"ا ی‬. "from sys import *" ‫رة‬H@
(6  5) 8‫ی أآ‬g‫ ا‬T"59 ‫ ) ن‬R ‫ﻥ‬H‫ أن ا‬i ، import ‫رة‬H@ 5 A9 ‫" وا)]م‬from..import"
.‫( أي أ)!ء‬6 TH‫ أي اﺵ‬23 ‫ و) ف‬، U‫
اء‬
: A module's __name__*
. module ‫ ال‬J ‫ل ا @ ذ‬:%  *‫!اﺝ‬I ‫   ا‬0‫ ا د‬+ ‫ﻥت‬$7 ‫  ا‬G /‫ ا  و‬0  module 0‫آ‬
‫ ی‬، N‫و‬P‫( ا!ة ا‬6 module ‫ رد ال‬G‫ ؛ وذ; @  ﻥ‬9) 5‫
  ﺵ‬.  (6 ‫ ا!ل‬C5) ;‫وذ‬
. module ‫ ال‬GL‫ ا< ا‬C>
‫ ؟‬.o module ‫ ردا‬G p‫ و‬5G4‫ ه( ﻥ‬59  ]G R!‫ﻥ‬H‫ @  ی ن ا‬e6 <‫ ا‬C> ‫ ذا  أردت أن‬
. C‫ ا) ا! دی‬N‫ا إ‬G __name__ ‫)]ام‬9 UM !‫ه"ا ی‬

Using a module's __name__*

Example 8.2. Using a module's __name__

#!/usr/bin/python
# Filename: using_name.py

if __name__ == '__main__':
print 'This program is being run by itself'
else:
print 'I am being imported from another module'

Output

$ python using_name.py
This program is being run by itself

$ python
>>> import using_name
I am being imported from another module
>>>

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


!L
YH`‫ ی‬C‫! ذ; ا! دی‬b‫__' ی‬main__' )A‫د وه"ا ا‬M __name__ )‫ ا‬C!M‫ ن ی‬8‫ی‬9 (6 C‫ دی‬C‫آ‬
. H)!‫اث ا‬P‫ ا‬C!@ C8!9 !‫]م وی‬G!‫ل ا‬d. U‫"ا‬9
: J‫ﺹ ﺏ‬% Modules 0 G*
. ;"‫ آ‬C‫ دی‬HQ‫ ن ی‬8‫ی‬H R ‫ﻥ‬9 C‫ آ‬.e]‫ ل ا‬F N<@ ;‫ ذ‬C!Q9 ‫ و) ف  م‬، C5)  ‫; أ‬9 . ^<‫إﻥء دی‬
. ;‫ ; ذ‬YZ ) (‫ل ا‬8!‫ وا‬. .py ‫ ا اد‬N<@ UL‫ @<; اآ ا ا‬e6
Example 8.3. How to create your own module

#!/usr/bin/python
# Filename: mymodule.py

def sayhi():
print 'Hi, this is mymodule speaking.'

version = '0.1'

# End of mymodule.py

R ‫ا‬9 (6 T‫! ا@ﻥ‬9 ‫!رﻥ‬9 ( `. ‫ ی ﺝ ﺵ(ء‬d6 ‫وآ! ى ؛‬. eG9 C‫ ه دی‬Td@‫ أ‬H!‫ ا‬R ‫ﻥ‬H‫ه"ا ا‬
. ‫ى‬.P‫ ن ا‬8‫ی‬9 R ‫ا‬9 (6 C‫]م ه"ا ا! دی‬G‫! ی<( ) ف ﻥى آ ﻥ‬6‫ و‬. ‫ ن‬8‫ی‬9
‫( أ‬6 ‫ أن ی ن‬23‫ ی‬C‫ أو أن ا! دی‬، R ‫ﻥ‬H‫ ا‬U<@ C!Q‫ ا"ي ی‬C‫ ا‬p4‫( ﻥ‬6 Z ‫ أن ی‬23‫ ی‬C‫"آ أن ه"ا ا! دی‬
. sys.path !L
(6 ‫د ا! ﺝ دة‬P‫ا‬

#!/usr/bin/python
# Filename: mymodule_demo.py

import mymodule

mymodule.sayhi()
print 'Version', mymodule.version

Output

$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ ال‬p4‫]م ﻥ‬G‫_ أﻥ ﻥ‬Adotted notation ‫ ن إ@دة ا)]م‬8‫ی‬9 3‫ وی‬. C‫ @ ا! دی‬N‫{<  ل إ‬g‫! ﻥ‬59 !<‫}ا) آ‬
‫ اـ‬p4‫ﻥ‬notation . ‫ﺵء‬P‫
ً ﺝیة ` ا‬F <Q‫ ﻥ‬Cm‫ @< أن ﻥ‬p ‫ "ا‬، 5<W ^! "(‫ ﻥ‬8‫ی‬9"‫ ر‬Q‫ء ﺵ‬4Z

from..import*:

Here is a version utilising the from..import syntax.

#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:
# from mymodule import *

sayhi()
print 'Version', version

mymodule_demo.py. R‫ ا‬C8 mymodule_demo2.py ‫ ـ‬R‫ا‬

: dir()  ‫*ا ا‬
، ‫ت ه( اوال‬6Q!‫ ا‬T"‫ ه‬. C‫ده ا! دی‬M ‫ت‬6Q!< !L
C!Q dir() 3 !‫< ا‬.‫ی!; ا)]ام اا اا‬
. C‫( ا! دی‬6 6Q!‫ ا‬classes ‫ت‬Hg‫ وا‬، ‫وا!>ات‬
‫ و@ @م وﺝ د أی‬. C‫( ا! دی‬6 6Q!‫)!ء ا‬P‫! ا‬L
 Q (56 ، dir() ‫ ا)! ا‬C‫; ا! دی‬Lg@‫@ إ‬
. (M‫ ا‬C‫( ا! دی‬6 6Q!‫)!ء ا‬P9 !L
 Q ، 5  argument
: dir  ‫ ام ا ا‬I ‫* ا‬
Example 8.4. Using the dir function

$ python
>>> import sys
>>> dir(sys) # get list of attributes for sys module
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
'__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type',
'excepthook', 'exec_prefix', 'executable', 'exit', 'getcheckinterval',
'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding',
'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
'meta_path','modules', 'path', 'path_hooks', 'path_importer_cache',
'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
'version', 'version_info', 'warnoptions']
>>> dir() # get list of attributes for current module
['__builtins__', '__doc__', '__name__', 'sys']
>>>
>>> a = 5 # create a new variable 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'sys']
>>>
>>> del a # delete/remove a name
>>>
>>> dir()
‫]'‪['__builtins__', '__doc__', '__name__', 'sys‬‬
‫>>>‬

‫*آ‪ 0 A `$‬ا ‪!7‬ﻥ‪: b‬‬


‫‪ (6‬ا‪H‬ای رأی ا)]ام ‪ dir‬ا! دی‪ C‬ا!‪ G‬رد ‪ . sys‬ی! رؤی
‪ !]Z !L‬ا‪ Q‬ا( !‪. 5<@ C‬‬
‫‪Q9‬ه
! ‪])9‬ام اا ‪9 dir‬ون !ی أي ‪9‬را ات إ‪ – 5‬ا‪6‬ا‪ Z‬؛ ه( ‪ Q‬إ
‪ !L‬ا‪Q‬‬
‫ا!! <! دی‪ C‬ا‪. (M‬‬
‫و أﺝ‪ (6 dir md C‬ه"ه ا‪Q9 !
، C!Q‬ی > ﺝی "‪ "a‬وإ)د‪ T‬إ‪ !
N‬و‪Q9‬ه ﻥ م ‪dir -M49‬‬
‫‪،‬و)‪ _d‬أن هك
! ‪ 6b‬إ‪ N‬ا‪ p49 !L‬ا‪ .{a} )A‬ﻥ م ‪"M9‬ف ا ا!‪ (6 >!< HG‬ا! دی‪ C‬ا‪(M‬‬
‫‪])9‬ام @‪H‬رة "‪ "del‬و) ف ی‪ pQ‬ه"ا ا> ة ﺙﻥ ‪ (6‬ﻥ‪ R‬اا ‪. dir‬‬
‫ ‪ – del N<@ md‬ه"‪ T‬ا‪HQ‬رة ‪]G‬م ‪"M‬ف ا) > ‪ Q9‬أن ی @!‪ C‬ا!>‪ ،‬وه( ‪ (6‬ه"‪ T‬ا‪، "del a" : M‬‬
‫و‪ A‬ی!; @<‪ N‬ا!ى ا‪ g‬ی‪ C‬ا  ل إ‪ N‬ا!> ‪ – a‬وآﻥ‪   5‬ﺝ دة @<‪ N‬ا‪dF‬ق
‪CH‬‬
‫* ا ‪:I‬ﺹ ‪:‬‬
‫ا! دی‪ C‬أ  ‪P 4‬ﻥ‪! 5‬ك ‪ ]9‬ت وو†‪ L‬ی! إ@دة ا)]ا ‪9 (6 5‬ا ‪ . 3‬وا!‪ H‬ا) ا( ( ‬
‫‪9‬ی‪ 8‬ن ‪8 HQ‬ل @<‪ N‬ا! دی<^ ‪ .‬و
 رأی آ ﻥ‪]G‬م ه"‪ T‬ا! دی<^ وإﻥء ا! دی<^ ا] ‪ 9‬آ"; ‪.‬‬
‫و‪ !6‬ی<( ) ف ﻥ‪ kQ9 <Q‬ا!‪4‬ه ا!‪ !5‬وا( @‪." data structures" N‬‬
‫ﻥت‬$7 ‫ ا‬0‫آ‬$‫ ه‬: "  ‫ ا‬02, ‫ا‬
Data Structures
Table of Contents ...................................................................................‫  ا ت‬
Introduction ........................................................................................................... 
List ........................................................................................................................!L‫ا‬
Quick introduction to Objects and Classes ......................‫ت‬Hg‫ت وا‬L‫ @ ا‬UQ‫   )ی‬
Using Lists................................................................................................. L‫ا)]ام ا ا‬
Tuple.............................................................................................................tuple L‫
ا‬
Using tuples .................................................................................................................
Tuples and the print statement .......................................................................................
Dictionary.......................................................................................................... ‫ا س‬
Using Dictionaries ...................................................................................p ‫ا)]ام ا ا‬
Sequences ...........................................................................................................C)dG‫ا‬
Using Sequences................................................................................ ‫ت‬dG<G!‫ا)]ام ا‬
References....................................................................................................... ‫اﺵرات‬
Objects and References ......................................................................‫ت واﺵرات‬L‫ا‬
More about Strings................................................................................ C!3‫ا!^ی @ ا‬
String Methods ..............................................................................`‫ ا‬C)dG‫ ا‬2)‫ا‬
summary........................................................................................................... d.


 *

‫ى‬.‫ ا‬T‫ر‬HQ9‫ و‬.Q ‫ﻥت‬H‫ ا‬kQ9 C! 5!‫ت{ ی‬4<!‫ﻥت وا‬H‫ ا‬m}C‫د هآ‬3 ))‫ﻥت ه( أ‬H‫ ا‬C‫هآ‬
-- ‫ ن‬8‫ی‬9 (6 3  ‫ﻥت‬H‫ ا‬C‫ﺙ أﻥ اع هآ‬d‫ وهك ﺙ‬، <`‫ﻥت ذات ا‬H‫! @ ا‬3 ‫]م ]^ی‬G (56 ،
‫ة‬M‫ ا‬CQ3 5‫ وآ اﻥ‬، 5 C‫ ا)]ام آ‬4‫ و)ى آ‬.dictionary ‫ وا س‬، tuple !L
‫ و‬،list !L‫ا‬
. C5)‫ا‬

list:  
‫ا‬
!L‫ا‬list <G<) ‫ ی!; ان ]^ن‬d8!6 ، <`‫ ذات ا‬Q‫! @ ا‬3 C!M (‫ﻥت ا‬H‫ ا‬C‫أ هآ‬:
!6 ، ‫ه <اء‬Q @ 56 ;‫ ق ی‬G< !L
(6 4 c‫ ا` ر آ!  آ‬C5)  P‫ ه"ا ا‬.!L
N6 ‫ د‬H‫ا‬
!6 9 <6 b‫ ن ی‬8‫ی‬9 ‫(  أن‬6 ، ‫ ق‬G‫! ا‬L
(6 C`4 g) (6 `@ C‫ أن ی; آ‬C!M!‫@ا أن ا‬
59.

‫ﻥء‬9 ‫د ان  م‬3!9 .!L


‫ی‬M ‫ ن اﻥ; ی‬8‫ی‬9 54‫ ی‬N [ ] Q9 )
9 ‫>( ان  ن‬H‫ ی‬Q‫! ا‬L

، ‫ د‬H‫"ف ا‬M‫ أوﻥ‬b‫ ی! ان ﻥ‬، "|‫ و‬.!L‫( ا‬6 ‫ د ا اردة‬H‫ @ ا‬iMH‫ او ازا او ا‬6Z‫ ی!; ا‬، !L

T> !‫ { اي أن ه"ا ا ع ی‬H<!‫< <>}ا!>ة أو ا‬9


‫ﻥت‬9 ‫! ه( ﻥ ع‬L‫ ﻥ ل ان ا‬M‫وﻥ‬.

{CL`4‫ت }ا‬Hg‫ت وا‬L< UQ‫   )ی‬Objects and Classes



 ‫ن‬q‫ ا‬N‫ت و‬.‫ أ‬c‫ أﻥ(
 آ‬W‫ر‬Objects , Classes >!‫]م ا‬G  @ ;‫ﻥ‬h6 ، ‫ @م‬U‫ ﺝ‬9 i G‫و‬
L‫ﻥ; أﻥت آ‬h6 ، 5 5‫ أﻥ‬d8 C‫
!  و‬U‫إ‬Object " i <`4< 9 "class {|4‫}أو ا‬int ;!‫( ا ا
ی‬6 .
‫ان ى‬help(int) C !‫ وی‬.Cb6‫ ا‬C9 L‫ ا ا‬54 class ‫)]ام‬d ‫دة‬M!‫ اوال ا‬C8 2)‫ أ‬N<@ C! ‫أن‬
" |4‫ ا‬T"‫ص ه‬. U‫ ﺝ‬9class ‫ ال‬T"5 L‫ @  ی ن ی; آ‬e6 ‫ﺝ^اء‬P‫ ا‬T"‫ ه‬4†‫!ل و‬Q)‫ ی!; ا‬. e6 "
class |4‫ق ا‬6‫ی ر‬F ‫ ن‬8‫ی‬9 6  ‫ل ؛‬8!‫ ا‬CH) N<@ list" .!L‫ی ا‬5‫ ﻥ‬N‫ @` ا‬6Zh9 ; Y!G (‫" ا‬
: {>`‫ ا‬T"‫ل }ه‬8!‫ ا‬CH) N<@mylist.append('an item') `‫< ﻥ‬G<) b) string !L
N‫"ا‬
mylist" _A dotted notation ‫ ال‬2)‫ أ‬N‫ { <  ل ا‬e!‫) ا‬A‫ }ا‬objects.

|4‫ ا‬class ‫  ل‬b‫ ای‬5 ‫ی! ان ی ن‬fields ;< -]‫! ی‬6 5 ‫)]ا‬A ‫ ) ى >ات ادة‬cG (‫وه‬
;‫ @  ی ن ی‬2GM6 ‫)!ء‬P‫ ا‬/ ‫ ا!>ات‬T"‫ ی!; ا)]ام ه‬.e6 |4‫ا‬object b‫ ل أی‬M‫ ا‬.|4‫ <; ا‬
e!‫) ا‬A‫ ا‬5<@ ‫ل‬d. 5‫ی! ا  ل إ‬dotted notation ، ‫ل‬8!‫ ا‬CH) N<@ ، mylist.field .

: @‫ ام ا
ا‬I ‫*ا‬
Example 9.1. Using lists

#!/usr/bin/python
# Filename: using_list.py

# This is my shopping list


shoplist = ['apple', 'mango', 'carrot', 'banana']

print 'I have', len(shoplist), 'items to purchase.'

print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,

print '\nI also have to buy rice.'


shoplist.append('rice')
print 'My shopping list is now', shoplist

print 'I will sort my list now'


shoplist.sort()
print 'Sorted shopping list is', shoplist

print 'The first item I will buy is', shoplist[0]


olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist

Output

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

b‫!ﻥ‬7 ‫ ا‬0 A `$‫آ‬


>!‫ا‬shoplist (6 .‫ ق‬G‫ ا‬N‫ ا‬2‫ ذاه‬-] ‫ ق‬G !L
@ ‫رة‬H@ shoplist `‫ ا‬C)dG‫]^ی ا‬9 ‫ ﻥ م‬،
strigs ;‫( ذ‬6 !9 !L‫ ا‬N<@ ‫ت‬L‫ اي ﻥ ع ا‬6Z‫ ی!; ا‬U‫  "آ أﻥ‬، 5‫ ا( )ی‬Q‫)!ء ا‬P
‫ى‬.P‫ ا‬L‫ ا ا‬N‫@اد و‬A‫ا‬.

<M‫ ا)]م ا‬b‫ وی ای‬for..in" " (‫! ه‬L‫ ان ا‬b‫ ای‬c‫ اﻥ; ادرآ‬9 A ، ‫ن‬q‫ ا‬.‫ د‬H‫! ا‬L
‫ل‬d. ‫<ار‬
‫<ة‬G<G! sequence ‫<ة‬G<G!!‫]` ص ا‬9 }
‫ و) ف ﻥ‬. sequence. nA G
(6

" ‫رة‬H@ ‫ی‬5‫( ﻥ‬6 "،" <6 ‫]م‬G‫_ أﻥ ﻥ‬A " print" ‫رة‬H@ C‫ آ‬Q9 g)P‫ ا‬C4 ‫@ ا<ءی‬Hg‫! ا‬
print !5!‫^ ا‬3‫< ﻥ‬Q3‫ و‬gG9 5‫ و‬، ;‫ ذ‬CQ4 MH
‫ی‬FHQ 
T"‫" ه‬.

‫ق‬6‫ی ار‬F ‫)]ام‬9 !L


N‫ ا‬9 6Z9 !
، ‫ه‬Q9append N‫ إ‬list object ‫ ان ﻥ
ه‬nH) !‫ آ‬،
‫!ی‬9 FGH9 ;‫! وذ‬L‫ یت ا‬M HF n‫ی‬F @ !L‫ ا‬N‫ ا‬U6Z‫ ا‬CQ49  
H‫ ان ا‬nM‫ ﻥ‬، ‫ ﺙ‬.CH

‫رة‬H@ n‫ی‬F @ !L‫ا‬print H ‫ أﻥ‬U‫ی‬g9  5QHg9 ‫ ا(  م‬.

‫ی‬F ‫)]ام‬9 !L‫ ا‬29 ‫ ﻥ م‬، ‫ ﺙ‬sort Q‫ ی‬A‫ و‬5G4‫! ﻥ‬L‫ ا‬N<@ ‫)< ب یﺙ‬A‫ ذ; ان ه"ا ا‬54‫ ﻥ‬.!L<
`‫ ا‬C)dG‫ ا‬C!@ ‫ی‬F @ <]‫ وه"ا ی‬-- Q!‫! ا‬L‫ا‬strigs >< <9
L‫ ل ان ا ا‬9 UQ‫ وه"ا  ﻥ‬.
`‫ ا‬C)dG‫ وأن ا‬،strigs9‫ ﺙ‬.

" ‫ن‬H‫ ا)]ام ا‬n‫ی‬F @ ;‫ ذ‬nM‫ ﻥ‬.!L‫ ا‬U‫ وﻥی إزا‬، ‫ ق‬G‫( ا‬6 9 ‫( ﺵاء‬5‫ @  ﻥ‬، ‫ﺙ‬del ‫ ه‬. "
" ‫ن‬H‫ ی م ا‬U‫! و ﻥی ازا‬L‫( ا‬6 ‫ ا"ي‬H‫ ا‬N‫ ﻥ ا‬،del!L‫  ا‬U6"M9 ". H‫ وه ا‬U‫د  ﻥی ازا‬M‫ﻥ‬
‫]م‬G‫ ﻥ‬M‫( ﻥ‬9‫ و‬، !L‫ول ا‬P‫ا‬del shoplist[0])0 Q‫أ ا‬H‫ ن ی‬8‫ی‬9 ‫ )"آ أن‬.(
n‫ی‬F @ 5‫ د‬N‫ ا‬2)A‫ ا‬C‫ف آ‬Q ‫ ی ان‬c‫اذا آ‬list object m‫ اﻥ‬، help(list)C4‫)!ل ا‬A .
: Tuple*
@ ‫ف‬Q tuples. tuples C‫ی‬Q ;!‫ ی‬A ‫ اي‬strings `‫ ا‬C!3‫ ا‬C8 9‫ ﺙ‬5‫ اﻥ‬A‫ ا‬L‫ ا ا‬C8 Tuples
‫ت‬AM‫( ا‬6 ‫]م‬G  ‫ @دة‬Tuples .( ) ‫
اس‬P‫ زوج ا‬C.‫ دا‬،، ‫ت‬d`6 59 ، <`4 ‫ د‬9 ‫ی‬M n‫ی‬F
‫ ا‬tuple ‫! @ ا أي‬3 ‫ ن أن‬9 ‫ض‬4‫ ی! أن ﻥ‬- ‫]م‬G!‫ده ا‬M‫ن او اا ی‬H‫ ا‬56 ‫ا( ی ن‬
.> A  ]G!‫ا‬

tuples ‫ ام‬I ‫ا‬


Example 9.2. Using Tuples

#!/usr/bin/python
# Filename: using_tuple.py

zoo = ('wolf', 'elephant', 'penguin')


print 'Number of animals in the zoo is', len(zoo)

new_zoo = ('monkey', 'dolphin', zoo)


print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]

Output

$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


tuple ‫ ل‬F N<@ ‫` ل‬M< 5 ‫ ی! ا)]ا‬len ‫ ﻥى ان اا‬M‫ وﻥ‬.‫ د‬H‫ ا‬tuple N‫ ی ا‬zoo >!‫ا‬
i new_ zoo N‫ اﻥت إ‬M‫ ا‬T"‫ ه‬C‫ن ﻥ‬q‫ا‬. ;"‫ آ‬sequence ‫<ة‬G<G! (‫ ه‬tuple ‫ أن‬N<@ b‫وه"ا یل أی‬
N‫ إ‬H‫ ﺝ‬CQ49 ‫ اﻥت ا! ﺝ دة‬M‫ ا‬kQ9 N<@ new_zoo tuple ‫ ي‬M ‫ "ا‬. <> YH` !‫ ای‬zoo ‫أن ال‬
tuple C.‫ دا‬tuple ‫_ أن ال‬d‫  ﻥ‬P‫ وا
ا‬N‫ إ‬m9‫ و‬,. !‫ ای‬zoo ‫ ال‬cH<‫ اﻥت ا( ﺝ‬M‫ ا‬2‫ﺝ‬
، 5‫ ه ی‬4 A

[ ] Q9!‫
اس ا‬P‫ زوج ا‬C.‫ دا‬Q‫ ا‬Z ‫ی‬M n‫ی‬F @ tuple ‫( ال‬6Q‫ ا‬N‫ی! ا  ل إ‬
H‫ ا‬N‫ ا  ل إ‬gG‫ ﻥ‬.indexing operator )54‫ ا‬C @ N!G‫ وه"ا  ی‬. lists L‫( ا ا‬6 T<Q6  UH
‫ی‬M9 new_zoo tuple (6 i8‫ ا‬H‫ ا‬N‫ وی! ا  ل إ‬، new_zoo [2] ‫ی‬M9 new_zoo (6 i8‫ا‬
. ‫)< ب‬P‫ ا‬54 ‫د ان‬3!9 ، ‫ ﺝا‬YZ‫ ه"ا وا‬new_zoo [2] [2].

myempty = (). C8 ( )‫
اس‬P‫ زوج ا‬n‫ی‬F @  W‫ر‬4‫ ا‬Tuple ‫ ال‬:Tuple with 0 or 1 items
( ‫ول )وا‬A‫ ا‬H‫ ا‬Q9 ، <6  ]G ‫ده‬M ‫ @<; ان‬.FGH‫ ا‬T"59 p ‫ وا‬9 59 tuple ، ;‫و ذ‬
. HQ‫ ا‬C.‫ دا‬object C gM!‫
اس ا‬P‫ ا‬9 ‫ زوج‬9‫ و‬tuple 9 ‫ق‬4‫ ن ی! ان ی‬8‫ی‬9 ‫ن‬h6 ;" ‫ ؛‬H‫ا‬
.2 H‫! ا‬b‫ أن ی‬tuple ‫( أﻥ; ی‬Q c‫ اذا آ‬singleton = (2 , ) ‫د‬M ‫ @<; ان‬d8

‫!ل‬$‫ ﺏ‬/!7 9‫ﺡ‬:


tuple ‫ ال‬N<@ nHg‫  ی‬P‫ ا‬p4‫ ﻥ‬. ‫ل‬9 (6 ‫ آ! ه‬gGH W L‫ ا ا‬d8!6. 5‫ ه ی‬4 A !L
C.‫! دا‬L‫ا‬
‫رة‬H@ e6 U‫ﻥ‬h6 ، ‫ ن‬8‫ی‬H9  P‫ ا‬n<Q‫ر  ی‬H6 . &‫ إ‬tuple C.‫ دا‬list ‫ أو‬،list C.‫ دا‬tuple ‫ أو ال‬،tuple ‫ ال‬C.‫دا‬
. ‫ ع‬Z !‫( ا‬6  C‫ ه"ا آ‬،.o object L‫)]ام آ‬9 ‫ ]^ﻥ‬objects ‫ت‬L‫@ آ‬
: Tuples and the print statement*
:‫ل‬8!‫ وإ; ه"ا ا‬.print ‫ن‬H‫ ا‬tuples (‫ ه‬QL‫ت ا‬A!Q)A‫ ا‬8‫واة أآ‬
Example 9.3. Output using tuples

#!/usr/bin/python
# Filename: print_tuple.py

age = 22
name = 'Swaroop'

print '%s is %d years old' % (name, age)


print 'Why is %s playing with that python?' % name

Output

$ python print_tuple.py
Swaroop is 22 years old
Why is Swaroop playing with that python?

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ د‬H‫ ا‬tuple 5<‫ ی‬٪ ^ ‫ ا‬5QH‫ ی‬Q ‫ت‬4‫)]ام ا‬9 `‫< ﻥ‬G<) ".‫ ی! ان ی‬print ‫ن‬H‫• ا‬
N<@ ‫ت ی! ان  ن‬4‫ ا! ا‬.Q ‫ی‬g9 RL‫ ا‬UW (6 ‫]م‬G ‫ت‬4‫ ا! ا‬.‫ت‬4‫ <! ا‬U9g!‫ا‬
<9 ‫ د‬9 N<@ ‫ ي‬M ‫ ان‬23‫ ی‬tuple .MM`‫@اد ا‬² %d ‫ و‬strings `‫ ا‬C)dG< %s ‫ار‬W
. ‫م‬m‫ ا‬p4‫( ﻥ‬6 ‫ت‬4‫ ا! ا‬T"5
، tuple (6 ‫ول‬A‫ ا‬H‫) ا!> ا"ي ه ا‬A 9g ‫ وه"ا‬A‫ أو‬%s ‫]م‬G‫ ﻥ‬i ‫!ل‬Q)‫_ أن اول ا‬A •
.tuple (6 (‫ﻥ‬8‫ ا‬H‫ ا"ي ه ا‬age C C9!‫ ا‬%d ‫ﻥ( ه‬8‫وا  ا‬
‫ ن‬C.‫< دا‬G<G‫ ا‬T"‫ ! ه‬CL‫ا‬9‫< ﻥ` و‬G<) N‫ ا‬tuple (6 9 C‫ ل آ‬M‫ ی‬U‫ ن ه ه أﻥ‬8‫ی‬9 U<!Q‫•  ی‬
.‫ وه< ﺝا‬name >!‫ @
! ا‬ZQ)‫ ه ا‬%s ‫ "ا‬.‫ت‬4‫ا! ا‬
string ‫ل‬9 2@d‫ ا‬8‫ ا‬23‫ وی‬R‫ ا‬9‫ <>ی آ‬C5G‫ ا‬CQ3‫ ی‬print ‫ن‬H< ‫)]ام‬A‫• ه"ا ا‬
.‫ن‬q‫ ا‬N <Q6 !‫ ن آ‬C‫( آ‬6 C‫ ا‬4‫!ل ا‬Q)‫ ا‬23‫ ی‬U‫ آ! اﻥ‬. P‫ ذات ا‬nM
N C!Q‫ وه"ا ی‬.;<‫
( أﺝ‬H9 ‫ی‬Q‫ ن ا‬8‫ی‬H ‫ واك‬.%s  ‫ ی!; ا)]ام ا‬، c
‫ ا‬mQ •
‫ ى وا اآ‬G b‫ أن ه"ا ی‬i‫ ؛ی‬UMM`‫ت ا‬4‫ء ا! ا‬g@‫( ا‬6 2W 
، ;‫ و ذ‬.‫ر
م‬P‫ا‬
. ;3 ‫ﻥ‬9 M
‫ ی ﺝ زوج‬A -- ‫ وا‬9 U<‫ ی‬% ^ ‫ ا‬5QH‫ت ا( ی‬4‫]م أ ا! ا‬G‫ ﻥ‬،print (‫ﻥ‬8‫ن ا‬H‫( ا‬6 •
.`‫< ا‬G<G‫( ا‬6 ‫(  @  ی ن هك و وا‬6 e6 C!Q‫ ه"ا ی‬.‫
اس‬P‫ا‬

‫*ا
س‬
U!)‫ ا‬U6Q n‫ی‬F @ -]‫`ل ﺵ‬d C4 ‫ @ ان أو‬3 ‫ ی!; أن‬i ‫ آب @ ﻥ‬98!9 ‫ا س ه‬
unique ‫یا‬6 ‫ أن ی ن‬23‫ح ی‬4!‫ن ا‬9 !<@ .(C4‫)( ا )ا‬A‫ )ا‬Y4!‫ ﻥرك ا‬M‫ ؛ ﻥ‬d8 . 5!)‫ا‬/
. eHb9 )A‫ ا‬p49 ‫ إذا آن ی; ﺵ]`ن‬MM ‫< ت‬Q N<@ ‫` ل‬M‫ ی!; ا‬A U‫ أﻥ‬i

ojects ‫ ا س و ی!; ا)]ام‬Y4! (`‫ ا‬C)dG‫ ا‬C8 ) e6 9‫ ﺙ‬ojects ‫ ی!; ا)]ام‬U‫@<! اﻥ‬
. Y4!< gG9 ‫]م ) ى اﺵء‬G A ‫>( ان‬H‫ ی‬U‫  أﻥ‬9 ;‫< <>  ا س ی! أن ﻥﺝ ذ‬9
‫ أو‬9‫ﺙ‬

_A d = {key1 : value1, key2 : value2 } ‫رة‬HQ‫)]ام ا‬9 ‫( ا س‬6 T‫ وا ا!"آ ر‬Y4!‫زوج ا‬
‫ ه"ا‬C‫ و آ‬، <6 n‫ی‬F @ ‫ن‬d`4 5G4‫زواج أﻥ‬P‫" وا‬:" g‫ ا‬n‫ی‬F @ <`4 !‫ا‬/‫ح‬4!‫أن أزواج ا‬
. { } ‫ة‬Q3!‫
اس ا‬A‫( زوج ا‬6 C.‫دا‬

5H ;<@ Q) ، Q H ‫ اذا اردت‬.2 ‫ی‬F ‫ أي‬5 cG ‫( ا س‬6 key/value ‫"آ ان أزواج‬
. 5!Q)‫ ا‬CH
;G49

. "dict" Hg‫ ا‬instances/objects ‫ت‬L‫آ‬/<8 ‫ أ‬HQ 5 ‫)]ا‬9 ‫ ال ) م‬p ‫ا ا‬

Q$‫ ام ا
ا‬I ‫*ا‬
Example 9.4. Using dictionaries

#!/usr/bin/python
# Filename: using_dict.py

# 'ab' is short for 'a'ddress'b'ook

ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}

print "Swaroop's address is %s" % ab['Swaroop']

# Adding a key/value pair


ab['Guido'] = 'guido@python.org'

# Deleting a key/value pair


del ab['Spammer']

print '\nThere are %d contacts in the address-book\n' % len(ab)

for name, address in ab.items():


print 'Contact %s at %s' % (name, address)

if 'Guido' in ab: # OR ab.has_key('Guido')


print "\nGuido's address is %s" % ab['Guido']
Output

$ python using_dict.py
Swaroop's address is swaroopch@byteofpython.info

There are 4 contacts in the address-book

Contact Swaroop at swaroopch@byteofpython.info


Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org

Guido's address is guido@python.org

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ح‬4!‫ی ا‬M ‫ل‬d. key/value ‫ﺙ ﺵ>< أزواج‬. U
 nH) ‫)]ام ا
 ا"ي‬9 ab ‫` ا س‬9 !

eG9 2‫_ ان اآ‬d‫ ﻥ‬tuples. ‫ و‬lists @ ‫م‬d‫( ا‬6 }


‫ آ! ﻥ‬indexing operator )54‫ ا‬C @ ‫)]ام‬9
. ;"‫ آ‬p ‫ﺝا < ا‬

N‫ <  ل ا‬indexing operator ‫ ا)]ام‬n‫ی‬F @ FGH9 key/value ‫ أزواج ﺝیة‬b‫وی! ان ﻥ‬
. Td@‫ ا!"آ رة ا‬M‫( ا‬6 Guido ‫< ل‬Q6 !‫ آ‬، U‫ح وا)د
! إ‬4

indexing ‫ی ا س‬M‫ ﻥ‬FGH9 M‫ ﻥ‬." del" ‫ن‬H‫)]ام ی ای ا‬9 !‫ا‬/‫ح‬4!‫ی! "ف أزواج ا‬
T"‫( ه‬6 ‫ح‬4!< <9!‫ ا‬U!‫ ا‬6Q! ‫ هك ﺝ‬cG ."del" ‫ن‬H‫ ا‬N‫ح و!ی ذ; إ‬4!‫ زا ا‬operator
. <!Q‫ا‬

!L
Q (‫ ا س ا‬items method ‫)]ام‬9 ‫( ا س‬6 key/value ‫ زوج‬C‫ آ‬N‫ إ‬C`‫ ذ; ﻥ‬Q9
N‫ إ‬TG‫ ه"ا ا^وج وﻥ‬2MG‫ ﻥ‬.!9 @ H ‫ح‬4!‫ د – وا‬H‫ ي زوﺝ ا‬M‫ ی‬tuple C‫ آ‬i tuples ‫ ال‬
.for-block <‫( آ‬6 ‫ ا‬T"‫ ه‬Hg‫ ﺙ ﻥ‬، for..in <M‫)]ام ا‬9 ‫ زوج‬C C9!‫ ان ا‬Q‫ا) ا!>ات وا‬
class ‫ ال‬has_key ‫ی‬F N ‫ او‬in C>!‫)]ام ا‬9 ‫ ﺝ د‬key/value ‫  اذا آن زوج‬6Q !‫ی‬
help(dict) ‫)]ام‬9 "dict" class ‫ق ال‬g< < ‫! ا‬L‫ ا‬N<@ ‫ع‬dFd nL‫ ان ى ا ﺙ‬gG "dict"

:Keyword Arguments and Dictionaries*


‫ ان‬nH) ‫ و‬، ;9 ]‫( اوال ا‬6 keyword arguments c ])‫
 ا‬c‫ ان آ‬، _d‫ ﻥ‬.o Q N<@
@‫ و‬، ‫ی اا‬Q ‫را ات‬9 !L
(6 ;ِ<Hِ
‫د‬M key/value ‫( ذ; – زوج‬6 6 e6! p ‫
ا‬c ])‫ا‬
(6 symbol table N!G‫ ا س )وه  ی‬N‫ح ا  ل إ‬4 ‫د‬3 ‫ وه‬، ‫ اا‬C.‫ا‬9 ‫ ا!>ات‬C>
. ( ‫ `! ا!ﺝ‬Y<g`
:Sequences ‫ت‬:1-1 ‫*ا‬
56 ‫ و ذا‬، ‫<ة‬G<G!!‫ و  ه( ا‬، Sequences ‫<ات‬G<G!!‫ ا‬N<@ <8 ‫ ه( أ‬strings ‫ و‬lists ‫ و‬tuples
(6 UQ9 9 2<‫  ان ﺝ‬Y (‫) ا‬54‫<ة ه( @!< ا‬G<G!!< GL‫!ت ا‬G‫ا]`  ؟ اﺙن ا‬
. ‫<ة‬G<G!!‫<ة أي ﺝ^ءا ا‬G<G!!‫ ا‬M‫ ﺵی‬QG ‫  ان‬Y‫ ا"ي ی‬g‫ و@!< ا‬، ‫ﺵة‬H ‫<ة‬G<G!!‫ا‬
‫ت‬:1-1 ‫ ام ا‬I ‫*ا‬
Example 9.5. Using Sequences

#!/usr/bin/python
# Filename: seq.py

shoplist = ['apple', 'mango', 'carrot', 'banana']

# Indexing or 'Subscription' operation


print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]

# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]

# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]

Output

$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


<!@ U‫ اﻥ‬N<@ U‫ یر ا‬b‫ وه"ا ای‬.<G<G!‫دی ا‬6 @ N<@ ‫` ل‬M< ‫رس‬54‫ ا)]ام ا‬4‫ ﻥى آ‬، A‫او‬
C9!‫ ا‬H‫ ن ا‬8‫ی‬9 ; 2<3‫ ) ف ی‬، Td@‫ ا‬H ‫ ] [ آ! ه‬6 Q 9 <G<G!< ‫ی @د‬M9 c!
!<‫ آ‬.‫آب‬A‫ا‬
‫ول‬A‫ ا‬H‫ ا‬2<3‫ ی‬shoplist [0] ، ‫ و ه‬.0 ‫ر
م‬A‫أ @ ا‬H‫ ن ی‬8‫ی‬9 ‫ ﻥ"آ ان‬. <G<G!‫( ا‬6 UQZ !
shoplist <G<G (6 9‫ اا‬H‫ ا‬2<3‫ ی‬shoplist [3] ‫و‬

‫ ی‬shoplist [-1] ، ‫ "ا‬.‫<و‬G<G!!‫ی ا‬5‫ ﻥ‬2GM‫ ی‬، UM‫ ا‬T"‫( ه‬6 ، H<) ‫ ان ی ن @دا‬b‫س ای‬54< !‫ی‬
. <G<G!!‫( ا‬6 9 .o (‫ ﺙﻥ‬2<3‫ ی‬shoplist [-2] ‫<ة و‬G<G!!‫( ا‬6 .P‫ ا‬H‫ ا‬N‫ا‬

‫ر
م‬P‫ زوج ا‬-‫ری‬.‫ا‬- 5<‫< ی‬G<G!‫ی ا) ا‬M n‫ی‬F @ ‫]م‬G slicing operation g‫@!< ا‬
c!

(‫) ا‬54‫ ﺝا @!< ا‬Q9  N‫ إ‬UH‫  ی‬P‫_ ان ه"ا ا‬d‫ ﻥ‬. [:] Q9 )
C.‫ دا‬g9  `4
. ;"‫ آ‬cG ":" ‫ن اأ)ن‬g‫ری و ا‬.‫ر
م ا‬A‫ "آ أن ا‬.5!Q)9

Q9) (‫ﻥ‬8‫د ا‬Q‫ وا‬، M‫ ای‬U ‫أ‬H ‫ ا"ي‬Z !‫ ا‬N‫ ی ا‬g‫( @!< ا‬6 ( g‫ ا‬CH
) ‫ول‬P‫ا
 ا‬
‫ای‬9 ‫أ‬H) ‫ ن‬8‫ی‬9 ‫ن‬h6 ، ‫د‬M W ‫ إذا آن اول @د‬.M‫ ای‬T@ 
 ‫ ا"ي‬Z !< 56 ‫ ( ی‬g‫ا‬
@ ‫ء‬H‫ود ا‬Q M‫ @<! أن ای‬.<G<G!‫ی ا‬5‫( ﻥ‬6 
) ‫ ن‬8‫ی‬9 ‫ن‬h6 ‫ﻥ( وآ‬8‫ واذا آن ا
 ا‬.<G<G!‫ا‬
QHG 56 ‫ء‬5‫ﻥ‬A‫ ا‬Z  ‫ف و أ‬b‫ای ی‬H‫ ا‬Z ‫؛‬d8 . ‫ء‬5‫ﻥ‬A‫ ا‬Z CH
(5)‫ و‬،‫ای‬H‫ ا‬Z
. <G<G!‫ ا‬M‫ﺵی‬

@ 
‫ و ی‬، 2 Z N‫ إ‬6Z9 1 Z !‫ءا ا‬9 <G<G!‫ ا‬Qg
Q shoplist [1:3] ، ‫وه"ا‬
<G<G!‫] ا‬G‫ ﻥ‬Q shoplist [:] ، C8!9‫ و‬.‫ د‬Q‫ی ی‬H‫ ه"ی ا‬Qg
‫ن هك‬h6 (9‫ و‬، 3 Z !‫ا‬
. 5<!‫آ‬9

، ‫ل‬8!‫ ا‬CH) N<@ .<G<G!‫ی ا‬5‫ ﻥ‬Z‫ <! ا‬HG‫ر


م ا‬A‫]م ا‬G‫ و‬.HG‫ ا‬Z‫ ا! ا‬g b‫ی!; ای‬
‫! أي ﺵ(ء‬b‫ ی‬A U‫ و‬، <G<G!‫( ا‬6 .P‫ ا‬H‫( ا‬8G (‫< ا‬G<G!‫ ا‬Qg
Q) shoplist [: -1]
. .o

‫ ی!; ان‬iM9 ‫ ري‬4‫ ا‬iM!‫أي ا‬. (<@4‫ ن ا‬8‫ی‬9 G4 ‫)]ام‬9 M‫ ای‬T"‫ت ه‬4 4<] ‫ت‬4  ‫ﺝب‬
p49 !3‫ ا‬، strings ‫ و‬lists ‫ و‬،tuples C> ;!‫ت ه أﻥ; ی‬dG<G!‫( ا‬6 mQ‫ وا(ء ا‬.‫ را‬6 RL‫ى ا‬
!‫ی‬g‫ا‬
References
!U‫(  ذا‬6 object C8!‫ ی‬A‫ و‬object N‫ ا‬A‫  ا!> إ‬A ، ‫ أ ا!>ات‬N‫ ا‬G‫ وی‬object `  @
‫ت‬L‫ ا‬U6 ‫ ]^ن‬i  H!‫^ء ذاآة ا‬3‫ ذ; ا‬N‫ أي أن ا) ا!> ی إ‬، ‫ ا!اد‬NQ!‫وه"ا ه ا‬
. object ‫ ال‬N‫) إ‬A‫ ا‬binding e9‫ ر‬N!G‫ وه"ا  ی‬.objects

N‫ج ا‬M (‫ ا‬references 2HG9 n


‫ و ﺙ! ﺙ ر‬،  A‫ إزاء ه"ا ا‬n<9 Q ‫ ان‬N‫ﺝ ا‬M9 cG ،  !@
: (‫ل ا‬8!‫ ذ; ا‬Yb‫ وی‬. 59 <@ N<@ ‫أن  ن‬

Example 9.6. Objects and References


#!/usr/bin/python
# Filename: reference.py

print 'Simple Assignment'


shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!

del shoplist[0] # I purchased the first item, so I remove it from the list

print 'shoplist is', shoplist


print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object

print 'Copy by making a full slice'


mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item

print 'shoplist is', shoplist


print 'mylist is', mylist
# notice that now the two lists are different

Output

$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


5G4‫<ت ﻥ‬Q‫( ا‬6 ‫ اح ح‬mQ . ‫! او‬L‫] ا‬G‫ ﻥ‬CQ3 ‫ اﻥ; اذا اردت ان‬T‫ ان ﻥ"آ‬N‫اج ا‬M ‫ ا"ي‬P‫ا‬
(MM`‫@اد ا‬P‫ ا‬C8 gG9 ‫ت‬L‫ آ‬cG) ‫ة‬Q!‫ت ا‬L‫ت أو ا‬dG<G!‫ﻥ اع ا‬A‫ن @<; أن <; ا‬h6 ،
g‫]م @!< ا‬Gslicing operation !‫ه‬d‫ آ‬، .o )‫ ا‬N‫د ا)د ا) ا!> ا‬3!9 e6 c!
‫ اذا‬.]G‫ ﻥ‬C!Q
‫ اذا   "را‬2@!‫ ﺝ! اﻥ اع ا‬N‫"ا ی! ان یدي ا‬56 ، U‫ ذا‬L‫ ا‬N‫ی ا‬.

: ‫!ل‬$‫ ﺏ‬/!7 9‫ﺡ‬:

"$?
‫ ﺕ‬$- A‫ أن ﺕ
م ﺏ‬J$-G ، *D I1‫ ﻥ‬oND k @‫ ا
ا‬F-p ‫ن‬$‫د ﺏ‬D ‫ ﺕآ! أن إ‬slicing operation 0 A
. -1-1 ‫  ا‬I1‫ﻥ‬

strings $2D ‫ ا‬0 :1 ‫ ا‬G = ‫*ا‬


‫؟‬5@ U6Q !‫  ا!^ی ا"ي ی‬. 9) c
‫( و‬6 C`49 `‫ ا‬C)dG‫ ا‬CQ49 
‫ ﻥ‬
b‫ ه( ای‬HQ `‫ ا‬C)dG‫ف ان ا‬Q C‫ ه‬، G objects ‫ ﺝ^ء‬-M6 ‫ ﺵ(ء أول‬C‫ آ‬CQ4 2)A‫ ا‬5‫وی‬
‫ت‬G!‫ی ا‬Q N -‫! ا‬

‫ال‬sjrings‫ ه( ﺝ! ال‬R ‫ﻥ‬H‫( ا‬6 5 ]G (‫ ا‬objects ‫ ال‬class) str T"5 ‫ة‬4!‫ ا‬2)P‫ ا‬kQ9 .(
m‫ اﻥ‬، 2)A‫ ا‬T"‫! آ < ه‬L
N<@ ‫` ل‬M<‫ و‬.(‫ل ا‬8!‫( ا‬6 N<3 |4‫ا‬help(str).
Example 9.7. String Methods

#!/usr/bin/python
# Filename: str_methods.py

name = 'Swaroop' # This is a string object

if name.startswith('Swa'):
print 'Yes, the string starts with "Swa"'

if 'a' in name:
print 'Yes, it contains the string "a"'

if name.find('war') != -1:
print 'Yes, it contains the string "war"'

delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)

Output

$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


`‫ ا‬C)dG‫ ا‬2)‫ أ‬8‫ ﻥى ا‬، ‫ ه‬stringsn‫ی‬F C!Q‫ ا‬C.‫دا‬. Startswith ‫  اذا‬6Q! ‫]م‬G
C>!‫ ا‬.‫ة‬gQ!‫!< ا‬3‫أ ا‬H `‫< ا‬G<G‫ ا‬c‫آﻥ‬in ‫ ه ﺝ^ء‬NgQ!‫ ا‬-‫  اذا آن ا‬-M4 ‫]م‬G‫ی‬
.`‫< ا‬G<G‫ا‬.

‫ی‬Ffind (6 NgQ!‫ ا‬-‫ ا‬Z ‫د‬3‫ی‬A ‫]م‬G string -‫ ا‬N<@ ‫ ر‬8Q‫( ا‬6 ‫ح‬3‫ اذا  ی ا‬-1 ‫أو إ@دة‬
|4‫ ا‬.‫ﻥ ي‬8‫ا‬str `‫< ا‬G<G‫< ا‬G<G!‫ د ا‬9 e9‫( ر‬6 @‫ر‬9 U‫ی‬F b‫ ای‬5 string 9 ‫د‬M 54`9
<G<G!‫ ا‬9 C‫آ‬sequence . 5 ‫< ﻥ`  ة‬G<) H‫ أآ‬Q‫و‬
‫ﺹ‬:I ‫ا‬
R ‫ا‬9 9‫ ) ن ا)) @ آ‬T"‫ﻥت ه‬H‫ ا‬C‫ هآ‬.C`49 ‫ ن‬8‫ی‬9 (6 3 !‫ﻥت ا‬H‫ ا‬C‫)ف هآ‬9 !

‫ ل‬Q 3M9.

Q‫( ا‬6 R ‫ﻥ‬9 9‫ `! وآ‬4‫! ی<( آ‬6 ‫ و ) ف ﻥى‬،‫( ن وا‬6 ‫ ن‬8‫ی‬9 ‫ أ))ت‬8‫ن ی ا‬A‫وا‬
‫ ن‬8‫ی‬H (M‫ا‬.

!(A ‫ ا‬02, ‫ا‬


‫ن‬6‫ ﺏ‬+ c‫ !ﺏ‬K‫ آﺏ‬- -N 0‫ﺡ‬

Table of Contents................................................................................ ‫  ا ت‬


The Problem ................................................................................................... -N ‫ا‬
The Solution ........................................................................................................ 0 ‫ا‬
First Version.........................................................................{c‫!ﺏ‬1- } F ‫و‬k‫ﺹ ارة ا‬r‫ا‬
Second Version .. .................................................................................$‫ﻥ‬6 ‫ﺹ ارة ا‬r‫ا‬
Third Version ....................................................................................... 6 6 ‫ﺹ ارة ا‬r‫ا‬
Fourth Version .................................................................................... A‫ﺹ ارة ا !اﺏ‬r‫ا‬
More Refinements ...............................................................................* ‫=  ا‬
The Software Development Process ........................................ ‫ت‬$/!7 ‫ ﺕ?!ا‬$- G
Summary ........................................................................................................ ‫ﺹ‬:%

، Q ‫ﺝ^اء‬A‫ ا‬T"‫ ﺝ! ه‬2) (‫ی ا‬g‫ ا‬N<@ ‫ة‬m‫ن ) ف ﻥ<( ﻥ‬q‫ ن وا‬8‫ی‬9 > 4<] ‫ أﺝ^اء‬c4)‫ ا‬
.4 ‫ ﺵ(ء‬A ‫ ا"ي‬R ‫ﻥ‬H‫ ا‬9‫ `! وآ‬n‫ی‬F @

-N ‫*ا‬
. ‫! ي‬5!‫ت ا‬4<!‫ ﺝ! ا‬F‫] ا‬G‫ ﻥ‬C!Q9 ‫ ی م‬3 ‫ﻥ‬9 ‫ أﻥ( أری‬N‫ا!< ه‬
‫ ه‬C<M‫ ا‬C<‫ ا‬.CM‫أ @!< ا‬H  HG9 6‫< ت آ‬Q ‫ هك‬cG ، gG9 < C‫ أن ه"ا ی‬W‫ور‬
]G‫ ا‬Z ) ‫ ؟ أی‬F‫ ا‬5]G‫ت ا( ) ﻥ‬4<!‫د ا‬M‫ آ ی! أن ﻥ‬، ‫ل‬8!‫ ا‬CH) N<@ .‫< ب‬g!‫ا‬
‫؟‬UFA‫] ا‬G‫( ا‬6 5‫ ؟ آ ی ]^ی‬U‫ ا!]^ﻥ‬UFA‫ا‬

T"‫( ه‬6‫ و‬.3 ‫ﻥ‬9 C!@ 4‫ ر  ل آ‬P‫! ا‬L


^539 ‫ ﻥ م‬.3 ‫ﻥ‬9 !`‫ ﻥ‬، YM C9 <!‫ ا‬C<M Q9
‫ ا ع‬p4‫ ﻥ‬U‫  اﺝ‬A ;<Q ، !`‫ ا‬C!Q9 c!
‫ اذا‬.C!Q9 5 
4‫ن آ‬9 ‫! ا‬L‫ﻥء ا‬h9 c!
، M‫ا‬
.2F  ‫ وه"ا أ‬، ‫ ر‬P‫ ا‬G ]‫ ا‬U‫ی‬F U -]‫ ﺵ‬C‫ آ‬-- C‫ا!آ‬

.!L
N6 ‫دة‬M F‫] ا‬G‫ ﻥ‬5 C!Q‫د ا( ﻥ‬P‫ت وا‬4<!‫ ا‬.1

.(FA‫& ا‬G< pL‫ ا‬C‫( ا‬6 ‫ أن  ن ]^ﻥ‬23‫ ی‬FA‫] ا‬G‫ ا‬.2

.‫> ط‬b < (6 ‫>( أن  ن‬H‫ ی‬. G!‫ت ا‬4<!‫ ا‬.3

.(M‫ ا‬c
‫! اری& وا‬b‫> ط ی‬b!‫رﺵ ا‬P‫ ا) ا‬.4

!‫ وی‬.p‫ ی ﻥ‬/ p )


Q‫( أي  زی‬6 (Z‫ا‬6‫ ا‬C9 ‫ ا!ح‬zip ()‫  ا‬P‫]م ا‬G‫ ﻥ‬M‫ ﻥ‬.5
T‫ ی‬4‫ أرﺵ‬R ‫ﻥ‬H  ‫ ی!; ا)]ام اي أ‬U‫ – @<! اﻥ‬Info-Zip program R ‫ﻥ‬9 ‫] ( ویوز ا)]ام‬G!
.9 ‫ ا]ص‬c9G‫ ا‬U‫ إ‬arguments 
‫  !ی‬NG‫ ی‬N  ‫وا‬A‫ ا‬g) ;<!‫ ی‬U‫! اﻥ‬F

*:0 ‫ا‬
. CM‫" ا‬4 ‫ أدا‬HQُ‫ ا د ا"ي ی‬2‫ ی! أن ﻥ‬، G ‫ن‬q‫ ا‬3 ‫ﻥ‬9 !` ‫وآ! أن‬

First Version ‫ول‬4‫ﺹ ار ا‬r‫ا‬


Example 10.1. Backup Script - The First Version
#!/usr/bin/python
# Filename: backup_ver1.py

import os
import time

# 1. The files and directories to be backed up are specified in a list.


source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or
something like that

# 2. The backup must be stored in a main backup directory


target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.


# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup


if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

Output

$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip

،
 ‫ذا  ی`ف آ! ه‬h6. <) C9 C!Q‫ ی‬C‫ه‬، 3 ‫ﻥ‬9 H]‫ إﻥ ﻥ‬i ‫ر ؛‬H.A‫( < ا‬6 ‫ن‬q‫ ا‬M‫• ﻥ‬
. R ‫ﻥ‬H‫ء( ا‬g.A‫ )ا‬bugs ‫ ؛ اي ازا اـ‬3 ‫ﻥ‬9 YM` < N‫ﻥل ا‬A‫ ن @< ا‬G6
b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬
time ‫ و‬os ^<‫ ا! دی‬4G‫ ﻥ‬M‫وﻥ‬. T g]6 ‫ ة‬g. ‫ ا د‬N‫  ی `! ا‬C‫ ی‬M9 !
‫_ آ‬d)
target " C‫ ا‬."source" !L
(6 F‫ ا‬5]G‫ ا( ) ﻥ‬U‫د‬P‫ت وا‬4<!‫د ا‬M‫ ﻥ‬، ‫ ﺙ‬.‫)اده‬9 !
‫و"ا‬
‫رﺵ‬P‫" ا) ا‬target_dir ">!‫( ا‬6 ‫د‬M!‫ وه"ا ه ا‬، UFA‫ت ا‬4<!‫( ن ]^ی ﺝ! ا‬Q‫" ی‬
. time.strftime () ‫)]ام اا‬9  2<3‫ ا"ي ی‬c
‫( وا‬M‫ ه اری& ا‬UL‫ﻥ‬h9 ‫> ط ه ) م‬b!‫ا‬
target_dir C‫( ا‬6 ‫ و)]^ن‬.zip ‫ اد‬9 .b‫و) ف ی ن ای‬

CM) %Y 4`‫ ا‬. Td@‫ ا!"آ ر ا‬R ‫ﻥ‬H‫( ا‬6 ‫ ا( ا)] ه‬C8 ‫ت‬4‫" ا‬. time.strftime ()‫اا‬
.‫ وه< ﺝا‬12 ‫ و‬01 9 ‫ ر
 @ي‬54 9 5‫ ا‬5<M CM) %m 4`‫ وا‬، ‫ون ان‬9 G‫ ا‬5<M
[Python Reference [‫ ن‬8‫ی‬H (Q‫ ا!ﺝ‬C‫( ]ا‬6 5<@ ‫ ر‬8Q‫ت ی! ا‬4‫ ا! ا‬T"5 < ‫! ا‬L‫وا‬
(UG4‫ ﻥ‬M‫ ا‬N<@ p ‫ )و‬C‫_ ان ه"ا ه !ﺙ‬A .;9 ]‫ ا‬Q‫( ا زی‬6 ‫ ن‬8‫ی‬9 (‫ ا"ي ی‬Manual]
( tuple 5< ٪ ‫)]ام‬9) print ‫ن‬H‫( ا‬6  ]G!‫ت ا‬4‫<! ا‬

Q ‫ اﺙ‬9 e9‫ اي ی‬C!3‫; ا‬H‫( ا"ي ی‬6Z‫ ا‬C>!‫)]ام ا‬9 target ‫> ط‬b!‫ ا‬C‫ ا) ا‬C!Q9 !

.T"49 ‫  ) م‬P‫! ا‬b (‫ وا‬، zip_command : `‫< ﻥ‬G<) —‫ ﻥ‬، ‫ ﺙ‬.‫ه إ واة ﺝیة‬Q‫وی‬
( ‫ أو ﺵ دوس‬6F p 6F) C‫ ا‬N<@ U<> n‫ی‬F @ C!Q‫  ی‬A‫  اذا آن ه"ا ا‬6Q ;!‫ی‬

zip  P‫ ان ا‬N‫]م ¦ﺵرة إ‬G‫ ی‬q ‫را ات – ا]ر‬H‫ ا]رات وا‬kQ9 ‫ ي‬M‫ ی‬U ]G‫ ا"ي ﻥ‬zip  P‫ا‬
N<@‫د } أ‬² recursively C!Q ‫ ان‬zip  P‫د ان ا‬M‫ ی‬r‫ – ا]ر‬quietly ‫وء‬59 C!Q‫>( أن ی‬H‫ی‬
i‫ ﺙ‬A ‫ری‬. 9 !3‫ و
  ا‬.;"‫@ آ‬4‫ ا‬U‫د‬A‫ ا‬C.‫ت دا‬4<!‫@ وا‬4‫ ا‬U‫د‬A‫ ا‬C! ‫>( ان‬H‫{اي ی‬C4)P
@ H T‫> ط ا!اد إﻥؤ‬b!‫رﺵ ا‬P‫) ا‬9 @ H ‫ ا]رات‬T"‫ – ه‬qr !‫ وه‬n‫ی‬g‫( ا
` ا‬6 ‫د‬M!‫! وا‬5
U‫ی‬F ‫)]ام‬9 <!3‫ ا‬C.‫ دا‬source !L
‫ ل‬M‫ ﻥ‬M‫ ﻥ‬.F‫ ا‬5]G9 ‫ ا( ) م‬U‫د‬A‫ت وا‬4<!‫! ا‬L9
. 5 ‫ ا)]ا‬4‫ آ‬CQ49 ‫ وا( ﺵهﻥ‬C!3‫ ا‬b join

0  Q‫ – وه ی‬C‫( ا‬6 ‫م‬m‫ ا‬C.‫ دا‬C!Q‫ آ!  آن ی‬os.system ‫)]ام اا‬9  P‫ ا‬C>‫ا ﻥ‬.‫وأ‬
. g]‫ إ ر
 ا‬Q‫ ی‬U‫ﻥ‬6 A‫ وإ‬، ‫ح‬39 <!Q‫ ا‬c! ‫إذا‬

C‫ وه"ا ه آ‬، cM3‫ أو ﻥ‬c<6 UFA‫] ا‬G‫ن ا‬9 H) )‫@ ر‬Hg9 ‫ وﻥ م‬،  P‫ ا‬3‫ ﻥ‬N<@ ‫وا@!دا‬
!!5!‫ت ا‬4<!‫ ا‬UF‫] ا‬G‫ ﻥ‬C!Q c9) ‫ﻥء‬9 !
 ، ‫ ع‬Z !‫( ا‬6 

: ‫ وز‬D‫  و‬I1 9‫ﺡ‬:

.‫( ویوز‬6 d<


ً‫ ان  ن ﻥ‬23‫ و ی‬، C‫) أي < أو د‬P target C‫ ا‬source !L‫د ا‬M ‫ی!; ان‬
C8! (\) backslashs ‫]م‬G‫ ن ی‬8‫ی‬9 ‫و‬، C`4 C‫)\( آ‬backslash ‫]م‬G‫وا!< ه( ان ویوز ی‬
! escape sequences ‫وب‬5‫ ا‬C)d)

N<@ .raw strings ‫]م‬G ‫ ! او @<; أن‬escape sequence ‫)]ام‬9 5G4‫ ﻥ‬F‫ ا‬C8! ‫ @<; ان‬، ;"
-'C:\Documents' ‫]م‬G A ‫' و‬r'C:\Documents' ‫'أو‬C:\\Documents' ‫ ا)]م‬، ‫ل‬8!‫ ا‬CH)
! \D :  */ escape sequence ‫< هوب‬G<) ‫]م‬G c‫اﻥ‬

( ]G .‫ت‬4<!< F‫] ا‬G‫" ﻥ‬.‫ ! ﻥی ان ﻥ‬U ‫ ی! ا)]ا‬، (FA‫& ا‬G< c9) C!Q9 !
‫ن‬q‫ا‬
C> ‫ ی! ا‬N U
 nH) ‫ ا"ي‬M‫ ا‬N<@ ‫"ي‬4‫ی ا!< ا‬F ‫)]ام‬9 ‫ ن‬M`‫ ی‬p‫ ی ﻥ‬/ p
. ‫ت‬3 H‫ أو < ﻥ ا‬C>‫ < ا‬N!G‫ وه"ا  ی‬.‫( اي ن‬6‫ و‬c
‫( اي و‬6 (FA‫& ا‬G‫ ا‬R ‫ﻥ‬9
، ‫ل‬8!‫ ا‬CH) N<@ .
 c‫ !  آ! آ‬C!Q A N‫و‬A‫ ا‬R ‫ا‬H‫ و )@دة( ا‬، YM C9 Td@‫ ا‬R ‫ﻥ‬H‫ ا‬C!Q‫ی‬
&‫ ا‬، ‫ ا د‬U9‫( آ‬6 ‫ت‬g.‫
 ا‬c‫ او اذا آ‬، YM C9 R ‫ﻥ‬H‫  `! ا‬c‫واذا آ‬، C‫
  ن هك آ‬
. ;3 ‫ﻥ‬9 YM` gb) ‫ < ا`! او‬N‫ دة ا‬Q‫ @<; ا‬Q) ، 2) C9‫و‬
: $‫ﻥ‬6 ‫ﺹ ارة ا‬r‫*ا‬
N<@ C!Q‫ ان ی‬U!‫ ی‬N U<@ ‫ت‬GM‫ ا‬kQ9 ‫ل‬.‫ ی! ان إد‬، ;‫ و ذ‬.‫ ی‬C!Q‫ ی‬cH‫ی‬G‫ ا‬N‫و‬A‫ ا‬U]G‫ا‬
. ‫ت‬3 H‫ < ﻥ ا‬N!G‫ وه"ا  ی‬.N ‫ ا)س ی‬N<@ Cb6‫ أ‬M‫ﻥ‬

C.‫ا‬9 <!< )‫ آ‬time ‫)]ام‬9 -- <!‫! ا‬G Cb6‫ ه( ﻥ أ‬5L49 ‫ت‬Q‫ت ا( ﺵ‬GM‫ ا‬T"‫أ ه‬
FA‫]; ا‬G‫أ ا!^ات ه( ان ﻥ‬. UFA‫] ا‬G‫ ا‬C‫! د‬Z C‫ آ‬current date ‫ ری& ا م‬C‫ا‬
89 `
‫ت أ‬4<!‫ ل أ)!ء ا‬F ‫ى وه( ان‬.‫ وهك ^ة ا‬.5‫ إدار‬C5)P‫! ا‬6 ‫ و"ا‬،  ‫ی ه‬g9 5‫ی ]^ی‬
C!Q9 c!

c‫  اذا  آ‬5G9 ‫ف‬Q ‫@ك ان‬G) <`4!‫ ا‬U‫د‬A‫ى ه( ان ا‬.‫ و هك ^ة ا‬.‫ی‬g‫ ا‬T"59
.‫ "; ا م‬F‫] ا‬G‫
 ا]"ت ﻥ‬c‫ اذا آ‬e6 C‫ ی م " إﻥء ا‬C‫ @ آ‬F‫] ا‬G‫ﻥ‬
Example 10.2. Backup Script - The Second Version

#!/usr/bin/python
# Filename: backup_ver2.py

import os
import time

# 1. The files and directories to be backed up are specified in a list.


source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or
something like that

# 2. The backup must be stored in a main backup directory


target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.


# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Create the subdirectory if it isn't already there


if not os.path.exists(today):
os.mkdir(today) # make directory
print 'Successfully created directory', today

# The name of the zip file


target = today + os.sep + now + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup


if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

Output

$ python backup_ver2.py
Successfully created directory /mnt/e/backup/20041208
Successful backup to /mnt/e/backup/20041208/080020.zip

$ python backup_ver2.py
Successful backup to /mnt/e/backup/20041208/080428.zip

b‫!ﻥ‬7 ‫ ا‬0 A `$‫* آ‬

C‫ ا‬C.‫( دا‬M‫) ا م ا‬9 C‫ اذا آن هك د‬nM‫و ا>ات ه( ان ﻥ‬.UG4‫  زال ه ﻥ‬R ‫ﻥ‬H‫ ه"ا ا‬8‫ا‬
‫]  اا‬G UQ`‫ ﻥ‬M6 ، ‫ ﺝ د‬W ‫ذا آن‬h6 .os.exists ‫)]ام اا‬9 UFA‫] ا‬G< pL‫ا‬
.os.mkdir

(6 '/' ‫ ) ن‬U‫; اي اﻥ‬9 ‫ ا]ص‬C>‫م ا‬m 6‫ و‬C`4!‫ ا‬C‫( ا‬gQ‫ ی‬56 - os.sep >!‫_ ا)]ام ا‬A
‫وف‬M‫ ا‬T"‫ ه‬A9 Os.sep ‫ ا)]ام‬.‫ آ ش‬C> ‫م‬m‫( ﻥ‬6 ':'‫( ویوز و‬6 '\ \' ‫ و) ن‬، p‫ ی ﻥ‬، p
.m‫ ا‬T"‫ ه‬H@ C!Q‫ وی‬A !M 3 ‫ﻥ‬9 CQ3) ‫ﺵ‬H C9

: 6 6 ‫ﺹ ارة ا‬r‫*ا‬

8‫ و @   ن هك ا‬، FA‫& ا‬G‫ ا‬8‫ ا‬C!Q9 c!


c‫ ﺝا @  آ‬C!Q ‫ﻥ‬8‫] ا‬G‫ا‬
CH) N<@ ! ‫ ذا‬C‫ أﺝ‬c‫وآﻥ‬، F‫] ا‬G‫ ﻥ‬C‫ض آ‬W 9 n‫ی‬4‫( ا‬6 9 Q ‫ وﺝت‬، UFA‫& ا‬G‫ا‬
‫ ا>ات‬T"‫ ه‬Z‫ ﺙ أردت ان ا@ب @ أ‬، ‫ل‬8!‫ أو ا‬R ‫ﻥ‬H< GL‫ ا>ات ا‬kQ9 c<Q‫ﻥ(
 ﺝ‬6 ، ‫ل‬8!‫ا‬
‫رﺵ‬P‫ ا) ا‬N<@ ‫]م‬G!‫ ا‬n<Q‫ق ا‬6‫ ار‬n‫ی‬F @  5G9 UM !‫ وه"ا ی‬. ‫> ط‬b!‫رﺵ ا‬P‫ا) ا‬
.‫> ط‬b!‫ا‬
Example 10.3. Backup Script - The Third Version (does not work!)

#!/usr/bin/python
# Filename: backup_ver2.py

import os
import time

# 1. The files and directories to be backed up are specified in a list.


source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or
something like that

# 2. The backup must be stored in a main backup directory


target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.


# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' +
comment.replace(' ', '_') + '.zip'

# Create the subdirectory if it isn't already there


if not os.path.exists(today):
os.mkdir(today) # make directory
print 'Successfully created directory', today

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup


if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

Output

$ python backup_ver3.py
File "backup_ver3.py", line 25
target = today + os.sep + now + '_' +
^
SyntaxError: invalid syntax
‫ ؟‬b‫!ﻥ‬7 ‫ ا‬0 A @ `$‫آ‬
‫ ن ان‬8‫ی‬9
‫ ا"ي ی‬m‫( ا‬Z‫ ی‬A c9G‫( ان ا‬Q‫ > ي ! ی‬g. !‫ ن ی ل ان ﺙ‬8‫ی‬9 .!C!Q‫ ی‬A R ‫ﻥ‬H‫ه"ا ا‬
eHb‫أ ی‬H‫ ﻥ‬N .T@ g]‫ﻥ @ ا!ن ا"ي اآ ا‬H]‫ آ"; ی‬، ‫ ن‬8‫ی‬9 U 
(‫ ا‬g]‫_ ا‬d‫ @  ﻥ‬.T‫یا‬
. gG‫ ذ; ا‬3 ‫ﻥ‬9

‫ن‬9 ‫د‬M ‫ ی و‬p ‫ و‬، ‫ی دی‬g) N‫ ا‬G‫( ا 
 اﻥ‬g!‫ ا‬gG‫ ﻥى ان ا‬،
‫ ا‬Umd!9‫و‬
، gG‫( ه"ا ا‬6 operand 9G ‫ون ود‬9 (+) C>!‫ ن أن ا‬8‫ی‬9 ‫ وﺝ‬6 ، ))‫ ا‬.Q ‫ ا!دی‬gM‫هذی ا‬
d‫ ^ال  ا‬A (g!‫ ا‬gG‫د أن ا‬M‫ "آ اﻥ ی! أن ﻥ‬. C!Q‫ ا‬C‫ف آ ی ا‬Q‫ ی‬A U‫( أﻥ‬g!‫( ا‬9‫و‬
C!Q9 ‫ ﻥ م‬، ;" .‫ ا!دي‬gG‫ی ا‬5‫( ﻥ‬6 \ backslash 4<]‫< ا‬L!‫ ا‬F‫)]ام ا‬9 ‫ ا!دي ادم‬gG‫( ا‬6
bug fixing g]‫ح ا‬d‫ إ‬N!G‫ وه"ا  ی‬.3 ‫ﻥ‬H YM`‫ه"ا ا‬

A‫ﺹ ارة ا !اﺏ‬r‫*ا‬


Example 10.4. Backup Script - The Fourth Version

#!/usr/bin/python
# Filename: backup_ver2.py

import os, time

# 1. The files and directories to be backed up are specified in a list.


source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or
something like that

# 2. The backup must be stored in a main backup directory


target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.


# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# Notice the backslash!

# Create the subdirectory if it isn't already there


if not os.path.exists(today):
os.mkdir(today) # make directory
print 'Successfully created directory', today

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup


if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

Output

$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip

$ python backup_ver4.py
Enter a comment -->
Successful backup to /mnt/e/backup/20041208/082316.zip

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫<ت‬Q 2@ G‫ ﻥ‬.3 U]G‫( ا‬6 5<!Q9 !
(‫< ا‬Q4‫ت ا‬GM‫ل ا‬d. (b!‫ن! د@ ﻥ ﻥ‬q‫ ا‬C!Q‫ ی‬R ‫ﻥ‬H‫ه"ا ا‬
‫ت‬d.!‫ ل ا‬F YZ G‫ ﺵ| ی‬U9 C.‫
 أد‬CQ49 ‫]م‬G!‫  اذا آن ا‬6Q ‫ ﺙ‬raw_input ‫)]ام دا‬9 ‫]م‬G!‫ا‬
 ‫أو‬، (F‫د رو ا‬3 c‫ آﻥ‬5<Q)  2HG enter ‫ح‬4 N<@ e>b9 ‫]م‬G!‫ اذا
م ا‬.len ‫)]ام اا‬9
. CH
U9 !
 C!@ (6 ‫ه ﻥع‬Q9 ، (‫ أی >ات‬

.zip ‫ اد‬A‫ ا‬CH


e6 ‫> ط‬b!‫رﺵ ا‬P‫) ا‬9 nM<‫ ی‬n<Q‫"ا ا‬56 ، n<Q ‫ أی ل ؛ اذا آن هك‬N<@‫و‬
T"‫ت ه‬4<!‫ ا)!ء ا‬C8 ‫ن ادارة‬A ;"6 underscores __ <4) ‫ ﺵط‬n<Q‫( ا‬6 ‫ت‬G!‫ال ا‬H)‫_ ان ا‬d‫وﻥ‬
. 89 C5)‫أ‬

‫ت‬D$1 ‫*=  ا‬
N<@ . GM< ‫ل‬3 !L‫ و هك دا‬،  ]G!‫ ا‬mQ! Z ‫` رة‬9 C!Q‫ ی‬c9) (‫ ه‬Q9‫اارة اا‬
YH` ;3 ‫ﻥ‬9 CQ3 –v ‫ی ا]ر‬M ;!‫ ی‬i ، R ‫ﻥ‬H< ‫ب‬5)‫ ى ا‬G 6Z‫ ی!; إ‬، ‫ل‬8!‫ ا‬CH)
. ‫  ارا‬8‫اآ‬
N‫ ا‬C )‫ و‬. ‫وا‬A‫ ا‬g) N<@ c9G‫ ا‬N‫ <!ور إ‬6ZA‫د ا‬P‫ت وا‬4<!< Y!G ‫ى أن‬.P‫ت ا‬GM‫ ا‬
extend ) ‫ی ا‬F ‫)]ام‬9 ‫ ا( ی‬source !L
N‫ إ‬54b‫ أن ﻥ‬gG‫ وﻥ‬sys.argvoghg !L
;‫ذ‬
. list Hg‫ه ا‬6  (‫ ا‬method
@ U‫  أﻥ‬P‫ وأ ^ای ذ; ا‬. zip  P‫ ا‬A9 tar  P‫)]ام ا‬9 cM!) (‫ أﻥ‬R ‫ﻥ‬H‫( ا‬6 9"5!‫ ر ا‬P‫ ا‬
C
‫  ن أ‬FA‫] ا‬G‫ )@ وا‬8‫ أآ‬YH`‫( ی‬FA‫& ا‬G‫ ا‬YH` gzip 2‫ ﺝ‬N‫ إ‬H‫ ﺝ‬tar ; ‫ا)]ا‬
 P‫ ا‬. 5G9 ;"‫ آ‬.tar.gz ‫ت‬4< C Q‫ ی‬WinZip ‫ن‬h6 ‫( ویوز‬6 ‫رﺵ‬P‫ وإذا أردت ا)]ام ه"ا ا‬. !3
UH8 ‫ ﺙ‬c‫ اﻥﻥ‬U<!M ‫] ( ویوز‬G! !‫ وی‬. p‫ی ﻥ‬/p !m‫ أﻥ‬mQ (6 (Z‫ا‬6‫ ا‬C9 ‫ ح‬tar
.;"‫آ‬

:(‫ ا‬C9  P‫ن ) ن ا‬q‫ا‬

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))

.T‫ أدﻥ‬MZ ‫وا]رات‬


.‫ إﻥء أرﺵ‬creation N‫ ی ا‬-c

.talkative ‫ وﺙﺙة‬b‫ إی‬8‫ ان  ن اآ‬23‫  ی‬P‫ أي أن ا‬verbose N‫ ی إ‬-v

.U ‫>( ا)]ا‬H‫ ا"ي ی‬gzip <6 ‫ أن‬N‫ ی إ‬-z

)A‫ ا‬p4‫ ﻥ‬C!M‫ إذا آن ی‬.o < CM CM‫>( أن ی‬H‫ ی‬U‫رﺵ اي اﻥ‬P‫( اﻥء ا‬6 ‫ أي ا ة‬force N‫ ی ا‬-f
.CQ49
N<@ .UFA‫] ا‬G‫ ا‬excluded ‫ده‬QH)‫ ا‬23‫ت ا( ی‬4<!‫! ا)!ء ا‬L
!b‫ ا!< ا"ي ی‬N‫ ی ا‬-x
FA‫] ا‬G‫( ا‬6 ~ ‫ ب‬U5!‫ت ا‬4<!‫م ادراج اي ا)!ء ا‬Q <!‫( ه"ا ا‬6 ~* ‫ی‬M ;!‫ ی‬، ‫ل‬8!‫ ا‬CH)
* @**
N<@ tarfile ‫ او‬zipfile ^<‫)]ام ا! دی‬9 ‫رﺵ ) ن‬P‫ ه"ا ا ع ا‬C8 ‫< ﻥء‬b4!‫ق ا‬g‫ ا‬8‫أآ‬
b‫ت ای‬H!‫ ا‬T"‫)]ام ه‬9 .CQ49 5 ‫ری وا!ح ; ا)]ا‬Q!‫ ن ا‬8‫ی‬9 H ‫ ﺝ^ءا‬C 5‫ اﻥ‬.(‫ا ا‬
m‫ه‬9 ‫ء‬g.‫; أ‬4< ‫ أن‬C5G‫ ا‬5‫ﻥ‬A ،‫! م‬Q‫ ا‬U‫ وﺝ‬N<@5 ‫)]ا‬9 Y`‫ ی‬A (‫ وا‬os.system ‫ ا)]ام‬23
.5 ‫)]ا‬9
‫ل‬8 ;‫ ذ‬HQ‫ "ا ی‬، M9 !<Q ‫اض‬WA F‫& ا‬G‫ ﻥ‬C!Q os.system ‫ی‬F ‫ أ)]م‬c‫ آ‬6 ، ;‫و ذ‬
.(4‫! ی‬9 b‫ أی‬4 M‫( ا‬6 5‫ و‬، !3‫ ا‬CH
 54 ‫ آف  ن‬C9 eG9
:‫ت‬$/!7 ‫ ﺕ?!ا‬$- G* .١
M‫ ا‬N<@ 5`]< !‫ ی‬C‫ ا!ا‬T"‫ن ه‬h6 .‫ت‬3 H‫ ا‬9‫( @!< آ‬6 4<]!‫ ا‬C‫ﺝز ا!ا‬9 !

‫ن و‬q‫ا‬
: (‫ا‬

What (Analysis ) ....... ( C<M‫ ذا )ا‬.١


How (Design) ........(!`‫ آ )ا‬.٢
Do It (Implementation) ......("4‫ ذ; )ا‬CQ6 .٣
Test (Testing and Debugging) (‫ء‬g.A‫ ا‬YM`‫ر و‬H.‫ر )ا‬H.A‫ ا‬.٤
Use (Operation or (‫ ا)]ام )او @!< ا‬.٥
Maintain (Refinement) ( GM‫ ا`ﻥ )ا‬.٦Deployment)
*@**
‫ ﺙ‬C<M9 
– FA‫& ا‬G‫ ا‬C!@ c9) (6 TQH‫ﺝاء ا"ي ا‬A‫ ه( ا‬R ‫ا‬H‫ ا‬9 59 N !‫ی ا‬g‫ا‬
‫ آ! ه‬C!Q‫ ی‬U‫ <آ أﻥ‬R ‫ﻥ‬H‫ ا)]ام ا‬. YM`‫ر وا‬H.A‫ ا‬. R ‫ﻥ‬H< gG9 > "49 ‫أ‬9‫ ا‬.!`‫ا‬
Do It-Test-" "‫ا)]م‬-‫ﺝب‬-CQ6‫ "ا‬: ‫ اورة‬T"‫( ار ه‬6 !)‫ أی ^ات یه وا‬Z‫ أ‬، ‫ن‬q‫ وا‬.

! NH A‫ و‬، ‫ت ! آ^رع‬3 H‫ و"آ ؛ ا‬.‫< ب‬g!‫ ا‬M‫ ا‬N<@ ‫ي @د ا!ات‬P "Use
" Software is grown, not built "

‫ﺹ‬:I ‫ا‬
T"‫ ه‬C8 9‫( آ‬6 ‫ ا( رك‬4<]!‫ ا‬C‫ ن وا!ا‬8‫ی‬9 (6 ]‫ت ا‬9G‫ا‬/R ‫ا‬H‫ ا‬C!@ 4‫و رأی آ‬
 YH` ‫ ; ان‬NG‫ ی‬N C`4‫( ه"ا ا‬6 <Q6 !<8 ;G49 ;3 ‫ا‬9 ‫ اﻥء‬4!‫ ا‬U‫ اﻥ‬3 !9‫ ور‬.R ‫ا‬H‫ا‬
" object-oriented" L‫ ا‬3 H‫! ی<(؛) ف ﻥ
} ا‬6‫و‬. C‫ ا!آ‬C N<@ ‫ @ ارة‬db6 ‫ ن‬8‫ی‬9

!NG ‫ ا دي‬02, ‫ا‬


*‫ ا ﺝ‬$D ‫ ا‬/!7 ‫ا‬
Object-Oriented Programming
Introduction ............................................................................................................  
The self ....................................................................................................................‫ا"ات‬
Classes .......................................................................................................{‫ت‬Hg‫|ت}ا‬4‫ا‬
Creating a Class............................................................................................... Hg‫إﻥء ا‬
Object Methods ............................................................................................ ‫ت‬L‫ق ا‬F
Using Object Methods.........................................................................‫ت‬L‫ق ا‬F ‫ا)]ام‬
The __init__ method............................................................................... __init__ ‫ی‬F
Using the __init__ method ........................................................... __init__ ‫ی‬F ‫ا)]ام‬
Class and Object Variables .............................................................. L‫ وا‬Hg‫ >ات ا‬
Class and Object Variables..................................................... L‫ وا‬Hg‫ا)]ام >ات ا‬
Inheritance ...........................................................................................................‫ا ارث‬
Using Inheritance .....................................................................................‫ا)]ام ا ارث‬
Summary ............................................................................................................d.
:
*
.‫ﻥت‬H9 2@d (‫ﻥت ا‬H‫ ا‬C‫  ل دوال أو آ‬3 ‫ﻥ‬9 !`9 !
 ، ‫ن‬q‫ ا‬N‫ و‬3 ‫ا‬9 !‫( ﺝ‬6
;3 ‫ﻥ‬9 m ‫ى‬.‫ی أ‬F ‫ وهك‬.procedure-oriented 5‫ ا! ﺝ‬L‫ اﺝا‬3 H‫ی ا‬F ‫ ه"ا‬N!G‫وی‬
L‫ ا‬3 H‫ ﻥ! ذج ا‬N!G‫ وه"ا  ی‬.object L9 N!G‫! ی‬6 Q 54<>‫ﻥت و‬H‫ وا‬4† ‫ ا‬9 !3‫ا"ي ه ا‬
‫ة او‬H‫ آ‬R ‫ا‬9 9‫ن @  ی آ‬A‫ ا‬kQ9 (6 ‫ و‬U‫ﺝاءی‬A‫ ا‬U3 H‫ ی!; ا)]ام ا‬c
‫ ا‬mQ (6 .U‫ا ﺝ‬
. U‫ ا ﺝ‬L‫ آ‬3 H‫ ی!; ا)]ام ت ا‬، 5 2G‫ﻥ‬P‫ ا‬CM‫ان ی ن ه"ا ه ا‬

@ ‫ ﻥ‬n<] Hg6 .5‫ ا! ﺝ‬L‫ ا‬U3 H< GL‫ﺵل ا‬P‫ان ه! ا‬HQ‫ ی‬objects ‫ت‬L‫ وا‬Classes ‫ت‬Hg‫ا‬
@ ‫ ی! ان ی ن ی; >ات ﻥ‬U‫ ذ; ه اﻥ‬N<@ G
P‫ أ ا‬.Hg‫< ا‬8 ‫ت أ‬L‫ ا‬HQ i ‫ﺝیا‬
HQ (‫ ه( ا!>ات ا‬UMM`‫@اد ا‬A‫
 ان ا!>ات ا( ]^ن ا‬N‫ وا( ﺝ ا‬int YM`‫د ا‬Q‫ا‬
. int Hg‫( ا‬objects ‫ت‬L‫ت )أوآ‬A
:*C/C++/Java/C# /!7 9‫ﺡ‬:*
‫ار‬A‫ ا‬CH
) 6‫ وﺝ‬C++ ‫ف‬d]9 ‫ وه"ا‬.(int Hg‫ت ) ا‬L‫ آ‬5‫ اﻥ‬N<@ C Q UMM`‫@اد ا‬A‫ ا‬N U‫_ اﻥ‬d‫ﻥ‬
. class Hg‫  ل ا‬C4‫ !^ی ا‬help(int) m‫ اﻥ‬.CP‫ ا‬L‫ا‬9 ‫ ه( اﻥ اع‬UMM`‫@اد ا‬A‫ ا‬i (1.5
/!7C# ‫ و‬Java 1.5 ‫ م‬54 UH‫ ی‬U‫ أﻥ‬i 5‫ إ‬6   P‫ون ذ; ا‬3) boxing ‫ و‬unboxing

N‫ وا!>ات ا( !( ا‬.‫ت‬L‫ ا‬T"‫ ه‬N‫ ا( !( ا‬U‫دی‬Q‫)]ام ا!>ات ا‬9 ‫ﻥت‬H‫ت ]^ی ا‬L< !‫ی‬
N‫)]ام اوال ا( !( ا‬9 U4†‫م و‬5 b‫ ای‬5 ‫ت أن ی ن‬L< !‫ ی‬. fields ‫  ل‬N!G L‫ او ا‬Hg‫ا‬
n‫ی‬4‫( ا‬6 ‫@ﻥ‬G 5‫ﻥ‬A !5 ‫ت‬M<g`!‫ ا‬T"‫ ه‬.Hg‫" ا‬5 methods 2)‫ق أو ا‬F N!G ‫ اوال‬T"‫ ه‬.Hg‫ا‬
، Q!‫ ﺝ‬5<‫ وآ‬. L‫ او آ‬Q HF N‫ و<; ا( !( ا‬، 5‫(  ذا‬6 <G (‫ اوال وا!>ات ا( ه‬9
.Hg‫ت <; ا‬4 5‫ اﻥ‬N<@ 5‫ق ی! ان یر ا‬F ‫ ل وال‬M‫ا‬

5!‫ أو ی‬، Hg‫ ا‬instance/object L‫ آ‬/  N‫! ان !( ا‬5 C !‫ ل  ن ﻥ @ – ی‬M‫ا‬
.(‫ ا ا‬N<@ Hg‫ و >ات ا‬M‫!( >ات ا‬G (56 .5G4‫ ﻥ‬Hg‫ ا‬N‫ان !( ا‬

(6 ‫ رﺝ‬Hg‫ق ا‬F‫ ل و‬M‫ ا‬.class keyword Hg< (‫ زة‬3M!‫)ا‬4!‫)]ام آ<!ت ا‬9 ‫ ی إﻥؤه‬Hg‫ا‬
.<‫  ا‬m

The self ‫*ا ات‬


(6Z‫ ا) أول إ‬5 ‫ أن  ن‬23‫ ی‬5‫ وذ; اﻥ‬-- ‫دی‬Q‫د ی] اوال ا‬M ‫رق وا‬6 Hg‫ ا‬2)P ‫ی ﺝ‬
5 ‫ و) ف ی‬،method ‫@( ال‬G  @  ‫را‬H‫"ا ا‬5 !
(gQ A ;‫ و‬، !L‫را ا‬9 ‫ای‬9 N‫ف ا‬b‫ی‬
. self )9 NmM 5‫ﻥ‬h6 ، ‫ق‬4A‫ ا‬2G‫ و‬، U‫ ذا‬L‫ ا‬N‫د ی ا‬M!‫ ه"ا ا!> ا‬.‫ ن‬8‫ی‬9 
‫آ یدي‬9 ‫ ه‬.o )‫ اي ا‬-- self )‫]م ا‬G ‫ة ان‬9 N ‫ ی‬،  ‫را‬H‫ ا‬T"5 )‫ء أي ا‬g@‫ ی!; إ‬U‫ اﻥ‬W‫ور‬
N‫ را و‬6 U9 ‫ف‬Q‫; ) ف ی‬3 ‫ﻥ‬H ‫ وأي
رئ‬--‫ري‬Q )‫)]ام ا‬A ‫ی ا!^ای‬Q‫ وهك ا‬.‫ س‬HQ‫ ا‬N‫إ‬
. self ‫]م‬G c‫@ك اذا آ‬G ‫( ی! ان‬U< !‫ ی ا‬g‫| ا‬9) IDE ‫ ا!]`` ل‬R ‫ا‬9

*C++/Java/C# /!7 9‫ﺡ‬:*

C# ‫ و‬+‫ ﺝ‬+ this ‫(رة‬r‫ و ا‬C++ < + self !(] ‫دل ا‬A‫ن ﺕ‬6‫ ﺏ‬+ self ‫رة‬7G

<8 P‫أ ا‬.‫ ؟‬5 !


‫ء‬g@‫ ا‬N‫ﺝ ا‬M9 cG c‫ ؟ و!ذا أﻥ‬self ‫(
! ل‬gQ‫ ن ی‬8‫ی‬9 ‫@<; ان ه ؛ آ أن‬
 @ myobject. N!G‫ ی‬Hg‫ ا‬T"‫ل ه‬8 ‫ و‬myclass N@ class ;‫ أن ی‬d8 C .MZ‫ وا‬P‫ ه"ا ا‬CQ3)
‫ ن‬8‫ی‬9 n‫ی‬F @ L< 5<‫ ی‬M ‫ ی‬U‫ﻥ‬h6 ، MyObject.method(arg1, arg2) : (<‫ آـ! ی‬L‫"ا ا‬5 method ‫@( ال‬G
self -]‫  ی‬C‫ وه"ا آ‬- MyClass.method(MyObject, arg1, arg2 N‫إ‬

‫` ل‬M< ‫ی‬F ‫ی‬M N‫ﺝ ا‬M9 ‫ ^ال‬A ;‫ﻥ‬h6 ، argument ‫" أي‬. A ‫ی‬F ;‫ اذا آن ی‬U‫ اﻥ‬b‫( ای‬Q‫وه"ا ی‬
. self argument N<@
:Object Methods*
‫ أن ال‬CQ49 
‫ ﻥ‬classes/objects > ‫ إذا آن ی‬A‫ اوال إ‬C8 
F ‫ ي‬M ‫ أن‬5!‫ ی‬self . (6Z‫إ‬
. ;‫ ذ‬N<@ A8 ‫ن ) ف ﻥى‬q‫وا‬
‫ ام‬I ‫*ا‬:Object Methods
Example 11.2. Using Object Methods

#!/usr/bin/python
# Filename: method.py

class Person:
def sayHi(self):
print 'Hello, how are you?'

p = Person()
p.sayHi()

# This short example can also be written as Person().sayHi()

Output

$ python method.py
Hello, how are you?

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ه ؛ ﻥى‬self ‫_ أن ال‬A . C!Q method ‫!ة‬G!‫ ا‬sayHi ‫ت‬d Q ‫" أي‬. A parameters ‫و  ^ال‬
N<@ ‫ ي‬Mself . ‫ اا‬C.‫ا‬9
:The __init__ method*
U‫ی‬F ‫ و)ى  ا!>^ى‬.‫ ن‬8‫ی‬9 ‫ت‬HF (6 . !‫ اه‬5 (‫ق ا‬g‫ی أ)!ء ا‬Q‫__ ی ﺝ ا‬init__
‫ن‬q‫ا‬.

‫ی‬F__init__Hg< (!!‫ ا‬L‫ ا‬C!@ ‫د‬3!9 C!Q . 59 ‫| ی ام‬5 ‫ اي‬CQ4 ‫ة‬4 ‫ی‬g‫ ا‬T"‫ه‬
U‫< ا!^دوﺝ‬4G‫ ا‬F‫_ ا‬A .;9 ‫ ا]ص‬L‫ ) ا‬underscoreU‫ی‬5‫( ﻥ‬6‫) و‬A‫ای ا‬9 (6 !‫ه‬d‫__ ( آ‬.
:__init__ method ‫ ام‬I ‫*ا‬
Example 11.3. Using the __init__ method

#!/usr/bin/python
# Filename: class_init.py

class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print 'Hello, my name is', self.name

p = Person('Swaroop')
p.sayHi()

# This short example can also be written as Person('Swaroop').sayHi()

Output

$ python class_init.py
Hello, my name is Swaroop

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


‫ی‬F ‫ی‬M9 !
، ‫__ه‬init__ 2‫ ﺝ‬N‫ إ‬H‫را  )ﺝ‬H‫" ا) ا‬. ‫ی‬Fself ‫ ﻥ م‬، ‫ وه‬.(T‫د‬Q!‫ا‬
b‫ ای‬N!G‫ ﺝی ی‬C ‫د إﻥء‬3!9 name .)A‫ ا‬p4‫ ﻥ‬C!M 5‫ اﻥ‬W‫ ر‬4<] ‫_ أن هك >ی‬A .dotted
notation !59 ‫ق‬4‫  ان ﻥ‬Y!G‫ ی‬.

‫ی‬F ‫@( ا‬G‫ ﻥ‬A ‫_ اﻥ‬A ، ;‫ه ذ‬P‫__ وا‬init__ ‫!ی‬9 ‫ و ﻥ م‬arguments ) ‫ ا‬C.‫ا‬9
‫ ا) ال‬Q9 class  n<. —‫ @  ﻥ‬instance ‫ ال‬T"‫ ﺝیة ه‬class T"‫ وه"ا ه ا!>^ى ا]ص ه‬.
‫ی‬g‫ا‬.
C ‫ ا)]ام‬N<@ ‫
درون‬M‫ ﻥ‬، ‫ن‬q‫ ا‬self.name ‫ی‬F (6 N<3 (‫
 ا‬F (6 sayhi .
C++/Java/C# /!7 9‫ﺡ‬:*

C++/Java/C# (6 constructor ‫ !ﺙ< ـ‬ __init__ ‫ی‬F

: Class and Object Variables*


(6 .59 ‫ﻥت ا]ص‬H‫ن )ى ﺝ^ء ا‬A‫ وا‬، ‫ت‬L‫ت وا‬Hg‫ ا‬4† 9 n<Q‫^ء ا‬3‫ ا‬CQ49 
‫ أن ﻥ‬nH) 
!Z M ‫)!ء‬P‫ ا‬T"‫ ه‬. ‫ت‬L‫ت وا‬Hg‫ت أ)!ء ا‬W‫ا‬49 gH ‫ ) ى >ات @دی‬cG 5‫ اﻥ‬،
‫ا ا‬
. e6 ‫ت‬L‫ت وا‬Hg‫ ا‬T"‫)ق ه‬

(‫ وا‬object variables L‫ و >ات ا‬class variables Hg‫ >ات ا‬-- ‫ ل‬M‫وهك ﻥ @ن ا‬
. ‫ !<; <!>ات‬- (‫ ا ا‬N<@ - L‫ أو ا‬Hg‫ ا‬c‫ ! اذا آﻥ‬QH `

]G‫ ی ﺝ ) ى ﻥ‬A .Hg‫ ا‬T"5 (‫ت‬AM‫ت )ا‬L‫ل ﺝ! ا‬d.  C!Q 5‫ اﻥ‬NQ (6 ‫ ك‬Hg‫ >ات ا‬
. b‫ى ای‬.A‫ت ا‬AM‫( ﺝ! ا‬6 >‫ ه"ا ا‬pQ‫ ی‬، Hg‫ > ا‬N<@ C!Q9 L‫ و@  ی م ا‬Hg‫ > ا‬
U L‫ آ‬C‫ آ‬، UM‫ ا‬T"‫( ه‬6‫ و‬.Hg‫( ا‬6 "object/instance" ‫ل‬8!‫ ا‬/ L‫د ا‬6 C‫ آ‬5<!‫ ی‬L‫ >ات ا‬
(6 )A‫ ا‬p49 ‫ ا"ي‬CM‫ﺵل ا‬A‫ ا‬C‫ي ﺵ‬9 eH A‫ آ و‬cG 5‫ أي أﻥ‬CM‫ ا‬U9 . ]G‫ﻥ‬
. 5!56 C5G‫ ا‬CQ3) ‫ل‬8!‫ وه"ا ا‬.Hg‫ ا‬p4‫ ﻥ‬4<] instance

Using Class and Object Variables 


7? ‫!ات ا  وا‬$< ‫ ام‬I ‫*ا‬
Example 11.4. Using Class and Object Variables
#!/usr/bin/python
# Filename: objvar.py

class Person:
'''Represents a person.'''
population = 0

def __init__(self, name):


'''Initializes the person's data.'''
self.name = name
print '(Initializing %s)' % self.name

# When this person is created, he/she


# adds to the population
Person.population += 1

def __del__(self):
'''I am dying.'''
print '%s says bye.' % self.name

Person.population -= 1
if Person.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population

def sayHi(self):
'''Greeting by the person.

Really, that's all it does.'''


print 'Hi, my name is %s.' % self.name

def howMany(self):
'''Prints the current population.'''
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %d persons here.' % Person.population

swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

kalam = Person('Abdul Kalam')


kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()

Output

$ python objvar.py
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


Hg‫ ا‬N‫ !( إ‬population ‫ت ؛ وه‬L‫ت وا‬Hg‫ >ات ا‬QHF H (6 @G‫ ی‬U‫ و‬C‫ ی‬F ‫ل‬8 ‫ه"ا‬
‫( ه‬9‫( و‬self ‫)]ام‬9 G ‫ ) وه‬L‫ ا‬N‫ ی!( إ‬name >!‫ وا‬. Hg< ‫ >ا‬HQ ‫ و"ا‬، Person
. L< >
‫_ أن‬A . self.population ‫ آـ‬p‫ و‬Person.population ‫ " آـ‬population" Hg‫ > ا‬N‫وه"ا ﻥ إ‬
‫)]ام‬9 L‫ ا) > ا‬N‫ ﻥ إ‬M‫ ! وﻥ‬Hg‫( > ا‬4]‫ ) ف ی‬HF >!‫) آ‬A‫ ا‬p4‫ ﻥ‬C!M‫ ی‬L‫ > آ‬
. L‫ و >ات ا‬Hg‫ >ات ا‬9 eG9 ‫ف‬d.‫"آ أن هك ا‬. L9 ]‫ق ا‬g‫( ا‬6 self.name
‫ ﻥ^ی @د‬،ِ‫ی‬g‫ ا‬T"‫( ه‬6‫ و‬. name َ Person ِM< initialize C!Q C ُ !QَGُ ‫ی‬F __init__ ‫ن‬ ّ 9 ْ_A
L‫ آ‬C ‫د‬M self.name 
‫_ أن‬d‫ آ"; ﻥ‬. 6b YH`‫ ی‬Person ‫ أن ی وا‬i 1 ‫!ار‬9 population
. L‫ >ات ا‬QHF N‫ی إ‬
N@‫ وذ; ی‬. e6 self >!‫)]ام ا‬9 L‫ ا‬p49 ]‫ق ا‬g‫ ا!>ات وا‬N<W  ‫ أن‬23‫ ی‬U‫"آ أﻥ‬
N‫ ی! ا  ل إ‬. ‫ق‬g‫ت وآ"; ا‬Hg< docstrings ‫ ا)]ام‬b‫ ﻥى أی‬R ‫ﻥ‬H‫( ه"ا ا‬6 . . ‫إﺵرة‬
‫ آـ‬docstring ‫ی‬g‫ وا‬Person.__doc __ ‫)]ام‬9 runtime C>‫ ا‬c
‫( و‬6 class docstring
L‫ @  ی ﺵ; آ‬N@G (‫__ ا‬del__ ‫ى‬.‫ أ‬. ‫ی‬F ‫وی ﺝ‬، __init__ method C8 . Person.sayHi.__doc
‫ب ال‬G ‫ﻥص‬h9 FGH9 ‫ی ﻥ م‬g‫ ا‬T"‫( ه‬6‫ و‬. ‫^ء ا"اآة‬3‫!ل ه"ا ا‬Q)‫م @دة ا‬m‫ ا‬N‫ إ‬U‫ و) إ@د‬، ;‫ ذ‬Q9 U ‫ ی! ا)]ا‬A‫و‬. ‫ ا! ت‬N<@ 
. 1 ‫!ار‬9 Person.population
‫]م‬G ‫ أن‬e6 ;<@ ‫ ح‬Z 9 ;‫ ذ‬C!@ ‫وإذا أردت‬. C!Q) ‫ی‬g‫ ا‬T"‫!ن أن ه‬Z ‫ هك‬p‫ و‬، ‫]م‬G W L‫ @  ی ن ا‬C!Q __del__ ‫ی‬F
. 9G‫< ا‬8 P‫( ا‬6 T<!Q)‫ ا"ي ا‬del ‫ن‬H‫ا‬

: C++/Java/C /!7 9‫ﺡ‬:


‫ @ ال‬C‫آ‬class  !@ HQ (‫ﻥت‬H‫ @ ا‬N‫ إ‬6Z‫ )إ‬public ‫ ال‬C‫ وآ‬methods (6 <] HQ
__ ‫< ^دوﺝ‬4) F‫)]ام ﺵ‬9 ‫)!ء‬P‫ﻥت ا‬H‫]م @ ا‬G cF ‫"ا‬W ‫ء وا ؛‬8)‫وهك ا‬. ‫ ن‬8‫ی‬9
C8 .__privatevar . !
5 CQ3 <@49 )A‫ ا‬C ‫ ن‬8‫ی‬9 ‫]م‬G‫ ی‬. private variable.
_ <4) F9 YH`‫ أن ی‬23‫ ی‬L‫ أو ا‬Hg‫ ا‬C.‫ دا‬e6 C!QG‫ > ی‬C‫ ه( أن آ‬QH!‫
 ا‬4A‫ن ا‬h6 ‫ه"ا‬
underscore  @ ‫ى‬.P‫)!ء ا‬P‫ وﺝ! ا‬public ‫ أي‬CH
‫]م‬G ‫ وی! أن‬classes/objects.
__ ‫< ا!^دوﺝ‬4G‫ ا‬F‫ ا‬L‫د‬9 ‫ ن )  @ا‬8‫ی‬9 ‫ری‬H‫ إﺝ‬cG‫ و‬2GM6 
4‫ ا‬T"‫"آ أن ه‬double
underscore prefix (

Inheritance ‫ا ارث‬
o ‫ل‬d. U<!@ ‫ ذ; ی‬CL)‫ و‬.‫ وأ‬، ‫!ل ا د‬Q)‫ ه إ@دة ا‬5‫ ا! ﺝ‬L‫ ا‬3 H< GL‫ ا‬6!‫أ ا‬
. Inheritance mechanism ‫ا ارث‬
;‫ض اﻥ‬4 . ‫ت‬Hg‫ ا‬9 (@6 ‫ وﻥ ع‬pL‫ ﻥ ع ر‬9 
d@ nHg U‫ أﻥ‬N<@ Cb6‫ ا‬C9 U<] !‫ا ارث ی‬
G‫) وا‬A‫ ا‬C8 ‫ ا!آ‬-L`]‫ ا‬kQ9 5‫ وی‬. <‫( آ‬6 ‫ب‬dg‫<! وا‬Q!‫ ا‬Q9!9 ‫ ی م‬R ‫ﻥ‬9 9‫ی آ‬
‫ ودرﺝت و `ری‬، !<Q!< ‫وإﺝزات‬، !<Q‫ واورات ا‬2‫ اا‬C8 Q -L`. ;"‫ آ‬5‫ وی‬. ‫ ان‬Q‫وا‬
. H<g<
‫ه‬Q ، ‫ آ ﺝیة‬. 6Zh9 ‫ و‬، !53Q‫ ﻥ ع و‬C ‫ت‬Hg‫< ا‬G @ ‫ی!; أن — ﻥ‬
HF ‫ﻥء‬h9 ‫ ی! ان  ن‬Cb6P‫ی ا‬g‫ وا‬. ‫ ﺝا‬d‫  ﺙ‬P‫ ه"ا ا‬YH`‫ ی‬Q‫ و)ی‬. <G HF C‫ آ‬N‫ إ‬56Z‫إ‬
N‫و‬P‫ ا‬Hg‫ ا‬T"‫ ث ه‬student HF‫ و‬teacher HF CQ3 ‫ه‬Q9‫ و‬، SchoolMember N!G ‫ آ‬
‫د‬M‫ ذ; ی! أن ﻥ‬Q9‫ و‬. Hg‫ ا‬T"5 sub-types @6 ‫ن أﻥ اع‬MH`) .o NQ!9‫ و‬. (SchoolMember)
. sub-types (@4‫ﻥ اع ا‬P‫ ا‬T"‫ ه‬-L`.
o ‫ ه"ا‬pQ‫ ) ف ی‬، SchoolMember (6 4†‫ت اي و‬W/c4Z‫ إذا أ‬.‫ی‬g‫ ا‬T"‫( ه‬6 ‫هك @ة !^ات‬
C ID card field ‫
 ه ی ﺝیا‬g9 C b ‫ل ؛ ی!; ان‬8!‫ ا‬CH) N<@ . ;"‫@ آ‬4‫ﻥ اع ا‬P‫ ا‬N<@
(6 ‫دﺙ‬M‫ أی ل ا>ات ا‬N<@‫ و‬. SchoolMember Hg‫ ا‬6Z‫ إ‬n‫ی‬F @ FGH9 ‫ب‬dg‫<! وا‬Q!‫ا‬
. ‫ى‬.P‫ ا‬subtypes (6 ‫ ﺙ‬A subtypes @4‫ﻥ اع ا‬P‫ا‬
‫ ا"ي ی! أن‬SchoolMember L‫ره آ‬H@9 H<g‫<! أو ا‬Q!‫ت ا‬L‫ آ‬N‫ ی!; أن  إ‬U‫ى اﻥ‬.P‫ا!^ة ا‬
‫ ان‬i ‫ ؛‬polymorphism U‫وﺝ‬P‫د ا‬Q N!G‫ وذ; ی‬. )‫ء ا!ر‬b@‫ب @د أ‬G C8 ‫ت‬AM‫ ا‬kQ9 (6 ‫ا‬4
!‫ ی‬L‫ ا‬d8!6 . Q
 CP‫( أي   @  ی ن ا ع ا‬6 ‫ل‬HG‫ ی! أن ی‬sub-type (@4‫ا ع ا‬
N‫ﺝ إ‬M9 G‫و‬، CP‫ ا‬Hg‫ ا)]ام آ د ا‬Q‫_ آ"; أﻥ ﻥ‬d‫ وﻥ‬. <P‫ ا‬Hg‫  ا‬U4`9 T‫ار‬
. <G ‫ت‬HF  ])‫(   ا‬6 H‫ آ! آن واﺝ‬، 4<] ‫ت‬HF (6 T‫ار‬
Teacher HF ، superclass ‫)) أو‬P‫ ا‬Hg‫ ا‬5‫ﻥ‬9 ‫ف‬Q M‫ ا‬T"‫( ه‬6 SchoolMember ‫!ة‬G!‫ ا‬Hg‫ا‬
‫ل‬8!‫ن ه"ا ا‬q‫ و)ى ا‬. subclasses @6 ‫ت‬HF ‫ أو‬derived classes  ‫ت‬HF N!G Student HF‫و‬
. R ‫ﻥ‬9 ‫(  رة‬6 (‫ا‬
Using Inheritance ‫ ام ا ارث‬I ‫ا‬
Example 11.5. Using Inheritance

#!/usr/bin/python
# Filename: inherit.py

class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print '(Initialized SchoolMember: %s)' %
self.name

def tell(self):
'''Tell my details.'''
print 'Name:"%s" Age:"%s"' % (self.name,
self.age),

class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher: %s)' % self.name

def tell(self):
SchoolMember.tell(self)
print 'Salary: "%d"' % self.salary

class Student(SchoolMember):
'''Represents a student.'''
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student: %s)' % self.name

def tell(self):
SchoolMember.tell(self)
print 'Marks: "%d"' % self.marks

t = Teacher('Mrs. Shrividya', 40, 30000)


s = Student('Swaroop', 22, 75)

print # prints a blank line

members = [t, s]
for member in members:
member.tell() # works for both Teachers and Students

Output

$ python inherit.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)

Name:"Mrs. Shrividya" Age:"40" Salary: "30000"


Name:"Swaroop" Age:"22" Marks: "75"

: b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


Hg‫ی ا‬Q (6 Hg‫) ا‬9 @ H tuple (6 base class ))P‫ ا‬Hg‫ی ا) ا‬M9 ‫ ﻥ م‬، ‫!ل ا ارث‬Q)A
، self >!‫)]ام ا‬9 YZ‫ وا‬C9 N@G ))P‫ ا‬Hg9 ]‫__ ا‬init__ <)‫_ أن و‬d‫ ذ; ﻥ‬Q9 .
A ‫ ن‬8‫ی‬9 ‫إن‬، ‫! ا( @<; أن "آه‬5!‫ ر ا‬P‫ ا‬. L‫( ا‬6 ))P‫ ا‬Hg‫ ذ; ی! إ@اد ﺝ^ء ا‬C‫أﺝ‬
YZ‫ وا‬C9 5L@)9 ‫ و@<; أن  م‬، o ‫ی‬g9 ))P‫ ا‬Hg< "constructor" ‫@( اا ا!˜ة‬G‫ی‬
.;G49
;‫ ذ‬Q9‫ و‬، method ‫ اء ال‬Hg‫ ی ا) ا‬n‫ی‬F @ ))P‫ ا‬Hg‫ ا‬CL)‫@( و‬G‫ ی! أن ﻥ‬U‫_ أﻥ‬d‫آ"; ﻥ‬
‫د‬3!‫ آ‬Student ‫ أو‬Teacher ‫ت‬A RQ‫ ی! أن ﻥ‬U‫_ أﻥ‬A . arguments ‫ أي‬self >!‫ ا‬N‫ﻥ! إ‬
. SchoolMember Hg‫ت ـ‬A
.SchoolMember Hg9 ]‫ ا‬cG‫@( ی ا)@ؤه و‬4‫ ع ا‬9 ]‫ ا‬tell <) ‫_ آ"; أن ا‬d‫وﻥ‬
CQ4‫ ی‬M‫ ا‬T"‫( ه‬6 ‫ وه‬، ‫( ا ع‬6 methods CL) ‫ @ ا‬iMH‫( ا‬6 ‫أ‬H‫! ی‬L‫ ن دا‬8‫ی‬9 ‫ ذ; ه أن‬54 ‫ق‬g‫أ ا‬
‫)) واة‬P‫ت ا‬Hg‫ ا‬N‫ ا!! إ‬methods ‫ @ ال‬iMH‫( ا‬6 ‫ا‬H‫ ی‬U‫ﻥ‬h6 method ‫د ال‬3‫ إی‬gG‫"ا  ی‬W‫ و‬.;‫ذ‬
. Hg‫ی ا‬Q (6 tuple (6 ‫دة‬M 5‫ أﻥ‬C‫< ا]ى أﺝ‬
i‫ ا ری‬N!G "L@، i‫ ا ری‬tuple !L
(6 ‫| رﺝ‬6 8‫ –إذا آن هك أآ‬Y<g`!9 . md
. ‫د‬Q!‫ا‬

‫ و‬.59 UgH!‫ت ا‬M<g`!‫ @ ]< ا‬db6 ‫ت‬L‫ت وا‬Hg< 4<]!‫ ا‬2‫ اﻥ‬3‫)ف ا‬9 ‫ن‬q‫
! ا‬
3 H‫ل ا‬3 (6 ‫ ى‬G!‫ @ ا‬3 9 > HQ ‫ ن‬8‫ی‬9 .5‫ ا! ﺝ‬L‫ ا‬3 H‫ت ا‬Hg ‫ و‬L‫ ا‬6 b‫ﻥ ای‬5‫ﺵ‬
QH‫( ا!ى ا‬6 ‫ا‬8‫@ك آ‬G) ‫ی‬Q9 ‫ه‬4!‫ ا‬T"‫ ه‬56‫ و‬5‫ ا! ﺝ‬L‫ا‬.

‫ ن‬8‫ی‬9 (6 ‫ت‬4<!‫ ا‬N‫ ا  ل ا‬4‫ت وا!]ﺝت وآ‬d.!‫ ا‬C Q‫ ا‬4‫< آ‬Q‫ ) ف ﻥ‬، (<‫! ی‬6‫و‬.

!NG ‫ﻥ‬6 ‫ ا‬02, ‫ا‬


Input/Output
Table of Contents ..........................................................................................‫ یت‬M!‫! ا‬L

Files .........................................................................................................................‫ت‬4<!‫ا‬
Using file ........................................................................................................<!‫ا)]ام ا‬
Pickle...............................................................................................................................
Pickling and Unpickling unpickling ...............................................................................
Summary ................................................................................................................d.

‫]م )وی! أن  ن‬G!‫ ا‬C@4‫ ا‬N<@ ;3 ‫ﻥ‬H ‫ء


رة‬g@‫( إ‬6 2W  @ ‫و
ت‬P‫ ا‬8‫) ن هك ا‬
‫ ی! أن‬A‫ و‬.‫ ا راء‬N‫ إ‬RL‫ ا‬kQ9 Hg ‫ ﺙ‬، ‫]م‬G!‫ت ا‬d. ". ‫ اﻥ; ی إن‬.(‫]م‬G!‫; ه"ا ا‬G4‫ ﻥ‬c‫أﻥ‬
<] ‫ ا)]ام‬b‫ی! أی‬، output C HG9 .(‫ ا ا‬N<@ print ‫ن‬H‫ وا‬raw_input ‫)]ام‬9 ;‫ ذ‬nM‫ﻥ‬
، string `‫< ﻥ‬G<) N<@ C`M rjust ‫ی‬F ‫ ی!; ا)]ام‬، ‫ل‬8!‫ ا‬CH) N<@ .str (string) class 2)‫أ‬
. C4‫ !^ی ا‬help(str)m‫اﻥ‬. ‫د‬M ‫ض‬Q right justified ‫ر‬H n ‫وا"ي ه‬

‫ و
اءة‬، ‫ إﻥء‬N<@ ‫ ارة‬.‫ت‬4<!‫ ا‬C Q‫ ه( ا‬input/output ‫ت وا!]ﺝت‬d.!‫ ا‬.o L‫ی ﺝ ﻥ ع ﺵ‬
. C`4‫( ه"ا ا‬6 2‫ﻥ‬3‫( ه"ا ا‬6 iMH)‫ و‬، R ‫ا‬H‫ ا‬8 ())‫ت أ  أ‬4<!‫ ا‬9‫وآ‬

: ‫ت‬,- ‫*ا‬
read, 2)‫ وا)]ام أ‬file Hg< L‫ إﻥء آ‬n‫ی‬F @ 9‫ت <اءة أو ا‬4<!‫ وا)]ام ا‬Y6 ;!‫ی‬
< N‫ إ‬9‫ ااءة أو ا‬N<@ ‫ ارة‬.<!‫ ا‬N‫ إ‬9‫ <اءة ا!< أو ا‬2) C9 write ‫ أو‬readline
‫ ی!; ا)@ء أ)< ب‬، <!‫( ا‬5  @‫ و‬، ‫ا‬.‫ ﺙ أ‬.<!‫ ا‬Y4 T‫ی‬M9 c!
‫)< ب ا"ي‬P‫ ا‬N<@ 
‫ی‬
. <!‫ ا)]ام ا‬5‫ﻥ اﻥ‬9 ‫ ن‬8‫ی‬9 ’<H close
Example 12.1. Using files

#!/usr/bin/python
# Filename: using_file.py

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''

f = file('poem.txt', 'w') # open for 'w'riting


f.write(poem) # write text to file
f.close() # close the file

f = file('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default


while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print line, # Notice comma to avoid automatic newline added by Python
f.close() # close the file

Output

$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


Y6 ‫ ا( ﻥی‬mode / e!‫ی ا) ا!< وا‬M n‫ی‬F @file Hg‫ ا‬instance/ ‫ﻥء‬h9 !
، A‫أو‬
(6 ‫ وهك‬، ('a') ‫ ك‬e!‫'( أو ﻥ‬w') 9< e!‫ أو ﻥ‬، ('r') g)‫ ی! أن ی ن <اءة وا‬e!‫ ا‬.59 <!‫ا‬
. 5@ C4‫; ا!^ی ا‬gQ ‫ ) ف‬help(file) ، !‫ﻥ!ط ا‬P‫ی ا‬Q‫ا ا
ا‬

. -<!‫ ه"ا ا‬close ‫ا‬.‫ ا!< ﺙ أ‬N‫ إ‬9< file Hg< write ‫ وا)]ام أ)< ب‬9‫ ا‬e!‫( ﻥ‬6 < Y4‫ ﻥ‬A‫ أو‬-
C‫( آ‬6 ‫ ﻥأ‬.(Z‫ا‬6A‫ ااءة ه ا‬e!‫ ی ن ﻥ‬، g!‫د ﻥ‬M‫ وإذا  ﻥ‬.‫ى <اءة‬.‫ ا!< ة أ‬p4‫ ﻥ‬Y4‫ ﻥ‬، ;‫ ذ‬Q9
g) N‫ إ‬6Z9 d ‫ا آ‬g) ‫ی إ‬g‫ ا‬T"‫ ه‬. loop/< (6 ،readline ‫)]ام أ)< ب‬9 <!‫ ا‬g)

‫ و‬5‫ی ا!<
  ا  ل إ‬5‫ أن ﻥ‬N‫ ی إ‬56 ، ‫رغ‬6 g) ‫ و"ا @  یﺝ إ‬، .e]‫ی ا‬5‫ﺝی ﻥ‬
. loop/<M‫ا‬
gG‫ن ا‬P print ‫ن‬H‫ ا‬U4b‫وا"ي ی‬، L< ‫ ﺝی‬g) ‫ ! وث‬print ‫ن‬9 <6 ‫]م‬G‫_ أﻥ ﻥ‬A‫ و‬-
. <!‫ ه"ا ا‬close ‫ا‬.‫ أ‬، ‫ﺙ‬. ‫ ﺝی‬g) ‫( إﺵرة‬5‫ ی‬CQ49 <!‫ا"ي یأ ا‬
.YM C9 C!Q‫ ی‬R ‫ﻥ‬H‫ <آ أن ا‬poem.txt <!‫ یت ا‬M N<@ <F‫ ا‬، ‫ن‬q‫ ا‬-
Pickle

(6 Object/ L‫( إ ن ]^ی أي آ‬6 ‫]م‬G ، pickle N@ standard module/‫ری‬Q ‫  وة‬6  ‫ ن‬8‫ی‬9
.‫ اوام‬N<@ L‫ ]^ی ا‬N!G‫ وه"ا  ی‬.‫س‬G ‫ دون‬A 5<@ C`M ‫ ﺙ‬، ‫( < وا‬6 ‫ ن‬8‫ی‬9

‫  ب‬U‫ اﻥ‬A‫ ؛ إ‬pickle C‫ ا! دی‬U9 ‫  ی م‬p4‫ ﻥ‬eHb9 C!Q (‫ وا‬cpickle N!G ‫ى‬.‫ أ‬module/‫وهك وة‬
W‫ ا‬N<@ ، modules/‫ ا ات‬T"‫ ی!; ا)]ام أي ه‬.(8‫ ة أو أآ‬1000)‫!ار‬9 ‫ وه أ)ع‬C ><9
C‫د ا! دی‬3 FGH9 ‫ ا ات‬T"‫ ه‬C‫ أن آ‬N‫ ﻥ إ‬M‫ ﻥ‬، ‫ "آ‬.‫ ه‬cpickle ‫]م ا ة‬G ‫أﻥ ) ف‬
pickle
Pickling and Unpickling
Example 12.2. Pickling and Unpickling

#!/usr/bin/python
# Filename: pickling.py

import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data' # the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file


f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # remove the shoplist

# Read back from the storage


f = file(shoplistfile)
storedlist = p.load(f)
print storedlist

Output

$ python pickling.py
['apple', 'mango', 'carrot']

b‫!ﻥ‬7 ‫ ا‬0 A `$‫*آ‬


C‫ ی! ا)]ام ا) ا
` اﺝ‬i ‫ ا!ل‬C5) ‫ و ه‬.import..as ‫ ا<> ي‬2‫]م اآ‬G‫_ أن ﻥ‬d‫ ﻥ‬، A‫أو‬
> ‫ل‬d. (cPickle or pickle) <] C‫ دی‬N‫ﻥل إ‬A‫  ا‬Y‫ ی‬U‫ﻥ‬h6 ، M‫ ا‬T"‫( ه‬6 N‫ و‬.C‫ا! دی‬
p ‫ آـ‬، C‫ ا! دی‬T"‫ ه‬N‫ ﻥ إ‬FGH9 M‫ وﻥ‬، R ‫ﻥ‬H‫ ا‬9 (6! ‫ وا‬gG eG9
n‫ی‬F @ ‫ ح‬4!‫( ا!< ا‬6 L‫ و]^ی ا‬9‫ ا‬e!‫( ﻥ‬6 file L‫ ا‬Y49 ‫ ﻥ م‬A‫ أو‬، <!‫( ا‬6 L‫]^ی آ‬
. pickling N!G <!Q‫ ا‬T"‫ه‬. pickle C‫ ا! دی‬dump ‫ا)@ء اا‬

unpicklingN!G <!Q‫ ا‬T"‫ ه‬. L‫ ا‬Q‫ ا"ي ی‬pickle C‫ <! دی‬load ‫)]ام اا‬9 L‫ ا‬2MG‫ وﻥ‬، ;‫ ذ‬Q9
‫ﺹ‬:I ‫*ا‬
.pickle C‫ت وا)]ام ا! دی‬4<!‫ ا‬3Q b‫ت وا!]ﺝت وأی‬d.!‫ ﻥ
 ]< أﻥ اع ا‬
. exceptions ‫ءات‬8)A‫ م ا‬54 (6 iMH) (<‫! ی‬6‫و‬
!NG \ 6 ‫ ا‬02, ‫ا‬
Exceptions ‫ءات‬D6 k‫ا‬
‫  ا ت‬
Errors .............................................................................................................. ‫ء‬g.P‫ا‬
Try..Except ...............................................................................................Try ‫ء‬8)A‫ا‬
Handling Exceptions........................................................................... ‫ءات‬8)A‫ ا‬3Q
Raise Exception.......................................................................................‫ءات‬8)A‫ ا‬6‫ر‬
How to raise exception ................................................................... ‫ءات‬8)A‫ ا‬6‫ ر‬4‫آ‬
Try ..Finally..................................................................................................................
Using Finally..........................................................................................Finally ‫ا)]ام‬
Summary............................................................................................................-]<

‫ اءة‬H‫ ذاه‬c‫ث  آ‬M‫ ذا ی‬، ‫ل‬8!‫ ا‬CH) N<@ .;3 ‫ﻥ‬9 (6 Q L8)‫ت ا‬A ‫ث‬M  @ ‫ءات‬8)A‫ ا‬
‫)]ام‬9 RQ ‫ت‬AM‫ ا‬T"‫ ه‬C8 ‫؟‬C!Q‫ آن ی‬3 ‫ﻥ‬9 6‫!`د‬9 c6" c‫ ﺝ د؟ أو  إذا آ‬W <!‫ <  وا‬
.‫ءات‬8)A‫ا‬

{; 5H } U‫ یی‬6‫ ن وا"ي ی‬8‫ی‬9 ‫ه‬A ‫ ر ی‬P‫ ا‬T"‫ ؟ ه‬M`‫ ا‬W ‫ت‬M‫ ا`ی‬kQ9 ;3 ‫ﻥ‬H ‫ ذا  آن‬
. g. ‫ك أن هك‬H]‫وی‬

Errors ‫?ء‬%4‫*ا‬
‫ف‬M‫_ ا‬A ‫؟‬Print ‫ه آـ‬H‫ وآ‬print 9‫( آ‬6 Ld ‫ﻥ إ‬g.‫ ذا  أ‬.print statement N‫ إ‬gG9 ‫ة‬m‫ﻥ‬
.syntax error ‫ > ي‬g. ّ!‫ إ أن ﺙ‬6‫ ن ی‬8‫ی‬9 ، M‫ ا‬T"‫( ه‬6‫ و‬.‫! ل‬G‫ف ا‬M‫ل وا‬9‫ا‬

>>> Print 'Hello World'


File "<stdin>", line 1
Print 'Hello World'
^
SyntaxError: invalid syntax

>>> print 'Hello World'


Hello World

‫_ أن‬d‫ﻥ‬syntaxerror ‫ء‬g.P‫ ا‬RQ U<Q4‫ وه"ا ه  ی‬.T@ 9‫ ا‬g. ‫ ا!ن ا"ي  اآف‬b‫ وأی‬، 6‫ی‬
error handler.g]‫"ا ا‬5

‫ء‬D6 k‫ا‬Try
e>Z‫ ا‬.  ]G!‫ت ا‬d. ‫ول
اءة‬M)Ctrl-d.‫ث‬M‫ ذا ی‬m‫واﻥ‬

>>> s = raw_input('Enter something --> ')


Enter something --> Traceback (most recent call last):
File "<stdin>", line 1, in ?
EOFError

UQ
‫ی ا!< @   ﻥ ﻥ‬5‫ ﻥ‬N<@ ‫ ر‬8Q‫  ا‬U‫( أ)) أﻥ‬Q‫ وا"ي ی‬EOFError N@‫ ی‬g. ‫ إ‬6‫ ن ی‬8‫ی‬9
(Ctrl-d e>b‫ل ا‬d. C8!‫)ا"ي ی‬

.‫ء‬g.P‫ ا‬T"‫ ه‬C8 C Q‫ ا‬4‫ )ى آ‬، (<‫! ی‬6‫و‬

Handling Exceptions.. ‫ءات‬D6 k‫ ا‬/ A

try-block !Z ‫دة‬Q!‫ﻥت ا‬H‫)س ا‬P9 QZ‫و


 و‬. try..except ‫رة‬H@ ‫)]ام‬9 ‫ءات‬8)A‫ ا‬3Q !‫ی‬
. except-block (6 ‫ء ا( ی‬g.P‫ت ا‬3Q C‫ آ‬QZ‫وآ"; و‬،
Example 13.1. Handling Exceptions

#!/usr/bin/python
# Filename: try_except.py

import sys

try:
s = raw_input('Enter something --> ')
except EOFError:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program

print 'Done'

Output

$ python try_except.py
Enter something -->
Why did you do an EOF on me?

$ python try_except.py
Enter something --> Python is exceptional!
Done

b‫!ﻥ‬7 ‫ ا‬0 A `$‫آ‬


‫ء‬g.P‫ ﺝ! ا‬3Q ‫و‬M ‫ و ﺙ‬try block <‫( آ‬6 g. 8 
(‫ ا‬statements ‫ اـ‬C‫ آ‬b‫ﻥ‬
!L
‫ أو‬، ‫د‬M ‫ء وا‬8)‫ أو ا‬g. 3Q U!‫ ی‬except H‫ ا‬. except <‫ ا‬/ H‫(  @ا ا‬6 ‫ءات‬8)A‫وا‬
، ‫ة‬gQ!‫ءات ا‬8)A‫ء أو ا‬g.P‫ إذا  ی هك أ)!ء ا‬.‫ءات‬8)A‫ ا‬/ ‫ء‬g.² ( )
9) ZQ!‫ ا‬C!3‫ا‬
. try 9 C‫ آ‬eH C
P‫ ا‬N<@ ‫ وا‬except 9 ‫ أن ی ن هك‬23‫ وی‬.‫ءات‬8)A‫ء وا‬g.P‫ ﺝ! ا‬RQ)


‫ و‬.)‫ ر‬Hg‫ وی‬R ‫ﻥ‬H‫" ا‬4 
‫@( و ی‬G‫ ن ی‬8‫ی‬H (Z‫ا‬6A‫ ا‬RQ!‫ن ا‬h6 RQ‫ء  ی‬8)‫ أو ا‬g. ‫إذا آن أي‬
.C!Q‫( ه"ا ا‬6 CQ49 ‫رأی‬
‫ @ @م وﺝ د أي‬T"4 ‫ ی‬else H‫ ا‬. try..catch <9 eH else H‫ أن ی ن ی; ا‬b‫وی! أی‬
.‫ءات‬8)‫ا‬
.‫ء ا"ي ث‬8)A‫  ل ا‬6Z‫< ت إ‬Q ‫ "ا ی! ا)ﺝع‬exception object N<@ ‫` ل‬M‫ی! آ"; ا‬
.(‫ل ا‬8!‫( ا‬6 ‫ ه"ا‬N<3‫وی‬

Raising Exceptions ‫ءات‬D6 k‫" ا‬+‫ر‬


‫ واـ‬، ‫ء‬8)A‫ا‬/ g]‫د ا) ا‬M ‫ أن‬b‫ @<; أی‬23‫ ی‬.raise statement ‫)]ام‬9 ‫ءات‬8)A‫ ا‬6‫ی!; ر‬
UQ6‫ء ا"ي ی!; ر‬8)A‫ أو ا‬g]‫ ا‬. exception/‫ء‬8)A‫ ا‬2‫ ﺝ‬N‫ إ‬H‫ @ ﺝ‬Z ‫ ی ن‬exception object
Hg‫ أو ا‬Error Hg‫  @ ا‬HF ‫ﺵ‬H W ‫ﺵ أو‬H C9 HQ (‫ وا‬class ‫>( أن ی ن‬H‫ی‬
.(‫ ا ا‬N<@ Exception

How To Raise Exceptions


Example 13.2. How to Raise Exceptions

#!/usr/bin/python
# Filename: raising.py

class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast

try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print 'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
else:

print 'No exception was raised.'


Output

$ python raising.py
Enter something -->
Why did you do an EOF on me?

$ python raising.py
Enter something --> ab
ShortInputException: The input was of length 2, was expecting at least 3

$ python raising.py
Enter something --> abc
No exception was raised.

0 A `$‫آ‬
4<) ‫د‬M g. /‫ء‬8)‫]م أي ا‬G ‫ ی! أن‬A 
‫ أﻥ‬W‫ ا‬N<@ 9 ‫ص‬. ‫ء‬8)A‫ﻥء ﻥ ع ا‬h9 !
، ‫ه‬
< N<@ ‫ ي‬M (‫ وه‬.ShortInputException ‫س‬d‫ی ه ا‬3‫ء ا‬8)A‫ وه"ا ا ع ا‬.b‫اض إی‬WP
.R ‫ﻥ‬H‫ ا‬UQ
‫ ل آن ی‬F `
‫ وا"ي ه أ‬atleast ‫ و‬، ‫ت‬d.!‫ ل ا‬F ‫ وه‬length --

/ g]‫ ا‬L‫ﺝاء ا!رﻥ ا‬h9 ‫ @ ا!> ا"ي ی م‬db6 ، error ‫س‬d9 ‫ ﻥ"آك‬، except H‫( ا‬6
‫س ا]ص‬d‫ ا‬C.‫( دا‬6‫ و‬.‫( ا)@ء اا‬6 arguments ‫ وال‬parameters C <‫ !ﺙ‬HQ (‫ وا‬.‫ء‬8)A‫ا‬
.‫]م‬G!< H) )‫@ ر‬Hg atleast‫ و‬length: <M‫ ا‬object ‫]م‬G‫ ﻥ‬except

Try..Finally

finally ‫)]ام‬9 ;‫ ؟ وی! أن ی ذ‬A ‫ء أو‬8)‫ ا‬6‫ق ه"ا ا!< ) اء  ر‬dW‫ أ ا!< أردت إ‬c‫ ذا  آ‬
.5 <9!‫ ا‬try <‫ ا‬p4 finally <‫ آ‬2‫ ﺝ‬N‫ إ‬H‫ ﺝ‬except ‫ د‬9 ‫ ی!; ا)]ام أ‬U‫ @<! اﻥ‬.block
. !5<‫( ا)]ام آ‬6 2W c‫ إذا آ‬.q‫ ا‬C.‫ا‬9 ‫! وا‬b gb)

Using Finally
Example 13.3. Using Finally

#!/usr/bin/python
# Filename: finally.py

import time

try:
f = file('poem.txt')
while True: # our usual file-reading idiom
line = f.readline()
if len(line) == 0:
break
time.sleep(2)
print line,
finally:
f.close()
print 'Cleaning up...closed the file'

Output

$ python finally.py
Programming is fun
When the work is done
Cleaning up...closed the file
Traceback (most recent call last):
File "finally.py", line 12, in ?
time.sleep(2)
KeyboardInterrupt

: 0 A `$‫آ‬
CH
‫ ﺙﻥ‬٢ ‫ !ة‬sleeping FH@‫ی ا‬F c<.‫ أد‬c‫ و( آ‬، ‫د‬Q!‫ آ‬file-reading C!Q‫
 ا‬g9 ‫ﻥ م‬
‫ ن )ی ﺝا‬8‫ی‬9) ‫ء‬eH9 C!Q‫ ی‬R ‫ﻥ‬H‫ ا  "; ه أن ا‬2HG‫ وا‬.time.sleep ‫ی‬F ‫)]ام‬9 g) C‫@ آ‬HF
.R ‫ﻥ‬H‫إ>ء ا‬/QF! Ctrl-c e>Z‫ ا‬، C!Q‫ ی‬R ‫ﻥ‬H‫ ا‬Cm‫ و@  ی‬. (UQHg9

‫ی‬. R ‫ﻥ‬H‫ء ا‬5‫ اﻥ‬CH


U‫ و‬، ‫ <]وج‬U‫ی‬F (6 R ‫ﻥ‬H‫ و ا‬N<ُ‫ ی‬KeyboardInterrupt ‫ء‬8)A‫_ أن ا‬d‫ﻥ‬
.<!‫ق ا‬dW‫ وی إ‬finally H‫" ا‬4

:#I-

9 . ‫ء‬8)‫ أﻥ اع ا‬g —‫ و رأی آ ﻥ‬. try..finally ‫ و‬try..except ‫ﻥت‬9 ‫ ﻥ
 ا)]ام‬
.;"‫ءات آ‬8)A‫ ا‬6‫ ر‬4‫وآ‬

.)‫ ن ا‬8‫ی‬9 H iMH) CH!‫ ا‬C`4‫( ا‬6‫و‬


!NG "‫ ا !اﺏ‬02, ‫ا‬
$ $
‫ن ا‬6‫ ﺏ‬7
The Python Standard Library
Table of Contents......................................................................................‫  ا ت‬
Introduction..............................................................................................................  
The sys module................................................................................................ sys C‫ا! دی‬
Command Line Arguments .............................................................................................
More sys...................................................................................................... sys @ ‫ا!^ی‬
The os module....................................................................................................os C‫ا! دی‬
Summary.................................................................................................................-]<

modules/‫ ا ات‬CL‫ @د ه‬N<@ ‫ ي‬M (‫ وه‬.‫ ن‬8‫ی‬H 2‫ آ‬C‫ ن ا)  آ‬8‫ی‬9 UH :  
 5G9 5< !‫ ی‬C‫ ا!آ‬mQ ‫ إن‬i ‫ ن ا) ؛‬8‫ی‬9 H N<@ ‫دوا‬Q ‫!ن أن‬9 !‫ه‬P‫ و ا‬.‫ة ﺝا‬4!‫ا‬
.modules/3 H‫ ا ات ا‬H!‫ ا‬T"‫ف ه‬Q c‫@ إذا آ‬G9‫و‬

!3 < ‫ ا‬C4‫ ا‬N<@ ‫ ر‬8Q‫ ی!; ا‬.H!‫ ا‬T"‫( ه‬6  ]G!‫ ا‬modules/ ‫ ا ات‬kQ9 iMH)
(‫ ا‬nL‫( ا ﺙ‬6 " Library Reference/H!‫ " ﺝ ا‬G
(6 )‫ ن ا‬8‫ی‬9 H (6 modules ‫ا ات‬
.;9 ‫ ن ا]ص‬8‫ی‬9 2‫( آ‬

: The sys module*


CQ49 ‫ و
 رأی‬. system-specific functionality ‫م‬m‫دة ا‬M 4†‫ و‬N<@ " sys" C‫ ي ه"ا ا! دی‬M‫ی‬
. command-line arguments N<@ ‫ ي‬M‫ ا"ي ی‬sys.argv !L

Command Line Arguments


Example 14.1. Using sys.argv

#!/usr/bin/python
# Filename: cat.py

import sys

def readfile(filename):
'''Print a file to the standard output.'''
f = file(filename)
while True:
line = f.readline()
if len(line) == 0:
break
print line, # notice comma
f.close()

# Script starts from here


if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()

if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters
if option == 'version':
print 'Version 1.2'
elif option == 'help':
print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]:
readfile(filename)

Output

$ python cat.py
No action specified.

$ python cat.py --help


This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help

$ python cat.py --version


Version 1.2

$ python cat.py --nonsense


Unknown option.

$ python cat.py poem.txt


Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
:0 A `$‫آ‬
‫ت‬4<!‫ ا‬kQ9 ‫ی أ)!ء‬M e6 ;<@ .p‫ ی ﻥ‬/p ( ]G @ ‫ ا! ف‬cat  P‫ول < ا‬M‫ ی‬R ‫ﻥ‬H‫ه"ا ا‬
. output/‫ ]ج‬N‫ إ‬5@Hg9  P‫ا` و) ف ی م ا‬

‫ ا"ي ه‬sys.argv !L


(6 C
A‫ ا‬N<@ ‫! @` وا‬L‫ هك دا‬، <@4 ‫ی‬g9 p ‫ ن‬8‫ی‬H R ‫ﻥ‬9 C!Q‫@  ی‬
. 4`‫ ا‬Q‫أ ا‬H‫ ن ی‬8‫ی‬9 ‫ أن‬i sys.argv[0] ‫ وی ن  آـ‬d @ YH`‫( ی‬M‫ ا‬R ‫ﻥ‬H‫ا) ا‬
.`Q‫ى <( ذ; ا‬.‫ أ‬command line arguments

@ ‫ ا!^ی‬6Q ‫]م‬G!< ‫د‬M 5‫ ا]رات ا( ا!آ أﻥ‬kQH9 T!‫!ل @< أن ﻥ‬Q)A‫ ا‬C5) R ‫ﻥ‬H‫ ا‬CQ3
-- ‫ إذا آن ا]ر‬.3 ‫ﻥ‬H ‫دة‬M ‫  اذا آن أي ا]رات‬6Q! argument ‫]م أول‬G‫ ﻥ‬M‫ ﻥ‬.R ‫ﻥ‬H‫ا‬
d<
(gQ‫ ﻥ‬، -- help ‫د ا]ر‬M‫ @  ﻥ‬، C8!9‫ و‬.R ‫ﻥ‬H‫@ ر
 إارة ا‬HF ‫ ی‬،  ]G version
m‫ اﻥ‬، !L‫ل دا‬M‫ وآ! ه ا‬.R ‫ﻥ‬H‫ <]وج ا‬sys.exit ‫!ل دا‬Q)‫ ا‬4G‫ ﻥ‬M‫ ﻥ‬.R ‫ﻥ‬H‫اح  ل ا‬
.C4‫ !^ی ا‬help(sys.exit)

C‫ آ‬g) C‫@ آ‬HF FGH9  ، R ‫ﻥ‬H‫ ا‬N‫ت ی !یه إ‬4<!‫دة وأ)!ء ا‬M ‫رات‬. ‫ ی ن هك‬A  @
. ‫وا‬P‫ ا‬g) N<@ ‫د‬M 2 (6 .q‫ واا < ا‬، <

U!‫ ی‬i – R ‫ﻥ‬H‫ ه"ا ا‬U9 ‫)س  ی م‬P‫( ا‬6 (‫ وه‬concatenate !< ‫`ر‬.‫ ا‬cat  P‫ ا‬، H)!9‫و‬
. output/ ‫ اﺵ أو ا]ج‬N<@ Q ‫ت‬4<!‫ ا‬8‫اﺙن أو أآ‬، M< ‫ أو‬gH ‫ت‬4< <G<) ‫@ < أو‬HF

: sys G = ‫ا‬

‫!ة‬G!‫ ا‬tuple/ C9 ‫ و ا‬. 5H89 c!


(‫ ن ا‬8‫ی‬9 ‫< ت @ إارة‬Q ;gQ sys.version `‫< ا‬G<G‫ا‬
. ;3 ‫ﻥ‬H ‫ ن‬8‫ی‬9 ‫دة إارة‬M ‫  أﺝ^اء‬C5)‫ی أ‬F ;gQ sys.version_info

[swaroop@localhost code]$ python


>>> import sys
>>> sys.version
'2.3.4 (#1, Oct 26 2004, 16:42:40) \n[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)]'
>>> sys.version_info
(2, 3, 4, 'final', 0)

،sys.stdin !b sys (C‫( ا ة )ا! دی‬6 !‫ه‬P‫ى ذات ا‬.P‫ ا‬Q‫ ا‬: * M!‫ ا‬3 H!<*
‫یت‬3 (6 standard error ‫ و‬standard output ‫ و‬،standard input n9g sys.stderr‫ و‬sys.stdout
. (‫ ا ا‬N<@ ;3 ‫ﻥ‬9

The os module "os" 0‫ا د‬


c‫ إذا آ‬. !‫ أه‬5 ‫ ا ة‬T"‫ ه‬.operating system C>‫م ا‬m  @ 4†‫ و‬C8! 3 H‫ ا ة ا‬T"‫ه‬
‫ ویوز‬N<@ ‫ أو‬p‫  آ‬N<@ C!Q‫ ( ی‬9  ‫  ن‬R ‫ﻥ‬H< Y!G‫ ی‬U‫ اي أﻥ‬- ;3 ‫ا‬H <G ‫ `ت‬C!@ ‫ی‬
A9 os.sep >!‫ ذ; ا)]ام ا‬N<@ <8 P‫ و ا‬.‫ ذ; أي >ات‬2<g‫ ودون أن ی‬C‫آ"; دون أي آ‬
.‫د‬M ‫م‬m <G |9 ‫ر أو‬G ‫ی‬M <!@

.U‫"ا‬9 YZ‫ وا‬5!mQ ‫ و‬T‫ رﺝ أدﻥ‬os C‫ة ا! دی‬L6 ‫ﺝ^اء‬P‫ ا‬8‫أآ‬
( ]G! 'posix' ‫"  یوز و‬nt" d8!6 ، 5 ]G (‫د ا!` ا‬M os.name `‫< ا‬G<G‫ا‬ •
. p‫ی ﻥ‬/p
‫ ن‬8‫ی‬H cH‫ )ی‬U<@ C!Q‫( ا"ي ی‬M‫ ا‬C‫ ا‬C8 ، (M‫ ا‬C!Q‫ ا‬C‫ د‬N<@ ‫` ل‬M< os.getcwd() ‫اا‬ •
.
. (‫ ا ا‬N<@ |H‫` ل أو إ@اد >ات ا‬M< ‫]م‬G os.putenv() ‫ و‬os.getenv() ‫اوال‬ •
. (M‫ ا‬C‫( ا‬6 ‫<ات‬3!‫ت وا‬4<!‫ ا‬C‫ أ)!ء آ‬Q os.listdir() ‫اا‬ •
. ‫ت‬4<!‫"ف أ ا‬M ‫]م‬G os.listdir() ‫اا‬ •
. C<  ‫ أ‬C> ‫]م‬G os.system() ‫اا‬ •
. ‫ر‬G!‫( ا‬6 <!‫ وا) ا‬C‫ ا) ا‬Q os.path.split() ‫اا‬ •

>>> os.path.split('/home/swaroop/byte/code/poem.txt')
('/home/swaroop/byte/code', 'poem.txt')

‫ < أو‬N‫ ی إ‬NgQ!‫ر ا‬G!‫  إذا آن ا‬-M4 ‫]م‬G os.path.isdir() ‫ و‬os.path.isfile() ‫• اوال‬
‫ ﺝ د‬NgQ!‫ر ا‬G!‫  إذا آن ا‬6Q! ‫]م‬G os.path.exists() ‫ اا‬، C8!9‫ و‬. (‫ ا ا‬N<@ <3
. CQ49
. ;"‫ آ‬help(sys) ‫]م‬G ‫ وی! أن‬. C4‫ ن ا) !^ی ا‬8‫ی‬9 nL‫( وﺙ‬6 iMH‫ی!; ا‬

*#I-*
‫ ن‬8‫ی‬9 nL)‫( و‬6 iMH ‫>( @<; أن‬H‫ ی‬. )‫ ن ا‬8‫ی‬9 H (6 sys ^<‫ ا! دی‬L†‫ و‬bQ9 ‫
 رأی‬
. ;"‫ وا!^ی ا! دی<^ آ‬5  ‫ ا!^ی‬N<@ C`M )‫ا‬
.Aً !‫ اآ‬8‫ ن أآ‬8‫ی‬9 (6  ‫ ﺝ‬CQ3) (‫وا‬، ‫ ن‬8‫ی‬9 @  2‫( ﺝ اﻥ‬g>‫ ا( ) ف ﻥ‬C`4‫( ا‬6‫و‬

!NG QI ‫ ا‬02, ‫ا‬


‫ن‬6‫ا =  ﺏ‬
More Python

Table of Contents.......................................................................................... ‫ﺝ ول ا ت‬


Special Methods............................................................................................ ‫ﺹ‬I ‫ ا‬.$  4‫ا‬
Single Statement Blocks..........................................................................‫!دة‬, ‫ ا‬g‫ر‬2 ‫ ا‬0‫آ‬
List Comprehension........................................................................................  
‫ ا‬$ M‫ﺕ‬
Using List Comprehensions ..............................................................D M ‫ ام ا
ا@ ا‬I ‫ا‬
Receiving Tuples and Lists in Function................................  ‫ ا ا‬+ @‫ وا
ا‬0‫ﺏ‬$ ‫ل ا‬7
 ‫ا‬
Lambda Forms............................................................................................. lambda ‫ﻥ ذج‬
Using Lambda Forms.......................................................................lambda ‫ ام ﻥ ذج‬I ‫ا‬
exec and eval statements……………………………………………. eval ‫ و‬exec g‫ر‬2‫ﺕ‬
The assert statement .................................................................................... assert g!2‫ﺕ‬
The repr function................................................................................................. repr  ‫دا‬
Summary..................................................................................................................#I-

(g>) C`4‫( ه"ا ا‬6 ، 5 ]G ‫ ن وا( ) ف‬8‫ی‬9 @  ‫ و‬GL‫ ر‬2‫ ﺝ اﻥ‬g>9 ‫ح‬39 !

‫ن و‬q‫ا‬
. A!‫ اآ‬8‫ ن أآ‬8‫ی‬9 ><9 6Q CQ3 (‫ ا‬2‫ اﻥ‬3‫ا!^ی ا‬
: Special Methods

__del__‫__ و‬init__ 2)‫ أ‬C8 classes/‫ت‬Hg‫( ا‬6 . !‫ أه‬5 (‫ ا] ا‬2)P‫ ا‬kQ9 ‫هك‬
.CQ49 ‫ﻥه‬5‫ أه!
 ﺵ‬5 (‫وا‬

<!Q x[key] C!QG ‫ إذا أردت ان‬، ‫ل‬8!‫ ا‬CH) N<Q6 . Q ‫]م < )< ك‬G ]‫ ا‬2)P‫ ا‬،  !@
‫" أ)< ب‬4 ‫د‬3 ‫( ﺙ‬tuples‫ و‬L‫( ا ا‬6 5 ]G (‫ ا‬C8 ) ;‫ ی‬class C‫; أﺝ‬9 ]‫) ا‬54‫ا‬
!5G4‫ ﻥ‬list HF U<!Q9 ‫ ن‬8‫ی‬9 ‫"ا  ی م‬56 ، ;‫( ذ‬6 4 c‫ إذا آ‬. ;<!@ ‫__ وی‬getitem__()

2)P‫ ا‬C‫ آ‬N<@ ‫ف‬Q ‫ ی أن‬c‫ إذا آ‬.(‫ول ا‬3‫( ا‬6 ‫ة ا] واردة‬4!‫ ا‬Methods /2)P‫ ا‬T"‫ ه‬kQ9
.‫ ن‬8‫ی‬H (Q‫ ا!ﺝ‬C‫( ا‬6  !]Z !L
‫ هك‬، ]‫ا‬
Table 15.1. Some Special Methods
‫!ح‬N ‫ا‬ @ k‫ا‬
‫!ل‬Q)d 8‫ ا! ی‬L‫ د ا‬Q‫ @  ی‬e6 N@G ‫ی‬g‫ ا‬T"‫وه‬ __init__(self, ...)
L‫ أن ی   ا‬CH
e6 N@G __del__(self)
str() ‫]م‬G‫ أو @  ﻥ‬L‫ ا‬print‫ن‬H‫]م ا‬G‫ @  ﻥ‬N@G __str__(self)
"&‫إ‬،>،+" C ‫ ا‬Q‫! ا‬3 . 2)‫ ی ﺝ أ‬C8!9‫ "< " و‬Less than N@G
__lt__(self, other)
C Q‫@ ا)]ام ا‬
x[key] )54‫]م @!< ا‬G  @ N@G __getitem__(self, key)
sequence/<G<G!‫ ا‬L len() 3 !‫!ل اا ا‬Q)‫ @ ا‬N@G __len__(self)

:Single Statement Blocks..‫!دة‬, ‫ ا‬g‫ر‬2 ‫ ا‬0‫آ‬

‫ ى‬G p4‫ ذات ﻥ‬5‫ ا‬.‫ أ‬9 ‫ﻥت ه( ﺝ^ء‬H‫ آ< ا‬C‫ را)& أن آ‬56 ;‫>( أن ی ن ی‬H‫ ی‬، ‫ن‬q‫وا‬
، ‫ﺝ^اء‬P‫ ا‬mQ! HG9 YM ‫ ه"ا‬، G .{ 9‫ اا‬C`4‫( ا‬6 indentation NQ ‫ }راﺝ‬indentation /<8‫ا‬
p4‫ ﻥ‬N<@ T‫د‬M ‫ |" ی!; أن‬، ‫ن وا‬9 ‫! ) ى‬b A ‫ﻥت‬H‫ آ< ا‬c‫ إذا آﻥ‬.٪ ١٠٠ 
‫ د‬cG 5‫و‬
.looping statement ‫ أو‬conditional statement ، d8 C ، gG‫ا‬
: ‫ ح‬Z 9 ;‫ل ا( یح ذ‬8!‫وا‬

>>> flag = True


>>> if flag: print 'Yes'
...
Yes

W‫ ا‬N<@ .<‫ ا‬CG H‫ آ‬p‫ و‬- ‫ ذات ا!ن‬C.‫( دا‬6 ‫]م‬G‫ ا ا ی‬Y‫ن ا`ی‬h6 ، ‫آ! ی! أن ﻥى‬
T"‫ ه‬short-cut ‫ی اـ‬F ‫]م‬G A‫ة أ‬9 (‫ وإﻥ( أو‬، >‫; أ‬3 ‫ﻥ‬9 CQ3 ‫ ی!; ا)]ام ه"ا‬، ;‫ذ‬
/Y‫ `ی‬6Z‫ إ‬89 C5)P‫ ) ن ا‬U‫ أﻥ‬GL‫ب ا‬H)P‫أ ا‬.&‫ ا‬، ‫ء‬g.P‫ ا‬nM‫ء  ا‬8)9
. <G‫ ا‬indentation /<8‫]م ا‬G c‫( إذا آ‬6Z‫ إ‬statement

> n‫ی‬F @ ‫ﻥت‬H‫ل ا‬.‫( إد‬6 ‫@ك‬G‫ن ذ; ی‬h6 ، (<@4‫ ا‬e!‫( ا‬6 ‫ ن‬8‫ی‬9 G4 ‫ @ ا)]ام‬U‫_ أﻥ‬A b‫أی‬
... N‫ > ا!ﺵ إ‬5‫ﻥ‬h6 ، if 4!‫ ا<! ا‬C. ‫ أن‬Q9 ، aboe  (6‫ و‬.Ld C9 prompts /‫ا!ﺵات‬
‫ح‬4 e>b‫ ﻥ‬، ‫ی‬g‫ ا‬T"59 statement ‫ ال‬C!‫ @  ﻥ‬.Q9 U ‫ء‬5‫ﻥ‬A‫  ی ا‬statement ‫ أن ال‬N‫ إ‬
‫ر‬m‫ ا!ﺵ ای واﻥ‬N‫ دة إ‬Q‫ وا‬U<‫ن آ‬H‫" ا‬4 ‫ ن‬8‫ی‬9 (5‫ ی‬، ;‫ ذ‬Q9 .c<!‫ﻥت
 اآ‬H‫ آ أن ا‬enter
.‫ت ا‬d.!‫ا‬

 
‫ ا‬$ M‫ ﺕ‬List Comprehension

‫ و ی أن‬، ‫@اد‬P‫! ا‬L


;‫ ی‬، ‫ل‬8!‫ ا‬CH) N<@ .M‫! ا‬L‫! ﺝیة ا‬L
‫اﺵق‬/‫ص‬d])A ‫]م‬G
.٢ H‫ @   ن أآ‬e6 ‫ و‬٢ (6 9‫و‬b ‫ر
م‬P‫! †ة ﺝ! ا‬L
N<@ C`M
. List comprehensions‫ت‬k ‫ ا‬E‫ ه‬06 K$ 6

Using List Comprehensions


Example 15.1. Using List Comprehensions

#!/usr/bin/python
# Filename: list_comprehension.py

listone = [2, 3, 4]
listtwo = [2*i for i in listone if i > 2]
print listtwo

Output

$ python list_comprehension.py
[6, 8]

0 A `$‫*آ‬
. (if i > 2) ‫ اوط‬kQ9   @ (2*i) U9 ‫>( ام‬H‫ ا"ي ی‬2@d‫ی ا‬M ‫ل‬d. ‫! ﺝیة‬L
n‫ ﻥ‬، ‫ه‬
`@ C‫ آ‬N‫ <  ل إ‬loops/‫<ت‬M‫]م ا‬G‫ ا!ات ﻥ‬8‫( ا‬6 .Q W ‫ ^ال‬A <P‫! ا‬L‫_ أن ا‬A
، 
‫ د‬8‫ی أآ‬F (‫ وه‬list comprehensions ‫)]ام‬9 nM‫ ا(ء ی! أن ی‬p4‫ وﻥ‬، !L
@
.  Z‫ وو‬،  ‫وإ‬

‫ ا وال‬+ @‫ وا
ا‬0‫ﺏ‬$ ‫ل ا‬7
 ‫ ا‬Receiving Tuples and Lists in Functions

** ‫ * أو‬L‫د‬9 ‫)]ام‬9 ‫ أو
 س‬tuple 54 9 ‫ اا‬parameters/‫ت‬d Q ‫ل‬H)A ، . ‫ی‬F ‫وهك‬
. ‫( اا‬6 arguments > ‫" @د‬.‫ @  ﻥ‬4  ‫ وه"ا أ‬.(‫ ا ا‬N<@

>>> def powersum(power, *args):


... '''Return the sum of each argument raised to specified power.'''
... total = 0
... for i in args:
... total += pow(i, power)
... return total
...
>>> powersum(2, 3, 4)
25

>>> powersum(2, 10)


100

args (6 5‫ اا ی ]^ی‬N‫ ا( !ر إ‬6Z‫ ا‬arguments !‫ وﺝ‬، args >!‫ ا‬N<@ * L‫د‬H‫ ا‬2HG9‫و‬
‫  ن‬parameters/‫ت‬d Q!‫ ا‬N‫ إ‬m‫ أن ی‬23‫ ی‬، * A9 c ])‫ **
 ا‬L‫د‬H‫ ا‬c‫وإذا آﻥ‬.tuple 54 9
.‫ < س‬key/value !
/‫ح‬4 ‫أزواج‬

:Lambda Forms
runtime/C>‫ ا‬c
‫ أﺙء و‬5@‫ه إرﺝ‬Q9‫ت دا ﺝیة و‬L‫ ﻥء آ‬lambda Y‫]م ا`ی‬G‫ی‬

Using Lambda Forms


Example 15.2. Using Lambda Forms

#!/usr/bin/python
# Filename: lambda.py

def make_repeater(n):
return lambda s: s * n

twice = make_repeater(2)

print twice('word')
print twice(5)

Output

$ python lambda.py
wordword
10
Using Lambda Forms
Example 15.2. Using Lambda Forms

#!/usr/bin/python
# Filename: lambda.py

def make_repeater(n):
return lambda s: s * n

twice = make_repeater(2)

print twice('word')
print twice(5)

Output

$ python lambda.py
wordword
10

: 0 A `$‫آ‬
. 5@‫ وإرﺝ‬runtime/C>‫ ا‬c
‫( و‬6 ‫ت دا ﺝیة‬L‫ ﻥء آ‬make_repeater ‫ه ؛ ا)]  اا‬
/HQ9 @ H parameter/d Q ". lambda ، ‫)س‬P‫( ا‬6 . ‫ <ا‬L‫]م ﻥء آ‬G‫ ی‬lambda Y‫ا`ی‬
. ‫یة‬3‫ل اا ا‬d. 5@‫ ی إرﺝ‬HQ‫ و
! ه"ا ا‬، ‫ اا‬T"5 G3‫ ا‬YH`‫ وا"ي ی‬e6 ‫ وا‬expression
e6 ‫ات‬HQ‫ و ا‬. lambda form C.‫]م دا‬G‫ ی! أن ی‬A print Y‫ ا`ی‬N U‫_ أﻥ‬A

The exec and eval statements

`‫< ﻥ‬G<)   !‫ ی‬، ‫ل‬8!‫ ا‬CH) N<@ .< ‫< ﻥ` أو‬G<) (6 5‫ ن ا( ی ]^ی‬8‫ی‬9 ‫ﻥت‬9 "4 ‫]م‬G‫ی‬
eG9 ‫ل‬8 ‫ وه"ا‬. exec ‫ن‬H‫)]ام ا‬9 ‫ﻥت‬H‫ ا‬T"‫" ه‬4 v ‫ و ﺙ‬runtime/C>‫ ا‬c
‫ ن @ و‬8‫ی‬H ‫] ي آ د‬
: C‫ـ‬4)9 T‫ا‬

>>> exec 'print "Hello World"'


Hello World

‫ن‬H‫ ا‬eval
:C4)9 ‫ل‬8!‫( ا‬6 ‫< ا` آ! ى‬G<G‫( ا‬6 ‫ ا( ]^ن‬M`‫ ن ا‬8‫ی‬9 ‫ات‬HQ /‫ب‬GM ‫]م‬G‫ی‬

>>> eval('2*3')
6

The assert statement "assert" g!2‫ﺕ‬


C
P‫ ا‬N<@ ‫ن ی; واا‬9 ‫ آا‬c‫ إذا آ‬، ‫ل‬8!‫ ا‬CH) N<Q6 . true  |‫" آ أن ﺵ‬assert" Y‫]م `ی‬G‫ی‬
Y‫ `ی‬HQ‫ |" ی‬، true ‫ إذا  ی‬g. 6‫ و‬، ;‫ ذ‬nM< U ]G ‫ ی أن‬c‫ وأﻥ‬، !L
(6 Q‫ا‬
. AssertionError  4‫" ی‬assert" Y‫ ا`ی‬C4‫ و@  ی‬.M‫ ا‬T"‫( ه‬6 8 "assert"

The repr function .. "repr"  ‫ا ا‬

‫ أو‬C‫ ی‬M b‫ أی‬N!G‫ )و‬Backticks ‫اـ‬. L `‫< ﻥ‬G<G (‫


ﻥ ﻥ‬C8! N<@ ‫` ل‬M< ‫]م‬G repr ‫اا‬
.c
‫ ا‬mQ eval(repr(object)) == object ;‫ ) ن ی‬U‫_ أﻥ‬A .UG4‫ ا(ء ﻥ‬CQ4 (‫)ت‬H
A‫ ا‬p@

>>> i = []
>>> i.append('item')
>>> `i`
"['item']"
>>> repr(i)
"['item']"

‫ت‬L‫ ا‬TQ !6 M‫ ی!; ا‬. Hg< <9


L< C8! N<@ ‫` ل‬M< ‫]م‬G backticks ‫ او‬repr ‫ اا‬، ))‫أ‬
.;9 ]‫ ا‬class ‫( ال‬6 __repr__ ‫ی‬F ‫ی‬M ‫ل‬d. repr ‫; <ا‬9 ]‫ا‬

: ‫ﺹ‬:%*
(6 ، ‫ و‬.‫ ن‬8‫ی‬9 Y d C‫ آ‬Q9 ve>‫ و ی!; اآ أﻥ  ﻥ‬،C`4‫( ه"ا ا‬6 ‫ ن‬8‫ی‬9 ‫ و ا!^ی !^ات‬
R ‫ا‬H‫أ أی ا‬H ; ‫ی‬4‫ ا‬U6  P‫ وه"ا ا‬. ;Hg (6 U ]G ‫  ) ف‬mQ gW 
‫ ﻥ ن‬،<!‫ ا‬T"‫ه‬
.5L‫<; ﻥ‬H) (6 c‫أﻥ‬

.‫ ن‬8‫ی‬9 ‫ ا)ف ا!^ی‬4‫ ) ف ﻥ


} آ‬،CH!‫ ا‬C`4‫( ا‬6
‫ا ‪ 02,‬ا ‪1‬دس ‪!NG‬‬
‫?‪ What Next‬وذا ﺏ‪ A‬؟‪-‬‬

‫  ا ت‬

‫ا‪3 H‬ت ا) ‪Graphical Software .................................................................................‬‬


‫ ﺝ^ أدوات‪Summary of GUI Tools .......................................................................... GUI‬‬
‫ا)ف ا!^ی‪Explore More ................................................................................................‬‬
‫‪Summary................................................................................................................. d.‬‬

‫إذا آ‪

c‬أت ه"ا اب ‪Q9‬ی ‪ N‬ا‪q‬ن‪ ،‬و‪d. UHF‬ل آ‪ 9‬ا‪Q‬ی ا‪H‬ا ‪ 9d6 ،R‬أﻥ; أ‪4® cMH‬‬
‫و ‪G‬ی‪9 M‬ی‪ 8‬ن ا‪q‬ن‪ .‬ور‪  !9‬ن أﻥت ‪ kQ9‬ا‪H‬ا ‪)A R‬ف ا!دة ا‪ !<Q‬ی; و‪ nHg‬ا!‪5‬رات ا(‬
‫اآ‪9 (6 5HG‬ی‪ 8‬ن‪ .‬إذا   ‪ ;<@ (>H6 ،CQ49 ;"9‬إن ‪ .CQ4‬وا‪G‬ال ا‪q‬ن ه ' ذا ‪Q9‬؟'‪.‬‬

‫وأود أن ا
ح @<; ‪ 3Q‬ه"‪ T‬ا!<‪ :‬ا ‪9 ;G4‬ﻥ ‪ gG9address-book R‬ا‪P‬وا  ‪])9 -‬ام  ی!;‬
‫إ‪ 6Z‬أو ‪Q‬ی‪ C‬أو "ف‪ ،‬أو ا‪ @ iMH‬ﺝ‪5‬ت ا‪`A‬ل ا] ‪;9‬؛ ‪ C8‬ا‪
P‬ء وا‪)P‬ة وا^ ‪d‬ء‪ ،‬و ‪5 <Q‬‬
‫ ‪ @ C8‬ان ا‪H‬ی اوﻥ( و ‪ /‬أو ر
 ا‪ .5‬ا‪ C4‬ی‪^] 23‬ی‪ 5‬وا)ﺝ@‪ (6 5‬و
‪.nA c‬‬

‫ه"ا أ  )‪ C5‬إ‪ ،   N‬إذا ‪6‬ت ‪ (6‬ﺝ! ا‪ Q‬ا!]<‪ 4‬ا( ﻥ‪ Q‬أﻥ رﻥ @‪H‬ه ‪ N‬ا‪q‬ن‪ .‬وإذا آ‪c‬‬
‫ی  ﺝ‪5‬ت ‪9‬ن آ‪ 4‬ا!‪ ;h6 ، 
(b‬ذ; ا<!‪.Y‬‬

‫ﺕ‪g$ -‬‬
‫)ی‪ (>H‬أ‪  A‬ن
أت ذ;(‪.‬‬
‫* أﻥ— ‪ <Q C8! class‬ت ا]‪.-‬‬
‫*ا)]م ا س ]^ی آ‪L‬ت ا]‪ -‬ا)!‪H@9 U‬ر‪4 T‬ح‪.key/‬‬
‫*ا)]م ا! دی‪^] cPickle C‬ی ا‪L‬ت ‪!)9‬ار @<‪ N‬اص ا`<‪.2‬‬
‫*ا)]م ا‪ 2)P‬ا! ‪ < 3‬س ‪ 6Z‬و"ف و‪Q‬ی‪ C‬ا‪P‬ﺵ]ص‪.‬‬

‫@   ن
درا @<‪ N‬أن ‪ CQ4‬آ‪ C‬ذ;‪ ،‬ی!; أن @( أﻥ; ‪9 R H‬ی‪ 8‬ن‪ .‬وا‪q‬ن‪ ،‬و@<‪ N‬ا‪ 4‬ر أر)‪9 ( C‬ی ﺵ‬
‫‪"5‬ا اب ا‪ .☺ mQ‬ه"‪ T‬ا]‪ g‬ة ا‪.‬ری و( أو; ‪.59‬‬

‫إ; ‪ kQ9‬ا‪g‬ق ! ا< ر<; ‪9‬ی‪ 8‬ن ‪:‬‬

‫ا ‪$/!7‬ت ا ! ‪Graphical Software $‬‬


‫ ‪H‬ت ‪}GUI‬واﺝ‪ 5‬ا!‪]G‬م ا) ‪])9 {Graphical User Interface-‬ام ‪9‬ی‪ 8‬ن ‪ --‬أﻥ‪M9 c‬ﺝ إ‪5‬‬
‫‪9 C!Q‬ا ‪ ;3‬ا)  ‪])9‬ام ‪9‬ی‪ 8‬ن‪ .‬ی!; إﻥء ‪ irfanview‬او ‪ kuickshow‬ا] ‪ ;9‬أو أي ﺵ(ء ‪C8‬‬
‫ذ; ‪!Q)9‬ل ‪H‬ت ‪9 (6 GUI‬ی‪ 8‬ن ا‪ 4<WP‬ا] ‪ .59‬ا‪ 4<WP‬ه( ا( ‪ ; Y!G‬ا‪9 (6 9‬ا ‪9 R‬ی‪ 8‬ن‬
‫وا)]ام ا!‪H‬ت ا( !‪ c‬آ‪  (6 59‬ذا‪ C ><9 5‬أو ‪ C++‬أو ‪W‬ه ا<>ت‪.‬‬

‫ه‪D‬ك ا ‪  !$6‬ا ‪$I‬رات ل‪ GUI‬ﺏ ‪ I‬ام ﺏ‪6‬ن ‪:‬‬

‫• ‪ .PyQt‬ه"‪ T‬ه( >< ‪9‬ی‪ 8‬ن `وق أدوات ‪ Qt‬ا"ي ه ا‪)P‬س ا"ي ‪<5) Qt .KDE U<@ c9‬‬
‫ا‪!Q)A‬ل <>ی ‪ ،‬و
ی ﺝا ‪  `.‬ﻥ‪m‬ا !`! ‪ Qt‬ووﺙ‪ 5L‬ا!"ه<‪ .‬ی!; ا)]ا ‪ `9 5‬رة‬
‫ة‪3 /‬ﻥ @<‪ ، p N‬و )‪ 6 gb‬ﺙ!‪ 5‬إذا آ‪ c‬ی ا)]ا ‪ N<@ 5‬ویوز‪PyQt .‬‬
‫ة‪3 /‬ﻥ إذا أردت إﻥء ‪9‬ا ‪ /p N<@ (GPL) `. HF R‬ی ﻥ‪ p‬و‪ 6‬ا!‪ C9‬إذا أردت إﻥء‬
'GUI Programming with Python: Qt ‫ ه‬PyQt ‫ة ـ‬3‫ ا!`در ا‬.< ‫ت ذات‬3 9
.C4‫ !^ی ا‬official homepage !)‫ ا‬M4`‫ ا‬m‫ اﻥ‬Edition'
U<@ c9 ‫)س ا"ي‬P‫و ا"ي ه ا‬GTK+ ‫ ن `وق أدوات‬8‫ی‬9 <> (‫ ه‬T"‫ ه‬. PyGTK •
.GNOME
‫ت‬Hg ‫ وی!; إﻥء‬، 5Q  YH` ‫د أن‬3!9 ;‫ وذ‬، ‫!ل‬Q)A‫( ا‬6 ‫ت‬W‫ ا!او‬8‫ ا‬5‫ ی‬GTK+
N<@ ‫ ﺝا‬C!Q GTK+ GM9 ]‫ ا‬nL‫ ا ﺙ‬.U@ NW A  ‫ ا)  أ‬5‫!`! ا اﺝ‬9 ‫ ا!ور‬.Q‫ )ی‬GUI
N<@ ‫ت ا!!< آ‬3 H‫ة وآ"; ا‬M‫ت ا‬3 H‫ ی!; أن ` ا‬.Q9 C!‫ ویوز  ی‬N‫ و ءه إ‬p
.C4‫ <!^ی ا‬official homepage !)‫ ا‬M4`‫ ا‬m‫ اﻥ‬. GTK+
.wxWidgets ‫ ن `وق أدوات‬8‫ی‬9 <> T"‫ ه‬. wxPython •
‫ ویوز‬، p N<@ C!Q‫ ﺝا و‬portable / !M (56 ;‫ ذ‬W‫ ور‬، 59 ‫ص‬. (!<Q NM 5 wxPython
‫ ی‬g‫|ت ا‬9 } IDEs ‫ی‬Q‫ ی ﺝ ا‬. embedded platforms 3 !‫ ا‬C!Q‫ `ت ا‬N<@ N‫ و‬، ‫ ك‬،
‫( و‬Stani's Python Editor) SPE C8 GUI ‫ `!!ت‬N‫ إ‬6Z9 wxPython ‫ا! <{  ـ‬
. wxPython N<@ ‫ت ا!!< آ‬3 H‫ة وآ"; ا‬M‫ت ا‬3 H‫ ی!; أن ` ا‬. wxGlade ‫ت‬5‫ `! ا اﺝ‬
. C4‫ <!^ی ا‬official homepage !)‫ ا‬M4`‫ ا‬m‫اﻥ‬
c‫ أﻥ; رأی‬9d6 IDLE c ])‫ ; وا‬nH) ‫ إذا‬. ‫( ا ﺝ د‬6 ‫دوات‬P‫ ا‬n‫ واة أ
م دی‬T"‫ ه‬TkInter •
. < ‫ ﺵ‬PythonWare.org " F-G TkInter ‫ـ‬9 ]‫ ا‬nL‫ ا ﺙ‬. C!Q‫ أﺙء ا‬TkInter
‫ه أن‬P‫ وا‬. ‫  ) اء‬N<@ ‫ وا ﻥوز‬p‫ی ﻥ‬/p C‫ آ‬N<@ C!Q‫ و‬portable/ !M TkInter
. $ $
‫ن ا‬6‫ ﺏ‬A‫ ﺝ=ء  ﺕز‬TkInter
GuiProgramming wiki page at Python.org : m‫ اﻥ‬، ‫• <!^ی ا]رات‬

GUI ‫ أدوات‬G #I-


‫دوات‬P‫ ا
ح @<; أن ]ر واة <; ا‬. ‫ ن‬8‫ی‬9 N<@ GUI ‫ ی ﺝ أداة
) واة ل‬A U‫_ أﻥ‬M‫ ء ا‬G
‫ اء أي‬6‫ ی!; ا‬C‫ ه‬: ‫رك ه‬.‫ی ا‬M (6 ‫ول‬P‫ ا‬C Q‫ ا‬. c‫; أﻥ‬4
N<@ !Q‫وذ; ی‬، Td@‫ا!"آ رة أ‬
. ‫ه! ؟‬d‫ أم وﻥوز أو آ‬p ‫ ؟‬5<@ C!Q‫; أن ی‬3 ‫ﻥ‬H ‫ أي ا!`ت ی‬: (‫ﻥ‬8‫ ا‬C Q‫ ا‬. ‫؟‬GUI ‫أدوات‬
. ‫ ؟‬GNOME ‫ أم‬KDE ‫ ؛‬p N<@ 5 ]G  )‫ت ا‬5‫ أي ا اﺝ‬: i8‫ ا‬C Q‫ا‬

:$-7
1 ‫ل ا‬2, ‫ا‬
‫ري ـ‬.‫ أن ی ن ا‬C!M!‫ و ا‬. )‫ت ا‬5‫ ا اﺝ‬3 9 @ ‫"ا اب‬5 <`6 ‫ أو‬C`6 9‫( آ‬6 ‫ت‬6 
N‫!م إ‬b‫ﻥ‬A‫ ع ی!; ا‬Z !‫  ل ه"ا ا‬m‫ت ﻥ‬5‫ إذا أردت إن م وﺝ‬.‫دوات‬P‫ آ]ر `وق ا‬wxPython
5<!@ !‫ت ا( ی‬GM‫دل اش ااء  ل ا‬H‫ أ‬i byte-of-python mailing list : ‫یی‬H‫! ا‬L‫ا‬
. ‫ اب‬5

: = ‫ف ا‬N ‫ا‬

U4 9 U‫ وه"ا یر إ‬.U@ iMH  H!‫ ا‬T"‫( ه‬6 3) ، c
‫ ا‬mQ (6‫ و‬.< ‫ ﺵ‬H ‫ ن‬8‫ی‬H )‫ ا‬H!‫ا‬
‫ء‬9 (6 Q9!‫ ا‬CH
‫ ن‬8‫ی‬H )‫ ا‬nL‫ل ا ﺙ‬d. ‫ ل‬3 ‫ن‬9 ‫ة‬9 N‫ أﻥ أو‬.‫ ن‬8‫ی‬9 (6 '6Z‫ریت ا‬gH‫ 'ا‬4G<6
.‫ ن‬8‫ی‬9 ><9 ‫ة‬H‫ آ‬R ‫ا‬9 9‫آ‬
G4 ‫ ن و‬8‫ی‬9 > ‫ أث اارات‬3) .‫ ن‬8‫ی‬H !)‫ ا‬GL‫ ا‬M4`‫ ه( ا‬T"‫ – ه‬Python.org •
.‫ ن‬8‫ی‬9 2‫  ل ]< ﺝ اﻥ‬g‫ي ا!
ت ا‬3 i ‫یی‬H‫ ا‬L‫ ]< ا ا‬b‫ وهك أی‬.><
;!‫ ی‬.><‫ ا‬T"‫ي اش  ل ه‬3‫ ی‬i ، H‫ ا‬N<@ ‫ر‬H.P‫! @ ا‬3 (‫ ه‬Comp.lang.python •
c‫ﻥﻥ‬A‫ ا‬N<@ ‫! @ت‬3!‫ ا‬T"‫ ه‬N‫ ی!; ا  ل إ‬.;< ‫ر‬H.P‫! @ ا‬3 N‫را; إ‬G4)‫<; وا‬L)‫إر)ل ر‬
‫س‬Q‫د اﻥ‬3 (‫ ا( ه‬mailing list ‫یی‬H‫! ا‬L‫ ا‬N‫!م إ‬b‫ﻥ‬A‫ أو ا‬Google Groups ‫)]ام‬9
.‫ر‬H.P‫! @ ا‬3!
C 4‫  ل آ‬YL`‫ت أو ا‬4 ‫! @ ا‬3 !‫ ﺝ‬i ‫
 <>ی‬2‫ آ‬: Python Cookbook •
.‫ ن‬8‫ی‬H ‫]م‬G C U‫
اء‬9 A ‫ ه"ا اب‬.‫ ن‬8‫ی‬9 ‫)]ام‬9 C‫ أﻥ اع ا!آ‬kQ9
. David Mertz. 5H‫ آ‬.‫ ن‬8‫ی‬9 @ ‫ت
! <>ی‬A <G<) : Charming Python •
‫ إذا
أت !  ه"ا اب‬.‫ ن‬8‫ی‬9 (3 H ‫ة‬H]‫ وه آب ﺝ ﺝا "وي ا‬: Dive Into Python •
Q9 '‫ ن‬8‫ی‬9 (6 ‫ 'ا> ص‬Dive Into Python ‫ی ا  أن أ‬W ;‫ و )و‬،‫ن أ‬q‫ ا‬c‫ﻥ‬6 (M‫ا‬
XML ^53‫ اد وا‬d <9‫( ذ; > ا ^ ا‬6 !9 Z‫! @ ا! ا‬3 C!‫ وه ی‬.;‫ذ‬
.Functional Programming ‫ و‬Unit Testing ،Processing
‫ ن وا)]ام‬8‫ی‬9 (6 R ‫ا‬9 9‫ ی!; آ‬U‫( اﻥ‬Q‫ وه"ا ی‬.6‫( > ﺝ‬6 ‫ ن‬8‫ی‬9 G4 ‫ت‬Hg ‫ ه أ‬: Jython •
‫ن‬9 ‫ة‬9 ;M`‫ وأﻥ أﻥ‬،;"‫ آ‬6‫ ﺝ‬R H c‫ إذا آ‬.RZ‫ وﻥ‬G R ‫ﻥ‬9 Jython !;"‫ آ‬6‫ت ﺝ‬H
.; ‫و‬M jython (gQ
NET / Mono / ` N<@ U<> !‫ وی‬.C# ><‫( ا‬6 ‫ ن‬8‫ی‬9 G4! nHg ‫ ه‬: IronPython •
(‫ى ا‬.P‫ت ا‬H!‫ وا‬NET ‫ت‬H .‫ ن وا)]ام‬8‫ی‬9 (6 R ‫ا‬9 9‫ ی!; آ‬U‫( اﻥ‬Q‫ وه"ا ی‬.DotGNU
e6 Y<`‫ وی‬pre-alpha <!9 3 9 ‫  ^ال‬Iron python . ;"‫ث آ‬d8‫ ا!`ت ا‬T"‫ه ه‬6 
.‫ن‬q‫ ا‬N 2‫ی‬3<

C‫ اﺝ‬C!Q‫ و) ا‬، c6 )‫ ﺵآ یو‬N‫ إ‬b‫


 اﻥ‬ironPython 2‫ وا"ي آ‬، Jim Hugunin
. CHG!‫( ا‬6 ironPython < ‫ اارة ا‬N‫ إ‬C ‫ا‬

bytecode N‫ﺵة إ‬H ‫ وﺝ‬Lisp ‫ ـ‬59 (‫ وه‬.‫ ن‬8‫ی‬9 >< Lisp frontend 5‫ ه واﺝ‬:Lython •
.‫د‬Q!‫ ن ا‬8‫ی‬9 ‫< آ د‬.‫ دا‬C!Q ‫ ) ف‬5‫( أﻥ‬Q‫  ا"ي ی‬P‫ ا‬، ‫ ن‬8‫ی‬9

‫ ا"ي‬Daily Python-URL!
!5!‫ ر ا‬P‫ و ا‬.‫ ن‬8‫ی‬9 (6 ‫ی ا! ارد‬Q‫ی وا‬Q‫• وهك ا‬
, Vaults of Parnassus T"‫ وآ"; ه‬، ‫ ن‬8‫ی‬9 ‫ أاث‬.o N<@ ‫ث‬M ‫ و‬L‫ع دا‬dF‫ ا‬N<@ ;<Q3‫ی‬
. 8‫ وا‬8‫ وا‬Python Notes, dirtSimple.org, ONLamp.com Python DevCenter

:‫ﺹ‬:I ‫ا‬
HQ c
‫ أي و‬8‫ن وأآ‬q‫ ا‬c‫ أﻥ‬." !‫ی‬5‫ای ا‬9 (‫ آ! ی  ن "ه"ا ه‬، ‫ی ه"ا اب و‬5‫ ﻥ‬N‫ن إ‬q‫ و< ا‬
{!‫(  }أ‬6 ‫ء‬H‫ ی!; ا‬.‫ ن‬8‫ی‬9 ‫)]ام‬9 C‫ی ا!آ‬Q‫ ا‬CM QG ;‫ ﺵ‬d9 c‫ وأﻥ‬،‫ ن‬8‫ی‬H !ِ5‫]  ﻥ‬G
;9 . ‫ب‬Q‫  ی أ‬2‫ أو اآ‬،5<] !‫ ی‬A ‫( أ را‬Z!‫( ا‬6 c‫ﻥ اع ا( آﻥ‬P‫ ا‬C9 ‫زك <م‬5‫ﺝ‬
. ! ‫ء‬H‫( ا‬6 ‫ ِاﺵع‬،‫ و"ا‬.8‫ ا‬8‫وا‬

*☺*☺*☺*


 ‫ واة وام   و 
و‬...‫ا  ا ي  
  ات‬
‫! ا "ت‬
5"6‫ ا‬," 4& 23‫ا‬.‫ ب ا‬1‫ ه ا ا‬,-. + ‫
ا*) (ء‬%&‫ن ا و‬$  
‫ ﺡ‬2.45 ,‫ ا‬- ‫م‬2008 BC.‫ أ‬+ 17 @&‫ا‬$‫ ا‬- ‫هـ‬1429.9:‫ ا‬2"‫ ر‬+11
...‫اء‬.N‫ ا‬4)‫ا‬$9‫ 
إ‬2L C ‫ان ﺡ   وأن‬K" 4& ‫ن‬$1C ‫ أن‬."‫ ا‬2"‫!ل ا ا‬J‫أ‬
http://www.linuxac.org4. ‫ ا‬51 " 2 P ‫ء‬%‫  ( أ‬O9‫أ‬
c"e‫ ا‬.‫ه‬d cN‫ ا‬+ ,a‫ة د‬$‫ د‬+ )$. * ..‫ ب‬1‫ ه ا ا‬43‫ر‬a 4)‫ا‬$9‫ إ‬+ ‫ء‬-.

[kaspersky0] i9 4 ‫ف‬.h‫أ‬:


 ‫آ‬
. - ,C‫ ر‬1Jp‫ا‬

You might also like