Good To Know:


Oopsie!Either you knew everything or there was a problem in fetching my facts

Posted by : Unknown Tuesday, 20 August 2013

logo
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



prompt



  • 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
addition,substraction,multiplication in python

  • 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
division in python
  • 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
integer division in python

  • 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
remainder of a division

  • 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
order symbol in python
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,
expressions in python


  • 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


over riding preferences using brackets in python

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
syntax errors in python

  • It can also occur with misplaced parenthesis
  •  

syntax error


  • 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
Semantic Errors in python

Thats Pretty much it,lets just wind up this class with a quick summary,


Arithmetic Operators:



OperatorOperationExpressionEnglish descriptionResult
+addition11 + 5611 plus 5667
-subtraction23 - 5223 minus 52-29
*multiplication4 * 54 multiplied by 520
**exponentiation2 ** 52 to the power of 532
/division9 / 29 divided by 24.5
//integer division9 // 29 divided by 24
%modulo (remainder)9 % 29 mod 21





Arithmetic Operator Precedence:


OperatorPrecedence
**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

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Translate

Blogger Tips and Tricks

Popular Post

Random post

- Copyright © smartify me -

- Administered by Krishna Chaitanya -
Google+