Variables and Constants in Python
In this tutorial, you will be learning about Variables and Constants in Python programming.
Topics Covered:
- What are Variables?
- Rules on Naming and Using Variables in Python.
- Strings or str
- Examples for Strings
- How to add tab spaces between strings?
- How to assign one string variable to another?
- Numbers in python
- Integers and Float
- Constants in python
- What are constants
- How constants are declared and assigned in python?
Variables and Constants in Python
What are Variables?
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.
Rules on Naming and Using Variables in Python
There are certain rules or restrictions on naming and using variables in Python. They are:
- In any programming language, a reserved word cannot be used as a variable.
- All the variables’ names must start with a letter or the underscore character.
- A number cannot be assigned as a variable.
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.
- A variable can be named using alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (For instance, Alpha, alpha and ALPHA are three different variables).
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
- String
- Number
Strings
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
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1123′,’_blank’)”]
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:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1125′,’_blank’)”]
a=(“DeveloperPublish) print(a) b=(“Developer\tPublish) print(b)
Output:
DeveloperPublish Developer Publish
How to assign one string variable to another?
To assign one string variable to another string, follow these steps:
- First, assign a string to a variable.
- Now you need to assign this particular variable to another variable.
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1128′,’_blank’)”]
a= “Developer Publish Academy” print (a) b=a print (b)
- The resulting print statement of ‘b’ will return the value of ‘a’.
Output
Developer Publish Academy Developer Publish Academy
Integers
Integers are represented by int, they store whole numbers, including negative numbers. Fractions are excluded from integers.
Example:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1130′,’_blank’)”]
a=-57 print(type (a))
Output:
<class 'int'>
Now let’s add two integers with variables in python
Code:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1131′,’_blank’)”]
a=-57 b=75 print(a+b)
Output:
18
Float
Float covers the fractions and decimal values in python.
Example:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1132′,’_blank’)”]
a=-57.57 print(type(a))
Output:
<class 'float'>
Constants in Python
What are Constants?
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.
How constants can be declared and assigned in Python?
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:
- First, create a file under the name constants.py. If you have an existing program, just open it.
- Enter the following code in the file
PI=3.14
- Now create a second file named as main.py
- Enter the following code in the file
import constant print(constant.PI)
- Now, run the main.py file
- You will get the value of PI as declared in your program.