Pages

Saturday, April 29, 2017

Python Practice


#  Character Input Solution
name = input("Enter you name: ")
print("You name is: "+ name)

# Divisors Solutions
num = int(input("Enter the valid number: "))
divlst = []
for n in list(range(1,num+1)):
    if num % n == 0:
        divlst.append(n)
print(divlst)

# find even list
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
evenlst = []
for num in a:
    if num % 2 == 0:
        evenlst.append(num)

print("the new even list is", evenlst)
or
evenlst = [number for number in a if number % 2 == 0]
print("the new even list is", evenlst)

# fibonacci series
def fib(num):
    n1 = 0
    n2 = 1
    count = 0
    if num <= 0:
        print("Pelase enter positive number")
    elif num == 1:
        print("fibanacci series upto :", num , ":", n1)
    else:
        print('fibanacci series upto :', num , "is :")
    while count < num:
        print( n1, end= " ")
        n3 = n1 + n2
        n1 = n2
        n2 = n3
        count += 1
     
fib(5)

or

def fibonacci(n):
    fib_sequence = []
    if n <= 0:
        return fib_sequence
    elif n == 1:
        fib_sequence.append(0)
        return fib_sequence
    else:
        fib_sequence = [0, 1]
        while len(fib_sequence) < n:
            next_num = fib_sequence[-1] + fib_sequence[-2]
            fib_sequence.append(next_num)
        return fib_sequence

# Example usage:
num_terms = 10
fib_sequence = fibonacci(num_terms)
print("Fibonacci sequence with", num_terms, "terms:", fib_sequence)


# print list having numbers less than 10
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
numlst = []
for num in a:
    if num < 10:
        numlst.append(num)

print(numlst)

or
>>> numlst = [num for num in a if num < 10]
>>> numlst

[1, 1, 2, 3, 5, 8]

# find common elements between two lists
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
# first convert the list to set
a = set([1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
print(type(a))
b = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
print(type(b))
res = a.intersection(b)
print("common elements: ", res)

or

>>> a = set(a)
>>> a
{1, 2, 3, 34, 5, 8, 13, 21, 55, 89}
>>> 
>>> b = set(b)
>>> b
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
>>> 
>>> res = a.intersection(b)
>>> res

{1, 2, 3, 5, 8, 13}


# find max of three values
def max_3(a,b,c):
    if (a>b) and (a>c):
        return a
    if (b>a) and (b>c):
        return b
    if (c>a) and (c>b):
        return c
print("Max value is : ", max_3(7,3,9))

# find new list having 1st and last element
newlst = []
def test(lst):
    newlst.append(lst[0])
    newlst.append(lst[-1])
    print(newlst)
lst = [1,2,3,4,5,6,7]
test(lst)

# find odd or even number
num = int(input("Enter the number "))
if num %2 == 0:
    print(num, "is the even number")
else:
    print( num, "is the odd number")

# find if element is present in given list
def find(ordered_list, element_to_find):
    for element in ordered_list:
        if element == element_to_find:
            return True
    return False

l = [1,2,3,4,5]
print(find(l,10))

or

>>> def find(num, lst):
...     if num in lst:
...             return True
...     else:
...             return False
... 
>>> lst = [1,2,3,4,5]
>>> find(2,lst)
True
>>> 
>>> find(10,lst)
False

>>> 

# find if given string is palindrome
name = input("Enter the string: ")
if name[::] == name[::-1]:
    print(name, "is a palindrome")
else:
    print(name, "is not a palindrome")

# find if number is prime or not
num = int(input("Enter a number: "))
if num %2 != 0 or num == 1:
    print(num , "is a prime number")
else:
    print(num, "is not a prime number")

# find the sum of two numbers
a = input ("Enter the value of a : ")
b = input ("Enter the value of b : ")
## by default input takes a string
res = int(a) + int(b)
print("Final ans = ", res)

>>> def sum(a,b):
...     return (a+b)
... 
>>> sum(0.3,5)

5.3

# remove duplicate values.
lst = [1,1,2,3,4,5,4,2,10,15,5,6]
newlst = set(lst)
print(newlst)

# reverse the order
 def rev(str):
    str = input("Enter the sentence: ")
    new = str[::-1]
    print(new)
rev(str)

>>> def rev(str):
...     str = input("Enter the input:" )
...     newstr = str[::-1]
...     print(newstr)
... 
>>> rev("str")
Enter the input: Hello Nawraj How are you?

?uoy era woH jarwaN olleH 

# How to plot graph
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("studs.csv")
df.plot()
plt.show()

# square of all numbers
numlst = [1,2,3,4,5,6,7,8]
reslst=list(map(lambda x : x*x, numlst))
print(reslst)


>>> res = list(map(lambda x:x*x, numlst))
>>> res

[1, 4, 9, 16, 25, 36, 49, 64]

# find missing number in list
def missing_no(lst):
    #print(range(lst[0],lst[-1]+1))
    return sum(range(lst[0],lst[-1]+1)) -sum(lst)
lst = [1,2,3,5,6]
print(missing_no(lst))



# Verify perfect square number
import math
def is_perfect_square(n):
    return math.sqrt(n).is_integer()
print(is_perfect_square(56))

# compute power of 2

import math
def power2(n):
    return math.pow(2,n)
 

print(power2(9))

or

def powerof2(num):
    return ((num & (num - 1)) == 0) and (num != 0) ## bitwise calculation
print(powerof2(8))

# print year and month
import calendar
y = int(input("Input the year: "))
m = int(input("Input the month: "))
print(calendar.month(y,m))

# Write a Python program to convert month name to a number of days.
print("List of months: January, February, March, April, May, June, July, August, September, October, November, December")
month_name = input("Enter the month name: ")
if month_name == "February":
    print("No of days: 28/29 days")
elif month_name in ("April", "June", "September", "November"):
    print("No of days: 30 days")
elif month_name in ("January", "March", "May", "July", "August", "October", "December"):
    print("No of days: 31 days")
else:
    print("Wrong month name")

##  Write a Python program to check a triangle is equilateral, isosceles or scalene
print("Input the lengths of the triangle")
x = input("x:")
y = input("y:")
z = input("z:")
if x == y == z:
    print("equilateral triangle")
elif x != y != z:
    print("scalene triangle")
else:
    print("Isoceles triangle")

# Write a Python program to calculate the sum and average of n integer numbers(input from the user). Input 0 to finish.
print("Input some integers to calculate the sum and average. Input 0 for exit")
sum = 0
count = 0
lst = [1,3,4,5,9]
for n in lst:
    sum += n
    count += 1
if count == 0:
    print("Input some numbers")
else:
    print("Average and sum of numbers: ", sum/count, sum)

## Write a Python program to create the multiplication table (from 1 to 10) of a number
n = int(input("input the number to create multiplication table: "))
for i in range(1,11):
    print(n, 'x', i, "=", n*i)

#remember
n = 5 ## type is int
n = "5" ## type is str

# Write a Python program to construct the following pattern, using a nested loop number
1
22
333
4444
55555
666666
7777777
88888888
999999999

for i in range(1,10):
    print(str(i)*i)

# Write a Python script to concatenate following dictionaries to create a new one
dic = {}
dic1 = {1:10,2:20}
dic2 = {3:30,4:40}
dic3 = {5:50,6:60}
for d in (dic1, dic2, dic3):
    dic.update(d)
print(dic)

# Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included)
lst = []
for n in range(1500,2701):
    if n%7 == 0 and n%5 == 0:
        lst.append(n)
print(lst)

# Write a Python program to guess a number between 1 to 9.
# Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears again until the guess is correct, on successful guess, user will get a "Well guessed!" message, and the program will exit.
import random
target_num, guess_num = random.randint(1,10), 0
while target_num != guess_num:
    guess_num = int(input('Gues a number between 1 and 9 until u get it right: '))
print('well guessed')

# Write a Python program to construct the following pattern, using a nested for loop.
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

n = 5
for i in range(5):
    for j in range(i):
        print('* ', end='')
    print("")

for i in range(n,0,-1):
    for j in range(i):
        print("* ", end="")
    print('')

# Write a Python program that accepts a word from the user and reverse it
word = input("Enter the word: ")
res = word[::-1]
print(res)

# Write a Python program to count the number of even and odd numbers from a series of numbers
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
even_num = []
odd_num = []
for n in numbers:
    if n%2 == 0:
        even_num.append(n)
    else:
        odd_num.append(n)
print("Number of even numbers: ", len(even_num))
print("Number of odd numbers: ", len(odd_num))

#or

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
even_count = 0
odd_count = 0
for n in numbers:
    if n%2 == 0:
        even_count += 1
    else:
        odd_count += 1

print("Number of even numbers: ", even_count)
print("Number of odd numbers: ", odd_count)

or

even_num = len([n for n in numbers if n % 2 == 0])
odd_num = len([n for n in numbers if n % 2 != 0])

# Write a Python program that prints each item and its corresponding type from the following list
datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12], {"class":'V', "section":'A'}]
print(datalist)
for item in datalist:
    print(item, type(item))
    
