Numbers, Boolean, and String are the basic data types that are present in almost every programming language. Python is no exception in it as well. All these three data types are available in Python.
Numbers Data Type in Python
- In Numbers, Python supports Integer, Floating Point, and Complex number data types.
int Data Type in Python
- To define a int variable. We don’t even need to write int in it.
x = 10 y = 2910 z = 23738393937393739
Range of int Data type in Python
- The concept of the range is not applicable in python because, in python, everything is an object. Even the int variable x is also an object. So, there is no range applicable.
- This is the reason, I’ve deliberately declare a 18 digit int value in it.
Number with Binary, Octal and hexadecimal Base in Python
- We can easily convert numbers in Python from One Base to another using the built-in helper functions.
bin() # it will convert to binary from any base oct() # it will convert to octal from any base hex() # it convert to hexadecimal from any base
Example for Base Conversion in Python 3
print(bin(15)) print(bin(0o777)) print(bin(0X1234)) print(oct(786))
- As python is dynamically typed language, we are not required to declare the data type of a variable while defining it. So, we can change the data type of a variable to other types. It is perfectly valid in Python.
float Data Type and Complex Numbers in Python 3
- Similar to int data type, we can declare floating point numbers in Python.
fp = 3.14
Complex Numbers are used in scientific notations and research purposes. We can declare complex numbers and perform operations on it as well.
Basic Structure of Complex Numbers
Declaration of Complex Numbers in Python
complexOne = 30+10j complexTwo = 40+20j print(complexOne.real) # To get real part of complex number print(complexOne.imag) # To get the imaginary part of complex number
- We have to use j in complex data types. It represents the imaginary part of a complex number.
- We use .real property to get the real part in complex numbers and .imag property to get the imaginary part of complex numbers.
Operations on Complex Numbers in Python
complexOne = 30+10j complexTwo = 40+20j print(complexOne + complexTwo) # Addition Operation print(complexTwo - complexOne) # Subtraction Operation print(complexTwo * complexOne) # Multiplication Operation
Boolean Data Type in Python 3
- Similar to other programming languages, the bool data type can hold only two values True and False. Here T and F are capital.
trueValue = True falseValue = False
- Internally, Python stores True as 1 and False as 0. So the below operations are valid in python.
print(trueValue + falseValue) # Output 1 print(trueValue - falseValue) # Output 1 print(trueValue / trueValue) # Output 1.0
String Data Type in Python 3
- To declare Strings in Python, we can either use single quotes or double-quotes. But generally, programmers stick with single quotes to represent a string.
- If we require multiple lines to declare a string value, then we can use triple Single quotes or triple Double quotes.
- If our string itself contains double or single quotes, then also we can use triple quotes in python.
firstString = "Python is an amazing language." secondString = 'This is a String.' thirdString = '''This string contains "double quotes"''' fourthString = ''' This is multi Line String'''
I hope now you have a good knowledge of the basic fundamental data types in Python 3. If you have any doubts or suggestions regarding this article then do share your thoughts below.