Python Essentials: a Fast Track to Key Concepts — Chapter 1: Variable Basics

Sajjad Hadi
4 min readJun 13, 2023

--

This image is generated by AI for the first chapter of Python Essentials: a Fast Track to Key Concepts.

Variables are fundamental elements in programming that allow us to store and manipulate data. In Python, variables play a crucial role in writing efficient and effective code. In this article, we will explore the basics of variables, including their introduction, assignment, naming conventions, and various variable types. Along the way, we will delve into real-world examples to help solidify your understanding.

Chapters of This Series

  1. Chapter 1: Variable Basics
  2. Chapter 2: Numbers
  3. Chapter 3: Strings
  4. Chapter 4: String Methods
  5. Chapter 5: Booleans
  6. Chapter 6: Lists

1. Introduction to Variables

In Python, a variable is like a labeled container that holds a value. It allows us to store and reference data for later use. Variables can represent various types of information, such as numbers, text, or complex data structures. Let’s consider a real-world example to illustrate this concept.

Real-world Example

Imagine you are building a simple calculator application. You need to store the user’s input for later calculations. Here’s how you can use variables in Python:

# Store the user's input in variables
num1 = 10
num2 = 5

In this example, we create two variables: num1 and num2. These variables hold the values 10 and 5, respectively. We can then use these variables to perform calculations or manipulate the data as needed.

Pay attention!

A comment in Python is a piece of text that is not executed as part of the program but serves as a note or explanation for human readers. Comments are prefixed with the # symbol, and they are used to provide context, clarify code functionality, or disable specific code segments temporarily. It is important to pay attention to comments as they can greatly improve code readability and maintainability. For example:

# This is a comment explaining the purpose of the following code
result = x + y # Adding the values of x and y
# a = 2

2. Variable Assignment

Variable assignment is the process of giving a variable a value. In Python, you can assign values to variables using the assignment operator =. Let's explore this through an example.

Real-world Example

Let’s consider a scenario where you want to keep track of the current temperature. You can assign the temperature value to a variable called current_temperature:

# Assigning a value to a variable
current_temperature = 25.5

Here, we assign the value 25.5 to the variable current_temperature. Now, you can use this variable throughout your code to access and manipulate the temperature data.

3. Variable Naming Conventions

When naming variables, it is essential to follow certain conventions to make your code more readable and maintainable. Here are some commonly accepted naming conventions:

  • Variable names must only contain alphabetical characters and numbers. We can also use underscores (_).
  • Numbers cannot be at the beginning of the names.
  • Variable names should be descriptive, indicating their purpose.
  • Use lowercase letters and underscores to separate words (e.g., student_name, total_score).
  • Avoid using reserved keywords (e.g., if, for, while) as variable names.
  • Be consistent with your naming style throughout your codebase.

Real-world Example

Let’s assume you are developing a program to track a user’s fitness progress. You can use meaningful variable names to enhance code readability:

# Tracking fitness progress
steps_per_day = 10000
calories_burned = 2500

In this example, we use variable names like steps_per_day and calories_burned to clearly indicate their purpose.

4. Variable Types

Python is a dynamically typed language, meaning that variables can hold values of different types. Here are some commonly used variable types in Python:

  • Numeric Types: Integers (int) and Floating-Point Numbers (float) are used to represent whole numbers and decimal numbers, respectively.
  • Strings: Strings (str) are used to represent sequences of characters, such as text or words.
  • Booleans: Booleans (bool) can hold either True or False values, representing logical conditions.
  • Lists, Tuples, Sets, and Dictionaries: These are used to store collections of values with different structures and purposes.

Real-world Example

Let’s say you are building an e-commerce application. You can use different variable types to represent product information:

# Product information
product_name = "Python Programming Book"
price = 29.99
in_stock = True

In this example, we use a string variable (product_name) to store the product name, a float variable (price) to store the product price, and a boolean variable (in_stock) to indicate if the product is in stock.

5. Conclusion

Understanding variable basics is crucial for any Python programmer. In this article, we explored the introduction to variables, variable assignment, naming conventions, and various variable types. By applying these concepts to real-world examples, you can enhance your understanding and gain practical experience. Variables are powerful tools that enable us to work with data dynamically, making Python a versatile programming language. Keep practicing and experimenting with variables to unlock the full potential of your Python programs.

If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.

--

--