• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Python Literals

Python Literals

Author: Aditya Raj
Last Updated: December 28, 2022

While reading materials for programming in python, you must have encountered certain phrases like keywords, variables, constants and literals. In this article, we will study the underlying concepts for definition and usage of literals in python.

Table of Contents
  1. What Are Python Literals?
  2. What Are String Literals in Python?
  3. What Are Numeric Literals?
  4. What Are Boolean Literals?
  5. The special Literal – None 
  6. Literal Collections in Python
  7. Conclusion

What Are Python Literals?

Literals are the raw data that are assigned to variables or constants while programming. In python, we have different types of literals such as string literals, numeric literals, boolean literals and a special literal None. In the following sections, we will study each type of python literal and will also study about literal collections.

What Are String Literals in Python?

String literals are sequences of characters surrounded by single, double or triple quotes. In python we have two types of string literals namely single line strings and multiline strings. 

A single line string is the string literal that terminates on encountering a newline. It  can be defined by enclosing one or more characters inside single or double quotes as follows.

myString="This is a single line string"
anotherString='This is also a single line string'

A multiline string can be defined by enclosing characters expanding to multiple lines in triple quotes as follows.

myString="""This is
a
multiline string.
"""

What Are Numeric Literals?

Numeric literals are used to represent numbers in a program. In python we have different types of numeric literals such as integers, floating point numbers and complex numbers.

Integers in python are numbers with no fractional component. An integer representing a decimal number can be defined as follows.

myNum=1117

We can also define integers from other number systems.  A binary number is represented as a numeric literal starting with ‘0b’ and consists of digits 0 and 1 as follows.

myNum=0b10101

A number in the hexadecimal number system starts with ‘0x’ and can be represented in python as follows.

myNum=0x123ab

An integer literal in the octal number system starts with ‘0o’ and can be represented in python as follows.

myNum=0o124

A floating point literal in python represents a real number which consists of Integral as well as fractional part. A floating point number in the decimal number system can be represented as follows.

myNum=123.345

Complex numbers are in the form of a+bj where ‘a’ represents the real part and ‘b’ represents the imaginary part of the complex number. A numeric literal representing a complex number can be written as follows.

myNum=3+4j

What Are Boolean Literals?

In python, there are two types of boolean literals namely True and False. They can be defined in a program as follows.

myVar=True
myVar1=False

The special Literal – None 

The literal ‘None’ is used to specify that the variable to which it is assigned does not refer to any object. A variable with value ‘None’ can be defined as follows.

myObj=None

Literal Collections in Python

There are different types of collection objects such as python dictionaries, lists, sets and tuples in which we can store the python literals. 

We can store different types of data in a python list as follows.

myList=[1,2,"pfb",3.14]

We can even break a string literal at spaces using python string split operation and keep the substrings in a list as follows.

myStr="I am a single line string"
myList=myStr.split()
print("String literal is:",myStr)
print("Created list is:",myList)

Output:

String literal is: I am a single line string
Created list is: ['I', 'am', 'a', 'single', 'line', 'string']

A tuple also contains data just as lists but tuples are immutable and new elements cannot be added to it or any element cannot be deleted from a tuple. We can define a tuple as follows.

myTuple=(1,2,"pfb",3.14)

A python dictionary contains data in the form of key value pairs as follows.

myDict={1:1,2:"pfb","name":'Chris', "Value":100.5}

A set in python can hold unordered and non duplicate data . Each element in a set is unique. We can define a set containing several elements as follows.

mySet={1,2,"pfb",3.14}

Conclusion

In this article, we have learnt about different types of python literals and we have also seen how collections such as lists, dictionaries, tuples and sets are used to store literals in python.

To learn more about python programming, you can read this article on string manipulation. You might also like this article on how to use sys.argv list in Python.

Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us