Script Valley
Python: Complete Language Course
Python Basics and SyntaxLesson 1.2

Python variables and data types explained

int, float, str, bool, type function, dynamic typing, variable naming rules, type conversion

Variables and Types

Python is dynamically typed โ€” you do not declare a type. The interpreter infers it from the value assigned.

age = 25          # int
price = 9.99      # float
name = "Alice"    # str
is_active = True  # bool

print(type(age))   # <class 'int'>

Naming Rules

Variable names must start with a letter or underscore, contain only letters, digits, and underscores, and cannot be a reserved keyword (if, for, class, etc.). Use snake_case by convention.

Type Conversion

Convert explicitly using built-in functions. Implicit conversion does not happen between incompatible types โ€” Python raises a TypeError.

x = "42"
n = int(x)       # 42
f = float(x)     # 42.0
s = str(3.14)    # "3.14"
b = bool(0)      # False โ€” 0, "", [], None are all falsy

# This raises TypeError:
# result = "5" + 5
result = int("5") + 5  # correct: 10

Use type() during debugging to verify what a variable holds. Mismatched types are the most common beginner error.

Python infers the type from the value, but that does not mean types do not matter โ€” they absolutely do. Passing a string where an integer is expected still raises a TypeError, just at runtime rather than compile time. Calling type() mid-debug is a fast way to confirm what you are actually working with. For user-facing applications, always validate and convert input before using it in calculations. The most common beginner trap is reading input(), which always returns a string, and then trying arithmetic on it without converting first.

Up next

Python operators and expressions โ€” complete guide

Sign in to track progress

Python variables and data types explained โ€” Python Basics and Syntax โ€” Python: Complete Language Course โ€” Script Valley โ€” Script Valley