Python Crash Course for Beginners | Python Constants and Literals
In Python, constants and literals are two important concepts that are used to represent fixed values in a program. Constants are variables whose values cannot be changed, while literals are values that are directly written into a program’s code. In this article, we will discuss constants and literals in detail and provide coding examples to illustrate their usage.
Python Constants
In Python, a constant is a variable whose value cannot be changed once it has been assigned. Constants are typically used to represent fixed values such as mathematical constants (e.g., pi) or configuration settings that do not change. There is no built-in support for constants in Python, so developers conventionally use all capital letters to indicate that a variable is a constant. Here is an example:
PI = 3.14
In this example, we have created a constant named PI
and assigned it the value of 3.14
. Once a value is assigned to PI
, it cannot be changed.
Python Literals
In Python, a literal is a value that is directly written into a program’s code. Literals can be used to represent various types of data such as numbers, strings, and boolean values.
Numeric literals
Numeric literals are used to represent numbers in Python. There are several types of numeric literals in Python, including integers, floating-point numbers, and complex numbers. Here are some examples:
# Integer literal
x = 5
# Floating-point literal
y = 3.14
# Complex number literal
z = 3 + 4j
In this example, we have created three variables named x
, y
, and z
, each of which is a numeric literal.
String literals
String literals are used to represent text in Python. A string literal is simply a sequence of characters enclosed in quotation marks. Here is an example:
my_string = "Hello, world!"
In this example, we have created a variable named my_string
and assigned it the value of the string literal "Hello, world!"
.
Boolean literals
Boolean literals are used to represent the truth values True
and False
in Python. Boolean literals are often used in conditional statements to control the flow of a program. Here is an example:
is_raining = True
is_sunny = False
In this example, we have created two variables named is_raining
and is_sunny
, each of which is a boolean literal.
In summary, constants and literals are important concepts in Python that allow developers to represent fixed values in a program. Constants are variables whose values cannot be changed once they have been assigned, while literals are values that are directly written into a program’s code. By understanding these concepts and practicing with coding examples, developers can create powerful and flexible Python programs that can solve a wide range of problems.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.