BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Variables with examples

Last Updated: March 29, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

Variables are used to store data, they take memory space based on the type of value we assigning to them. Creating variables in Python is simple, you just have write the variable name on the left side of = and the value on the right side, as shown below. You do not have to explicitly mention the type of the variable, python infer the type based on the value we are assigning.

num = 100      #num is of type int
str = "Chaitanya"      #str is of type string

Variable name – Identifiers

Variable name is known as identifier. There are few rules that you have to follow while naming the variables in Python.
1. The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in Python which means num and NUM are two different variables in python.

Python Variable Example

num = 100
str = "BeginnersBook"
print(num)
print(str)

Output:
Python variables

Python multiple assignment

We can assign multiple variables in a single statement like this in Python.

x = y = z = 99
print(x)
print(y)
print(z)

Output:

99
99
99

Another example of multiple assignment

a, b, c = 5, 6, 7
print(a)
print(b)
print(c)

Output:
Python multiple variables assignment in a single line

Plus and concatenation operation on the variables

x = 10
y = 20
print(x + y)

p = "Hello"
q = "World"
print(p + " " + q)

Output:
Python variable example

However if you try to use the + operator with variable x and p then you will get the following error.

unsupported operand type(s) for +: 'int' and 'str'

Data Types

A data type defines the type of data, for example 123 is an integer data while “hello” is a String type of data. The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed

Immutable data types in Python are:
1. Numbers
2. String
3. Tuple

Mutable data types in Python are:
1. List
2. Dictionaries
3. Sets

❮ PreviousNext ❯

Top Related Articles:

  1. Python Keywords and Identifiers with examples
  2. Python Recursion
  3. Python Constructors – default and parameterized
  4. Python List with examples
  5. Python Data Types

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap