Table Of Contents
Python variables and constants are the basic fundamentals. Lets see Python variables and constants
- Variables in python :- Variables are anything such as persons name, address, number etc.
- Variables definition : The name given to the memory location.
- Creating Variables
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
- Syntax :- Variable_name= value
- Variable example :- x=10
- There’s no need to declare new variables in Python. Just assign a value to a name where you need it: var = “Magic”
Variable Types in Python
- In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type.
- Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type:
- Example :- x=10print(x) => x=10x=15
print(x) => x=15
Variables rules in python
- A variable name must start with a letter or the underscore character
- Variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Variables in python program
variables in python
print("Variables in python")
print("abc=",abc) # x =10
abc=20 # Assign value to same variable
- Assign value to variable abc=10
- You can re-Assign value to same variable => abc=15
- Constant definition :- constant is a type of variable whose value cannot be changed.
- The variable with a fixed value is constant.
- In python, there is no concept of constants type.
- In case where user needs constant values then, User take care of the values which user wants to use as constants.
- Note :- No constants in python.
Read also :-
Data types in python