Friday 11 August 2017

Python Basic Modules and Functions


******************************************************************************************

   Contents:
1) basic functions for string handelling
2) import math
3) complex (x,y)
4) import cmath
5) import datetime
6) zip function


***************************************************************

string handelling:

This includes basics of mathematical functions.

Let 's' be a string. Assume = 'MySkillracK'
1) s.lower() => 'myskillrack'
2) s.upper() => 'MYSKILLRACK'
3) len(s) => 11      - Gives the length of the string
4) s.count('l') = >2      - Gives the number of times a character appears in string  - here 'l'
      ***Note that this is case sensitive***
    s.count('k') => 1
    s.count('K') => 1
    s.lower().count('k') => 2
Let 's' be a string. Assume = '   MySkillracK   '       - having space characters in start and end
5) s.lstrip() => 'MySkillracK   '    - Removes spaces from the very left of string (if any)
6) s.lstrip() => '   MySkillracK'    - Removes spaces from the very right of string (if any)


***************************************************************

import math

 This includes basics of mathematical functions.
import math => Type this to import the math module which has many inbuilt functions:

1) math.sqrt(x)






Returns the square root value of x.
2) math.ceil(x)






Returns the smallest integer greater than => if is a decimal number
Returns => if is an integer
3) math.floor(x)






Returns the greatest integer smaller than => if is a decimal number
Returns => if is an integer
4) math.fabs(x)






Returns the absolute value of x.
5) math.gcd(a,b)



- Returns the greatest common divisor of the integers a and b.
- If either a or b is nonzero, then the value of gcd(a,b) is the largest positive integer that divides both a and b.
- gcd(0,0) return 0.
6) math.exp(x)
       Returns e**x.



7)math.expm1(x)
       Returns e**x - 1.


8)math.sin(x)
       Returns the sine of x (here x is in radians.)

9)math.cos(x)
       Returns the cosine of (here is in radians.)

10)math.tan(x)
       Returns the tangent of (here is in radians.)



11)math.asin(x)
       Returns the sine - inverse of (the value returned is in radians.)

12)math.acos(x)
       Returns the cosine - inverse of (the value returned is in radians.)



13)math.atan(x)
       Returns the tan - inverse of (the value returned is in radians.)

14)math.degrees(x)
Convert angle x from radians to degrees.
15)math.radians(x)
Convert angle x from degrees to radians.
16)math.pi
Returns The mathematical constant π = 3.141592…, to available precision.
17)math.e
Returns The mathematical constant e = 2.718281…, to available precision.
18)math.pow(xy)
      Returns x raised to power y  => (x**y)



19)math.log(x,b)
Returns log(x) to base b.


20)math.log(x)
Returns natural logarithm of x => log(x) to base e.


21)math.log2(x)
Return the base-2 logarithm of x. This is usually more accurate than log(x,2)


22)math.log10(x)
Return the base-10 logarithm of xThis is usually more accurate than log(x,10)
***************************************************************
complex(x,y) = x+yj => returns a complex number
(without including any module)
***************************************************************

import cmath

 This includes basics of complex mathematical functions.
import cmath => Type this to import the cmath module which has many inbuilt functions:

1) cmath.phase(complex(x,y))


Returns the phase (argument of the complex number x+yj ) 
2) cmath.polar(complex(x,y))


- Returns the complex number in it's polar co-ordinates.
- Returns a pair (r,theta) where r is the modulus of the complex number and theta is the phase of it.
3) cmath.rect(complex(r,theta))

- Returns the complex number in rectangular co-ordinates.
4) cmath.exp(complex(x,y))


- Returns the exponential value of complex number.
***************************************************************

import datetime

 This includes basics of datetime functions.

1) datetime.date.today()


- Returns today's date in the format (YYYY, MM, DD)

2) datetime.date.today().strftime("%Y")


- Returns the current year (Eg: '2017')
- strftime() actually pulls out the date from the date_string as a string.
Example: See PPS_3_Q.4)

3) datetime.date.today().strftime("%B")


- Returns the current month (Eg: 'August')

4) datetime.date.today().strftime("%y")


- Returns the current year (Eg: '17')

5) datetime.date.today().strftime("%b")


- Returns the current month (Eg: 'Aug')

6) datetime.date.today().strftime("%W")


- Returns the week number of the year (Eg: '32')

7) datetime.date.today().strftime("%w")


- Returns the week day of the week (Eg: Returns '6' for Saturday)
8) datetime.date.today().strftime("%j")

- Returns the day of the year.
9) datetime.date.today().strftime("%d")

- Returns the day of the month.
10) datetime.date.today().strftime("%A")

- Returns the day of the week. (Eg.: 'Saturday')
11) datetime.date.today().strftime("%a")

- Returns the day of the week. (Eg.: 'Sat')

from datetime import date 
from datetime import timedelta 

>>> d1=date(2013,7,12)
>>> d2=d1+timedelta(days=2,hours=25)
>>> d2
        datetime.date(2013, 7, 15)
Attributes: timedelta(weeks=.., days=.., hours=..,minutes=.., seconds=..)


>>> d1=date(2013,7,12)
>>> d2=date(2013,7,15)
>>> print(d2-d1)
       3 days, 0:00:00

Example:
import datetime

mydate = datetime.date(1943,3, 11)  #year, month, day
print(mydate.strftime("%A"))
>> Thursday
***************************************************************

zip()

  • If no parameters are passed, zip() returns an empty iterator
  • If a single iterable is passed, zip() returns an iterator of 1-tuples. Meaning, the number of elements in each tuple is 1.
  • If multiple iterables are passed, ith tuple contains ith Suppose, two iterables are passed; one iterable containing 3 and other containing 5 elements. Then, the returned iterator has 3 tuples. It's because iterator stops when shortest iterable is exhaused.
Eg:
numberList = [1, 2, 3]
strList = ['one', 'two', 'three']
result = zip(numberList, strList)
resultSet = set(result)
print(resultSet)

Output:
{(1, 'one'), (3, 'three'), (2, 'two')}

Eg:
numbersList = [1, 2, 3]
strList = ['one', 'two']
result = zip(numbersList, strList)
resultSet = set(result)
print(resultSet)

Output:
{(1, 'one'), (2, 'two')}     - Forms tuples upto the possible lowest index.
                                      - stops when shortest iterable is exhausted.

Unzipping the Value Using zip():
coordinate = ['x', 'y', 'z']
value = [3, 4, 5, 0, 9]
result = zip(coordinate, value)
resultList = list(result)
print(resultList)
c, v =  zip(*resultList)
print('c =', c)
print('v =', v)
.......................................................................................................................
IN PROGRESS.. COMING SOON...
Edits are Welcomed!
.......................................................................................................................

4 comments:

  1. you r awesome thank you so and very much

    ReplyDelete
  2. Bro skillcopy is not working. I asked my friends also they said the same thing. Please solve this issue as fast as possible.

    ReplyDelete