Page 426 - CITS - Computer Software Application -TT
P. 426
COMPUTER SOFTWARE APPLICATION - CITS
Data Types of Python
Data types are the classification or categorization of data items. Python supports the following built-in data types.
Scalar Types
1 int: Positive or negative whole numbers (without a fractional part) e.g. -10, 10, 456, 4654654.
2 float: Any real number with a floating-point representation in which a fractional component is denoted by a
decimal symbol or scientific notation e.g. 1.23, 3.4556789e2.
3 complex: A number with a real and imaginary component represented as x + 2y.
4 bool: Data with one of two built-in values True or False. Notice that ‘T’ and ‘F’ are capital. true and false are
not valid booleans and Python will throw an error for them.
None: The None represents the null object in Python. A None is returned by functions that don’t explicitly return
a value.
Sequence Type
A sequence is an ordered collection of similar or different data types. Python has the following built-in sequence
data types...
1 String: A string value is a collection of one or more characters put in single, double or triple quotes.
2 List: A list object is an ordered collection of one or more data items, not necessarily of the same type, put in
square brackets.
3 Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put
in parentheses.
Mapping Type
1 Dictionary: A dictionary Dict() object is an unordered collection of data in a key:value pair form. A collection of
such pairs is enclosed in curly brackets. For example: {1:”Steve”, 2:”Bill”, 3:”Ram”, 4: “Farha”}
Set Types
1 set: Set is mutable, unordered collection of distinct hashable objects. The set is a Python implementation of
the set in Mathematics. A set object has suitable methods to perform mathematical set operations like union,
intersection, difference, etc.
2 frozenset: Frozenset is immutable version of set whose elements are added from other iterables.
Mutable and Immutable Types
Data objects of the above types are stored in a computer’s memory for processing. Some of these values can be
modified during processing, but contents of others can’t be altered once they are created in the memory.
Numbers, strings, and Tuples are immutable, which means their contents can’t be altered after creation.
On the other hand, items in a List or Dictionary object can be modified. It is possible to add, delete, insert, and
rearrange items in a list or dictionary. Hence, they are mutable objects.
Casting, string, Boolean
Casting
Casting is a process of converting a variable’s data type into another data type. If you want to specify the data
type of a variable, this can be done with casting.
Example:
x = str(3) # x will be ‘3’
y = int(3) # y will be 3
z = float(3) # z will be 3.0
413
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137