Tuesday, July 31, 2018

Lesson-5: Dictionaries

This blog assumes that you have a Linux machine with Python already installed. 
If you don't, then you can use an AWS cloud based image that has Python. For help, refer to my blog
I am using Python 2.7 for this tutorial.


DICTIONARIES


A dictionary is a list of key:value pairs.

"Name":"Abraham Lincoln"   is a key value pair.

1. Dictionary is defined as follows.

         dict1 = {'Name':'James' , 'Age':27, 'Height':5.9}

2. A value can be retrieved as follows.

         dict1['Name']



Example Program - 7:
********************************************************************
#! /bin/python

dict1 = {'Name': 'James', 'Age':27, 'Height':5.9}

print "\n dict1 = ", dict1  # Check the displayed order of objects

print "\n Name: ", dict1['Name']

dict1['Name'] = 'Andrew'

print "\n Updated dict1 = ", dict1  

print "\n Keys of dict1 = ", dict1.keys()

del dict1['Name']

print "\n Updated dict1 = ", dict1  

print "\n The length of dict1 = ", len(dict1)

dict2 = dict1

# Let us clear dict1

dict1.clear()

print "\n Cleared dict1 = ", dict1  

dict1 = dict2

print "\n Updated dict1 = ", dict1  

print "\n dict2 = ", dict2

********************************************************************














Why do you think both  dict1 and dict2 are empty at the end of the program? You can find the answer in the next lesson.

EXERCISE - 4

Write a program that
1. Defines two dictionaries and prints them
2. Clears dictionary1
3. Assigns dictionary2 to dictionary1 and prints them both
4. Creates two more dictionaries.
4. Tries to concatenation (+) and repetition (*) the latest ones. Check what happens.

Sunday, July 29, 2018

Lesson-4 : Tuples


This blog assumes that you have a Linux machine with Python already installed. 
If you don't, then you can use an AWS cloud based image that has Python. For help, refer to my blog
I am using Python 2.7 for this tutorial.

TUPLES


Tuples are lists that are IMMUTABLE; meaning, you can NOT change the objects in a tuple.

They can hold objects of different data types.


Example Program - 5:
************************************************************

#! /bin/python

tup = ('This','is a','frozen','list')

print "\n tup = ", tup

print "\n tup[2] = ", tup[2]   # The indexing starts at zero.

************************************************************








Try to change the value of an item in a tuple and see what happens.

tup[2] = 34

Example Program - 6:
*************************************************************
#! /bin/python

tup1 = (6,5,4)

tup2 = tup1 + (3,2,1)

print "\n tup2 [3:5] = ", tup2 [3:5]

print "\n tup2[5] =", tup2[-2]    # Check if this returns correct value

# Type cast a list into a tuple

list1 = ["i","am","a","list"]

tup3 = tuple(list1)

print "\n tup3 = ", tup3

# Check what this returns

print "\n min(tup3) = ", min(tup3)

print "\n max(tup2) = ",max(tup2)

tup4 = tup3 * 3

print "\n tup4 = ", tup4, "\n"


***************************************************************
















EXERCISE - 3

Write a program that
1. Defines a tuple and assigns 6 objects
2. Replaces the 5th item with some other value. (Think about work arounds)



Lesson-3: Lists

This blog assumes that you have a Linux machine with Python already installed. 
If you don't, then you can use an AWS cloud based image that has Python. For guidance refer to my blog
I am using Python 2.7 for this tutorial.

LISTS

Lists are Python's arrays. 

1. They are MUTABLE; meaning the contents of a list can be changed. 

2. They can hold objects of different data types

Example Program-4:
*******************************************************************
#!/bin/python

list1 = [12,45,"Mahatma",'Gandhi',5.6]

print "\n list1 = ", list1

# The list reference starts at 0

print "\n list1[0] =",list1[0]

# This is how you can change the value of a list

list1[2] = 'Mohan Das'

print "\n Updated list1 = ", list1

# This is how you remove an object from the list

# using pop() you can remove 1 item

list1.pop(1)

print "\n Updated list1 after deletion of an item = ", list1

#using del() you can delete a range of items

del list1[1:3]

print "\n Updated list1 after deletion of a few more items = ", list1

# length of the list can be obtains with len()

print "\n Current length of list1 = ",len(list1)
# Extend the list as follows

list1 = list1+[7.6,"Soccer",9]

print "\n list1 after extension = ", list1

# Check this out

list1 = list1 * 3

print "\n latest list1=", list1, "\n"

********************************************************************














EXERCISE-2

Write a program that 
1. Has two lists. 
2. List1 has 5 objects.
3. List2 has 6 objects.
4. Concatenate both lists to form List3
5. Delete the last object from List3.
6. Print List3 and its current length. 
7. Delete the middle 3 items from List3.
8. Print List3 and its current length. 
9. Compare List1 and List3 and print results.


Lesson-2: User Input and Type Casting

This blog assumes that you have a Linux machine with Python already installed.


If you don't, then you can use an AWS cloud based image with Python.  For guidance, refer to my blog.


I am using Python 2.7 for this tutorial.

USER INPUT

What if you want the user of the program to input data.

Use the following.

raw_input()

NOTE: Python 3.0 supports input() in place of raw_input()


Example-Program2:
*************************************************************************************

#! /bin/python   # This is where your python is. Change this if the path differs on your computer

input1 = raw_input("Enter input1:")

input2 = raw_input("Enter input2:")

print "\n Input1 = ", input1

print "\n Input2 = ", input2

print "\n Input1 + Input2 = ", input1+input2

********************************************************************

Enter two integers as inputs and run this program. What is the output of the last line?

Note: raw_input() always reads the input as a string. 

If you need a specific data type, you need to type cast.


TYPE CASTING

Try the following program.

Example-Program3:
*********************************************************************
#! /bin/python   # This is where your python is. Change this if the path differs on your computer

int1 = raw_input("Enter integer1:")

int1 = int (int1)

int2 = int(raw_input("Enter integer2:"))

print "\n int1 = ", int1

print "\n int2 = ", int2

print "\n int1 + int2 = ", int1+int2

**********************************************************************













EXERCISE-1

Write a program that
1. Reads two integers and two floats from the user.
2. Prints the difference between the two integers and floats.
3. Type casts integers to floats and prints the sum of those.

Lesson-1 : Variables

This blog assumes that you have a Linux machine with Python already installed.

If you don't, then you can use an AWS cloud based image with Python.  For guidance, refer to my blog.

I am using Python 2.7 for this tutorial.

VARIABLES


Variables in Python are declared simply as follows.

variable = value

The data type of the variable is automatically assumed based on the value you provide.

a = 2

The above is an example of declaring and assigning a value to variable a. Since 2 is an integer, the data type automatically becomes integer.

Other examples:

b = 'String1'

c = " String2"
d = 3.2

Notice that single or double or triple quotes can be used for strings.

d is a float.

Example Program-1
***************************************************************

#! /bin/python

int1 = 12
int2 = 14

str1 = 'Abraham'
str2 = "Lincoln"

float1 = 5.4
float2 = 2.0

# Print the values of the variable
print "\n int1 = ", int1
print "\n int2 = ", int2
print "\n float1 = ", float1;  # Semicolon is optional if you love it so much!
print "\n float2 = ", float2
print "\n str1 = ", str1
print "\n str2 = ", str2, "\n"
print ("Done!"), str2

***************************************************************


Lesson-6 : Sets and Frozen Sets

This blog assumes that you have a Linux machine with Python already installed.  If you don't, then you can use an AWS cloud base...