Python Reference Materials https://python.iexpertify.com Sun, 27 Apr 2014 10:54:51 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.10 Sequences in Python https://python.iexpertify.com/blog/2014/04/27/sequences-in-python/ https://python.iexpertify.com/blog/2014/04/27/sequences-in-python/#comments Sun, 27 Apr 2014 10:54:51 +0000 http://python.iexpertify.com/?p=37 Read More]]> Sequences are those types that support Indexing and Slicing. In that respect, strings, unicodes, lists, tuples, buffers and xrange are all sequence types

Indexing allows you to fetch a particular item in the sequence directly

Slicing operation which allows us to retrieve a slice of the sequence i.e. a part of the sequence

For example:

t = 12345, 54321, ‘hello!’

>>> t

(12345, 54321, ‘hello!’)

>>> t[0]

12345

>>> t[:-1]

(12345, 54321)

]]>
https://python.iexpertify.com/blog/2014/04/27/sequences-in-python/feed/ 1
Dictionary in Python https://python.iexpertify.com/blog/2014/04/27/dictionary-in-python/ https://python.iexpertify.com/blog/2014/04/27/dictionary-in-python/#respond Sun, 27 Apr 2014 10:54:17 +0000 http://python.iexpertify.com/?p=35 Read More]]> Values are unordered pair of key-value pairs

Each value has a corresponding key. Thus there exists a one-to-one relation between them

You cannot get a key by value, as otherwise

Keys are immutable but values can either be immutable or mutable

Dictionary cannot have a duplicate key thus a one-to-one relation must always exist between key and value. Assigning a value to an existing key will wipe out the old value

>>> d = {“Hello”:10, “world”:20}

>>> d

{‘world’: 20, ‘Hello’: 10}

>>> d[“Hello”]

10

>>> d[“Hello”] = 30

>>> d

{‘world’: 20, ‘Hello’: 30}

 

  • Dictionaries support the following in-built methods, but are not limited to these:

d[k] retrieves a value from a dictionary

d[k]=x sets a value in the dictionary

d.has_key(k) tests for the presence of a keyword

d.get(k, d) returns a default if a key is not found

d.keys() returns a list of keys from a dictionary

d.values() returns a list of values

]]>
https://python.iexpertify.com/blog/2014/04/27/dictionary-in-python/feed/ 0
Tuple Concept in Python (Similar to List) https://python.iexpertify.com/blog/2014/04/27/tuple-concept-in-python-similar-to-list/ https://python.iexpertify.com/blog/2014/04/27/tuple-concept-in-python-similar-to-list/#respond Sun, 27 Apr 2014 10:53:39 +0000 http://python.iexpertify.com/?p=33 Read More]]> It is like a list with all comma separated elements enclosed in a pair of parenthesis.

It is an immutable list. A Tuple can not be changed in any way once it is created.

>>> t = (“Hello”, 10, “world”, 20) >>> t (‘Hello’, 10, ‘world’, 20)

Like a list, all its elements are ordered. First element starts at index 0.

>>> t[0]

‘Hello’

 

Negative indices count from the end of the tuple, just like a list

>>> t[-1]

20

Slicing also works for tuple same as it works for list.

>>> t[1:3]

(10, ‘world’) # creates a new tuple

 

Unlike list, they have no in-built methods. So you cannot add, remove, index elements in a tuple.

 

Thus, they are truly immutable in a way that lets you write create write-protect data.

]]>
https://python.iexpertify.com/blog/2014/04/27/tuple-concept-in-python-similar-to-list/feed/ 0
List concept in Python https://python.iexpertify.com/blog/2014/04/27/list-concept-in-python/ https://python.iexpertify.com/blog/2014/04/27/list-concept-in-python/#respond Sun, 27 Apr 2014 10:52:53 +0000 http://python.iexpertify.com/?p=31 Read More]]> It is a list of comma separated values in square brackets. >>> x = [‘Hello’, 10, ‘World’, ’20’] >>> print x [‘Hello’, 10, ‘World’, ’20’]

First element in the list starts at 0 index. >>> x[0] ‘Hello’

>>> type(x) # function type() specifies type of an object.

<type ‘list’> # x is of type list

 

>>> x[-1]

’20’

>>> x[-2]

‘World’

List can have items of different types unlike in C/C++ where we can have array of only single type.

Lists can be sliced, concatenated and so on: >>> x[:2] #list sliced to display first two elements

[‘Hello’, 10]

 

>>> x[:-1]

[‘Hello’, 10, ‘World’] # list sliced to drop 2 elements

>>> x[:1] + [‘Good Morning’] # Concatenate 2 lists

[‘Hello’, ‘Good Morning’]

Apart, from this you have several other in-built methods like append, count, extend, reverse, pop, etc to add, delete, count items, etc.

>>> x.append(40)

>>> x

[‘Hello’, 10, ‘World’, ’20’, 40]

• Unlike strings, which are immutable, it is possible to change individual elements of a list:

>>> a

[‘Hello’, 10, ‘World’, ’20’]

>>> x[1] = x[1] + 23

