introducere in programare folosind python · 2020. 3. 11. · bibliotecamath python mathematics...

75
SPAM Dr. Elena Ovreiu Universitatea POLITEHNICA din Bucuresti www.ovreiu.com INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON

Upload: others

Post on 22-Jan-2021

20 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

SPAM

Dr. Elena OvreiuUniversitatea POLITEHNICA din Bucurestiwww.ovreiu.com

INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON

Page 2: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

CURS 2 –TIPURI DE DATE

Page 3: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date• Informatiile stocate si manipulate de programele

software sunt numite date• Exista 2 tipuri diferite de numere:• (5, 4, 3, -6) sunt numere intregi, nu au parte

fractionara• (.25, .10, .05, .01) sunt fractii zecimale

Page 4: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date• In memoria unui computer, numerele intregi si cele

zecimale sunt reprezentate diferit. • Spunem ca numerele intregi si cele zecimale sunt

tipuri de date diferite (data type)• Tipul de date al unui obiect determina ce valoari

poate avea si ce tipuri de operatii pot fi executate cu acel obiect

Page 5: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: int, float

• Numerele intregi sunt reprezentate folosind tipul de date integer (int)

• Valorile sunt toate numerele intregi pozitive sinegative

• Numerele care au parte zecimala sunt reprezentateca floating points (float)

Page 6: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: int#tipul intreg: int

(10, 119, -10)

• a=10: variabilei a i se atribuie valoarea 10

• Pentru a afla tipul unei variabile, folosim functia type()

<type 'int'>

Page 7: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: float #tipul zecimal: float

(32.5, -15.9, 10.0)

<type 'float’>

<type 'int'>

<type 'float'>

Page 8: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Functia type()

Python are o functie speciala care ne spune tipul de date al unei valori: type()>>> type(3)<class 'int'>>>> type(3.1)<class 'float'>>>> type(3.0)<class 'float'>>>> var = 32>>> type(var)<class 'int'>

Page 9: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: int, floatDe ce este nevoie de 2 tipuri de numere?• Unele valori nu pot fi zecimale (cum ar fi iteratia

la for)• Cei mai multi algoritmi matematici sunt eficienti

cand folosesc numere intregi• Tipul float stocheaza doar o aproximare a

numarului real ce trebuie reprezentat• Din moment ce float este doar o aproximatie,

este bine sa folosim int de fiecare data cand e posibil

Page 10: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: complex, binar, hexazecimal

(3+5j)<type 'complex'>

e= 10<type 'int'>

f= 255<type 'int'>

Page 11: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date Boolean

<type 'bool'>TrueFalse

Page 12: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de dateOperatiile intre numere de tip int produc int, operatiile intrefloat produc float. Operatiile intre int si float vor produce rezultat float

7.0<type 'float'>

7<type ‘int’'>

12.012.0

12

Page 13: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date

3

3.3333333333

3.33333333333

3.33333333333

Page 14: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date

• Impartirea intreaga produce un numar intreg!

a=10

Page 15: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date

• Ce se intampla daca avem operatii care contin int sifloat?

• x = 5.0 * 2

Page 16: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date• Ca Python sa evalueze aceasta expresie, fie

converteste 5.0 in 5, fie 2 in 2.0. • Conversia unui float la int se face cu pierdere de

informatie (de ex. 5.4 convertit in int devine 5)• int pot fi convertite la float prin adaugarea .0 (2

devine 2.0)

Page 17: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date>>> float(22/5)4.0 (22/5=4; float(4)=4.0 ) >>> int(4.5)4>>> int(3.9)3>>> round(3.9)4>>> round(3)3

Page 18: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date

• Daca se doreste rotunjirea unui float intr-un alt float, atunci functia round() mai primeste un parametru ca argument care indica numarul de zecimale duparotunjire.

3.163.14

Page 19: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date>>> int("32")

32

>>> float("32")

32.0

• Functiile int si float sunt folosite ca o alternative sigura pentru functia eval

Page 20: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversia tipului de date• Pentru a converti din int in binar, se foloseste functia

bin()

0b1010• Pentru a converti din int in hexazecimal, se foloseste functia

hex().

0xff

Page 21: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date string■ Cea mai comuna aplicatie a PC-urilor este procesarea

cuvintelor. ■ Textul este reprezentat in programe prin tipul de date string

■ Un sir este o secventa de caractere incadrada de ghilimele(“ “) sau apostrofuri (‘ ’)

Page 22: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date string

