Basic fundamentals of python part 3

By
Some Operations and type Conversions:-
This concepts is more helpful for beginners and professionals.You can learn python basic concepts from here with examples.This concepts are more helpful to learn advance python programming also. 
There are some operations and type conversions in python programs as given below:-
  • Arithmetic operators :-
+, -, *, /, %, //, **
+ --> Addition of two numbers(digits).
e.g.
a=9
b=5
c=a+b
print(c)
Output:-
14

- --> Subtractions of two numbers.
e.g.

a=10
b=5
c=a-b
print(c)
Output:-
5

--> Multiplication of numbers.
e.g.

a=10
b=5
c=4
d=a*b*c
print(d)
Output:-
200
/ --> Division of Two numbers.
a=100
b=5
c=a/b
print(c)
Output:-
20.0

%  --> It returns remainder
e.g.

a=10
b=3
c=a%b
print(c)
Output:-
1

**  --> Exponential operator 

e.g.
base=3
exponent=4
result=base**exponent
print("Exponential value is:",result)
Output:-
Exponential value is: 81
                             OR
Use pow( ) function:-To calculate exponent value on any base ,python uses pow() function.
base=3
exponent=4
result=pow(base,exponent)
print(result)
Output:-
Exponential value is: 81
Note:- To calculate exponent value on base e,python uses exp( ) function.
e--> mathematical constant
e=2.71828 (approx)
First import math module for use exp( )  function in python programs.
Syntax:-
math.exp(exponent)
program:
import math
exponent=4
result=math.exp(exponent)
print (result)

Output:-
54.598150033144236

// -->it returns quotient after discarding fractional part(decimal point).
e.g.
a=50
b=3
c=50//3
print(c)
Output:-
16

  • Compound  assignments operators :-
There are following compound assignments operators used in python programs as shown below:-
+= , -= ,/= , %= , **= , //=
a+=5 : #same as  a=a+5
a-=5 : #same as  a=a-5
a*=5 : #same as  a=a*5
a/=5 : #same as  a=a/5
a**=5 : #same as  a=a^5
a//=5 : #same as  a=a//5

  • Type conversions:- In Python Programs ,we can convert one numeric type to another using built-in function int(),float(),complex(),char(),str() and bool().
1. )   int(float/numeric string)  # from float to int & float to numeric string
e.g.
int(3.5)                             #output: 3
int('786')                           #output: 786
  
2.)  int (numeric string, base)        # from numeric string to int in base
e.g.
  int ('123',10)            #output: 123

3.) Complex(int / float) # convert to complex  with imaginary part 0
e.g.
complex(25)                  #output: 25+0j
complex(25.70)               #output: 25.70+0j

4.) complex(int\float , int/float       # convert to complex 
e.g.
complex(5,7)                     #output 5+7j
complex(5.3,7.5)            #output 5.3+7.5j
complex(5.3,7)                #output 5.3+7j
complex(5,7.5)               #output 5+7.5j

5.) bool(int / float)              # from int/float to True/False (1/0)
e.g.
bool(24)                                  #output True
bool(12.60)                            #output True
bool(1)                                    #output True
bool(0)                                   #output False
bool()                                      #output False
bool(True)                             #output True
bool(False)                            #output False
bool(None)                            #output False

6.) str(int/float/bool)         # convert to string
e.g. 
str(50)                                         #output '50'                        
str(6.4)                                        #output '6.4' 
str(True)                                     #output 'True' 
str(Flase)                                    #output 'False' 

6.) chr(int)                  # convert to Unicode character
e.g.
chr(0)                                        #output '\x00'
chr(5)                                        #output '\x05'
chr(65)                                      #output 'A''
chr(67)                                      #output 'C'
chr(95)                                      #output '-'
chr(125)                                      #output '}'

#Watch Complete Video:-

0 comments:

Post a comment

Powered by Blogger.