Definition of Python Constants
Python Constants are basically those variables whose values do not change during the program implementation.
- Python is basically a programming language that makes complex programs easier to understand and implement also it is used in commercial applications.
- These constants are basically assigned to a document.
- The document consists of variables and functions which are therefore recorded in the main file.
- The constants are thus written inside this main file in capital letters and underscores by isolating the words.
Assigning and Declaring Python Constants
- The first and foremost thing to remember while declaring Python Constants is that the constant values should always be declared in capital letters/uppercase.
- The values declared should not be abbreviations of the names and it has to be declared completely.
- The first step is to create a new python file called constant.py and enter the values. We should always remember that the constant values should be fixed and not changeable. Also as seen in the snapshot, both the constants have been named in capital letters/uppercase.
PI = 3.14
GRAVITY = 9.8
- The next step is to create another file named mainzz.py and add the code, which means to say that we will import the constant values to the main file and later print the constants.
print (constant. GRAVITY)
- Finally, when we run the code, it will show us the values of these constants which are non-interchangeable.
Python Constants and Literals with Examples
Literals are also constant values in Python which are fixed. Hence, python mainly supports 2 types of literals which are,
- Numeric literals
- String Literals
1. Numeric Literals
These are numeric values which remain constant. There are 3 types of Numeric Literals which are,
When we speak of these 3 classes of built in literals, we assume that we can use any numerical value which is not the case. We need to select fixed values for each literal.
lit_1 = 0b1100
lit_2 = 202
lit_3 = 0o320
lit_4 = 0x13c
fllit_1 = 12.4
fllit_2 = 13.5e2
a = 4.16j
- Here we create a new file called lit.py to represent the literals. So, lit_1 represents binary literals, lit_2 represents decimal literals, lit_3 represents octal literals and lit_4 represents hexadecimal literals.
- Once we print these values, we get the output as shown in the first line of the above code.
- Similarly, we create values to the floating point literals which are decimal values and when we represent 13.5e2, it means 13.5*10^2 and thus we get the desired output as shown in line 2 of the above code.
- Finally, we add a complex literal. When the complex literal is assigned to a variable a and printed out, the operation which Python performs is a.real and a.imaginary and it gives both the values as shown in the above code.
2. String Literals
String literals are a series of keywords that are enclosed by a pair of quotations.
- Concatenating the string literals – Concatenation is a combination of more than one statement together. We use concatenation with escape characters.
message = "Hello World"
Here, \n is used to produce the message is a new line.
The string literals which can be added in multiple lines. The representation of a multiline string literal should always be enclosed in three double quotations (“””).
print("""Python helps me learn. Python helps me learn.
Python helps me learn. Python helps me learn.
These are the literals having only two values that are “True“ and “False“.
bool_1 = (1 == True)
bool_2 = (1 == False)
bool_3 = True + 8
bool_4 = False + 7
print("value is: ",bool_1)
print("value is: ",bool_2)
print("value is: ",bool_3)
print("value is: ",bool_4)
In Boolean constants/literals, the numeral 1 always represents True and the numeral 0 always represents False. Hence, in the above program, since 1=true, it adds up 8 and 1 to produce the output as 9 and since the numeral 1 is not equal to false, the program does not recognize and add up 7 with 1 and hence it remains constant.
There are various other types of literals such as Lists and tuple.
- Lists are basically adding values in square parenthesis and assigning it to a variable. These lists can be strings or integers and they can be fixed that is they are constant.
veggies = ["tomato","potato","onion"]
nums = [3,4,5,6]
Thus, we discussed about various python constants and also the built in python constants which will help us in designing and programming on different games, websites, software development, and many more. It also helps us enhance our programming skills. These Python basics helps us to get a good grip of the language which is used to perform various mathematical operations and implement these Python constants and variables in the commands. The syntax is very much different and easier compared to other programming languages.
This is a guide to Python Constants. Here we also discuss on assigning and declaring python constants along with python constants and literals with examples. You may also have a look at the following articles to learn more –