Literals
Literals - The data in itself
LITERALS
A literal is data whose value is determined by the literal itself
We can surmise its the speed of light or the lenght of a hypotenuse in Pythagaros' theorem. It can be anything and you cannot make a choice without further inofrmation!! And this is the clue: 123 is a literal and c is not
We use literals to encode data and put them into our code. Let's see what conventions we have to obey!!
print ("2")
print (2)
2
2
We can see one is a string ("2") and the other is an interger (2) They are stored in completely different ways inside the computers memory
the string exists as just a string - a series of letters
the integer is converted into bits
the
print ()
function is able to show both in a human readable form
Integers
Numbers handled by computers are of two types
Integers - whole numbers, no fractional part.
floating-point or (floats) are numbers that contain the fractional part
Octals and Hexadecimal Numbers
We can write numbers in an octal (Base 8) representation. If a number is preceded with a zero-oh (0o
) it will be treated as an octal value. This means that the number must contain digits from the [0...7] range only.
We can also write numbers in a hexadecimal (Base16) representation
0o123
is an octal number with a decimal value of 83
0x123
is a hexadecimal number with a decimal value of 291
Floats - Non empty decimal fraction :)
Floats are used in Python to represent numbers that aren't integers (whole numbers). Some examples of numbers that are represented as floats are 0.5 and -7.8237591. They can be created directly by entering a number with a decimal point, or by using operations such as division on integers.
With floats we use the point instead of a comma to indicate the decimal remainder
You can omit zero when it is the only digit in front or after the decimal point
04
can be written as.4
Computers can't store floats perfectly accurately, in the same way that we can't write down the complete decimal expansion of 1/3 (0.3333333333333333...). Keep this in mind, because it often leads to infuriating bugs!
Floats vs Integers
Look at the following and there is a difference between the 2
4.0 is a
floating-point
number4 is an
integer
For floating points its not only a point that indicates the number is a floating point but also the letter e
used in scientific notation.
For example speed of light is 300 000Km/s or 300 000 000 m/s which can be written as 3x10^8 m/s
or in Python its written as 3E8
(the letter E can also be used as a lower case e
)
The exponent has to be an integer
The base may be an integer
A float can be added to an integer, because Python silently converts the integer to a float.
Coding Floats
Lets take Planck's Constant which is 6.62607 x 10^ -34. In python it will be written as 6.63607E-34
Lets say we have decided to use the float literal 0.00000000000000000000001
print (0.00000000000000000000001)
1e-21
As you saw previously, dividing any two integers produces a float. A float is also produced by running an operation on two floats, or on a float and an integer.
print( 8 / 2 ) Ans=4.0
print( 6 * 7.0 ) Ans=42.0
print( 4 + 1.65 ) Ans=5.65
Using Quotes Inside Strings
When creating strings we surround them with quotes. Can either be single or double quotes.
>>>print ("Program 'Game Over' 2.0") Program
Program 'Game Over' 2.0
The double quotes above are like bookends. They tell the computer where the string starts and ends, so you can use as many single quotes you want between the "bookends" This would be the same if the bookends were single quotes, you can use as many double quotes between the single quote bookends as you want.
>>>print ('Program "Game Over" 2.0')
Program "Game Over" 2.0
print ("I'm Monty Python")
print ('I\'m Monty Python')
I'm Monty Python
I'm Monty Python
Boolean Values
To conclude with Pythion Literals, there are 2 more and represent 'truthfulness' Each time you ask Python if one number is greater than another, the questions results in the creation of some specific data - a Boolean value
This makes use of two values (and are case-sensitive) 'True' or 'False' denoted as 1
and 0
LAB
print('"I\'m\"\n' '""learning""\n' '"""Python"""')
Last updated
Was this helpful?