What is Python
– Multiple paradigm language, similar to Perl but can be used for OOP and more readable
Can be compiled to many things, into DLL, C, Py ( different compilers for Python )
Why ? Quality : Focuses on readability (indentation), productivity
Example a = [1,2,3, 4,5, 6]
[ i**2 for i in a ] to print and array
Focuses on Productivity ( 20 lines in Java equates to 4 lines in python ), debugging, maintenance
Focus : Portability: Run on any machine, portable across multiple OS
Focus : Support Libraries , Open Source, Many Libraries for application,
Focus : Component Integration : Integrate with multiple components
About
– Netherlands
– Monty Python
– Interpreter ( as you type, it executes ), good for interactive data analysis
– Scalable, Object Oriented
– Drop Box ( Windows and Web ) , Ultraseek ( Search Engine), Zope Corporation, Yum,
– Traditional Use : Testing Automation, Embedded Scripting ( Router using Cpython Interpreter), AI, Image Processing, GUI ( Kivi ), Databases( NoSQL , Mongo DB, Couch DB, can work with JDBC and ODBC ), System Utilities ( System Admins), Internet Scripting
– Python in Data Analytics – Light, Mathematicians ( Weather Forecasting, Ad Targeting, Scientific Analysis, Risk Management Analysis)
New Uses are emerging – New Libraries Pydoop, Pandas, Scipy
Exmaples – Forecast Watch, Appnexus (ads), Altis (Investment), Los Almos
Pythons 1.0, to 3.6 latest, ( 2.7 is compatible )
WHY SUMMARY – Interepreted.– Interacive – Dynamic – Functional – OOP, Readable
PYTHON INSTALLATION
– Pre- installed on most Linux, UNix, ( version pre-installed is 2.x )
– Version Check ( python — version )
– Running python script >> python script.py
BASICS
– Getting Help : help(‘for’), help (‘import’), help(‘replace’) , a.replace?
– Getting Help : pydoc ( documentation module for python comes with python installation , search for python documentation or generate html for documentation purpose) : ubuntu prompt : $ pydoc for
– IDE ( Pycharm , )
– IDE (Jupyter QT console ) – you can get prompts for auto complete methods, example dir(a) live give all methods available for that object )
– Comments ( # Single, “””” Triple Quotes for multiple lines “”””)
– Literal Constants – Number or Strings is a Value
– Types of Numbers
– Integers ( Example 0, 1, 23232, -22 ) ,
Dividing Integer by Integer will give only integer part of the component , Implemented as C longs. Inorder to return a decimal convert one of the numbers to decimal example … 3/2 = 1 and 3/2.0 = 1.5
A number preceding with a 0 , example 015 is an OCTAL number ) so 5= 05 will return true , but 12 = 012 will not return true
– Long Intergers – Example 324342234324L , represented by an I or L at the end
– Floating point numbers = > examples 1.0 , 1.2e-2, 1.1e10, 8.99E4
Implemented as C doubles,
Returns a float if operations involve number and float ( general tendency is that the when calculation is done for 2 numbers, the most complex form datatype result is obtained )
– Octal, Hex constants ( Octal must start with 0 example 02, Hex must start with 0x examples 0x1ff )
– Complex numbers ( Example ( 1 + 3j ), 2.0 + 4.0j , always needs to end with a j or J
Python Identifiers
– Variables start with A – Z, a -z, 0 or _ , CASE SENSITIVE , CANNOT have special characters @, $, % within identifiers, NO DECLARATION of datatype is required.
– Naming convention ( Class names start with upper case and all other lower case ), STARTING WITH _ indicates the identifier to be PRIVATE, starting with 2 leading underscore indicates that the identifier is STRONGLY PRIVATE, starting with 2 underscores and ending with 2 ending underscores LANGUAGE DEFINED special name
–
Operations on Numbers
– Arithmetic operators : a+b, a-b, a/b, a*b
– Modulo : a%b
– Exponent : a**b
– Other elementary numbers not defined in python but in packages like NumPy and SciPy
– Order of operations is PEMDAS ( Parentheses, Exponents, Multiplication, Division, Addition, Subtraction )
Comparison Operators
– a < b , a > b , a >= b, a <= b
– Identity tests => a == b , a != b
Bitwise Operators
– OR ( a | b ), AND ( a & b ) , XOR ( a ^ b )
– Shift bits LEFT ( a << b ), RIGHT ( a >> b )
Operations on String
– Python does not support character, even a single letter is treated as a string,
– Separation of space will give you ( list of strings ) , example , a = ” This is a string ” , a[0] = This, a[1]= Space a[2] = is
– String are immutable
– Can be defined by a single or double quote , Double lets you expand the string across multiple lines
– String are added by + ‘abc’ + ‘abc’ = abcabc
– Strings are multiplied by ‘abc’ * 3 = ‘abcabcabc’
– Create a multi line string , use a \ at the end of the line of the string
– String Slicing, Indexing : a = ” This is a string” , a[:3] = “Thi” , a[-1]= “g”, a[2:4] = “is” , Index starts at 0, [i] get the i’th letter from the string
– Membership checking : in, not in operator ‘is’ in a ‘strings’ not in a
– Formatting Strings using %: d = ” This is %s, and she is %d years of age” %(‘Tom’, 18 )
– Functions
.capitalize ( Capitalize the first character ), count( # of times string occurs ), index ( similar to find(), first position of the found string ), index raises an error but find() does not
encoding and decoding : s.encode(encoding=”UTF-8″, errors=’strict’) , By default in python 3 encoding is UTF-8
max(), min(), replace(), upper()
Indentation
– Important in Python, used to identify group of statements, can be done by adding spaces or tabs
– its a MUST, as there are no braces
– Group of statements must be indented by the same amount, the standard indentation is 4 characters.