Sunday, August 12, 2018

Python Data Types



Python Programming language has five standard data types and they are as follows:

Numbers
String
List
Tuple
Dictionary

Number

Type
Format
Description
int
n = 10
Signed Integer
long
n = 345L
(L) Long integers, they can also be represented in octal and hexadecimal
float
n = 25.37
(.) Floating point real values
complex
n = 1.14J
(J) Contains integer in the range 0 to 255.

Normally Python converts variable automatically but we can also use Python conversion functions (int(), long(), float(), complex()) to convert data from one type to another. To know the type of a variable, the type () returns information.

String

String is sequence of characters and string variables are created by enclosing characters in quotes. Python uses single quotes (' ) double quotes ( " )and triple quotes (""") to denote literal strings. Triple quoted strings denotes multi line string.

firstName = 'Suddhashil'
lastName = "Chakraborty"
message = """Normally Python converts variable automatically but we can also use Python conversion functions (int(), long(), float(), complex()) to convert data from one type to another. To know the type of a variable, the type () returns information."""

Entire string value can be accessed or we can access a part of the string variable using brackets ‘[]’. Example:

var1 = 'Hello World!'
var2 = 'This is India'

print var1[0] # this will print the first character in the string an `H`
print var2[1:5] # this will print the substring 'his i`( 1 denotes the first index while 5 characters are to be extracted from the string).


List

Lists are a very useful variable type in Python programming language. A list can contain a series of values. List variables are declared by using brackets [ ] following the variable name.

A = [ ] # This is a blank list variable
B = [11, 2, 4, 6] # this list creates an initial list of 4 numbers.
C = [2, 4, 'Suddhashil'] # lists can contain different variable types.

Index of lists in Python starts from 0 and the last index is length of the list. 'len ()' function returns the length / number of elements minus 1 in the list.

mylist = ['A1', 'A2', 'A3', 'A4']
B = len(mylist) # This will return the length of the list which is 3. The index is 0, 1, 2, 3.
print mylist[1] # This will return the value at index 1, which is 'A1'
print mylist[0:2] # This will return the first 3 elements in the list.

Lists can be multiple dimensional. We can declare multiple dimensions by separating an with commas. In the following example, the MyList variable is a two-dimensional list:

MyList = [[], []]
In a two-dimensional list, the first number is always the number of rows; the second number is the number of columns.

Lists are mutable and dynamic.


Tuple

Tuples are almost same as lists but they are fixed in size once they are assigned values. Tuples are immutable and are defined by parenthesis ().

mylist = ('A1', 'A2', 'A3', 'A4')

Main features of Tuples

Tuples have no append or extend method.

Elements cannot be removed from a tuple.

We can find elements in a tuple, since this doesn’t change the tuple.

We can also use the in operator to check if an element exists in the tuple.

Tuples are faster than lists. 

It makes we code safer if we “write-protect” data that does not need to be changed.



Dictionary

Dictionaries in Python are lists of Key - Value pairs where 'key' is always unique value. The main operation of a dictionary is to extract a value based on the key. 

Dictionaries are created by using braces ({}) with pairs separated by a comma (,) and the key and values are associated with a colon(:). 

We can access and modify 'values' in a Dictionary using the 'key'.

dict = {'Suddhashil': 14, 'Satavisha': 21}

dict['Suddhashil'] = 15  # set the value associated with the 'Suddhashil' key to 15

print (dict['Suddhashil']) # print the value of the 'Suddhashil' key - 15.

dict['Dhananjoy'] = 56 # Add a new key 'Dhananjoy' with the associated value - 56

print (dict.keys()) # print out a list of keys in the dictionary

print ('Dhananjoy' in dict) # test to see if 'Dhananjoy' is in the dictionary.  This returns true.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner