Python data types and variables

Declaring any variable and assigning value in python is much simpler than any other programming language. Python has some built-in data types like integer, float, complex number etc.

Data types in Python

Declaring variable and assign value in Python is very straightforward!

In python while declaring variable, just assign some value, based on value the variable- data type will be decided.


To check the data type of any variable just print type (variable-name), for example if we want to know the data type of above variable, will write following statement.


Python Data Types

Here are some commonly used data types.

  • Python Data Type - String Example
    strMessage ="this is a string example";
    print("variable value", strMessage, " and data type is ", type(strMessage))

    We can concatenate multiple strings and store in a variable

    #concat multiple strings
    str1="I am learning ";
    str2="Python development";
    print(str1 + str2)

    You may be interested to look at Python String Methods.

  • Python Data Type - Numeric Example
    print("variable value", studentCount, " and data type is ", type(studentCount))
  • Python Data Type - Float Example
    print("variable value", totalAmount, " and data type is ", type(totalAmount))
  • Python Data Type – Tuple Example

    Tuple is another data type which is a sequence of data similar to list (collection object), in Tuple datatype we can have only one type of data and also multiple type of data comma separated.

    #tuple having only integer type of data.
    print("variable value", tupleExample1, " and data type is ", type(tupleExample1))
    #tuple having multiple type of data.
    tupleExample2=("Online Learning", 1,2,3,"Fun")
    print("variable value", tupleExample2, " and data type is ", type(tupleExample2))
  • Python Data Type – Dictionary Example

    Dictionary variable we can store any object information with key-value pair, while retrieving value from any Dictionary variable, we need to use any key, and the key must match.

    stu = {"firstName":"Bill","lastName":"Hokins", "age":33, "Address":"South Kolkata"}
    #print value with any key

    You should also look at Python Dictionary Methods.

  • Two-dimensional array in Python

    In following example, we have created two single-dimension arrays, then a two-dimensional array

    col = 5
    array1 = [0 for i in range(col)]
    array2 = [0,1,2,3,4,5,6,7,8,9]

    here is two-dimensional array example in python.

    array3 =[array1,array2]
    #[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Python Data Types, Variables
Python if-else Conditional Logic, Loops, Array
Python programming examples | Join Python Course