Different Datatypes
Different Datatypes
In the real world, we come across different types of data like age of a person(integer), number of rooms in a house
(integer), price(floating-point number), height (floating-point number), Names of people, places and things (strings),
inventory list (list) etc.
Data types categorize data for efficient processing in Python and all programming languages. They assign specific
characteristics to data, enhancing accuracy and control in operations.
possible operations that can be performed. (Arithmetic operations can be applied on numeric data and not strings)
Number
String
List
Tuple
Set
Dictionary
In Python, each value possesses a data type. A variable in Python is capable of holding a value of any data type.
The same variable in Python can refer to data of different data types at different times. Thus, variables in Python are not
strongly typed, implying that you don't have to explicitly declare the data type of a variable when creating it.
We use the built-in function type() to determine the type of data that a variable is referring to at any given point in time.
type(variable_name)
returns the data type of variable_name
Numbers
Numbers
Numbers
int
float
complex
1.int:
It stands for integer. This Python data type stores signed integers.
In Python an integer can be of any length, with the only limitation being the available memory.
Example 1:
a = -7
<class 'int'>
Example 2:
a = 12536984596965565656236534754587821564
<class 'int'>
2.float:
It stands for floating-point numbers. This Python data type stores floating-point real values.
For example : An int can only store the number 20, but float can also store numbers with decimal fractions like 20.25.
Example:
a = 3.0
3.complex:
It stands for complex numbers. A complex number is a combination of a real number and an imaginary number.
It takes the form of a + bj Here, a is the real part and b*j is the imaginary part.
Example
<class 'complex'>
Write the missing code in the given program to know which class the given variable a belongs to.
Strings
The computer doesn’t see letters at all. Every letter you use is represented by a number in memory.
For example, the letter A is actually the number 65. This is called encoding. There are two types of encoding for characters
– ASCII and Unicode.
ASCII uses 8 bits for encoding whereas Unicode uses 32 bits. Python uses Unicode for character representation.
Index starts from 0 to n-1, where n is the number of characters in the string.
Python allows negative indexing in strings. The index of -1 refers to the last item in the string, -2 refers to the second last
item and so on.
Output :
Output :
If single quote (') is part of the string, and the characters are enclosed between single quotes, then a backslash (\) should
be added before (') of the string.
The other way of having a single quote in the string content and enclosing the string within single quotes is using a
backslash(\)
Output :
Output:
Output :
We can also use triple single quotes(''' ''') to create multi-line strings in Python.
Output :
Note: Using triple single quotes or triple double quotes, we can also create
single line strings.