Curriculum
In this tutorial, you will be learning about Variables and Constants in Python programming.
In python, variables are used to store data. Unlike other programming languages, there is no command or declaration statement for declaring a variable, i.e. a variable can be created at any moment while writing a code to store a set of values/memory space. Therefore, any operation done using the variable affects its memory location.
For instance,
a =5
here ‘a’ is the variable and 5 is the value assigned to the variable.
Note: Python understands the type of variable instantly based on the value assigned to the variable.
There are certain rules or restrictions on naming and using variables in Python. They are:
For instance, take 5=7
Here you can’t assign 5 as a variable to store the value of 7. Python cannot process such types of data.
Tip: To maintain a clear python script, name your variables in relation to the subject area, so that the variable name clearly describes its purpose.
Now let us see the most commonly used variable types in detail
They represent a sequence or set of characters such as a message or text.
Strings are represented using quotation marks. You can use either single or double quotes to represent a string. Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexing from 0 to -1 at the end.
The plus (+) sign concatenation operator will add two strings
a = ('Developer publish') print (a) print (a[0]) print (a[2:5]) print (a[2:]) print (a * 2) print (a + "Academy" )
Output
Developer publish D vel veloper publish Developer publishDeveloper publish Developer publishAcademy
We can see that the text is printed without any spaces or gaps in between. Therefore, to add tab spaces, insert t to provide four tab spaces in between the characters.
Example:
a=(“DeveloperPublish) print(a) b=(“DevelopertPublish) print(b)
DeveloperPublish Developer Publish
To assign one string variable to another string, follow these steps:
a= “Developer Publish Academy” print (a) b=a print (b)
Output
Developer Publish Academy Developer Publish Academy
Integers are represented by int, they store whole numbers, including negative numbers. Fractions are excluded from integers.
Example:
a=-57 print(type (a))
Output:
<class 'int'>
Now let’s add two integers with variables in python
Code:
a=-57 b=75 print(a+b)
Output:
18
Float covers the fractions and decimal values in python.
Example:
a=-57.57 print(type(a))
<class 'float'>
Constants are similar to variables but with a difference that their value cannot be changed once given. In other words, constants are fixed values throughout the program.
Generally, Constants are separately declared and assigned in a module in Python. Here, the module is a new file; this file will contain all the variables, functions, and constant values. Then it is imported into the main file. Constants are written in capital letters with underscores separating the words within the module.
To declare a constant in python:
PI=3.14
import constant print(constant.PI)