In Tech Knowledge learn various technologies

Recent Post

Thursday, December 5, 2019

PYTHON DATA TYPES

PYTHON DATA TYPES

PYTHON DATA TYPES

Data type determines the type of data a variable can be stored and what type of operation can be performed on it. 


Python supports following types of data type 
1. None: It does not contain any value.
2. Numeric: It consists of numeric digits and they may or may not have decimal.


Int: It is a whole number, positive and negative without fraction (decimal). There is no limit for the size of an int.
Float: Float is a number, positive and negative with fraction (decimal).
Complex: Complex number is a combination of real and imaginary numbers.
Bool: It is the datatype which can be either a 1 (true) or a 0 (false).

Example-1
# 10 is assigned to num, num is an integer variable.
>>> num=10          

# sum variable stored the summation of value of num and 5.2.
>>> sum=num+5.2         

#print() function displayed the value of sum.
>>> print(sum)
15.2

#type() function displayed the data type of “sum”. 
>>> type(sum)
#Here, “sum” is a float type variable.
<class 'float'>   

#int() function to convert the type of sum variable to int data type. 
>>> x=int(sum)

#type() function displayed the type of “x”. 
>>> type(x)
#Here, “x” is an int type variable
<class 'int'>
  
Example-2
# 10 is assigned to num1, num1 is an integer variable.
>>> num1=10

# 4 is assigned to num2, num2 is an integer variable.
>>> num2=4

# the value of num1 is being multiplied by itself the value of num2
>>> result=num1 ** num2

#print() function displayed the value of result.
>>> print(result)
10000

Example-3
# 10 is assigned to num1, num1 is an integer variable.
>>> num1=10

# 4 is assigned to num2, num2 is an integer variable.
>>> num2=4

# result stored the remainder after division of num1 by num2.
>>> result=num1%num2

#print() function displayed the value of result.
>>> print(result)
2

Example-4
# 10 is assigned to num, num is an integer variable.
>>> num=10

# “+=” is a addition assignment operator. Here, 4 is add to num.
>>> num +=4               

#print() function displayed the value of num.
>>> print(num)
2

Example-5
# 4 is assigned to num1, num1 is an integer variable.
>>> num1=4

# 6 is assigned to num2, num2 is an integer variable.
>>> num2=6

# compare the value of num1 with the value of num2.
>>> result=num1<num2

#print() function displayed the value of result.
>>> print(result)
True

#type() function displayed the type of “result”. 
>>> type(x)
#Here, “x” is a boolean type variable
<class ‘bool’>


3. String: String is a sequence of characters within the single(‘ ‘), double (“ “), or triple(“”” “””)quotation marks.

Example-6
# “Hello” is a string with a single word within a double quotation.
>>> str1="Hello"

#print() function displayed the value of str1.
>>> print(str1)
Hello

#type() function displayed the data type of “str1”. 
>>> type(str1)
#Here, “str1” is a string type variable.
<class ‘str’>

# “Hello World” is a string with multiple words within a ("") .
>>> str2="Hello World"

#print() function displayed the value of str2.
>>> print(str2)
Hello World

# “Hello World” is a string with multiple words within a ('') .
>>> str3='Hello World'

#print() function displayed the value of str3.
>>> print(str3)
Hello World

#str4 stored a string with single and double quotation
>>> str4="It's is my first 'string' program"

#print() function displayed the value of str4.
>>> print(str4)
It's is my first 'string' program

4. List: It is a group of ordered elements which can be modified. It can stored different types of elements. Duplicate entries are also allowed. List of the elements are enclosed within square brackets [] and elements are separated by a comma (,). Elements of list can be access by its positive or negative indexing.
Example-7
# create a list and assigned into dataList variable
>>> dataList=[10,5,9,55,28]

#print() function displayed the value of dataList variable.
>>> print(dataList)
[10, 5, 9, 55, 28]

#print() displayed the value of index 0 of dataList variable.
>>> print(dataList[0])
10

#displayed the value of index 0 of dataList variable.
>>> dataList[0]
10

#displayed the value of index 4 of dataList variable.
>>> dataList[4]
28

#displayed the value of -1 last index of dataList variable.
>>> dataList[-1]
28

