EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Python Constants

Home » Software Development » Software Development Tutorials » Python Tutorial » Python Constants

Python Constants

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.

Example:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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.

import constant
print(constant.PI)
print (constant. GRAVITY)

  • Finally, when we run the code, it will show us the values of these constants which are non-interchangeable.

Python Constants-1.1

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,

  • Integer
  • Float
  • Complex

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.

Example:

lit_1 = 0b1100
lit_2 = 202
lit_3 = 0o320
lit_4 = 0x13c
fllit_1 = 12.4
fllit_2 = 13.5e2
a = 4.16j
print(lit_1,lit_2,lit_3,lit_4)
print(fllit_1,fllit_2)
print(a,a.imag,a.real)

Output:

Python Constants-1.2

  • 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.

Popular Course in this category
Free Python Course2+ Hours | Lifetime Access | Verifiable Certificates
4.5 (203 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)Angular JS Training Program (9 Courses, 7 Projects)
  • Concatenating the string literals – Concatenation is a combination of more than one statement together. We use concatenation with escape characters.

Example:

message = "Hello World"
print((message+"\n")*10)

Output:

Python Constants-1.3

Here, \n is used to produce the message is a new line.

Multiline Strings

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 (“””).

Example:

print("""Python helps me learn. Python helps me learn.
Python helps me learn. Python helps me learn.
""")

Output:

Output-1.4

Boolean Literals

These are the literals having only two values that are “True“ and “False“.

Example:

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)

Output:

Output-1.5

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.

Other Literals

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.

Example:

veggies = ["tomato","potato","onion"] nums = [3,4,5,6] print(veggies)
print(nums)

Output:

Output-1.6

Conclusion

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.

Recommended Articles

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 –

  1. Python Counter
  2. Python Sort Array
  3. Python Power Function
  4. Python Concurrency

Free Python Course

2+ Hours

Lifetime Access

