“Be so busy improving yourself so that you have no time to criticize others”
Now, you must have been familiar with comments and print statements in python, In this article, we will discuss in detail,4 basic data types in python.
What is a data type?
Every value in Python has its own data type. A data type tells us what kind of information is available and what are all the different operations that can be done using the data.
Since everything is an object in python data types are classes and variables are instances of the class.
You can get the data type of any object by using the type() function
Integers are basically numbers without a decimal point. It holds both positive and negative numbers.
In python 3, there is no limit on how big an integer can be, provided your system has the required memory space.
If we want to represent values of binary or decimal or hexadecimal that can be easily done and print its corresponding integer value of base 10.
|0b (zero + lowercase letter ‘b’)||Binary number (base 2)||print(0b110011) #51|
|0B (zero + lowercase letter ‘B’)||Binary number (base 2)||print(0B110011) #51|
|0o (zero + lowercase letter ‘o’)||Octal number (base 8)||print(0o123) #83|
|0O (zero + lowercase letter ‘O’)||Octal number (base 8)||print(0O123) #83|
|0x (zero + lowercase letter ‘x’)||Hexadecimal number (base 16)||print(0x12A) #298|
|0X (zero + lowercase letter ‘X’)||Hexadecimal number (base 16)||print(0X12A) #298|
print(type(10)) print(type(0b111011)) print(type(0o123)) print(type(0x12A))
<class 'int'> <class 'int'> <class 'int'> <class 'int'>
A number with a decimal point is considered a floating-point in python. It can contain both positive and negative numbers. It can have one or more decimal points.
Float can use the letter e which represents the power of 10. For example, 12e5 means 12 * 10 ^ 5.
The maximum value a floating-point number can have is approximately 1.8 ⨉ 10308. Python will indicate a number greater than that by the string inf.
The min value a floating-point number can have is approximately 1.8 ⨉ 10308. Python will indicate a number greater than that by the string -inf.
The closest a nonzero number can be to zero is approximately 5.0 ⨉ 10-324. Anything closer to zero than that is effectively zero.
print(type(1.234)) print(12e5) print(-1.79e308) print(-1.89e308) print(1.79e308) print(1.89e308) print(5e-324) print(1e-324)
<class 'float'> 1200000.0 -1.79e+308 -inf 1.79e+308 inf 5e-324 0.0
Complex numbers are specified as <real part>+<imaginary part>j.
Please note, in maths, we use the imaginary number as I but in python, we represent the imaginary part as either j or J.
If you want to represent a real part alone in a complex number then we can represent it as <real part>+0j
If you want to represent the imaginary part alone in complex numbers then we can directly use <imaginary part>j
print(type(1+0j)) print(type(20j)) print(type(1+20j))
<class 'complex'> <class 'complex'> <class 'complex'>
Strings are set of characters and numbers put together. We can make a string by putting single or double quotes around them. Even an integer can change into the string by wrapping them inside single/double quotes.
A string can contain many characters it does not have an ending limit provided your computer has memory.
As we learned in the previous module, Escape sequence we can use all the escape characters inside a string.
A raw string is prefixed by r or R, which specifies that escape sequences in the associated string are not translated. The backslash character does not have a specific meaning.
Hello world Hello\tworld
Another way of writing strings without escape sequence is writing the string within a pair of three single/double quotes. Escape sequences still work in triple-quoted strings, but single quotes, double quotes, and newlines can be included without escaping them. This provides a convenient way to create a string with both single and double quotes in it.
print("""Hello world we are creating multi-line string""")
Hello world we are creating multi-line string
Boolean can have only two values. They are True and False. The result of every condition is a boolean value.
We will learn how to implement boolean values when we cover logical operators.
<class 'bool'> <class 'bool'>
- Print many different values and check their data types.
- Provide huge numbers and check if it is accepting.
- Combine two different data types and print.
- Try to do errors and debug them.
In this article, we have discussed the basic data types in detail. If you have any doubts feel free to post them in the comments.