Data types in Python
Generally for any technology to understand or work as a beginner
we need to understand the types/input predefined instruction of the language
platform we start working. In this we will discuss the type with calling and
code snippet for Data types in Python which includes inbuilt primitives and
structured:
For details You can also visit Operators-in-Python
- dictionary
- list
- tupples
- factors (we will avoid these, but they have their uses)
- logical
- integer
- numeric
- complex
- character
Dictionaries
A dictionary
is built in data type of Python which are indexed by keys, which can be any
immutable type, strings and numbers can always be keys. A
dictionary are storing a value with some key and extracting the value given the
key.
Syntax Meaning
D1
= { } Empty dictionary
D2
= {'Ax': 2, 'Bn': 3} Two-item
dictionary
D2['session'] Indexing by key
D2.has_key('UserName'), UserName' in D2 membership test
D2.keys(
), D2.values( ), D2.items( ) lists of
keys, values, items
D2.copy(
), D2.update(D1) shallow copy, dict
merging
D2.get(key,
default=None) "indexing"
w/default value
len(D1) Length (number stored entries)
D2[key]
=300 Adding/changing
del
D2[key] deleting
D4
= dict(zip(klist, vlist)) Construction
List
A
list is Python-object which can contain many different types of elements inside
it. This can be created using [] bracket .
#
Empty
Command::
dList =[]
Some
of the important command used to manipulate/operate upon the list as below-
dList.count(x) Returns the number of occurrences
of x in dList
dList.index(x) Returns the index of the first
occurrence of x in dList or raises an exception if dList has
no such item
dList.append(x) Appends x to the end of dList
dList.extend(l) Appends all the items of list dList to
the end of dList
dList.insert(i,x) Inserts x at
index i in dList
dList.remove(x) Removes the first occurrence
of x from dList
dList.pop(i=-1) Returns the value of the item at
index i and removes it from dList
dList.reverse(
) Reverses, in-place, the items
of dList
dList.sort(f=cmp) Sorts, in-place, the items of dList,
comparing items by f
Tuple
A
tuple is a sequence of values similar to a list. The values stored in a tuple
can be any type, and they are indexed by integers. Difference between tuples
and list as tuples are immutable. Tuples are also comparable and hash-table so
we can sort lists of them and use tuples as key values in Python dictionaries..
#
Empty
Command::
dtple =()
Some
of the important command used to manipulate/operate upon the list as below-
(
) An empty tuple
dtple
= (0,) A one-item tuple (not an
expression)
dtple
2 = (0, 'Ni', 1.2, 3) A four-item tuple
dtple
2 = 0, 'Ni', 1.2, 3 Another four-item
tuple (same as prior line)
dtple[i] Indexing
dtple[i:j] slicing
len(dtple) length (number of items)
dtple
+ dtple2 Concatenation
dtple2
* 3 repetition
for
x in dtple2 Iteration
3
in dtple2 membership
Some
of the inbuilt data types
`expr,...` String conversion
{key:expr,...} Dictionary creation
[expr,...] List creation
(expr,...) Tuple creation or simple parentheses
f(expr,...) Function call
x[index:index] Slicing
x[index] Indexing
x.attr Attribute reference
x**y Exponentiation (x to yth power)
~x Bitwise NOT
+x, -x Unary plus and minus
x*y, x/y, x//y, x%y Multiplication, division, remainder
x+y, x-y Addition, subtraction
x<<y, x>>y Left-shift, right-shift
x&y Bitwise AND
x^y Bitwise XOR
x|y Bitwise OR
x<y, x<=y, x>y, x>=y Comparisons
x<>y, x!=y, x==y Equality/inequality tests*
x
is y, x is not y Identity tests
x
in y, x not in y Membership tests
not
x Boolean NOT
x
and y Boolean AND
x
or y Boolean OR
lambda
arg,...: expr Anonymous simple
function
Data types in Python
Reviewed by Rupesh
on
11:02
Rating:
No comments: