Python: Variables and Data Types



This tutorial documents the variables and data types in Python3.

Variables are specific types of objects in Python programming language and used to store and manipulate data within program code.


Python language does not require variables to be explicitly declared or associated with specific data types before they are actually used during code execution. As it applies to other objects in Python - same variable also could be associated with various data types during execution of program.


Data types - are a specific (known) structures of data defined in Python language documentation, other defined structures within program code or "dynamic" data types - structures of data created during program execution programmatically. 


Standard or built-in data types:

    Built-in Data Types
  • Numeric: int, float, complex;
  • Sequence Type: string, list, tuple;
  • Boolean;
  • Set;
  • Dictionary;


Numeric data types represents data which can be integer, floating number or complex number.


Python language has specific "reserved" words to define those data types, respectively: "int", "float" and "complex".


int - are whole numbers without decimal point, positive and negative, i.e. 1, 0, -875

float - are numbers with decimal point, positive or negative, i.e. 1.2, 0.0, -875.3

complex - are numbers with "real part" + "imaginary part", i.e. 1+4j, 3.14j


Examples of operations with numeric data types:

    Numeric Data Types

   
    Integer
    
    var = 3
    print("Data Type of var: ", type(var))
Output: Data Type of var: <class 'int'>
    Float
    
    var = 4.5
    print("Data Type of var: ", type(var))
Output: Data Type of var: <class 'float'>
    Complex

    var = 8 + 2j
    print("Data Type of var: ", type(var))
Output: Data Type of var: <class 'complex'>


Sequence data types represents collection of data types and stores multiple values in single data structure. There are multiple data types in Python within sequence data types: String, List, Tuple with respective reserved words: str, list, tuple


String - collection of one or more characters 


Examples of operations with string data type:

    String Data Type

    string_var = 'Hello World'
    print(string_var)
Output: Hello World
    string_var1 = "Hello World,"
    string_var2 = "\nI would like to learn Python language"
    string_var = string_var1 + string_var2
    print(string_var)
Output: Hello World,
I would like to learn Python language

    #Explicit types declaration is not required in Python, but it is possible
    string_var: str = 'Hello World'
    print(string_var )
Output: Hello World

    string_var: str = 'Hello World'
    print(type(string_var ))
Output: <class 'str'>

    #String is the object as every other type in Python, so this type have methods or
    #"actions" which can be applied to data value
    string_var = string_var.split(' ')
    print(string_var)
    print(type(string_var ))
Output: ['Hello', 'World']
  <class 'list'>



List - collection of data types, in example integers and strings or any other data types


Examples of operations with list data type:

    List Data Type

    #Explicit types declaration is not required in Python, but it is possible
string_var: str = 'Hello World'
    list_var: list = string_var.split(' ')
    print(list_var)
    print(type(list_var))
Output: ['Hello', 'World']
  <class 'list'>

    list_var1 = []
    list_var2 = [1, 2, 3, 4]
    list_var3 = ["Hello", "World"]
    list_var = list_var1 + list_var2 + list_var3
    print(list_var)
Output: [1, 2, 3, 4, 'Hello', 'World']

    list_var = []
    list_var.append(list_var3)
    list_var.append(list_var2)
    print(list_var)
Output: [['Hello', 'World'], [1, 2, 3, 4]]

    list_var2 = [1, 2, 3, 4]
    list_var3 = ["Hello", "World"]
    list_var2.extend(list_var3)
    print(list_var2)
Output: [1, 2, 3, 4, 'Hello', 'World']



Tuple - collection of data types, same as list data type, but data cannot be changed after tuple was created (this type is "immutable").


Examples of operations with tuple data type:

    Tuple Data Type

    #Explicit types declaration is not required in Python, but it is possible
tuple_var: tuple = ()
    print(type(tuple_var))
Output: <class 'tuple'>

    tuple_var = (1, 2, 3, 4,"Hello", "World")
    print(tuple_var)
Output: (1, 2, 3, 4, 'Hello', 'World')




Boolean data type - is logical data type, which define 2 possible values: "True" and "False"


Examples of operations with boolean data type:

    Boolean Data Type

   #Explicit types declaration is not required in Python, but it is possible
boolean_var: bool = False
    boolean_var = True
    print(type(boolean_var))
Output: <class 'bool'>



Set data type - is collection of data types, which define collection of data values 

Examples of operations with set data type:

    Set Data Type

   #Explicit types declaration is not required in Python, but it is possible
set_var: set = set()
    set_var = set([1, 2, 3, 4,"Hello", "World"])
    print(type(set_var))
Output: <class 'set'>



Dictionary data type - is collection of data types, which define collection of data values like a map, where there are key:value pairs. 

Examples of operations with dictionary data type:

    Dictionary Data Type

   #Explicit types declaration is not required in Python, but it is possible
dictionary_var: dict = {}
    dictionary_var = {1: "Hello", 3: "World"}
    print(type(dictionary_var))
Output: <class 'dict'>




Code examples available for experiments in the Github source control: 



Post a Comment

Previous Post Next Post