🐍
Python For Starters
  • Overview
  • Python for starters
  • Content
  • Foreword
  • Introduction To Python
    • Python Installation
    • Variables
    • Python Data Types
    • Python Data Structures
    • Python numbers
    • Python strings
    • Python Boolean
    • Python constants
    • Python Comments
    • Type Conversion
    • Control flow in python
  • Python Functions
    • Function Definition
    • Function Parameter
    • Recursive Functions
    • Using Global Variables in Functions
    • Lambda Functions
  • What are Data Structures
    • Python Lists
    • Python Tuples
    • Python Dictionaries
    • Python Sets
    • Iterable in python
    • Python Map, Filter & Reduce Functions
  • Error handling in Python
  • Loop ... Else Clause, Partial Functions & Type Hints
    • Partial Functions
    • Type Hints
  • Python Modules
  • Python Libraries & Packages
    • Python Packages
  • Directory & IO
    • Python IO Module
  • Pyenv
  • Virtual Environments
    • Pipenv
    • Virtualenv
  • Object Oriented Programming in Python
    • Class
    • Class Methods
    • Python Class Inheritance
    • Python Class Polymorphism
    • Python special methods
  • Appendix
  • Contributing
Powered by GitBook
On this page
  1. Introduction To Python

Variables

What are variables?

A variable in Python is a symbolic name that serves as a reference or pointer to an object. Once an object is assigned to a variable, it can be referred to by that name. However, the data is still contained within the object.

In Python, a variable is a named location in memory that stores a value. Variables are used to store data values in programs. When you create a variable, you specify a name for the variable and a value to store in the variable. You can then use the variable name in your program to refer to the value stored in the variable.

For example, you might create a variable named x and assign it the value 10. You can then use the variable x in your program to refer to the value 10.

# In this example, the variable x is created and assigned the value 10. 
# You can then use the variable x in your program like this:
variable = 1

# You can also assign a new value to a variable using 
# the assignment operator (=). For example:
variable = 2

# A variable with a type hint
variable_with_type: int = 1

print(variable_with_type) # print the result of our variable

Variable Rules in Python

  • A variable must start with a letter or the underscore character.

  • A variable cannot start with a number.

  • A variable can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )

  • Variables are case-sensitive (age, Age and AGE are three different variables)

PreviousPython InstallationNextPython Data Types

Last updated 1 year ago