Variables and Data Types
Python Programming
Variables and Data Types
Variables are fundamental building blocks in any programming language. In Python, a variable is a name that refers to a value stored in the computer's memory. Unlike statically typed languages such as Java or C++, Python is dynamically typed, meaning you do not need to declare the type of a variable explicitly — Python infers it from the value you assign. This makes Python code more concise and flexible, although it also means you need to be mindful of what types your variables hold at any given point in your program.
Creating Variables
In Python, you create a variable simply by assigning a value to a name using the equals sign (=). Variable names must start with a letter or underscore, can contain letters, digits, and underscores, and are case-sensitive. Python follows the convention of using snake_case for variable and function names.
# Creating variables
name = "Alice"
age = 30
height = 5.7
is_student = False
# Multiple assignment
x, y, z = 10, 20, 30
# Same value to multiple variables
a = b = c = 0
# Variable naming conventions (snake_case)
first_name = "John"
total_count = 42
is_valid_input = True
# These are DIFFERENT variables (case-sensitive)
myVar = 1
myvar = 2
MYVAR = 3
Core Data Types
Python has several built-in data types that you will use constantly. The most common ones are integers, floats, strings, and booleans. You can check the type of any variable using the built-in type() function.
# Integers — whole numbers, no size limit in Python
count = 42
negative = -17
big_number = 1_000_000_000 # underscores for readability
print(type(count)) # <class 'int'>
# Floats — decimal numbers (64-bit double precision)
price = 19.99
temperature = -3.5
scientific = 2.5e10 # 25,000,000,000
print(type(price)) # <class 'float'>
# Strings — text enclosed in quotes
greeting = "Hello, World!"
single = 'Also valid'
multiline = '''This string
spans multiple
lines.'''
print(type(greeting)) # <class 'str'>
# Booleans — True or False
is_active = True
has_permission = False
print(type(is_active)) # <class 'bool'>
# NoneType — represents absence of a value
result = None
print(type(result)) # <class 'NoneType'>
Type Conversion (Casting)
Sometimes you need to convert a value from one type to another. Python provides built-in functions for type conversion: int(), float(), str(), and bool(). Be careful with conversions that might lose information or raise errors.
# String to integer
user_input = "42"
number = int(user_input)
print(number + 8) # 50
# Integer to float
x = float(10)
print(x) # 10.0
# Number to string
age = 25
message = "I am " + str(age) + " years old"
# Float to integer (truncates decimal part)
pi = 3.14159
whole = int(pi)
print(whole) # 3
# Boolean conversions
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False (empty string)
print(bool("Hi")) # True (non-empty string)
print(bool(None)) # False
print(bool([])) # False (empty list)
Dynamic Typing
Python allows you to reassign a variable to a value of a different type. While this provides flexibility, it can also lead to bugs if you are not careful. Many Python developers use type hints (introduced in Python 3.5) to document expected types without enforcing them at runtime.
# Dynamic typing — variable can change type
x = 10 # int
x = "hello" # now a str
x = [1, 2, 3] # now a list
# Type hints (optional, for documentation and IDE support)
name: str = "Alice"
age: int = 30
scores: list[int] = [95, 87, 92]
Pro tip: Use theisinstance()function instead oftype()for type checking, because it also works with inheritance. For example,isinstance(True, int)returns True because bool is a subclass of int in Python.
Key Takeaways
- Variables in Python are created by assignment and do not require explicit type declarations — Python infers the type automatically.
- The core data types are
int,float,str,bool, andNoneType. - Use
type()to check a variable's type and built-in functions likeint(),float(),str()to convert between types. - Python is dynamically typed, so a variable can be reassigned to any type, but type hints help document your intent.
- Follow Python naming conventions: use snake_case for variables and functions, and remember that names are case-sensitive.