Welcome back. In the last blog, we took our first step into the world of Python by writing classic “Hello World” Program. If you have not read that yet, you can check it out(Python Programming Language For Beginners). Today we’re going to build the foundation and explore two of the fundamental concepts in Python, that is variable and data types. Don’t worry, this terms sounds a bit of technical, but by the end of this post you will see how simple and intuitive they really are. Let’s dive in!
What Are Variables?
You can think that, Variables as a little containers that store information. They allow you to save data and use it later when you need. For example, imagine you are writing a program to calculate the total cost of groceries. You need a way to store the price of each item, right? That’s where the variables comes into the picture .
In Python, creating a variable is as easy as giving it a name and assigning a value to it. Here’s an example:
item_price = 5.99 quantity = 3 total_cost = item_price * quantity print(total_cost)
In this example:
item_price
stores the price of an item (5.99).quantity
stores how many items you’re buying (3).total_cost
calculates the total cost by multiplying the two.
When you run this code, it will print 17.97
—the total cost of your groceries. Simple, right?
Rules for Naming Variables
Before you start naming variables left and right, there are a few rules that you should keep in mind.
- Use descriptive names : Your given name should be self explanatory. So rather, than using X and Y, we can use names like age, name or total score. This make your code easier to understand.
- Start with letter or underscore : Very well names cannot start with a number. For example, 1st_place is invalid, but first please is fine.
- Use lower case and underscore : Python conventions remind using lower case letters and underscores for multi-word variable names.( e.g, user_name, total _amount.)
Understanding data types
In python, every piece of data has a type. Think of data types as categories that tell python. What kind of data you are working with? Here is the most common ones.
- Strings(str) : Used for text. Strings are enclosed in quotes like this.
name = "Alice" greeting = 'Hello, world!'
- Integers(int) : Used for whole numbers like 1,42 or -7
age = 25 score = 100
- Floats(float): Used for decimal numbers, like 3.14 or -0.5
pi = 3.14159 temperature = 98.6
- Booleans(bool): Used for true and false values.
is_raining = True has_passed = False
You can check the type of a variable using the type() function:
print(type(name)) # Output: <class 'str'> print(type(age)) # Output: <class 'int'> print(type(pi)) # Output: <class 'float'> print(type(is_raining)) # Output: <class 'bool'>
Putting it all together
Let’s write a simple program that uses variables and data types. Imagine you are creating a profile for user :
name = "Alice" age = 30 height = 5.5 is_student = True print("Name:", name) print("Age:", age) print("Height:", height) print("Is Student:", is_student)
When you run this code, it will print:
Name: Alice Age: 30 Height: 5.5 Is Student: True
Quick tip for beginners
As you start working with variables and data types, you might run into errors like forgetting to add code around a string or using wrong operator. That’s okay .Errors are your friends they are just telling you something needs to fix. Take your time see the error message and do not be afraid of experiment.
What’s next?
Now you’re understand variables and data types and you are ready to take the next step. In the next step we will explore the operators (like +, -, *, /) and how to perform the calculations in python.
But before we move on take a moment to play around with the code. Try to creating your own variables and mixing with different data types seeing what happens .The more you practice the more comfortable you’ll get.
That’s it for today! If you have any questions or want to share your progress drop a comment below remember every line of code you write is a step forward keep going you are doing great .Until next time happy coding .