Lesson 1 - Variables and Data Types

  • 1.1 Creating Variables

    In Python, variables are created when you assign a value to it:


    x = 5
    y = 10.2
    z = “David”

    You can check the variable data types using the print command



    1.2 Data types

    Python has the following data types built-in by default

    Text Type: str
    Numeric Types: intfloatcomplex
    Sequence Types: listtuple
    Dictionary Type: dict
    Boolean Type: bool
    Set Types: set,frozenset

    Let’s look at some examples below

    Text Type:   
    x = “Population”,   h
    ere, x is a variable of type str

    Numeric Types: 
    x = 10, here x is a variable of type int
    x = 8.2, here x is a variable of type float
    x =  3+ 4j ,  here x  is a variable of type complex

    Sequence Type – List: 
    Names = [“John”, “David”, “Michael”]

    Sequence Type – Tuple: 
    Signal = (“red”, “yellow”, “green”)

    Dictionary Type – dict: 
    marks = {“Maths”:”90″, “Physics”:”85″}

    rained = True

    Set Type – set
    subjects = {“Maths”,”English”,”Science”}

    Set Type – frozenset
    subjects = ({“Maths”,”English”,”Science”})

    Know the type of a variable

    x = 5
    y = “John”



    1.3 Data type casting

    You can explicitly specify the data type of a variable using a method known as casting.
    x = str(5)    # x will be ‘5’
    y = int(5)    # y will be 5
    z = float(5)  # z will be 5.0