Variables and Data Types

Carson West

Operators

Variables and Data Types

Python uses variables to store data. A variable is essentially a named location in the computer’s memory where you can store a value. You don’t need to explicitly declare the data type of a variable in Python; it’s dynamically typed.

Variable Assignment:

x = 10          # Integer
y = 3.14       # Float
name = "Alice"  # String
is_active = True # Boolean
my_list = 1, 2, 3 # List
my_tuple = (4, 5, 6) # Tuple
my_dict = {"a": 1, "b": 2} # Dictionary

Data Types:

Type Conversion (Casting):

You can convert between Data Types using built-in functions:

x = 10
y = float(x)  # Convert integer to float
z = str(x)    # Convert integer to string
a = int(3.14) # Convert float to integer (truncates the decimal part)
b = bool(0)   # Convert 0 to False, any other number to True

Type Conversion

Naming Conventions:

Variable Naming Conventions

Example:

age = 30
name = "Bob"
height = 5.10
is_adult = True

print(f"Name: {name}, Age: {age}, Height: {height}, Is Adult: {is_adult}")