www
greetings
 

WARNING

This website contains content that is intended for a discerning audience.
You are here:

Python Variables and Data Types

Variables in Python are used to store data values. Unlike other programming languages, Python has no command for declaring a variable - a variable is created the moment you first assign a value to it.


Variable Assignment

x = 5
y = "Hello, World!"
z = 3.14

Python is dynamically typed, meaning you don't need to declare the type of a variable. The interpreter infers the type based on the value assigned.


Basic Data Types

Numbers

Python supports three numeric types:

Strings

Strings in Python are surrounded by either single or double quotes:

name = "Python"
message = 'Hello, World!'

Booleans

Boolean values represent True or False:

is_active = True
has_permission = False

Collections

Lists

Ordered, mutable collections:

fruits = ["apple", "banana", "cherry"]

Tuples

Ordered, immutable collections:

coordinates = (10, 20, 30)

Dictionaries

Key-value pairs:

person = {"name": "Alice", "age": 30}

Sets

Unordered, unique elements:

unique_numbers = {1, 2, 3, 4, 5}