GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Variable in Python Programming with Example | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Python Programming Tutorial
  • Python Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Typecasting
  • Operators in Python
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Identity Operators
    • Membership Operators
    • Operator Precedence
  • Input and Output in Python
    • Environment Setup
    • print() Function
    • input() Function
    • Comments
  • Decision Making in Python
    • Decision Making
    • if Statement
    • if else Statement
    • if elif Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Python
    • Loops
    • for Loop
    • while Loop
  • String in Python
    • String
    • String Methods
  • Built-in Data Structures in Python
    • Lists
    • Tuples
    • Sets
    • Dictionaries
  • Arrays in Python
    • Arrays
  • Searching in Python
    • Linear Search
    • Binary Search
  • Sorting in Python
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Python
    • Function
    • Recursive Function
    • Lambda Function
    • Closures
    • Decorators
    • Generators
  • OOPs in Python
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Class Variables
    • Static Methods
    • Inheritance
    • Types of Inheritance
    • MRO
    • Polymorphism
    • Abstract Classes
  • User-Defined Data Structures in Python
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Modules in Python
    • Modules
    • Time Module
    • Datetime Module
    • OS Module
    • Shutil Module
    • Sys Module
    • JSON Module
    • Random Module
  • Regular Expression in Python
    • Regular Expression
  • Exceptions in Python
    • Exception Handling
  • File Handling in Python
    • Text File Handling
    • Binary File Handling
    • CSV File Handling

bookmark
Table of Contents

    Variable in Python Programming with Example

    In this lesson, we will learn about the variable in Python programming and its application, along with an example.

    What is Variable

    A variable is like a container that stores information which can be used and manipulated by a program. Variables have names, and these names are used to access the stored data.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    Think variable as a box having a name where you can keep a piece of data that you can use later in your program. You can also change the data stored in a variable whenever you want.

    A variable has three key concept and they are:

    • Name: This is the label you give to the variable.
    • Value: This is the information you want to store in the variable.
    • Data Type: This indicates what kind of data the variable is holding (e.g., a number, text, etc.). We will discuss about it in our next lesson.

    Let's see some real-life examples of variables. Image you are organizing a birthday party and need to keep track of various details. So let's see how you can store those details in variables.

    You need to store the total number of guest coming to the party.

    You can use Variable Name: guest_count and store the value 20 in it like.

    guest_count = 20


    You need to store the name of the person whose birthday it is.

    birthday_person = "Alice"


    You need to remember the date of the party.

    party_date = "2024-06-15"


    You need to keep track of how much money you have for the party.

    budget = 5000.00


    You need to know if the party will be held indoors.

    is_indoors = True

    How to Declare Variable in Python

    Unlike other programming languages like C, C++ or Java, Python has no command for declaring a variable. A variable is created in Python the moment we assign a value to it.

    Let's see some examples for more understanding.

    Example 1

    To declare an integer and float variable in Python.

    x = 5
    x = 4.23

    Here, we have declared a variable x and assigned it the value 5, making it an integer variable. We can reuse the same variable to store a different type of value. As you can see on the second line, the same variable x is now used to store the float value 4.23, so the variable x now becomes a float variable.

    An integer variable is used to store whole numbers, such as 56, 8, and 493, which do not have a decimal part. A float variable, on the other hand, is used to store numbers with a decimal point, such as 2.3, 18.12, and 294.0.

    Example 2

    To declare a string variable in Python.

    y = "apple"

    Here, we have declared a variable y and assigned it the string value "apple", making it a string variable.

    Example 3

    To declare a boolean variable in Python.

    z = True

    Here, we have declared a variable z and assigned it the boolean value True, making it a boolean variable.

    A boolean variable is used to store logical values such as True or False.

    Example 4

    To declare multiple variables and assign multiple values in a single line.

    a, b, c, d = 5, 4.26, "apple", True

    Or

    a=5; b=4.26; c="apple"; d=True

    Here, we have declared four variables: a, b, c, and d, and assigned them the values 5, 4.26, "apple", and True, respectively, in a single line. This can be done in two different ways: using commas (,) or semicolons (;). You can use whichever method you prefer.

    Example 5

    To declare same value in multiple variables in a single line.

    a=b=c=15

    Here, we have declared three variables: a, b, and c, and assigned the same value of 15 to all of them in a single line.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.