Python tutorial - Introduction to Python

Download Python tutorial - Introduction to Python

Post on 15-Oct-2015




6 download

Embed Size (px)


A presentation covering introduction to Python language.


<p>Python for beginners Session for B.Tech COE-V Sem. 2013-14 </p> <p>Python for beginnersSession for B.Tech COE-V Sem.2013-14</p> <p>Part IIntroduction to Python. A Theoretical overview of evolution.Part IIThe technical aspects and coding standards. Learning Python.Part IIIAdvanced features of Python.Part IVExpectations from target audience.</p> <p>Session Roadmap -Need for Python.History of Python definitionCharacteristics of the languageFeaturesUsesComparative study with similar languagesPython and the coming future.</p> <p>Part INeed of a language that was easy to read and use.At the Same time also fulfilling the requirement of a language to be object oriented.Need of an highly extensible language that can be embedded in to existing applications.Need of an alternative to traditional available languages, so that new opportunities can be explored.</p> <p>WHY PYTHON WAS NEEDED?HISTORY OF PYTHONPython was developed by Guido Van Rossum in the late 80s &amp; 90s at national research institute for mathematics and computer science in netherlands.</p> <p>Guido van Rossum Developed as a successor to the ABC language and was capable of exception handling and integrate with Amoeba operating system. Python is a high-level, interpreted, interactive and object-oriented scripting language. Python was designed to be highly readable which uses English keywords frequently where as other languages use punctuation and it has fewer syntactical constructions than other languages.</p> <p>WHAT IS PYTHON ?Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. This is similar to PERL and PHP.Python is Interactive: This means that you can actually sit at a Python prompt and interact with the interpreter directly to write your programs.Python is Object-Oriented: This means that Python supports Object-Oriented style or technique of programming that encapsulates code within objects.Python is Beginner's Language: Python is a great language for the beginner programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.</p> <p>Characteristics of Python :Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages.</p> <p>Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL).</p> <p>Python is now maintained by a core development team at the institute, although Guido van Rossum still holds a vital role in directing its progress.</p> <p>It is Dynamically typed, object-oriented Language.</p> <p>Runs on Win, Linux/Unix, Mac, OS/2 etc.</p> <p>Versions: 2.xand 3.x</p> <p>Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language in a relatively short period of time.</p> <p>Easy-to-read: Python code is much more clearly defined and visible to the eyes.</p> <p>Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain.</p> <p>A broad standard library: One of Python's greatest strengths is the bulk of the library is very portable and cross-platform compatible on UNIX, Windows and Macintosh.</p> <p>Python featuresInteractive Mode: Support for an interactive mode in which you can enter results from a terminal right to the language, allowing interactive testing and debugging of snippets of code.</p> <p>Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms.</p> <p>Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.</p> <p>Databases: Python provides interfaces to all major commercial databases.</p> <p>GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh and the X Window system of Unix.</p> <p>Scalable: Python provides a better structure and support for large programs than shell scripting.</p> <p>Apart from the above-mentioned features, Python has a big list of good features, few are listed below:Support for functional and structured programming methods as well as OOP.It can be used as a scripting language or can be compiled to byte-code for building large applications.Very high-level dynamic data types and supports dynamic type checking.Supports automatic garbage collection.It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java.</p> <p>ScriptingRapid PrototypingText ProcessingWeb applicationsGUI programsGame DevelopmentDatabase ApplicationsSystem AdministrationsAnd many more.</p> <p>What can Python do?</p> <p>Rapid prototyping is a group of techniques used to quickly fabricate a scale model of a physical part or assembly using three-dimensional computer aided design (CAD) data.Construction of the part or assembly is usually done using 3D printing or "additive layer manufacturing" technology.Rapid prototypingIn computing, the term text processing refers to the discipline of mechanizing the creation or manipulation of electronic text. Text usually refers to all the alphanumeric characters specified on the keyboard of the person performing the mechanization, but in general text here means the abstraction layer that is one layer above the standard character encoding of the target text. The term processing refers to automated (or mechanized) processing, as opposed to the same manipulation done manually.Text processingIn computing, a web application is any application that uses a web browser as a client.[1][2] The term may also mean a computer software application that is coded in a browser-supported programming language (such as JavaScript, combined with a browser-rendered markup language like HTML) and reliant on a common web browser to render the application executable.Web applicationIn computing, graphical user interface is a type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation, as opposed to text-based interfaces, typed command labels or text navigation. GUIs were introduced in reaction to the perceived steep learning curve of command-line interfaces (CLI), which require commands to be typed on the keyboard.</p> <p>guiGame development is the process of creating a game. Development is undertaken by a game developer, which may range from a single person to a large business. Traditional commercial PC and console games are normally funded by a publisher and take several years to develop. Indie games can take less time and can be produced cheaply by individuals and small developers. The indie game industry has seen a rise in recent years with the growth of new online distribution systems and the mobile game market.Game developmentA database application is a computer program whose primary purpose is entering and retrieving information from a computer-managed database. Early examples of database applications were accounting systems and airline reservations systems, such as SABRE, developed starting in 1957.Database applicationsA system administrator, or sysadmin, is a person who is responsible for the upkeep, configuration, and reliable operation of computer systems; especially multi-user computers, such as servers. He seeks to ensure that the uptime, performance, resources, and security of the computers he or she manages meet the needs of the users, without exceeding the budget.</p> <p>system administrationAreaPerlRubyPythonReadabilityBadGoodGoodObject OrientedNoYesYesDebuggingGoodBadGoodComputation speedGoodBadOkComplexityLowHighHighStandardizedNoYesNoFail Safe ExceptionsNoYesYesPython vs. rest of the world -Program execution slower than java but are 3-5 times shorter than Java and 5-10 shorter than C++ and thus take less time to develop.</p> <p>Python vs Java &amp; C++Python vs JavascriptNearly equivalent but supports writing much larger programs and better code reuse through a true object-oriented programming style, where classes and inheritance play an important role. </p> <p>Python and futurePython is easy for analysts to learn and use, but powerful enough to tackle even the most difficult problems in virtually any domain. It integrates well with existing IT infrastructure, and is platform independent. Among modern languages, its agility and the productivity of Python-based solutions is legendary. Companies of all sizes and in all areas - from the biggest investment banks to the smallest social/mobile web app startups - are using Python to run their business and manage their data.Python for Big data;utm_medium=cpc&amp;utm_campaign=366_IN_Otros&amp;utm_content=Python&amp;utm_term=download%20python</p> <p>Source materialPart II - RoadMapInstallation ProcedureProgramming Modes and other rulesData TypesOperatorsConditional HandlingIteration handlingMathematical FunctionsString FunctionsList and Dictionary functionsUser defined functionsThe current production versions are Python 2.7.6 and Python 3.3.3. Download it from official website.This site hosts the "traditional" implementation of Python (nicknamed CPython). A number of alternative implementations are available as well, namelyIronPython (Python running on .NET)Jython (Python running on the Java Virtual Machine)PyPy (A fast python implementation with a JIT compiler)Stackless Python (Branch of CPython supporting microthreads)Other parties have re-packaged CPython. These re-packagings often include more libraries or are specialized for a particular application.</p> <p>Installation ProcedureINTERACTIVE MODE PROGRAMMING</p> <p>Interactive mode means invoking the interpreter without passing a script file as a parameter.</p> <p>Type print command on the right side of the prompt : Output will be :</p> <p>Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished.Write your python program in a script. Save the file with .py extension.</p> <p>SCRIPT MODE PROGRAMMING</p> <p>Now on cmd, change the path to your script location using cd command.Then write command : py test.pyOutput will be :PYTHON IDENTIFIERSName used to identify a variable, function, class, or any other object. Starts with a letter or an underscore(_), followed by zero or more letters, underscores or digits. Python is a case sensitive language. So identifiers a and A are considered to be two different identifiers. Special characters like @, $, % are not allowed within identifiers. Reserved keywords cannot be used as identifiers. List of reserved words in python : </p> <p>No braces to indicate blocks of code. Blocks of code are denoted by line indentation. All the statements within one block must be indented the same amount of spaces. For example, following is a correct code :</p> <p>LINES AND INDENTATION</p> <p>MULTILINE STATEMENTS Use continuation character (\) to indicate that the line should continue. For example :</p> <p>Do not have to be explicitly declared.Declaration happens automatically while assigning value to a variable. For example : VARIABLE</p> <p>Possible to assign a single value to multiple variables. For example :</p> <p>Five standard data types :NumberStringListTupleDictionaryNumber :Support four different numerical types :Int (Signed integers)Long (Long integers)Float (Floating point values)Complex (Complex number)For Example :</p> <p>STANDARD DATA TYPES</p> <p>Strings :Subsets of strings can be taken using slice operator ([ ] or [ : ]).Plus (+) sign is used as concatenation operator.Asterisk (*) sign is used as repetition operator.For Example :</p> <p>Lists : Compound data type. Contains items of same or different data types. Plus (+) is used as concatenation operator. Asterisk (*) is used as repetition operator.For example :</p> <p>Tuples : Similar to lists. Elements are enclosed within parentheses. Can be considered as read-only list. Elements cannot be updated and size cannot be modified.</p> <p>Dictionary : Hash table type. Consists of key-value pairs. Enclosed by curly braces ({ }).For example :</p> <p>DATA TYPE CONVERSIONBuilt-in functions to perform type conversion.</p> <p>OPERATORSArithmetic Operators :</p> <p>Comparison Operators :</p> <p> Assignment Operators :</p> <p> Bitwise Operators :</p> <p> Logical Operators :</p> <p> Membership Operators : </p> <p> Operator Precedence :Following table represents operators from highest precedence to lowest.An example of operators :</p> <p>DECISION MAKINGDecision making statements supported by Python :</p> <p>An example : </p> <p>LOOPS Loops supported by Python :</p> <p>Loop Control Statements Used to change normal execution sequence. Control statements supported by Python : </p> <p>MATHEMATICAL FUNCTIONS</p> <p>TRIGONOMETRIC FUNCTIONS</p> <p>MATHEMATICAL CONSTANTS</p> <p>For Example :STRING SPECIAL OPERATORS</p> <p>BUILT-IN STRING METHODS</p> <p>Example :BUILT-IN LIST FUNCTIONS</p> <p>Example :</p> <p>BUILT-IN DICTIONARY METHODS</p> <p>Example :</p> <p>FUNCTIONS Block of organized, reusable code. Block begins with keyword def followed by function name and parentheses (). Code block within every function starts with a colon (:) and is indented. Return statement is used to exit from a function. Arguments are passed by reference in Python. Keyword arguments allow to pass parameters out of order. Example :</p> <p> Default parameters are also supported. Example :</p> <p>Part III The Tunnel aheadPython modules and namespacesUser Inputs and OutputsFile I/OPartial FunctionsException HandlingObject Oriented approach and Python</p> <p>A Python module is as simple as a function kept in file for purpose of reuse.Modules are necessary to handle larger applications.Create functions in a .py file and save it.Using Modules-import module_name.module_function();from import *module_function();Python ModulesThis module represents the (otherwise anonymous) scope in which the interpreters main program executes commands read either from standard input, from a script file, or from an interactive prompt.When a program is executed directly _name_ is set to _main_Executing the python file directly means the main() command will be searched for.if __name__ == "__main__": main()One of the reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. </p> <p>Special Variables : _name_Provided for efficiency or to provide access to operating system primitives such as system calls.sys module sys.path - The variable sys.path is a list of strings that determines the interpreters search path for modules.dir() function - returns a sorted list of strings of variables, modules, functions inside a module.Standard ModulesWhats in a name?A name in Python is roughly analogous to a variable in jus...</p>