Tag Archives: Python

Python- Function

We use the def keyword to create a user-defined function.

eg: def function_name( parameters ) :

eg: There are two functions in this program. One which returns a value and the other which does not return a value.

def calculate_sum( num1, num2 ):
total = num1 + num2
print (“First number is :”, num1)
print (“Second number is : “, num2)
print (“The total sum is “, total)
return total ;

def calculate_diff (num1, num2):
print (“Function without return”)
print (“First number is :”, num1)
print (“First number is :”, num2)
print (“Difference between num1 and num2 is :”, num2-num1)

total = calculate_sum(10,20)
print (“We can return the value as well :”, total)





The output will be shown as :


First number is : 10
Second number is : 20
The total sum is 30
We can return the value as well : 30
Function without return
First number is : 20
First number is : 40
Difference between num1 and num2 is : 20


Python- The Dictionary

This is the implementation of the ‘key-value’ pair in Python. Each key and value is separated by a colon :

dict = {‘Color’: ‘Red’,’Value’:’255,0,0′}

The value of the dict can be updated as follows:

dict[‘Color’] = ‘Green’
dict[‘Value’] = ‘0,255,0’

You can delete individual pair using the del statement.

Len gives the length of the dictionary.

dict = {‘Color’: ‘Red’,’Value’:’255,0,0′}
print (“dict[‘Color’]”,dict[‘Color’])
print (“dict[‘Value’]”,dict[‘Value’])

dict[‘Color’] = ‘Green’
dict[‘Value’] = ‘0,255,0’

print (“dict[‘Color’]”,dict[‘Color’])
print (“dict[‘Value’]”,dict[‘Value’])

del dict[‘Color’]

del dict[‘Value’]
dict = {‘Color’: ‘Blue’,’Value’:’0,0,255′}


print(‘Str produces a printable string reprsentation of %s’ % str(dict))

The output of the above program:

Output :

{‘Color’: ‘Red’, ‘Value’: ‘255,0,0’}
dict[‘Color’] Red
dict[‘Value’] 255,0,0
dict[‘Color’] Green
dict[‘Value’] 0,255,0
{‘Value’: ‘0,255,0’}
Str produces a printable string reprsentation of {‘Color’: ‘Blue’, ‘Value’: ‘0,0,255’}

Python- The Strings

Strings in Python can be created by enclosing characters in either single quotes ‘example ‘  or in double quotes “example”

It’s easy to declare a string as shown below:

str1 = ‘hello world’

str2 =  “hello world”



To concatenate two strings we can use the ‘+’  operator



You can use brackets for slicing the string between indexes:


When you run the final program, you should get the output as below:

Python- The Decisions


  1. We will start with the simple if..else statement in Python.
  2.  Create a new file ifelse.py and enter the below lines:num = 100
    if (num == 100) : print(“Value of the number is 100”)
    else :
    print (“Value of the number is not equal to 100”)print(“Exit”)                          
  3. Save the file and under Run menu, click on Run module. You should get the output as shown below:


All in One

  1. Next, we will see how to use the if, elif and else statements in one program.
  2. Type the code snippet as given below:


      3. Save the file and under Run menu, click on Run module. You should get the output as shown below:


Python- Using the IDLE to run your program

  1. Start up the IDLE. Then under File, select New Window or prss CTRL + N, which will open up an untitled window. This is where we will be writing our first program.python12
  2.  In the untitled window, write your first programpython13
  3. Give the file a name and save it (hello.py in this case)
  4. Next, under Run, select Run Module
  5. Now in the main window (IDLE SHELL), you will see the output as shown below:python15
  6. The SHELL is restarted every time one runs a module in IDLE. We can now proceed to writing full length programs!

Python- Setting the PATH

So, after installation of python, we need to update the environment variable PATH in windows 10.

  1. Click Windows + Pause, to start the System Properties and update the value for PATH as shown below:

    2. Start your command prompt and type : PATH. This should now include the installation directory of Python.

Python- The Firsts

  1. Installing Python :  Visit https://www.python.org/   and click on Downloads–>Windows. python1
  2. Download the executale. Here I have downloaded python-3.5.2  python2.PNGpython3.PNG
  3. Once the executable is downloaded (python-3.5.2-amd64.exe), double click on the file which will lauch the setup.


  4. Once this is done, navigate to C:\Users\<yourusername>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.5, you should see the following :python6
  5.  Click on the IDLE (Python 3.5.64-bit) and the python shell is launched. python7.PNG
  6. And thats it! Takes less than 5 minutes to install python.
  7. Okey, Finish it up with the customary Hello World program. (The error to show that the parentheses are required from python 3 and above. Print has moved from being just a “statement” to being a “function”!)python8