newtum

Our Social Network

LEARN PYTHON: VARIABLES AND PRINT STATEMENTS

Learn Python: Variables and Print Statements

Python is one of the easiest programming languages to learn, and understanding variables and print statements is your first step. In this session, we'll build on our basic knowledge and explore how to assign values to variables and display them using print statements—with a real-world example of a cricket player.

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

1player = "Sachin Tendulkar"
2country = "India"
3matches = 463
4runs = 18426
5batting_avg = 44.83
6
7print("Name of Player:", player)
8print("Player from:", country)
9print("Matches Played:", matches)
10print("Total Runs:", runs)
11print("Batting Average:", batting_avg)
12
13# Clean output in one line
14print("\n--- Clean Output in One Line ---")
15print("Name of Player:", player, "\nPlayer from:", country, "\nMatches Played:", matches,
16"\nTotal Runs:", runs, "\nBatting Average:", batting_avg)

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.

Don’t worry about errors; every bug is a step closer to mastering Python.

— Manoj Kolhe

More Articles

Ready to Explore the Future of Technology?

Unlock the tools and insights you need to thrive on social media with Newtum. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.

Newtum

Newtum is an emerging online academy offering a wide range of high-end technical courses for people of all ages. We offer courses based on the latest demands and future of the IT industry.

© Newtum, Inc. All Rights Reserved.