Академический Документы
Профессиональный Документы
Культура Документы
432
22.1
27
27
. .
Python: / . . . : , 2010. 126 .
: . ( ALT Linux).
ISBN 978-5-905167-02-7
-
Python Python
(IDE) Geany Eric.
, ( ), Python, ,
IDE Geany IDE Eric,
Python. ,
(32 .).
Python turtle
- Tkinter.
: http://books.altlinux.ru/PythonSchool/
, , .
004.432
22.1
:
: (495) 662-3883. E-mail: zakaz@altlinux.ru
Internet-: http://shop.altlinux.ru
, , GNU
FDL. , :
ALT Linux. :
Python. . . ALT Linux ALT Linux. Linux
. .
ISBN 978-5-905167-02-7
. ., 2010
ALT Linux 2010
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1. Python
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
Python . . . . . . . . . .
Python . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Python
. . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4
4
5
22
26
31
32
34
2.1
2.2
2.3
2.4
2.5
2. Python
. . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
() . . . . . . . . .
( )
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
35
35
40
46
59
65
3. Python
3.1
. . . . . . . . . . . . . .
3.2
.
Tkinter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
4.
4.1
4.2
4.3
4.4
4.5
4.6
4.7
. Python? . . . . . . . . . . . . . .
. . . . .
() . . .
IDE Geany . . . . . . . . . . . . . .
IDE Eric . . . . . . . . . . . . . . . .
Tk Tkinter
. . . . . . . . .
70
70
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
92
92
93
94
98
111
123
124
1
Python
1.1
, ,
.
.
Python , Python-.
Python
Python (. ).
Python - . , .. .
Python ,
,
.
, ,
( ). ( )
.
.
:
import _
:
from _ import 1 , . . . N
Python =,
==.
1.2.
1.2
1.2.1
1.2.1.1
Python ( int),
( long), ( oat) ( ). .
Python
int () oat (). , int (12.6) 12,
oat (12) 12.0 ( ).
1.1.
1.1:
x +y
( x y)
x y
( x y)
x/y
x y (). ! x y
, !
.
:
100/8 12, 100/8.0 12.5
x y
( x y)
1. Python
1.1:
x//y
( ).
, ,
.
:
100//8 12
101.8//12.5 8.0
(
101.8/12.5 8.1440000000000001)
x%y
xy
x y
:
10%4 2
(x y).
.
:
23 8
2.3(3.5) 0.05419417057580235
, Python abs()
( , abs(3) 3), pow() (
, pow(2,3) 8), divmod() ( , divmod(17,5) (3,2)) round() (, round(100.0/6) 17.0).
, ,
.
(), , . math.
1.2.1.2
1.2.
1.2:
>
(, , a > b)
<
(,, a < b)
==
(, a b)
!=
(, a b)
not x
( x )
x and y
().
x and y, ,
x y.
x or y
().
x or y, ,
.
x in A
x
() A (. ).
a <x<b
1.2.2
Python ( ) ( ). () (, ),
.
, .
, , . ()
(.. ).
Python () ,
, ( ).
.
1. Python
1.2.2.1
( ,
) .
. Python
( ). () ,
. Python ,
.
( )
Python . (, )
Python.
str (). , str (123) 123.
-, int () ( int ( 123) 123), oat () ( oat ( 12.34 ) 12.34).
( ) ord() (, ord( s ) 115). ,
chr() ( chr(100) d).
1.3:
len (s)
s1 + s2
. s1
s2, , , +
s n ( n s)
s[ i ]
n- s, , 2
s i, 0 ( 0). . i < 0, ( 0,
1).
:
s=
s [2]
s[2]
1.2.
1.3:
s [ i : j :k]
, s
i j k ( i
,
j ). k ( s [ i : j ]), (
s [ i : j :1] ).
:
s=derevo
s [3:5] ev
s [1:5:2] ee
min(s)
max(s)
() ( )
:
s=derevo
min(s) d
()
:
s=derevo
max(s) v
, Python, (.. ,
). .
, , s1.
1.4:
s1,
n .
. n len(s1),
.
:
s1=ZoomZoom
s1. center(15) ZoomZoom
10
1. Python
1.4:
s1 ( )
n . n < len(s1), .
:
s1=ZoomZoom
s1. ljust (15) ZoomZoom
s1.count(s [, i , j ])
s1. nd (s [, i , j ])
s1 ( ) n
. n < len(s1), .
:
s1=ZoomZoom
s1. rjust (15) ZoomZoom
s
s1. . i j
( , ).
:
s1=abrakadabra
s1.count(ab) 2
s1.count(ab ,1) 1
s1.count(ab,1,3) 0,
s1[1:3] brakada
( ) s s1. . i j ( ).
:
s1=abrakadabra
s1. nd ( br ) 1
1.2.
1.4:
s1. rnd (s [, i , j ])
( )
s s1.
. i j
(
).
:
s1=abrakadabra
s1. rnd ( br ) 8
s1. strip ()
s1. lstrip ()
s1. rstrip ()
, ( - ).
:
s1=breKeKeKeKs
s2=s1. strip ()
s2 breKeKeKeKs
, ( - ).
:
s1=breKeKeKeKs
s2=s1. lstrip ()
s2 breKeKeKeKs
, ( - ).
:
s1=breKeKeKeKs
s2=s1. rstrip ()
s2 breKeKeKeKs
, () s2
s3. n ( ).
:
s1=breKeKeKeKs
ss=s1.replace( Ke, XoXo,2)
ss breXoXoXoXoKeKs
11
12
1. Python
1.4:
s1. capitalize ()
, (),
().
:
s1=breKeKeKeKs
s2=s1. capitalize ()
s2 Brekekekeks
s1.swapcase()
s1.upper()
s1. lower ()
1.2.2.2
, .
:
s1=breKeKeKeKs
s2=s1.swapcase()
s2 BREkEkEkEkS
, ().
:
s1=breKeKeKeKs
s2=s1.upper()
s2 BREKEKEKEKS
, ().
:
s1=breKeKeKeKs
s2=s1.lower()
s2 brekekekeks
Python ,
(, ,
).
.
,
t =(12 , b , 3 4 . 6 , d e r e v o )
1.2.
13
Python
- :
t =(x , s1 , y , s 2 ) =(12 , b , 3 4 . 6 , d e r e v o )
, .. t [0] x, t [3] ,
, s2.
.
s1 t [1] .
, Python
.
( t=()),
(, t=(domik,)).
.
Python, ,
divmod() .
-
, , ,
. ,
.
, .
1.5:
len (t)
( ) t
t1 + t2
. ,
t1
t2.
:
t1=(1,2,3)
t2=(raz , dva)
t3=t1+t2
t3 (1, 2, 3, raz , dva)
t n ( n t)
n- t
:
t2=(raz , dva)
t23 (raz,dva, raz , dva , raz , dva)
14
1. Python
1.5:
t[ i ]
t i, 0 ( 0) i < 0,
(
0, 1).
:
t3= (1, 2, 3, raz , dva)
t3 [2] 3
t3[2] raz
t [ i : j :k]
min(t)
max(t)
, t
i j k (
i , j
). k ( t [ i : j ]), (
t [ i : j :1] ).
:
t3= (1, 2, 3, raz , dva)
t3 [1:4] (2, 3, raz)
() .
:
t3= (1, 2, 3, raz , dva)
min(t3) 1
() .
:
t3= (1, 2, 3, raz , dva)
max(t3) raz
, ,
, , , , ,
.
tuple (), :
s= amamam
t=t u p l e ( s )
t ( a , m , a , m , a , m )
1.2.
15
.
, ,
. ,
(, . ).
1.2.2.3
Python , (, ,
, ). , ,
.
, :
l s t =[12 , b , 3 4 . 6 , d e r e v o ]
lst (
list, .. ).
Python - :
l s t =[x , s1 , y , s 2 ] = [ 1 2 , b , 3 4 . 6 , d e r e v o ]
, .. lst [0] x, lst [3] .
, s2.
.
s1 lst [1] .
, ,
.
( : lst =[]), (, lst =[domik]).
Python, ,
.
1.6:
len ( lst )
lst. .
16
1. Python
1.6:
lst1 + lst2
. ,
lst1 lst2 .
:
lst1 =[1,2,3]
lst2 =[raz , dva ]
lst3 =lst1+lst2
lst3 [1, 2, 3, raz , dva ]
lst n ( n lst )
lst [ i ]
lst [ i : j :k]
min( lst )
n- lst .
.
:
lst2 =[raz , dva ]
lst2 3 [raz,dva , raz , dva , raz , dva ]
lst i, 0 ( 0)
i < 0, (
1).
:
lst3 = [1, 2, 3, raz , dva ]
lst3 [2] 3
lst3 [2] raz
, lst
i j k (
i , j
). k ( lst [ i : j ]), (
lst [ i : j :1] ).
:
lst3 = [1, 2, 3, raz , dva ]
lst3 [1:4] [2, 3, raz ]
() .
:
lst3 = [1, 2, 3, raz , dva ]
min(lst3 ) 1
1.2.
1.6:
max(lst)
() .
:
lst3 = [1, 2, 3, raz , dva ]
max(lst3) raz
lst [ i ]=x
del lst [ i ]
i x.
x , .
.
:
lst3 =[1, 2, 3, raz , dva ]
lst3 [2]= tri
lst3 [1, 2, tri , raz , dva ]
lst3 [2]=[7,8]
lst3 [1, 2, [7, 8], raz , dva ]
i.
.
:
lst3 =[1, 2, [7, 8], raz , dva ]
del lst3 [2]
lst3 [1, 2, raz , dva ]
lst [ i : j]=x
lst x
( x).
:
lst3 =[1, 2, 3, raz , dva ]
lst3 [2:4]= tri
lst3 [1, 2, t , r , i , dva ]
lst3 [2:4]= a
lst3 [1, 2, a , i , dva ]
,
!
del lst [ i : j ]
,
( ).
:
lst3 =[1, 2, a , i , dva ]
del lst3 [2:4]
lst3 [1, 2, dva]
17
18
1. Python
,
, , , , , ,
.
Python, , ,
.
1.7:
lst .append(x)
x lst. x
. .
:
lst=[raz,dva,tri,1,2]
lst.append(3)
lst [raz,dva,tri,1,2,3]
lst .extend(t)
lst .count(x)
lst . index(x)
t lst
( ,
).
:
lst1=[1,2,3]
lst2=[raz,dva]
lst1.extend(lst2)
lst1 [1,2,3,raz,dva]
, x,
lst. .
:
lst=[1,2,3,raz,dva,raz,dva]
lst .count( raz ) 2
x
lst. , .
:
lst=[1,2,3,raz,dva,raz,dva]
lst . index( dva) 4
1.2.
1.7:
lst .remove(x)
x lst . ,
.
:
lst=[1,2,3,raz,dva,raz,dva]
lst.remove(dva)
lst [1,2,3,raz,raz,dva]
lst .pop(i)
lst . sort ()
lst . reverse ()
i lst.
( ). , . .
:
lst=[1,2,3,raz,raz,dva]
\ lstinline lst.pop(3)| raz|
lst [1,2,3,raz,dva]
\ lstinline lst.pop()| dva|
lst [1,2,3,raz]
x i
lst. i>=0, .
i>len ( lst ), . .
:
lst= [1,2,3,raz]
lst.insert(3,tri)
lst [1,2,3,tri,raz]
(
). .
:
lst=[1,2,3,tri,raz]
lst.sort()
lst [1,2,3,raz,tri]
.
.
:
lst=[1,2,3,raz,tri]
lst.reverse()
lst [tri,raz,3,2,1]
19
20
1. Python
1.2.
21
, ,
sum(), ().
:
l s t 1 =[1 ,2 ,3 ,4]
sum ( l s t 1 ) 10
t1 =(1 ,2 ,3)
sum ( t 1 ) 6
list ().
:
s= amamam
l s t=l i s t ( s )
l s t [ a , m , a , m , a , m ]
t =(5 , 1 2 , 3, 7 )
l s t 2= l i s t ( t )
l s t 2 [ 5 , 1 2 , 3, 7 ]
, tuple ()
.
split () join (). split () .
:
s= mama myla ramu
l s t =s . s p l i t ( )
# -
l s t [ mama , myla , ramu ]
join () ,
( ).
:
l s t =[ 1 , 2 , 3 ]
s= nea . j o i n ( l s t )
s 1 nea2nea3
range() .
:
r a n g e ( x0 , x1 , d )
[x0, x1)
d, ,
range (0 ,15 ,3) [0 , 3 , 6 , 9 , 12]
22
1. Python
range (n)
0 n 1 1.
range (k , n)
k n 1 1.
, range(),
range() ( x in range(a,b,d))
( x not in range(a,b,d)). (. 2.3).
sum() , range(),
.
:
sum ( r a n g e ( 1 0 ) ) 45
sum ( r a n g e ( 0 , 1 5 , 3 ) ) 30
1.3
Python
Python , .
Python python .
( ) . 1.1.
<CTRL>+<D>.
>>>.
<ENTER>, Python
.
- ,
, <ENTER>.
.
,
. ( ) (IDE Integrated Development Environment).
IDE , , .
IDE Python IDLE (. 1.2).
. ,
Python (..
).
1.3. Python
. 1.1: Python
. 1.2: IDLE
23
24
1. Python
Options IDLE ,
.
, , <F5>.
, IDLE . ,
.
IDLE ( ) , , IDLE
.
Python IDE Eric (. 1.3), . (,
)
(. 1.4).
, Eric
( ).
1.3. Python
25
26
1. Python
. 1.6: Kate
1.4
1.4.1
Python
() , , input ()
raw_input().
- ( ), ,
.
.
- ( ), (. 1.5).
- . input () raw_input() -
1.4. Python
27
-
<ENTER>. ,
.
, <ENTER> .
.
>>> , , ,
(, Python), # ().
input () ,
( ).
:
>>> a=i n p u t ( ) <ENTER>
#
12 <ENTER>
>>> a <ENTER>
# a
12
>>>
>>> a=i n p u t ( a : ) <ENTER>
#
a: 12 <ENTER>
#
>>>
>>> a , b , c=i n p u t ( : )
<ENTER>
#
: 3 , 4 , 5 <ENTER>
>>> b <ENTER>
# b
4
>>>
>>> t =(a , b , c , d )=i n p u t ( : ) <ENTER>
>>> 5 , bob , 3 , dno <ENTER>
>>> t [ 1 ] <ENTER>
#
bob
>>>t <ENTER>
(5, bob, -3, dno)
>>>
(
input () ),
list ().
28
1. Python
Python
raw_input(). input ().
.
raw_input() , .
:
>>> name=raw_input ( ? ) <ENTER>
? <ENTER>
>>> name <ENTER>
1.4. Python
1.4.2
29
.
,
.
. r (
), w () a ().
.
, .
.
1.dat
1 2 3
a b c
:
>>> f d=open ( 1 . d a t , r ) <ENTER>
:
>>> s=f d . r e a d ( ) <ENTER>
>>> s <ENTER>
1 2 3\na b c
>>>
, . Pyrhon
,
, . .
\n.
, ( new line).
.
split () .
>>> l s t =s . s p l i t ( \n ) <ENTER>
>>> l s t <ENTER>
[1 2 3,a b c]
, ,
split () lst .
( ).
>>> l s t 2= l s t [ 0 ] . s p l i t ( ) <ENTER>
30
1. Python
>>> l s t 2 <ENTER>
[1,2,3]
>>> b=i n t ( l s t 2 [ 1 ] ) <ENTER>
>>> b <ENTER>
2
( ) close ():
>>> f d . c l o s e ( ) <ENTER>
. readline ()
, :
>>> f d=open ( 1 . d a t , r ) <ENTER>
>>> s 0 1=f d . r e a d l i n e ( ) <ENTER>
>>> s 0 1 <ENTER>
1 2 3\n
>>> s 0 1=f d . r e a d l i n e ( ) <ENTER>
a b c
>>> f d . c l o s e ( ) <ENTER>
readlines () .
:
>>>
>>>
>>>
[1 2
>>>
2
>>>
f d=open ( 1 . d a t , r ) <ENTER>
l s t 0 1=f d . r e a d l i n e s ( ) <ENTER>
l s t 0 1 <ENTER>
3\n, a b c]
l e n ( l s t 0 1 ) <ENTER>
f d . c l o s e ( ) <ENTER>
- .
,
write (), .
>>>
>>>
>>>
>>>
f d=open ( 1 . d a t , w ) <ENTER>
s 2= c d e <ENTER>
f d . w r i t e ( s 2 ) <ENTER>
f d . c l o s e ( ) <ENTER>
cde, ,
, . close ()
.
.
>>> f d=open ( 2 . d a t , a ) <ENTER>
1.5.
>>>
>>>
>>>
>>>
>>>
31
s 1= c d e \n <ENTER>
f d . w r i t e ( s 1 ) <ENTER>
s 2= 34 5\ n <ENTER>
f d . w r i t e ( s 2 ) <ENTER>
f d . c l o s e ( ) <ENTER>
\n . 2.dat :
c d e
3 4 5
1.5
Python
, .
() .
( )
().
.
(IDE)
,
. .py (, my_program.py).
, () , Python ,
( ).
.
:
# -*- coding: utf-8 -*name=raw_input ( ? )
p r i n t , , name , !
Python, ,
p y t h o n my_program . py
IDE Python . IDLE
<F5>, Eric
<F2> <ENTER>.
32
1.6
1. Python
Python
1.6. Python
33
. 1.7: Python
34
1. Python
1.7
1. a<b=c , a<b==c
?
2. / // ?
?
3. divmod()?
4. Python?
5. s1 s2. print s1+s2 print s1,s2?
6. t1 t2. print t1+t2 print t1 , t2?
7. .
8. s=madagaskar. s [1:7:2] ?
9. , , ,
.
10. () q .
11. ()
.
2
Python
( ) , -, (
).
Python , .
2.1
, . -
. 2.1.
.
1. a b. ,
.
: -
. a x, b y. ,
a y, b x.
(): c, a, a b, b c.
- . 2.2.
:
a,b
c=a
36
2. Python
. 2.1:
a=b
b=c
a,b
Python: . a b ,
, .
Python:
# -*- coding: utf-8 -*#
#
( a , b )=i n p u t ( ( a , b ) : )
(a , b) = (b , a)
p r i n t : , a , \n , b : , b
1.4.2, \n
print.
2. () .
, .
2.1. .
37
. 2.2: -
: ( oklad, ) (
procent, ). ( nalog)
okladprocent/100, , ( summa)
okladnalog .
- . 2.3.
:
o k l a d , p r o c e n t
n a l o g=o k l a d p r o c e n t /100
summa=o k l a d n a l o g
summa , n a l o g
Python:
# -*- coding: utf-8 -*#
o k l a d=i n p u t ( " : " )
p r o c e n t=i n p u t ( "% : " )
n a l o g=f l o a t ( o k l a d p r o c e n t ) /100
summa=o k l a d n a l o g
p r i n t " : " , summa
p r i n t " : " , n a l o g
38
2. Python
. 2.3: -
, .
( oat ()).
3.
35
40
20
3
. ,
, 1
(): , ( a, b, c d ).
S1. S2=S1+b
S2/S1 ( res).
S1 res.
- . 2.4
:
a , b , c , d
1 : .., ... : , , . --: , 2009.
2.1. .
39
. 2.4: -
S1=a , b , c , d
S2=S1+b
r e s=S2/S1
S1 , r e s
Python :
# -*- coding: utf-8 -*#
t =(a , b , c , d )=i n p u t ( : )
S1=sum ( t )
S2=S1+b
r e s=f l o a t ( S2 ) /S1
p r i n t : , S1 , \n , , : , r e s
oat (). ( ,
res=oat(S2)/S1 res=oat(S2/S1)).
4. (
) , . . // (, 17/05/2009).
: 2009-06-15. , (-
40
2. Python
),
.
Python. Python datetime,
date .
.
# -*- coding: utf-8 -*#
#
from d a t e t i m e import d a t e
#
d1=d a t e . t o d a y ( )
#
d s=s t r ( d1 )
p r i n t "" , d s
#
l s t =d s . s p l i t ( )
l s t . reverse ()
#
r u s d a t e="/" . j o i n ( l s t )
p r i n t "" , r u s d a t e
.
2.1.1
1. - 4 .
2. ,,.
.
3. . (: math
sqrt ()).
4. . .
5. . , .
6. Z=1/(XY) X Y 0.
2.2
, -
2.2.
41
. 2.5:
42
2. Python
. 2.6: -
1. t !, t > 10 !,
t 6 10 2 .
: t,
s. t < 10 s=!, s=
!.
- . 2.7.
:
t
( t <10)
s= !
s= !
s
2 : .., ... : , , . --: , 2009.
2.2.
43
. 2.7: -
Python:
# -*- coding: utf-8 -*#
t=i n p u t ( : )
i f t <10:
s= !
else :
s= !
print s
:. IF () .
IF . Python ,
. , Python .
2 ( ). P , , !, P = 5, !, P = 4
!, P 6 3.
: P,
5. P
s , . P.
- . 2.8.
44
2. Python
. 2.8: -
:
P
(P=5)
s= !
(P=4)
s= !
s= !
s
Python:
# -*- coding: utf-8 -*#
P=i n p u t ( ? )
i f P==5:
s= !
e l i f P==4:
s= !
2.2.
45
else :
s= !
print s
elif Python else if ( )
( ).
2.2.1
1. . , : ,
4.
2. . , : , 5.
3. . , : , 7.
4. . , : ,
10.
5. : A B C. ,
M K.
6. . , : ,
, .
7. , D,
A?
8. S R , K?
9. . , : , .
10. . 500, 100, 10
2 ., .
11. : A,
H R. ,
M , , .
12. : A,
H R. ,
M , , .
46
2. Python
13. : X, Y , Z. , , , .
14. X. , [a, b].
15. Z = 1/(XY ) X Y .
16. : A, B, C. , A < B < C A > B > C .
17. X Y . Z. Z = X Y X > Y ,
Z = ln(X + Y ) .
18. a, b, c, d. ,
a, b c, d ,
.
19. A. f (A), f (x) = x2 + 4x + 5 ,
x 6 2; f (x) = 1/(x2 + 4x + 5).
20. A. f (A), f (x) = 0, x 6 0;
f (x) = x 0 < x 6 1, f (x) = x4 .
21. A. f (A), f (x) = 0 x 6 0;
f (x) = x2 x 0 < x 6 1, f (x) = x2 sin(x2 ).
22.
.
23. . , ,
.
. sqrt ()
log () math. pi (math.pi).
2.3
, ( ). .
, .
2.3.
47
. 2.9: -
(), , . , ( ), ( )
.
FOR (), WHILE ().
() ,
.
, ( ).
- () . 2.9,
- . 2.10.
, .
,
48
2. Python
. 2.10: -
.
, -
.
- . 2.10 ,
. , .
,
( - ), .
,
, .
, .
1. ,
N . .
:
:
N ;
i - ( ).
A[i] ;
:
S
C , C = S/N .
- . 2.11.
:
2.3.
49
. 2.11: -
N
S=0
i 1 N
A [ i ]
S=S+A [ i ]
C=S/N
C
, , ,
. , ,
. 1
( 1). ,
1, .
.
Python ( ,
range()).
# -*- coding: utf-8 -*#
50
2. Python
N=i n p u t ( : )
S=0
f o r i i n r a n g e (N1) :
a=i n p u t ( : )
S=S+a
C=S/N
p r i n t : ,C
, range(), 0,
N 1. ,
.
:,
Python .
, Python , .
Python
.
# -*- coding: utf-8 -*#
N=i n p u t ( : )
S=0
l s t =[]
f o r i i n r a n g e (N1) :
a=i n p u t ( : )
l s t . append ( a )
C=sum ( l s t ) /N
p r i n t : ,C
,
. ( ), .
2. , () ABBA, kazak .
: S ( , ..).
, . ,
( ag
), 1, 0,
. .
,
0. L (
2 ,
2.3.
51
. 2.12: -
).
- . 2.12.
:
S
f l a g =1
L= ( S )
N=L d i v 2
i 1 N
S [ i ]=S [ Li +1]
k=1
k=0
f l a g=f l a g k
52
2. Python
f l a g =1
!
k,
.
.
Python .
# -*- coding: utf-8 -*#
s 1=raw_input ( : )
#
L=l e n ( s 1 )
f l a g =1
f o r i in range ( L//2) :
i f s 1 [ i ]==s 1 [ i 1 ] :
k=1
else :
k=0
f l a g=f l a g k
i f f l a g ==1:
p r i n t
else :
p r i n t !
raw_input(), .
-
==, :, , , .
, , .
Python, ,
. , .
# -*- coding: utf-8 -*#
s 1=raw_input ( : )
l s t= l i s t ( s1 )
l s t . reverse ()
s 2= . j o i n ( l s t )
i f s 1==s 2 :
2.3.
53
. 2.13: -
p r i n t
else :
p r i n t !
,
- .
. !
Python.
, , ( )
WHILE. .
3. .
.
0.
, . SP,
SN.
- . 2.13.
:
54
2. Python
SP=0
SN=0
c h i s l o
c h i s l o <> 0
c h i s l o > 0
SP=SP+c h i s l o
SN=SN+c h i s l o
c h i s l o
SP
SN
Pascal BASIC
<>, .
,
, , 0 , .
0 (
).
WHILE .
Python - .
.
# -*- coding: utf-8 -*#
SP=0
SN=0
#
c h i s l o=i n p u t ( : )
#
w h i l e c h i s l o != 0 :
i f chis lo > 0:
SP=SP+c h i s l o
else :
SN=SN+c h i s l o
c h i s l o=i n p u t ( : )
#
p r i n t : , SP
p r i n t : ,SN
2.3.
55
. 2.14:
2.3.1
,
. Python
,
.
,
.
. ,
[3 1 0 5 2 7] [0 1 2 3 5 7].
.
.
.
A N .
( ), A[i] > A[i + 1],
.
,
. N 1 .
,
.
- . 2.14.
A[i] A[i + 1] .
:
56
2. Python
N, A
m N1 1 1
i 1 m
A [ i ] > A [ i +1]
X=A [ i ]
A [ i ]=B [ i +1]
A [ i +1]=X
A
.
. maxA . maxA , . ()
, maxA,
( ) . .
.
Python max(), min()
sort () ( sort () ).
2.3.2
1. - .
2. - .
3. , N .
, : A[1]
A[2]; A[3] A[4] . . .
4. , N .
, . . A[1]
A[2]; A[2] A[3]; . . . A[n] A[1].
5. , N .
.
6. , N .
M , K
M , P .
2.3.
57
7. , N .
M , K.
8. , N .
.
9. , N .
M , K.
10. , N .
.
11. , N .
,
.
12. , N .
, ,
, .
13. , N .
, ,
, .
14. , N .
.
,
.
15. , N . ,
.
16. , N .
, .
17. , N .
T . .
18. , N .
, [B; C].
58
2. Python
19. , N .
.
20. , N .
, .
21. , N .
, 3 , .
( 2 ).
22. M , . . ,
.
23. M , .
, , , .
24. M , . .
25. M , (
). ( ). .
26. M , (
). ( ).
.
27. M , .
.
28. M , .
. (
).
29. , ..
.
30. M , .
.
31. M , . ,
. .
2.4. ()
59
32. .
( ).
.
55555 .
33. . ( ) , . .
.
34. P H. P ,
H P H.
P H, .
35. , .
. 99999 .
36. . ,
B .
.
37.
. 65432 .
2.4
()
() . , .
, ( )
, .
. M
, N .
mas[i , j ], , .
M, N
i 1 N
j 1 M
mas [ i , j ]
.
i 1 N
j 1 M
60
2. Python
mas [ i , j ]
.
Python ( ) ( ,
. .).
Python numpy (Numeric Python).
Python :
# -*- coding: utf-8 -*#
import numpy
n=i n p u t ( : )
m=i n p u t ( : )
#
a=numpy . z e r o s ( [ n 1,m1])
#
f o r i i n r a n g e ( n1) :
f o r j i n r a n g e (m1) :
p r i n t [ , i , ] [ , j , ]
a [ i , j ]= i n p u t ( : )
#
() numpy.zeros()
(), ,
. , , , 0 ( Python
[0,0]). print ,
.
1. A, N M . .
:
:
n ;
m ;
A[i, j] ;
i, j .
:
S ( A[i, j] i j)
K (K = m n)
C (C = S/K)
2.4. ()
. 2.15: -
- . 2.15.
:
n ,m
S=0
i 1 n
j 1 m
A [ i , j ]
S=S+A [ i , j ]
K=nm
C=S/K
C
61
62
2. Python
Python:
# -*- coding: utf-8 -*#
import numpy
n=i n p u t ( : )
m=i n p u t ( : )
S=0.0
#
a=numpy . z e r o s ( [ n 1,m1])
#
f o r i i n r a n g e ( n1) :
f o r j i n r a n g e (m1) :
p r i n t [ , i , ] [ , j , ]
a [ i , j ]= i n p u t ( : )
S=S+a [ i , j ]
#
K=nm
C=S/K
p r i n t : ,C
2.4.1
1. A, N
M . A,
.
2. A, N
M .
.
3. A, N
M . A,
.
4. A, N
M .
.
5. A, N
M . . N + 1 M + 1
.
6. A, N
M . . ,
2.4. ()
63
.
N + 1 M .
7. A, N
M . . ,
. N M+1 .
8. A, N
M . , .
N + 1 M + 1 .
9. A, N
M . ,
L .
10. A, N
M .
K- .
11. A, N
M .
L- .
12. A, N
M .
.
13. A, N
M .
.
14. A, N
M .
.
15. A, N
M . . H.
, ,
.
16. A, N
M . L.
.
64
2. Python
17. A, N
M .
L.
18. A, N
N . , ,
, ( [0,0] [N,N],
[N,0] [0,N]).
19. A, N
N . ,
( ).
[0,0] [N,N].
20. A, N
N . , ( ).
[N,0] [0,N].
21. A, N
N . , ( ), , (
[0,0] [N,N]).
22. A, N
M . . , .
23. A, N
N . , , , ( [0,0] [N,N], [N,0] [0,N]).
24. A, N
M . L K, .
.
25. A, N
M . ,
.
N + 1 M + 1 .
2.5. ( )
65
26. A, N
M . L K,
.
.
27. A, N
M . .
H. , ,
.
28. A, N
M . K. .
29. A, N
M .
K.
30. A, N
M .
,
.
N + 1 M .
31. A, N
M .
,
.
N M + 1 .
2.5
(
)
, -
( , ). , ()
, . . (
).
( ),
.
(, ).
66
2. Python
,
(,
, ,
, .). -
.
( , , .) . ,
Basic . Pascal (record). Python
,
.
.
.
,
.
:
1. ;
2. , ;
3. ;
4. .
k
( name),
( plan),
( fact ).
( procent).
, , 100
.
Python , .
# -*- coding: utf-8 -*#
# k -
# name -
# plan -
# fact -
# procent - %
2.5. ( )
67
#
k=i n p u t ( " : " )
name = [ ]
plan =[]
f a c t =[]
#
for i in range ( k ) :
n=raw_input ( " : " )
name . append ( n )
p1=i n p u t ( " : " )
p l a n . append ( p1 )
p2=i n p u t ( " : " )
f a c t . append ( p2 )
#
p r o c e n t=map ( lambda x , y : x 100/ y , f a c t , p l a n )
f a k t y=z i p ( name , p r o c e n t )
p l a n y=z i p ( p l a n , name )
plany . sort ()
p r i n t 16 "="
p r i n t " : "
#
nedo=0
for i in range ( k ) :
s 1=f a k t y [ i ] [ 0 ]
s 2=f a k t y [ i ] [ 1 ]
i f s2 < 100:
nedo=nedo+1
#
p r i n t s1 , " : " , s 2
p r i n t " , : " , nedo
p r i n t " : " , max ( p l a n )
#
p r i n t " : "
for i in range ( k ) :
s 1=p l a n y [ i ] [ 1 ]
s 2=p l a n y [ i ] [ 0 ]
p r i n t s1 , " : " , s 2
map() lambda-
zip ()
. ,
zip (), .
. 2.16.
68
2. Python
. 2.16:
2.5.1
1.
35
40
20
3
. .
2. ( 0 100).
,
.
3. . ,
, , ,
.
2.5. ( )
69
4. ,
:
;
.
:
a) ;
b) , 10% ;
c) ;
d) .
3
Python
Python , .
Python
Tkinter
.
3.1
( -) ( ...)
. ,
. , . , .
turtle ().
(. 3.1)
() .
# -*- coding: utf-8 -*import t u r t l e
#
t u r t l e . reset ()
#
# turtle._root.mainloop() # Python 2.4.x 2.5.x
# ,
t u r t l e . m a i n l o o p ( ) # Python 2.6.x
, mainloop()
Python.
3.1.
71
. 3.1: turtle
, .
Python 2.6.
,
Python, . ()
,
, .
(
) .
(, , ), , , , Python
help( turtle ).
,
,
, .
, , .
up()
turtle .up()
down()
, ( )
turtle .down()
goto(x,y)
x, y
turtle .goto(50,20)
72
3. Python
color ( )
width(n)
turtle .width(3)
forward(n)
( , .
. 3.1) n
turtle . forward(100)
backward(n)
turtle .backward(100)
right (k)
(
) k
left (k)
(
) k
radians ()
turtle . radians ()
degrees()
( )
turtle . degrees()
circle (r)
|r|
. r ,
, .
circle (r ,k)
|r| k .
circle ()
ll ( ag )
ag
( ag =1) ( ag =0) . .
:
turtle . ll (1)
turtle . circle (50)
turtle . ll (0)
turtle . write (
! )
write ( )
3.1.
73
tracer ( ag )
clear ()
, .
, , turtle (, turtle . sin ())
.
. , . 3.2.
# -*- coding: utf-8 -*import t u r t l e
#
t u r t l e . reset ()
t u r t l e . t r a c e r (0)
t u r t l e . c o l o r ( #0000 f f )
#
t u r t l e . w r i t e ( 0 ,0 )
#
t u r t l e . up ( )
x=170
y=120
c o o r d s=s t r ( x )+" , "+s t r ( y )
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
x=130
y=100
c o o r d s=s t r ( x )+" , "+s t r ( y )
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
x=0
y=100
c o o r d s=s t r ( x )+" , "+s t r ( y )
74
3. Python
. 3.2:
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
t u r t l e . down ( )
x=0
y=100
c o o r d s=s t r ( x )+" , "+s t r ( y )
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
t u r t l e . up ( )
x=150
y=0
c o o r d s=s t r ( x )+" , "+s t r ( y )
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
t u r t l e . down ( )
x=150
y=0
c o o r d s=s t r ( x )+" , "+s t r ( y )
t u r t l e . goto ( x , y )
t u r t l e . write ( coords )
#
t u r t l e . mainloop ()
, .
, . 3.3, .
3.1.
# -*- coding: utf-8 -*import t u r t l e
#
t u r t l e . reset ()
t u r t l e . t r a c e r (0)
t u r t l e . width (2)
#
t u r t l e . up ( )
x=0
y=100
t u r t l e . goto ( x , y )
t u r t l e . f i l l (1)
t u r t l e . c o l o r ( #f f a a 0 0 )
t u r t l e . down ( )
t u r t l e . c i r c l e (100)
t u r t l e . f i l l (0)
t u r t l e . color ( black )
t u r t l e . c i r c l e (100)
t u r t l e . up ( )
#
x=45
y=50
t u r t l e . goto ( x , y )
t u r t l e . down ( )
t u r t l e . c o l o r ( #0000 aa )
t u r t l e . f i l l (1)
t u r t l e . c i r c l e (7)
t u r t l e . up ( )
t u r t l e . f i l l (0)
#
x=45
y=50
t u r t l e . goto ( x , y )
t u r t l e . down ( )
t u r t l e . c o l o r ( #0000 aa )
t u r t l e . f i l l (1)
t u r t l e . c i r c l e (7)
t u r t l e . up ( )
t u r t l e . f i l l (0)
#
x=55
y=50
t u r t l e . goto ( x , y )
t u r t l e . r i g h t (45)
75
76
3. Python
. 3.3:
turtle
turtle
turtle
turtle
turtle
#
turtle
x=0
y=50
turtle
turtle
turtle
turtle
turtle
#
turtle
. width (3)
. down ( )
. c o l o r ( #aa0000 )
. c i r c l e (80 ,90)
. up ( )
. r i g h t (135)
. goto ( x , y )
. width (2)
. color ( black )
. down ( )
. forward (100)
. mainloop ()
, ,
() 45 . ,
( . 3.1).
, .
.
, , 45 ,
90 . .
,
. (,
3.2. . Tkinter
77
)
goto(x,y). , , ,
.
3.1.1
1. , . 3.2 3.3, ?
2. ( ) .
3. ( ) ,
2 ( 8- ).
4. (y = axb )
,
.
3.2
.
Tkinter
, turtle , Tkinter.
(GUI Graphical User Interface) Python. (, , ) canvas () Tkinter , ,
, , , .
Tkinter.
Tkinter GUI, , ,
. .
# -*- coding: utf-8 -*import T k i n t e r
import math
#
t k=T k i n t e r . Tk ( )
t k . t i t l e ( " Sample " )
#
78
3. Python
b u t t o n=T k i n t e r . Button ( t k )
b u t t o n [ " t e x t "]=""
b u t t o n [ "command"]= t k . q u i t
b u t t o n . pack ( )
#
c a n v a s=T k i n t e r . Canvas ( t k )
c a n v a s [ " h e i g h t " ]=360
c a n v a s [ " w i d t h " ]=480
c a n v a s [ " b a c k g r o u n d "]="#e e e e f f "
c a n v a s [ " b o r d e r w i d t h " ]=2
c a n v a s . pack ( )
#
c a n v a s . c r e a t e _ t e x t ( 2 0 , 1 0 , t e x t=" 2 0 , 1 0 " )
c a n v a s . c r e a t e _ t e x t ( 4 6 0 , 3 5 0 , t e x t=" 4 6 0 , 3 5 0 " )
#
p o i n t s =[]
ay =150
y0 =150
x0=50
x1 =470
dx=10
#
f o r n i n r a n g e ( x0 , x1 , dx ) :
y=y0ay math . c o s ( n dx )
pp=(n , y )
p o i n t s . append ( pp )
#
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =" b l u e " , smooth =1)
#
y_axe = [ ]
yy =(x0 , 0 )
y_axe . append ( yy )
yy =(x0 , y0+ay )
y_axe . append ( yy )
c a n v a s . c r e a t e _ l i n e ( y_axe , f i l l =" b l a c k " , w i d t h =2)
#
x_axe = [ ]
xx =(x0 , y0 )
x_axe . append ( xx )
xx =(x1 , y0 )
x_axe . append ( xx )
c a n v a s . c r e a t e _ l i n e ( x_axe , f i l l =" b l a c k " , w i d t h =2)
#
tk . mainloop ()
3.2. . Tkinter
79
. 3.4: Tkinter
(. 3.4), .
,
. , math.
,
Tk, . title ( ).
(
widgets). .
pack(),
.
Button Tkinter,
. ( text)
- ( ),
command.
,
.
80
3. Python
. () canvas , ,
( , , canvas). , ( ).
, . , ,
(0, 0). canvas.create_text(),
.
, , .
canvas. create_line () () (x,y).
dx.
. . ,
. , , .
3.2.1
, . Tkinter, () .
( )
. 3.5.
, . . .
# -*- coding: utf-8 -*import T k i n t e r
import math
#
#
def plot_x_axe ( x0 , y0 , x1 ) :
x_axe = [ ]
xx =(x0 , y0 )
x_axe . append ( xx )
xx =(x1 , y0 )
x_axe . append ( xx )
3.2. . Tkinter
. 3.5: ,
81
82
3. Python
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =" b l u e " , smooth =1)
plot_y_axe ( x 0 i , y 0 i , y 1 i )
plot_x_axe ( x 0 i , y 0 i , x 1 i )
#
def p l o t _ f u n c 1 ( x0 , x1 , dx , y0 , y1 ) :
x 0 i=i n t ( x0 )
x 1 i=i n t ( x1 )
y 0 i=i n t ( y0 )
y 1 i=i n t ( y1 )
a=y0
b=y0y1
p o i n t s =[]
f o r x i n r a n g e ( x 0 i , x 1 i , dx ) :
y=i n t ( ay 1 i b/ x )
pp=(x , y )
p o i n t s . append ( pp )
#
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =" b l u e " , smooth =1)
plot_y_axe ( x 0 i , y 0 i , y 1 i )
plot_x_axe ( x 0 i , y 0 i , x 1 i )
#
def p l o t _ f u n c 2 ( x0 , x1 , dx , y0 , y1 ) :
x 0 i=i n t ( x0 )
x 1 i=i n t ( x1 )
y 0 i=i n t ( y0 )
y 1 i=i n t ( y1 )
a=(y0y1 ) / ( 1 5 x1 )
b=1+((y0y1 ) / ( x1x0 ) )
p o i n t s =[]
f o r x i n r a n g e ( x 0 i , x 1 i , dx ) :
y=y 0 i i n t ( a ( xx 0 i ) b )
pp=(x , y )
p o i n t s . append ( pp )
#
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =" b l u e " , smooth =1)
plot_y_axe ( x 0 i , y 0 i , y 1 i )
plot_x_axe ( x 0 i , y 0 i , x 1 i )
#
def p l o t _ f u n c 3 ( x0 , x1 , dx , y0 , y1 ) :
x 0 i=i n t ( x0 )
x 1 i=i n t ( x1 )
y 0 i=i n t ( y0 )
y 1 i=i n t ( y1 )
ay =150
3.2. . Tkinter
y 0 i =150
p o i n t s =[]
f o r x i n r a n g e ( x 0 i , x 1 i , dx ) :
y=y 0 i ay math . c o s ( x dx )
pp=(x , y )
p o i n t s . append ( pp )
#
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =" b l u e " , smooth =1)
plot_y_axe ( x 0 i , 0 , y 0 i+ay )
plot_x_axe ( x 0 i , y 0 i , x 1 i )
#
def DrawGraph ( ) :
f n=f u n c . g e t ( )
f=f n [ 0 ]
x0 =50.0
y0 =250.0
x1 =450.0
y1 =50.0
dx=10
#
i f f=="0" :
canvas . d e l e t e ( " a l l " )
p l o t _ f u n c 0 ( x0 , x1 , dx , y0 , y1 )
e l i f f=="1" :
canvas . d e l e t e ( " a l l " )
p l o t _ f u n c 1 ( x0 , x1 , dx , y0 , y1 )
e l i f f=="2" :
canvas . d e l e t e ( " a l l " )
p l o t _ f u n c 2 ( x0 , x1 , dx , y0 , y1 )
else :
canvas . d e l e t e ( " a l l " )
p l o t _ f u n c 3 ( x0 , x1 , dx , y0 , y1 )
#
#
t k=T k i n t e r . Tk ( )
t k . t i t l e ( " Sample " )
#
menuframe=T k i n t e r . Frame ( t k )
menuframe . pack ( { " s i d e " : " t o p " , " f i l l " : " x " } )
#
l b l =T k i n t e r . L a b e l ( menuframe )
l b l [ " t e x t "]=" : "
l b l . pack ( { " s i d e " : " l e f t " } )
#
83
84
3. Python
f u n c=T k i n t e r . S t r i n g V a r ( t k )
f u n c . s e t ( 0 y=Ax+B )
#
f s p i s =T k i n t e r . OptionMenu ( menuframe , f u n c ,
0 y=Ax+B ,
1 y=A+B/ x ,
2 y=Ax^B ,
3 y=A c o s ( Bx ) )
f s p i s . pack ( { " s i d e " : " l e f t " } )
#
btnOk=T k i n t e r . Button ( menuframe )
btnOk [ " t e x t "]=""
btnOk [ "command"]= DrawGraph
btnOk . pack ( { " s i d e " : " l e f t " } )
#
b u t t o n=T k i n t e r . Button ( menuframe )
b u t t o n [ " t e x t "]=""
b u t t o n [ "command"]= t k . q u i t
b u t t o n . pack ( { " s i d e " : " r i g h t " } )
# ()
c a n v a s=T k i n t e r . Canvas ( t k )
c a n v a s [ " h e i g h t " ]=360
c a n v a s [ " w i d t h " ]=480
c a n v a s [ " b a c k g r o u n d "]="#e e e e f f "
c a n v a s [ " b o r d e r w i d t h " ]=2
c a n v a s . pack ( { " s i d e " : " bottom " } )
tk . mainloop ()
() ( tk=Tkinter.Tk()). (Frame) (canvas). (
Label), (OptionMenu) . , , , -
.
pack() , (
- ).
. (
, Tkinter . StringVar ())
( ).
OptionMenu() .
3.2. . Tkinter
85
, DrawGraph().
, , , ,
.
def. , (..
).
DrawGraph()
, ,
.
( get()),
.
canvas. delete (" all ").
, . , ,
int ().
,
.
, , , .
3.2.2
: ,
,
. :
h0 = 0 v0 . ,
. .
. 3.6.
, x y .
x(t) = v t = v0 cos() t
(3.1)
86
3. Python
. 3.6: ,
y(t) = v t = v0 sin() t g t2
x ( 3.1)
x
t=
v0 cos()
(3.2)
(3.3)
y,
y(x):
g
(3.4)
y(x) = x tg() x2
2 v02 cos2 ()
,
, .
v (t) = v0 cos()
(3.5)
v (t) = v0 sin() g t
(3.6)
t0 , , v = 0.
v0 sin()
t0 =
(3.7)
g
H ( 3.2) t0 .
H = y(t0 ) =
v02 sin2 ()
2g
(3.8)
T , 2t0 , S
S = v T = v0 cos() 2 t0 =
v02 sin(2 )
g
(3.9)
.
.
3.2. . Tkinter
# -*- coding: utf-8 -*#
#
#
import T k i n t e r
import math
#
def plot_x_axe ( x0 , y0 , x1 ) :
x_axe = [ ]
xx =(x0 , y0 )
x_axe . append ( xx )
xx =(x1 , y0 )
x_axe . append ( xx )
c a n v a s . c r e a t e _ l i n e ( x_axe , f i l l =" b l a c k " , w i d t h =2)
#
def plot_y_axe ( x0 , y0 , y1 ) :
y_axe = [ ]
yy =(x0 , y1 )
y_axe . append ( yy )
yy =(x0 , y0 )
y_axe . append ( yy )
c a n v a s . c r e a t e _ l i n e ( y_axe , f i l l =" b l a c k " , w i d t h =2)
#
def DrawGraph ( ) :
#
d t a=s c . g e t ( )
a l p h a=d t a math . p i /180
d t l b l= c l i s t . g e t ( )
#
c a n v a s . c r e a t e _ r e c t a n g l e ( x 1 i 90 , y 1 i 50 ,
x 1 i +50 , y 1 i +10 , f i l l ="#e e e e f f " )
# g=10, v0 , canvas
g =10.0
v0=63
#
S=i n t ( ( v0 2 ) math . s i n ( 2 a l p h a ) /g )
H=i n t ( ( ( v0 2 ) ( math . s i n ( a l p h a ) ) 2 ) / ( 2 g ) )
#
p o i n t s =[]
for x in range ( x0i , x1i ) :
xx =(xx0 )
y=( xx math . t a n ( a l p h a ) ) (( xx 2 ) g/ \
( 2 ( v0 2 ) ( math . c o s ( a l p h a ) 2 ) ) )
87
88
3. Python
#
i f y > 0:
yy=i n t ( y0y )
else :
yy=y 0 i
#
pp=(x , yy )
p o i n t s . append ( pp )
#
c a n v a s . c r e a t e _ l i n e ( p o i n t s , f i l l =d t l b l , smooth =1)
plot_x_axe ( x 0 i , y 0 i , x 1 i )
#
d t e x t=" : "+s t r ( S )
v t e x t=" : "+s t r (H)
d a l n o s t=c a n v a s . c r e a t e _ t e x t ( x 1 i 70 , y 1 i 30 , t e x t=d t e x t ,
f i l l =d t l b l , a n c h o r="w" )
v y s o t a=c a n v a s . c r e a t e _ t e x t ( x 1 i 70 , y 1 i 10 , t e x t=v t e x t ,
f i l l =d t l b l , a n c h o r="w" )
#
#
t k=T k i n t e r . Tk ( )
t k . t i t l e ( "" )
#
menuframe=T k i n t e r . Frame ( t k )
menuframe . pack ( { " s i d e " : " t o p " , " f i l l " : " x " } )
#
l b l =T k i n t e r . L a b e l ( menuframe )
l b l [ " t e x t "]=" : "
l b l . pack ( { " s i d e " : " l e f t " } )
#
c l i s t =T k i n t e r . S t r i n g V a r ( t k )
c l i s t . set ( black )
#
c s p i s=T k i n t e r . OptionMenu ( menuframe , c l i s t ,
red ,
green ,
blue ,
cyan ,
magenta ,
purple ,
black )
c s p i s . pack ( { " s i d e " : " l e f t " } )
#
btnOk=T k i n t e r . Button ( menuframe )
3.2. . Tkinter
89
90
3. Python
. 3.7:
,
.
g v0 , .
,
.
.
.
Tkinter Python .
, ,
.
3.2.3
1. , . 3.4,
.
3.2. . Tkinter
91
2.
, . 3.2.
3. .
4. , ,
.
5. ,
,
.
4.1
. Python?
.
,
( ,
), Python ( `o, `a).
1. Python . 19901991 ,
(Guido van Rossum)
.
2. Python (
20 ). Python .
3. Python , .
, .
4. Python (). -,
- , -, Python
( Turbo Pascal).
4.2.
93
5. Python . , Python
GIMP, Python OpenOce.org, Python
3D- Blender, Python Plone MoinMoin Wiki, Python
.
6. Python , . Python
GNU/Linux ( , ).
7. ,
Python. , (, Kate Bluesh),
(IDE).
8. , Python -, (Unicode), Python
.
4.2
Python Python
( 2.4), Tkinter NumPy, Python
IDLE, Eric Geany, - xterm, rxvt
..
ALT Linux
geany
eric
xterm
python
python-base
python-doc
python-module-numpy
python-modules
python-modules-encodings
python-modules-tkinter
python-tools-idle
Python, Eric Geany ,
.
94
4.
( ) . , - ,
, , . ,
- .
(Integrated
Development Environment, IDE).
IDE, Linux, . IDE,
, ,
, . , IDE,
, , .
ALT Linux Geany,
Eric. IDE.
4.3
()
, , , . .
(http://glossary.ru).
4.3.0.1
.
4.3.0.2
:
, , ;
, ,
() .
4.3.0.3
, :
;
4.3. ()
95
, ;
.
:
: , , , , ;
: , .
4.3.0.4
19781-90 ,
.
4.3.0.5
( )
() , . : .
4.3.0.6
, , , .
, : , () ,
() .
, , .
4.3.0.7
, , .
4.3.0.8
, , , , .
.
96
4.3.0.9
4.
, , ,
.
4.3.0.10
,
.
( ).
.
, .
4.3.0.11
, .
4.3.0.12
, ( )
( ).
4.3.0.13
- , , ,
. ,
, .
4.3.0.14
, (
).
4.3.0.15
,
.
4.3.0.16
,
. ,
4.3. ()
97
. , .
4.3.0.17
, , , , .
4.3.0.18
.
: , , , , , .
4.3.0.19
() , . :
;
;
.
( , ,
).
4.3.0.20
( )
, , , ().
(), ..
98
4.
4.3.0.22
( , ),
, .
, a[1, 3] , a
1,3 ( 1, 3).
4.3.0.23
. .
, .
4.3.0.24
( )
.
( , ) (
, ).
4.3.0.25
,
.
4.4
IDE Geany
IDE Python -
Geany. Geany
( /). Geany . 4.1.
, .
, Python (, ) .
( ). ,
.
. ,
(
). ,
(C, Pascal . .).
- , -
99
4.4.1
IDE. , ,
Python .
/ <CTRL>+<ALT>+<P>. (. 4.2) .
Geany
. ( xterm) .
Geany,
Geany .
.
, Geany .
100
4.
. 4.2: Geany
(
),
.
Geany , . . 4.3 .
. (. 4.4)
.
101
. 4.4: Geany
, ,
( Fixed).
, . 4.3,
. ,
, , , .
(. 4.5), , .
,
. , .
, .
Geany, (
). . 4.6.
( )
(
). ,
102
4.
. 4.5: Geany
. 4.6: Geany
, .
Geany (. 4.1) , ,
.
, , . 4.7.
(
(
Geany).
. 4.17.
103
. 4.7: Geany
. 4.8: Geany
104
4.
. 4.9: Geany
(. 4.9) , .
, . (.
4.10) 4 ( Python) .
Tab .
(. 4.11)
- ,
.
105
. 4.11: - Geany
. 4.12: Geany
(. 4.12) , .
GTK (. 4.13). (
).
,
. HSV
(--) RGB (--),
HTML- ( ). ,
106
4.
. 4.13: GTK-
Geany
. 4.14: Geany
.
.
.
Geany (. 4.14) . ,
,
. Geany , .
107
. 4.15:
(),
Geany (. 4.15).
,
. ,
xterm.
(, ) Geany .
4.4.2
.
python-doc /usr/share/doc/python-doc-x.y.z, x.y.z Python (,
/usr/share/doc/python-doc-2.5.2).
(Konqueror Firefox), / ...
/usr/share/doc/python-doc-x.y.z/index.html.
.
Python . 4.16.
Python python.ru.
python-doc ,
.
108
4.
. 4.16: Python ( )
. 4.17:
4.4.3
, , (. 4.17).
, , ,
.
/ ( / ... ),
<CTRL>+<S>.
109
. 4.18: GTK- ()
GTK- /
(. 4.18). .
. ,
.
, ()
.
, ,
.
(
Python .py, . 4.18),
.
,
(. 4.19).
,
.
110
4.
. 4.19: Python
. 4.20: ,
( / ).
(,
Python) <F5>
.
4.4.4
Python
.
. . 4.20
. ( <ENTER>).
Geany ,
(. 4.21).
, .
( )
.
111
. 4.21:
. 4.22: :
( exception Python) 1.
0 ( (program
exited with code: 0), , .
.
4.5
IDE Eric
112
4.
. 4.23: : (
)
. 4.24: :
113
, ( ). , .
4.5.1
, (),
.
( ) ,
, (. 4.26).
, . , ( )
, , ( -
114
4.
. 4.26: Eric
). IDE Eric
(. 4.27, ).
- , (/...
Eric). (. 4.28), ( . 4.28)
(. 4.29).
, , .
( Eric Python
, ), , .
115
116
4.
(. 4.29) ,
.
, , IDE ( Fixed).
,
,
, Python , (. 4.30).
4.5.2
( , . 4.31).
IDE Eric, PDF, Python. ,
, Python ( python-doc - ) .
117
. 4.30:
web pdf ,
( )
.
Python .
IDE Eric ( , . 4.32).
... , html-. ,
Eric, web,
html .
Eric Eric
(. 4.33), .
Python html-
Python, , , , (. 4.34).
python-doc, .
118
4.
. 4.31:
119
. 4.33: Eric
4.5.3
, ,
(. 4.35).
/ ( / ... ),
<CTRL>+S.
/ (.
4.36). ()
.
( / ).
(, Python) <F2>,
(, .
4.37).
(
) , . , ,
, <ENTER>. ,
IDE Eric
<F2> <ENTER>.
() Eric
(. 4.38).
120
4.
. 4.35: Eric
121
. 4.36: () Eric
. 4.37: Eric
122
4.
. 4.40: Eric
4.5.4
IDE Eric
( , ) () (. 4.39).
.
(, ), Eric (.
4.40).
Python (exception) (
0), ,
.
,
. IDE Eric
(. 4.41).
, . 4.41, , .
IDE Eric ( )
,
.
4.6. Tk Tkinter
123
. 4.41: Eric ()
4.6
Tk Tkinter
Tk Tkinter () Geany .
, .
Eric Tkinter , quit Tk IDE Eric.
, quit Tk
Python, IDE Eric
Python , -
( IDE Eric...). Tkinter
IDE Eric ,
Tkinter
( ).
Tkinter
Python 2.5.x, , , .
Tkinter , ( canvas) . Tkinter 2.5.x
.
1. (/home/<username>) (, KWrite Kate)
.Xresources ( !)
2. - :
Tk f o n t : t e r m i n u s r 12
1. (
).
124
4.
, , xfontsel.
4.7
()
Python, , ....
,
.
( ) .
, , ( ) GNU GPL2 .
1. ... . . . 3-. : , 2003.
2. ... .
10-11 . .: . , 2005.
3. .., ... : , , . /: , 2009.
4. ... . .
. : , 2000.
5. .., .., ... . . . . , 2003.
6. .., . . Python. .
(. .) : , 2000.
7. .. Python. (. .) .: , 2002.
8. ... Python. . .: -,
2002.
9. ... Python. .: . , 2006.
10. . . Python 3.
(. .) .: -, 2009.