- Back to Home »
- Learn Python , Students , Tutorials »
- Learn Python-Class 2: Python as a Calculator
Posted by : Unknown
Tuesday, 20 August 2013
Hello Guyz in this class lets see some basic arithmetic operations python can perform and get familiarized with various arthematic operators and their working.in case you are new here and don't know whats going on,just take a look at our class one on Installing Python.
- Now Fire up IDLE
- See that symbol,the three arrows that is called a prompt.its where we type in commands and after typing each command,you need to hit enter or else it will not be executed.in programming we call it running or executing things.
- Lets just do some quick math using python,
- try in 2+2,56-95,48*96 etc
- You can see things are pretty much same as in your old calculators,once you type in an operation and hit enter,it shows you the result and the waits for the next command.as soon as you type in an expression and hit enter,the process of calculation or coming to a result is called interpretation. we say python shell interprets the result and the result itself is called as an output
- Now lets talk about the types of numbers we have in computing.in programming we have two kinds of numbers:
- Integers
- And Floating point numbers
- An integer is a number without any decimal or the point it means it has only the integer part in it.for example:2,3,85,95 etc all are integers.
- A floating point number is a number which has a decimal number following it for example:2.36,84.67 etc etc.
Note: 2 is an integer and 2.0 is a floating point number
Division and Integer Divisions:
- Now that we know the basic difference between an integer and a floating point number,lets look into the division operation.
- Lets try doing 4/2,3/2,7/3 and 7/5
- If you can observe,all the results were in floating point.and you can even see in the case of 7/3,the output is an approximate one ( see that last 3335 thing? ) so basically when you use a division operator( / ) it is interpreted as a float division.
- There is another type of division operator called the integer division operator. it is just a double slash // lets see what it results in by performing same 4//2 , 3//2 , 7//3 and 7//5 operations
- So an integer division results in a integer result,the output is only the base value of the division ignoring the decimal part
The Modulus and power Operators:
- A modulus operator ( % ) returns the remainder of a division operation
- the power operator is used to input the exponents of numbers like 2^5 which is 32 which means 2 is multiplied with itself for 5 times.in python,a power is represented by using a double astrick sign ( ** )
- ie., 2**5 is similar to 2^5
Operator precedence:
Now lets start combining various symbols together making expressions like,
2+3-6
4*9+63
9**2+6
65/8-9
- Lets see what the above expressions Evaluate to,
- As you can see python clearly follows the BODMAS rule (Brackets,Order,Division,Multiplication,Addition,Subtraction ) it is the order in which operations take place,ie., things in brackets are evaluated first,then order or power then followed by the rest.
- You can easily override operator preferences by using simple brackets or parenthesis
Syntax and Semantics :
- A Syntax is the way the symbols are used,its the valid combination of symbols allowed by python.
- Where as semantics is the meaning that a valid syntax,its what the syntax conveys
Errors:
- There are two types of errors,Syntax errors and Semantic errors
Syntax Errors:
- Syntax error occurs when we don not use a valid symbol combination,It is a result of an unusual or wrong syntax
- It can also occur with misplaced parenthesis
- Using a closing parenthesis without actually opening one may result in an error,but in the last case,with just a opened parenthesis,even if you hit enter,python still takes input and is actually waiting for the parenthesis to be closed,once all the opened parenthesis are closed,then it evaluates the expression and gives us the output
Semantic Errors:
- They occurs when a syntax which is perfectly alright but the meaning it conveys is wrong.like all the math errors come under semantic errors
Thats Pretty much it,lets just wind up this class with a quick summary,
Arithmetic Operators:
Operator | Operation | Expression | English description | Result |
+ | addition | 11 + 56 | 11 plus 56 | 67 |
- | subtraction | 23 - 52 | 23 minus 52 | -29 |
* | multiplication | 4 * 5 | 4 multiplied by 5 | 20 |
** | exponentiation | 2 ** 5 | 2 to the power of 5 | 32 |
/ | division | 9 / 2 | 9 divided by 2 | 4.5 |
// | integer division | 9 // 2 | 9 divided by 2 | 4 |
% | modulo (remainder) | 9 % 2 | 9 mod 2 | 1 |
Arithmetic Operator Precedence:
Operator | Precedence |
** | highest |
- (negation) | |
*, /, /, % | |
+ (addition), - (subtraction) | lowest |
Errors:
Syntax: violates the rules that describe valid combinations of Python symbols
Semantics: the meaning of a combination of Python symbols is wrong
Semantics: the meaning of a combination of Python symbols is wrong