# Write a Python program that prints all the numbers from 0 to 6 except 3 and 6
lst = []
for i in range(0,7):
    if i == 3 or i == 6:
        continue
    lst.append(i)
print(lst)

# Write a Python program to get the Fibonacci series between 0 to 50
x,y=0,1
while y<50: div="">
    print(y)
    x,y = y,x+y

#or
    
def fib(num):
    a,b = 0,1
    for num in range(num):
        if a < num:
            yield a
            a,b =b,a+b
print(list(fib(50)))

#  Write a Python program which takes two digits m (row) and n (column) as input and generates a two-dimensional array. The element value in the i-th row and j-th column of the array should be i*j
row_num = int(input("Input number of rows: "))
col_num = int(input("Input number of columns: "))
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]

for row in range(row_num):
    for col in range(col_num):
        multi_list[row][col]= row*col

print(multi_list)

#or
        
row = 2
column = 3
a = [[i*j for j in range(column)] for i in range(row)]
print(a)

# Write a Python program that accepts a string and calculate the number of digits and letters
data = "Python 3.2"
d=l=0
for i in data:
    if i.isdigit():
        d += 1
    elif i.isalpha():
        l += 1
    else:
        pass
print("letters: ", l)
print("digits: ", d)

# Write a Python program to check whether an alphabet is a vowel or consonant
char = input("Input the letter of the alphabet: ")
if char in ('a','e','i','o','u'):
    print("%s is a vowel " % char)
else:
    print("%s is a consonant " % char)

## add key to dictionary
dict1 = {0:10, 1:20}
dict1[2] = "30"
print(dict1)

# Write a Python script to check if a given key already exists in a dictionary
dict={1:"one",2:"two",3:"three"}
print(dict)
print(dict.keys())
print(dict.values())

key = input("Enter the value of key ")
for k in dict:
    if k == key:
        print("Entered key %s is already there", key)
    else:
        print("Entered key is not present")

# Write a Python program to iterate over dictionaries using for loops
d = {"blue" : 1, "green" : 2, "yellow" : 3}
for key,value in d.items():
    print(key, value)

#Write a Python program to map two lists into a dictionary
keys = ['1','2','3','4','5','6']
values = ['one','two','three','four','five','six']
d = dict(zip(keys, values))
print(d)

# Write a Python function to find the Max of three numbers
def sum(n1,n2,n3):
    if n1>n2 and n1>n3:
        print("max number is", n1)
    elif n2>n1 and n2> n3:
        print("max number is", n2)
    else:
        print("max number is", n3)
sum(3,1,7)
        
# Write a Python function to sum all the numbers in a list
def sum(lst):
    res = 0
    for i in lst:
        res += i
    return res
    
lst = [1,2,3,4,5,-1]
print(sum(lst))

# Write a Python function to multiply all the numbers in a list
def mul(lst):
    res = 1
    for i in lst:
        res = res*i
    return res
lst = [8, 2, 3, -1, 7]
print(mul(lst))

#Write a Python program to reverse a string
def rev(str):
    res = str[::-1]
    return res

str = "1234abcd"
print(rev(str))

#Write a Python function to calculate the factorial of a number
def fac(n):
    if n <= 0:
        return 1
    else:
        res = n* fac(n-1)
        return res

print("factorial is :", fac(4))

# Write a Python function to check whether a number is in a given range
def test_range(n):
    if n in range(3,9):
        print("%s is in range"%n)
    else:
        print("number is outside of range")
test_range(5)

# Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters
def string_test(s):
    d={"UPPER_CASE":0, "LOWER_CASE":0}
    for c in s:
        if c.isupper():
            d["UPPER_CASE"] += 1
        elif c.islower():
            d["LOWER_CASE"] += 1
        else:
            pass
    print("No of upper case chars: ", d["UPPER_CASE"])
    print("NO of lower case chars:", d["LOWER_CASE"])

string_test('The quick Brown Fox')

## Write a Python function that takes a list and returns a new list with unique elements of the first list
def unique_list(lst):
    x = []
    for a in lst:
        if a not in x:
            x.append(a)
    return x
   
lst = [1,2,3,3,4,4,5]
print(unique_list(lst))

or

def unique_list(lst):
    return list(set(lst))
 
lst = [1,2,3,3,4,4,5]
print(unique_list(lst))


    
# Write a Python program to print the even numbers from a given list
def print_even(l):
    even_list = []
    for i in l:
        if i % 2 == 0:
            even_list.append(i)
    return even_list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(print_even(l))

or

def print_even(lst):
    evenlst = []
    for n in lst:
        if n % 2 == 0:
            evenlst.append(n)
    print(evenlst)
           
lst = [1,2,3,4,5,6,7,8]           
print_even(lst)


## Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically
def input_str(s):
    lst = s.split('-')
    lst.sort()
    print('-'.join(lst))
    
input_str("green-red-yellow-black-white")

# Write a Python function to create and print a list where the values are square of numbers between 1 and 30 (both included)

def res(n1,n2):
        lst = list(range(n1, n2+1))
        res = map(lambda x: x*x, lst)
        return res
       
print(list(res(1,30)))

or

def sqr_num(n1,n2):
    res_list = []
    for i in range(n1,n2+1):
        res_list.append(i**2)
    return res_list

print(sqr_num(1,30))


# Write a Python program to access a function inside a function
def test(a):
    def add(b):
        nonlocal a
        a += 1
        return a+b
    return add
func = test(4)
print(func(4))

#Write a Python script to merge two Python dictionaries
d1 = {"a": "100", "b": "200"}
d2 = {"x": "4", "y": "500"}
d = d1.copy()
d.update(d2)
print(d)
print(d1.keys())
print(d1)
print(d1.values())

#Write a Python program to get the maximum and minimum value in a dictionary.
my_dict = {'x':500, 'y':5874, 'z': 560}
key_max = max(my_dict.keys(), key=(lambda k: my_dict[k]))
key_min = min(my_dict.keys(), key=(lambda k: my_dict[k]))

print('Maximum Value: ',my_dict[key_max])
print('Minimum Value: ',my_dict[key_min])

#or
values = my_dict.values()
print(values)
min = sorted(values)[0]
max = sorted(values)[-1]

print(min)
print(max)

# Write a Python program to multiply all the items in a dictionary
#import math
d = {'n1': 5,'n2': 2,'n3': 3}
#print(mul.dict.vlaues())
res = 1
for key in d:
    res= res*d[key]