('Hello', 'SPAM’)

>>> type(str1)<class 'str'>>>> type(str2)<class 'str'>

Page 23: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date string

This is the Python courseThis isthe Pythoncourse

Page 24: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String -indexarea

■ Pozitiile caracterelor intr-un sir sunt numerotate de la stanga, incepand cu 0 (incluzand spatiile)

■ Forma generala este <string>[<expr>], unde valoarealui expr reprezinta ce character din sir va fi selectat.

Page 25: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String -indexarea

>>> sir = "Hello SPAM"

>>> sir[0]

'H'

>>> print(sir[0], sir[2], sir[4])

H l o

>>> x = 8

>>> print(sir[x - 2])

>>> P

H e l l o P A M

0 1 2 3 4 5 6 7 8

S

Page 26: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String -indexarea

■ Intr-un sir de n caractere, ultimul caracter este pe pozitia n-1.

■ Putem indexa din dreapta folosind idecsi negativi>>> str[-1]

‘M'

>>> str[-3]

‘P'

H e l l o P A M

0 1 2 3 4 5 6 7 8

S

Page 27: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date string§ Concatenarea – Pune impreuna 2 siruri (+)

§ Repetarea (*)

Hello SPAMHello SPAM

Hello SPAMHello SPAMHello SPAM

H

l HelloSPAM

Page 28: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Lungimea unui sir: len()

10 (caracterele sirului + spatii )

• Un sir incepe cu indexul=0 si se termina cu indexul len(sir) -1

Page 29: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: slicing

-> This is the Python course

-> This is the Python course

-> This is-> rs

Elementul de pe pozitia -1 este ultimul caracter, penultimul caractar este pe pozitia -2, etc s[-3:-1] va afisa caracterele de pe pozitiile -3 si -2

Putem accesa o secventa continua de caractere, numita substring, printr-un process numit feliere (slicing)

-> This is

Page 30: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: slicing

Ti st• Afisarea se face din 2 in 2, incepand de la 0 pana la indexul 9, inclusiv

esruoc nohtyP eht si sihT• Caracterele sirului sunt afisate in ordine inversa (-1 ne arata pasul; semnul indicaodinea inversa)• Ce instructiune mai poate fi folosita pentru a afisa caracterele sirului in ordine inversa?

Page 31: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: slicing

§ Ce instructiune mai poate fi folosita pentru a afisa caracterelesirului in ordine inversa?

Slicing: <string>[<start>:<end>]start si end sunt ambele intIn urma procesului de slicing, se extrage subsirul care incepe la startdar nu include si pozitia end

Page 32: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: slicing

■ Daca lipseste o expresie (start sau end), atunci se foloseste inceputul sau sfarsitul sirului (depinde unde sunt puse :)

H e l l o B o b

0 1 2 3 4 5 6 7 8

str=“Hello Bob”

Page 33: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: slicing>>> str[0:3]

'Hel'

>>> str[5:9]

' Bob'

>>> str[:5]

'Hello'

>>> str[5:]

' Bob'

>>> str[:]

'Hello Bob'

Page 34: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipul de date string

Operator Meaning+ Concatenation* Repetition

<string>[] Indexing<string>[:] Slicinglen(<string>) Length

Page 35: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: functia strip()• Functia strip() elimina spatiile de la inceputul si sfarsitul

sirului.

This is the Python course• Functia lstrip() elimina spatiile de la inceputul sirului (l -left)• Functia rstrip() elimina spatiile de la sfarsitul sirului (r -right)

This is the Python course

Page 36: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: find()• find() gaseste un subsir in sir si returneaza pozitia unde

incepe subsirul

15

• 0 este indexul unde dorim sa incepem cautarea

• len(s) indexul unde dorim sa oprim cautarea

Page 37: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: find(), rfind()• Daca subsirul nu poate fi gasit, atunci functia find()

returneaza -1

814

Page 38: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: count(), replace()• count() numara aparitiile unui subsir intr-un sir.

• replace(old_sub, new_sub) inlocuieste un subsir cu un altul

This is zhe Pyzhon course

Page 39: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: upper, lower, title, capitalize

■ THIS IS THE PYTHON COURSE

■ this is the python course

■ This Is The Python Course

■ This is the python course

Page 40: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: split■ s.split() – imparte sirul s intr-o lista de subsiruri

■ Separarea se face in functie de parametrul pe care il primeste functia split(). In exemplul acesta, separarea este facuta in functie de spatiu

['This', 'is', 'the', 'Python', 'course']

Page 41: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

String: split

['Hello', ' my name is Ana', ' I am 26']

['Hello', 'string', 'methods!']['1', '2', '3', '4', '5', '6', '7']

Page 42: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Reprezentarea sirurilor■ In memoria computerelor, sirurile sunt reprezentate ca

secvente de 0 si 1, exact ca numerele.

■ Un sir este stocat ca o secventa de numere binare, un numar pentru fiecare caracter.

■ Nu conteaza ce valoare are atribuita.

Page 43: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Reprezentarea sirurilor■ La inceputurile computerelor, fiecare producator a folosit

propriul sistem de codare a caracterelor. ■ S-a introdus standardizarea sistemului de codare: standardul

ASCII (American Standard Code for Information Interchange): foloseste numere de la 0 la 127 pentru a reprezenta caracterelede la tastatura –cea americana (A-Z: 65-90; a-z:97-122)

■ Dezavantaj: imposibilitatea de a reprezenta caractere din altelimbi

Page 44: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Reprezentarea sirurilor

■ Sistemele modernesc folosesc Unicode, care include caractere din aproape toate limbile.

■ Python suporta standardul Unicode, care suporta peste100.000 de caractere.

■ Functia ord(str) returneaza codul numeric al unui singurcharacter (str)

■ Functia chr(nr_int) returneaza caracterul care corespundenumarului intreg nr_int

Page 45: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Functiile ord(), chr()>>> ord("A")

65

>>> ord("a")

97

>>> chr(97)

'a'

>>> chr(65)

'A'

Page 46: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: liste• Listele sunt colectii de date neomogene (date de tipuri diferite)

[] lista goala

[10, 15, -10, 'SPAM', 15.5]

• Functia slicing() se aplica si la liste

Page 47: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Tipuri de date: liste

5[10, 15, -10, 'SPAM', 15.5, 10, 15, -10, 'SPAM', 15.5, 10, 15, -10, 'SPAM', 15.5][-10, 'SPAM']

Page 48: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Liste: adaugare si eliminare de elemente• append() adauga un element la sfarsitul listei (nu mai

multe)

• remove() elimina un element din lista

[10, 15, -10, 'SPAM', 15.5, 20]

15, -10, 'SPAM', 15.5, 20]

