Showing posts with label Python: Data types. Show all posts
Showing posts with label Python: Data types. Show all posts

Python: Data type

 Data types are the kind of data that is to be stored in the variable which is being used while writing a program. Python decides the data type of a variable during the execution of program by their syntax. For example string or character data type enclosed in a single or double quotes represents a string value. A list is represented by square brackets and tuples are enclosed in parantheses and curly brackets for a dictionary.

In Python, we don't need to declair a variable by mentioning it with its data type. The interpreter of Python automatically detects the type of data being entered. This is known as dynamic typing. 

We can verify the type of the program used variable. The type() function in Python returns the type of the passed variable.

For example:

x = 10
y = "Welcome to Pyhon"
z = 20.45
print(type(x))
print(type(y))
print(type(z))

Output

<type 'int'>
<type 'str'>
<type 'float'>

In Python, there are five standard data types:

  • numeric
  • dictionary
  • sequence type
  • boolean
  • set


Numeric: This data type stores numeric value in the program. It is used for mathematical calculations.
Syntax : variable_name=value
Program:
total_employee=250
Here, total_employee is the name of the variable and 250 is the value.
Python supports three types of numerical data types, like:
  • int (integers): These are positive or negative whole numbers with no decimal point. For example: 45, -98 etc
  • float (floating point real values): These are positive or negative with decimal point. For example: 0.2, -5.9 etc
  • complex: A complex number appear in the form of m + ni where 'm' is the real part and 'n' is the imaginary part and 'i' represents the square root of '-1'. 
Dictionary: These are a kind of key-value pairs. The keys may be any data type like strings or numbers and value is a Python object.  Dictionaries are enclosed by  curly braces. Commas are used to separate the values.
Syntax : dictionary_name  = {key:'value'}
For example x = {1:'girls', 2:'boys'}
Here, 1 and 2 are the 'keys' and 'girls' and 'boys' are value.

Let's see a Program
x = {1:'apple', 2:'mango', 3:'grapes', 4:'pears'}
# Printing dictionary
print (x)
# Accesing value using keys
print("1st fruit is "+x[1])
print("2nd fruit is "+ x[4])
print (x.keys())
print (x.values())

Output:
{1: 'apple', 2: 'mango', 3: 'grapes', 4: 'pears'}
1st fruit is apple
2nd fruit is pears
dict_keys([1, 2, 3, 4])
dict_values(['apple', 'mango', 'grapes', 'pears'])

Boolean: 'True' and 'False' are the two default values for boolean type of data in Python. These are used to decide the given assertion is valid or not. False can be represented by '0' or letter 'F' and True can be represented by any value that is not zero.

Let's see the Python program to check the boolean type
# Python program to check the boolean type
a = 8
b = 6
print(a>b)
print(a<b)

Output
True
False

Another program to check the Python data type:
print(type(True))
print(type(False))
print(false)

Output
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined   

Set: The data type's unordered collection is 'Set' data type. It is iterable and mutable (can change after creation). The elements of a set have no fixed order, it might return the element's altered sequence. 
Either a sequence of element is enclosed with a curly braces and separated by a comma or the built-in function set()  is used to create the set. It can contain different kinds of values.
Let's understand by following Python program:
# Creating Empty set
set1 = set()
set2 = {'Bharat', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(100)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)

Output:
{'Bharat', 'Python',3 , 2}
{'Python', 'Bharat', 3, 2, 100}
{3, 'Python', 'Bharat', 100}

Sequence Type:
Python supports three types of sequence data types: 
  • String
  • List and
  • Tuple
Strings: These are the sequence of characters in quotation mark. These quotation mark may be single, double or triple.
We can perform various tasks with the strings data types like:
"Hello" + " world" will return as Hello world. 
So, the + operator is used to combine two strings.
"Python"*2 will return PythonPython
So, the * operator is used to repeat the strings.
The demonstration of Python strings data types is in following program:
# String using double quotes
s = "Hi Python"
# String using triple quotes
s1 = '''Hello world
How are you?'''
print(s)
print(s1)

Output
Hi Python

Hello world
How are you?

Now see another program to handle the string data types in different ways:
s1 = 'Hello world'
s2 = ' How are you?'
print (s2[3]) #printing 3rd character of the string
print (s1[0:3]) #printing first three character using slice operator
print (s1*2) #printing the string twice
print (s1 + s2) #printing the concatenation of s1 and s2

Output
w
Hel
Hello worldHello world
Hello world How are you?

List: It is a compound data type, in which the items present in a list are separated by comma(,) and enclosed inside  square brackets[].
To gain access to the list data, we use slice [:] operators. The list can also be handled by concatenate operator (+), repeat operator (*) etc., in the same way as strings data can be handled.
list1 = [1, 2, 3, "Hello", "world", 4]
#Checking type of given list
print(type(list1))

#Printing the list1
print (list1)

# List slicing
print (list1[3:])

# List slicing
print (list1[0:2])

# List Concatenation using + operator
print (list1 + list1)

# List repetation using * operator
print (list1 * 3)

Output

<class 'list'>
[1, 2, 3, "Hello", "world", 4]
['Hello', 'world', 4]
[1, 2]
[1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4]
[1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4]

Tuple: This data type is similar to the list which consists of different values and separated by comma. But element of tuple is enclosed in parantheses () where as list is in square braces[]. One main difference between list and tuple is that we can change the size and elements of a list but a tuple can't be updated once declaired.

Let's look at the program below to understand the 'tuple':
tup = (2,4, "Hello", "Python", 6)
# Checking type of tup
print (type(tup))

#Printing the tuple
print (tup)

# Tuple slicing
print (tup[1:])
print (tup[0:1])

# Tuple concatenation using + operator
print (tup + tup)

# Tuple repatation using * operator
print (tup * 3)

Output:
<class 'tuple'>
(2, 4, 'Hello', 'Python', 6)
(4, 'Hello', 'Python', 6)
(2,)
(2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6)
(2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6)

Exercises:
1. Name the data type of a variable in Python that holds decimal values.
2. The data type having default values as 'True' and 'False' is?
3. How many types of sequence data types are supported by Python?
4. In which data type, there is no fix order of the elements during the execution of program?
5. Which data type is known as key-value pairs?
6. What is the difference between 'list' and 'tuple'?
7. Identify the numeric data types in following examples:
i. 98
ii. 1.0008
iii. -76
iv. x+yi
v. -0.009

 

Python: Functions