Friday, December 27, 2019

Sum Of Series - PL/SQL

Program:

declare
n number:=&n;
i number;
begin
i:=n*(n+1);
n:i/2;
dbms_output.put_line('The sum of series is:'||n);
end;

Output:

Enter value for n; 5
The sum of series is: 15

Fibonacci - PL/SQL

Program:

declare
i number;
c number;
n number;
a number;
b number;
begin
for i in 1..n
loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end

Output:

Enter valur for n; 5
0
1
1
2
3

Palindrome - PL/SQL

Program:

declare
len number;
a integer;
str1 varchar(10):='&str1';
str2 varchar(10);
begin
len:=length(str1);
a:=len;
for i in 1..a
loop
str2:=str2||substr(str1,len,1);
len:=len-1;
end loop;
if(str1=str2) then
dbms_output,put_line(str1 ||' is a palindrome');
else
dbms_output,put_line(str1 ||' not a palindrome');
end if;
end

Output1:

Enter value for str1; liril
liril is a palindrome

Output2;

Enter value for str1; faith
faith not a palindrome


Factorial Of A Number - PL/SQL

Program:

declare
f number:=1;
i number;
n number:=&n;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('The factorial of a given no is:'||f);
end;

Output:
Enter alue for n: 6
The factorial of a given n is: 720

Addition Of Two Numbers - PL/SQL

Program:

declare
x integer;
y integer;
z integer;
begin
x:=&x;
y:=&y;
z:=x+y;
dbms_output.put_line('The sum of two number is ='||z);
end;



Output:

Enter value for x: 9
Enter value for y: 9
The sum of two number is =18


Saturday, April 14, 2018

Sample Program – Python Dictionary



pythonDictionary.py

# Dictionary - Python
from pip.utils.hashes import FAVORITE_HASH
from email.policy import default
studentInfo = {'rollNo':123,'name':'Avinash','age':8,'class':'III'}

print('*****Student Info*****')
print('Roll No: ',studentInfo['rollNo'],'\nName: ',studentInfo['name'],
      '\nAge; ',studentInfo['age'],'\nClass: ',studentInfo['class'])

fav = {'color':'Violet','fruit':'apple','animal':'tiger'}
print('\nFavourites: ',fav)

#dictionary functions
print('String representation: ',str(fav))
print('Length: ',len(fav))
print('Type: ',type(fav))

#copying a dictionary
fav_2 = dict.copy(fav)
print('fav_2 - copy of fav: ',fav_2)

#access the value by its key
print('Value of color: ',fav.get('color'))

#check if the key exits in the dictionary
print("Is the key fruit exists:"'fruit' in fav)

#print the list of values in a dictionary
print('Values of fav: ',fav.values())

#print the list of key in a dictionary
print("Keys of fav: ",fav.keys())

#print the list of items in a dictionary
print("Items of fav: ",fav.items())

#create a dictionary from keys from another dictionary with default value none
fav_3 = dict.fromkeys(fav)
print("fav_3: ",fav_3)

#clear a dictionary
dict.clear(fav_3)
print('fav_3 after clear: ',fav_3)

#update a dictionary with another dictionary value pair
fav_3.update(fav_2)
print('fav_3 after updating with fav_2: ',fav_3)

#adds a key if not present
fav_3.setdefault('place')
print('fav_3 after setdefault key: ',fav_3)


output:
*****Student Info*****
Roll No:  123
Name:  Avinash
Age;  8
Class:  III

Favourites:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
String representation:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
Length:  3
Type:  <class 'dict'>
fav_2 - copy of fav:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
Value of color:  Violet
Is the key fruit exists: True
Values of fav:  dict_values(['Violet', 'apple', 'tiger'])
Keys of fav:  dict_keys(['color', 'fruit', 'animal'])
Items of fav:  dict_items([('color', 'Violet'), ('fruit', 'apple'), ('animal', 'tiger')])
fav_3:  {'color': None, 'fruit': None, 'animal': None}
fav_3 after clear:  {}
fav_3 after updating with fav_2:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger'}
fav_3 after setdefault key:  {'color': 'Violet', 'fruit': 'apple', 'animal': 'tiger', 'place': None}




Sample Program – Python Tuple



pythonTuple.py

custInfo = ('CusId','CusName','City','Mobile')
custData = (1001,'Anitha','Pondicherry',876712349)

print("*****Cust Info*****")
print(custInfo[0],':',custData[0])
print(custInfo[1],':',custData[1])
print(custInfo[2],':',custData[2])

tuple_1,tuple_2 = (1,2,3,4),(1,7,8)
empty_tuple = ()

print('\nTuple 1: ',tuple_1,'\nTuple 1 count: ',len(tuple_1),'\nMax Value: ',max(tuple_1),'\nMin Value: ',min(tuple_1))
print('Tuple 2: ',tuple_2 )

#print empty tuple
print("Empty Tuple: ",empty_tuple)

#Assign value to empty tuple
empty_tuple = tuple_1 + tuple_2
print("Empty Tuple after assigning values: ",empty_tuple)

#converting list to a tuple
list_1 = ['Grapes','Apple','Orange']
list2Tuple = tuple(list_1)
print("List after converting it to tuple: ",list2Tuple)

#displaying a range from a tuple
print("Tuple_2 with first 2 values: ",tuple_2[0:2])
print("Tuple_2 with from 2nd index: ",tuple_2[1:])


output:
*****Cust Info*****
CusId : 1001
CusName : Anitha
City : Pondicherry

Tuple 1:  (1, 2, 3, 4)
Tuple 1 count:  4
Max Value:  4
Min Value:  1
Tuple 2:  (1, 7, 8)
Empty Tuple:  ()
Empty Tuple after assigning values:  (1, 2, 3, 4, 1, 7, 8)
List after converting it to tuple:  ('Grapes', 'Apple', 'Orange')
Tuple_2 with first 2 values:  (1, 7)
Tuple_2 with from 2nd index:  (7, 8)