1 / 74

Introduction about Python by JanBask Training

Introduction about Python by JanBask Training, we are offering Online Pyton Training. You should visit: http://www.janbasktraining.com/python/ for Pyton Training.

Download Presentation

Introduction about Python by JanBask Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introductionto By: JanBaskTraining.com

  2. ContentsataGlance • What isPython? • History andTimeline • Python 2 and3 • Philosophy • KeyFeatures • Paradigms • Popularity • GettingStarted • IDLEIDE • FirstProgram • OtherIDEs • PythonBasics • Variables and DataTypes • Operators • Type Conversion • Syntax andStructures • Input /Output • Identifiers • lines • Block andIndentation • Quotations • Comments JanBask Training

  3. ContentsataGlance • Object-OrientedPython • Classes • Inheritance • GarbageCollection • BePythonic! • Summary • References • ControlFlow • CompositeTypes • Lists • Tuples • Ranges • Dictionaries • Functions • Definitionsand Calling • NestedFunctions • First-ClassObjects JanBask Training

  4. What isPython? • Python is a widely-used general purpose (both industry and academia) high-level Programminglanguage. • It combines the power of systems languages, such as C and Java, with the ease and rapid development of scripting languages, such asRuby. JanBask Training

  5. History andTimeline • Python Invented by Guido vanRossum • in1991 at CWI in theNetherlands. • Python reached version 1.0 in January 1994. The major new features included in this release were the functional programmingtools. VanRossum Born: 31 January 1956 (age58) JanBask Training

  6. History andTimeline • Python 1.0 - January1994 • Python 1.5 - December 31,1997 • Python 1.6 - September 5,2000 • Python 2.0 - October 16,2000 • Python 2.1 - April 17,2001 • Python 2.2 - December 21,2001 • • Python 2.3 - July 29,2003 • Python 2.4 - November 30,2004 • Python 2.5 - September 19,2006 • Python 2.6 - October 1,2008 • Python 2.7 - July 3,2010 • Python 3.0 - December 3,2008 • • Python 3.1 - June 27,2009 • Python 3.2 - February 20,2011 • Python 3.3 - September 29,2012 • Python 3.4 - March 16,2014 JanBask Training

  7. Python2and3 • Python 2.0 was released in 2000, with many newfeatures • added. • Python 3.0, adjusting several aspects of the core language, was released in2008. • Python 3.0 isbackwards-incompatible. • Codes written for Python 2.x may not work under3.x! • Python 2.x is legacy, Python 3.x is the present and future of thelanguage. JanBask Training

  8. LanguagePhilosophy • Beautiful is better thanugly • Explicit is better thanimplicit • Simple is better thancomplex • Complex is better thancomplicated • Flat is better thannested • Sparse is better thandense JanBask Training

  9. KeyFeatures • Simple andMinimalistic • Easy toLearn • High-levelLanguage • Portable • Interpreted • Embeddable • ExtensiveLibraries • Free, Open Source, … andFun! JanBask Training

  10. ProgrammingParadigms • Python is a multi paradigm programminglanguage. • Imperative • Functional • Object-Oriented • Aspect-Oriented • Logic (rule base) Programming (byextension) • •… 10 of75 JanBask Training

  11. Popularity • Top 10 Programming Languages. • IEEESpectrum’s • 2014Ranking. Infographic: BrandonPalacio http://spectrum.ieee.org [JanBaskTraining]

  12. GettingStarted • There are three different ways to startPython • InteractiveInterpreter • from Unix, Linux, DOS,etc. • Python shell begin with>>> • Script from theCommand-line • Installpython. • python [YourScriptFileName.py] • Integrated Development Environment(IDE) • You can run Python from a graphical user interface (GUI)environment. • All in One solution like IDLE in nextslide. [JanBaskTraining]

  13. GettingStarted • IDLEIDE • Download Pythonfrom • http://python.org • Installit. • Runit. [JanBaskTraining]

  14. PythonShell [JanBaskTraining]

  15. SimpleScriptwithOutput [JanBaskTraining]

  16. Other IDEs:PyDev • PyDev is aPython • IDE forEclipse • http://pydev.org/ [JanBaskTraining]

  17. OtherIDEs:VisualStudio [JanBaskTraining]

  18. PythonBasics:ObjectsandVariables • In python everything is anobject. • So a variable is anobject. • A variable is name given to a memory location to store value in the computer’s mainstorage. [JanBaskTraining]

  19. PythonBasics:ObjectsandVariables • Every object / Variable has threecomponents: • Identity • Object’s address in memory does not change once it has beencreated. • Type (orClass) • A set of values and the allowable operations on those values exist foreach type. • Type of type istype!!! • Value • To bind value to a variable using assignment operator ( = ), forexample: • • x =12345 20 of75 [JanBaskTraining]

  20. PythonBasics:ObjectsandVariables • Python is a dynamically typed language,so: • Use Late Binding (run timebinding). • No need to declare variable before binding avalue. • Any given variable can have its value altered at anytime! [JanBaskTraining]

  21. Primitive DataTypes • Python has some standard types that are used to define the operations possible on them and the storage method for each ofthem. [JanBaskTraining]

  22. Primitive DataTypes [JanBaskTraining]

  23. Operators • BasicOperators • • Arithmetic (+, - , *, /, %, //,**) • • Assignment (=, +=, -=, *=, /=, %=, //= ,**=) • • Comparison (<, >, <=, >=, ==,!=) • Logical (and, or,not) • Notes: • • + on strings does stringconcatenation • * on (string * int) repeatsstring [JanBaskTraining]

  24. Operators [JanBaskTraining]

  25. TypeConversion • Python has strong typing language (unlikeJavaScript) • We need to use type converterfunctions: [JanBaskTraining]

  26. SyntaxandSemantic:Simplicity • Python syntax is simple, simple,simple!!! • Full python grammar (BNF) is less than 120line! • There are less than 35 keywords inpython. [JanBaskTraining]

  27. SyntaxandSemantic:Powerfully Simple Swap inJava: Simple Swap inPython: [JanBaskTraining]

  28. Input andOutput Script tocalculate circle area: Output: [JanBaskTraining]

  29. Identifiers • A Python identifier is a name used to identify avariable, • function, class, module, or otherobject. • An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). • Python does not allow punctuation characters such as@, • $, and % withinidentifiers. • Python is a case sensitive programming language. Thus Var and var are two different identifiers inPython. 30 of75 [JanBaskTraining]

  30. Lines • Single-LineStatements • Statements in Python typically end with a newline. • Multi-LineStatements • character ‘\’ use to denote that the line shouldcontinue. [JanBaskTraining]

  31. Lines • Statements contained within the [ ], { }, or ( ) brackets do not need to use the line continuation character. Forexample: • line containing only whitespace, possibly with a comment,is known as a blank line, and Python totally ignoresit. [JanBaskTraining]

  32. Lines • Multiple Statements on a SingleLine • The semicolon ( ; ) allows multiple statements on the singleline. • Multiple Statement Groups calledSuites • Groups of individual statements making up a single codeblock are calledsuites. • Compound or complex statements, such as “if”, “while”, “def”, and “class”, are those which require a header line and asuite. [JanBaskTraining]

  33. Blocks andIndentations • Blocks begin with colon mark ( :) • Nested blocks areallowed. • Line Indentation use to determine blocksscope! • The number of spaces in the indentation is variable, but all statements within the block must be indented the sameamount • pass keyword use to fill empty or not implementation blocksbody. • pass ≡ donothing [JanBaskTraining]

  34. Quotations • Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends thestring. • The triple quotes can be used to span the string across multiplelines. [JanBaskTraining]

  35. Comments • A hash sign (#) that is not inside a string literal beginsa • comment. • All characters after the # and up to the physical line end are part of the comment, and the Python interpreter ignoresthem. [JanBaskTraining]

  36. Control Flow:Conditions • Like other languages, Python has if and elsestatements • Python’s “else-if” is spelledelif [JanBaskTraining]

  37. Control Flow:Conditions • Python has an easy to use if-syntax for setting the valueof • avariable! [JanBaskTraining]

  38. TruthandFalsityValueTesting • Any object can be tested for truth value, for use ina • condition. • False: • 1. None (≡𝑛𝑢𝑙𝑙) • False (Python >=2.2.1) • Zero of any numeric type, e.g., 0, 0.0,0j • Any empty sequence or dictionary, e.g., ‘ ‘, ( ), [ ], {} • True: • Everythingelse [JanBaskTraining]

  39. Control Flow:Loops • While loop • The while loop continues to execute the same body of codeuntil • the conditional statement is no longerTrue. • We can use break and continue inside theloops 40 of75 [JanBaskTraining]

  40. Control Flow:Loops • Forloop • The for loop in Python is much simpler that otherC-like • languages. • We can use range() function to produce a list ofnumbers. [JanBaskTraining]

  41. SequenceTypes • There are three basic sequencetypes: • lists • Tuples • rangeobjects • Additional sequence typesinclude: • Strings(str) • Binary Data (bytes, bytearray,memoryview) [JanBaskTraining]

  42. SequenceTypes • Sequence types are very liketogether. • All of them consist of iterablesobjects. • There are somedifference: • Lists are mutable,heterogeneous. • Tuple are immutable,heterogeneous. • Ranges are immutable,homogeneous. • String are immutable,homogeneous. • •Immutable≡𝐶𝑎𝑛′𝑡𝑢𝑠𝑒𝑎𝑠𝑙− 𝑣𝑎𝑙𝑢𝑒 [JanBaskTraining]

  43. Lists • Lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different datatype. • [] – an emptylist. • [6] – an one-elementlist. • [5, 1+2j, ’hello’] - a 3-element list(heterogeneous). • • [[1,2], [3,4], [5,6]] - a list oflists. [JanBaskTraining]

  44. Lists • Lists may be constructed in severalways: • Using a pair of square brackets to denote the emptylist: • • L =[] • Using square brackets, separating items withcommas: • L = [a] or L = [a, b,c] • Using a listcomprehension: • L = [x for x initerable] • Using the typeconstructor: • L =list() or L =list(iterable) [JanBaskTraining]

  45. listcomprehension • A compact way to process all or part of the elements ina • sequence and return a list with theresults. • result = ['{:#04x}'.format(x) for x in range(256) if x % 2 ==0] • generates a list of strings containing even hex numbers (0x..) in the range from 0 to255. • The if clause is optional. If omitted, all elements in range(256)are • processed. [JanBaskTraining]

  46. Tuples • A tuple is another sequence data type that is similar tothe • list. • A tuple consists of a number of values separated by commas. • The main differences between lists and tuplesare: • Lists are enclosed in brackets ( [ ] ), and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot beupdated. • Tuples can be thought of as read-onlylists. [JanBaskTraining]

  47. Tuples • Tuples may be constructed in a number ofways: • Using a pair of parentheses to denote the emptytuple: • • T =() • Using a trailing comma for a singletontuple: • • T= 1, or T =(1,) • Separating items withcommas: • a, b, c or (a, b,c) • Using the tuple()built-in: • T = tuple() or T =tuple(iterable) [JanBaskTraining]

  48. Ranges • The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in forloops. • The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed). [JanBaskTraining]

  49. Ranges 50 of75 [JanBaskTraining]

More Related