Author Archives: Ranjit Singh Thakurratan

Python and Me – How to create a simple function?

I have always been into programming but never had been full time in it. Now I will admit I did get super close to getting hired as a jr. java programmer but that never happened!

While I have no regrets I am learning python now – not because I have to – but because I want to! Being able to write some code has always sounded cool to me and so here goes.

I am learning py (short for python here after) from a bunch of resources ranging from a pdf to codeacademy online. This is my first blog so I will kick this off with how to define a basic function in py(atleast what I have learnt so far).

So in py, you will declare a function with the DEF key word. Below is an example.
def myfirstfunction():
              print “this is my first function”
Well as you can see fairly simple, but wait – not so fast champ. So in py unlike in other languages, you don’t close a function. It sort of closes itself with the space thats left below the print. Py is a very space conscious language so watch out for those careless tabs and spaces that you may have in your program. Any weird spacing will throw a “IndentationError: expected an indented block”. So remember the statements start with atleast a space in py function and the function closes with a empty line after the last statement.
How do you call it? Well thats easy, just run myfirstfunction() and it will print – this is my first function.
Also remember you cannot start the function name with a number or special character.
Hit back if you have any questions.

Welcome to rjapproves!

Hello there!

 

Thanks for stopping by, I will get started on posting stuff soon!

 

RJ