Variables & Data Types in Python

    Kedar Kabra4 min readUpdated

    A variable in Python is just a name that points to a value. You create one by writing a name, an equals sign, and the value — no need to declare a type first:

    age = 21              # an integer (whole number)
    price = 49.99         # a float (decimal number)
    name = "Asha"         # a string (text)
    is_student = True     # a boolean (True or False)
    
    print(age, price, name, is_student)
    

    Python figures out the type for you. Let's look at each type and how to work with them.

    The four core data types

    Python has many types, but beginners use these four constantly:

    • int — whole numbers like 0, 42, -7.
    • float — numbers with a decimal point like 3.14, -0.5, 100.0.
    • str — text wrapped in quotes, like "hello" or 'Python'.
    • boolTrue or False (note the capital letters).

    You can check any value's type with the built-in type() function:

    # type() tells you what kind of value something is
    print(type(21))          # <class 'int'>
    print(type(49.99))       # <class 'float'>
    print(type("Asha"))      # <class 'str'>
    print(type(True))        # <class 'bool'>
    

    Naming variables

    A few simple rules keep your code clean and error-free:

    • Names can use letters, numbers, and underscores, but cannot start with a number.
    • Names are case-sensitive: score and Score are different variables.
    • Use lowercase with underscores for readability (this is the Python convention): total_marks, user_name.
    • Avoid Python keywords like if, for, class, True as names.
    total_marks = 480      # good: clear and lowercase
    totalMarks = 480       # works, but not the Python style
    # 2nd_attempt = 5      # ERROR: cannot start with a number
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Converting between types

    Sometimes you need to turn one type into another. This is called type conversion (or casting). The built-in functions int(), float(), and str() do the job:

    # input() always returns a string, so we convert it
    age_text = "25"
    age_number = int(age_text)     # str -> int
    print(age_number + 5)          # 30
    
    price = 49.99
    price_rounded = int(price)     # float -> int (drops the decimal, gives 49)
    print(price_rounded)
    
    count = 7
    count_text = str(count)        # int -> str
    print("You have " + count_text + " items")
    

    This matters a lot when reading user input, because input() always gives you a string — even if the user types a number.

    A small real example

    # Calculate a student's average marks
    
    # input() returns text, so we convert each one to a float
    maths = float(input("Marks in Maths: "))
    science = float(input("Marks in Science: "))
    english = float(input("Marks in English: "))
    
    # Add them and divide by 3 to get the average
    average = (maths + science + english) / 3
    
    # round() trims the result to 2 decimal places
    print(f"Your average is {round(average, 2)}")
    

    Common mistakes

    • Forgetting quotes around text: name = Asha causes a NameError because Python thinks Asha is a variable. Write name = "Asha".
    • Adding a string to a number: "Age: " + 25 raises a TypeError. Convert the number first with str(25), or use an f-string: f"Age: {25}".
    • Assuming input gives a number: input() returns a string. age = input(...) then age + 1 fails. Wrap it in int() or float().
    • Confusing = and ==: A single = assigns a value. A double == compares two values. Mixing them up is a classic beginner bug.
    • Capitalising booleans wrong: It's True and False, not true or TRUE.

    FAQ

    Do I have to declare the type like in Java or C? No. Python uses dynamic typing — the type is decided automatically from the value you assign.

    Can a variable change type later? Yes. x = 5 then x = "hello" is perfectly legal. Python just re-points the name.

    What is None? None is a special value meaning "no value yet". It's its own type (NoneType) and is useful as a placeholder.


    Now that you can store data, learn how to do things with it in Operators in Python and how to handle text in String Handling in Python. See all topics on the Python learning hub.

    Want to learn this properly? Join the waitlist for our Python course — taught in Jalgaon, beginner-friendly.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Kedar Kabra

    Instructor, Infoplanet

    Kedar Kabra teaches Python at Infoplanet, helping beginners become confident programmers through hands-on, project-first practice.

    Related guides