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.

v=10
print(v)

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.

print(type(v))

Python Data Types

Here we see some commonly used data type like string, integer, decimal, tuple, dictionary data type, array, date time etc

  • Python String Data Type 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 Numeric Data Type Example

    Numeric means we can assign any number, which can be negative also, but not with decimal, that would be considered as separate datatype.

    studentCount=100
    print("variable value", studentCount, " and data type is ", type(studentCount))
    
  • Python Float Data Type Example
    totalAmount=100.56
    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.
    tupleExample1=(1,2,3,4)
    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))
    

    Read more about how to use python tuple variables.

  • Python Dictionary Data Type 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
    print(stu["firstName"])
    

    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)]
    print(array1)
    
    
    array2 = [0,1,2,3,4,5,6,7,8,9]
    print(array2)
    
    

    here is two-dimensional array example in python.

    array3 =[array1,array2]
    print(array3) 
    
    #[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
    

    Learn more about Python Array with examples.

  • Python date time data type

    To work with python date time data type, we need import datetime reference in our python file like example below.

    import datetime
    
    currentDT = datetime.datetime.now()
    print(currentDT)   # 2022-03-30 22:22:22.461652
    print(currentDT.year) #2022
    print(currentDT.month) #3
    print(currentDT.day) #30
    

    We also can print the day name from full datetime object using in-built method. like print(currentDT.strftime("%A")) #Wednesday.

 
Python Data Types, Variables
Learn python programming with free python coding tutorials.
Other Popular Tutorials
Python if-else Conditional Logic, Loops, Array
Python programming examples | Join Python Course