Page 49: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Liste: adaugare si eliminare de elemente

Traceback (most recent call last):File "/Users/elenaovreiu/Documents/listetype.py",

line 9, in <module>l1.remove('spam')

ValueError: list.remove(x): x not in list

• Putem elimina elemente din lista si folosind indexul elementului pe care il dorim eliminat

Page 50: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Liste: adaugare si eliminare de elemente

[15, -10, 'SPAM', 15.5]

Page 51: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Liste: adaugare si eliminare de elemente• Putem elimina elemente din lista si folosind indexul

elementului pe care il dorim eliminat• clear() elimina toate elementele din lista• max() returneaza elementul maxim din lista

Page 52: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Conversii de tip

• Utilizarea functiei int in loc de eval asigura ca utilizatorul nu poate introduce decat valoari intregi. Daca se introduce alte tipuri de valori, programulcrapa.

• Dezavantaj: aceasta functie nu accepta input-urisimulatane.

Page 53: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Biblioteca Math

• O bilblioteca este un modul care contine functii. • Sa se scrie un program care calculeaza radacinile

unei ecuatii de gradul 2:

2 42

b b acxa

- ± -=

Page 54: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Biblioteca Math

• Ca sa folosim biblioteca math, trebuie sa ne asiguram ca avem aceasta linie in program:

import math• Importand o biblioteca inseamna ca toate functiile

din biblioteca respective devin disponibileprogramului.

Page 55: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Biblioteca Math

• Ca sa putem accesa functia sqrt din libraria math, scriem: math.sqrt(x)

• Folosind notatia cu . ii spunem lui Python ca functiasqrt se gaseste in modulul math.

• Pentru a calcula radacina, este suficent sa scriem:root = math.sqrt(b*b – 4*a*c)

Page 56: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

import math

def main():

print("This program finds the real solutions to a quadratic")

print()

a, b, c = eval(input("Please enter the coefficients (a, b, c): "))

Root = math.sqrt(b * b - 4 * a * c)

root1 = (-b + Root) / (2 * a)

root2 = (-b - Root) / (2 * a)

print()

print("The solutions are:", root1, root2 )

Page 57: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Biblioteca Math

Python Mathematics English

pi An approximation of pi

e e An approximation of e

sqrt(x) The square root of x

sin(x) sin x The sine of x

cos(x) cos x The cosine of x

tan(x) tan x The tangent of x

asin(x) arcsin x The inverse of sine x

acos(x) arccos x The inverse of cosine x

