Saturday, April 14, 2018

Sample Program – Python List



pythonList.py

subjects = ['Maths','Physics','Chemistry']
marks = [190,178,185]

print("*****Score Details*****")
print(subjects[0],':',marks[0])
print(subjects[1],':',marks[1])
print(subjects[2],':',marks[2])

list_1,list_2 = [1,2,3,4],[1,7,8]

print('\nList 1: ',list_1,'\nList count: ',len(list_1),'\nMax Value: ',max(list_1),'\nMin Value: ',min(list_1))
print('List 2: ',list_2 )

#inserting into list_2
list_2.append(10)
print('List 2 after append: ',list_2 )

#extending list_2 with sequence of values
list_2.extend([9,11,12])
print('List 2 after extend: ',list_2 )

#reversing the list
list_2.reverse()
print("Reverse list: ",list_2)

#finding the index of object in list
print('Index of 9 in list 2: ',list_2.index(9))

#sorting the list desc
list_2.sort(reverse= True)
print("List after sorting: ",list_2)

#removing object from the list
list_2.remove(11)
print("List after removing 11: ",list_2)

#updating object in the list
list_2[0]=13
print("List after updating index 0 with 13: ",list_2)

#displaying a range from a list
print("List with first 5 values: ",list_2[0:5])
print("List from 2nd index: ",list_2[1:])



output:
*****Score Details*****
Maths : 190
Physics : 178
Chemistry : 185

List 1:  [1, 2, 3, 4]
List count:  4
Max Value:  4
Min Value:  1
List 2:  [1, 7, 8]
List 2 after append:  [1, 7, 8, 10]
List 2 after extend:  [1, 7, 8, 10, 9, 11, 12]
Reverse list:  [12, 11, 9, 10, 8, 7, 1]
Index of 9 in list 2:  2
List after sorting:  [12, 11, 10, 9, 8, 7, 1]
List after removing 11:  [12, 10, 9, 8, 7, 1]
List after updating index 0 with 13:  [13, 10, 9, 8, 7, 1]
List with first 5 values:  [13, 10, 9, 8, 7]
List from 2nd index:  [10, 9, 8, 7, 1]



Sample program – Python Strings



pythonStrings.py
#Python strings
string_1 = 'Welcome to python strings' #strings assigned within single quotes
string_2 = "Python strings introduction" #strings assigned within double quotes
print ("String_1: ",string_1,"\nString_2: ",string_2)
print ("String from a given index: " , string_1[0])
print("Substring from given range: ",string_1[0:7])
print ("String Concatenation: ",string_1+"."+  string_2)
print ("String repetition: ",string_1[0] * 3)
print("Check the occurrence of a character in a string: ",'W' in string_1)
print("Check the non-occurrence of a character in a string: ",'W' not in string_1)
print("\nParagraph with triple single/double quotes: " , '''In python \t
multiple line of strings can be represented within triple quotes
''')

output:
String_1:  Welcome to python strings
String_2:  Python strings introduction
String from a given index:  W
Substring from given range:  Welcome
String Concatenation:  Welcome to python strings.Python strings introduction
String repetition:  WWW
Check the occurrence of a character in a string:  True
Check the non-occurrence of a character in a string:  False

Paragraph with triple single/double quotes: In python  
multiple line of strings can be represented within triple quotes



Sample program – Python Numbers



pythonNumbers.py
#Python Numbers
num_1 = num_2 = -123  #assigning same integer value to 2 variables
num_3 = 123.00  #assigning float value to variable
num_4 = 12.6j   #assigning complex number to variable
num_5 = 127 * -254 * 2.3j
print ("num_1 :", num_1, "\nnum_2 :" , num_2, "Type :" , type(num_1))
print ("num_3 :", num_3, "Type :" , type(num_3))
print ("num_4 :", num_4 , "Type :" , type(num_4))
print ("num_5 :" , num_5, "Type :" , type(num_5))

# deleting an object
del num_5


Output:
num_1 : -123
num_2 : -123 Type : <class 'int'>
num_3 : 123.0 Type : <class 'float'>
num_4 : 12.6j Type : <class 'complex'>
num_5 : (-0-74193.4j) Type : <class 'complex'>


Python Data Types

Python has 5 standard data types
·         Numbers 
Stores numeric values. It is further classified as follows
Ø  int
Ø  float
Ø  double
Ø  complex
                Syntax: variable = value
               Example: age = 26
·         String
Stores a set of character represented within single/double quotes
Syntax: variable = ‘value’ or variable = “value”
Example: name = ‘Anitha’ or name = “Anitha”              
·         List
 List are the versatile sequence type in python. List in python support multiple datatypes separated by comma and enclosed within square brackets. List are mutable
Syntax: variable = [val1, val2, val3…valn]
Example: empInfo = [‘Anitha’, 26, ’Engineer’]
·         Tuple
Tuple are similar to list but are non-mutable. Unlike list tuple are enclosed within   parenthesis
Syntax: variable = (val1, val2, val3…valn)
Example: studentInfo = (‘Avinash’, 5, ’Grade II’)
·         Dictionary
Dictionaries contains (key, value) pair and are kind of hash table type. The key can by be any datatype and values can be of arbitrary python object. Key-value pair are separated by colon : and multiple key-value pair are separated by comma and enclosed within curly braces {}
Key is unique and value may have duplicates
Syntax: variable = {key1:val1, key2:val2, key3:val3… keyn:valn}
Example: cusInfo = {‘cusID’: 234, ‘name’:’Atul’,’Age’:28,’Gender’:’M’}

Running Python Script File


Running Python Script File:

  • Open notepad -> write the python scrip and save it as PythonFile.py. py is the extension for python file

  •  Open cmd.exe
  •   Execute the python script by  entering python pythonFile.py and Enter



Running Your First Python Program

Running Your First Python Program:


  •         Open Command Prompt
  •          Type Cd <Python installed location in your system> and Enter
  •          Type python and Enter
  •          Type print (“Welcome to python world”)

Friday, March 03, 2017

Decode SQL

Usage : 

Decodes the values of a column with the one specified

How the Decode SQL works????

Consider  table "Participant_List" as shown below









Case :
List all the rows in a table with location specifying the full name

Syntax:
Select decode(<column_name>,
                       value 1,decode_value 1,
                       value 2,decode_value 2,
                       ...
                                   ,default_value)
from <table_name>

Sample Query:

Select participant_id,participant_name,participant_age,
           decode(location,
                        'IN','India',
                        'AU','Australia',
                        'SG','Singapore',
                        'US','United States'
                               ,'Unknown')
from Participant_List

Result: