Data Types
Data Types
1.Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Number:
Number stores numeric values.
The integer, float, and complex values belong to
a Python Numbers data-type.
• Python creates Number objects when a
number is assigned to a variable. For example;
Python supports three types of numeric data.
1. Int - Integer value can be any length such as integers
10, 2, 29, -20, -150 etc. Python has no restriction on
the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers
like 1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal
points.
3. complex - A complex number contains an ordered
pair, i.e., x + iy where x and y denote the real and
imaginary parts, respectively. The complex numbers
like 2.14j, 2.0 + 2.3j, etc.
Python provides :
type() function to know the data-type of the variable.
isinstance() function is used to check an object belongs
to a particular class.
Example:
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
Sequence Type
String
• The string can be defined as the sequence of
characters represented in the quotation
marks.
• In Python, we can use single, double, or triple
quotes to define a string.
• Python provides built-in functions and
operators to perform operations in the string.
• the operator + is used to concatenate two
strings as the operation "hello"+" python"
returns "hello python".
• The operator * is known as a repetition
operator as the operation "Python" *2 returns
'Python Python'.
Strings indexing and splitting:
the indexing of the Python strings starts from 0
• str = "HELLO"
• print(str[0])
• the slice operator [] is used to access the
individual characters of the string.
• the : (colon) operator in Python to access the
substring from the given string.
Negative indexes :
• Python programming language supports negative
indexing of arrays, something which is not available
in arrays in most other programming languages.
• This means that the index value of -1 gives the last
element, and -2 gives the second last element of
an array.
Example:
arr = [10, 20, 30, 40, 50]
print (arr[-1])
print (arr[-2])
List
• Python Lists can contain data of different
types. The items stored in the list are
separated with a comma (,) and enclosed
within square brackets [].The lists are mutable
types.
• [],+,*
list1 = [1, "hi", "Python", 2]
Tuple
a.remove("hello")
print(a)
print(type(b))
b.remove(10)
print(b)
1.The following code example would print the
data type of x, what data type would that be?
x=5
print(type(x))
2. The following code example would print the
data type of x, what data type would that be?
x = "Hello World"
print(type(x))
3.The following code example would print the data type of x,
what data type would that be?
x = 20.5
print(type(x))
4.
x = ["apple", "banana", "cherry"]
print(type(x))
5.
x = ("apple", "banana", "cherry")
print(type(x))
x = {"name" : "John", "age" : 36}
print(type(x))
x = True
print(type(x))