Saturday, 21 April 2018

Scalar Objects

Objects are the core things that Python programs manipulate. every object has a type that defines the kind of things that programs can do with objects of that type. Types can be either scalar i.e indivisible types  or non-scalar i.e divisible types. Scalar objects can be thought of like atoms of the language.

Python has four types of scalar objects:
1. int : it is used to represent integers
2. float : Literals of type float always include a decimal point
3. bool : it is used to represent the Boolean values True and False.
4. None : it is a type with single value .

Operators on type int and float :

  •  i+j  :  is the sum of i and j. If both are int than the result is also int else the result is float
  • i-j    :  is i minus j. If both are int than the result is int else the result is float. 
  • i*j   :  is the product of i and j. It gives an int output if both i and j are int else the output is float. 
  • i//j  :   is integer division means the answer is always the quotient and remainder is ignored.
  • i/j   :   is normal division where the result is int if both are integers else the result is float.
  • i%j:   is the remainder when the int i is divided by int j.
  • i**j: is i raised to power of j. If i and j are both of type int, the result is an int. if either of them is a float, the result is a float.
  • The comparison operators are ==(equal), != (not equal), > (greater), >- (at least), < (less) and <=  (at most). 
Example :

Question: Create a simple function that given 3 inputs - number 1, number 2 and operator calculates the result. def calculate(x , y , op): if op == '+' : z = x + y elif op == '-' : z = x - y elif op == '*' : z = x * y elif op == '//' : if y!=0: z = x // y else: raise ZeroDivisionError return z print calculate(9 , 9 , '+') print calculate(9 , 9 , '-') print calculate(9 , 9 , '*') print calculate(9 , 9 , '//') print calculate(11 , 0 , '//') OUTPUT WILL BE :
18 0 81 1 ZeroDivisionError
 


Operators on type bool are : 
  • a and b is True if both a and b are True and False otherwise
  • a or b is True if atleast one of a or b is true and False otherwise
  • not a is True if a is False and False is a is True.


No comments:

Post a Comment

Loan Prediction Analysis

In this post I will share my visuals ,learning and understanding of a dataset and using various algorithms of Machine Learning to analyse i...