Just a quick question, do you drink tea in a cup or a plate? Of course, in a cup, well. If you somehow manage to spill it into the plate in which your bun maska came that's your problem.
In the same way, in every programming language, there are different types of containers for storing different types of data known as Data Types.
Object Types / Data Types in Python
In Python, almost everything is an object, including the types of containers.
How many data types are there in Python? Short answer, MANY. Here we go:
- Number : 1234, 3.1415, 3 + 4j, 0b111, decimal.Decimal, fraction.Fraction
- String : 'spam', "Bob's", b'a\x01c', u'sp\xc4m'
- List : [1, [2, 'three'], 4.5], list(range(10))
- Tuple : (1, 'spam', 4, 'u'), tuple('spam'), namedtuple
- Dictionaries : {'food': 'spam', 'taste': 'yum'}, dict(hours = 10)
- Set : set('abc'), {'a', 'b', 'c'}
- File : open('eggs.txt'), open(r'C:\ham.bin', 'wb')
- Boolean : True, False
- None : None
- Function, modules, classes
- Advance : Decorators, Generators, Iterators, MetaProgramming
Well, don't worry. You'll be learning all these in depth if you keep reading these articles. This is just a BIG PICTURE, so no need to feel overwhelmed about it. Let us have a look at some of the basic ones with the breadth first, depth later approach.
Numbers
Integers
x = 10 # A positive integer
y = -10 # A negative integer
Well, simply, this is what an Integer is. A positive or a negative number (no decimals). Every programming language has that and Python is no exception to it. Infact, Python is used widely to play with numbers, whether it's integers or floating points (or decimals).
Floating Points
a = 6.7
b = 9.11
Well, these numbers are very interesting if you see, naah, they aren't what you are thinking, instead they are just decimals. Literate people call it a Floating Point or just Float.
Complex Numbers
If you are a mathematics geek, you might know what complex numbers are. For all the dumb illiterates here, let us look at what a complex number is:
2 is a real number
√2 is also a real number
√-1???????????????????????????????????????????????????????????
Does this even exist?
Well, not in reality, √-1 does exist in the imaginary world. I don't really know who thought of it but it was probably a mathematician who was high on chai shots due to which he started hallucinating his own fantasy world of Imaginary Numbers (just kidding, imaginary numbers solve a lot of problems, I don't really know them though as I am not a maths genius). He called it i (or iota). Weird name, isn't it? If that's weird, who the hell names a programming language on the name of a snake species? Screw the naming system let's jump in the world of complex numbers.
√-1 = i
√-2 = √2 * √-1 = i√2
√-3 = i√3
... and so on
What if we multiply i with another i?
i * i = √-1 * √-1 = -1
i^3 = -i (Basic maths)
i^4 = 1 (the negatives cancel each other)
i^5 = i
i^6 = -1
...and so on
i is an imaginary number, hence, 2i or 3i or superman*i all are imaginary too.
Then what are complex numbers? Simple, if we mix real and imaginary number together, we get something called shear madness or a complex number.
x = a + ib is the general form of a complex number where a and b are real numbers and i is just the result of an teaholic mathematician's fantasy.
In Python, you can store complex numbers as well, but with a j instead of an i.
complex_num = 2 + 3j
Why j instead of i?
Good question, its just what the devs agreed on as in programming, i has a completely different fanbase. It is used as a loop variable a lot and the programmers didn't want to confuse with 'iota'.
Binary
Yes, you can also store a number in binary in Python, just write 0b before it.
x = 0b1010 # x = 10 (10 in binary is 1010).
As simple as that, nothing to get afraid of.
What are decimal.Decimal and fractions.Fraction?
fractions.Fraction deals with rational numbers, no wonder.
But if we have float, why do we even need decimal.Decimal?
Why did I not write decimal() or fraction() but wrote the same word 2 times?
Both decimal and fractions are modules in Python, remember, we imported some in our previous articles just for fun? Decimal and Fraction (with the capital letters) are just classes inside it.
Now, what the hell are classes?
Those who are familiar with OOP, they know it very well, however, if you are not an OOP guy, then for now think of a class as a container, that's all. We'll look at it when we reach Object Oriented Programming in Python ;)
Almost all the programming languages have a limit on the precision of floating point numbers, Python floats are no exception. But, this is why Python nailed the number game in the industry, it does have a decimal module which provides the exact precision when working with decimal numbers.
Strings
An array of characters is called a string
You must have heard this phrase a lot, this is true for C/C++ but, in our pet snake named Python, "this is what a string is". You can store it with either a single or a double quote (''/"").
chai_str = 'adrak chai'
chai_str2 = "Parle'G"
These are some basic data types that exist in every language, Python does have many more though, lists, tuples, dictionaries, sets, etc. We'll be having deep dives in everything along with all these basic ones.
Thank You Hitesh sir for making this series on YouTube <3
I hope y'all enjoyed reading this one, next article coming soon, stay tuned, and have a great one!
Written by Divye Kapoor with ❤️
Comments
Join the discussionNo comments yet. Be the first to comment.
Chai aur Python
This series contains everything I learnt while watching Chai aur Python by Hitesh Sir on YouTube


