Python Literals

In this Python tutorial, we will learn about how to create Python Literal in Python and we will also cover different examples related to Python Literal. And, we will cover these topics.

  • Python Literals
  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Special Literals
  • Literal Collections

Python Literals

Before moving forward we should have some piece of knowledge about literals. Literals are the items that represent the fixed value in the code. It is also defined as a raw value or data that is given in a variable or constant.

Examples:

In the following example, we create a raw value that is given in a variable and constant.

a = 28
b = 25.3
c = 4+7j
print(a, b, c)

Output:

After running the above code we get the following output in which we can see the data we get in the form of variables.

Python Literals
Python Literals

String Literals

String literals can produce by enclosing the text in a single(‘ ‘), double(” “), and triple quotes(”’ ”’).By using the triple quotes(”’ ”’) the text can be written in multiple lines.

Examples:

In the following example, we creating string literals in which text are enclosed in single(‘ ‘),double(” “), and triple(”’ ”’) quotes.

a = 'Pythontpoint'

b = "Pythontpoint"
 

c = '''Python
       t
       points'''
 
print(a)
print(b)
print(c)

Output:

After running the above code we get the following output in which we can see string literals is shown on the command prompt.

Python string literals
Python string literals

Numeric Literals

There are three types of numerical literals

  • Integer
  • Float
  • Complex

Integer

An integer is defined as both positive or negative numbers including 0. It does not include any fractional number.

Example:

In the following example, we create integer literals.

  • i= 0b101001 this is binary literal.
  • j = 30 this is decimal literal.
  • k = 0o350 this is octal literal.
  • l = 0x15b this is hexadecimal literal.

i= 0b101001


j = 30
 

k = 0o350
 
l = 0x15b
 
print(i, j, k, l)

Output:

After running the above code we get the following output in which we can see integer literal is formed.

Python integer numeric literals
Python integer numeric literals

Float

Float literal is defined as real numbers which contain both integer or fractional values.

Examples:


i = 20.8
j = 40.0
 
print(i, j)

Output:

After running the above code we get the following output in which we can see that float literals are created.

Python float numeric literal
Python float numeric literal

Complex:

Complex literals are defined as in which the numerical consist of both real and complex parts. Complex literal is in the form of a+bi where a is the real part and b is the complex part.

Example:

In this example, we created complex literals which consist of both real and complex parts.

a = 2 + 3j in this 2 is the real part and 3 is the complex part.

b = 7j here real part is 0 and only complex part is present.

a = 2 + 3j
 
# real part is 0 here.
b = 7j
 
print(a, b)

Output:

After running the above code we get the following output in which we can see that complex literal is formed.

Python complex numeric literals

Boolean Literals

Boolean literals are defined as a binary number that has one or two possible values. They are true or false.

Examples:

i = (1 == True)
j = (1 == False)
k = True + 5
l = False + 9
 
print("i is", i)
print("j is", j)
print("k:", k)
print("l:", l)

Output:

After running the above code we get the following output in which we can see the variable i is true and variable j is false. In boolean true is represented by 1 and false is represented by 0.

Python boolean literals
Python boolean literals

Special Literals

Python has a special literal called ‘None’.None is used for defining the null values. None return a false value.

Examples:

special_literal = None
print(special_literal)

Output:

After running the above code we get the following output in which we can see a special literal is shown on the command prompt.

Python special literals
Python special literals

Literal Collections

Literal collections are of for types:

  • List Literals
  • Dictionary Literals
  • Tuple literals
  • Set Literals

List Literals

List are mutable and contain all items of different data types. It separated all items by commas (,)and enclose them with square brackets ( [] ).

Example



numbers = [1, 2, 3, 4, 5]
names = ['Michael M Duckett', 'Charles S Jewell', 'Mark B Williams', 5]
print(numbers)
print(names)

Output:

After running the above code we get the following output in which we can list containing the item separated by commas and enclosed with square brackets.

Python list literals
Python list literals

Dictionary Literals

Dictionary contains the data in the form of key pair. The item present in the dictionary is separated by commas(,) and enclosed by curly braces.

Example


alphabet = {'a': 'Dog', 'b': 'Ant', 'c': 'Mango'}
personal_information = {'name': 'Brad D Green', 'age': 25, 'ID': 25}
 
print(alphabet)
print(personal_information)

Output

After running the above code we get the following output in which we can see the data is in the form of key-pair separated with commas and enclosed with curly braces.

Python dictionary literals
Python dictionary literals

Set Literals

Set is defined as the collection of unordered data set. It is the same as a dictionary which separate the item by commas(,) and enclosed the item with curly braces ( { } ).

Example


vowels = {'a', 'e', 'i', 'o', 'u'}
fruits = {"orange", "grapes", "mango"}
 
print(vowels)
print(fruits)

Output

After running the above code we get the following output in which we can show the set is created.The item present inside the set separate by commas and enclosed with square bracket( { } ).

Python set  literals
Python set literals

So, in this tutorial, discussed Python Literals and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Literals
  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Special Literals
  • Literal Collections

Do Follow:

Python Variables

Python Datatypes

24 thoughts on “Python Literals”

  1. I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…

    Reply
  2. of course like your web site however you need to check the spelling on several of your posts. Several of them are rife with spelling issues and I to find it very troublesome to inform the truth nevertheless I’ll definitely come again again.

    Reply
  3. I?¦ll immediately snatch your rss as I can’t to find your email subscription hyperlink or newsletter service. Do you have any? Kindly allow me recognise in order that I may just subscribe. Thanks.

    Reply
  4. I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this increase.

    Reply
  5. I like the valuable info you provide on your articles. I’ll bookmark your blog and test once more here frequently. I am quite certain I’ll be told plenty of new stuff proper right here! Good luck for the following!

    Reply
  6. There are some attention-grabbing deadlines in this article but I don’t know if I see all of them middle to heart. There’s some validity however I will take hold opinion till I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as properly

    Reply
  7. Magnificent beat ! I would like to apprentice whilst you amend your website, how could i subscribe for a blog site? The account aided me a appropriate deal. I were tiny bit acquainted of this your broadcast offered shiny transparent concept

    Reply
  8. You can definitely see your enthusiasm in the paintings you write. The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe. Always follow your heart. “In order to preserve your self-respect, it is sometimes necessary to lie and cheat.” by Robert Byrne.

    Reply
  9. If you’re considering a reliable and organic method to lose
    weight, FitSpresso should be on your list.

    With a boost in metabolism, FitSpresso allows to shed unwanted fat throughout the day.

    FitSpresso also targets hunger pangs, making it easier to follow a healthy
    eating plan.

    Reply
  10. The very core of your writing whilst sounding reasonable originally, did not work properly with me after some time. Someplace throughout the paragraphs you actually managed to make me a believer unfortunately only for a short while. I however have got a problem with your leaps in logic and one would do nicely to fill in those breaks. In the event you actually can accomplish that, I will definitely end up being impressed.

    Reply
  11. sugar defender ingredients As
    a person that’s always bewared about my blood glucose, locating Sugar Defender has been a
    relief. I really feel so much more in control,
    and my current exams have actually revealed positive
    improvements. Knowing I have a trusted supplement to support
    my regular offers me satisfaction. I’m so grateful for Sugar Defender’s
    impact on my wellness!

    Reply
  12. The claw at one finish of the top is a two-pronged arch used to pull nails
    out of wood. It is steel-headed, wood- or steel-handled, and used for driving nails and other fasteners.
    A rubber mallet is available in handy when you’re trying to unstick painted home windows or need to do light hammering on surfaces that can be damaged.

    Reply

Leave a Comment