Lab 6 AI
Lab 6 AI
Numbers
Number stores numeric values. The integer, float, and complex values belong to
a Python Numbers data-type. Python provides the type() function to know the
data-type of the variable. Similarly, the isinstance() function is used to check
an object belongs to a particular class.
Python creates Number objects when a number is assigned to a variable. For
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))
Example - 2
str1 = 'hello world' #string str1
str2 = ' how are you' #string str2
print (str1[0:2]) #printing first two character using slice operator
print (str1[4]) #printing 4th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
List
Python Lists are similar to arrays in C. However, the list can contain data of
different types. The items stored in the list are separated with a comma (,) and
enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The concatenation
operator (+) and repetition operator (*) works with the list in the same way as
they were working with the strings.
Example
list1 = [1, "hi", "Python", 2]
#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an
associative array or a hash table where each key stores a specific value. Key can
hold any primitive data type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the
curly braces {}.
Example.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d.keys())
print (d.values())
Boolean
Boolean type provides two built-in values, True and False. These values are used
to determine the given statement true or false. It denotes by the class bool. True
Example.
# Python program to check the boolean type
print(type(True))
print(type(False))
print(false)
Set
Python Set is the unordered collection of the data type. It is iterable,
mutable(can modify after creation), and has unique elements. In set, the order of
the elements is undefined; it may return the changed sequence of the element.
The set is created by using a built-in function set(), or a sequence of elements is
passed in the curly braces and separated by the comma. It can contain various
types of values.
Example.
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)