This is the Day 14 of the #90DaysOfDevOps challenge in which we are going to discuss Python Data Types in more detail with more examples and Data Structure.
Python Data Types:
Remember while making a python file .py extension is used. In Python there are built-in datatypes :
1) Numeric Data Types:
int - holds all positive and negative whole numbers that have no limit.
float - holds decimal value or real numbers.
complex - holds complex numbers means Real Number + Imaginary Number (2+9j)
Examples:
# Performing int operation.
a = 10;
print ( "The type of a is", type(a))
# Performing float operation
b = 10.7;
print ("The type of b is", type (b))
# Performing complex operation
c = 7+ 8j;
print ("The type of c is", type (c))
"type" is used to check the datatype of the variable.
2) Sequence Data Types:
string - is a collection of one or more characters in single, double, or triple quotes.
str is used for single characters.
list - It was like arrays, containing more than different types of words.
tuples - It works the same as a list but the difference between these two is that tuples are immutable means that cannot be modified after creation.
Name = "Amit Maurya" ;
print("My name is", Name)
print (type(Name))
Job = "DevOps Engineer" ;
print ("I am currently for",Job,"as a Fresher")
print (type(Job))
List Example :
Fruits = ["Apple", "Mango", "Orange", "DragonFruit", "Guava" ]
print ([Fruits])
Fruits = ["Apple", "Mango", "Orange", "DragonFruit", "Guava" ]
print (Fruits[0])
At first, we are printing the list and second, we are getting the Fruit name on the index number 0. Remember in List we are using the [] bracket in giving values in the list.
Tuple Example:
In Tuple, we are using the () bracket in giving values.
# Printing tuples
Fruits = ["Apple", "Mango", "Orange", "DragonFruit", "Guava"]
print (tuple (Fruits))
#Printing Fruits and Name in tuples.
Fruits = ("Apple", "Mango", "Orange", "DragonFruit", "Guava" )
Name = ("Amit", "Maurya", "Shankar", "Ram")
tuples = (Fruits, Name)
print (tuples)
print (tuple(Name))
3) Boolean Data Types :
It is used for TRUE and FALSE means if they are right then it the decision will be TRUE or if the decision will be false then it will be FALSE.
print (type (True))
print (type (False))
4) Dictionary Data Type :
It is used for the collection of unordered datasets that hold in the KEY: VALUE pair. It is created by curly braces {} that are separated by a comma (,). In the dictionary, values can be duplicated but keys can't be. It is case -sensitive means we can use the same name but in different cases.
# Printing Profile in Dictionary by KEY:VALUE Pair.
Profile = {'Name' : 'Amit Maurya', 'Age': 20 , 'Degree' : 'BCA'}
print (Profile)
# Using dict method to print Dict
Dict = dict({1: 'Hashnode', 2: 'is for', 3:'Tech Bloggers'})
print(Dict)
Set Data Type :
In the set data type, the elements are not duplicated as well as not set in an ordered manner. It is used to avoid duplications in data.
# Mixed values in Set
set1 = set ([1,5,7,'Hello',9,'NewSet'])
print (set1)
# To cancel out duplicate elements
set2 = {1,5,7,'Hello',9,'NewSet',9,'Hello'}
print (set2)
Data Structure :
Data structure means organizing the data in a more efficient way by using python built-in functions or by using python user-defined functions.
Built-In Functions :
Tuples
List
String
Set
Dictionary
User-defined Functions :
Stack
Linked Lists
Trees
Hash Map
Graphs
Queues
As we discussed all Python built-in functions before with examples. The user-defined functions are the most advanced and most popular in Data Structure in every programming language.
So, That's it for today's Day 14 #90DaysofDevOps challenge.
Follow me on Hashnode for more Linux and DevOps blogs.
You can connect with me on Twitter (ID- amitmau07)