Python Data Type
tags: #python/intro
What is a data type?
Data types specify the type of data that is stored inside a variable.
Common Data Types in Python
Python consists of several built-in data types and structures:
| Data Types | Description |
|---|---|
| Numeric | holds numeric values (int, float, complex) |
| String | holds sequence of characters (str) |
| Collection | holds collection of items or elements (list, tuples, dictionary, set). They can also be referred to as data structures. |
| Boolean | holds either True or False |
| Each data type has its own set of properties, methods, and behaviours. |
int and float are considered to be simple or primitive or atomic data types because their values are not composed of any smaller parts.
We can use the type() function to know which class a variable or a value belongs to:
type(var_name)
Numeric Data Type
Python numeric data types store numeric values. Numeric instances (objects) are created when you assign a numeric value to them.
var1 = 1
var2 = 10
var3 = 10.023
There are three distinct numeric types: integers, floating point numbers, and complex numbers.
int(a numeric value without decimals. Can be positive or negative in value)float(these are real values i.e., they represent numeric values with decimals)complex
String Data Type
Python Strings are identified as a contiguous set of characters or a sequence of characters enclosed in single or double quotation marks.
varstr = "This is a string"
varstr2 = 'This is also a string'
Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
Boolean Data Type
Boolean data types represent either True or False, used for logical operations and conditions.
Collection Data Types
Common collection data types in Python include:
- Lists: This represents an ordered and mutable collection of items, enclosed in square brackets (
[]). - Tuples: Represents an ordered and immutable collection of items, enclosed in (
()). - Dict: Represents a collection of key-value pairs, enclosed in (
{}) with unique keys. - Set: Represents an unordered and mutable collection of unique elements, enclosed in (
{}) or defined using theset()function.