HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Data Types & Numbers

Data Types & Numbers

Text, integer, float, boolean; finding a type with type() and type conversion (casting).

Simple enough for a 5-year-old to understand

Basic data types

Every value has a type: text (str), integer (int), float (float), boolean (bool).

The four basic types
1
name = "Alex"
str — text (in quotes).
Variablestr — text (in quotes).
2
age = 36
int — integer.
Variableint — integer.
3
height = 1.72
float — decimal (with a dot).
Variablefloat — decimal (with a dot).
4
student = True
bool — true/false (True/False).
Variablebool — true/false (True/False).
5
print(name, age, height, student)
Writes them all.
Output / InputWrites them all.
VariableOutput / Input
💡 Hint: Floats use a dot, not a comma: 1.72 (correct), 1,72 (wrong).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.What type is the value 1.72?

About this topic

Every value in Python has a type, and that type determines what you can do with it. You can add two numbers or concatenate two texts; but when you try to combine a number and a text with a plus sign, Python raises an error. Most error messages stem from type mismatches.

Four basic types handle most of your work: integer (int), floating-point number (float), string (str), and boolean (bool). Whenever you wonder about the type of a value, you can ask the type function, and whenever you need to convert between types, you can use the int, float, and str functions.

What you will learn

  • Distinguishing between int, float, str, and bool types
  • Learning the type of a value with type
  • Performing type conversion with int, float, and str
  • Why division operations always return a float
  • Why data received via input is always text

When to use?

Type conversion is most needed when receiving external data: everything you read from the user via input is text, and numbers coming from a file or a web request also arrive as text. You need to convert these to numbers before doing calculations.

In the reverse direction, when combining a number with text on the screen, you either convert it with str or use an f-string.

Common mistakes

Wrong

age = input("Your age: ")
print(age + 1)

input always returns text, not a number. Because you tried to add a number to text, you get the "TypeError: can only concatenate str (not "int") to str" error.

Correct

age = int(input("Your age: "))
print(age + 1)

Wrong

print(int("12.5"))

The int function cannot directly convert a text written as a decimal; it gives the "ValueError: invalid literal for int() with base 10: '12.5'" error. You must first convert it to float, and then to int if necessary.

Correct

print(int(float("12.5")))

Wrong

if 0.1 + 0.2 == 0.3:
    print("equal")

Decimal numbers are stored approximately in binary in memory; the result of 0.1 + 0.2 is not exactly 0.3. That's why the comparison unexpectedly turns out false and the condition doesn't run at all.

Correct

if abs((0.1 + 0.2) - 0.3) < 1e-9:
    print("equal")

Frequently asked questions

What is the difference between int and float?

int is a whole number, it has no decimal part. float is a decimal number. Normal division in Python always returns a float — the result of 10 divided by 2 is 5.0, not 5. If you want an integer result, you must use the floor division operator.

Why do I get fractional calculation errors?

Because the float type stores decimal numbers approximately in binary; this is not unique to Python, it's the case in almost all languages. For tasks requiring precision like financial calculations, you should use the Decimal type instead of float.

Why are bool values True and False, and not true and false?

Boolean values in Python start with a capital letter: True and False. If you write them in lowercase, Python thinks they are undefined variables and raises a NameError. This is the most common mistake made by those coming from JavaScript.

Summary

In Python, four basic types — int, float, str, bool — handle most of your work, and the vast majority of error messages arise from type mismatches. Every piece of data received via input is text; you need to convert it with int or float before doing calculations.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code