Search This Blog

Wednesday, 15 December 2021

What are literals in Python

 

Python Literals

free responsive website templates

Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as follows:

Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types IntegerFloat and Complex.

Example : How to use Numeric literals in Python?

a = 0b1010 #Binary Literals
b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal


#Float Literal
float_1 = 10.5
float_2 = 1.5e2


#Complex Literal
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)

When you run the program, the output will be:

10 100 200 300

10.5 150.0

3.14j 3.14 0.0

In the above program

  • We assigned integer literals into different variables. Here, is binary literal, is a decimal literal, is an octal literal and is a hexadecimal literal.
  • When we print the variables, all the literals are converted into decimal values.
  • 10.5 and 1.5e2 are floating point literals. 1.5e2 is expressed with exponential and is equivalent to 1.5 * 10^2.
  • We assigned a complex literal i.e 3.14j in variable x. Then we use imaginary literal (x.imag) and real literal (x.real) to create imaginary and real part of complex number

String literals

string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes.

Example : How to use string literals in Python?

strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"

print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)

When you run the program, the output will be:

This is Python
C
This is a multiline string with more than one line code.
Ünicöde
raw \n string

In the above program, This is Python is a string literal and is a character literal. The value with triple-quote """ assigned in the multiline_str is multi-line string literal. The u"\u00dcnic\u00f6de" is a unicode literal which supports characters other than English and r"raw \n string" is a raw string literal.

Boolean literals

A Boolean literal can have any of the two values: True or False.

Example : How to use boolean literals in Python?

x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)

When you run the program, the output will be:

x is True

y is False

a: 5

b: 10

In the above program, we use boolean literal True and False. In Python, True represents the value as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value of y is False because 1 is not equal to False.

Similarly, we can use the True and False in numeric expressions as the value. The value of is because we add True which has value of 1 with 4. Similarly, b is 10 because we add the False having value of 0 with 10.

Special literals

Python contains one special literal i.e. None. We use it to specify to that field that is not created.

Example : How to use special literals in Python?

drink = "Available"
food = None
def menu(x):
    if x == drink:
        print(drink)
    else:
        print(food)
menu(drink)
menu(food)

When you run the program, the output will be:

Available
None

In the above program, we define a menu function. Inside menu, when we set parameter as drink then, it displays Available. And, when the parameter is food, it displays None.

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

Example : How to use literals collections in Python?

fruits = ["apple", "mango", "orange"] #list
numbers = (1, 2, 3) #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'} #set

print(fruits)
print(numbers)
print(alphabets)
print(vowels)

When you run the program, the output will be

['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

In the above program, we created a list of fruitstuple of numbersdictionary dict having values with keys desginated to each value and set of vowels.

We will learn more about literal collections in Advance Tutorial.

Identifiers in Python and its usage

 

Python Identifiers

create a website for free

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for writing identifiers

  • Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all are valid example. 
  • An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine. 
  • Keywords cannot be used as identifiers
>>> global = 1
  File "< interactive input >", line 1
    global = 1
             ^
SyntaxError: invalid syntax
  • We cannot use special symbols like !, @, #, $, % etc. in our identifier.
>>> a@ = 0
  File "< interactive input >", line 1
    a@ = 0
         ^
SyntaxError: invalid syntax
  • Identifier can be of any length.

What are keywords in python and how to use them

 

Python Keywords

free portfolio web templates

There is one more restriction on identifier names. The Python language reserves a small set of keywords that designate special language functionality. No object can have the same name as a reserved word.

In Python 3.6, there are 33 reserved keywords

You can see this list any time by typing help("keywords") to the Python interpreter. Reserved words are case-sensitive and must be used exactly as shown. They are all entirely lowercase, except for False, None, and True. 

Trying to create a variable with the same name as any reserved word results in an error:
>>> for = 3
SyntaxError: invalid syntax

how to implement YOLOv3 using Python and TensorFlow

Object Detection with YOLOv3 Introduction YOLOv3 (You Only Look Once version 3) is a real-time object detection algorithm that can detect ob...