print(res)

#or

d = {'n1': 5,'n2': 2,'n3': 3}
print(d.values())
res = 1
for i in d.values():
    res=res*i
print(res)
print(d.items())

# Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9).
import re
def match(str):
    charRe = re.compile(r'[^a-zA-Z0-9.]')
    str = charRe.search(str)
    return not bool(str)

print(match("ABCDEFabcdef123450"))
print(match("*&%@#!}{"))

# Write a Python program that matches a string that has an a followed by zero or more b's 
import re
def text_match(text):
    patterns = 'ab*?'
    if re.search(patterns, text):
        return "match found"
    else:
        return "match not found"
print(text_match("ac"))
print(text_match("abc"))
print(text_match("abbc"))
print(text_match("bc"))
print(text_match("ababab"))

#for a followed by one or more b's
import re
def text_match(text):
        patterns = 'ab+?'
        if re.search(patterns,  text):
                return 'Found a match!'
        else:
                return('Not matched!')

print(text_match("ab"))
print(text_match("abc"))

##Write a Python program that matches a string that has an a followed by zero or one 'b'.
import re  
def text_match(text):  
        patterns = 'ab??'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')    
print(text_match("ab"))  
print(text_match("abc"))  
print(text_match("abbc"))  
print(text_match("aabbc"))
print(text_match("a"))

# Write a Python program that matches a string that has an a followed by three 'b' 
import re
def text_match(text):
        patterns = 'ab{3}?'
        if re.search(patterns,  text):
                return 'Found a match!'
        else:
                return('Not matched!')

print(text_match("abbb"))
print(text_match("aabbbbbc"))

#Write a Python program that matches a string that has an a followed by two to three 'b' - 
import re  
def text_match(text):  
        patterns = 'ab{2,3}?'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("ab"))  
print(text_match("aabbbbbc"))

# Write a Python program to find sequences of lowercase letters joined with a underscore 
import re  
def text_match(text):  
        patterns = '^[a-z]+_[a-z]+$'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("aab_cbbbc"))  
print(text_match("aab_Abbbc"))  
print(text_match("Aaab_abbbc"))

# Write a Python program to find sequences of one upper case letter followed by lower case - 
import re  
def text_match(text):  
        patterns = '^[A-Z][a-z]+$'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("bc"))  
print(text_match("Abbbc"))  
print(text_match("aabSBbbc"))

## Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b' 
import re
def text_match(text):
        patterns = 'a.*?b$'
        if re.search(patterns,  text):
                return 'Found a match!'
        else:
                return('Not matched!')

print(text_match("aabbbbd"))
print(text_match("aabAbbbc"))
print(text_match("accddbbjjjb"))

## Write a Python program that matches a word at the beginning of a string
import re  
def text_match(text):  
        patterns = '^\w+'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("The quick brown fox jumps over the lazy dog."))  
print(text_match(" The quick brown fox jumps over the lazy dog."))

#Write a Python program that matches a word at end of string, with optional punctuation
import re  
def text_match(text):  
        patterns = '\w+\S*$'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("The quick brown fox jumps over the lazy dog."))  
print(text_match("The quick brown fox jumps over the lazy dog. "))  
print(text_match("The quick brown fox jumps over the lazy dog "))

# Write a Python program that matches a word containing 'z'
import re  
def text_match(text):  
        patterns = '\w*z.\w*'  
        if re.search(patterns,  text):  
                return 'Found a match!'  
        else:  
                return('Not matched!')  
  
print(text_match("The quick brown fox jumps over the lazy dog."))  
print(text_match("Python Exercises."))

# Write a Python program that matches a word containing 'z', not start or end of the word. - 
patterns = '\Bz\B'

##Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores 
import re
def text_match(text):
        patterns = '^[a-zA-Z0-9_]*$'
        if re.search(patterns,  text):
                return 'Found a match!'
        else:
                return('Not matched!')

print(text_match("The quick brown fox jumps over the lazy dog."))
print(text_match("Python_Exercises_1"))

## Write a Python program where a string will start with a specific number
import re
def match_num(string):
    text = re.compile(r"^5")
    if text.match(string):
        return True
    else:
        return False
print(match_num('5-2345861'))
print(match_num('6-2345861'))

# Write a Python program to remove leading zeros from an IP address.
import re
ip = "216.08.094.196"
string = re.sub('\.[0]*', '.', ip)
print(string)

# Write a Python program to check for a number at the end of a string
import re  
def end_num(string):  
    text = re.compile(r".*[0-9]$")  
    if text.match(string):  
        return True  
    else:  
        return False    
print(end_num('abcdef'))  
print(end_num('abcdef6'))

# Write Python program to search the numbers (0-9) of length between 1 to 3 in a given string.
import re  
results = re.finditer(r"([0-9]{1,3})", "Exercises number 1, 12, 13, and 345 are important")  
print("Number of length 1 to 3")  
for n in results:  
     print(n.group(0))

# Write a Python program to find the substrings within a string
import re
text = 'Python exercises, PHP exercises, C# exercises'
pattern = 'exercises'
for match in re.findall(pattern, text):
    print('Found "%s"' % match)

# Write a Python program to find the occurrence and position of the substrings within a string
import re
text = 'Python exercises, PHP exercises, C# exercises'
pattern = 'exercises'
for match in re.finditer(pattern, text):
    s = match.start()
    e = match.end()
    print('Found "%s" at %d:%d' % (text[s:e], s, e))
     
# Write a Python program to replace whitespaces with an underscore and vice versa
import re
text = 'Python Exercises'
text =text.replace (" ", "_")
print(text)
text =text.replace ("_", " ")
print(text)

# Write a Python program to extract year, month and date from an url.
import re  
def extract_date(url):  
        return re.findall(r'/(\d{4})/(\d{1,2})/(\d{1,2})/', url)  
url1= "https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/"  
print(extract_date(url1))

# Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format
import re
def change_date_format(dt):
        return re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})', '\3-\2-\1', dt)
dt1 = "2026-01-02"
print("Original date in YYY-MM-DD Format: ",dt1)
print("New date in DD-MM-YYYY Format: ",change_date_format(dt1))

# Write a Python program to match if two words from a list of words starting with letter 'P'.
import re
# Sample strings.
words = ["Python PHP", "Java JavaScript", "c c++"]

for w in words:
        m = re.match("(P\w+)\W(P\w+)", w)
        # Check for success
        if m:
            print(m.groups())

# Write a Python program to separate and print the numbers of a given string
import re
# Sample string.
text = "Ten 10, Twenty 20, Thirty 30"
result = re.split("\D+", text)
# Print results.
for element in result:
    print(element)

# Write a Python program to find all words starting with 'a' or 'e' in a given string
import re
# Input.
text = "The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly."
#find all the words starting with ‘a’ or ‘e’
list = re.findall("[ae]\w+", text)
# Print result.
print(list)

# Write a Python program to separate and print the numbers and their position of a given string.
import re
# Input.
text = "The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly."
for m in re.finditer("\d+", text):
    print(m.group(0))
    print("Index position:", m.start())

# Write a Python program to abbreviate 'Road' as 'Rd.' in a given string
import re
street = '21 Ramkrishna Road'
print(re.sub('Road$', 'Rd.', street))

# Write a Python program to replace all occurrences of space, comma, or dot with a colon - 
import re
text = 'Python Exercises, PHP exercises.'
print(re.sub("[ ,.]", ":", text))

#Write a Python program to find all five characters long word in a string
import re
text = 'The quick brown fox jumps over the lazy dog.'
print(re.findall(r"\b\w{5}\b", text))

#Write a Python program to find all three, four, five characters long words in a string.
import re
text = 'The quick brown fox jumps over the lazy dog.'
print(re.findall(r"\b\w{3,5}\b", text))