#replace the value of index 4 with 19.
>>> dataList[4]=19

#print() function displayed the value of dataList variable.
>>> print(dataList)
[10, 5, 9, 55, 19]

#after the last index of dataList variable append a new value 46.
>>> dataList.append(46)

#print() function displayed the value of dataList variable.
>>> print(dataList)
[10, 5, 9, 55, 19, 46]

#displayed the value of dataList variable.
>>> dataList
[10, 5, 9, 55, 19, 46]

#displayed the value of first two index (index 0 and 1) of dataList.
>>> dataList[:2]
[10, 5]

#type() function displayed the data type of “dataList”. 
>>> type(dataList)
<class 'list'>



5. Tuple: Tuple is a group of ordered elements which cannot be modified. Duplicate entries are also allowed. The elements of the tuple are enclosed within parentheses () and elements are separated by a comma (,). Tuples are read-only data type.

Example-8
# create a tuple and assigned into dataTuple variable
>>> dataTuple=(10,5,9,55,19)

#print() function displayed the value of dataTuple variable.
>>> print(dataTuple)
(10, 5, 9, 55, 19)

#displayed the value of index 0 of dataTuple variable.
>>> dataTuple[0]
10

#print() function displayed the value of index 0 of dataTuple.
>>> print(dataTuple[0])
10

#type() function displayed the datatype of “dataTuple”. 
>>> type(dataTuple)
<class 'tuple'>

6. Set: It is a group of unordered elements which can be modified. No duplicate entries are allowed. The elements of set are enclosed within braces {} and elements are separated by a comma (,). Its elements are not accessible using index.

Example-9
# Create a set and assigned into dataSet variable
>>> dataSet={10,5,9,55,19}

#print() function displayed the value of dataSet variable.
>>> print(dataSet)
{5, 9, 10, 19, 55}

#type() function displayed the datatype of “dataSet”. 
>>> type(dataSet)
<class 'set'>

7. Range: Range is a sequence of numbers and which cannot be changed.

Example-10
# create a range from 0 to 10
>>> dataRange=range(10)

#print() function displayed the range of dataRange variable. 
#Here, the range start from 0 to 10.
>>> print(dataRange)
range(0, 10)

#displayed the value of index 0 of the dataRange variable.
>>> dataRange[0]
0

#displayed the value of index 5 of the dataRange variable.
>>> dataRange[5]
5

#type() function displayed the datatype of “dataRange ”. 
>>> type(dataRange)
<class 'range'>

#create range from 4 to 20 
#the difference between the two numbers is 2
>>> dataRange1=range(4,20,2)

#displayed the value of index 0 of the dataRange1 variable.
>>> dataRange1[0]
4

#print() function displayed the range of dataRange1 variable. 
#Here, the range start from 4 to 20.
>>> print(dataRange1)
range(4, 20, 2)

#displayed the value of the range
>>> list(range(4,20,2))
[4, 6, 8, 10, 12, 14, 16, 18]

#displayed the value of index -1 (last index) of the dataRange1.
>>> dataRange1[-1]
18

#type() function displayed the datatype of “dataRange1”. 
>>> type(dataRange1)
<class 'range'>

8. Dictionary: It is a group of ordered elements in the form of key:value pair and which can be modified. No duplicate entries are allowed. Each key is separated from its value by a colon (:). The elements of dictionary are enclosed within braces {}.

Example-11
# Create a list and assigned into dataDict variable
>>> dataDict={1:'Joy', 2:'Smith', 3:'Jack'}

#print() function displayed the value of dataDict variable.
>>> print(dataDict)
{1: 'Joy', 2: 'Smith', 3: 'Jack'}

#displayed the value associated with the key ‘2’ of dataDict.
>>> dataDict[2]
'Smith'

#type() function displayed the datatype of “dataDict”. 
>>> type(dataDict)
<class 'dict'>

#keys() function displayed the list of keys within “dataDict”.
>>> dataDict.keys()
dict_keys([1, 2, 3])

#keys() function displayed the list of values within “dataDict”.
>>> dataDict.values()
dict_values(['Joy', 'Smith', 'Jack'])
















































No comments:

Post a Comment

please do not enter any spam link in the comment box