Variables
Nerd Cafe
What is a Variable?
A variable is a name that refers to a value stored in memory. Think of it as a box with a label that stores data. In machine learning, variables help us manage data, features, models, and results.
1. Declaring Variables
x = 5
name = "Mr Nerd"
price = 19.99
is_active = TruePractical Notes:
xis an integernameis a stringpriceis a floatis_activeis a boolean
No need to declare the type in Python — it figures it out!
2. Variable Naming Rules (Very Important in ML Projects)
# Valid
data = [1, 2, 3]
learning_rate = 0.01
model_name = "LinearRegression"
# Invalid
# 1model = "wrong" ❌ Starts with a number
# learning-rate = 0.01 ❌ Hyphen not allowedBest Practices (PEP8 Style Guide for ML Code):
Use lowercase with underscores:
train_data,test_accuracyBe descriptive:
input_vectorinstead ofivAvoid keywords:
class,def, etc.
3. Updating Variables
Shortcuts:
4. Data Types in Machine Learning
5. Practical Use in ML:
6. Multiple Assignments
7. Constants (Not Built-in, But You Can Use UPPERCASE)
Tip: Use ALL_CAPS to indicate values that should not change.
8. Type Checking (Important in Debugging ML Code)
9. Variable Scope (Global vs Local)
Output:
Variables inside functions do not change the global ones unless specified.
10. Dynamic Typing
Python is dynamically typed — you can reassign variables to different types.
Be careful in ML code! Accidentally changing data types can cause bugs.
11. Example in a Mini Machine Learning Context
12. Using Variables in NumPy (Essential for ML)
Output:
Summary Table
Basic variable
x = 5
Auto-detects type
Multiple assign
x, y = 1, 2
Split values
Type check
type(x)
Debugging help
Update value
x += 1
Shortcut for updating
Store parameters
params = {"lr": 0.01}
Common in ML projects
Constants
EPOCHS = 100
Use ALL_CAPS
Keywords
variables in python, python variable types, declaring variables, variable naming rules, python data types, python for machine learning, python constants, variable scope, python assignment, python beginner guide, python tutorial, python variable examples, variable best practices, python dynamic typing, python debugging, python multiple assignment, machine learning python code, python type checking, python variables cheat sheet, python variable use in ML, nerd cafe
Last updated