#Write a Python program to remove multiple spaces in a string
import re
text1 = 'Python      Exercises'
print("Original string:",text1)
print("Without extra spaces:",re.sub(' +',' ',text1))

#Write a Python program to remove all whitespaces from a string
import re
text1 = ' Python Exercises '
print("Original string:",text1)
print("Without extra spaces:",re.sub(r'\s+', '',text1))

# Write a Python program to implement pow(x, n).
class py_solution:
    def pow(self, x, n):
        if not n:
            return 1
        if n > 1:
            return eval("{}{}".format(x,"*{}".format(x)*(n-1)))
        if n < 1:
            return eval("1/({}{})".format(x, "*{}".format(x) * (-n - 1)))

print(py_solution().pow(2, -3));
print(py_solution().pow(3, 5));
print(py_solution().pow(100, 0));
            
# Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle
class Circle():
    def __init__(self,r):
        self.radius = r

    def area(self):
        return self.radius**2*3.14
    def perimeter(self):
        return 2*self.radius*3.14

NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())

#Write a Python program to remove duplicates from Dictionary
dict = {'one': 1, 'two': 2, 'three': 3, 'four': 1}
#print(dict)
#print(dict.items())
res = {}
for key,value in dict.items():
    if value not in res.values():
        res[key] = value
print(res)

## Write a Python program to check a dictionary is empty or not.
my_dict = {}
if not bool(my_dict):
    print("Dictionary is empty")
else:
    print("Dictionary is not empty")

##Write a Python program to combine two dictionary adding values for common keys
from collections import Counter
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d = Counter(d1) + Counter(d2)
print(d)

#Write a Python program to print all unique values in a dictionary.
L = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]
print("Original List: ",L)
u_value = set( val for dic in L for val in dic.values())
print("Unique Values: ",u_value)

#Write a Python program to sort a dictionary by key.
color_dict = {'red' : 1, 'blue': 2, 'orange': 3}
print(color_dict)
print(sorted(color_dict))
for key in sorted(color_dict):
    print("%s : %s" % (key, color_dict[key]))

#Write a Python program to sum all the items in a dictionary.
dict = {'n1': 1, 'n2': 2, 'n3': 3}
print(sum(dict.values()))

##lst=[1,2,3,0,5,6,0]. shift all 0's to left using python
You can achieve this by iterating through the list and moving all the zeros to the left while maintaining the order of the non-zero elements. Here's a Python code to do that: ```python def move_zeros_to_left(lst): # Initialize an empty list to store the result result = [] # Count the number of zeros in the list zero_count = lst.count(0) # Add zeros to the result list for i in range(zero_count): result.append(0) # Add non-zero elements to the result list for num in lst: if num != 0: result.append(num) return result # Test the function lst = [1, 2, 3, 0, 5, 6, 0] result = move_zeros_to_left(lst) print(result) ``` Output: ``` [0, 0, 1, 2, 3, 5, 6] ``` This code first counts the number of zeros in the list, then adds zeros to the result list followed by non-zero elements from the original list. This ensures that all zeros are shifted to the left while maintaining the order of the non-zero elements.

Thursday, April 27, 2017

Python Notes

Installation:

Install python 3.6.x
www.python.org
$ python   2.7x    -  idle
$ python3          -  idle3
NAWLEKHA-M-Q1GZ:python_training nawlekha$ idle3

IDE:-
------
idle    - default install
idlex   - extra features
pydev   - eclipse + python plugin
pycharm - is an IDE. PyCharm is cross-platform, with Windows, macOS and Linux versions

Python using "C"       - Cpython
python using "java"    - Jython
       using "C#"      - IronPython
       using c/cpython - stackless python
       using Cpython   - pypy

numpy      - data sci
matplotlib - graphs
pygame     - games
pybrain    - AI
sci-py     - machine learning
NLTK       - m/c learning
hadoop     - pyspark
WS         - AWS
web        - django/flask/bottle/cherrypy
Em         - rasberry-PI
IOT        - OS developed using python

=========================
>>Shell Scripts + awk + sed   - lack of data structures/libs

>>Perl                        - lack of read - regex/cpan.org - low level

>>TCL-TK                      - CLI - expect                  - low level

>>Python                      - app level program / SDN (software defined network)
                                pypi.python.org
                                simple
                                proc/oo/fp

>>Power SHell                 - windows
>> R                          - functional programming
>> Scala                      - functional programming
>> go scripts                 - threads - m/c dependency, used by google on server side

=========================
Python Arch

Object Oriented
>> every small component used in python is a OBJECT
Entities - world which exists
         - which can defined
         - which differ
Attributes -
Behaviours     -
Entities is divided into: (eg Employee) Array
Attributes - data on entity (Name, id, address, age) , (length, memory,name)
behaviour -  action on entity (createjob (), assignjob()), (add, insert,remove)

Data Structures:-
=================
a=10      a - object ref
          10- object
b=a       shallow copy - incr the ref count
c=10      data pool
a=63      it points another location
if a value change b value does't change. it points to another location

Im-Mutable  - int/long/float/str/tuple  # bool/complex/Nonetype/bytes
Mutable     - bytearray/list/set/dict   # frozenset

Basic INPUT statement in python:-
======================
a = input("string")
>>return value of input is a STRING

Basic OUTPUT statement in python:-
=======================
print("statement")
1) file extension should be .py
2) #! - she-bang
3) shld have execute perm
$ which python3

prog1.py:-
===========
#!/usr/bin/python3
a = input("Enter the value of a : ")
b = input("Enter the value of b : ")
res = a + b
print("Final ans = ",res)

prog2.py:-
===========
#!/usr/bin/python3
a = input("Enter the value of a : ")
b = input("Enter the value of b : ")

# type conversion
res = int(a) + int(b)
print("Final ans = ",res)

prog3.py:-
===========
#!/usr/bin/python3
a = int(input("Enter the value of a : "))
b = int(input("Enter the value of b : "))
res = a+b
print("Final ans = ",res)

string-class:-
==============
>>default is UNICODE strings
>>single quoted  a='hello'
>>double quoted  a="hello"
>>triple quoted  a='''hello'''

Convert the string from UNICODE to bytes
a="hello" # Str
res=a.encode("utf-8")   # converts unicode string to bytes
OR
a=b"Hello" # bytes
hardware representation is in bytes

a=b"hello"
res=a.decode("utf-8")    # convert bytes to UNICODE STRING

string operations:-
a='hello world'
string length          : len(a)
first element          : a[0]     # indexing
last element           : a[-1]
first 4 elems          : a[:4]    # slicing
last 4 elems           : a[-4:]
except first 4         : a[4:]
except last 4          : a[:-4]
except first 4 & last 4: a[4:-4]
Alt elem               : a[::2]
Alt elem               : a[1::2]
reverse                : a[::-1]
concate                : "hello" + a
upper                  : a.upper()

accept the string from the user

sampling
sa-MPLI-ng

1234567890
12-345678-90

res = input("Enter the string : ")
new = res[:2]+"-"+res[2:-2].upper()+"-"+res[-2:]
print(new)

accept the string from the user

sampling

S-nilpma-G

sol:
a[0].upper() + "-" + a[1:-1][::-1].lower() + "-" + a[-1].upper()

5/2   - true division
5//2  - floor division

Guess:-
========
a="12345678"

