Python Data Types Flashcards

1
Q

What are the 3 Python numbers?

Give examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 3 sequence data types?

A

List, tuple, range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the mapping data types?

A

Dict

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a List?

Give examples

A

a list is an ordered sequence of items.

Declaring a list:
a = [1, 2.2, ‘python’]

A list is mutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Tuple?

Give examples

A

A Tuple is an ordered sequence of items but they are immutable.

Declaring a tuple:
t = (5, ‘program’, 1+3j)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a String?

Give examples

A

A string is a sequence of unicode characters.

s = “this is a string”

print (s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you use a slicing operator to extract an item from a List?

Give examples

A

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:])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Dictionary?

Give examples

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly