When we work with any programming language, we need to use data types in order to store data in memory with variables. Based on our need we need to declare the data type.
Below are the list of Data Types in Python :
String
Numbers
List
Tuple
Dicitionary
- String : Strings in Python is a continuous sequence of characters enclosed within single or double quotes. Always the string index starts from Zero.
String Example In Python :
1 2 3 4 5 6 7 8 9 |
str = “Understanding the language” Print str # prints the complete string Print str[5] # prints the 5 character in the string i.e S Print str * 2 # will print the str 2 times Print str + “ python” # prints concatenated string |
OUTPUT :
Python_String_Example
2.Numbers : Numbers data type of Python will store numeric values. Number object will be initiated or comes into action as soon as we assign a value to the numbers.
Numbers Example In Python :
1 2 3 4 5 6 7 |
Num1= 10 Num2 = 20 Print(num1) Print(num2) |
OUTPUT :
Numbers In Python
3.Python Lists :Lists in python can contain multiple data types. List will contain values separated by commas within square brackets []. The values within list starts from the index zero.
List Example In Python :
1 2 3 4 5 6 7 |
list =[‘sam’,0981,10.09, ‘peter’, 100.00] print list# prints the complete string print list[2]# prints the second number 10.09 print list[1:] # prints elements starting from first element |
OUTPUT :
Python_List_Example
4.Python Tuples :Tuple is also similar data type as List. We can store values in Tuple using parenthesis. We cannot update the Tuple with values, it’s a static one, meaning if we declare 3 values in Tuples we cannot add the 4th value in the Tuple.
Tuples Example In Python :
1 2 3 4 5 6 7 |
Tuples=(‘sam’,0981,10.09, ‘peter’, 100.00) print Tuples # prints the complete string print Tuples[2] # prints the second number 10.09 print Tuples[1:] # prints elements starting from first element |
OUTPUT :
Tuples Examples In Python
- Python Dictionary : Python Dictionary is similar to Hash Table in Java. In other words, it works in the form of Key, value pairs. If we want to access any value from the Dictionary then we should know the Key of that particular pair. Dictionary is enclosed within curly braces.
Dictionary Example In Python :
1 2 3 4 5 |
dict = {“Student1″:”henry”,”studID”:”098″,”class”: “Play group”} print (dict) # prints complete Dictionary print (dict[“Student1”]) |
prints studID value “098”
print (dict.keys()) # prints all the keys values available in dictionary
print (dict.values()) # prints all the valuesavailable in dictionary
OUTPUT: