Nazivi, varijable, tipovi
Nazivi
Naziv ili ime je niz znakova koji označava varijablu, funkciju, tip ili razred.
Pravila oblikovanja naziva:
Primjeri ispravno oblikovanih naziva: seven, sedam, _7, __, the_girl_who_cried_champagne, TheGirlWhoCriedChampagne
Primjeri neispravnih naziva: 7_, C++, C#, četvrtak, The Girl Who Cried Champagne, the-girl-who-cried-champagne
Velika i mala slova se razlikuju, tako da su utviklingssang, Utviklingssang i UTVIKLINGSSANG tri različita naziva.
Varijable i pridruživanje
Varijabla je područje u memoriji računala kojem se može pristupiti navođenjem njezina naziva, a i nekih drugih izraza, poput indeksiranja u listi.
Varijable sadrže vrijednosti. Te se vrijednosti mogu mijenjati. Vrijednost se u varijablu upisuje ili „pohranjuje” pridruživanjem:
13 13 |
Operator = je operator pridruživanja, a ne ispitivanja jednakosti.
Naziv varijable može se pojaviti i s lijeve i s desne strane znaka pridruživanja, ali se značenja njezina naziva u ta dva slučaja razlikuju. S lijeve strane naziv označava područje u memoriji u koje se upisuje vrijednost izraza koji je s desne strane. S desne pak strane znaka pridruživanja naziv označava sadržaj varijable—vrijednost koja je u njoj pohranjena.
13 13 |
Naziv varijable se ne smije pojaviti s desne strane znaka pridruživanja prije no što je u varijablu upisana vrijednost.
Traceback (click to the left of this block for traceback) ... NameError: name 'c' is not defined Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_9.py", line 10, in <module> exec compile(u"print _support_.syseval(python, u'b = c', __SAGE_TMP_DIR__)" + '\n', '', 'single') File "", line 1, in <module> File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sagenb/misc/support.py", line 438, in syseval return system.eval(cmd, sage_globals, locals = sage_globals) File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/python.py", line 63, in eval eval(z, globals) File "", line 1, in <module> NameError: name 'c' is not defined |
14 14 |
Izraz koji označava varijablu naziva se lvalue (left hand value), dok se izraz koji označava vrijednost koja jest ili može biti sadržaj varijable naziva rvalue (right hand value).
Naredba pridruživanja ima oblik
lvalue = rvalue
15 15 |
26 26 26 26 |
Traceback (click to the left of this block for traceback) ... SyntaxError: can't assign to operator Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_8.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("YSArIGIgPSBj"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpxem9Tx/___code___.py", line 2 a + b = c SyntaxError: can't assign to operator |
39 39 |
13 26 26 13 13 26 26 13 |
[1, 2, 3, 4] [1, 3, 3, 4] [1, 2, 3, 4] [1, 3, 3, 4] |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 10, 11, 12, 13, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 10, 11, 12, 13, 6, 7, 8, 9] |
1 2 3 4 1 2 3 4 |
1 2 3 4 1 2 3 4 |
Kombinirano pridruživanje je spoj binarne aritmetičke operacije u kojoj se pojavljuje varijabla kojoj će rezultat biti pridružen i pridruživanja:
1 1 |
1 1 |
0 0 |
4 12 4 12 |
3 3 |
81 81 |
3 3 |
81/4 81/4 |
3 3 |
Tipovi
Tipom izraza određeni su
[Podsjećamo vas da smo pojam izraza definirali na drugom predavanju (MPZI_predavanje_02), a da smo aritmetičke i relacijske operacije uveli na prvom predavanju (MPZI_predavanje_01).]
6 6 |
Integer Ring Integer Ring Integer Ring Integer Ring Integer Ring Integer Ring |
<type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> |
-2 Integer Ring -2 Integer Ring |
8 Integer Ring 8 Integer Ring |
2 Rational Field 2 Rational Field |
3/2 Rational Field 3/2 Rational Field |
2 Rational Field 2 Rational Field |
5.00000000000000 5.00000000000000 5.00000000000000 5.00000000000000 |
Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision Real Field with 53 bits of precision |
<type 'sage.rings.real_mpfr.RealLiteral'> <type 'sage.rings.real_mpfr.RealLiteral'> <type 'sage.rings.real_mpfr.RealNumber'> <type 'sage.rings.real_mpfr.RealNumber'> <type 'sage.rings.real_mpfr.RealLiteral'> <type 'sage.rings.real_mpfr.RealLiteral'> <type 'sage.rings.real_mpfr.RealNumber'> <type 'sage.rings.real_mpfr.RealNumber'> |
0.666666666666667 Real Field with 53 bits of precision 0.666666666666667 Real Field with 53 bits of precision |
'ABC' 'BCA' 'ABC' 'BCA' |
<type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> |
<type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> <type 'str'> |
Traceback (click to the left of this block for traceback) ... TypeError: unsupported operand type(s) for -: 'str' and 'str' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_39.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ayAtIGg="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmppwenqc/___code___.py", line 2, in <module> exec compile(u'k - h File "", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str' |
Traceback (click to the left of this block for traceback) ... TypeError: can't multiply sequence by non-int of type 'str' Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_40.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aCpr"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpD6fnYz/___code___.py", line 2, in <module> exec compile(u'h*k File "", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str' |
'BCBCBCBC' 'BCBCBCBC' <type 'str'> 'BCBCBCBC' 'BCBCBCBC' <type 'str'> |
[1, 2, 3, 4, 5, 6] [5, 6, 1, 2, 3, 4] [1, 2, 3, 4, 5, 6] [5, 6, 1, 2, 3, 4] |
<type 'list'> <type 'list'> <type 'list'> <type 'list'> <type 'list'> <type 'list'> <type 'list'> <type 'list'> |
[1, 2, 3, 4, 1, 2, 3, 4] [1, 2, 3, 4, 1, 2, 3, 4] <type 'list'> [1, 2, 3, 4, 1, 2, 3, 4] [1, 2, 3, 4, 1, 2, 3, 4] <type 'list'> |
U prethodnim izrazima tipovi su određeni implicitno na temelju konstanata i operacija koje se u njima pojavljuju. No, tipovi se mogu, a u nekim slučajevima i moraju, eksplicitno zadati:
... cijeli brojevi:
2 Integer Ring 2 Integer Ring |
2 Integer Ring 2 Integer Ring |
2 Integer Ring 2 Integer Ring |
2 <type 'int'> 2 <type 'int'> |
... racionalni brojevi:
1/2 Rational Field 1/2 Rational Field |
2 Rational Field 2 Rational Field |
2 Rational Field 2 Rational Field |
3 Rational Field 3 Rational Field |
... „realni” brojevi:
4.00000000000000 Real Field with 53 bits of precision 4.00000000000000 Real Field with 53 bits of precision |
4.00000000000000 Real Field with 53 bits of precision 4.00000000000000 Real Field with 53 bits of precision |
4.0 <type 'float'> 4.0 <type 'float'> |
|
5.0000000000000000000000 0.600 5.0000000000000000000000 0.600 |
Real Field with 80 bits of precision Real Field with 12 bits of precision Real Field with 80 bits of precision Real Field with 12 bits of precision |
Real Field with 80 bits of precision Real Field with 80 bits of precision |
prikaz s pomičnim (plivajućim) zarezom (u engleskom: s plivajućom točkom — floating point):
dekadski sustav: x=±m⋅10e
m — mantisa, 1≤m<10 ili m=0
e — eksponent
123456780000000000000,0=1,2345678⋅1020
123,45678=1,2345678⋅102
0,00012345678=1,2345678⋅10−4
0,000000000000000000012345678=1,2345678⋅10−20
binarni sustav (najjednostavnije): x=s⋅m⋅2e
s∈{−1,1}
m — mantisa, 1≤m<2 ili m=0
e — eksponent
15.9545897701910 24.0823996531185 3.61235994796777 15.9545897701910 24.0823996531185 3.61235994796777 |
16 24 4 16 24 4 |
... imaginarni i kompleksni brojevi:
I Symbolic Ring I Symbolic Ring |
I Symbolic Ring I Symbolic Ring |
1.00000000000000*I Complex Field with 53 bits of precision 1.00000000000000*I Complex Field with 53 bits of precision |
1.00000000000000*I Complex Field with 53 bits of precision 1.00000000000000*I Complex Field with 53 bits of precision |
1.00000000000000 Complex Field with 53 bits of precision 1.00000000000000 Complex Field with 53 bits of precision |
3*I + 4 Symbolic Ring 3*I + 4 Symbolic Ring |
4.00000000000000 + 3.00000000000000*I Symbolic Ring 4.00000000000000 + 3.00000000000000*I Symbolic Ring |
4.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision 4.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision |
4.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision 4.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision |
1.00000000000000*I Complex Field with 53 bits of precision 1.00000000000000*I Complex Field with 53 bits of precision |
3.00000000000000 + 4.00000000000000*I Complex Field with 53 bits of precision 3.00000000000000 + 4.00000000000000*I Complex Field with 53 bits of precision |
3.0000000000000000000000 + 4.0000000000000000000000*I Complex Field with 80 bits of precision 3.0000000000000000000000 + 4.0000000000000000000000*I Complex Field with 80 bits of precision |
... vektori:
(5, 7, 9) (5, 7, 9) |
Vector space of dimension 3 over Rational Field Vector space of dimension 3 over Rational Field Vector space of dimension 3 over Rational Field Vector space of dimension 3 over Rational Field Vector space of dimension 3 over Rational Field Vector space of dimension 3 over Rational Field |
32 Rational Field 32 Rational Field |
(-3, 6, -3) Vector space of dimension 3 over Rational Field (-3, 6, -3) Vector space of dimension 3 over Rational Field |
[ 4 5 6] [ 8 10 12] [12 15 18] Full MatrixSpace of 3 by 3 dense matrices over Rational Field [ 4 5 6] [ 8 10 12] [12 15 18] Full MatrixSpace of 3 by 3 dense matrices over Rational Field |
Pretvorba tipova u „miješanim” aritmetičkim operacijama:
... ZZ → QQ → RR → CC
Integer Ring Rational Field Integer Ring Rational Field |
9/2 Rational Field 9/2 Rational Field |
Real Field with 53 bits of precision Real Field with 53 bits of precision |
6.00000000000000 Real Field with 53 bits of precision 6.00000000000000 Real Field with 53 bits of precision |
4.50000000000000 Real Field with 53 bits of precision 4.50000000000000 Real Field with 53 bits of precision |
Complex Field with 53 bits of precision Complex Field with 53 bits of precision |
7.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision 7.00000000000000 + 3.00000000000000*I Complex Field with 53 bits of precision |
... RealField (m) → RealField (n) ako je m > n
3.00 Real Field with 12 bits of precision 3.00 Real Field with 12 bits of precision |
6.00 Real Field with 12 bits of precision 6.00 Real Field with 12 bits of precision 6.00 Real Field with 12 bits of precision 6.00 Real Field with 12 bits of precision |
O pridruživanju, još i opet...
1 1 1 1 |
3 1 3 1 |
... ali, za složen(ij)e tipove:
(1, 20, 3) (1, 20, 3) (1, 20, 3) (1, 20, 3) |
(1, 20, 3) (1, 2, 3) (1, 20, 3) (1, 2, 3) |
|
|