In this article, you will learn about the different types of literals in python.
A Literal in python is a raw data or a constant value that can get assigned to a variable. For example,
x = 100
Here 100 is literal, and we assigned it to a variable x.
There are different types of literals in python. For example,
- Numeric Literals
- String Literals
- Bool Literals
Numeric Literals
Numeric literals represent the numbers in python, and it can be of the following types i.e.
- Integer Literal:
- Positive & negative round numbers.
- For example, 100, -20, 15 etc.
- Float Literal:
- Positive & negative decimal numbers.
- For example, 10.89, -20.1, 15.22 etc.
- Binary Literal:
- The binary representation of numbers.
- For example, 00000110 is binary literal for 6
- Octal Literal:
- Octal representation of numbers.
- For example, o0027 is octal literal for 23
- Hexadecimal Literal:
- Hexadecimal representation of numbers.
- For example, ox0055 is hexadecimal literal for 85.
- Complex Literal: Represents the complex numbers.
- For example 5 + 7i
Boolean Literal
There are only two Boolean literals in python, i.e., True and False.
String Literals
A String Literal in python is a group of characters. For example,
x = "Sample String" y = 'Another String' print(x) print(y)
Output:
Sample String Another String
We can enclose the string literals in either single quotes or double quotes, or triple quotes. Here x & y are two different variables and they are referring to two other string literals.
Multi-line String Literals
While defining a string literal, there is no difference between single or double-quotes. We can create a string literal by either or them. But if our string literal is a little big and consists of multiple lines, then we need to use the triple quotes (‘’’) to enclose a multi-line string literal. For example,
x = """This is a little big string""" print(x)
Output:
This is a little big string
or we can use three single quotes too,
y = '''This is a little big string''' print(y)
Output:
This is a little big string
We can also use the single or double quotes to define multi-line string literals, but we need to end the lines with an escape character ‘\’. For example,
z = "This is a big \ string, seriously \ very big string." print(z)
Output:
This is a big string, seriously very big string.
Although we provided the string in multiple lines, but there are no newline characters in the string because we used the ‘\’ to break the line.
Escape characters in string literals
We can escape characters in a string literal. Escape character starts with a ‘\’, and each escape character serves a special purpose.
For example, if we want to have a quote inside a string literal like — Varun’s car –, then we need to tell the interpreter that the given quote does not represent the end of the string. Instead, it is a part of the string literal. We can do that using an escape character \’. For example,
x = 'Varun\'s car' print(x)
Output:
Varun's car
Here \’ is an escape character and represents a single quote in the string. Some of the other escape characters are,
- \ : Newline continuation
- \\ : Display a single \
- \’ : Display a single quote
- \” : Display a double quote
- \b : Backspace
- \n : New Line
- \t : Horizontal Tab
- \v : Vertical Tab
- \r : Enter
Conclusion:
There are three different types of literals in python. They are mainly used to initialize variables with hard coded values and sometimes also used in conditions.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.