p1  = a[0:len(a)//2]

p2  = a[len(a)//2:]

print(p1)  # 1234
print(p2)  # 5678

others functions:-
-------------------
search for a substring in a string ---->  "hello" in mystr
                                   ---->  "hello" not in mystr
split the string                   -----> flst = a.split(delimiter)
a="192.168.1.125"

>> is "a" is string var - YES
>> what is delimiter    - "."
flst = a.split(".")
print(flst)
print(len(flst))
print(flst[0])
print(flst[-1])

find:-
------------
a.replace()
a.count()
a.index()
a.rindex()
a.find()
a.rfind()
a.rstrip()
a.lstrip()
a.strip()

----------------------------------------------------------------
a='sample data was added'

display the first word    a.split()[0]
display the last word     a.split()[-1]

display the first words last char  a.split()[0][-1]
display the last words first char  a.split()[-1][0]

a=10 20 30 40 50 60 70"
display first 4 fields

a.split()[0:4]

a="north 10,20,30,40"
display the first & last fields i.e 10 & 40

flst = a.split()[1].split(",")
print(flst[0],flst[-1])

import re
a="north 10,20,30,40"
flst = re.split("[\s,]",a)
OR
import re
a="sample-----hello-data----using-done"
b=re.sub("-+","-",a)
print(a)

a="north 10,20,30,40"

Branching:-
===========
>> and
>> or
>> not
>> elsif

if conidition:
  statement1
  statement2
else:
  statement1
  statement2

ex1:
 if a>=0 and a<=100:
   print("in range")
 else:
   print("out of range")

ex2:
 if a[0] in "aeiou":
   print("Vowel")
 else:
   print("Conso")

accept string & check the given string is a palindrome

madam
nitin
nayan
gadag

name = "nayan"
print(name[::])
print(name[::-1])
if name[::] == name[::-1]:
    print("palindrome")
else:
    print("no palindrome")

Generate natural nos:-
----------------------
>> what is the diff b/w range & xrange  of python 2.x
>> What is the diff b/w zip  & izip
>> GENERATOR pattern function - range(start,stop-1,step)
>> range(1,11)
>> list(range(1,1))

range will not allocate memory for all elements. It will assign memory based on demand.
range returns generator.generator has to be iterated then only we can extract the values

Loop:-
======
i=1
while i<=10:
 print(i)
 i+=1

Iterator:-
==========
>> special classes designed to work on CONTAINERs
>> automatically starts
>> automatically ends  - StopIteration
>> automatically advance

name='harish'

for alpha in name:
  print(alpha,end=" ")

Guess:-
--------
for i in range(1,6):
  print(i," = ",i*i)

Guess:-
=======
numlst = [10,20,30,40,50,60,70,80]

for i in range(len(numlst)):
  print(i,numlst[i])
OR
for num in numlst:
  print(num)

data is divided into
sequences = tuples, bytes, list
and
non-sequences = set and dict

=======================
Tuple-class:-
==============
>>constant collection
>>Im-mutable
>>months=("jan","feb","mar","apr".....)
>>weeks=()
>>numbers=("zero".....)

1) a = (10,20,30,40)
   OR
   a = 10,20,30,40

2) for num  in  a:
     print(num)

3) a,b,c = 10,20,30   # equal distribution - Tuple Unpacking

   a,b,*c = 10,20,30,40,50
   a,*b,c = 10,20,30,40,50
   *a,b,c = 10,20,30,40,50

Bytearray-class:-
=================
>>collection of bytes
>>collection of unsigned integers
>>mutable

a = bytearray([1,2,3,4])

=======================

List-class:-
=============
>> collection
>> Mutable

defined      - alst = [1,2,3,4,5]
length       - len(alst)

Add          - alst.append(60)
add more     - alst.extend([70,80,90])
insert       - alst.insert(index,value)

delete       - alst.pop(index)
delete       - alst.remove(VALUE)  # first occurance

sort         - a.sort()
reverse      - a.reverse()
stat fns     - sum(alst)
               max(alst)
               min(alst)

Guess:-
========
a = (10,20,[30,40,50],60,70)
a[0]
a[1]
a[2]
a[2][0] = 55
a[2].append(66)

numlst = [1,2,3,4,5,6,7,8,9]

oddlst = []  # compulsory
evenlst = []  #

for num in numlst:
   if num % 2 ==0:
     evenlst.append(num)
   else:
     oddlst.append(num)

grplst = ["team1-55",
          "team2-34",
          "team3-26",
          "team4-63",
          "team5-23"
         ]

#total vals
numlst=[]
for grp in grplst:
  flst = grp.split("-")
  numlst.append(int(flst[1]))

print(sum(numlst))

ex1:-
======
#outputlst = list(map(FUNCTNAME, inputlst))

datalst = ["10","20","30","40","50"]

templst=[]
for data in datalst:
  templst.append(int(data))
OR
for i in range(len(datalst)):
  datalst[i] = int(datalst[i])
OR
datalst = list(map(int, datalst))
print(sum(datalst))

https://public.etherpad-mozilla.org/p/python

studlst=[
         "arun-blr-CSE-15,20,18",
         "hari-chn-CIV-23,24,21",
         "tanu-hyd-MEC-18,19,11",
         "ravi-tvm-ECE-11,19,20"
       ]
avg of best 2

sol:
for stud in studlst:
  name,loc,dept,*mlst = stud.replace("-",",").split(",")
  mlst=list(map(int,mlst))
  mlst.sort(reverse=True)
  avg=(mlst[0] + mlst[1])/2
  print(name,loc,dept,avg)

team1= ["anil","ravi","guru","jagu","john"]
team2= ["john","anil","hari","amith","jaya"]
for mem in team1:
  if mem in team2:
    print(mem)

set-class:-
===========
>> collection of unique values
>> duplicates are deleted
>> non-sequence data structure
>> un ordered datastructure

define    :  a={10,20,30,40,50}
             b={10,40,80,20,70}

union     :  a|b   # a.union(b) : {70, 40, 10, 80, 50, 20, 30}
intersect :  a&b   # a.intersection(b)  : {40, 10, 20}
minus     :  a-b   # a.minus(b) : {50, 30}

common b/w two sets   : a&b
uncommon b/w two sets : (a|b) - a&b
                      : (a-b) | (b-a)

dict-class:-
============
>> collection of key-value
>> mutable
>> lookup table
>> non-sequence / un ordered data structure
>> key-based lookup
define      : colors={
                      "blue" : [10,20,30],
                      "red"  : [40,50,60],
                      "green": [70,80,90]
                     }
get         : colors["red"]
            : colors.get("red","UNKNOWN COLOR")
set         : colors["red"]=[11,22,33]
add         : colors["white"] = [5,6,7]
            : colors.update({"white" : [5,6,7]})
search      : if "black" in colors
delete      : colors.pop("red")
ALL KEYS    : colors.keys()
ALL VALS    : colors.values()
traverse    : for i in colors:
                 print(i,colors[i])
-------------------------------------------------
colors={
        "blue" : [10,20,30],
        "red"  : [40,50,60],
        "green": [70,80,90]
    }

user = input("Enter any color : ").lower()

if user in colors:
    print("yes")
    print("Selected color = ",user)
    print("Value          = ",colors[user])
else:
    print("no")
    print(colors.keys())



studs = {"arun"  : "blr-cse-VIT",
         "john"  : "chn-ece-BIT",
         ..
        }
prompt the user to enter the name
         name
         loc
         dept
         coll name


emps={ 101  :  "arun-blr-500",
       102  :  "jack-chn-503",
       103  :  "ravi-hyd-501",
       104  :  "tina-tvm-500"
     }
depts={500 : "sales",
       501 : "purch",
       502 : "accts",
       503 : "hrd"
      }

emp-id : 101

name     =
loc      =
deptid   =
deptname =

code = int(input("Enter the emp code : "))

if code in emps:
  name,loc,deptid = emps[code].split("-")
  print("NAme = ",name)
  print("loc  = ",loc)
  print("Depid = ",deptid)
  print("dname = ",depts[int(deptid)])
else:
  print("Not Found")

===========================

