Python Data Types
The data can be stored in various ways and in also various ways. Lets say you , result marks can be
stored in numeric type and your address can be stored in alpha numeric ways. We have cleared the python basics in previous chapters if you haven't read it then click hereNow we are going to learn about python data types. Python has 5 standard data types.
Python Data types :-
- Numbers
- String
- List
- Tuple
- Dictionary
Lets see these data types one by one.
Python Numbers
As name indicates this data type stores numeric data. It can be any number not matter it is int,float,etc.
For deleting the number by using " del " statement. The syntax is,
del var1[,var2[,var3[....,varN]]]]
We can delete it by one by one ,
Python supports four different numerical types −
- int (signed integers)
- long (long integers, they can also be represented in octal and hexadecimal)
- float (floating point real values)
- complex (complex numbers)
Python Strings
A contiguous set of characters represented in the quotation marks is known as strings. We can write string in single quotes or double quotes.The indexing of the string starts from 0 .
We can do slicing of strings. Slicing simply means take a particular subset form string. There is another term known as concatenation, which means we can add two strings (not number wise). We can do this by using plus (+) operator. We can print one particular part of the string many times by using asterisk (*) operator. Lets understand through example ,
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
OUTPUT :-
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python Lists
It is the most versatile compound data type. A list contains the items which are separated by commas. Lists are enclosed within square brackets ( [] ).
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. For example −
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
OUTPUT :-
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python Tuples
It is sequence data type and it is similar to the python lists. Any tuple consists of number of values separated by commas. The list are enclosed in the quotes while tuples are enclosed within parentheses. Difference between the tuple and list is lists are immutable while tuples are mutable..
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints the complete tuple
print tuple[0] # Prints first element of the tuple
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
print tuple[2:] # Prints elements of the tuple starting from 3rd element
print tinytuple * 2 # Prints the contents of the tuple twice
print tuple + tinytuple # Prints concatenated tuples
OUTPUT:-
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
Now lets see invalid tuples,
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
Python Dictionary
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
OUTPUT :-
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Stay connected !!!
0 Comments