Blogger Themes

Search This Blog

www.embeddedstudy.com | Copyright © 2017 | All Rights Reserved | Nithin Pradeep . Theme images by Storman. Powered by Blogger.

Follow by Email

Popular KOZHI

Sponsor

Download

Blogger Tricks

FEATURED

What is an Embedded System?

An embedded system is a combination of hardware and software that is designed to carry out a certain task or tasks, meaning it has a s...

JOIN THE TEAM

Popular Posts

Wikipedia

Search results

Search This Blog

Social

More Links

Social

ad

ads

Thursday, 17 August 2017

Data Types In Python


         Data types determine whether an object can do something, or whether it just would not make sense. Other programming languages often determine whether an operation makes sense for an object by making sure the object can never be stored somewhere where the operation will be performed on the object (static typing). Python does not do that. Instead it stores the type of an object with the object, and checks when the operation is performed whether that operation makes sense for that object (dynamic typing)


In this Section we are going to discuss about various data types in Python....

Mutable vs Immutable Objects
In general, data types in Python can be distinguished based on whether objects of the type are mutable or immutable. The content of objects of immutable types cannot be changed after they are created.

Some immutable types:
  • int, float, long, complex.              
  • str
  • bytes
  • tuple
  • frozen set
       Some mutable types:
  •  byte array
  • list
  • set
  • dict


Numeric types:
  • int: Integers; equivalent to C longs in Python 2.x, non-limited length in Python 3.x
  • long: Long integers of non-limited length; exists only in Python 2.x
  • float: Floating-Point numbers, equivalent to C doubles
  • complex: Complex Numbers
Sequences:
  • str: String; represented as a sequence of 8-bit characters in Python 2.x, but as a sequence of Unicode characters (in the range of U+0000 - U+10FFFF) in Python 3.x
  • bytes: a sequence of integers in the range of 0-255; only available in Python 3.x
  • byte array: like bytes, but mutable (see below); only available in Python 3.x
  • list
  • tuple

Python List

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].
>>> a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])

# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])

# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])

>>OUTPUT
a[2] =  15
a[0:3] =  [5, 10, 15]

a[5:] =  [30, 35, 40]


Lists are mutable, meaning, value of elements of a list can be altered.
>>> a = [1,2,3]
>>> a[2]=4
>>> a
[1, 2, 4]

Python Tuple

Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.
It is defined within parentheses () where items are separated by commas.
>>> t = (5,'program', 1+3j)
We can use the slicing operator [] to extract items but we cannot change its value.
t = (5,'program', 1+3j)

# t[1] = 'program'
print("t[1] = ", t[1])

# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])

# Generates error
# Tuples are immutable
t[0] = 10

OUTPUT:
t[1] =  program
t[0:3] =  (5, 'program', (1+3j))

Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
    t[0] = 10
TypeError: 'tuple' object does not support item assignment.

Python Strings

String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.
>>> s = "This is a string"
>>> s = '''a multiline
Like list and tuple, slicing operator [ ] can be used with string. Strings are immutable.

s = 'Hello world!'

# s[4] = 'o'
print("s[4] = ", s[4])

# s[6:11] = 'world'
print("s[6:11] = ", s[6:11])

# Generates error
# Strings are immutable in Python
s[5] ='d'

OUTPUT:
s[4] =  o
s[6:11] =  world

Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
    s[5] ='d'
Type Error: 'str' object does not support item assignment.

Python String Slicing

Python Supports backward Indexing unlike others.
let string   s='PYTHON'
slicing syntax is like this [starting range : ending range : step]
for forward slicing  starting range < ending range
for backward slicing  starting range > ending range 
s[ 2: ] = 'THON'
s[ 2 : 4 ]='TH'
s[ 4 : 2 ] =' '  (will print nothing because for forward slicing  starting range < ending range)
s[ -1 : -3]=' ' (same as above)
s[ -3 : -1 ]='HO'
s[ : : 2 ] = 'PTO' (step is 2)
s[ 4 : 1 : -2] ='OT'
s[ : : -1] = 'NOHTYP' (reversing the string)

This section continues.....

0 on: "Data Types In Python"