๐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 that is 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 = "Nivesh"
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:
- Variable names should start with a letter (AโZ or aโz) or an underscore `_`
- They may contain numbers after the first character
- You cannot use Python keywords such as
if
,for
,class
,while
, etc. - Variable names are case-sensitive (e.g.,
Age
andage
are different)
๐งช Valid and Invalid Examples of Variable Names
โ Valid:
name = "Krishna"
_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:
Type | Example |
---|---|
int |
x = 5 |
float |
pi = 3.14 |
str |
name = "A" |
bool |
is_ok = True |
list |
nums = [1, 2] |
tuple |
t = (1, 2) |
dict |
d = {"a": 1} |
Each of these types is useful in different scenarios โ and weโll explore them in future lessons.
๐ Assigning Multiple Values
Python allows assigning values to multiple variables in a single line:
a, b, c = 1, 2, 3
You can also assign the same value to multiple variables at once:
x = y = z = 100
๐ค Displaying Variable Output
You can use the print()
function to display the value of a variable.
name = "Radha"
print("Hello", name)
Output:
Hello Radha
๐ Best Practices for Naming Variables
To write clean and readable Python code, follow these guidelines:
- โ
Use descriptive names:
total_price
is preferred overtp
- โ
Use lower case with underscores (snake_case):
my_name
,student_marks
- โ Don't use single-letter names unless in short loops (
i
,j
, etc.) - โ Avoid using cryptic or ambiguous names such as
x1
,temp123
, etc.
๐ป Stay tuned, and keep coding!
Python Variables (Step-by-Step Guide)
Top comments (0)