Introduction
Hello and welcome to my new blog post. On day 6 of my python coding journey, I learned about Variables and Datatypes. I also learned about 5 different datatypes and wrote some code.
So let's get started......
What is a Variable?
From the above analogy, I hope you can guess what a variable is.
The variable is like a container that holds data. Creating a variable means that you are creating a placeholder in the memory of the computer and assigning it some value.
For example -
a = 1
b = True
c = "Chintan Jain"
d = None
This means storing 1 in my computer memory or RAM and give the address of the storage location to "a".
In the above codebase, there are four variables viz- a,b,c,d and each of them stores some data.
What is DataType?
Data Type specifies the type of value the variable holds. Data type ensures that there is no error in the program and supports the proper functioning of the code.
DataTypes are categorised into 5 types below:-
Numeric data - int type, float type, complex type
Text data - Str type
Boolean data - True, False
Sequenced data - list, tuple
Mapped data - dict type
In python, we can also print the type of any operator using type() function
a = 1
print(type(a))
b = "1"
print(type(b))
Numeric DataType
I learnt about 3 types of numeric datatypes i.e int, float and complex
Example -
int - 25,8,4
float - 1.23, 7.18, 99.9999
complex - 3+4i
Text DataType
Text datatype is a string variable. It stores characters or words or sentences
Example -
a = "Hello World"
b = "Python"
Note that the type of a, b is Str i.e string
When writing a string do ensure to write it in single ' ' or double quotes " ".
Boolean DataType
Boolean consists of True or False
Sequenced DataType
Sequenced data types are list and tuple
List - A list is an ordered collection of data with elements separated by a comma and enclosed within square brackets.
Lists are mutable and can be modified after creation.
list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1)
output
[8, 2.3, [-4, 5], ['apple', 'banana']]
Tuple - A tuple is an ordered collection of data with elements separated by a comma and enclosed within parentheses.
Tuples are immutable and can not be modified after creation.
tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1)
Output
(('parrot', 'sparrow'), ('Lion', 'Tiger'))
Mapped DataType
dict - A dictionary is an unordered collection of data containing a key: value pair. The key: value pairs are enclosed within curly brackets.
dict1 = {"name":"Anmol", "age":18, "canVote":True}
print(dict1)
output
{'name': 'Anmol', 'age': 18, 'canVote': True}
NOTE - Do remember that everything in python is an object
Final code
i= 1
j= 1.1
a = complex(8, 2)
b = True
c = "Chintan"
d = None
print(a)
print(b)
a1 = 9
print(a + a1)
print("The type of i is ", type(i)) #type - int
print("The type of j is ", type(j)) #type - float
print("The type of a is ", type(a)) #type - complex
print("The type of b is ", type(b)) #type - boolean
print("The type of c is ", type(c)) #type - str
print("The type of d is : ",type(d)) #type - NoneType
list1 = [8, 2.3, [-4, 5], ["apple", "banana"]]
print(list1) #list ordered collection of data
tuple1 = (("parrot", "sparrow"), ("Lion", "Tiger"))
print(tuple1) #Just like list they are ordered collection of data except Tuples are immutable and can not be modified after they are created
dict1 = {"name":"Sakshi", "age":20, "canVote":True}
print(dict1) #dictionary is an unordered collection of data containing a key:value pair.
a1=1
print(a1)
Output
Resources Used
Conclusion
Thanks, guys for going through this blog post. I hope this post helps someone newbie like me and makes an impact in learning python. Building a solid foundation is very important when we learn something new. So now just building foundation and along the way hoping to build some super cool projects.
Thank you if you read this post and have found this useful.
So this is it for the BLOG post and Day #6 of Python.
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.