Variable type - PYTHON PROGRAMMING (2010)

PYTHON PROGRAMMING (2010)

Variable type

There are five main Data types which are as follows.

· Numbers

· String

· List

· Tuple

· Dictionary

Numbers

It contains numeric values and is created when value is assigned to them.

Example

var1 = 1

var2 = 10

Syntax of delete statement is as follows.

del var1[,var2[,var3[....,varN]]]]

Syntax for deleting multiple or single objects are as follows.

del var

del var_a, var_b

It supports four different numerical styles which are as follows.

· Int

· Long

· Float

· Complex

Strings

· It is identified as the set of characters.

· The plus sign is represented as the string concatenation operator.

· Similarly the asterisk is represented as the repetition operator.

Example

#!/usr/bin/python

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.

List

These are written in between the brackets and with commas.

Example

#!/usr/bin/python

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.200000000000003]

abcd

[786, 2.23]

[2.23, 'john', 70.200000000000003]

[123, 'john', 123, 'john']

['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Tuples

It is also a similar sequenced data type like list.

Example

#!/usr/bin/python

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

tinytuple = (123, 'john')

print tuple # Prints complete list

print tuple[0] # Prints first element of the list

print tuple[1:3] # Prints elements starting from 2nd till 3rd

print tuple[2:] # Prints elements starting from 3rd element

print tinytuple * 2 # Prints list two times

print tuple + tinytuple # Prints concatenated lists

Output

('abcd', 786, 2.23, 'john', 70.200000000000003)

Abcd

(786, 2.23)

(2.23, 'john', 70.200000000000003)

(123, 'john', 123, 'john')

('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john'

Dictionary

It is a kind of hash table and the values are enclosed in the curly bracket.

Example

#!/usr/bin/python

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']

Image