Understanding Variables and Print Statements in Python
Variables are like containers in Python that store data. These can be text, numbers, or even more complex data. But before you start using them, there are a few simple rules you must follow:
- Variable names must be a single word.
- They can include alphabets, numbers, and underscores, but must start with an alphabet.
- They should not be Python keywords (e.g., print, type, etc.).
Let’s take a practical example. Suppose we want to store details of a famous cricketer like:
- Name
- Country
- Total matches played
- Total runs scored
- Batting average
In Python, we don’t need to declare data types explicitly. Just assign a value, and Python will handle the rest. After storing the data, we’ll use print() statements to display it. Initially, the output may look unorganized, but we can enhance it by formatting our print statements for clarity.
We’ll also demonstrate how to simplify multiple print statements into a single line using newline characters (\n). This keeps our code clean and readable.
Sample Python Code
Code Breakdown
- player, country, matches, runs, batting_avg: These are variables storing player data.
- Each print() statement displays one piece of information.
- The last print() shows how to format output in a single statement using \n for new lines.
- String values are wrapped in quotes, while numbers are written directly.
- x = "Python Programming": Assigns a string to variable x.
- Python manages all data types automatically—no need to declare them.
Output
Name of Player: Sachin Tendulkar
Player from: India
Matches Played: 463
Total Runs: 18426
Batting Average: 44.83
--- Clean Output in One Line ---
Name of Player: Sachin Tendulkar
Player from: India
Matches Played: 463
Total Runs: 18426
Batting Average: 44.83
Watch the Full Video
Check out our full YouTube video session to see this code in action and gain more insight into Python fundamentals: Watch on YouTube
Wrapping Up
In this blog, we explored how to declare and print variables in Python using a simple cricket player example. Practice this well—it forms the base for everything you’ll do in Python going forward. Keep experimenting, and remember: every expert was once a beginner.