0% found this document useful (0 votes)
2 views

Python Literals

The document provides an overview of Python literals, including string, numeric, boolean, special, and collection literals. It explains the different types of each literal, with examples demonstrating their usage in Python code. Additionally, it highlights the mutability of lists and tuples, and the structure of dictionaries and sets.

Uploaded by

kondasanjeevarao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Literals

The document provides an overview of Python literals, including string, numeric, boolean, special, and collection literals. It explains the different types of each literal, with examples demonstrating their usage in Python code. Additionally, it highlights the mutability of lists and tuples, and the structure of dictionaries and sets.

Uploaded by

kondasanjeevarao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16


Python Java JavaScript SQL C++ HTML CSS React
← prev next →

Python Literals
Python Literals can be defined as data that is given in a variable or constant.

Python supports the following literals:

1. String literals:
String literals can be formed by enclosing a text in the quotes. We can use both single
as well as double quotes to create a string.

Example:

"Aman" , '12345'

Types of Strings:

There are two types of Strings supported in Python:

a) Single-line String- Strings that are terminated within a single-line are known as
Single line Strings.

Example:

text1='hello'

b) Multi-line String - A piece of text that is written in multiple lines is known as


multiple lines string.

There are two ways to create multiline strings:

1) Adding black slash at the end of each line.


Example:

text1='hello\
user'
print(text1)

'hellouser'

2) Using triple quotation marks:-

Example:

str2='''''welcome
to
SSSIT'''
print str2

Output:

welcome
to
SSSIT

II. Numeric literals:


Numeric Literals are immutable. Numeric literals can belong to following four
different numerical types.

Int(signed Long(long float(floating Complex(complex)


integers) integers) point)

Numbers( can be Integers of Real numbers with In the form of a+bj


both positive and unlimited size both integer and where a forms the
negative) with no followed by real part and b
fractional part.eg: lowercase or fractional part eg: forms the
100 uppercase L eg: -26.2 imaginary part of
87032845L the complex
number. eg: 3.14j

Example - Numeric Literals

x = 0b10100 #Binary Literals


y = 100 #Decimal Literal
z = 0o215 #Octal Literal
u = 0x12d #Hexadecimal Literal

#Float Literal
float_1 = 100.5
float_2 = 1.5e2

#Complex Literal
a = 5+3.14j

print(x, y, z, u)
print(float_1, float_2)
print(a, a.imag, a.real)

Output:

20 100 141 301


100.5 150.0
(5+3.14j) 3.14 5.0

III. Boolean literals:


A Boolean literal can have any of the two values: True or False.

Example - Boolean Literals

x = (1 == True)
y = (2 == False)
z = (3 == True)
a = True + 10
b = False + 10

print("x is", x)
print("y is", y)
print("z is", z)
print("a:", a)
print("b:", b)

Output:

x is True
y is False
z is False
a: 11
b: 10

IV. Special literals.


Python contains one special literal i.e., None.

None is used to specify to that field that is not created. It is also used for the end of
lists in Python.

Example - Special Literals

val1=10
val2=None
print(val1)
print(val2)

Output:
10
None

V. Literal Collections.
Python provides the four types of literal collection such as List literals, Tuple literals,
Dict literals, and Set literals.

List:

List contains items of different data types. Lists are mutable i.e., modifiable.

The values stored in List are separated by comma(,) and enclosed within square
brackets([]). We can store different types of data in a List.

Example - List literals

list=['John',678,20.4,'Peter']
list1=[456,'Andrew']
print(list)
print(list + list1)

Output:

['John', 678, 20.4, 'Peter']


['John', 678, 20.4, 'Peter', 456, 'Andrew']

Dictionary:

Python dictionary stores the data in the key-value pair.

It is enclosed by curly-braces {} and each pair is separated by the commas(,).

Example

dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}


print(dict)

Output:

{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}

Tuple:

Python tuple is a collection of different data-type. It is immutable which means it


cannot be modified after creation.

It is enclosed by the parentheses () and each element is separated by the


comma(,).

Example

tup = (10,20,"Dev",[2,3,4])
print(tup)

Output:

(10, 20, 'Dev', [2, 3, 4])

Set:

Python set is the collection of the unordered dataset.

It is enclosed by the {} and each element is separated by the comma(,).

Example: - Set Literals

set = {'apple','grapes','guava','papaya'}
print(set)

Output:
{'guava', 'apple', 'papaya', 'grapes'}

Next Topic Python Operators

← prev next →

Related Posts

Hello World Program in Python


With the rapid growth of Python in new technologies, it continues to define the
future of software development, which makes it an important skill in
today's tech environment. In the following tutorial, we are going to make
our first Python program. Python Program to Print - Hello,...

 4 min read

Python Tutorial
| Python Programming Language Python is one of the most popular and widely
used programming language in nowadays, because of its simplicity, extensive
features and support of libraries. Python also have clean and simple syntax
which makes it beginner-friendly, while it also provides powerful libraries and
frameworks...

 4 min read

Python Comments
We'll study how to write comments in our program in this article. We'll also learn
about single-line comments, multi-line comments, documentation strings, and
other Python comments. Introduction to We may wish to describe the code we
develop. We might wish to take notes of why a...

 3 min read

Python Features
Features of Python From the development of Ada Lovelace's machine algorithm
in 1843 to the latest and popular high-level programming languages like C++,
Python, and Java, a wide range of programming languages have been
developed by programmers in order to solve real-world challenging problems.
However, not...

 13 min read

History of Python
Programming Language Python ranks among the most used and flexible
coding languages of the present day. It is also incredibly easy to read, write,
implement, and is well-supported with a variety of libraries. Based on these
factors, Python is renowned for numerous functions like building...

 5 min read

How to Install Python?


Introduction: Python is among the most widely used programming languages.
Due to its user-friendliness, readability, and extensive library ecosystem, whether
you are a novice or an expert developer, the first step to accomplish this is to
install Python on Windows if you wish to write and...

 4 min read

Python Operators
In the following tutorial, we will discuss about the operators used in the Python
programming language. An Introduction to Operators in Python In general,
Operators are the symbols used to perform a specific operation on different
values and variables. These values and variables are considered as the...

 13 min read

Python Keywords
Every scripting language has designated words or keywords, with particular
definitions and usage guidelines. Python is no exception. The fundamental
constituent elements of any Python program are Python keywords. This tutorial
will give you a basic overview of all Python keywords and a detailed discussion
of...

 13 min read

Python Applications
Applications of Python Programming Python is known for its general-purpose
nature that makes it applicable in almost every domain of software
development. Python makes its presence in every emerging field. It is the
fastest-growing programming language and can develop any application. Top 10
Here, we are specifying...

 3 min read

Subscribe to Tpoint Tech


We request you to subscribe our newsletter for upcoming
updates.

Your Email Subscribe 


Learn Important Tutorial

Python Java
Javascript HTML

Database PHP

C++ React

B.Tech / MCA

Data
DBMS
Structures

Operating
DAA
System
Computer Compiler
Network Design

Computer Discrete
Organization Mathematics

Ethical Computer
Hacking Graphics

Web Software
Technology Engineering

Cyber
Automata
Security
C C++
Programming

Java .Net

Python Programs

Control Data
System Warehouse

Preparation

Aptitude Reasoning
Verbal Interview
Ability Questions

Company
Questions

You might also like