View All
View All
View All
View All
View All
View All
View All
    View All
    View All
    View All
    View All
    View All

    Variables and Data Types in Python [An Ultimate Guide for Developers]

    By Rohit Sharma

    Updated on Feb 26, 2025 | 7 min read | 6.3k views

    Share:

    Python programming language is one of the most in-demand programming language in the Machine Learning world. Python language helps the developer to focus more on implementing the functions than just writing complex codes. The main feature of Python is the ease of readability and access. Data Types and Variable in Python are two of the fundamental concepts of Python programming language. 

    Definition of Variables in Python

    The Values in Data Type and Variables keep varying. The values are stored in the memory location of a variable in a programming language. According to the specifications, the value stored can be changed. 

    When a value is allocated to a variable, a python variable is declared. There is no need to give any extra command to create a variable in Python. Let us look at the regulations and rules to create a variable and how its declaration is made. Read: Data Types of Python.

    Definition and Declaration of Variable

    There is no need to give any command for the declaration of a variable in Python other than just providing a value. So, a variable is implicitly declared after the assignation of value. 

    Rules for Declaration of Variable

    1. No Special Characters Allowed: 
    2. There is no special character allowed in a variable.
    3. Only Underscores and Alpha-Numeric characters can be allowed in a variable.
    4. The value in the variable of Python is case sensitive.

    Check out our data science online courses to upskill yourself

    Different Data Types in Python

    There are six main types of Data types in Python. Four of them are numerical Data Types that holds only numeric values. They are Boolean, Complex Number, Float and Integers. Let’s all the data types in details

    • Integers

    Whole Numbers are used for the representation of Integer Data Type Value.

    For example, 

    1| x = 200

    2 | y = 424

    3 | z = 488

    As long as the value of an integer is in the whole number, it will stay an integer.

    One can use type() function to know the type of data type of any variable. 

    • Float

    Decimal point values are used for the representation of Float Value.

    For example,

    1 | x = 11.55

    2 | y = 13.45

    3 | z= 55.66

    If there is a decimal value in an integer, then it will automatically become a float data type.

    • Complex Numbers

    Imaginary values are used to represent complex numbers. The value “j” is used at the end of the number to represent Imaginary Values.

    For example,

    1 | y = 12 + 6j

    2 | x = 15 +7j

    3 | z = 77 + 88j

    • Boolean

    The Boolean data type is used to get an output, either true or false. It categories the output in false or true.

    For example,

    1 | num = 6 > 2

    2 | #num is boolean variable

    3 | type(num)

    4 | #the output will be boolean

    5 | print(num)

    6 | #this will print true

    • Strings

    Unicode character values are used for the representation of String Data Type. A string can also be a single character because there is no data type for a character in the python programming language. Double quotes or single quotes are used in the value of string for declaration or denotation. Square brackets and indexes are used for the value in a string to be accessed. 

    For example:

    name = ‘python’
    
    name[5] 
    
    #this will give you the output as ‘o’

    The nature of string is immutable because the string cannot be changed after it is once replaced.

    Command line input for strings

    1 | name = ‘python’

    2 | name.upper()

    3 | #this will make the letters to uppercase

    4 | name.lower()

    5 | #this will make the letters to lowercase

    6 | name.replace(‘p’) = ‘P’

    7 | #this will replace the letter ‘p’ with ‘P’

    8 | name[2: 5]

    9 | #this will return the strings starting at index 2 until the index 5.

    • Lists

    In Python programming language, there are Four types of collection data types. One should know the limits and functions of a collection before selecting a type of collection. One of the collection data types is Lists. The other three collection data types are Dictionary, Set, Tuple.

    A list can be changed and ordered again, which is not similar in the case of a string. One can also add duplicate value in the List. Square brackets are used for the declaration of List.

    1 | pythonlist = [9, 10, 11, 12, 23, 4, 49, 4, 4, ‘python’, ‘n’]

    How to access value from a list?

    In String, Values are accessed by using indexes.

    1 | pythonlist[3:7]

    2 |  

    3 | #this will get the values from index 3 until index 7.

    How to add or replace a value in a list?

    1 | pythonlist[6] = ‘python’

    2 |

    3 | #this will replace the value at index 3.

    4 |

    5 | pythonlist.append(‘python’)

    6 |

    7 | #this will add the value at the end of the List.

    8 |

    9 | pythonlist.insert(3, ‘machine learning)

    10 | 

    11 | #this will add the value at index 3.

    There are other operations on the List which can be performed are as follows:

    Method Name and its Property     

    • reverse() – returns the reversed list
    • sort() – sorts the list
    • remove() – removes the item with the specified value
    • pop() – removes the element from the specified position
    • index() – returns the index of the element
    • count() – returns the number of elements of the specified value
    • extend() – add the elements of the List to the end of the current List
    • copy() – returns a copy of the list
    • clear() – removes all the elements from the list
    • Tuples

    It is a type of collection data type which cannot be muted or changed. By the use of index values, the values of Tuple is accessed. The benefits in the tuples are in order, and it can also have values in duplicate. Round Brackets are used to declare the value in a tuple. 

    For example,

    1 | pythontuple = (100,100,200,300,400,500,500,500)

    2 |  

    3 | #to count the number of elements

    4 | 

    5 | pythontuple.count(500)

    6 | 

    7 | #the output will be 3

    8 |  

    9 | #to find the index

    10 |  

    11 | pythontuple.index(300)

    #the output will be 4. since the index number at 300 is 4.

    • Sets

    This type of data type is not in order, and it has no index as well. Curly brackets are used in Python for the declaration of a set.

    For example,

    1 | pythonset = {1, 2, 3, 4, 5, 5}

    background

    Liverpool John Moores University

    MS in Data Science

    Dual Credentials

    Master's Degree17 Months

    Placement Assistance

    Certification6 Months

    upGrad’s Exclusive Data Science Webinar for you –

    Transformation & Opportunities in Analytics & Insights

    • Dictionary

    It is a type of collection data type which is similar to other collection data type, but its values are represented in pairs. It can be changed and no need to put it in order. 

    1 | pythondictionary = { ‘tensorflow’ : ‘ ML’, ‘data’ ” ‘ python’ }  

    Conclusion

    Python is a quite popular language today mainly because of the variables and data types it can handle. These data types are uncommon and not found in most other languages, making Python unique for core mathematical, statistical and computational implementations.

    Enroll now for the program – Professional Certificate Program in Data Science for Business Decision Making from IIM-Kozhikode

    Frequently Asked Questions (FAQs)

    1. In Python, how are global and local variables different from each other?

    2. What is the K-map approach for reducing any Boolean function to its simplest form?

    3. In Python, how do you read a variable?

    Rohit Sharma

    761 articles published

    Get Free Consultation

    +91

    By submitting, I accept the T&C and
    Privacy Policy

    Start Your Career in Data Science Today

    Top Resources

    Recommended Programs

    upGrad Logo

    Certification

    3 Months

    Liverpool John Moores University Logo
    bestseller

    Liverpool John Moores University

    MS in Data Science

    Dual Credentials

    Master's Degree

    17 Months

    IIIT Bangalore logo
    bestseller

    The International Institute of Information Technology, Bangalore

    Executive Diploma in Data Science & AI

    Placement Assistance

    Executive PG Program

    12 Months