Εργαστήριο 5 - Python -...

Click here to load reader

  • date post

    28-Jan-2017
  • Category

    Documents

  • view

    227
  • download

    3

Embed Size (px)

Transcript of Εργαστήριο 5 - Python -...

  • 5

    : , , ,

  • 5: Python

    5.1

    Python,

    , ,

    .

    5.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

  • 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 ().

    5.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)

    [-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

  • 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 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)

    .

    5.4

    Python .

    (turtle graphics). Logo,

    1980

    .

    :

    >>> from turtle import *

    , 1 :

    >>> reset()

    Turtle Graphics:

    ( cursor,

    ) .

    1 " "

    .

    .

  • reset

    . (0,0).

    goto. .:

    >>> goto(300,0)

    300 pixels ( )

    . goto (

    ). goto

    :

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

    :

    >>> speed(fastest)

  • ... .

    , up, :

    >>> up() >>> goto(100,100)

    :

    >>> 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) >>> 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 help2

    Python:

    .

    .

    5.5

    5.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

    2 on-line-help, , .

    forward(100) fd(100). "turtle", help . import .

  • .

    :

    >>> 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() (-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))

    cx, cy x y . 200

    pixels 2*36=226 pixels

    .

  • 5.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): 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, .

    . 5.4 .

    r:

    >>> def triangle(r): forward(r) left(120) forward(r) left(120) forward(r) 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) 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 . .

    :

    >>> 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)

    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)

    :

    >>> 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)

    >>> color('black') >>> for i in range(18):

    circle(100) left(20)

    ,

    :

    >>>