Variables in python

Python is a powerful and beginner-friendly language. One of its most fundamental concepts is the use of variables. In this tutorial, you'll learn how to create and use variables in Python.

What Are Variables?

Variables act as containers for storing data values. In Python, you don't need to declare the data type of a variable. You simply assign a value to a variable, and Python handles the rest.

Creating Variables

To create a variable in Python, you use the assignment operator =. Here’s an example:

name = "Alice"
age = 25
is_student = True

In this example:

  • name: Holds a string value "Alice".
  • age: Holds an integer value 25.
  • is_student: Holds a boolean value True.

Using Variables

You can use variables in expressions or functions. For example:

greeting = "Hello"
name = "Alice"
print(greeting + ", " + name + "!")

Output:

Hello, Alice!

Variable Naming Rules

When naming variables in Python, follow these rules:

  • Variable names must start with a letter or an underscore (_).
  • Variable names can contain letters, numbers, and underscores but cannot start with a number.
  • Variable names are case-sensitive (age and Age are different).
  • Variable names cannot be Python keywords (e.g., if, for, while).

Example: Calculations with Variables

Variables are often used in calculations:

x = 10
y = 5
result = x + y
print("The result is:", result)

Output:

The result is: 15

Next Steps

Now that you understand variables, try creating your own! Experiment with different data types and operations to see how variables behave in Python. Happy coding! You can learn more python, try learning about the if else statement!