Skip to main content

Posts

Functions in Python

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...
Recent posts

Flow Control in Python

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 ...'...

Important Python Functions

Some Important Python Functions The following are some of the useful built-in Python functions. len() The function len() returns the length of a string, list, set or array. say x=['new', 'tomato', 'ketchup'] len(x) returns 3. print() print('Hellow') Observe the response. input() input() receives a value from the Python console. x=input() Now input a string from the keyboard to the console. The string will be stored in the variable x. print(x) will print the string. float(),int(), str() and complex() float(5) returns 5.0, int(5.3) returns 5, str(5.3) returns the string '5.3' and complex(5,3) returns 5+j3.

Data Types in Python

datatype Next: About this document ... Everything in Python is an object. The various data types are listed in Table  1 . Table 1: Data types in Python No. Data Type Description 1 Numbers The numbers can be integers, float, fractions or complex. 2 Boolean Can be True or False 3 String Sequence of unicode characters 4 List Sequence of ordered homogenous values 5 Tuples Sequence of heterogenous values 6 Bytes, Bytearray These are binary buffers that represent bytes (numbers 0 through 255), often helpful in image processing. 7 Sets Set is an unordered collection with no duplicate elements. The numbers can be integers, long integers, float or complex. The entries int(10) , float(10) , long(10) and complex(10,3) return 10 , 10.0 , 10L and 10+3j respectively. Boolean is a True or False. Strings contain homogenous objects, put within single or double quotes. x='hellow' t...

Overview of Python

Python is an object oriented and  interpreted programming language, that is widely used in scientific computing web applications general programming Python runs on variety of platforms including Linux,Windows and Mac and is free from hassles of commercial software licenses. Python is freely available at www.python.org and offers the freedom to view and modify the code. The chapter presents the basics of Python programming such as func- tions, control statements, variable etc. The objectives of introducing Python to the reader are  to familiarize the reader with a general purpose object oriented program-ming language.  to familiarize the reader with data visualization using Python. This will be useful when the reader works with tabular data in  many experiments to initiate the reader with the manipulation of .csv files and spreadsheets using Python. This is essential since many instruments such as storage oscilloscopes, spectrum analyzers etc. ou...