Variable Types in Python

Python supports the traditional variable types found in other programming languages.

In Python, you can change a variable's data type at any time simply by assigning it a new value.

    Variable Declaration in Python

    Python uses dynamic typing for variables.

    In Python, there's no need to declare a variable's type before using it.

    Simply assign a value to a variable.

    The interpreter automatically detects the data type and assigns it to the variable (numeric, string, boolean).

    Integer Variables

    To assign an integer to a variable, simply write the integer after the equals sign without quotes.

    variable = value

    Example of assignment

    year = 2000

    The interpreter recognizes that the variable year is an integer because it is assigned an integer value (2000).

    String Variables

    To assign a string to a variable, write the string after the equals sign, enclosed in either double quotes "" or single quotes ''

    variable = "string"

    or

    variable = 'string'

    Both methods (double quotes or single quotes) are equivalent.

    Example of assignment

    firstName = "Andrea"
    lastName = 'Minini'

    The variable firstName is a string variable because it is assigned a string of characters ("Andrea").

    The variable lastName is also a string variable because the string is enclosed in single quotes ('Minini').

    Double Quotes or Single Quotes?

    You can use either double quotes "" or single quotes '' to assign a string.

    In most cases, it doesn't matter.

    Often, the choice depends on the presence of special characters in the string.

    Example

    If the text contains single quotes, using double quotes as delimiters makes the inner single quotes normal characters.

    text = "this is an example"

    Conversely, if the text contains double quotes, using single quotes as delimiters makes the inner double quotes normal characters.

    text = 'the symbol " for double quotes'

    However, mixing single and double quotes as delimiters for the same string is incorrect.

    In this case, the interpreter will throw an error.

    >>> name='andrea"
    File "<stdin>", line 1
    name='andrea"

    How to Change a Variable's Data Type

    To change a variable's data type, simply assign it a new value. It’s very easy.

    The new assignment will also change the data type.

    Example

    In this script, I assign two values to the same variable.

    year = 2018
    year = "2018"

    In the first statement, the variable is an integer.

    In the second statement, it becomes a string because I assigned it a string value.

    Note: In other programming languages like C or Java, changing the data type through assignment generates an error message. In Python, however, it is processed without any issue. It is syntactically correct.

    Although it is syntactically correct, changing the data type of the same variable within a script is not recommended because it can cause unexpected errors during processing.

    An Example of an Unexpected Error

    The + operator performs different operations depending on the data type:

    1. Adds numeric values
    2. Concatenates string values

    However, you cannot add a numeric value to a string or concatenate a string with a numeric value.

    >>> year = 2018
    >>> print(year + 1)
    2019
    >>> year = "2018"
    >>> print(year + 1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: must be str, not int

    If the operands of the operation do not have the same data type, the Python interpreter will return an error message.

    Python Lists

    Unlike other languages, Python stands out for its array variables (or lists). They are much more developed and advanced.

    What is an array? It is a variable that contains a list of numeric or alphanumeric elements. Each element is uniquely associated with an index number to identify its position in the series. Graphically, an array is a column of data with multiple rows. If the array contains multiple columns, it is called a matrix.
    the difference between arrays and matrices

    Main types of lists in Python

    1. Simple List
    2. Tuple
    3. Set
    4. Dictionaries
     
     

    Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

    FacebookTwitterLinkedinLinkedin
    knowledge base

    Variables in Python

    1. Variables
    2. Variable Types
    3. Strings
    4. Inline Assignment
    5. Lists
    6. Tuples
    7. Sets
    8. Dictionaries