S2 Python Datatypes (2) 2sect
S2 Python Datatypes (2) 2sect
Python Datatypes
1 cmp(list1, It compares the elements of both the This method is not used in the Python 3 and the above
list2) lists. versions.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of
where it was found
Python String
Creating Dictionary
In Python, a Dictionary can be created by placing a sequence of
elements within curly {} braces, separated by ‘comma’. Values in a
dictionary can be of any datatype and can be duplicated, whereas
keys can’t be repeated and must be immutable. Dictionary can also
be created by the built-in function dict(). An empty dictionary can be
created by just placing it to curly braces{}.
Accessing elements of
Dictionary
In order to access the items of a dictionary refer to its key name. Key
can be used inside square brackets. There is also a method
called get() that will also help in accessing the element from a
dictionary.
Dictionary Methods
Method Description
clear() Removes all the elements from the dictionary
• Data type with one of the two built-in values, True or False.
• Boolean objects that are equal to True are truthy (true), and those
equal to False are falsy (false).
• It is denoted by the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid booleans
otherwise python will throw an error.
Python Datatypes
Data type conversion
• Datatypes
Python Numbers
Python List
Tuple
String
Set
Dictionary
• Type Conversion
Solution 1
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 2.0
print(num2, 'is of type', type(num2))
num3 = 1+2j
print(num3, 'is of type', type(num3))
Solution 2
#create a tuple
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
print(tuplex)
#return the number of times it appears in the tuple.
count = tuplex.count(4)
print(count)
Solution 4
birth_year='1988'
a=int(birth_year)
current_year=2023
print(current_year-a)
age=current_year-a
print(age)
print('I am', age ,'years old')
Thank You