a=10          #int
b="hello"     #str
c=2.5         #float
e=bytearray() #bytearray
f=(1,2,3)     #tuple
g=[1,2,3]     #list
h={1,2,3}     #set
i={}          #dict
j=b"sample"   #bytes
k=None        # empty

==========================
Composition:-
==============
a = [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ]
type(a)

print(a[0][0])
1

How to install a 3rd party modules in Python:-
==============================================
$ sudo pip3 install numpy
OR
$ sudo easy_install3 numpy
pip3 install pandas
pip3 install matplotlib

numpy:-
=======
>> written in "C" - cython
>> fast to python

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[1,2,3],[4,5,6],[7,8,9]])

c1=a+b

c2=a.dot(b)

print(c1)
print(c2)

print(a[:,0])
print(a.sum())

print(np.arange(1.0,2.0,.1))
----------------------------------------------------------------------------
studs= {"arun" : [1,2,3],
        "ravi" : [4,5,6]
       }

studs["arun"]
studs["arun"][0]
studs = {
          "arun" : {"dept" : "CSE", "marks" : [1,2,3]  },
          "ravi" : {"dept" : "ECE", "marks" : [4,5,6]  }
        }

studs["ravi"]["dept"]
studs["arun"]["marks"][0]

HTML - XML/JSON
JSON -
Perl -
Ruby -
======================
Files :-
========
>> to store the data permanently

>> Text Files   - human readable
>> Binary Files - m/c readable

>> 3 ref points   BOF - 0
                  CUR - 1
                  EOF - 2

>> Diff modes
    r - rb  - readonly     - BOF - retained
    w - wb  - writeonly    - BOF - lost
    a - ab  - appendonly   - EOF - retained
    r+ - rb+ - read-write  - BOF - retained
    w+ - wb+ - write-read  - BOF - lost
    a+ - ab+ - append-read - EOF - retained

>> Random Access

   filehandler.seek(No_of_bytes, REF_POINT)

   filehandler.tell() - return nos bytes advanced from BOF to CURR Loc

fob.seek(0,0)    # to move to BOF
fob.seek(25,0)   # from BOF move 25 bytes ahead
fob.seek(100,1)  # from CURR loc move 100 bytes ahead
fob.seek(0,2)    # move to EOF

f1 = open("one.txt","w")

f1.write("Hello world\n")
f1.write("sample data was added\n")
f1.write("Thatz the end")

f1.close()

f2 = open("one.txt","r")
for line in f2:
  print(line)

f2.close()
--------------------------------------------------------------
fob = open("data.txt","w+")

fob.write("kar-blr\n")
fob.write("ap-ama\n")
fob.write("ts-hyd\n")
fob.write("tn-chn\n")
fob.write("ker-tvm")

fob.seek(0,0)
for line in fob:
  line=line.strip()
  print(line.split("-")[0])

fob.close()
--------------------------------------------------------------
fob = open("data.txt","w+")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30)

csvfile = open("out.csv","w")

fob.seek(0,0)

for line in fob:
  line = line.strip()
  if line:
    flst = line.split("-")
    tot  = sum(map(int,flst[2::2]))
    print("%s,%.2f\n" %(flst[0],tot))
    csvfile.write("%s,%.2f\n" %(flst[0],tot))
fob.close()
csvfile.close()

----------------------------------------------------------------
strbuffer = fob.read()    # read complete file as string
strbuffer = fob.read(50)  # read only 50 bytes
strbuffer = fob.readline()  # read upto \n or EOF
lstbuffer = fob.readlines() # read complete file
fob.write(strbuffer)
fob.writelines(lstbuffer)
-----------------------------------------------------------------
studs.csv
----------
name,dept,avg
arun,CSE,80
ravi,ECE,85
john,ELE,78
hari,CSE,67

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("studs.csv")

print(df)

df.plot()

plt.show()
------------------------------------------------------------------------------

import os
import sys

if not os.path.isfile("one.txt"):  #check whether file exists
  print("File not found")
  sys.exit()
------------------------------------------------------------------------------
with open("one.txt","r") as fob:  # file autoclosed
  print("hello")
  print("hai")

print("Some")
------------------------------------------------------------------------------
xlrd - excel reader
xlwt - excel writer

ex:

import xlrd

b = xlrd.open_workbook("hello.xlsx")

sheet = b.sheet_by_index(0)

for i in range(sheet.nrows):
 for j in range(sheet.ncols):
  print(sheet.cell(i,j).value)

numpy           OR   custom built Framework  or PYATS
pandas
matplotlib
xlrd
xlwt
--------------------------------------------------
Functions:-
===========
f(x) = x2
f(5) = 25
def f(x):
  return x*x
OR
f1 = lambda x : x*x
>>> f1(5)
25

ex:
f(x,y) = x+y
f = lambda x,y :  x+y
>>> f(2,3)
5
def fun(x,y):
  return x+y

>> var defined within the fn - LOCAL
>> var defined outside the fn - GLOBAL
>> fns has to be defined before its call

def add(n1,n2):
  res = n1 + n2
  retunr res

a=10
b=20
c = add(a,b)
print(c)

--------------------------------------------------------
namelst = ["arun","ravi","john","manu","eshu"]
reslst=[]
for name in namelst:
  reslst.append(name[0])
OR
reslst = [name[0] for name in namelst  ]   # list comprehension
OR
reslst = list(map(lambda x : x[0], namelst))  # map-iterator
--------------------------------------------------------
numlst = [1,2,3,4,5,6,7,8]
reslst=list(map(lambda x: x*x , numlst))
zonelst=['north-10','south-20','east-30','west-40']
numlst = list(map(lambda x: int(x.split("-")[1]), zonelst))
print(sum(numlst))
--------------------------------------------------------
# proof the that all the cALL in python are CALL BY REFERENCE
def add(n1,n2):
 print("n1 = ",id(n1))
 print("n2 = ",id(n2))
 res = n1 + n2
 return res

a=10
b=20
print("a = ",id(a))
print("b = ",id(b))
c = add(a,b)
print(c)

--------------------------------------------------------
Guess:-
========
def fun(alst):
  alst[0:4] = "0" * 4
  print("ALST = ",alst)
numlst = [10,20,30,40,50]
fun(numlst)
print("NUMLST = ",numlst)

------------------------------------------------------------------------
positional args:-
=================
def addRecord(name,dept,loc,sal):
  pass

addRecord()                       # error
addRecord("arun")                 # Error
addRecord("arun","sales")         # error
addRecord("arun","sales","blr")   # error
addRecord("arun","sales","blr",15000)  # right-way
addRecord("sales","blr","arun",15000)  # right-way  # programmers

default args:-
==============
def addRecord(name=None,dept="finan",loc="hyd",sal=0):
  pass

addRecord()                       # work
addRecord("arun")                 # works
addRecord("arun","sales")         # works
addRecord("arun","sales","blr")   # works

addRecord("arun","sales","blr",15000)  # works
addRecord("sales","blr","arun",15000)  # works  # programmers resp
                                                # to pass in correct order
keyword args:-
==============
def addRecord(name=None,dept="finan",loc="hyd",sal=0):
  pass

addRecord(loc="chn")
addRecord(loc="blr",dept="IMS")


variable args:-
===============
>> *args
>> args is a TUPLE

def fun(*args):
  print(args)

fun()
fun(10)
fun(10,20,30,40)
fun(10,20)
fun(10,20,30)


variable keyword args:-
=======================
>> **kwargs
>> dictionary

def fun(**kwargs):
  print(kwargs)

fun(a=10,b=20,c=30)
fun()
fun(old="new.txt", new="that.txt")

Guess:-
---------
def fun(*args,**kwargs):
  pass

>> non-keyword first and then keyword args

others:-
--------
>>global
>>globals()
>>locals()

