Python Manual Data TypesV2
Python Manual Data TypesV2
Every value in Python has a datatype. Since everything is an object in Python programming, data
types are actually classes and variables are instance (object) of these classes.
Numbers
• Integers, floating point numbers and complex numbers falls under Python numbers
category. They are defined as int, float and complex class in Python.
• We can use the type() function to know which class a variable or a value belongs to.
Example 1:
a=5 #data type is implicitly set to integer
print(a, " is of type", type(a))
Output :
5 is of type <class 'int'>
Examples 2:
a = 2.5 #data type is changed to float
print(type(a))
Output :
<class 'float'>
Example 3:
a = 1 + 2j #data type is changed to complex number
print(type(a))
Output :
<class 'complex'>
Boolean
Boolean represents the truth values False and True
Examples 1:
a = True #a is a boolean type
print(type(a))
Output :
<class 'bool'>
Python Strings
String is sequence of Unicode characters.
We can use single quotes or double quotes to represent strings.
Multi-line strings can be denoted using triple quotes, ''' or """.
A string in Python consists of a series or sequence of characters - letters, numbers, and
special characters.
Strings can be indexed - often synonymously called subscripted as well.
Similar to C, the first character of a string has the index 0.
Examples 1:
s = “Trident Academy"
print(s)
Output :
Trident Academy
Examples 1:
float(5) #convert interger to float using float() method
Output :
5.0
Examples 2:
int(100.5) #convert float to integer using int() method
Output :
100
Examples 3:
str(20) #convert integer to string
Output :
'20‘
Examples 2:
a = 10
print("The value of a is", a)
Output :
The value of a is 10
print() function separates its outputted arguments with spaces. This behavior can be
changed, too. The keyword argument that can do this is named sep (like separator).
Look at the example →
Example 1:
Output
Example 2:
Output
Python Input
Examples 3:
#we can use keyword arguments to format the string
print("Hello {name}, {greeting}".format(name="students", greeting="Good Morning"))
Output :
Hello students, Good Morning
Operators
Python Arithmetic Operators
Python Identity Operators
Python Membership Operators