top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Datatypes of Python and Array operations



AGENDA –


What is Python?


Datatypes in Python.


What is an Array?


Basic array operations.


What is Python


Python is an open-source, general-purpose interpreted, popular and easily readable programming language available for the most popular operating systems. It is used in machine learning, web development, software development, desktop applications, and many other fields.


Datatypes in Python



NUMBERS


INTEGERS – It includes whole number values. e.g. x = 20


FOAT – It includes decimal values. e.g. x = 20.25


COMPLEX – Imaginary part added to the number. e.g. 20j


BOOLEAN


When you compare two values, the expression is evaluated and Python returns the Boolean answer expression in true or false.


e.g. 50 > 30 = true


STRING


String returns in single or double quotes. Strings are immutable meaning you cannot change it. Strings in Python are arrays of bytes representing Unicode characters.


e.g. print("Hello") print('Hello')


LIST


List is a collection of arrays which is ordered and can be changed. In list duplicate entries are present and we use square brackets to create lists. We can use different data types within list elements.


e.g. list1 = [ “Tim”, “Diana”, 2, 6, “max”]

In above example, I have created list named as list1 having String and Int elements.


DICTIONARY


Dictionary is a collection of key value pair which can be ordered and can be changed. We can use key indexes to get elements because they are unique. We need to use curly brackets to create dictionary.


e.g. subjects { 1 : ‘physics’, 2 : ‘chemistry’, 3 : ‘biology’ }

In above example, I have created a dictionary names subjects with Keys as 1,2,3 and values for these keys as physics, chemistry and biology respectively.


TUPLE


Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable and allow duplicate values. Tuples are written with round brackets


e.g. subjects = (‘physics’, ‘chemistry’, 1, 2, ‘chemistry’, 1)


SET


Sets are used to store multiple items in a single variable. Sets are unordered unchangeable, unindexed and not allowed duplicate values. You can add or remove items from set. Set can contain different data types.

e.g. set1 = {“eva”, 2, “sam”, 5, 8}


ARRAY


Arrays are used to store multiple values in one single variable and you can access the values by referring to an index number. Arrays are mutable and indexing is used to find elements in array. Array take only a single datatype value. We need to import array module to create array. We can import array using Alias( import array as arr) , without Alias( import array) and using * ( from array import * ).


Basic Array Operations


Find the length of Array – To find a length of array need to use len function and answer is in integer value which is equal to the number of elements present in the array.

e.g.

import array

a=array.array('i', [5,6,7,8])
print(len(a))

output: 4


Add elements in Array – For adding elements in array we can use append, insert and extend function. Append function can be used when we want to add elements at the end of array. Extend function can be used when we want to add more than one elemnts to the end of array. Insert function can be used when we want add element in particular position in the array.

e.g. Append function

import array as arr

a=arr.array('d', [2.2,2.3,3.5])

a.append(3.3)
print(a)

output: array('d', [2.2, 2.3, 3.5, 3.3])


e.g. Extend function

import array as arr

a=arr.array('d', [ 2.2, 2.3, 3.5 ])

a.extend([5.1,3.2,4.4])
print(a)

output: array('d', [2.2, 2.3, 3.5, 5.1, 3.2, 4.4])


e.g. Insert function

import array as arr

a=arr.array('d', [2.2,2.3,3.5])

a.insert(2,4.2)
print(a)

output: array('d', [2.2, 2.3, 4.2, 3.5])


Remove elements from Array – To remove element from array pop and remove function can be used. When we are using pop function need to specify parameter otherwise last element can be removed.

e.g. Pop function

import array

a=array.array('i', [5,6,7,8])

a.pop(3)
print(a)

output: array('i', [5, 6, 7])


e.g. Remove function

import array

a=array.array('i', [5,6,7,8])

a.remove(6)
print(a)

output: array('i', [5, 7, 8])


Array concatenation - Concatenation function is used for joining two arrays. We cannot concatenate array which holds different data type.

e.g.

import array

a=array.array('i', [5,6,7,8])

b=array.array('i', [9,3,2])

c=array.array('i')

c=a+b
print(c)

output: array('i', [5, 6, 7, 8, 9, 3, 2])


Slicing of Array – Slicing means to fetch particular data from array. Array can be sliced by using symbol( : ).

e.g.

import array

a=array.array('i', [5,4,3,7,6,8,9,1])
print(a[0:4])

output: array('i', [5, 4, 3, 7])


Looping Array elements - Looping is simply a functionality that is commonly used in programming for achieving repetitive tasks. It can vary from iterating each element of an array or strings, to modifying a whole database.


For Loop

‘FOR’ loop is frequently used to iterate elements of lists. In below example there are 6 elements and hence ‘For’ loop will run 6 times.

e.g.

import array as arr

a=arr.array('i', [2,4,6,8,1,3])

b=2
print("int values")
for x in a:
 c=x+b
 print(c)
print('done')

output: int values

4

6

8

10

3

5

Done


While Loop ‘While’ loop is used when you do not know the number of iterations. To break while loop you need to specify a valid condition. As soon as condition is satisfied the program will come out of loop.

e.g.

import array

a=array.array('i', [2,3,4,5,6,7])

b=2

while b<len(a):
    print(a)
    b=b+1

output:

array('i', [2, 3, 4, 5, 6, 7])

array('i', [2, 3, 4, 5, 6, 7])

array('i', [2, 3, 4, 5, 6, 7])

array('i', [2, 3, 4, 5, 6, 7])

92 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page