Verifiable Certificates

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Python Tutorial
  • Basic Part II
    • Comments in Python
    • Finally in Python
    • Python Multiline Comment
    • Python Data Types
    • Python Variables
    • Python Variable Types
    • Python Global Variable
    • Python Variable Scope
    • Python Private Variables
    • Python Default Arguments
    • Python Command-line Arguments
    • Indentation in Python
    • Object in Python
    • Python Keywords
    • Python Literals
    • Pointers in Python
    • Iterators in Python
    • Python User Input
    • Python Enumerate
    • Python Commands
    • Type Casting in Python
    • Python Identifiers
    • Python Constants
    • What is NumPy in Python?
    • Cheat Sheet Python
  • Basics Part I
    • Introduction To Python
    • What Is Python
    • Careers in Python
    • Advantages of Python
    • Uses of Python
    • Python Features
    • Python Fast And python psyco
    • Python ImportError
    • Benefits and Limitations of Using Python
    • What can I do with?Python
    • Is Python a scripting language
    • Is Python Object Oriented
    • Is Python Open Source
    • Python Socket Programming
    • Useful Tips on Python Programming
    • Python You Should Be Using It
    • Python Web Development
    • Python Programming Beginners Tutorails
    • Practical Python Programming for Non-Engineers
    • Python Programming for the Absolute Beginner
    • Versions of?Python
  • Frameworks
    • Python Frameworks
    • Python Compilers
    • Python Editors
    • Best Compiler for Python
    • Python IDE for Windows
    • Python IDE on Linux
  • Installation
    • How To Install Python
    • Install Python on Linux
    • Install Python on Windows
    • Install Anaconda Python
  • Operator
    • Python Operators
    • Arithmetic Operators in Python
    • Python Comparison Operators
    • Logical Operators in Python
    • Assignment Operators in Python
    • Unary Operators in Python
    • String Operators in Python
    • Boolean Operators in Python
    • Identity Operators in Python
    • Python Bitwise Operator
    • Python Remainder Operator
    • Python Modulus Operator
  • Control Statement
    • Conditional Statements in Python
    • Control Statements in Python
    • If Condition in Python
    • If Statement in Python
    • If Else Statement in Python
    • else if Statement in Python
    • Nested IF Statement in Python
    • Break Statement in Python
    • Python Switch Statement
  • Loops
    • Loops in Python
    • For Loop in Python
    • While Loop in Python
    • Do While Loop in Python
    • Python Nested Loops
    • Python Infinite Loop
    • Python Event Loop
  • Sorting
    • Sorting in Python
    • Sorting Algorithms in Python
    • Bubble Sort in Python
    • Merge Sort in Python
    • Heap Sort in Python
    • Quick Sort in Python
    • Python Sorted Function
  • Function
    • Python Built-in Functions
    • Math Functions in Python
    • Python String Functions
    • Trigonometric Functions in Python
    • Python Input Function
    • Python Input String
    • Python String Operations
    • Python Stream
    • Python Multiline String
    • Python Regex
    • Python Regex Tester
    • Python regex replace
    • Python File Methods
    • Python Import CSV
    • Python Read CSV File
    • Python write CSV file
    • Python Delete File
    • Python File readline
    • Python if main
    • Python Main Method
    • List Method in Python
    • Python List Length
    • Recursive Function in Python
    • Copy List in Python
    • Python Range Function
    • Python Substring
    • Python list remove()
    • Python List Index
    • Python Set Function
    • Python len Function
    • Python eval()
    • Python Counter
    • ord Function in Python
    • strip Function in Python
    • Split Function in Python
    • Python Round Function
    • Python Map Function
    • Python String Join
    • Python format() Function
    • Python Contextlib
    • Python Compare Strings
    • Python Return Value
    • Python List count
    • Filter in Python
    • Python Slice String
    • Python Absolute Value
    • Python Trim String
    • Python Type Function
    • Lowercase in Python
    • Python xrange
    • Python yield
    • Python Find String
    • Max Function in Python
    • Python Power Function
    • pop() in Python
    • Python argparse
    • Python Pickle
    • Python Zip Function
    • Python Split String
    • super() in Python
    • Python Extend
    • Python String Replace
    • Python PEP8
    • Python Filter Function
    • Python if then else
    • Lambda in Python
    • Python BeautifulSoup
    • Python Sleep
    • Python Function Generator
    • Python @classmethod decorator
    • Python Endswith
    • Python BufferedReader
    • Python Async
    • Python Parser
    • Python SystemExit
    • Python pip
    • Python kwargs
  • Array
    • Arrays in Python
    • 2D Arrays In Python
    • 3d Arrays in Python
    • Multidimensional Array in Python
    • Python Array Functions
    • String Array in Python
    • Python Sort Array
    • Python Array Length
  • Inheritance
    • Inheritance in Python
    • Single Inheritance in Python
    • Multiple Inheritance in Python
    • Interface in Python
  • Exception
    • Python Exception Handling
    • Custom Exception in Python
    • Indentation Error in Python
    • Python IOError
    • Python EOFError
    • Python NotImplementedError
    • Python TypeError
    • Python ValueError
    • Python AssertionError
    • Python Unicode Error
    • Python NameError
    • Python StopIteration
    • Python OverflowError
    • Python KeyboardInterrupt
  • Advanced
    • Scope in Python
    • Python Collections
    • Constructor in Python
    • Destructor in Python
    • Python Overloading
    • Overriding in Python
    • Function Overloading in Python
    • Method Overloading in Python
    • Operator Overloading in Python
    • Method Overriding in Python
    • Encapsulation in Python
    • Static Method in Python
    • Assert in Python
    • Python References
    • Python Virtualenv
    • Python mkdir
    • Logistic Regression in Python
    • Dictionary in Python
    • Regular Expression in Python
    • Python Import Module
    • Python OS Module
    • Python Sys Module
    • Python Generators
    • Abstract Class in Python
    • Python File Operations
    • Sequences in Python
    • Stack in Python
    • Queue in Python
    • Tuples in Python
    • Python Magic Method
    • Python Sets
    • Python Set Methods
    • Priority Queues in Python
    • Reverse Engineering with Python
    • String Formatting in Python
    • Python isinstance
    • String Length Python
    • Python Concurrency
    • Python List
    • Python Initialize List
    • Python Unique List
    • Python Sort List
    • Python Reverse List
    • Python Empty List
    • List Comprehensions Python
    • List Operations in Python
    • Python Database Connection
    • Python SQLite
    • Python SQLite Create Database
    • Send Mail in Python
    • Bash Scripting and Python
    • Violent Python Book
    • NLP in Python
    • Matplotlib In Python
    • Gray Hat Python: Security
    • Python Subprocess
    • Python Threading Timer
    • Python Threadpool
    • Python Statistics Module
    • How to Call a Function in Python?
    • Python Curl
    • JSON in Python
    • Python json.dumps
    • Python Turtle
    • Python Unit Test
    • pass Keyword in Python
    • Tokenization in Python
    • Random Module in Python
    • Python Multiprocessing
    • Python getattr
    • Collection Module in Python
    • Print Statement in Python
    • Python Countdown Timer
    • Python Context Manager
    • File Handling in Python
    • Python Event Handler
    • Python Print Table
    • Python Docstring
    • Python Dictionary Keys
    • Python Iterator Dictionary
    • Python Class Attributes
    • Python Dictionary Methods
    • Namedtuple Python
    • Namedtuple Python
    • Namedtuple Python
    • Python Class Constants
    • Python Validation
    • Python Switch Case
    • Python Rest Server
    • Python Yield vs Return
    • Python Pickle vs JSON
  • Tkinter
    • Tkinter Widgets
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
  • Programs
    • Patterns in Python
    • Star Patterns in Python
    • Swapping in Python
    • Factorial in Python
    • Fibonacci Series in Python
    • Reverse Number in Python
    • Palindrome in Python
    • Random Number Generator in Python
    • Prime Numbers in Python
    • Armstrong Number in Python
    • Strong Number in Python
    • Leap Year Program in Python
    • Square Root in Python
    • Python Reverse String
    • Python Object to String
    • Python Object to JSON
    • Python Classmethod vs Staticmethod
  • Python 3
    • Python 3 Commands
    • Python 3 cheat sheet
  • Interview Question
    • Python Interview Questions And Answers

Related Courses

Python Certification Course

Programming Languages Courses

Angular JS Certification Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Free Python Course Learn More