Top Banner
ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΤΜΗΜΑ ΗΛΕΚΤΡΟΛΟΓΩΝ ΜΗΧΑΝΙΚΩΝ ΚΑΙ ΤΕΧΝΟΛΟΓΙΑΣ ΥΠΟΛΟΓΙΣΤΩΝ 22Y103 ΕΙΣΑΓΩΓΗ ΣΤΟΥΣ ΥΠΟΛΟΓΙΣΤΕΣ Εργαστηριακές Ασκήσεις Κ. Σγάρμπας, Ν. Αβούρης, Π. Σταθοπούλου Πάτρα - Σεπτέμβριος 2010 (Έκδοση 3.01)
136

22Y103 ΕΙΣΑΓΩΓΗ ΣΤΟΥΣ ΥΠΟΛΟΓΙΣΤΕΣcgi.di.uoa.gr/~ys02/22y103_lab_201011_v301.pdf · • Εισαγωγή στον προγραμματισμό με χρήση

Feb 06, 2018

Download

Documents

buitruc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 22Y103

    . , . , .

    - 2010 ( 3.01)

  • 2

  • 3

    1. ..................................................................................................................................................... 5

    ........................................................................................................................................................... 5 .......................................................................................................................................... 5 ..................................................................................................................................... 6 ...................................................................................................................................................... 7 ................................................................................................................................................... 7

    2. .......................................................................................................................11 ........................................................................................................................................11

    1: , , .................................................................................................................................................13

    1.1 ......................................................................................................................................13 1.2 eClass (http://eclass.upatras.gr)...................................................14 1.3 (http://hci.ece.upatras.gr/labs)...............................................................................................................................................................19

    2: : FTP, EMAIL, , HTML ...........................................................................................................................................28

    2.1 ...............................................................................................................................................28 2.2 .................................................................................................................28 2.2 FTP...................................................................................................................................30 2.4 .............................................................................................32 2.5 HTML**.............................................................................................................................................33 ................................................................................................................................................37

    3: : ...............................39 3.1 ...............................................................................................................................................39 2 (. , . , . )..................................................................39 3.2 ...........................................................................................................................................39

    4: 2 :.........................................43 4.1 ...............................................................................................................................................43 2 (. , . , . )..................................................................43 4.2 ...........................................................................................................................................43

    5: PYTHON .................................................................48 5.1 ...............................................................................................................................................48 5.2 ................................................................................................................48 5.3 Python ..............................................................................................50 5.4 ...............................................................................................................53 5.5 input, if, else.........................................................................................................................56 5.6 2 .......................................................58 5.7 ...........................................................................................................................................61

    6: PYTHON.............................................................62 6.1 ...............................................................................................................................................62 6.2 ......................................................................................................................62 6.3 for .................................................................................................63 6.4 ......................................................................................................................66 6.5 ......................................................................................................................71 6.6 ...................................................................................................................73 6.7 ...........................................................................................................................................81

    7: PYTHON...................................................................83

    http://eclass.upatras.grhttp://hci.ece.upatras.gr/labsftp://ftp...................................................................................................................................30

  • 4

    7.1 ...............................................................................................................................................83 7.2 ...............................................................................................................................................83 7.3 ............................................................................................................................84 7.4 ...............................................................................................................................................90 7.5 ............................................................................................................................................93 7.6 ...........................................................................................................................................97

    8: PYTHON .............................................................99 8.1 ...............................................................................................................................................99 8.2 .....................................................................................................................99 8.3 ...................................................................................................103 8.4 ..........................................................................................................................104 8.5 e-mail .............................................................................................................................105 8.6 .........................................................................................................................................107

    9: PYTHON ............................................................108 9.1 .............................................................................................................................................108 9.2 ..................................................................................................................108 9.3 ( ) ............................................................................114 9.4 .............................................................................................................120 9.5 .........................................................................................................................................123

    10: ..................................................................................................125 ...................................................................................................................................................125

    3. ...................................................................................................................................126 .....................................................................................................................................................126

    4. .................................................................................................129 ........................................................................................................................................................129 ...............................................................................................129 ............................................................................................................................................130 PASSWORD WINDOWS................................................................................................130 UNIX ..........................................................................131 PASSWORD UNIX......................................................................................................131 .....................................................................................................................132

    5. ............................................................134 ? ...................................................................................134 LOGIN .........................................135 ...............................................................................................136

  • 5

    1.

    "22103 " 1 , , . Python. , :

    Python.

    , ,

    (project) . , , . . . 4 .

    . 13 . . (10)

  • 6

    (1) . :

    1 2 3 1 4 2 5 Python 1 6 Python 2 7 Python 3 8 Python 4 9 Python 5 13

    7 12 . 7 (6) . , .

    , , , . . . 30% 22103 . , , 4-5 , , , , . , . . , 50 ,

  • 7

    . . (2) . , , .

    . (http://hci.ece.upatras.gr) (http://hci.ece.upatras.gr/labs), . http://kypes.ece.upatras.gr/.

    . , . .

    http://hci.ece.upatras.grhttp://hci.ece.upatras.gr/labshttp://kypes.ece.upatras.gr/

  • 9

  • 11

    2.

    ; , 10 . : 1 2 : FTP, Email, , HTML 3 : 4 : 5 1 6 2 7 3 8 4 9 5 10 ; . . ; ( ) , . 10, . ; . . ; 22103 (40%),

  • 12

    (60%). ; . . , . 50% , . . . ; , . . . ; . , . . , , . ( ) ; , . ; , , , .

  • 13

    1: , ,

    1.1 ()1 22103 . . . .

    http://www.ece.upatras.gr o http://myece.ece.upatras.gr T (UPNet) http://www.upnet.gr e-class http://eclass.upatras.gr/ http://www.upatras.gr

    22103 . eClass , . eClass . ; - . 1 (computer-room) .

    http://www.ece.upatras.grhttp://myece.ece.upatras.grhttp://www.upnet.grhttp://eclass.upatras.gr/http://www.upatras.gr

  • 14

    .

    1.2 eClass (http://eclass.upatras.gr)

    eClass. 22103 , . eClass , . eClass , . Internet eClass http://eclass.upatras.gr, . !!!! .

    eClass 22103 , , . , , . , (username) (password), , & . http://eclass.upatras.gr (web browser, Internet Explorer, Mozilla Firefox) 1. eClass 1. 2

    http://eclass.upatras.grhttp://eclass.upatras.grhttp://eclass.upatras.gr

  • 15

    1. / . LDAP 3 .

    2. .

    2. . 2 :

  • 16

    3. LDP.

    e-mail: [email protected] e-mail: xxxxxxx &

    / , & . . eClass.

    4. .

    mailto:[email protected]

  • 17

    4, . & 5 .

    5. .

    :

    5 ( 6) , , . 7, 8 eClass.

  • 18

    6. .

    7. .

    8. eClass.

    eClass . 22103 .

  • 19

    . !!! username e-mail (. [email protected]) password , eClass. eClass http://eclass.upatras.gr/manuals/manual.php

    1.3 (http://hci.ece.upatras.gr/labs) 22103 . http://hci.ece.upatras.gr/labs. - . T Moodle . . 2005-2006, . , , . .

    mailto:[email protected]://eclass.upatras.gr/manuals/manual.phphttp://hci.ece.upatras.gr/labshttp://hci.ece.upatras.gr/labs

  • 20

    - . (. ):

  • 21

    .

    . (login) (password) . .

    : aYY-XXXX :

    !

    : = (.. 2008 YY=08) = (.. 4548) , Email, , . : email . email upnet (@upnet.gr)

    Email: [email protected]

    : .

    : .

    mailto:@upnet.gr)mailto:[email protected]

  • 22

    .

    .

    Email

    (login) (password) . ( ) .

  • 23

    :

    aYY-XXX*

    (login password) , .

    , login password, .

    * = (.. 2006 YY=06) = (.. 4548)

  • 24

    . . . , .

    , . . , .

  • 25

    . .

  • 26

    : )

    , .

    ) .

    )

    . 14 1 .

    ) .

  • 27

    )

    , , . :

    email (.. [email protected]). . . Forum forum . forum . pdf . pdf ( ) save a copy. .

    mailto:[email protected])

  • 28

    2: : FTP, Email, , HTML

    2.1 , . (. 219-249).

    2.2 . . Windows Linux Mozila Thunderbird, Microsoft Outlook, Eudora, Netscape Messanger , , . , gmail.com, hotmail.com . webmail . webmail . (gmail, hotmail, yahoomail ): login password . http://www.upnet.gr/email.php https://mail.upnet.gr .

    http://www.upnet.gr/email.phphttps://mail.upnet.gr

  • 29

    . :

    : . .

    Drafts: . .

    Sent: .

    Trash: .

    . :

    : .

    : mail .

    : , Drafts .

    : .

    : . .

    : .

    . , . , .

  • 30

    (.. , , .) . . (forward) , . . (reply). T . . . reply , ( ).

    2.2 FTP FTP (File Transfer Protocol) . (client) , (server) . , (login name) (password). ( ) , (login name) anonymous (password) . login name password. . FTP (ftp.ntua.gr) . .

    ftp://ftp.ntua.gr)

  • 31

    ftp . ftp . ftp , . FTP prompt ftp ( ). prompt prompt FTP ftp>, . FTP ?, ? [] .. ? get. , :

    open ftp . close . bye ftp. pwd

    . ls

    . cd . get . mget . put . mput . FTP: open: ftp ftp . open. open, ftp , (login name) (password). cd: cd [ ] [ ] . get: get . get cp Unix. :

    get s_path/s_file d_path/d_file

  • 32

    : s_path

    , s_file , d_path

    d_file

    . get notes. s_path, d_path d_file notes , ftp notes . mget: mget get . mget file1 file2 file3 file1, file2, file3 . : mget *. : , get ... mget ... !ls.

    2.4 (internet) , , , , , , video .. , ( ) (links) , . , , , . , (web server) , (client), . (browsers) , . Microsoft Internet Explorer, Mozila FireFox, Google Chrome, Safari, Opera . .

  • 33

    , , video. "" (MS Word .). "", . . URL (.. www.ece.upatras.gr) (links). Ctrl-F Find. Bookmark Ctrl-D. Ctrl-B. Ctrl-P ( Print), Alt-< Alt-> ( Back Forward, ).

    2.5 HTML** . , (- , ), , . 250-266 (HTML tutorial), HTML: http://www.w3schools.com/html/ ( USB flash disk) . HTML (HyperText Markup Language). (.. ) . HTML. HTML ( vi Unix Notepad Windows). HTML (tags) . .

    ... ...

    2.1 .

    http://www.ece.upatras.grhttp://www.w3schools.com/html/

  • 34

    :

    2.2 .

    < > (.. , , .) (tag). * , . . :

    . :

    . ..

    . ..

    . ..

    . . ..

    . ( .)

  • 35

    2.3 .

    2.4 .

    :

    . . .. 1 2

    (HR = Horizontal Rule) . . .. 1 2

    .

  • 36

    1 2

    1 2

    2.5 .

    2.6 .

    1 2 . ALIGN=Left . :

    To . .. & .. Mail me

    . . .. :

    .

    http://www.ee.upatras.grmailto:[email protected]>Mail

  • 37

    & Mail me :

    2.7 .

    2.8 .

    . . , click .

    , , . 2.1 (webmail):

    1. mail.upnet.gr . ( ) (forward) . ( email

    http://www.ee.upatras.grmailto:[email protected]

  • 38

    .). .

    2. google groups facebook ( ).

    2.2 :

    1. & (2 , 2 2 ...). a) ; b) . c) . universities

    2. . ; ; openuniv

    3. 3 . (, , ). trip.

    (universities, dvd, openuniv, course, trip) . 2 A4. 2.3 ** .html ( ) (.. ) . HTML . .html, , : ) ) ) (, , .) ) . . ! (: )

  • 39

    3: :

    3.1 . . Synergo. . Synergo.

    2 (. , . , . ) : Synergo (http:/hci.ece.upatras.gr/synergo), (HCI group). SYNERGO , , , .

    3.2

    : , . ) N . To N . max, min . : 1) . : chat ( ) chat . 2) . . 3) sticky notes ( )

    : http:/hci.ece.upatras.gr/synergo . username password Username: student Password: student

  • 40

    Options ( ) Start Synergo . . :

    , AM username password . :

    Username: a07-5634 Password: a07-5634

    , , : - (.. a07-5242-maxminmo). , . , , : 1-AM2- (.. a07-5242-a07-5642- maxminmo). , .

    o o ( *.synergo) My Documents, My models. My models eclass email . ( ), .

  • 41

    *.synergo , *.model *,history ZIP .

    Synergo

    The Process Symbol represents any process, function, or action and is the most frequently used symbol in flowcharting.

    The Input/Output Symbol represents data that is available for input or resulting from processing (i.e. customer database records).

    The Decision Symbol is a junction where a decision must be made. A single entry may have any number of alternative solutions, but only one can be chosen.

    The Connector Symbol represents the exit to, or entry from, another part of the same flow chart. It is usually used to break a flow line that will be continued elsewhere. It's a good idea to reference page numbers for easy location of connectors.

    , ,

    . (2) .

    . ) ) : (connect with) ) sticky notes, , : insert sticky note

  • 43

    4: 2 :

    4.1 ( 3) . . Synergo. . Synergo.

    2 (. , . , . ) : Synergo (http:/hci.ece.upatras.gr/synergo), (HCI group). SYNERGO , , , .

    4.2

    : n . . (Binary Search). . .

    o

    1) n :

    . ,

  • 44

    , . 2) x/y xDIVy 3) . : chat ( ) chat . 4) sticky notes ( ) 5) .

    n=6 =246

    1. position 2.

    2

    5

    67

    128

    245

    246

    1 2 3 4 5 6

    3. position 246? (67< 246) 4. 5.

    2

    5

    67

    128

    245

    246

    1 2 3 4 5 6

    6. position 246? (245< 246) 7. 8. ...

    ( . )

    min Max position

    min Max

    position

  • 45

    : http:/hci.ece.upatras.gr/synergo . username password Username: student Password: student

    Options ( ) Start Synergo . . :

    , AM username password . :

    Username: a07-5634 Password: a07-5634

    , , : - (.. a07-5242-binary). , . , , : 1-AM2- (.. a07-5242-a07-5642-binary). , .

  • 46

    o o ( *.synergo) My Documents, My models. My models eclass email . ( ), . *.synergo , *.model *,history ZIP .

    , ,

    . (2) .

    . ) ) : (connect with) ) sticky notes, , : insert sticky note

  • 47

    The Process Symbol represents any process, function, or action and is the most frequently used symbol in flowcharting.

    The Input/Output Symbol represents data that is available for input or resulting from processing (i.e. customer database records).

    The Decision Symbol is a junction where a decision must be made. A single entry may have any number of alternative solutions, but only one can be chosen.

    The Connector Symbol represents the exit to, or entry from, another part of the same flow chart. It is usually used to break a flow line that will be continued elsewhere. It's a good idea to reference page numbers for easy location of connectors.

  • 48

    5: Python

    5.1 Python. Python . . 1990 Guido van Rossum. : v (interactively)

    . .

    v , .

    . (. ), .

    v C, C++ Java. v site

    http://www.python.org ( ). Python .

    5.2 Python . (. ) : v http://www.python.org

    http://www.python.orghttp://www.python.org

  • 49

    v DOWNLOAD ( )

    . 3 .

    v .

    (on-line help), site .

    . Python. . (Python Command Line) Integrated Development Environment (IDLE Python GUI). IDLE.

    3 : site Python , 2.x 3.x. 2 3 ( ). (2010) 2.7 2 . (2008) 2.5 .

  • 50

    :

    Python.

    5.3 Python +, -, *, /. print. : >>> print 5+3 8 >>> print (7-2)*4 20 . , : >>> print 7+2, 7-2, 7*2, 7/2 9 5 14 3 . Python , . ( ) . .: >>> print 7/2, 7/2.0, 7.0/2, 7.0/2.0, 7./2, 7/2., 7./2. 3 3.5 3.5 3.5 3.5 3.5 3.5

  • 51

    , **. .: >>> print 9**2, 9**0.5 81 3.0 Python . .: >>> print 2**1000 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 , (Memory Register = MR). Python . (variables) ( ) : >>> a=5 a 5. >>> metavliti2=34.45 , metavliti2 34.45. : >>> print a, metavliti2, metavliti2/a >>> 5 34.45 6.89 Python , . a+bj j . .: >>> n=5+2j >>> m=2+3j >>> print n, m, n+m, n-m, n*m, n/m (5+2j) (2+3j) (7+5j) (3-1j) (4+19j) (1.23076923077-0.846153846154j) . . , 1, : >>> print (-1)**0.5 Traceback (most recent call last): File "", line 1, in (-1)**0.5 ValueError: negative number cannot be raised to a fractional power

  • 52

    ... . : >>> print (-1+0j)**0.5 (6.12323399574e-17+1j) ... 4 ( 6.123x10-17). Python , , , , , (. , , ). on-line help Python . , Python import. , (cos) math, : >>> from math import cos : >>> print cos(0.5) 0.87758256189 , Python. * . .: >>> from math import * >>> print cos(0.5), sin(0.5), tan(0.5) 0.87758256189 0.479425538604 0.546302489844 , , . Python . .

    4 : Python . Python ( ) ( bits) . , .

  • 53

    5.4 . 2x2-5x+2=0. , - , : >>> a=2 >>> b=-5 >>> c=2 >>> diak=b*b-4*a*c >>> print diak 9 , : >>> rd=diak**0.5 : >>> x1=(-b-rd)/(2*a) >>> x2=(-b+rd)/(2*a) >>> print x1, x2 0.5 2.0 . : >>> print 'x1=', x1, " x2=", x2 x1= 0.5 x2= 2.0 print . , ( ) . . 2x2-5x+2=0 , 2x2-5x+3=0. c 2 3. -, ! Python . . File>New Window (Ctrl+N) IDLE...

  • 54

    ... ( copy-paste , >>> )...

    ... Save As (Ctrl+Shift+S). , , .py. . triwnymo.py.

    Run>Run Module (F5)...

  • 55

    ... :

    c=3, , 2 2x2-5x+3=0.

    triwnymo.py . , . (. 2 ), . , a,b,c .

  • 56

    , . ( ). . . ( ) . , . . , . , .

    5.5 input, if, else print . input. : >>> a=input() 4 >>> print a 4 a input. , , input a, a . input : >>> b=input(' :') :12 >>> print b 12 . ( )

  • 57

    , . if. : ... a=input( :) b=input( :) if b==0: print .

    b= input( :) print :, a/b ... , a () b () . . if. b==0 ( b ) , (TAB) , if ( if) print. , print. (: Python TABs . .) =. , . == . if :

    < > = == !=

    if else , . : ... a=input( :) if a>=0: print ( ).

    b=a else: print . b=-a print :, b ... , . if a>=0 .

  • 58

    , else a>=0 . print , . , if , if, .

    5.6 2

    2 . , . . 2 :

    . . if . , , if. ,

  • 59

    , else. 5:

    . ( ), . ( ) .

    5 (# -*- coding: cp1253 -*-) (codepage 1253). , , , 1 2 . cp1253 (Windows ) utf-8 ( Unicode).

  • 60

    , x1 x2 (2.0*a) (2*a) . Python, . , a=0 b , : b=b+0.0 b, , . Python b , . , . . : diak=diak+(0+0j) diak , diak . :

  • 61

    5.7 . Moodle . . #1 5.6 . . #2 6 3 Ax3+Bx2+Cx+D=0. Synergo .

    Ax3+Bx2+Cx+D=0 A=0 2 #1. A

    : 023 =+++ cbxaxx , ABa = ,

    ACb = ,

    ADc = .

    : 23223 274418 cbbacaabc +=

    >0, 3 . =0, 3 ( ).

  • 62

    6: Python

    6.1 Python, , , .

    6.2 Python import. . f(x)= 0.0001*x3+0.015*x2-1.14*x-2.8. Python def : >>> def f(x): return 0.0001*x**3+0.015*x**2-1.14*x-2.8 , def . return ( , print). : >>> print f(3.5) -6.6019625 >>> print f(-12), f(111.11) 12.8672 192.886177563 ( ), . , a, b c: >>> def diakrinousa(a,b,c): return b*b-4*a*c >>> print diakrinousa(4,2,-1) 20 , , ,. : >>> def remainder(x,y): z=x/y

  • 63

    r=x-z*y return r >>> def check_odd_or_even(x): y=remainder(x,2) if y==0: return 'even' else: return 'odd' remainder . check_odd_or_even remainder (odd) (even). >>> print remainder(8,3) 2 >>> print check_odd_or_even(13) odd >>> print check_odd_or_even(14) even check_odd_or_even ().

    6.3 for f(x) x. print, Python : >>> for x in [0, 1, 2, 3, 4]: print ' x=', x, ' f(x)=',f(x) x= 0 f(x)= -2.8 x= 1 f(x)= -3.9249 x= 2 f(x)= -5.0192 x= 3 f(x)= -6.0823 x= 4 f(x)= -7.1136 [0, 1, 2, 3, 4] . for x ( print) x. Python range : >>> print range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range , 0. >>> print range(-3,4)

  • 64

    [-3, -2, -1, 0, 1, 2, 3] . >>> print range(1,15,2) [1, 3, 5, 7, 9, 11, 13] . 15, 1 2. , , range() for, : >>> for x in range(100): print ' x=', x, ' f(x)=',f(x) x= 0 f(x)= -2.8 x= 1 f(x)= -3.9249 x= 2 f(x)= -5.0192 x= 3 f(x)= -6.0823 ... x= 97 f(x)= 119.0223 x= 98 f(x)= 123.6592 x= 99 f(x)= 128.3849 . (nested). , . : 1: (a, b, c) 0

  • 65

    8 15 17 9 12 15 9 40 41 10 24 26 12 16 20 12 35 37 15 20 25 15 36 39 16 30 34 18 24 30 20 21 29 21 28 35 24 32 40 27 36 45 for a b b c. range a> def is_prime(n): if n>> for t in range(20): print t, is_prime(t) 0 no 1 no 2 yes 3 yes 4 no

  • 66

    5 yes 6 no 7 yes 8 no 9 no 10 no 11 yes 12 no 13 yes 14 no 15 no 16 no 17 yes 18 no 19 yes . . ( a n, n/a n) .

    6.4 Python . (turtle graphics). Logo, 1980 . : >>> from turtle import * , 8 : >>> reset() Turtle Graphics:

    8 . .

  • 67

    ( cursor, ) . reset , . (0,0). goto. .: >>> goto(300,0)

    300 pixels ( ) . goto ( ). goto : >>> goto(-200,-100) >>> goto(0,0)

  • 68

    >>> goto(0,110) >>> goto(-110,110) >>> goto(-110,0) >>> goto(0,0)

    : >>> speed(fastest) ... . , up, : >>> up() >>> goto(100,100)

  • 69

    : >>> down() ... . (up) , (goto) (down) . . ( . , 100 pixels : >>> forward(100) >>> left(120) >>> forward(100) >>> left(120) >>> forward(100) >>> left(120) forward(100) 100 pixels . left(120) 120 . 3 , :

    : >>> up() >>> goto(-250,130) >>> down() >>> forward(100) >>> left(120)

  • 70

    >>> forward(100) >>> left(120) >>> forward(100) >>> left(120) >>> up() >>> goto(200,-170) >>> down() >>> forward(100) >>> left(120) >>> forward(100) >>> left(120) >>> forward(100) >>> left(120)

    ( goto) . turtle on-line help9 Python:

    9 on-line help, , . forward(100) fd(100). turtle. help . import .

  • 71

    . .

    6.5 6.2 f(x)= 0.0001*x3+0.015*x2-1.14*x-2.8 : >>> def f(x): return 0.0001*x**3+0.015*x**2-1.14*x-2.8 . : >>> reset() >>> speed('fastest') >>> forward(300) >>> goto(0,0) >>> left(90) >>> forward(300) >>> goto(0,0) >>> left(90) >>> forward(300) >>> goto(0,0) >>> left(90) >>> forward(300) >>> up() >>> goto(-300,f(-300)) >>> down()

  • 72

    (-300,f(-300)) . . : >>> color('red') : >>> for x in range(-300,300): goto(x,f(x))

    f(x) [-300,300]. . x y . , sin(x). pixel 6 pixels, . , : >>> from math import * >>> cy=200 >>> cx=36. >>> x=-300 >>> up() >>> goto(x,cy*sin(x/cx)) >>> down() >>> for x in range(-300,300): goto(x,cy*sin(x/cx))

  • 73

    cx, cy x y . 200 pixels 2*36=226 pixels .

    6.6 turtle circle(r) r. 100 . random () : >>> from random import * random random() 0 1: >>> print random() 0.178361201055 >>> print random() 0.671443452792 randint(low,high) [low,high]: >>> print randint(10,100) 47 : >>> for i in range(100):

  • 74

    up() x=randint(-300,300) y=randint(-250,200) goto(x,y) down() color(random(),random(),random()) r=randint(5,50) circle(r) (x,y) x [-300,300] y [-250,200]. color(R,G,B). color . , (, , ) . [0,1] . r [5,50] . 100 for. :

    ; circle(r) . help turtle, . . 6.4 . r: >>> def triangle(r): forward(r) left(120) forward(r) left(120) forward(r)

  • 75

    left(120) : >>> clear() . clear() reset() , . >>> for i in range(100): up() x=randint(-300,300) y=randint(-250,200) goto(x,y) down() color(random(),random(),random()) r=randint(5,50) triangle(r)

    . 100 0 99. ( is_prime ) . . . write(). : >>> clear() >>> for i in range(100): up() x=randint(-300,300) y=randint(-250,200) goto(x,y) down() color(random(),random(),random()) r=randint(5,50)

  • 76

    if is_prime(i)=='yes': circle(r) else: triangle(r) write(i)

    , , . circle . , : >>> circle(50,360) ( 360 ) 50, : >>> circle(50,180) ... ( 180 ). , circle . . ( ). : >>> reset() >>> speed('fastest') >>> for i in range(1000): circle(randint(-40,40),randint(-45,45)) 1000 , 40 pixels 45 :

  • 77

    . : >>> reset() >>> for i in range(20): circle(10+5*i,180)

    20 . 10 pixels 5 pixels . , : >>> def spiral(x,y,arc,times,inner,increment): up() goto(x,y) down() for i in range(times): circle(inner+increment*i,arc)

  • 78

    spiral (x,y) times , arc . inner increment. .. 20 : >>> reset() >>> speed('fastest') >>> for i in range(20): color(random(),random(),random()) x=randint(-200,200) y=randint(-200,200) arc=randint(-360,360) spiral(x,y,arc,randint(10,100),randint(-30,30),randint(-10,10))

    . , : >>> reset() >>> for i in range(8): circle(100,22.5) circle(10) circle(100,22.5) circle(-10) ... :

  • 79

    : >>> reset() >>> for i in range(200): circle(30+i,15) circle(5) circle(30+i,15) circle(-5)

    , : >>> reset() >>> speed('fastest') >>> color('red') >>> for i in range(36): circle(50) left(10)

  • 80

    >>> color('black') >>> for i in range(18): circle(100) left(20)

    , : >>> def daisy(inner, outer, number, colour): angle=360.0/number radius=-(outer-inner)/2. color(colour) up() goto(0,0) right(90) forward(inner) left(90) for i in range(number): down() circle(radius) up() circle(inner,angle) : inner outer number colour. , .: >>> reset() >>> speed('fastest') >>> daisy(30,100,36,'red') >>> daisy(50,150,20,'blue') >>> daisy(70,200,50,'green')

  • 81

    6.7 , . . . #1 Project Euler (http://projecteuler.net).

    10 Python. , (copy-paste site) 11 .

    10 . 11 , site.

    http://projecteuler.net)

  • 82

    #2 turtle Python . , , (, , ). (screen capture) .

  • 83

    7: Python

    7.1 Python, (lists) (dictionaries) . (methods) .

    7.2 . , : >>> a=[14,17,11,22,19,11,33] >>> print a [14, 17, 11, 22, 19, 11, 33] : . . , . , , , . .: >>> q=[34, 'hello', -12.17, ['a', 'b']] >>> print q [34, 'hello', -12.17, ['a', 'b']] q : 34, hello, 12.17 ['a', 'b']. len(): >>> print len(a), len(q) 7 4 :

  • 84

    >>> print a[0], q[1] 14 hello 1 0, 2 1, ... , : 1 , 2 , ... ..: >>> print a[-2], q[-1] 11 ['a', 'b'] () : >>> print a, a[2:5] [14, 17, 11, 22, 19, 11, 33] [11, 22, 19] () +: >>> a=[1,2] >>> b=[34,56] >>> c=a+b >>> print c [1, 2, 34, 56] * ( 3): >>> d=a*3 >>> print d [1, 2, 1, 2, 1, 2] , , .

    7.3 , (module) . , , ( Beep() ) winsound Python Windows: >>> from winsound import Beep , import , Beep. B . Python ( ) . winsound

  • 85

    Windows Windows. , . : >>> Beep(1200,500) . 1200Hz (500msec). Beep [37, 32767] Hertz . milliseconds. Beep, . .: >>> from random import * >>> for i in range(20): freq=randint(1200,20000) print freq,"Hz" Beep(freq,200) 10594 Hz 4543 Hz 15006 Hz ... 14551 Hz 6327 Hz 9590 Hz . . , . : 440 Hz. . . (880 Hz) . (220 Hz) . : >>> List=[220,440,880] >>> for freq in List:

    print freq,"Hz" Beep(freq,500)

    3 2 (880-440=440 Hz) 1 2 (440-220=220 Hz), .

  • 86

    . , : >>> from math import log >>> print log(List[2])-log(List[1]) 0.69314718056 >>> print log(List[1])-log(List[0]) 0.69314718056 , , . Beep(): >>> La_List=[] >>> F=440 >>> while F> print La_List [440, 880, 1760, 3520, 7040, 14080, 28160] 440 Hz . : >>> F=440 >>> while F>=37: if not(F in La_List): La_List=[F]+La_List F=F/2 440, if. F , . , , F . : >>> print La_List [55, 110, 220, 440, 880, 1760, 3520, 7040, 14080, 28160] Beep(). : >>> def play_list(L,Duration): for freq in L:

  • 87

    print freq, "Hz" Beep(freq,Duration) , L Duration Duration msec . : >>> play_list(La_List,500) 55 Hz 110 Hz 220 Hz 440 Hz 880 Hz 1760 Hz 3520 Hz 7040 Hz 14080 Hz 28160 Hz . 2022000 Hz. 22000 Hz . 20 Hz . , . 11 , 12 . , = 2(1/12) = 1.05946309436. , : = 440 Hz, , (# = *440). , ( = **440) ... 12 . 12*440=2*440 = 2(1/12). Python: >>> A=440. >>> d=2.**(1./12.) >>> print d 1.05946309436 >>> L=[A] >>> for i in range(12): A=A*d L=L+[A] >>> print L [440.0, 466.1637615180899, 493.8833012561241, 523.2511306011974, 554.3652619537443, 587.3295358348153, 622.253967444162, 659.2551138257401, 698.456462866008, 739.988845423269, 783.9908719634989, 830.6093951598906, 880.0000000000003]

  • 88

    L 440 Hz 880 Hz , ( 880 Hz). Beep(). Beep() . . int(): >>> print int(2.4), int(2.5), int(2.6) 2 2 2 int() . , 2.4 2.5 2.6 2. , 0.5 . .: >>> print int(0.5+2.4), int(0.5+2.5), int(0.5+2.6) 2 3 3 L : >>> K=[] >>> for f in L: K=K+[int(0.5+f)] >>> print K [440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880] . Python . .: >>> M=[int(0.5+x) for x in L] int(0.5+x), x L. : >>> print M [440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880] : >>> play_list(M,300) 440 Hz 466 Hz 494 Hz 523 Hz 554 Hz 587 Hz 622 Hz 659 Hz 698 Hz 740 Hz 784 Hz 831 Hz

  • 89

    880 Hz . , : >>> M2=[2*x for x in M] >>> print M2 [880, 932, 988, 1046, 1108, 1174, 1244, 1318, 1396, 1480, 1568, 1662, 1760] >>> M0=[x/2 for x in M] >>> print M0 [220, 233, 247, 261, 277, 293, 311, 329, 349, 370, 392, 415, 440] 3 : >>> Mall=M0+M+M2 >>> print Mall [220, 233, 247, 261, 277, 293, 311, 329, 349, 370, 392, 415, 440, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 880, 932, 988, 1046, 1108, 1174, 1244, 1318, 1396, 1480, 1568, 1662, 1760] , . 440 880 2 Mall, 13 ( ). , , 7.2: >>> Mall=M0[:12]+M+M2[1:] >>> print Mall [220, 233, 247, 261, 277, 293, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1046, 1108, 1174, 1244, 1318, 1396, 1480, 1568, 1662, 1760] To M0[:12] M0[0:12] 12 M0. , M2[1:] M2 . : >>> for i in range(20): note=randint(0,36) freq=Mall[note] print "Note=", note, " Freq=", freq, "Hz" Beep(freq,200) Note= 13 Freq= 466 Hz Note= 15 Freq= 523 Hz Note= 21 Freq= 740 Hz ... Note= 9 Freq= 370 Hz Note= 24 Freq= 880 Hz Note= 31 Freq= 1318 Hz

  • 90

    Mall Beep(). , . .

    7.4 (Dictionary) (key). : >>> a={'La':440, 'La#':466, 'Si':494, 'Do':523} 4 , { }. : . :. a. : >>> print a['La#'] 466 >>> print a['Si'], a['Do'] 494 523 , . . , , : >>> print a {'Do': 523, 'Si': 494, 'La#': 466, 'La': 440} : >>> a['Sol']=392 >>> a['Sol#']=415 >>> print a {'Do': 523, 'La': 440, 'Sol#': 415, 'La#': 466, 'Si': 494, 'Sol': 392} , : >>> a['Do']=261 >>> print a {'Do': 261, 'La': 440, 'Sol#': 415, 'La#': 466, 'Si': 494, 'Sol': 392} del: >>> del a['Do']

  • 91

    >>> print a {'La': 440, 'Sol#': 415, 'La#': 466, 'Si': 494, 'Sol': 392} , : >>> a={'Do':261, 'Do#':277, 'Re':293, 'Re#':311, 'Mi':329, 'Fa':349, 'Fa#':370, 'Sol':392, 'Sol#':415, 'La':440, 'La#':466, 'Si':494} >>> print a {'Do': 261, 'Re': 293, 'Do#': 277, 'La': 440, 'Mi': 329, 'Sol': 392, 'Sol#': 415, 'Fa': 349, 'Si': 494, 'Fa#': 370, 'Re#': 311, 'La#': 466} ... . . : >>> p=['Do','Mi','Sol','La','La#','La','Sol','Mi'] : >>> f=[a[x] for x in p] >>> print f [261, 329, 392, 440, 466, 440, 392, 329] : >>> play_list(f,250) 261 Hz 329 Hz 392 Hz 440 Hz 466 Hz 440 Hz 392 Hz 329 Hz ... : >>> play_list(f*4,250) 261 Hz 329 Hz ... 392 Hz 329 Hz . 5 : >>> f5=[int(0.5+x*d**5) for x in f] >>> print f5 [348, 439, 523, 587, 622, 587, 523, 439] >>> play_list(f5,250) ... 7 : >>> f7=[int(0.5+x*d**7) for x in f] >>> print f7

  • 92

    [391, 493, 587, 659, 698, 659, 587, 493] >>> play_list(f7,250) : >>> play_list(f*2+f5+f+f7+f5+f*2,250) 261 Hz 329 Hz 392 Hz ... 440 Hz 392 Hz 329 Hz , ( ). : >>> p1=['Do','Re','Mi','Fa','Sol','La','Si'] >>> f1=[a[x] for x in p1] >>> for i in range(20): Beep(f1[randint(0,6)],250) >>> p2=['Do','Mi','Sol','La','La#'] >>> f2=[a[x] for x in p2] >>> for i in range(20): Beep(f2[randint(0,4)],200) >>> p3=['Do#','Re#','Fa#','Sol#','La#'] >>> f3=[a[x] for x in p3] >>> for i in range(20): Beep(f3[randint(0,4)],200) . , : >>> def rand_music(KLIM,OCT,Notes,Speed): f=[a[x] for x in KLIM] q=f while OCT>1: OCT=OCT-1 r=[x*2 for x in f] q=q+r L=len(q)-1 for i in range(Notes): note=randint(0,L) freq=q[note] Beep(freq,Speed) KLIM , OCT , Notes Speed . : >>> rand_music(['Do#','Re#','Fa#','Sol#','La#'],2,20,250)

  • 93

    . . . msec, 2 : 1/2, 1/4, 1/8, 1/16, 1/32 1/64. 64, . 30*64=1920 msec. (). : >>> a['P']=28000 : >>> music=[['La',16],['Sol',16],['La',2],['P',8],['Sol',16], ['Fa',16],['Mi',16],['Re',16],['Do#',4],['Re',2]] . , . : >>> def play_musiclist(L): for x in L: [note,time]=x freq=a[note] duration=1920/time print "Note=", note, "(", freq, "Hz) Dur= 1/",

    print time, "(", duration, "msec)" Beep(freq,duration) >>> play_musiclist(music) Note= La ( 440 Hz) Dur= 1/ 16 ( 120 msec) Note= Sol ( 392 Hz) Dur= 1/ 16 ( 120 msec) Note= La ( 440 Hz) Dur= 1/ 2 ( 960 msec) Note= P ( 28000 Hz) Dur= 1/ 8 ( 240 msec) Note= Sol ( 392 Hz) Dur= 1/ 16 ( 120 msec) Note= Fa ( 349 Hz) Dur= 1/ 16 ( 120 msec) Note= Mi ( 329 Hz) Dur= 1/ 16 ( 120 msec) Note= Re ( 293 Hz) Dur= 1/ 16 ( 120 msec) Note= Do# ( 277 Hz) Dur= 1/ 4 ( 480 msec) Note= Re ( 293 Hz) Dur= 1/ 2 ( 960 msec)

    7.5 , on-line help Python ( web-site) (modules) . :

  • 94

    def. . . sin() Beep(). . + : >>> print [1,2]+[3,4] [1, 2, 3, 4] : >>> def connect(L1,L2): return L1+L2 connect(): >>> print connect([1,2],[3,4]) [1, 2, 3, 4] : >>> L=['a','b','a','c','a','a','d'] >>> print L.count('a') 4 count() . . , . dot notation ( ). , : >>> def my_count(A,x): return A.count(x) : >>> print my_count(L,'a') 4 . , .

  • 95

    Python. upper() (strings) . >>> v='' >>> print v.upper() keys() values() . .: >>> a={'Do':261, 'Do#':277, 'Re':293, 'Re#':311, 'Mi':329, 'Fa':349, 'Fa#':370, 'Sol':392, 'Sol#':415, 'La':440, 'La#':466, 'Si':494} >>> print a.keys() ['Do', 'Re', 'Do#', 'La', 'Mi', 'Sol', 'Sol#', 'Fa', 'Si', 'Fa#', 'Re#', 'La#'] >>> print a.values() [261, 293, 277, 440, 329, 392, 415, 349, 494, 370, 311, 466] ( ) . reverse() . . : >>> L=[1,2,3,4] >>> L.reverse() >>> print L [4, 3, 2, 1] >>> L.reverse() >>> print L [1, 2, 3, 4] ( ): >>> L=[1,2,3,4] >>> M=L >>> print L,M [1, 2, 3, 4] [1, 2, 3, 4] >>> M.reverse() >>> print L,M [4, 3, 2, 1] [4, 3, 2, 1] M. ; Python =. L=[1,2,3,4] Python [1,2,3,4] L. M=L L, L M. . : >>> L=[1,2,3,4] >>> M=[1,2,3,4]

  • 96

    ... : >>> L=[1,2,3,4] >>> M=L[:] L[:] L[0:len(L)] . Python L. . : >>> L=[1,2,3,4] >>> M=L+[] ... L ([]) M. , reverse() : >>> print L,M [1, 2, 3, 4] [1, 2, 3, 4] >>> M.reverse() >>> print L,M [1, 2, 3, 4] [4, 3, 2, 1] Python , . : >>> L=5 >>> M=L >>> print L,M 5 5 5 . 5 L M. . : >>> M=M+1 5 6. 6, M . L 5 . : >>> print L,M 5 6 , . () . .

  • 97

    , . , , : >>> def rev(L): M=L+[] M.reverse() return M >>> J=[1,2,3,4] >>> print J, rev(J), J [1, 2, 3, 4] [4, 3, 2, 1] [1, 2, 3, 4] reverse(): >>> def rev2(L): if L==[]: return L H=L[0] T=L[1:] RT=rev2(T) return RT+[H] >>> J=[1,2,3,4] >>> print J, rev2(J), J [1, 2, 3, 4] [4, 3, 2, 1] [1, 2, 3, 4] rev2() . , (L[0]), (L[1:]) L[0] . rev2() ! M=M+1 . . () , () , () . (), () () rev2() . .

    7.6 , . . #1

  • 98

    20-22000 Hz . . #2 : mysterysong=[['Do',3,'1'],['Fa',2,'2'],['Fa',1,'3'],['Fa',4,'4'],['Sol',3,'5'],['La',2,'6'],['La',1,'7'],['La',4,'8'],['La',1,'9'],['Sol',1,'10'],['La',1,'11'],['La#',2,'12'],['Mi',2,'13'],['Sol',2,'14'],['Fa',4,'15']] ( ) 1 , 2 3 ( ) . 240 msec. :

    1 1x240 = 240 msec 2 2x240 = 480 msec 3 3x240 = 720 msec,

    Python dictionary , winsound mysterysong. 1-15 . #3 Python 5-10 .

  • 99

    8: Python

    8.1 Python, e-mails, .

    8.2 (.. 2008). , , . Python calendar , on-line Help ( F1 Library Reference > Data Types > calendar):

    , calendar() . : >>> from calendar import * >>> print calendar(2008) ... :

  • 100

    calendar() (string). print. , .: >>> a=calendar(2008) >>> print a e-mail . ( ) Python. , . . , PDF , , ,

  • 101

    (. , ). bytes (fonts) , ( ) , , . , ( ) . (.txt), (text editors). byte ASCII . Python open(), : >>> my_file=open("my_first_text_file.txt","w") open() Python . : v (my_first_text_file.txt)

    . v

    (w , r ). my_file Python . , . open() :

    : v ()

    Python.

  • 102

    path open().

    v 0 bytes. , .

    , write(). , write() (. , , ), . , : >>> my_file.write(" Python:\n\n") >>> a=calendar(2008) >>> my_file.write(a) >>> my_file.write("\n\nPython: ...\n... .\n(Read the Manual!!!)") \n ( Enter/Return). , write() print, : >>> print >>my_file, a write() \n strings. , print : >>> print >>my_file, " Python:" >>> print >>my_file, "" >>> a=calendar(2008) >>> print >>my_file, a >>> print >>my_file, "" >>> print >>my_file, "Python: ... " >>> print >>my_file, "... . " >>> print >>my_file, "Read the Manual!!!)" , close() , RAM . >>> my_file.close() editor:

  • 103

    , Python, : >>> my_file=open("my_first_text_file.txt","r") open() "r", . read() contents, : >>> contents=my_file.read() >>> print contents , . . , close(): >>> my_file.close()

    8.3 , : >>> file1=open("my_first_text_file.txt","r") >>> file2=open("my_second_text_file.txt","w") >>> contents=file1.read() >>> file2.write(contents)

  • 104

    >>> file2.close() >>> file1.close() os, : >>> from os import * os , getcwd() () : >>> print getcwd() C:\PF\Python25 ... listdir() : >>> d=getcwd() >>> print listdir(d) ['DLLs', 'Doc', 'graphics1.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'my_first_text_file.txt', 'my_second_text_file.txt', 'NEWS.txt', 'Pygame DocEx', 'python.exe', 'pythonw.exe', 'README.txt', 'tcl', 'test1.py', 'Tools', 'triwnymo.py', 'w9xpopen.exe'] ... os system(), Command Line (DOS prompt). system(). : : >>> system("copy my_first_text_file.txt my_third_text_file.txt") 0 >>> system("copy my_third_text_file.txt my_forth_text_file.txt") 0 : >>> system("ren my_forth_text_file.txt my_fourth_text_file.txt") 0 : >>> system("del my_third_text_file.txt") 0 0 .

    8.4 Python . , zipfile zlib . : >>> from zipfile import *

  • 105

    >>> from zlib import * >>> fz=ZipFile("AllFiles.zip","w",ZIP_DEFLATED) ZipFile() open(). "AllFiles.zip" ("w"). ZIP_DEFLATED . , . fz : >>> fz.write("my_first_text_file.txt") >>> fz.write("my_second_text_file.txt") >>> fz.write("my_fourth_text_file.txt") ... : >>> fz.close() AllFiles.zip :

    8.5 e-mail e-mail Python, e-mail. smtplib help Python. e-mail: >>> from smtplib import * >>> server=SMTP("patreas.upatras.gr") SMTP() server e-mail . , . . : >>> file=open("my_first_text_file.txt","r") >>> message=file.read() >>> file.close() ... , :

  • 106

    >>> message=""" e-mail : [email protected] : [email protected] Python smtplib.""" , . Python . sendmail() e-mail: >>> server.sendmail("[email protected]", "[email protected]", message) e-mail , . ( ). server, sendmail() : >>> server.set_debuglevel(1) sendmail() : send: 'ehlo think.wcl.ee.upatras.gr\r\n' reply: '250-patreas.upatras.gr patreas.upatras.gr Universty of Patras\r\n' reply: '250-PIPELINING\r\n' reply: '250-SIZE 10000000\r\n' reply: '250 8BITMIME\r\n' reply: retcode (250); Msg: patreas.upatras.gr patreas.upatras.gr Universty of Patras PIPELINING SIZE 10000000 8BITMIME send: 'mail FROM: size=400\r\n' reply: '250 ok\r\n' reply: retcode (250); Msg: ok send: 'rcpt TO:\r\n' reply: '250 ok\r\n' reply: retcode (250); Msg: ok send: 'data\r\n' reply: '354 Please start mail input.\r\n' reply: retcode (354); Msg: Please start mail input. data: (354, 'Please start mail input.') send: 'test\r\n.\r\n' reply: '250 Mail queued for delivery.\r\n' reply: retcode (250); Msg: Mail queued for delivery. data: (250, 'Mail queued for delivery.') {} , e-mail : >>> server.quit() send: 'quit\r\n' reply: '221 Closing connection. Good bye.\r\n'

    mailto:[email protected]:[email protected]:[email protected]:[email protected]:FROM:mailto:TO:

  • 107

    reply: retcode (221); Msg: Closing connection. Good bye.

    8.6 , . ( ) . #1 Python 21 ( 2001 2100) xxxx.txt, xxxx (. 2001.txt, 2002.txt ). , 100 ZIP 21century.zip .

    #2 3 + p , p :

    p = v1+v2 v1 = (6*v2+m4+m2) mod 7 v2 = (16+m19) mod 30 m2 = 2*( mod 4) m4 = 4*( mod 7) m19 = 19*( mod 19)

    (x mod y) x/y. Python 21 e-mail . e-mail :

    2001 15 . 2002 5 . 2003 27 . ...

    : , / . import libraryname, from libraryname import *. , , , ( dot notation) libraryname.function()

  • 108

    9: Python

    9.1 . , , .

    9.2 Python ; , , (. ) . ( ) (objects). (object oriented programming). . 12. (. 5, 8, Q, A, ), (, , , ) , ( ) . , , , (), . . (class) : >>> class card(): pass def. class, , -

    12 . , .

  • 109

    . pass. Python, pass . . card . (): >>> c1=card() >>> c2=card() , . c1 c2 card(). card() . . dot notation () : >>> c1.value='5' >>> c1.symbol='d' >>> c1.color='R' >>> c1.fig=False >>> c2.value='K' >>> c2.symbol='c' >>> c2.color='B' >>> c2.fig=True c1 5 (d=diamond), (R=red) (c1.fig=False), c2 (K=King) (c=club), (B=black) (c2.fig=True). : >>> print c1.value, c1.symbol, c2.value, c2.symbol 5 d K c . . , . . ; (color, fig) (symbol, value); : >>> class card(): def __init__(self,val,sym): self.value=val self.symbol=sym if self.symbol in "sc": self.color='B' else: self.color='R' if self.value in "JQK": self.fig=True else: self.fig=False

  • 110

    >>> c1=card('5','d') >>> print c1.value, c1.symbol, c1.color, c1.fig 5 d R False pass . : 4 . . __init__ ( underscores, init, underscores) 13, . __init__ . (. c1 c2 ), self. self __init__. , val sym. , c1=card(5,d), self c1, val 5 sym d. __init__ c1.value c1.symbol. , if else c1.color c1.fig. : c1=card(5,d) val sym card, __init__, card ! ( ). . : >>> print c1.value, c1.symbol, c1.color, c1.fig 5 d R False ... , : >>> print c1 , /, /. (. , ) Python . __str__. __str__ __init__: >>> class card(): 13 __str__, __del__, __new__, __cmp__, . on-line help .

  • 111

    def __init__(self,val,sym): self.value=val self.symbol=sym if self.symbol in "sc": self.color='B' else: self.color='R' if self.value in "JQK": self.fig=True else: self.fig=False def __str__(self): return self.value+self.symbol __str__ (self). (string). self.value+self.symbol. : >>> c1=card('5','d') >>> print c1 5d , dot notation. : >>> class card(): def __init__(self,val,sym): self.value=val self.symbol=sym if self.symbol in "sc": self.color='B' else: self.color='R' if self.value in "JQK": self.fig=True else: self.fig=False def __str__(self): return self.value+self.symbol def detailed_info(self): print '=',self.value, ' =',self.symbol print '=',self.color, ' =',self.fig >>> c1=card('5','d') >>> c1.detailed_info() = 5 = d = R = False 52 , . . . . - : import random class deck(): values="A23456789TJQK" # symbols="shcd" # content=[] # pile=[] # def __init__(self): self.content=[]

  • 112

    self.pile=[] for s in self.symbols: for v in self.values: c=card(v,s) self.content.append(c) def __str__(self): s="" cntr=0 for i in self.content: s=s+str(i)+" " cntr=cntr+1 if cntr%13==0: s=s+'\n' if s[-1]'\n': s=s+'\n'

    s=s+str(len(self.content))+"-"+str(len(self.pile)) return s def shuffle(self): random.shuffle(self.content) def draw(self): if len(self.content)>> d=deck() for , , c=card(v,s) content self.content.append(c). for

  • 113

    content. , d : >>> print d As 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ac 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd 52-0 __str__. s self.content, 13 , content pile. s=s+str(i)+ . To i , str(i) string print i, __str__ card. , . shuffle, module random ( import random ). . self.shuffle() random.shuffle(). dot notation. : >>> d.shuffle() ... : >>> print d 8d 3c 2c Ts Ks 9c Jc 5h 2h Kd 3h Td 5c 5d 7s 8h 9d Ad 5s Tc 7c 4c 6c 7d Qh Qs 3d Ah Ac Js Qc 4s 3s 6d Jd As Kh 4h 9h Th 9s Jh 6h 4d 2s 7h 2d Qd Kc 8c 8s 6s 52-0 ; . draw() . , draw() (self.content[0]) , , empty. content pile. >>> h=d.draw() >>> print h 8d >>> print d 3c 2c Ts Ks 9c Jc 5h 2h Kd 3h Td 5c 5d 7s 8h 9d Ad 5s Tc 7c 4c 6c 7d Qh Qs 3d Ah Ac Js Qc 4s 3s 6d Jd As Kh 4h 9h Th 9s Jh 6h 4d 2s 7h 2d Qd Kc 8c 8s 6s 51-1 , collect() . pile content pile. . >>> for i in range(10): h=d.draw()

  • 114

    >>> print d Td 5c 5d 7s 8h 9d Ad 5s Tc 7c 4c 6c 7d Qh Qs 3d Ah Ac Js Qc 4s 3s 6d Jd As Kh 4h 9h Th 9s Jh 6h 4d 2s 7h 2d Qd Kc 8c 8s 6s 41-11 >>> d.collect() >>> print d Td 5c 5d 7s 8h 9d Ad 5s Tc 7c 4c 6c 7d Qh Qs 3d Ah Ac Js Qc 4s 3s 6d Jd As Kh 4h 9h Th 9s Jh 6h 4d 2s 7h 2d Qd Kc 8c 8s 6s 8d 3c 2c Ts Ks 9c Jc 5h 2h Kd 3h 52-0 . collect() , . , . . import module14 . , card(), deck() import random . playing_cards.py Python, from playing_cards import *.

    9.3 ( ) :

    1. ( ) .

    2. 7 1 .

    3. . 4. ,

    , (. 5 , 5 ). ( ) .

    5. . 6.

    , .

    14 modules . . text editor random.py Lib Python.

  • 115

    module playing_cards , 5 . (global variables) : d , , table, computer_hand, human_hand, , , -, . -, . , : from playing_cards import * 15 card deck. : def computer_plays(): global d, table, computer_hand target_val=table[-1].value target_sym=table[-1].symbol for c in computer_hand: if c.value==target_val or c.symbol==target_sym: print " / ",c computer_hand.remove(c) table.append(c) if len(computer_hand)

  • 116

    , target_val target_sym . , for - computer_hand target_val target_sym computer_hand remove() table append(). / string computer wins. continue. for , d.draw() . if count. else / , computer_hand return computer_plays(). . computer_plays(). , (recursive). 16 computer_hand. computer_plays() . continue, computer wins, count, . , , . . (. ) : () , () : def human_plays(): global d, table, human_hand print print " ", len(d.content), "" print " / ", len(computer_hand), "" print " ", len(table), "" t=table[-1] print " ",t print " ",len(human_hand) HHS=[str(x) for x in human_hand] print HHS print 16 . , , .

  • 117

    print " " print " ENTER " sel=raw_input() if sel=="": new_card=d.draw() if new_card=="empty": return "count" else: human_hand.append(new_card) return human_plays() else: if not(sel in HHS): print sel, ";;; ." return human_plays() target_val=t.value target_sym=t.symbol if sel[0]target_val and sel[1]target_sym: print " ",sel return human_plays() print " ",sel ind=HHS.index(sel) selc=human_hand[ind] human_hand.remove(selc) table.append(selc) if len(human_hand)

  • 118

    (+1 , -1 /) ( ) human_plays() computer_plays(). def next_turn(who_plays): if who_plays==1: print print "------------------- -------------------" return human_plays() else: print print "-------------------- / ---------------------" print return computer_plays() , evaluate() human_wins, computer_wins, count . count . def evaluate(result): global computer_hand, human_hand if result=="count": ch=len(computer_hand) hh=len(human_hand) print " , / ",ch, " ", hh if ch>hh: result="human wins" if ch

  • 119

    human_hand.append(d.draw()) computer_hand.append(d.draw()) print " ...", if random.random()

  • 120

    9.4 2, 3 . / , . , . , , . playing_cards.py :

  • 121

    class pack(deck): def __init__(self,number_of_decks=2): d=deck() self.content=d.content*number_of_decks . . Python : >>> p=pack(3) >>> print p As 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ac 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd As 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ac 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd As 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ac 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd 156-0 p pack 3 , pack(3). , 3 156 (=3x52) . >>> p.shuffle() >>> print p 2c 5s 3c Qd 8h Jd Js 8d Ac 7s 9c 5s 9d Ad 5h 4c 6d 9h Jc 5h 4d 8c Qs 6d 4h Qh 4h Ks As Td 5c Jh 7s 6h 2s 3s Kh Kh 3d Qc 8d 9c 9d Kh 9s 3d 9h 6h 7s Kc 5h Ah Tc 6c 8c 9h Tc Kd Kc 2d 7c Qc 4c 2d 9d 2h Jh 5c 3h 4h Ac Ah 2s Ks Th Th 7d 7d Kd 6s Kc As Jc Qs 6s 6d 2d 4s 7c 5d 2h 5d 6h 3c Tc 2s 8d 9c 5d 2c Js 6c Qd 5c 4s Kd 3h Jh Td 2c 3s 8h 3d 7h 3h Qs Ts 4s 4c Ad 6c Ts 5s Th 7h 8s 7d 4d Ah Js Qh Qc 9s Ad Jd 3c 7h Td Ks 8c 8s Qd 3s 9s 7c 8h 2h 4d As Jc 6s Ac Ts Qh 8s Jd 156-0 . pack __init__. shuffle(), __str__. ; ; class pack(deck): . . deck. Python pack deck. deck ( ).

  • 122

    ( __init__) . pack strings values symbols, content pile __str__, shuffle(), draw() collect() deck. __init__ . : __init__ self, number_of_decks=2 . =2 . (default value). e=pack(2) e=pack() 2 . , __init__ d, d.content number_of_decks self.content. content pack content . . card_game.py: ... # print " ( 2)" print "====================================" print n=input(" ; ") print ", ",n,"" print # d=pack(n) ... input() d deck() pack(n). . , , (. ) n . . . . 141 . 141+1+7+7=156=3x52, .

  • 123

    9.5 playing_cards.py card_game.py , . . #1 : 1. (A), ,

    . ( ).

    2. (K) . 3. (Q) 2

    . #2

  • 124

    ( ). . (: computer_player computer_plays(). . , / .)

  • 125

    10:

    . . . . (, ). , . () () . 13-14. .

  • 126

    3.

    6 4 5 , . ( 6 ). Python. , , , . , . . . google groups. (http://groups.google.com/). . :

    : ) , ) ) .

    (10 ). ( ). ( ,

    , , , .).

    ( ). (.. google groups)

    .

    , . . ( ).

    http://groups.google.com/

  • 127

    . (, ) .

  • 129

    4.

    () 120 , . : ( 2, 56 ) , ( 1, 50 ) ( , , ). .

    .

  • 130

    ( ). . UNIX (Amalia) UNIX . , . . . (Windows Unix) . , .

    ( ). , . Shift, Ctrl, Enter, Alt. , .

    password Windows login name , eceXXXX, XXXX (4) . login name . password . . login name password. , "123456" . password . password 123456, . password, Ctrl+Alt+Del Change Password. , password (.. 123456) password (2) . password 6 2 1 (.. -, $, #, &).

  • 131

    Unix, passwords. , . , / , & , , . (5) (1) . ( , Amalia). .

    UNIX UNIX Windows. password o UNIX. Shortcut to Term.exe 3.1.2. , : (Start) > (Program Files) > QvtNet > QVT-Term. telnet : (Start) > (Run) > "telnet". ( ) Terminal Unix amalia. Login: Unix. Unix.

    3.1.2 UNIX.

    password UNIX Unix , .

  • 132

    . , , . "" . (Hewlett Packard) . passwd , / , & , , . (5) (1) . ( , Amalia).

    ( http://kypes.ece.upatras.gr/kypesdocs/UserPolicy.pdf) 1.

    : .

    : . .

    : .

    : . 2.

    / () (.. ).

    . .

    (password) . .

    (password) , , .

    .

    .

    .

    http://kypes.ece.upatras.gr/kypesdocs/UserPolicy.pdf)

  • 133

    3.

    , , , , , . , .

    . . -

    ( ). ,

    , , .

    . 4.

    (.. - Denial of Service attacks Port scans).

    (malware). . . . ,

    . (E - Mail spam).

    , ,

    , (.. , .) , , . , . , , , , . [email protected].

    mailto:[email protected]

  • 134

    5.

    ? : : 1) link " " . 2) . browse link " ".

    3) , . .

  • 135

    4) link " " . .

    5) / . . . / . http://rheya.hci.ee.upatras.gr/moodle/mod/forum/post.php?delete=34

    login : : 1) . Caps Lock Case Sensitive ( ) 2) . email .

    http://rheya.hci.ee.upatras.gr/moodle/mod/forum/post.php?delete=34

  • 136

    : ( pdf) ( ) save a copy.