January 12, 2020

Python tutorial 18 | Dictionaries

Moving on to the next tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains the dictionary data structure. Please view the Python video tutorial 18 or read on... What is dictionary in Python programming? It is a group of key value pairs, that may be stored in any order. The Python dictionary keys can be strings or numbers (immutable data types only). Also, each dictionary key is unique. Python dictionaries are mutable, meaning that any dictionary item may be updated. In other programming languages, dictionaries may be called associative arrays. The dictionary syntax is:

{key0:value0, key1:value1,..., keyn:valuen}

In the above  dictionary format, the key value pairs are separated by commas. The first item is key0:value0, the second item is key1:value1 and so on. The entire group of key value pairs is enclosed within curly braces. Now, let us see Python dictionary examples, Python dictionary operations and Python dictionary functions.

# Python code
num_to_words = {1:'one', 2:'two', 10:'ten', 3:'three', 5:'five'}
# print the Python dictionary (the items may be in any order)
print (num_to_words)
# print the Python dictionary item's value for the given key i.e. print 'three'
print (num_to_words[3])
# modify the Python dictionary item's value for the given key
num_to_words[1] = 'unity'
# print the updated value i.e. print 'unity'
print  (num_to_words[1])

January 06, 2020

Python tutorial 17 | Tuples

Let us go to the next tutorial in the Python Tutorials for Beginners.  This Python beginner tutorial explains the tuple sequence data type. Please view Python video tutorial 17 or read on... What are tuples? In Python programming, a tuple is a sequence of items. The tuple items may be of the same data type or different data types like integers, floats, strings, lists or other tuples. The tuple items are commonly of different data types. Tuples are immutable, meaning that after a tuple is defined, no tuple item can be updated. Also, a tuple that is an item in another tuple is called a nested tuple. The tuple syntax is:
item0, item1, item2,..., itemn

The tuple syntax can also be the items within parentheses.
(item0, item1, item2,..., itemn)

In the above tuple format, the items are separated by commas. The item indexes start from 0.  Now, let us see Python tuple examples and tuple operations with Python examples.

January 01, 2020

Python tutorial 16 | Exception handling part 2 | Try except statement

Moving on with Python Tutorials for Beginners, let us go to the next one from Exception Handling part 1 tutorial. This Python beginner tutorial further explains how to handle any exception in Python. Please view the Python video tutorial 16 or read on... I have explained what are exceptions in my previous Python tutorial 15. In Python programming, the Python try statement syntax can also have an optional else clause:

try:
    try clause
except named_exception(s):
    except clause

else:
    else clause

In the above try statement format, we can put code in the else clause that will only be run when no exception is thrown in the try clause. In other words, the else clause should have code to be be run after the successful completion of the try clause. The else clause may be written after multiple excepts also. Now, let us see the else clause Python examples.

# Python code example
# This Python code, as is, should run the else clause.
try:
    i = 10 # if you comment this line, it should generate an exception and skip the else clause
    print (i*2)
except NameError:
    print ('Please define the variable first.')
else:
    print ('Printed the double of the value!')

The Python try statement syntax can also have an optional finally clause:
try:
    try clause
except named_exception(s):
    except clause

else:
    else clause
finally:
    finally clause