Best Laptop
Python Tutorial 4 – Python Literals

Python Tutorial 4 – Python Literals

Hi Friends, I hope you would have liked the previous tutorial https://itkaksha.com/python-tutorial-3-writing-first-program/ about writing basic Python programs. Let`s now begin this tutorial on Python Literals.

Literals are something whose value is determined by the literal itself. They represent some fixed value. For ex:

  • 25 – it’s a literal because 25 defines itself as an integer
  • 10.5 – it’s a literal because it defines itself as a decimal/float number
  • “I love Python” – it’s a literal because it defines itself as a string
  • “2” – it’s a literal because it’s a string having character 2(since its string, “2” won`t be treated as integer)
  • c – now if we see this letter c, then it does not convey anything apart from the fact that it’s an alphabet. Hence it`s not a literal.

Python Literals are of 4 types – integers, floating numbers that have decimal part, strings and Boolean which carry only two values True or False. Based on the type of the literal, Python decides how to store that literal in memory.

Integers
They are numbers that do not carry fractional part. Representing them is very straight just we used to write these. 32, 15, 66 are integers.

However, there is one point when writing long numbers like this: 12,333,444. This is allowed on paper, but python does not accept comma separator. Either write simply 12333444 or 12_333_444. Python 3.6 and above allows underscores as digit separator.

To use negative number, just put a negative sign before the number. For positive number, you may use plus sign although not necessary.

For dealing with integers, we have heard of below type of number systems:

  • Binary – this is the number system that computer uses to perform calculations. Here integers are generated as powers of 2:
    111 in binary means 1*2^2 + 1*2^1+1*2^0 = 4 + 2 + 1 =7
  • Decimal – this is the number system that we normally use while writing the numbers. Integers are generated as powers of 10
    123 = 1*10^2 + 2*10^1 + 3*10^0 = 100 + 20 + 3

Apart from the above two, we have octal (power of 8) and hexadecimal (power of 16) way of representing numbers. Python has a way of representing number in Octal and hexadecimal system.

  • Octal Number:
    If any number is preceded by 0o(zero-o/O), then it will be treated in octal format. Also, when using this format, we can use numbers only from 1-7
    print(0o36) = 30
    3*8^1 + 6*8^0 = 24 + 6 = 30
  • Hexadecimal Number:
    If any number is preceded by 0x(zero-xX), then it will be treated in hexadecimal format.
    print(0x15) = 21 
    1*16^1 + 5*16^0 = 16 + 5 = 21

Floating Number/Decimal Number

  • These are numbers that have a decimal part like 10.8, -3.4 etc.
  • Also, in Python, you can write 0.4 as .4 also. Both are same.
    4.0 can we written as 4.
    This means that if there is only one zero on either side of the decimal, we can skip that.
  • Another way to write is a float number is having exponentials using e or E. E represents how may powers 10 have.
    5000000 = 5E6

    Here 6 is exponent which must be an integer. 5 is base which can be float also

    2.5 * 10^5 = 2.5E5

    Try below examples in python and experiment with more like these:

    print(0.00004) = 4E-5
    print(0.000003) = 3e-06
    print(5E4) = 50000.0
    print(2E-3) = 0.002
    print(700000000000000000000000000000.0) = 7e+29

Strings
Strings are text values/characters that we need to process. While using them, we need to enclose the string in quotes either single or double

print(“I love Python”)
print(‘I am writing Python Programs’)

How do we enclose a string inside a string?

  • Using escape character:
    print(“I am learning \”Python 3.0”\ version”)
    print(‘I am learning \’Python 3.0’\ version’)
  • Using different quotes inside a string
    print(“I am learning ‘Python 3.0’version”)
    print(‘I am learning “Python 3.0” version’)

Boolean
Another Python literals is Boolean literal that can hold only two values – True(1) and False(0). When we write a program to ask a question, Python replies in True or False.

print(1>2) - False
print(20>10) – True

print(True>False)
print(False>True)

This is because True holds value 1 while False is 0. 1 is greater than 0

Leave a Reply

Your email address will not be published. Required fields are marked *

Python 3.11.1 Features