example for special fn - globals()
def fun():
  num=55  # LOCAL
  print("FUN = ",num)
  print("GLOBL = ",globals()["num"])

num=10    # GLOBAL
print("MAIN = ",num)
fun()
print("MAIN = ",num)

ex: for keyword global

def fun():
  global num
  num=55  # LOCAL
  print("FUN = ",num)

num=10    # GLOBAL
print("MAIN = ",num)
fun()
print("MAIN = ",num)

===============================
Modules:-
=========
>> collection of fns/classes
>> file extension shd be .py
>> can be include   import modulename
                    from modulename/package import *
>> once a module is import - BYTE CODE .pyc
>> module search path  import sys
                       sys.path

mathlib.py   - open source
mathlib.pyc  - closed source
mathlib.exe  - standalone / non-portable - py2exe

packages:-
==========
>> is a FOLDER
>> collection of modules/subpackages
>> every pack shld have a compulsory file - __init__.py
               
                            mylibs
                              |
     -------------------------------------------------------------------
    |                                                   |                                   |
mathlib.py another.py __init__.py
---------- ----------              -------------
def add(a,b): def add():              __all__=['mathlib',
  print("PACk = ",__package__)      print("HEllo")                 'another']
  print("SYM  = ",__name__)
  print("ANS = ",a+b)

def minus(a,b):
  print(a-b)

def divide(a,b):
  print(a/b)

def mult(a,b):
  print(a*b)

sample1.py:-
===========
import mylibs.mathlib
mylib.mathlibs.add(10,20)

sample2.py:-
============
from mylibs import *    # __init__.py
mathlib.add(10,20)
another.add()

sample3.py:-
============
from mylibs.mathlib import *
add(10,20)

=============================
mathlib.py
----------
add()/minus()/divide()/mult()

Method1:- Method2:
========= =========
import mathlib import mathlib as m

mathlib.add() m.add()

Method3:- method4:-
========== ==========
from mathlib import add from mathlib import *
add() add()

Method5:-
==========
from mathlib import add as libadd
def add():
  print("HELLO WORLD")

add()
libadd()

===========================
using import - call f1/f2/f3

import vendors.intel
import vendors.sony
import vendors.subvendor.nokia

vendors.intel.f1()
vendors.sony.f2()
vendors.subvendor.nokia.f3()

===========================
using from - call f1/f2/f3
from vendors import *
intel.f1()
sony.f2()
subvendors.nokia.f3()

===============================
from vendors.subvendors.nokia import *
f3()

===============================
import modulename - fully qualified names
from package import * - relative names
from package.module import * - relavtive name

===============================
classes:-
=========

>> local var - private will be accessible only within class
   global var - public will be accessible in/outside class


>> special fns
   constructor - fn same name as the CLASSNAME
                 is automatically called when a OBJECT IS CREATED

   destructor  - fn same name as the ~CLASSNAME
                 is automatically called when a OBJECT goes out of scope


>> Compile Time Class Development   - C++/JAVA/C#

>> Run Time Class Development       - PERL/PYTHON


Python Class Development:-
==========================
>> Run Time Classes - Monkey patching - Metaclasses
>> Class heirarchy - base class for all the classes - "object"
>> OLd class style  - python 2.x
>> New class style  - python 2.x / python 3.x
>> this pointer is renamed as "self"
>> self pointer is a compulsory arg for all the MEMBER FNS - RUN TIME CLASSES
>> constructor is def __init__
>> destructor is  def __del__  # optional - garbage collector
>> default access specifier within class - PUBLIC

mylib.py:-
==========
class Circle:

   def __init__(self,a):
      self.radius = a
      self.area = 0

   def find(self):
      self.area = 3.14 * (self.radius**2)

   def show(self):
      print(" %.2f = %.2f " %(self.radius,self.area))

sample.py:-
===========
import mylib
c1 = mylib.Circle(2.5)
c1.find()
c1.show()

=============================
class student:
 def __init__(self,name,mlst):
  self.name = name
  self.mlst = mlst

 def find(self):
  self.avg = sum(self.mlst)/3

 def show(self):
  print("%s %s %s"  %(self.name,self.mlst,self.avg)

 def compare(self,other):
  if self.avg > other.avg:
    return True
  else:
    return False


from mylib import student

s1 = student("Arun",[10,20,30])
s2 = student("RAvi",[40,30,10])

s1.find()
s2.find()

if s1.compareAvg(s2):
  s1.show()
else:
  s2.show()

Magic Methods:-
===============

>> Operator overloading

+    def __add__(self,other)
-    def __sub__(self,other)
*    def __mul__(self,other)
/    def __div__(self,other)

==   def __eq__(self,other)
!=   def __ne__(self,other)
>    def __gt__(self,other)
>=   def __ge__(self,other)
<    def __lt__(self,other)
<=   def __le__(self,other)

---------------------------------------------------------------------------
how to write a class         - keyword class
object im memory             - dictionary
this pointers                - self   # is not a keyword
acess data members in class  - self.datamember
acess data mem out class     - object.datamember
access methods               - object.methodname(args)
ctor                         - def __init__()
dtor                         - def __del__()
private data members         - self.__datamember
semi private                 - self._datamember
public data members          - self.datamember
private method               - def __method(self):
operator overloading         - magicmethods def __eq__(self,other)

static data members          - class properties
                               classname.staticdatamembers
static methods               - @staticmethod
inheritance                  - ISA-relationship
method over-riding           - super().methodname()
                               OR
                               classname.METHODNAME(self)

see later:-
----------
import abc

---------------------------------------------------------------------------
ex of static data members:-
===========================

a=10

class Sample:
  total
  def __init__(self,val):
    b=20
    self.value = val

what is a ? - GLOBAL VARIABLE print(a)
what is b ? - LOCAL VAR  print(b)
what is self.value ? - data member print(self.value)
what is total      ? - STATIC DATA MEMBER print(Sample.total)


ex:
class stock(object):  new style classes in python 2.x
class stock:          old style classes in python 2.x

class stock:          only style in python 3.x

ex:
class stock:

   __total=0
   def __init__(self,name,cat,qty):
     self.name = name
     self.cat  = cat
     self.qty  = qty
     stock.__total = stock.__total + self.qty

   @staticmethod           # inbuilt decorator
   def showtotal():
     print("Total = ",stock.__total)


if __name__ == '__main__':
 stock.showtotal()
 s1 = stock("a","b",10)
 s2 = stock("a","b",10)
 s3 = stock("a","b",10)
 s4 = stock("a","b",10)
 stock.showtotal()

---------------------------------------------------------------
example for Inheritance:-
=========================
>> there is CTOR CASCADING in python, which we have to do

class Account:
  def __init__(self,num,name=None,bal=0):
    self.num = num
    self.name = name
    self.bal  = bal
  def show(self):
    print("%s %s %s" %(self.num,self.name,self.bal))

class Savings(Account):                  # ***** Deriving class
  def __init__(self,num,name=None,bal=0,intrate=4,cap=0):
    Account.__init__(self,num,name,bal)  # **** CTOR CASCADING
    self.intrate = intrate
    self.cap     = cap
  def show1(self):
    print("%s %s" %(self.intrate,self.cap))


s1 = Savings(12345,"arun",15000,5.5,100000)
s1.show()
s1.show1()

---------------------------------------------------------------
example for method over-riding:-
================================
class Account:
  def __init__(self,num,name=None,bal=0):
    self.num = num
    self.name = name
    self.bal  = bal
  def show(self):
    print("%s %s %s" %(self.num,self.name,self.bal))

class Savings(Account):                  # ***** Deriving class
  def __init__(self,num,name=None,bal=0,intrate=4,cap=0):
    Account.__init__(self,num,name,bal)  # **** CTOR CASCADING
    self.intrate = intrate
    self.cap     = cap
  def show(self):                      # over ridden method
    super().show()                     # how to invoke a OVER RIDDEN METHOD
    print("%s %s" %(self.intrate,self.cap))


s1 = Savings(12345,"arun",15000,5.5,100000)
s1.show()

---------------------------------------------------------
Exception handling:-
====================
>> Software interrupts in a program - Exception

>> Implicit handlers
     done by the compilers it self
     abrupt exit
     there is no communication to the programmers


>> Explicit handlers
     designed by programmers
     safe exit
     program resumes back to the SAID POINT
     keywords try/except/finally/raise

>> asynch exceptions - automatic
>> synch exceptions  - mannual  - raise

ex:
# user defined exceptions
class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

n1 = input("Enter the value of n1 : ")
n2 = input("Enter the value of n2 : ")

n1 = int(n1)
n2 = int(n2)

if n1==0 or n2==0:
  raise ZeroError("Value cannot be ZERO")

res = n1 + n2
print("FINAL = ",res)

ex:
class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

try:
    n1 = input("Enter the value of n1 : ")
    n2 = input("Enter the value of n2 : ")
    n1 = int(n1)
    n2 = int(n2)

    if n1==0 or n2==0:
       raise ZeroError("Value cannot be ZERO")
    res = n1 + n2
    print("FINAL = ",res)
except:
    print("Oh problem")

ex:
import traceback

class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

try:
    n1 = input("Enter the value of n1 : ")
    n2 = input("Enter the value of n2 : ")
    n1 = int(n1)
    n2 = int(n2)

    if n1==0 or n2==0:
       raise ZeroError("Value cannot be ZERO")
    res = n1 + n2
    print("FINAL = ",res)
except ZeroError as e1:
  print("Action-1",e1)
except ValueError as e2:
  print("Action-2",e2)
except EOFError as e3:
  print("ACtion-3",e3)
except:                            # Generic CATCH BLOCK
    print("Oh problem")
    traceback.print_exc()
finally:
    print("Cleanup")

==============================
Inbuilt libs:-
==============
CORE LIBs - which are installed with the compiler

1) Data persistance modules

