Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, October 26, 2018

Python for data science


Operations:
Output: Sample prints
print(5 + 5) #Addition
print(5 - 5) # subtraction
print(3 * 5) # Multiplication
print(10 / 2) #Division
print(18 % 7) #Modulo
print(4 ** 2) #exponentiation
# How much is your INR 100 worth after 7 years at the rate of interest per year is 10%?
print(100 * 1.1 ** 7)
Variables:
Str -  “sangeeth”, ‘gowtham’
Bool - True /False
Int – 1, 9
Float – 1.98, 8.78
Function: Type() - //to find unknown type of variables
List:
My_list = []
Ex:
= [1, 3, 4, 2]
= [[1, 2, 3], [4, 5, 7]]
= [1 + 2, "a" * 5, 3] = [3,aaa,3]
= ["room1", 11.2478, "room2", 8.459, "room3", 74.56, "room4", 74.698, "room5", 10.25]
Subset listing:
List Index get:  [0,1,2,3…] (Asc),  [….-3,-2,-1] [Desc]
List Slicing:  [Start (inclusive) : End (exclusive)]
My_list = ["room1", 11.2478, "room2", 8.459, "room3", 74.56, "room4", 74.698, "room5", 10.25]
Get_First_6list = My_list [:6] or Get_First_6list = My_list [1:6] or Get_last_list = My_list[6:]
Sub setting lists of lists:
My_list = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
My_list[2][0]
My_list[2][:2]
List Manipulation:
My_list = ["room1", 11.2478, "room2", 8.459, "room3", 74.56, "room4", 74.698, "room5", 10.25]
To update list
My_list[<index position>] = “room5” or 9.897
Adding to new list by amending
My_list_1 = My_list + [“room6”, 24.5896]
Delete list
del(My_list [10]); del(My_list [11])
del(My_list [10:11])
del(My_list [-4:-2])
del(My_list [-3]); del(My_list [-4])
                Assigning one list to another
                                My_list _Copy = My_list (Note: while update My_list_copy it will affect My_list)
My_list_copy1 = My_list [:] (Note: This will copy all list values from My_list, but while update My_list_copy1 it will affect My_list)
Functions:
Max
Min
Sorted
len
Complex
Help(<functionName>) – to get details for function and arguments
Methods:
Upper (Ex: <StrVariables>.upper()), Count (Ex: <Variables>.count(..))
print(<list>.index(20.0)) – Index position finding
print(<list>.count(9.50)) – getting count of repeated times in list values
append(), that adds an element to the list it is called on,
remove(), that removes the first element of a list that matches the input, and
reverse(), that reverses the order of the elements in the list it is called on.
Packages:
import math
import numpy
from math import radians (Note: To import radians function from math package)
from scipy.linalg import inv as my_inv (Note: Suppose you want to use the function inv(), which is in the linalg subpackage of the scipy package)
Numpy: (Numeric Python)
Adv:
Powerful
Collection of values
Hold different types
Change add remove
Data Science
                Mathematical operation over collection
                Speed
NumPy array:
                Calculation over entire arrays
                Easy and fast for complex calculation
Syntax: Example
import numpy as np
np_array1 = np.array(array1)
Manipulation with np array = np_array1 + np_array2 (+, _ ,*, /, **)


Ex:
height = [180, 175, 165, 168, 189, 195]
weight = [74, 52, 42, 85, 96, 53]
import numpy as np
np_height = np.array(height)
print(np_height)
np_height_m = np_height * 0.0254
print(np_height_m)
np_height_m = np.array(height) * 0.0254
np_weight_kg = np.array(weight) * 0.453592
bmi = np_weight_kg / np_height_m ** 2
print(bmi[light])
2D – NumPy Array
N dimensional array
My_list = [[180, 78.4],           
           [215, 102.7],           
           [210, 98.5],           
           [188, 75.2]]
import numpy as np
np_ My_list = np.array(My_list)
print(type(np_My_list))
print(np_My_list.shape)
Output:
<class 'numpy.ndarray'>
(4, 2) (Shape will return total size of array (row, column))
               



Ex:
# Print out the 4th row of np_My_list
print(np_My_list [4,:])
# Select the entire second column of np_My_list
np_array1 np_My_list [:,1]
# Print out value of 3th player
print(np_baseball[2, 0])
Output:
[ 70 195]
75
Basic statistics:
Data Analysis:



Thursday, October 12, 2017

Tuple in Python


Tuple: like as container or array

Sample:
Tuple:
tup1 = ('Sangeeth', 'chennai','1309','gowtham',1990', '2000','0406');
tup2 = (1,2,3,4,5,6,7,8);
print  tup1[1]
print  tup2[3:5]

output:
chennai
3,4,5

String:
var1 = "sangeeth"
var2 = "gowtham"
print var1[0]

print var2[0:3]

output:
s
gow


Packing and Unpacking

Sample:

x = ("Sangeeth", chennai, "madurai")    # Tuple packing code
(me, where, too) = x    # Tuple unpacking Code
print me
print where
print to

Output
Sangeeth
chennai
madurai

Comparison Operator
A comparison operator in Python can work with tuples.It starts with comparing the first element from each of the tuples

Sample:
a=(3,8)
b=(2,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
    
a=(4,7)
b=(4,3)
if (a>b):print "a is bigger"
else: print "b is bigger"
    
a=(2,3)
b=(4,1)
if (a>b):print "a is bigger"
else: print "b is bigger"

Built In Function
all(), any(), enumerate(), max(), min(), sorted(), len(), tuple(), etc.


Python First Sample Code

Sample Code Python

Sample 1:
def main():
  print "Hello Folks!" 

print "Welcome to Python Programming"

Sample 1 - Output:
Welcome to Python Programming

Reason:
It is Important that after defining the main function, you call the code by
if__name__=="__main__"
and then run the code, only then you will get the output "hello Folks" in console

Sample 2:

def main():
  print "Hello Folks!"


if __name__== "__main__":
  main()


print "Welcome to Python Programming"

Sample 2 - Output:
Hello Folks!
Welcome to Python Programming

Python

Python is an object-oriented programming language created by Guido Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries and is extensible to C or C++. Many large companies use the Python programming language include NASA, Google..
Python is widely used in Artificial Intelligence, Natural Language Generation, Neural Networks and other advanced fields of Computer Science. Python had deep focus on code readability & this class will teach you python from basics.
Characteristics:

  • It provides rich data types and easier to read syntax than any other programming languages
  • It is a platform independent scripted language with full access to operating system API's
  • Compared to other programming languages, it allows more run-time flexibility
  • It includes the basic text manipulation facilities of Perl and Awk
  • A module in Python may have one or more classes and free functions
  • Libraries in Pythons are cross-platform compatible with Linux, MacIntosh, and Windows
  • For building large applications, Python can be compiled to byte-code
  • Python supports functional and structured programming as well as OOP
  • It supports interactive mode that allows interacting testing and debugging of snippets of code
  • In Python, since there is no compilation step, editing, debugging and testing is fast.

Uses

  • Programming video games
  • Artificial Intelligence algorithm
  • Programming various scientific programs such as statistical models

Skip Inventory reservation on Sales order lines in D365FO

[ExtensionOf(classStr(InventUpd_Reservation))] final class InventUpd_ReservationCls_SAN_Extension {     void updateNow()     {         if(mo...