Skip to main content

Doing arithmetic in python


Python is an interpreted language. We can either collect sequences of commands into text files and save this to a file as a Python program. It is convention that these files have the file extension “.py",for example
 test.py.
We can also enter individual commands at the Python prompt which are immediately evaluated and carried out by the Python interpreter. This is very useful to understand how to use certain commands (often before one puts these commands together in a longer Python program )
To start the interpreter we can either
On windows/Linux : start IDLE
OR
On windows: Find the MS-DOS prompt, and type python followed by the return key.
On Linux/Unix/Mac OSX: Go to the terminal type python followed by the return key.
The python prompt (the chevron >>>) signals that Python is waiting for input from us:
>>> 
We can now enter commands, for example 4+5, followed by the RETURN key.
>>> 4+5
9
>>> 
Once we press the return key, Python will evaluate the expression (4+5) and display the computed value (9) in the next line. It then displays the python prompt (>>>) in the next line to indicate that it is ready for the next input.
This interactive programming environment is sometimes referred to as the read-eval-print loop (REPL), because the expression is read, evaluated, the result is printed, and then the loop starts again.
Now you can try the other arithmetic operations
+(add)            -(sub)       *(mul)        /(div)            %(mod)          **(exp)

Examples:
>>>4+3
7
>>>4-3
1
>>>4*3
12
>>>4/3
1
>>>4%3
1
>>>4**3
64

Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language"""
This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’
We can also slice a string. Segment of a string is called a slice.
>>>s[1:3] ‘yt’
If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’
>>>s[2:6] ‘thon’
If the last index is not mentioned, the slicing will go till the end o…

User Defined Functions in Python

So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s) and are called User Defined Functions. These functions can then be combined to form a module which can then be used in other programs by importing them.
In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition.
To define a function keyword def is used. After the keyword comes an identifier i.e.name of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function. A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while executing. In Python, statements in a block are written with indentation. Usually, a block begins when a li…

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…