This shows you the differences between two versions of the page.
ic:resurse:python [2020/10/02 15:39] acosmin.maria |
ic:resurse:python [2023/10/02 00:10] (current) razvan.smadu [Python3 Crash Course] |
||
---|---|---|---|
Line 1: | Line 1: | ||
===== Python3 Crash Course ===== | ===== Python3 Crash Course ===== | ||
+ | |||
+ | === Ce este Python? === | ||
+ | |||
+ | Python este un limbaj interpretat, ușor de folosit, în special datorită faptului că nu necesită compilare sau linkare. Interpretorul permite rularea de fișiere care conțin cod scris în Python, dar permite și rularea de cod în modul interactiv. În plus, este un limbaj perfect pentru prototipat idei și testat algoritmi, însă are dezavantajul de a rula mai lent decât limbaje precum C, C++ și Java. | ||
+ | |||
+ | Mediul de dezvoltare poate presupune utilizarea unui editor de text/IDE și al unul terminal, sau folosirea unor medii de dezvoltare bazate pe interfațe web, precum [[https://jupyter.org/index.html|Jupyter Notebooks]], [[https://research.google.com/colaboratory/|Google Colaboratory]] și [[https://www.kaggle.com/|Kaggle]]. | ||
+ | |||
+ | === Instalare Python 3 === | ||
+ | |||
+ | În linux este destul de simplu întrucât multe distribuții vin deja cu Python instalat. Pentru a verifica dacă aveți deja instalat Python pe sistemul vostru, puteți deschide un terminal și rula | ||
+ | <code> | ||
+ | python --version # OR python3 --version | ||
+ | </code> | ||
+ | |||
+ | In cazul în care comanda vă returnează un versiunea de python instalată (ex. Python 3.7.9), atunci nu mai trebui să faceți nimic. | ||
+ | |||
+ | <note> Recomandăm să aveți instalat Python 3, întrucât Python 2 nu mai are [[https://www.python.org/doc/sunset-python-2/|suport oficial]] de la 1 ianuare 2020, iar sintaxa între cele două versiuni diferă. </note> | ||
+ | |||
+ | În cazul în care nu aveți Python 3 instalat, puteți folosi managerul de pachete (ex. apt, yum), [[https://docs.conda.io/en/latest/miniconda.html|conda]], sau puteți descărca direct de pe [[https://www.python.org/downloads/|site-ul oficial]]. | ||
+ | |||
+ | În Windows, puteți descărca de pe [[https://www.microsoft.com/en-us/p/python-39/9p7qfqmjrfp7|Microsoft Store]], puteți folosi [[https://docs.conda.io/en/latest/miniconda.html|conda]], sau puteți descărca direct de pe [[https://www.python.org/downloads/|site-ul oficial]]. | ||
+ | |||
=== Command Line (CLI) === | === Command Line (CLI) === | ||
- | Start REPL (Read Execute Print Repeat) interactive mode and exit: | + | Pornirea modului interactiv REPL (Read Execute Print Repeat) și ieșirea din el: |
<code python> | <code python> | ||
shell$ python # OR python3 | shell$ python # OR python3 | ||
Line 11: | Line 33: | ||
</code> | </code> | ||
- | Execute files: | + | Executarea fișierelor: |
<code python> | <code python> | ||
shell$ python filename.py | shell$ python filename.py | ||
Line 20: | Line 42: | ||
=== Basics === | === Basics === | ||
- | Variables and numerical data types: | + | Variabile și tipuri de date numerice: |
<code python> | <code python> | ||
a = 10 # int | a = 10 # int | ||
b = 10.2 # float | b = 10.2 # float | ||
- | c = None # NULL value from Python | + | c = None # NULL value in Python |
</code> | </code> | ||
- | Strings and string formatting: | + | Stringuri și formatarea strigurilor |
<code python> | <code python> | ||
s1 = "Ana are mere" | s1 = "Ana are mere" | ||
Line 36: | Line 58: | ||
</code> | </code> | ||
- | Functions: | + | Funcții: |
<code python> | <code python> | ||
def my_function(a, b): | def my_function(a, b): | ||
Line 42: | Line 64: | ||
</code> | </code> | ||
- | **!Note:** In Python we don't use curly braces. Instead, we use indentation to define the scope of a function/statement. | + | **!Notă:** În Python nu avem acolade (ca în limbajele C-like). În schimb, folosim identarea pentru a defini scopul unei funcții/instrucțiuni. |
=== Input/Output === | === Input/Output === | ||
Line 55: | Line 77: | ||
</code> | </code> | ||
- | Files: | + | Fișiere: |
<code python> | <code python> | ||
f = open("path/to/file.txt") # default behaviour is 'read text' | f = open("path/to/file.txt") # default behaviour is 'read text' | ||
Line 70: | Line 92: | ||
</code> | </code> | ||
- | === Operators & Flow Control === | + | === Operații === |
- | * +, +=, -, -=, *, *=, /, /= | + | * Aritmetice: +, +=, -, -=, *, *=, /, /= |
- | * ==, !=, <, <=, >, >= | + | * Logice: ==, !=, <, <=, >, >= |
<code python> | <code python> | ||
Line 82: | Line 104: | ||
</code> | </code> | ||
- | === Conditional Statements & Loops === | + | === Structuri condiționale și repetitive === |
- | We can use most of the keywords from C: | + | Putem folosi majoritatea cuvintelor cheie (keywords) din C: |
<code python> | <code python> | ||
i = 0 | i = 0 | ||
Line 102: | Line 125: | ||
</code> | </code> | ||
- | **!Note:** We use 'and' and 'or' to combine 2 logical statements. | + | **!Notă:** Folosim 'and' și 'or' pentru a combina 2 structuri logice |
- | === Data Structures === | + | === Structuri de date === |
- | Lists: | + | Liste: |
<code python> | <code python> | ||
l1 = [1, 2, 3] | l1 = [1, 2, 3] | ||
Line 120: | Line 143: | ||
</code> | </code> | ||
- | Dictionaries: | + | Dicționare: |
<code python> | <code python> | ||
dictio = {} | dictio = {} | ||
Line 130: | Line 153: | ||
</code> | </code> | ||
- | Tuples: | + | Tupluri: |
<code python> | <code python> | ||
t = (1, 2, 3) # same as lists, but immutable | t = (1, 2, 3) # same as lists, but immutable | ||
</code> | </code> | ||
- | Sets are like dictionaries in terms of complexity, but they store only the "keys". | + | Seturile sunt similare cu dicționarele în termeni de complexitate, dar stocheaza doar cheile. |
<code python> | <code python> | ||
s = {1, 2, 3} | s = {1, 2, 3} | ||
2 in s # True O(1) time complexity | 2 in s # True O(1) time complexity | ||
+ | unique_elements = list(set([1, 1, 2, 2, 3, 3])) # unique_elements = [1, 2, 3] | ||
</code> | </code> | ||
- | === Object Oriented Programming === | + | === Programare Orientată pe Obiecte === |
<code python> | <code python> | ||
Line 160: | Line 184: | ||
</code> | </code> | ||
- | === Built-in Functions === | + | === Funcții Built-in === |
== range(start, stop, step) == | == range(start, stop, step) == | ||
Line 171: | Line 195: | ||
</code> | </code> | ||
- | In Python 2.x range() generates first the entire list of numbers and then iterates through it. | + | În Python 2.x, range() prima dată genereaza întreaga lista de numere, iar apoi iterează prin ele. |
<code python> | <code python> | ||
for i in range(5): # equivalent to: for i in [0, 1, 2, 3, 4] | for i in range(5): # equivalent to: for i in [0, 1, 2, 3, 4] | ||
</code> | </code> | ||
- | The alternative is the xrange() function which uses a generator to generate numbers as you need them. Python 3.X range() function is the xrange() function from Python 2.x. | + | Alternativa este funcția xrange() care se folosește de un generator pentru a genera numere, la nevoie. Funcția range() din Python 3.x este echivalentul funcției xrange() din Python 2.x. |
<code python> | <code python> | ||
def xrange(start, stop, step): | def xrange(start, stop, step): | ||
Line 201: | Line 225: | ||
</code> | </code> | ||
- | Other useful Built-In functions: abs(), all(), any(), max(), min(), zip(), type(), sum(), str(), chr(), ord(), sorted(), round(), reversed(), int(), float() | + | Alte [[https://docs.python.org/3/library/functions.html|funcții built-in utile]]: abs(), all(), any(), max(), min(), zip(), type(), sum(), str(), chr(), ord(), sorted(), round(), reversed(), int(), float() |
=== Comprehensions === | === Comprehensions === | ||
Line 217: | Line 241: | ||
</code> | </code> | ||
- | === Libraries and Imports === | + | === Biblioteci și Importuri === |
<code python> | <code python> | ||
python -m pip install packagename # Windows | python -m pip install packagename # Windows | ||
Line 234: | Line 258: | ||
</code> | </code> | ||
- | ==== References ==== | + | === Type Hints === |
- | * Official documentation: [[https://www.python.org/|here]] | + | |
+ | Python este un limbaj tipat dinamic. Pentru a ușura întelegerea codului, putem folosi type hints care reprezintă adnotări asupra tipurilor de date. Acestea nu sunt forțate la runtime, fiind responsabilitatea programatorului să se asigure că tipurile nu induc în eroare. Detalii legate de type hints se pot găsi în [[https://docs.python.org/3/library/typing.html|documentație]], [[https://peps.python.org/pep-0484/|PEP 484]] și [[https://peps.python.org/pep-0483/|PEP 483]]. | ||
+ | |||
+ | Exemple: | ||
+ | <code python> | ||
+ | from typing import Dict, List, Sequence, Tuple, TypeVar | ||
+ | |||
+ | my_string: str = "hello world" # A variable storing a string | ||
+ | my_number: int = 10 # A variable storing an integer | ||
+ | |||
+ | # A function returning nothing | ||
+ | def main() -> None: | ||
+ | return # Returns nothing (i.e., None) | ||
+ | |||
+ | # A function that takes some arguments, and returns a tuple | ||
+ | def compute(a: int, b: float, c: List[str]) -> Tuple[int, float]: | ||
+ | return a, b # This is a tuple of `a` and `b` | ||
+ | |||
+ | T = TypeVar('T') # Declare type variable | ||
+ | |||
+ | def first(l: Sequence[T]) -> T: # A generic function | ||
+ | return l[0] | ||
+ | |||
+ | # Type alias | ||
+ | MyCustomType = Dict[str, List[Tuple[int, int]]] | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Referințe ==== | ||
+ | * Documentația oficială: [[https://www.python.org/|here]] | ||
* Tutorial: [[https://www.tutorialspoint.com/python/index.htm|here]] | * Tutorial: [[https://www.tutorialspoint.com/python/index.htm|here]] | ||
* Cheatsheet: [[https://www.pythoncheatsheet.org/|here]] | * Cheatsheet: [[https://www.pythoncheatsheet.org/|here]] | ||
* Book: [[https://www.pdfdrive.com/python-crash-coursepdf-e33417142.html|here]] | * Book: [[https://www.pdfdrive.com/python-crash-coursepdf-e33417142.html|here]] | ||
- | * Online Course: [[https://www.coursera.org/specializations/python|here]] | + | * Curs online: [[https://www.coursera.org/specializations/python|here]] |