What is none literal in python with example

Generally, literals are a notation for representing a fixed value in source code. They can also be defined as raw values or data given in variables or constants. Python has different types of literal such as:

  1. String literals
  2. Numeric literals
  3. Boolean literals
  4. Literal Collections
  5. Special literals

What is String literals

A string literal can be created by writing a text[a group of Characters ] surrounded by a single[”], double[“”], or triple quotes.  By using triple quotes we can write multi-line strings or display them in the desired way. 

Example: Here geekforgeeks is a string literal that is assigned to a variable[s]. 

Python3

s = 'geekforgeeks'

t = "geekforgeeks"

m =

print[s]

print[t]

print[m]

Output

geekforgeeks
geekforgeeks
geek 
           for 
               geeks

What is Character literal

It is also a type of string literal where a single character is surrounded by single or double quotes.

Example:

Python3

v = 'n'

w = "a"

print[v]

print[w]

Output

n
a

What is Numeric literal

They are immutable and there are three types of numeric literal: 

  1. Integer 
  2. Float python
  3. Complex.

Integer:

Both positive and negative numbers including 0. There should not be any fractional part.

Example:

We assigned integer literals [0b10100, 50, 0o320, 0x12b] into different variables. Here, ‘a‘ is a binary literal, ‘b’ is a decimal literal, ‘c‘ is an octal literal, and ‘d‘ is a hexadecimal literal. But on using the print function to display a value or to get the output they were converted into decimal.

Python3

a = 0b10100

b = 50

c = 0o320

d = 0x12b

print[a, b, c, d]

Output

20 50 208 299

Float

These are real numbers having both integer and fractional parts.

Example:

24.8 and 45.0 are floating-point literals because both 24.8 and 45.0 are floating-point numbers. 

Python3

e = 24.8

f = 45.0

print[e, f]

Output

24.8 45.0

Complex 

The numerals will be in the form of a + bj, where ‘a‘ is the real part and ‘b‘ is the complex part.

Example:

Python3

z = 7 + 5j

k = 7j

print[z, k]

Output

[7+5j] 7j

What is Boolean literal

There are only two Boolean literals in Python. They are true and false. In Python, True represents the value as 1 and False represents the value as 0

Example 1:

In this example ‘a‘ is True and ‘b‘ is False because 1 is equal to True.

Python3

a = [1 == True]

b = [1 == False]

c = True + 3

d = False + 7

print["a is", a]

print["b is", b]

print["c:", c]

print["d:", d]

Output

a is True
b is False
c: 4
d: 7

Example 2:

Another example to show boolean literal.

Python3

x = [1 == True]

y = [2 == False]

z = [3 == True]

r = [1 == True]

a = True + 10

b = False + 10

print["x is", x]

print["y is", y]

print["z is", r]

print["a:", a]

print["b:", b]

Output

x is True
y is False
z is True
a: 11
b: 10

What are literal collections

Python provides four different types of literal collections:

  1. List literals
  2. Tuple literals
  3. Dict literals
  4. Set literals

What is List literal

The listcontains items of different data types. The values stored in List are separated by a comma [,] and enclosed within square brackets[[]]. We can store different types of data in a List. Lists are mutable.

Example: 

Python3

number = [1, 2, 3, 4, 5]

name = ['Amit', 'kabir', 'bhaskar', 2]

print[number]

print[name]

Output

[1, 2, 3, 4, 5]
['Amit', 'kabir', 'bhaskar', 2]

What is Tuple literal

A tuple is a collection of different data-type.  It is enclosed by the parentheses ‘[]‘ and each element is separated by the comma[,]. It is immutable.

Example: 

Python3

even_number = [2, 4, 6, 8]

odd_number = [1, 3, 5, 7]

print[even_number]

print[odd_number]

Output

[2, 4, 6, 8]
[1, 3, 5, 7]

What is Dictionary literal

Dictionary stores the data in the key-value pair. It is enclosed by curly braces ‘{}‘ and each pair is separated by the commas[,].  We can store different types of data in a dictionary. Dictionaries are mutable.

Example: 

Python3

alphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'}

information = {'name': 'amit', 'age': 20, 'ID': 20}

print[alphabets]

print[information]

Output:

{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'name': 'amit', 'age': 20, 'ID': 20}

What is Set literal

Setis the collection of the unordered data set. It is enclosed by the {} and each element is separated by the comma[,].

Example: We can create a set of vowels and fruits. 

Python3

vowels = {'a', 'e', 'i', 'o', 'u'}

fruits = {"apple", "banana", "cherry"}

print[vowels]

print[fruits]

Output

{'o', 'e', 'a', 'u', 'i'}
{'apple', 'banana', 'cherry'}

What is Special literal

Python contains one special literal [None]. ‘None’ is used to define a null variable. If ‘None’ is compared with anything else other than a ‘None’, it will return false.

Example:

Python3

water_remain = None

print[water_remain]

Output

None

What is literals in Python with example?

Generally, literals are a notation for representing a fixed value in source code. They can also be defined as raw value or data given in variables or constants. Example: Python3.

Is none a literal or keyword?

Special Literals in Python Python literals have one special literal known as None. This literal in Python is used to signify that a particular field is not created. Python will print None as output when we print the variable with no value assigned to it. None is also used for end of lists in Python.

What are the types of literals in Python?

Python provides the four types of literal collection such as List literals, Tuple literals, Dict literals, and Set literals.

How do you declare none in Python?

Examples.
# Declaring a None variable. var = None. ... .
# Declaring a None variable. var = None. ... .
# Declaring a variable and initializing with None type. typeOfNone = type[None] ... .
# Comparing None with none and printing the result. print [None == None] ... .
# Comparing none with False and printing the result. ... .
# Declaring an empty string..

Chủ Đề