-
Notifications
You must be signed in to change notification settings - Fork 4
Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:
- Text Type:
str
- Numeric Types:
int
,float
,complex
- Sequence Types:
list
,tuple
,range
,str
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- Binary Types:
bytes
,bytearray
,memoryview
- None Type:
NoneType
You can get the data type of any object by using the type() function: Print the data type of the variable x:
x = 5
print(type(x))
In Python, the data type is set when you assign a value to a variable:
x00 = "Hello World" # str
x01 = 20 # int
x02 = 20.5 # float
x03 = 1j # complex
x04 = ["apple", "banana", "cherry"] # list
x05 = ("apple", "banana", "cherry") # tuple
x06 = range(6) # range
x07 = {"name": "John", "age": 36} # dict
x08 = {"apple", "banana", "cherry"} # set
x09 = frozenset({"apple", "banana", "cherry"}) # frozenset
x10 = True # bool
x11 = b"Hello" # bytes
x12 = bytearray(5) # bytearray
x13 = memoryview(bytes(5)) # memoryview
x14 = None # NoneType
If you want to specify the data type, you can use the following constructor functions:
x15 = str("Hello World") # str
x16 = int(20) # int
x17 = float(20.5) # float
x18 = complex(1j) # complex
x19 = list(("apple", "banana", "cherry")) # list
x20 = tuple(("apple", "banana", "cherry")) # tuple
x21 = range(6) # range
x22 = dict(name="John", age=36) # dict
x23 = set(("apple", "banana", "cherry")) # set
x24 = frozenset(("apple", "banana", "cherry")) # frozenset
x25 = bool(5) # bool
x26 = bytes(5) # bytes
x27 = bytearray(5) # bytearray
x28 = memoryview(bytes(5)) # memoryview
There are four collection data types in the Python programming language:
-
List
is a collection which is ordered and changeable. Allows duplicate members. -
Tuple
is a collection which is ordered and unchangeable. Allows duplicate members. -
Set
is a collection which is unordered, unchangeable*, and unindexed. No duplicate members. -
Dictionary
is a collection which is ordered** and changeable. No duplicate members.
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
In Python, a sequence is an ordered collection of elements, where each element is identified by an index. Sequences are a fundamental data type in Python and provide a way to store and access multiple values.
There are several built-in sequence types in Python, including:
-
Strings: A sequence of characters, such as
"Hello"
or'World'
. String values are immutable, meaning they cannot be changed once created. -
Lists: A sequence of elements enclosed in square brackets
[ ]
, such as[1, 2, 3]
or['apple', 'banana', 'orange']
. Lists are mutable, allowing you to modify, add, or remove elements. -
Tuples: A sequence of elements enclosed in parentheses
( )
, such as(1, 2, 3)
or('red', 'green', 'blue')
. Tuples are immutable like strings. -
Range: A sequence of numbers generated using the
range()
function, such asrange(0, 10)
orrange(5)
. The range represents an immutable sequence of numbers within a given range. -
Bytes and Bytearrays: Sequences of integers representing binary data. Bytes are immutable, while bytearrays are mutable.
Sequences in Python can be indexed and sliced. Indexing allows you to access individual elements by their position, and slicing allows you to extract a portion of the sequence.
For example, you can access elements in a sequence using square brackets, like sequence[index]
, where sequence
is the name of the sequence and index
is the position of the element you want to access.
my_string = "Hello, World!"
print(my_string[0]) # Output: 'H'
my_list = [1, 2, 3, 4, 5]
print(my_list[2]) # Output: 3
my_tuple = ('red', 'green', 'blue')
print(my_tuple[1]) # Output: 'green'
Sequences provide a way to work with collections of data in Python, and each sequence type has its own characteristics and use cases.
Boolean
Int
Float
Strings
Tuples
Complex
Frozenset
List
Dict
Set
آبجکت های پیمایش پذیر در پایتون
این پیمایشکردن با استفاده از حلقه ها صورت میگیره
Lists
Tuples
Dictionaries
Strings
Sets
- Introduction
- Variables
- Data Types
- Numbers
- Casting
- Strings
- Booleans
- Operators
- Lists
- Tuple
- Sets
- Dictionaries
- Conditionals
- Loops
- Functions
- Lambda
- Classes
- Inheritance
- Iterators
- Multi‐Processing
- Multi‐Threading
- I/O Operations
- How can I check all the installed Python versions on Windows?
- Hello, world!
- Python literals
- Arithmetic operators and the hierarchy of priorities
- Variables
- Comments
- The input() function and string operators
Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations
- Comparison operators and conditional execution
- Loops
- [Logic and bit operations in Python]
- [Lists]
- [Sorting simple lists]
- [List processing]
- [Multidimensional arrays]
- Introduction
- Sorting Algorithms
- Search Algorithms
- Pattern-matching Algorithm
- Graph Algorithms
- Machine Learning Algorithms
- Encryption Algorithms
- Compression Algorithms
- Start a New Django Project
- Migration
- Start Server
- Requirements
- Other Commands
- Project Config
- Create Data Model
- Admin Panel
- Routing
- Views (Function Based)
- Views (Class Based)
- Django Template
- Model Managers and Querysets
- Form
- User model
- Authentification
- Send Email
- Flash messages
- Seed
- Organize Logic
- Django's Business Logic Services and Managers
- TestCase
- ASGI and WSGI
- Celery Framework
- Redis and Django
- Django Local Network Access
- Introduction
- API development
- API architecture
- lifecycle of APIs
- API Designing
- Implementing APIs
- Defining the API specification
- API Testing Tools
- API documentation
- API version
- REST APIs
- REST API URI naming rules
- Automated vs. Manual Testing
- Unit Tests vs. Integration Tests
- Choosing a Test Runner
- Writing Your First Test
- Executing Your First Test
- Testing for Django
- More Advanced Testing Scenarios
- Automating the Execution of Your Tests
- End-to-end
- Scenario
- Python Syntax
- Python OOP
- Python Developer position
- Python backend developer
- Clean Code
- Data Structures
- Algorithms
- Database
- PostgreSQL
- Redis
- Celery
- RabbitMQ
- Unit testing
- Web API
- REST API
- API documentation
- Django
- Django Advance
- Django ORM
- Django Models
- Django Views
- Django Rest Framework
- Django Rest Framework serializers
- Django Rest Framework views
- Django Rest Framework viewsets