Saturday, 5 July 2025

Python Variables – A Complete Beginner's Guide (Step-by-Step)

๐Ÿ Python Variables – A Complete Beginner's Guide (Step-by-Step)

Let's learn variables in Python in the most basic, most beginner-friendly manner possible — with examples, rules, and actual use cases. Whether you're a beginner or refreshing your basics, this tutorial will assist you in creating a strong foundation.


๐Ÿ”ต What is a Variable in Python?

In Python, a variable is just a label that refers to a value in memory.
You can imagine it as a labeled box that holds some data.

Here's an example:

x = 10

In this situation:

  • x is the variable name (the label)
  • 10 is the value being stored (the content of the box)

Python knows automatically that 10 is an integer — no type declaration needed!


๐Ÿ”ค How to Declare a Variable in Python

The syntax to declare a variable is simple:

variable_name = value

Examples:

name = "Steve"
age = 21
pi = 3.14
is_student = True

As you can notice, Python allows you to store text, numbers, decimals, and even Boolean values in variables.


✅ Rules for Naming Variables in Python

When defining variables, use these easy rules:

  1. Variable names should start with a letter (A–Z or a–z) or an underscore `_`
  2. They may contain numbers after the first character
  3. You cannot use Python keywords such as if, for, class, while, etc.
  4. Variable names are case-sensitive (e.g., Age and age are different)

๐Ÿงช Valid and Invalid Examples of Variable Names

✅ Valid:

name = "Adam"
_age = 30
num1 = 100

❌ Invalid:

# 1num = 50     → Begins with a number
# for = "loop"  → "for" is a reserved keyword

๐Ÿง  Python is Dynamically Typed

Python is a dynamically typed language, meaning you don't have to tell Python what type a variable is when you declare it. Python determines it for you.

x = 5         # x is an integer
x = "hello"   # Now x is a string
x = 3.14      # Now x is a float

Yes — you can assign various types of values to the same variable!


๐Ÿ“š Variable Types in Python

Here's a handy reference of general data types you can hold in variables:

TypeExample
intx = 5
floatpi = 3.14
strname = "A"
boolis_ok = True
listnums = [1, 2]
tuplet = (1, 2)
dictd = {"a": 1}

๐Ÿ” Assigning Multiple Values

Python allows assigning values to multiple variables in a single line:

a, b, c = 1, 2, 3

Assign the same value to multiple variables:

x = y = z = 100

๐Ÿ“ค Displaying Variable Output

You can use the print() function to display the value of a variable:

name = "Sweety"
print("Hello", name)

Output:

Hello Sweety

๐Ÿงฉ Best Practices for Naming Variables

  • ✅ Use descriptive names: total_price is preferred over tp
  • ✅ Use lowercase with underscores (snake_case): my_name, student_marks
  • ❌ Avoid single-letter names unless in short loops (i, j, etc.)
  • ❌ Don’t use confusing or vague names like x1, temp123, etc.

๐Ÿš€ What’s Next?

This is just the beginning of your Python journey. In the next blog, we’ll dive into Data Types and how they work with variables in more detail.

๐Ÿ‘‰ Stay tuned, and keep coding! ๐Ÿ’ป

Python Variables – A Complete Beginner's Guide (Step-by-Step)

๐Ÿ Python Variables – A Complete Beginner's Guide (Step-by-Step) Let's learn variables in Python in the most basic, most beginner...