Literals in Python
Let us explain it.
You can read or watch the explanation
Literals in Python :
Let’s understand the literal meaning. What is a literal?
A literal is data which is determined by itself. I know it is difficult for beginners to understand. Let’s take an example.
Look at the following set of digits.
321 - What would it be?
Can you guess what it is? Yes, as you guessed it is a Three hundred and twenty one.
Now, look at this one.
a - What it would be?
Can you guess exactly what it is?
It would be an amplitude or acceleration or some constant of something. But you cannot say what exactly it is. By this we can say that three hundred and twenty one is a literal because it is determined by itself and “a” is not a literal.
We understood the literal meaning. But, Can anyone say
why we use literals in Python?
We use literals to encode data and we keep this encoded data in our code. You will get to know about it soon.
Open your IDLE and write code with me as
save it and run it and check
What output did you get?
You would get the output of 2 identical lines.
But in the first print( ) you gave an integer and in the second print( ) you gave a string and both represented exactly the same output. But, internally computers store these values completely in different ways. Means It will take string as Series of letters and numbers will be converted to a set of bits
But, the print( ) function represents its output in human readable form. This is the Speciality of programming.
Conventions in Python :
Numbers are represented in 2 types:
- Integers
- Floating point - Which has a fractional part.
The distinction among them is very important because python will use them to store them in its memory.
Coming to the integers, How will you give a negative number? Obviously by adding ‘-’ on the left of the number as we did in maths in our school days. If you want to give a positive it is not necessary to mention ‘+’. However, You can add a plus sign if you wish to do it.
There are 2 additional conventions in python which are completely out of the world of mathematics and the first one allows us to do
Octal representation: if an integer is preceded with o0 or 0o. For example the octal number of 321 will be 209 and the print( ) function does this conversion automatically.
hexadecimal numbers: if an integer is preceded with 0x or 0X. For example the hexadecimal number of 321 will be 801 and the print( ) function manages these values too.
Floating point numbers:
Whenever we use a term like two and half or zero point four, the computer considers them as floating numbers. You can also write 0.4 as .4 as this is just a simple rule, You can omit zero when we have the only digit in front of or after the decimal point.
But it’s not only points that make a float.
Scientific notation - “e”
You can also use the letter “e”. Whenever you want to use any numbers which are very large or very small. You can use this Scientific notation.
For example, You want to write a number like.
400000000 - In Physics you would be writing as 4 * 10 power 8.
In python, we will be writing it as 4E8. you can also use lower case 4e8. where e is nothing but an exponent.
Where the value after e i.e., exponent value must be an integer because that many times Python multiplies 10. The value in front of the e i.e.,base may be an integer.
Up To now we have done with numbers i.e., integers and Floating point numbers. Let’s learn more in detail about strings.
Strings :
Strings are used to process text like names, addresses, etc. But not numbers. You already know a bit about them in the previous lessons that strings need quotes as Floating needs points.
I have one question for you. I want an output like I like “Python Programming”
How do we do it without generating an error?
If anyone knows the code then pause this video for a moment and drop the code in the comment box.
Let’s try to write the code this way.
what happens if you run this code. You will get SyntaxError: invalid syntax
Now Let’s try by changing quotes like
If you run it you will get an expected output. Now it’s working. So this is one way. There is another way. You can also use a backslash. So that python will understand that it is not a quote but data. Let’s try it, The code would be
By running it you will get the expected output. Now the question is
how can you add an apostrophe into a string already having apostrophes ?.
Like for example I want to print an output like
I’m Girl Coder
How can I do that?
One way is to keep this string between double quotes like this
You will get the output as you know and the other way is, All I can do is keep a backslash before an apostrophe though the string is between apostrophes like this one.
Run it and you will be getting the correct output. And one more thing you can also keep a string empty as well by keeping single or double quotes.
By this time we have learned about numbers and strings.
Boolean Convention :
There are 2 more additional ones in literals and these are not as obvious as before.
Before knowing them let’s write some code
Let’s give a = 10 and b = 10 and now try to print(a>b) and run it
What output will you get ?
You will be getting the output as False This is what you need to understand. You have written a bit of code and you are asking the python program a question that whether a is greater than b or not and Python is saying false which means a is not greater than b.
Now let’s modify code and this time
Now run What you got as output? You got True as output means Yes, b is greater than a yes…??.
What is True or False then… These are nothing but boolean values and these values belong to boolean literal.
This boolean literal has only 2 values:
- True and the other is
- False
Why are we getting the values as only either True or False? Why can’t it respond like Yes, They are, instead of True or No, they are not in place of False?
Because as we know computers know only 2 kinds of answers and that to binary values i.e., either 0 or 1 and the program must be able to react according to the received answers where 0 means False and 1 means True.
These values have strict denotations which means python responds by saying only True when the condition is satisfied and it says only False when the condition is not satisfied and these 2 boolean values are case sensitive.
So far you learned about
- Numbers - To represent numbers we have 2 literals
- Integers
- Float
- Strings - you’ve seen different ways to send the string using quotes
- Boolean - it has 2 values True and False
See you in the next blog.
Post a Comment
0Comments