Free Web Development Online Tutorials, Learn to Code
How to declare variable in python, Python data types examples, built-in data types and data structure, array in python

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 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
    studentCount=100
    print("variable value", studentCount, " and data type is ", type(studentCount))
    
  • Python Data Type - Float 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))
    
  • 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
    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]]
    
Comment
Name
Email
Website
Subscribe
 
Python Data Types, Variables

Python if-else Conditional Logic, Loops, Array
Python programming examples | Join Python Course