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.
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.
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.
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.
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.
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.
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.
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.
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.
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( { } ).
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:
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!…
What Is Java Burn? Java Burn is a natural health supplement that is formulated using clinically backed ingredients that promote healthy weight loss.
You are a very intelligent individual!
Wow, superb weblog structure! How lengthy have you been blogging for? you make running a blog glance easy. The entire look of your web site is magnificent, as well as the content!
I love your writing style truly enjoying this website .
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.
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.
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.
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!
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
We stumbled over here by a different web page and thought I might check things out. I like what I see so i am just following you. Look forward to going over your web page for a second time.
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
You are my intake, I have few web logs and infrequently run out from to brand.
Hello my friend! I want to say that this post is awesome, nice written and include almost all important infos. I’d like to see more posts like this.
I like this site because so much useful stuff on here : D.
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.
You can definitely see your expertise within the work you write.
The sector hopes for even more passionate writers such as you who aren’t afraid to
mention how they believe. Always follow your heart.!
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.
Hello! Do you know if they make any plugins to help with Search Engine
Optimization? I’m trying to get my website to rank for some targeted
keywords but I’m not seeing very good gains. If you know of any please share.
Cheers! You can read similar text here: Wool product
Some truly wonderful work on behalf of the owner of this web site, utterly outstanding content material.
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.
Caution: Do not use a VOM except the appliance you need to test is unplugged or the facility to the circuit is turned off.
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!
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.