L2
L2
L2
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric int, float, complex
Types:
Sequence list, tuple, range
Types:
Mapping dict
Type:
Set Types: set, frozenset
Boolean bool
Type:
Binary bytes, bytearray, memoryview
Types:
Getting the Data Type
• You can get the data type of any object by using the type() function:
Example:
Print the data type of the variable x:
x=5
print(type(x))
In Python, the data type is set when you assign a value to a variable:
If you want to specify the data type, you can use the following constructor
functions
Example
x = str("Hello World")
Print(x)
Print(type(x))
Output hello world
<class ‘str’>
Literals
A string literal can be created by writing a text(a group of Characters ) surrounded by the single(”),
double(“”), or triple quotes. By using triple quotes we can write multi-line strings or display in the
desired way.
# in single quote
s = 'geekforgeeks'
# in double quotes
t = "geekforgeeks"
# multi-line String
m = '''geek
for
geeks''‘
print(s)
print(t)
print(m)
Output:
geekforgeeks
geekforgeeks
geek
for
geeks
Numeric literals
Integer
Float
Complex.
1. Integer :
Both positive and negative numbers including 0. There should not be any fractional part.
Example:
# integer literal
# Binary Literals
a = 0b10100
# Decimal Literal
b = 50
# Octal Literal
c = 0o320
# Hexadecimal Literal
d = 0x12b
print(a, b, c, d)
Output:
20 50 208 299
In the program above we assigned integer literals (0b10100, 50, 0o320, 0x12b)
into different variables. Here, ‘a‘ is binary literal, ‘b’ is a decimal literal, ‘c‘ is
an octal literal and ‘d‘ is a hexadecimal literal. But on using print function to
display value or to get output they were converted into decimal.
2. Float
These are real numbers having both integer and fractional
parts.
Example:
# Float Literal
e = 24.8
f = 45.0
print(e, f)
Output:
24.8 45.0
3. Complex Literal
The numerals will be in the form of a+bj, where ‘a‘ is
the real part and ‘b‘ is the complex part.
Example:
z = 7 + 5j
# real part is 0 here.
k = 7j
print(z, k)
Output:
(7+5j) 7j
Boolean literals
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
In python, True represents the value as 1 and False represents the
value as 0. In the above example ‘a‘ is True and ‘b‘ is False because 1
equal to True.
Literal Collections
Example :
# List literals
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]
Tuple literals
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:
# Tuple literals
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)
Dictionary literals
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:
# Dict literals
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}
Set literals
Set is 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.
# Set literals
vowels = {'a', 'e', 'i', 'o', 'u'}
fruits = {"apple", "banana", "cherry"}
print(vowels)
print(fruits)
Output:
{'o', 'e', 'a', 'u', 'i'}
{'apple', 'banana', 'cherry'}
Special literals
Output:
None