Chapter 3: Working with Variables in Python

In Python, variables are one of the fundamental concepts you’ll work with. They allow you to store data that your programs can manipulate and use. In this chapter, we’ll explore what variables are, how to check the Python version you’re using, and the different ways you can assign values to variables. By the end of this chapter, you’ll be comfortable working with variables and ready to use them in your Python programs.

Table of Contents


Getting Python Version

Before starting with Python basics, it’s important to ensure that you have Python installed correctly on your system. One of the first things you might want to do is check the version of Python you have installed. This is especially useful if you’re working with features that may differ between versions of Python.

 Checking the Python Version:

You can check the Python version using the `sys` module, which provides access to some variables used or maintained by the Python interpreter.

Try the code yourself:

Code Explanation:

import sys

The import statement is used to include library files in a Python script. If you have written C programs before, you might have used #include <stdio.h> to include standard input-output library functions. The import statement in Python serves a similar purpose. This line imports the `sys` module, which contains system-specific parameters and functions.

print(sys.version)

This line prints the version of Python that is currently running.

Example Output:

3.10.9 (main, Jan 23 2023, 22:32:48) [GCC 10.2.1 20210110]

This output shows that Python version 3.10.9 is installed, and it was built on September 23, 2023.


Now that we’ve ensured Python is installed and working correctly, let’s move on to understanding variables.

What is a Variable?

A variable is used to store data and work with it in your programs. For example, you can store numbers, strings (words or sentences), lists, and other types of data in variables. When you create a variable, you reserve some space in memory to hold that value. The interpreter allocates memory space for the variable based on the type of value assigned to it.

Rules for Creating Variables

When naming variables in Python, there are several rules and best practices to follow:

1. Variable Names Must Start with a Letter or an Underscore:

 A variable name can start with a lowercase (`a-z`) or uppercase (`A-Z`) letter or an underscore (`_`).

Example: `name`, `_name`, `Name`

2. Subsequent Characters Can Be Letters, Numbers, or Underscores:

After the first character, variable names can include numbers (`0-9`), but the first character cannot be a number.

Example: `name1`, `first_name`, `_name2`

3. Case Sensitivity:

Variable names are case-sensitive in Python. This means `name`, `Name`, and `NAME` would be considered three different variables.

4. Reserved Words or Keywords Cannot Be Used as Variable Names:

Python has a set of reserved words or keywords that cannot be used as variable names. Examples include `if`, `else`, `for`, `while`, `class`, `def`, etc.

Here are some examples demonstrating the rules for creating variables:


Different Ways to Assign Values to Variables

In Python, there are several ways to assign values to variables. This flexibility allows for more concise and readable code. Here are some common methods to assign values to variables.

Simple Assignment

As you have seen in the previous examples, you can assign a value to a variable using the equals (=) sign.

In the above code we have three variables , “a” , “temp” and “name” , the values of these variables are 10 , 20 and “peter”.

Note: In Python, we don’t have to specify the type of a variable before using it. Programming languages like C, C++, and Java require the data type to be declared first. In Python, a simple assignment operation (=) is enough to create a variable and assign data to it.

Try the code yourself:

 2. Multiple Assignment

You can assign values to multiple variables in a single line.

Here, `a` is assigned the value `10`, and `b` is assigned the value `20`.

3. Chain Assignment

You can assign the same value to multiple variables simultaneously.

`In this case, `x`, `y`, and `z` are all assigned the value `10`.

 4.Mixed Assignment

You can also assign different types of values to multiple variables in one line.

Here, “name” is assigned the value `peter` (a string), and `age` is assigned the value “20” (an integer).

Try the code :

Notes:

1. a = 10 and a = ’10’ are not the same. In the first command, the type of `a` is an integer, while in the second, `a` is a string.

2. You can use single quotes, double quotes, or triple quotes to create strings. There is no difference in functionality between single and double quotes in Python.

Different ways of Creating Strings:

You can assign string values to variables using single quotes (`’`), double quotes (`”`), or triple quotes (`”’` or `”””`).

In Python, there is no difference between using single quotes and double quotes when creating strings, as shown in above examples.

Creating Multi-line Strings:

Single quotes or double quotes can be used to assign a word or a single line to a variable. You can use triple single quotes (`”’`) or triple double quotes (`”””`) to create multi-line strings, as shown in the below example

Try the code:

These different ways of assigning values to variables in Python allow for flexible and readable code. Understanding how to use variables effectively is fundamental to programming in Python.



We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.