Python Data Types Flashcards
What are the 3 Python numbers?
Give examples
Integers, floating point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.
We can use the type() function to know which class a variable or a value belongs to. Similarly, the isinstance() function is used to check if an object belongs to a particular class.
a = 5
print(a, “is of type”, type(a))
a = 2.0
print(a, “is of type”, type(a))
a = 1+2j
print(a, “is complex number?”, isinstance(1+2j,complex))
OUTPUT
5 is of type
2.0 is of type
(1+2j) is complex number? True
What are the 3 sequence data types?
List, tuple, range
What are the mapping data types?
Dict
What is a List?
Give examples
a list is an ordered sequence of items.
Declaring a list:
a = [1, 2.2, ‘python’]
A list is mutable.
What is a Tuple?
Give examples
A Tuple is an ordered sequence of items but they are immutable.
Declaring a tuple:
t = (5, ‘program’, 1+3j)
What is a String?
Give examples
A string is a sequence of unicode characters.
s = “this is a string”
print (s)
How do you use a slicing operator to extract an item from a List?
Give examples
a = [5, 10, 15, 20, 25, 30, 35, 40]
# a[2] = 15 print("a[2] = ", a[2])
# a[0:3] = [5, 10, 15] print("a[0:3] = ", a[0:3])
# a[5:] = [30, 35, 40] print("a[5:] = ", a[5:])
What is a Dictionary?
Give examples
is an unordered collection of key - value pairs. It is generally used for large datasets.
Dictionaires have a key:value and can be of any type.
d = {1: ‘value’, ‘key’: 2}
print (type(d))
print (“d[1] = “, d[1]);
print (“d[‘key’]”, d[key]);
OUTPUT
d[1] = value d['key'] = 2