pickle   - import pickle
json     - imprt json

shelve   - import shelve
db       - import sqlite3

import pickle

fob = open("info.pickle","wb")

info = { "A" : 10,
         "B" : "hello",
         "C" : 2.5
         "D" : [10,20,30,40],
         "E" : (1,2,3)
         "F" : {"a":1,"b":2}
       }
pickle.dump(info, fob)
fob.close()
------------------------------------------------------------------------
import pickle

fob = open("info.pickle","rb")

res = pickle.load(fob)

fob.close()

print(res)

--------------------------------------------------------------------

2) File Related

import glob        - list all the files
import fileinput   - read the file
import filecmp     - compare 2 files/2 dirs
import shutil      - copy/move file/dirs
import tarfile     - compress folder
import zipfile     - compress file
import stat        - file stats
ex:
import glob
res = glob.glob("*.txt")
print(res)

ex:
import fileinput
res = fileinput.input("one.txt")
print(list(res))

3) Date & Time

from datetime import date,timedelta

print(date.today())

d1 = date(2010,5,1)
d2 = date.today()

print(d2-d1)
print(d1==d2)
print(d2+timedelta(15))


from datetime import time
from datetime import datetime

4) Others

import math
import random
import hashlib   - md5/sha1/sha256
import optparse/argparse/shopts
import logging

import os
import sys
ex:
from optparse import OptionParser

p = OptionParser()
p.add_option("--host1",type="str",action="store",dest="a")
p.add_option("--host2",type="str",action="store",dest="b")
p.add_option("--out",type="int",action="store",dest="c")
p.add_option("--in",type="int",action="store",dest="d")

args, extraargs = p.parse_args()

print(args.a)
print(args.b)
print(args.c)
print(args.d)

=============================

debug   - 10
info    - 20
warning - 30  - default level
error   - 40
critical- 50


import logging

logging.debug("Hello-1")

logging.info("Hello-2")

logging.warning("Hello-3")

logging.error("Hello-4")

logging.crtical("Hello-5")

ex:

import logging
logging.basicConfig(level=logging.DEBUG)

logging.debug("Hello-1")

logging.info("Hello-2")

logging.warning("Hello-3")

logging.error("Hello-4")

logging.critical("Hello-5")

============================
import sys   -  Interface b/w programmer & Python Inter

sys.version
sys.implementation
sys.path

sys.argv
sys.stdin
sys.stdout
sys.stderr

sys.platform
sys.getsizeof(a)
sys.getrefcount(a)


import os    - Interface b/w programmer & underlying OS

os.name
os.pid()
os.getcwd()
os.system("commands")  #    os.system("echo $$")
os.remove("one.txt")
os.environ
os.environ["PATH"]

Concurrency:-
=============

import time
import os

def job1():
  print("REAding from file",os.getpid())
  time.sleep(4)

def job2():
  print("Writing from file",os.getpid())
  time.sleep(6)

def job3():
  print("copying files", os.getpid())
  time.sleep(7)

def job4():
  print("printing to PRN", os.getpid())
  time.sleep(5)

if __name__ == '__main__':
  start = time.time()
  job1()
  job2()
  job3()
  job4()
  end = time.time()


import multiprocessing
import threading
import subprocess
import concurrent


ex:
import time
import os
from multiprocessing import Process
from threading import Thread


def job1():
  print("REAding from file",os.getpid())
  time.sleep(4)

def job2():
  print("Writing from file",os.getpid())
  time.sleep(6)

def job3():
  print("copying files", os.getpid())
  time.sleep(7)

def job4():
  print("printing to PRN", os.getpid())
  time.sleep(5)

if __name__ == '__main__':
  p1 = Process(target=job1,args=())
  p2 = Process(target=job2,args=())
  p3 = Process(target=job3,args=())
  p4 = Process(target=job4,args=())

  p1.start()
  p2.start()
  p3.start()
  p4.start()
  start = time.time()
  p1.join()
  p2.join()
  p3.join()
  p4.join()

  end = time.time()
  print("Time took = ",end-start)


ex:-
====

import time
import os
from multiprocessing import Process
from threading import Thread


def job1():
  print("REAding from file",os.getpid())
  time.sleep(4)

def job2():
  print("Writing from file",os.getpid())
  time.sleep(6)

def job3():
  print("copying files", os.getpid())
  time.sleep(7)

def job4():
  print("printing to PRN", os.getpid())
  time.sleep(5)

if __name__ == '__main__':
  p1 = Thread(target=job1,args=())
  p2 = Thread(target=job2,args=())
  p3 = Thread(target=job3,args=())
  p4 = Thread(target=job4,args=())

  p1.start()
  p2.start()
  p3.start()
  p4.start()
  start = time.time()
  p1.join()
  p2.join()
  p3.join()
  p4.join()

  end = time.time()

  print("Time took = ",end-start)

Queue
Lock
Semaphores

t = threading.Lock()
t.acquire()

act

t.release()
==========================

Test     - pyUnit / unittest - WBT


import unittest
import yourlibtotest

class mytest(unittest.TestCase):

  def setUp(self):
    pass

  def tearDown(self):
    pass

  def test1(self):
    self.assertEquals(yourlibtotest.funct(10,20) , 0)


if __name__ == '__main__':
  unittest.main()


Doc     :   def fun():
             ''' doc
             some
             some
             '''
             source code


Install

Distribute   - distutils / setuptools

setupy.py
==========
from distutils.core import setup
setup(name='foo',
      version='1.0',
      py_modules=['file1',file2,file3,file4],
      )


$ python setup.py sdist

$ foo.tar.gz
=========================

$ extract tar file

$ Cd foo

$ python setup.py install
========================

debug
automation  - CLI/Net/Web