>>> print x [‘Hello’, 33, ‘World’, ’20]

]]>
https://python.iexpertify.com/blog/2014/04/27/list-concept-in-python/feed/ 0
Datastructures in Python https://python.iexpertify.com/blog/2014/04/27/datastructures-in-python/ https://python.iexpertify.com/blog/2014/04/27/datastructures-in-python/#respond Sun, 27 Apr 2014 04:02:40 +0000 http://python.iexpertify.com/?p=29 Python has a number of structures to group together other values such as:

List

Tuple

Dictionary

Sequence

 

]]>
https://python.iexpertify.com/blog/2014/04/27/datastructures-in-python/feed/ 0
Strings in Python https://python.iexpertify.com/blog/2014/04/27/strings-in-python/ https://python.iexpertify.com/blog/2014/04/27/strings-in-python/#respond Sun, 27 Apr 2014 04:02:08 +0000 http://python.iexpertify.com/?p=27 Read More]]> Strings are immutable. They can be expressed in several ways in single quotes or double quotes:

>>> ’Hello World’ ’Hello World’ # using single quote

>>> “doesn’t” “doesn’t” # using double quotes

>>> ’”Yes,” he said.’ # A mix of both ’”Yes,” he said.’

>>> “\”Yes,\” he said.” ’”Yes,” he said.’

>>> u’Hello World’ # Creates a unicode string u’Hello World’

>>> “””Hello World””” # Using triple quote ‘Hello World’

>>> ”’Hello World”’ ‘Hello World’

The small ‘u’ in front of the quote indicates that a Unicode string is to be created

 

>>> r’Hello \n World’             # Creates a raw string that do

# not give special attention to

# escape sequences.

‘Hello \\n World’

]]>
https://python.iexpertify.com/blog/2014/04/27/strings-in-python/feed/ 0
Python Data Types https://python.iexpertify.com/blog/2014/04/27/python-data-types/ https://python.iexpertify.com/blog/2014/04/27/python-data-types/#respond Sun, 27 Apr 2014 04:01:29 +0000 http://python.iexpertify.com/?p=25 Read More]]> Python supports two basic data types: Numbers and Strings:

Numbers

Strings

 

Numbers

Python supports four numeric types that all are immutable:

Integer

>>> a = 10 >>> b = 20 >>> a * b 200

Long Integer

Float

>>> a = 2.5 >>> b = 1.5 >>> a / b 1.6666666666666667

Complex

>>> x = 1 + 2j >>> x.real 1.0 >>> x.imag 2.0

]]>
https://python.iexpertify.com/blog/2014/04/27/python-data-types/feed/ 0
Some Basics of Python https://python.iexpertify.com/blog/2014/04/27/some-basics-of-python/ https://python.iexpertify.com/blog/2014/04/27/some-basics-of-python/#respond Sun, 27 Apr 2014 04:00:57 +0000 http://python.iexpertify.com/?p=23 Read More]]> Python binds the name to the expression’s value

Python is loosely typed in a way that names are not required to be declared for their types. You can simply assign any type of data to a single variable

For example:

>>> x = 1 # x is a name bound to value 1

>>> x = ‘Hello’ # same name now stores a string

>>> print x

Hello

 

Python is a strong object oriented in a way that everything is an object; be it is a string, number, function, exception, etc. everything is just an object.

]]>
https://python.iexpertify.com/blog/2014/04/27/some-basics-of-python/feed/ 0
Choice of Editors / IDEs https://python.iexpertify.com/blog/2014/04/27/choice-of-editors-ides/ https://python.iexpertify.com/blog/2014/04/27/choice-of-editors-ides/#comments Sun, 27 Apr 2014 04:00:15 +0000 http://python.iexpertify.com/?p=21 Read More]]> There are numerous editors or IDEs that one can use for developing python programs.

On install, Python can be started easily by typing ‘python’ on the shell. On successful invocation of python a prompt >>> is displayed which can then be used to submit python commands/programs to python for running.

To exit this python shell, you can press Ctrl + z button and press Enter.

Active State’s python has a nice Python Editor that you can use to edit python programs. This editor also has a python shell where these programs are run. Moreover, this editor has a nice word auto-completion facility that can help you determine the possible attributes a module is exporting.

There are similar other python editors like UliPad (also provides a python shell), Crimson Editor, etc. Crimson editor is just a generic text editor much like TextPad and has no way to launch python shell for running programs.

The one IDE I like the most is Eclipse Ganymede. This is a generic SDK that can be complemented with PyDev Plug-in to enable Python and Jython editor. It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others.

I use PythonWin or python shell alone for short test programs. For my run time environment, I prefer Eclipse

Python list of editors or IDEs is very long. Most of them are freeware / shareware.

]]>
https://python.iexpertify.com/blog/2014/04/27/choice-of-editors-ides/feed/ 1
Installable / Source Code https://python.iexpertify.com/blog/2014/04/27/installable-source-code/ https://python.iexpertify.com/blog/2014/04/27/installable-source-code/#respond Sun, 27 Apr 2014 03:59:40 +0000 http://python.iexpertify.com/?p=19 Read More]]> The Installable or the source code of Python for various platforms can be sought either from:

www.python.org

www.activestate.com

 

Both the sites have vast documentation of library modules.

]]>
https://python.iexpertify.com/blog/2014/04/27/installable-source-code/feed/ 0