CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

08 April, 2022

Python Literals

 Programing Coderfunda     April 08, 2022     Python     No comments   

 

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:

  1. "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:

  1. 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:

  1. text1='hello\    
  2. user'    
  3. print(text1)  
'hellouser'  

2) Using triple quotation marks:-

Example:

  1. str2='''''welcome  
  2. to  
  3. SSSIT'''    
  4. 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 integers)Long(long integers)float(floating point)Complex(complex)
Numbers( can be both positive and negative) with no fractional part.eg: 100Integers of unlimited size followed by lowercase or uppercase L eg: 87032845LReal numbers with both integer and fractional part eg: -26.2In the form of a+bj where a forms the real part and b forms the imaginary part of the complex number. eg: 3.14j

Example - Numeric Literals

  1. x = 0b10100 #Binary Literals  
  2. y = 100 #Decimal Literal   
  3. z = 0o215 #Octal Literal  
  4. u = 0x12d #Hexadecimal Literal  
  5.   
  6. #Float Literal  
  7. float_1 = 100.5   
  8. float_2 = 1.5e2  
  9.   
  10. #Complex Literal   
  11. a = 5+3.14j  
  12.   
  13. print(x, y, z, u)  
  14. print(float_1, float_2)  
  15. 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

  1. x = (1 == True)  
  2. y = (2 == False)  
  3. z = (3 == True)  
  4. a = True + 10  
  5. b = False + 10  
  6.   
  7. print("x is", x)  
  8. print("y is", y)  
  9. print("z is", z)  
  10. print("a:", a)  
  11. 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

  1. val1=10    
  2. val2=None    
  3. print(val1)     
  4. 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

  1. list=['John',678,20.4,'Peter']    
  2. list1=[456,'Andrew']    
  3. print(list)    
  4. 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

  1. dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}  
  2. 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

  1. tup = (10,20,"Dev",[2,3,4])  
  2. 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

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

Output:

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

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Python StringPython StringTill now, we have discussed numbers as the standard data types in Python. In this section of the tutorial, we will discuss the most popul… Read More
  • Python While loopPython While loopThe Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tes… Read More
  • Python continue StatementPython continue StatementThe continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement sk… Read More
  • Python PassPython PassIn Python, the pass keyword is used to execute nothing; it means, when we don't want to execute code, the pass can be used to execute empty… Read More
  • Python break statementPython break statementThe break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loop… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...
  • SQL ORDER BY Keyword
      The SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts ...
  • Enabling authentication in swagger
    I created a asp.net core empty project running on .net6. I am coming across an issue when I am trying to enable authentication in swagger. S...
  • failed to load storage framework cache laravel excel
       User the export file and controller function  ..         libxml_use_internal_errors ( true ); ..Good To Go   public function view () : ...
  • AdminJS not overriding default dashboard with custom React component
    So, I just started with adminjs and have been trying to override the default dashboard with my own custom component. I read the documentatio...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Efficiently remove expired cache data with Laravel Cache Evict - 6/3/2025
  • Test Job Failures Precisely with Laravel's assertFailedWith Method - 5/31/2025
  • Prism Relay - 6/2/2025
  • Enhance Collection Validation with containsOneItem() Closure Support - 5/31/2025
  • Filament Is Now Running Natively on Mobile - 5/31/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda