Python Variables
- Read
- Discuss
Think of a variable as a bucket in which you can put anything. Let’s take a bucket and name it “x”, as shown in the figure below. In this bucket, you can put anything you want to; suppose we put “22” inside it. Now, whenever we ask the “x” what is inside you, it will tell us 22
Creating Variables
To declare a variable in Python, you simply need to assign a value to it. For example:
x = 10
This assigns the value 10 to the variable x. You can then use the variable x to refer to the value 10 throughout your code.
Variable Names
In Python, variable names can contain letters, numbers, and underscores. Variable names are case-sensitive, so x and X are considered to be different variables.
There are a few rules to follow when choosing a variable name:
- Variable names cannot begin with a number.
- Variable names should not contain spaces. Use underscores instead (e.g. my_variable).
- Variable names should not be the same as Python keywords (e.g. for, while, def, etc.).
Let’s see some more examples of variables.
x = 22
print(x)
y = "Hi! How are you?"
print(y)
Output:
22
Hi! How are you?
In the above code the
Leave a Reply
You must be logged in to post a comment.