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

Comments

  1. I just found this blog and have high hopes for it to continue. I have added to my favorites. python course

    ReplyDelete
  2. Pretty Post! Thank you so much for sharing this good content, it was so nice to read and useful to improve my knowledge as an updated one, keep blogging.

    Python Certification Training in Electronic City

    ReplyDelete

Post a Comment

Popular posts from this blog

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. Th

Identifiers,Variables and Keywords

Identifiers An identifier is a name given to entities like variables,functions,class, functions etc. It helps to differentiate one entity from another. Rules for writing identifiers 1.Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.  Names like myClass, var_1 and print_this_to_screen, all are valid example. 2.An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name. 3.Keywords cannot be used as identifiers. Eg: global = 1 is invalid 4.We cannot use special symbols like !, @, #, $, % etc. in our identifier. a@=0 is invalid 5.An identifier can be of any length. Things to Remember Python is a case-sensitive language. This means, Variable and variable are not the same. Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count=10 would make more sense, and it would be easier to figure out what it represents when you look at your code after

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.