Introducere - Introduction
Iată principalele tipuri de date încorporate în Python - Here are the main built-in data types in Python:
- Tipuri numerice
- Tipuri text
- Tipuri boolean
- Tipuri secvență
- Tipuri Dicționar
- Tipuri Set
- Tipuri binare
- None Type
- Numeric types
- Text types
- Boolean types
- Sequence types
- Dictionary types
- Set types
- Binary types
- None Type
Python dispune de mai multe tipuri de date integrate, disponibile imediat, fără a necesita instrucțiuni de import. Aceste tipuri sunt clasificate, în linii mari, în: tipuri numerice, secvențe de text, secvențe, mapări, mulțimi, secvențe binare și tipuri de valoare de adevăr.
Python has several built-in data types available out of the box without requiring any import statements. These types are broadly categorized into numeric, text sequence, sequence, mapping, set, binary sequence, and truth value types.
Prezentare generală a tipurilor încorporate din Python - Overview of Python Built-in Types
| Categorie | Nume tip | Mutabilitate | Exemple |
|---|---|---|---|
| Numeric Types | int, float, complex | Immutable | 42, 3.14, 2+3j |
| Text Sequence | str | Immutable | "Hello" |
| Boolean Type | bool | Immutable | True, False |
| Sequence Types | list, tuple, range | list (Mutable), Others (Immutable) | [1, 2], (1, 2), range(5) |
| Mapping Type | dict | Mutable | {"key": "value"} |
| Set Types | set, frozensets | set (Mutable), frozenset (Immutable) | {1, 2}, frozenset([1, 2]) |
| Binary Sequence | bytes, bytearray, memoryviewby | tearray (Mutable), Others (Immutable) | b"hi", bytearray(5) |
| None Type | NoneType | Immutable | None |
Tipurile de date din Python clasifică tipul de valori pe care o variabilă le poate conține, determinând modul în care sunt stocate datele și ce operații pot fi efectuate.
În Python, fiecare valoare este un obiect, iar toate obiectele au un tip de date. Tipurile de date sunt clasificate în două grupuri principale în funcție de mutabilitate sau dacă starea lor poate fi modificată după ce sunt create.
Python, un limbaj de tipizare dinamică, deduce automat tipul de date al unei variabile pe baza valorii atribuite acesteia. Puteți determina tipul de date al oricărei variabile sau obiect folosind funcția type() încorporată.
Python's data types categorize the kind of values a variable can hold, determining how data is stored and what operations can be performed.
In Python, every value is an object, and all objects have a data type. Data types are categorized into two main groups based on mutability, or whether their state can be changed after they are created.
Python, a dynamically typed language, automatically infers the data type of a variable based on the value assigned to it. You can determine the data type of any variable or object using the built-in type() function.
Funcția type() din Python are două scopuri principale - type() function in Python serves two main purposes.
- Pentru a obține tipul unui obiect.
- Când este apelată cu un singur argument, type(object), aceasta returnează tipul sau clasa obiectului respectiv.
- To get the type of an object.
- When called with a single argument, type(object), it returns the type or class of that object.
animal = "vulpe"
print(type(animal)) ==> <class 'str'>
- Pentru a crea o clasă dinamic.
- Apelată cu trei argumente, type(name, bases, dict), permite crearea de noi clase (types) în timpul execuției. Aceasta este o caracteristică mai avansată utilizată în metaprogramare.
- To create a class dynamically.
- When called with three arguments, type(name, bases, dict), it allows for the creation of new classes (types) at runtime. This is a more advanced feature used in metaprogramming.
# Creați dinamic o clasă numită "ClasaMea"
# Aceasta moștenește de la "object" și are atributul "valoare"
ClasaMea = type('ClasaMea', (object,), {'valoare': 100})
# Creează o instanță a clasei create dinamic
obiect = ClasaMea()
print(obiect.valoare) ==> 100
print(type(obiect)) ==> <class '__main__.ClasaMea'>
- type() vs. isinstance() pentru verificarea tipului.
- Deși type() poate verifica tipul unui obiect, isinstance() este adesea preferată, în special în cazul moștenirii. type(obj) == SomeClass necesită o potrivire exactă a clasei, returnând False pentru subclase. În schimb, isinstance(obj, SomeClass) returnează True dacă obj este o instanță a SomeClass sau a oricăreia dintre subclasele sale, aliniindu-se mai bine cu principiile orientate pe obiecte.
- type() vs. isinstance() for type checking.
- While type() can check an object's type, isinstance() is often preferred, particularly with inheritance. type(obj) == SomeClass requires an exact class match, returning False for subclasses. In contrast, isinstance(obj, SomeClass) returns True if obj is an instance of SomeClass or any of its subclasses, aligning better with object-oriented principles.
For more details and examples, refer to the official Built-in Types documentation
For more details and examples, refer to the official Data types documentation
Click CSS pentru style sheet
Click JavaScript pentru style sheet
