We love it when someone describes fun things about us. What are these ‘fun things’ called? Characteristics. Another word for characteristics is called variables! In Python Programming, variables are similar to that of characteristics but in different variations. You can store various values like numbers, words and a lot more through variables and make it lively through code at any point of time! That’s the magic about Python. Python lets you develop n’ number of creations ranging from apps to games. And Variables make this magic appear lively. Wanna see how? Scroll down below to clearly understand how to effectively use variables with Python.
What is a Variable?
In Python, a variable is like a container where you can store information (data). Think of it like a box with a label on it. You can put something inside the box (like a number, a word, or other types of information) and give the box a name. Later, you can open the box to see what’s inside or even change what’s in the box.
Step 1: Naming a Variable
To create a variable, you just need to choose a name for it. There are a few rules for naming variables:
- The name can only contain letters, numbers, and underscores (_).
- The name cannot start with a number.
- The name should not be a Python keyword like if, else, while, or for.
Examples of good variable names:
age = 12 name = “Alice” is_student = True |
In the above examples:
- ‘age’ is the very variable that holds the number 12.
- ‘name’ is the variable that holds the text “Alice”.
- ‘is_student’ is a variable that holds the value True (meaning “yes” or “correct”).
Step 2: Storing Information in a Variable
When you create a variable, you “assign” information to it. You do this by using the equals sign (=). Here’s how it works:
my_number = 10 |
In this example, ‘my_number’ is a variable, and we are putting the number ‘10’ inside it.
You can also store other types of information, like text or words. In Python, words are called “strings” and they must be put inside quotation marks:
my_name = “Charlie” |
Step 3: Using Variables
Once you’ve created a variable, you can use it in your code. For example, if you want to print out the value of a variable, you can use the print() function:
my_age = 15 print(my_age) # This will print: 15 |
You can also change the value of a variable by re-assigning it a new value:
my_age = 15 print(my_age) # Prints: 15 my_age = 16 print(my_age) # Now prints: 16 |
Step 4: Types of Variables
There are different types of variables in Python depending on what kind of data you want to store:
- Integers: Whole numbers like 5, 10, or -3.
- Floats: Decimal numbers like 3.14 or 0.99.
- Strings: Words or sentences inside quotation marks like “Hello!” or “Python is fun!”.
- Booleans: True or False values (useful for yes/no or on/off situations).
Examples:
age = 10 # Integer height = 4.5 # Float name = “John” # String is_hungry = False # Boolean |
Step 5: Doing Math with Variables
You can also use variables to do Math. For example, if you have two numbers stored in variables, you can add, subtract, multiply, or divide them.
num1 = 7 num2 = 3 # Addition result = num1 + num2 print(result) # Prints: 10 # Subtraction result = num1 – num2 print(result) # Prints: 4 # Multiplication result = num1 * num2 print(result) # Prints: 21 # Division result = num1 / num2 print(result) # Prints: 2.33 |
Another small example where codes are easily understandable through variables:
radius = 5 pi = 3.14 area = pi * radius ** 2 print(area) # This prints: 78.5 |
In this example, the ‘pi’ and radius ‘variables’ make the code easier to understand.
Conclusion:
Wasn’t that interesting? Variables make your code easier to read and change. Instead of writing the same number again and again, you can now just store it in a variable and use the variable name. Want to learn more about coding? Jump right on to code camps for kids, where your kids learn to adapt to the digital world of coding through simple and fun learning experiences! Let them join their first trial class today!
Encourage learning everywhere you go and have fun with your coding journey! Happy Coding!