Functions in Python Dr. Hari V S Department of Electronics and Communication College of Engineering Karunagappally What You will Learn You will learn the syntax of Python functions The Syntax The syntax used the keyword $def$ followed the name of the function again followed by a colon (:) that is then followed by an indented code block of instructions. The function def function_name(arg1.arg2,....argN): statements return results A Simple Python Function Needless to say that the statements and the return command should form an indented block. Consider a simple function that returns the sum of the input values. It is realized as the function below. def summ(x,y): return x+y Unlike MATLAB or IDL, it is possible to execute a function in the Python shell. You may type the above function in a Python shell or an enhanced Python shell such as IPython. Once the function is executed, if you type summ(7,4), it re...
Flow Control in Python Dr. Hari V S Department of Electronics and Communication College of Engineering Karunagappally The instructions in the code are executed, skipped or repeated based on flow control statements. These contain a set of conditions and a code block that is executed according to the conditions. These code blocks are identified by indentation, which is a unique feature of Python. Let us consider an if ... else statement. if ... else print 'Enter the password..' pass=input() if pass=='hari': print 'Access granted ...' else: print 'Wrong password ...' Many conditions are checked by the elif statement as print 'Enter an integer...' x=input() if x print 'The number is less than 10 ...' elif x print 'The number is less than 50' elif x print 'The number is less than 100' else: print 'the number is greater than 100 ...'...