September 8, 2024

Tirth's Blog

My technical notes

Python Basics: Introduction to Variables

1 min read

In Python, variables are fundamental for storing data. Variables allow you to hold and manipulate data values that can be referenced throughout your code.

**What is a Variable?**
A variable is essentially a name that refers to a value. Python makes it easy to create variables without specifying their type explicitly. This is part of Python’s dynamic typing system.

**Creating Variables**
To create a variable, simply assign a value to a name using the `=` operator:

x = 10
y = “Hello, Python!”
z = 3.14

**Using Variables**
You can use variables to perform operations and functions. For instance, adding two variables:

sum = x + 5
print(sum) # Output will be 15

**Variable Naming Rules**
– Variable names must start with a letter or underscore.
– Followed by letters, numbers, or underscores.
– Python is case-sensitive, so `myVar` and `myvar` are different variables.

**Examples**
Here’s a simple example of using variables:

name = “Alice”
age = 25
print(f”Name: {name}, Age: {age}”)

**Conclusion**
Understanding how to use variables is crucial for writing effective Python code. They provide a means to store and manipulate data efficiently.

Copyright © All rights reserved. | Newsphere by AF themes.