atan(x) arctan x The inverse of tangent x

Page 58: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Biblioteca Math

Python Mathematics English

log(x) ln x The natural (base e) logarithm of xlog10(x) The common (base 10) logarithm of x

exp(x) The exponential of x

ceil(x) The smallest whole number >= xfloor(x) The largest whole number <= x

Page 59: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar• Factorialul este definit ca:

n! = n(n-1)(n-2)…(1)• 6!= 6*5*4*3*2*1 = 720• Cum putem scrie un program care sa calculeze

factorialul unui numar ?• Input numarul al carui factorial in calculam, n• Calculam factorialul lui n: il punem in variabila fact• Output: print fact

Page 60: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• Cum calculam 6! ?• 6*5=30• Apoi, 30*4=120• 120*3=360• 360*2=720• 720*1=720

Page 61: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• Facem multiplicari repetate si memoram produsulrezultat.

• Acest tip de algoritm se numeste acumulator: deoarece aculmulam rezultatul intr-o variabila, numita variabila acumulator.

Page 62: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• Structura generala a unui algoritm de acumulareeste: • Initializam variabila accumulator• Bucla pana la rezultatul final• Update variabila acumulator

Page 63: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• Avem nevoie de o bucla!fact = 1

for factor in [6, 5, 4, 3, 2, 1]:fact = fact * factor

• De ce am initializat fact = 1?

Page 64: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• La fiecare iteratie, valoarea anterioara a lui facteste folosita pentru a calcula urmatoarea valoare a lui fact. Facand initializarea, i se seteaza lui factvaloarea initiala.

• Deoarece inmultirea este asociativa si comutativa, putem rescrie programul in felul urmator:

Page 65: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

fact = 1

for factor in [2, 3, 4, 5, 6]:fact = fact * factor

• range(n) returneaza 0, 1, 2, 3, …, n-1• range contine un parametru optional: range(start, n)

care genereaza: start, start + 1, …, n-1

Page 66: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• range(, strat, n, step) returneaza start, start+step, …, n-1

• list(<sequence>) genereaza o lista>>> list(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Page 67: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

>>> list(range(5,10))[5, 6, 7, 8, 9]>>> list(range(5,10,2))[5, 7, 9]

Page 68: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar

• range(2, n+1) -> 2, 3,…n• range(n, 1, -1) -> n, n-1, ..2

Page 69: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar# factorial.py

def main():

n = eval(input("Please enter a whole number: "))

fact = 1

for factor in range(n,1,-1):

fact = fact * factor

print("The factorial of", n, "is", fact)

main()

Page 70: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Factorialul unui numar>>> main()

Please enter a whole number: 100

The factorial of 100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

• Versiunile noi de Python pot rula valori mari, nu siversiunile mai vechi:

Page 71: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32

>>> main()

Please enter a whole number: 13

13

12

11

10

9

8

7

6

5

4

Traceback (innermost last):

File "<pyshell#1>", line 1, in ?

fact.main()

File "C:\PROGRA~1\PYTHON~1.2\fact.py", line 5, in main

fact=fact*factor

OverflowError: integer multiplication

Page 72: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Limitele lui int

• Desi este un numar nelimitat de numere intregi, doar un numar limitat pot fi reprezentate.

• Acest numar este dat de numarul de biti pe care un CPU il foloseste pentru a reprezenta o valoareintreaga.

• PCs obisnuite folosesc 32 sau 64 bits.• Aceasta inseamna 232 numere, centrate in 0.

Page 73: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Limitele lui int

• Range-ul este astfel –231 to 231-1. • 100! este mult mai mare decat 231-1• Daca initializam fact=1.0 obtinem: >>> main()Please enter a whole number: 30The factorial of 30 is 2.652528598121911e+32

Page 74: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Reprezentarea numerelor foarte mari

• Numerele foarte mari si foarte mici sunt reprezentate in format stiintific sau exponential.

• 2.652528598121911e+32 inseamna2.652528598121911 * 1032

>>> main()Please enter a whole number: 100The factorial of 100 is 9.332621544394418e+157

Page 75: INTRODUCERE IN PROGRAMARE FOLOSIND PYTHON · 2020. 3. 11. · BibliotecaMath Python Mathematics English pi An approximationof pi e e An approximation of e sqrt(x) The square root

Reprezentarea numerelor foarte mari

• Float este o aproximare• Ne permite sa reprezentam numere mari, dar cu

precizie fixa• Versiunile noi de Python extind reprezentarea

numerelor intregi atat cat este necesar. Singuralimitare fiind memoria computerului.