A variable is a name that refers to a memory location storing data. Unlike other languages, variables do not need to be declared with a specific type in python.
Variables are created by simply assigning a value to a name:
age = 25
name = "Alice"
- Variable names must start with a letter or an underscore (_).
- Variable names can contain letters, numbers, and underscores but they cannot start with a number.
- Python is case-sensitive, so
age
andpython Age
are going to be treated as two different variables. - Avoid using reserved keywords (e.g.,
if
,else
,for
) as variable names.
Python’s basic data types include:
- Integer (int): Whole numbers, positive or negative, without a decimal. Integers are unbounded in Python, meaning they can be as large as memory allows.
count = 10
- Float (float): Numbers with decimal points. Floats can represent fractions and real numbers.
price = 19.99
- String (str): A sequence of characters (letters, symbols, digits, etc.). Strings are enclosed within single, double, or triple quotes.
name = "Alice"
- Boolean (bool): Represents True or False values, often used for conditional expressions.
is_valid = True
- NoneType (None): Represents the absence of a value or a null value. Commonly used as a placeholder or default value in functions.
result = None
- Type Casting: Converting one data type to another. Python provides several functions to convert between types:
int("10") # Converts string to integer -> 10
float("3.14") # Converts string to float -> 3.14
str(25) # Converts integer to string -> "25"
bool(0) # Converts integer to boolean -> False
-
Implicit Type Conversion: Python automatically converts data types in certain operations (e.g., adding an integer and float results in a float).
-
Type Checking: Using
type()
to check the data type of a variable.isinstance()
checks if a variable is of a particular type:
age = 25
print(type(age)) # <class 'int'>
print(isinstance(age, int)) # True
Python doesn’t have built-in constant support, but by convention, variable names in all uppercase are used to indicate constants. Typically, constants are defined at the beginning of a script or within a module:
PI = 3.14159
MAX_USERS = 100
- snake_case is used for variables and functions (e.g., user_name, calculate_total).
- ALL_CAPS is used for constants (e.g., PI, MAX_USERS).
- CamelCase is used for class names (e.g., CustomerProfile).