Documente Academic
Documente Profesional
Documente Cultură
RELOR ȘI LIMBAJE DE
PROGRAMARE II
Introducere in Python
http://www.eed.usv.ro/~ionelar/teaching.html
http://www.eed.usv.ro/~ionelar/
2
teaching.html
John M. Zelle, Python Programming An Introduction to Computer Science(3rd ed)
Interpretor vs.
PCLP II :: Introducere în Python
Compilator (1)
▪ Limbaj de nivel înalt:
▪ c = a+b
▪ Acestă intrucțiune trebuie translatată în
calculator.
▪ Compilatoarele convertesc programele
mașină al sistemului.
http://www.eed.usv.ro/~ionelar/teaching.html
Interpretor vs.
PCLP II :: Introducere în Python
Compilator (2)
▪ Interpretoarele simulează înțelegera unui
limbaj de nivel înalt a calculatoarelor;
▪ Sursa programului nu este translatată în
limbaj mașină;
▪ Un interpretor analizează și execută codul
sursă intrucțiune cu instrucțiune.
John M. Zelle, Python Programming An Introduction to Computer Science(3rd ed)
4
http://www.eed.usv.ro/~ionelar/teaching.html
Interpretor vs.
PCLP II :: Introducere în Python
Compilator (3)
▪ O dată ce un program este compilat, el poate fi
programului.
▪ În general, programele compilate rulează mai rapid
http://www.eed.usv.ro/~ionelar/
6
teaching.html
Crearea unui
PCLP II :: Introducere în Python
fișier Python
▪ Crearea unui fișier cu extensia .py,
folosind un editor de texte sau din consola
Python
http://www.eed.usv.ro/~ionelar/
7
teaching.html
http://www.eed.usv.ro/~ionelar/
8
teaching.html
http://www.eed.usv.ro/~ionelar/teaching.html
▪ Sintaxa:
print(object(s), separator=separator, end=end,
file=file, flush=flush)
object(s) Any object, and as many as you like. Will be converted to string before printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one.
Default is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)
file Optional. An object with a write method. Default is sys.stdout
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False).
Default is False
Scrierea - print()
PCLP II :: Introducere în Python9
▪ Numere complexe
http://www.eed.usv.ro/~ionelar/ http://www.eed.usv.ro/~ionelar/
teaching.html teaching.html
12
Variabile.
Tipuri de
date
PCLP II :: Introducere în Python13
Tipuri de
http://www.eed.usv.ro/~ionelar/
teaching.html
Variabile. date
PCLP II :: Introducere în Python14
▪ float()
▪ str()
http://www.eed.usv.ro/~ionelar/
15
teaching.html
▪ Aritmetici
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
http://www.eed.usv.ro/~ionelar/
16
teaching.html
Operatori
▪ Atribuirea multiplă
PCLP II :: Introducere în Python18
http://www.eed.usv.ro/~ionelar/teaching.html
Operatori
▪ Comparare
== Equal x == y
!= Not equal x != y
20
21
22
23
▪for
▪while
PCLP II :: Introducere în Python24
http://www.eed.usv.ro/~ionelar/
teaching.html
Structuri de
control http://www.eed.usv.ro/~ionelar/
teaching.html
IF...ELSE
http://www.eed.usv.ro/~ionelar/
teaching.html
27
WHILE
PCLP II :: Introducere în Python29
http://www.eed.usv.ro/~ionelar/
teaching.html
FOR http://www.eed.usv.ro/~ionelar/
teaching.html
PCLP II :: Introducere în Python28
WHILE
▪ Buclă infinită http://www.eed.usv.ro/~ionelar/
teaching.html
Șiruri de
caractere
▪ While ... else
PCLP II :: Introducere în Python30
▪ Extragerea unui
caracter
▪ Extragerea unui
subșir
PCLP II :: Introducere în Python31
http://www.eed.usv.ro/~ionelar/ http://www.eed.usv.ro/~ionelar/
teaching.html teaching.html
32
Șiruri de
caractere
▪ Transformarea în
minuscule
▪ Transformarea
majuscule
în
caractere
PCLP II :: Introducere în Python33
▪ Împărțirea în
subșiruri
▪ Transformarea în
majuscule
PCLP II :: Introducere în Python34
http://www.eed.usv.ro/~ionelar/
teaching.html
Șiruri de
http://www.eed.usv.ro/~ionelar/
teaching.html
▪ Formatarea
șirurilor
Șiruri de PCLP II :: Introducere în Python35
caractere
PCLP II :: Introducere în Python Colecții
(Vectori)
36
Liste
▪ Accesarea unui
element
▪ Modificarea unui
element
PCLP II :: Introducere în Python38
http://www.eed.usv.ro/~ionelar/
teaching.html
Liste
PCLP II :: Introducere în Python37
http://www.eed.usv.ro/~ionelar/ http://www.eed.usv.ro/~ionelar/
teaching.html teaching.html
▪ Dimensiunea
liste
unei
Liste
Liste ▪ Adăugarea unui
element în listă
▪ Manipularea listei
folosind bucle ▪ APPEND
▪ Căutarea unui
element ▪ INSERT
PCLP II :: Introducere în Python40
PCLP II :: Introducere în Python39
http://www.eed.usv.ro/~ionelar/ http://www.eed.usv.ro/~ionelar/
teaching.html teaching.html
41
Liste
▪ Ștergerea întregii
liste
PCLP II :: Introducere în Python42
http://www.eed.usv.ro/~ionelar/
teaching.html
▪ LIST
▪ COPY Liste
Liste – metode
PCLP II :: Introducere în Python
predefinite
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
http://www.eed.usv.ro/~ionelar/teaching.html
sort() Sorts the list
44
http://www.eed.usv.ro/~ionelar/teaching.html
Liste
liste.py
PCLP II :: Introducere în Python45
[out]:
46
Lista comprehensivă
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Citirea vectorului de la tastatura
[out]:
PCLP II :: Introducere în Python47
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
PCLP II :: Introducere în Python48
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
PCLP II :: Introducere în Python49
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Definire matrice
[out]:
PCLP II :: Introducere în Python50
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Definire matrice
[out]:
PCLP II :: Introducere în Python51
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Definire matrice
[out]:
PCLP II :: Introducere în Python52
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Definire matrice
[out]:
PCLP II :: Introducere în Python53
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
Liste (tablouri)
▪ Citire matrice de la tastatura
PCLP II :: Introducere în Python54
tuple)
▪ Elementele sunt liste)http://www.eed.usv.ro/~ion
elar/teaching.html
incadrate intre () (vs. 55
[] –
PCLP II :: Introducere în Python Tuple
No. Function with Description
http://www.eed.usv.ro/~ionelar/ [out]:
teaching.html
56
Tuple
http://www.eed.usv.ro/~ionelar/
teaching.html
http://www.eed.usv.ro/~ionelar/
58
teaching.html
No. Methods with Description
1 dict.clear()Removes all elements of dictionary dict
2 dict.copy()Returns a shallow copy of dictionary dict
3 dict.fromkeys()Create a new dictionary with keys from seq and
values set to value.
4 dict.get(key, default=None)For key key, returns value or default
if key not in dictionary
5 dict.has_key(key)Returns true if key in dictionary dict, false
otherwise
6 dict.items()Returns a list of dict's (key, value) tuple pairs
7 dict.keys()Returns list of dictionary dict's keys
8 dict.setdefault(key, default=None)Similar to get(), but will set
dict[key]=default if key is not
already in dict
9 dict.update(dict2)Adds dictionary dict2's key-values pairs to dict
10 dict.values()Returns list of dictionary dict's values
http://www.eed.usv.ro/~ionelar/teaching.html
Dicționare
PCLP II :: Introducere în Python59
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
Dicționare
PCLP II :: Introducere în Python60
http://www.eed.usv.ro/~ionelar/teaching.html
Dicționare
PCLP II :: Introducere în Python61
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
Dicționare
PCLP II :: Introducere în Python62
63
64
http://www.eed.usv.ro/~ionelar/
x=?
PCLP II :: Introducere în Python Funcții
▪ Încadrul listei de argumente pot fi
transmise argumente cu valoare implicită
http://www.eed.usv.ro/~ionelar/ http://www.eed.usv.ro/~ionelar/
teaching.html teaching.html
65
Funcții
http://www.eed.usv.ro/~ionelar/
PCLP II :: Introducere în Python66
teaching.html
Funcții
[out]:
▪ Afișarea
comentariilor din
funcție:
http://www.eed.usv.ro/~ionelar/ [out]:
teaching.html
PCLP II :: Introducere în Python68
Funcții
PCLP II :: Introducere în Python67
teaching.html
69
http://www.eed.usv.ro/~ionelar/
[out]:
[out]:
http://www.eed.usv.ro/~ionelar/
70
teaching.html
http://www.eed.usv.ro/~ionelar/teaching.html
Module (import)
▪ Utilizarea unei functii din alt modul
[out]:
PCLP II :: Introducere în Python71
http://www.eed.usv.ro/~ionelar/teaching.html
[out]:
http://www.eed.usv.ro/~ionelar/teaching.html
1023+*
push(3)
pop(3)
3 push(50)
push(50)
pop(2)
push(5) push(10)
push(5) 102 + 3 = 5
1010 * 5 = 50
pop(5) 1010 * 5 = 50
pop(5) 50
pop(5) 50
50
push(2) pop(50)
25 pop(50)
pop(10)
pop(50) http://www.eed.usv.ro/~ionelar/
teaching.html
PCLP II :: Introducere în Python77
76
http://www.eed.usv.ro/~ionelar/
teaching.html
not exist.
't' Open in text mode. (default)
'b' Open in binary mode.
'+' Open a file for updating (reading and writing)
http://www.eed.usv.ro/~ionelar/teaching.html