P.VIJAYABABU [email protected] 2017 Python Programming
Total Page:16
File Type:pdf, Size:1020Kb
P.VIJAYABABU [email protected] 2017 Python Programming Language BY P.VIJAYABABU PH NO: 9985333934 EMAIL: [email protected] Python Programming Language UNIT -1 History of Python: The name Python was selected from "Monty Python’s Flying Circus" which was a British sketch comedy series created by the comedy group Monty Python and Broad cast by the BBC from 19 9 to 19!"# Python was created in the early 199$s by Guido &an 'ossum at the (ational 'esearch )nstitute for Mathematics and Computer *cience in (etherlands# Python was created as a successor of a language called +BC ,+ll Basic Code) and released publicly in1991# Guido remains Python’s principal author. although it includes many contributions from active user community# Between 1991 and /$$1 there are se&eral &ersions released. current stable release is 0#/# )n /$$1 the Python *oftware Foundation (P*F- was formed. a non-pro2t organization created specifically to own Python-related )ntellectual Property# 4ope Corporation is a sponsoring member of the P*F#+ll most all Python releases are Open *ource# What is Python: Python is an open source. object-oriented. high-le&el powerful programming Language# De&eloped by Guido &an 'ossum in the early 199$s# (amed after Monty Python Python runs on many 9(): &ariants. on the Mac. and on ;indows /$$$ and Later# +&ailable for download from http<//www#python#org# Python programs are composed of modules Modules contain statements *tatements contain e>pressions ?>pressions create and process objects which means it can be used to build Features of Python: Open source < Python is publicly a&ailable open source software. any one can use source code that doesn@t’t cost anything# Easy-to-learn < Popular (scripting/e>tension) language. clear and easy synta>. no type declarations. automatic memory management. high-le&el data types and operations. design to read (more ?nglish like synta>) and write (shorter code compared to C. CAA.and Ja&a- fast# High-level Language< High-le&el language (closer to human) refers to the higher le&el of Concept from machine language (for e>ample assembly languages-# Python is an e>ample of a high-le&el language like C. CAA. Perl, and Ja&a with low1le&el optimization# Portable< High le&el languages are portable. which means they are able to run across all major hardware and software platforms with few or no change in VIJAY Page 2 Python Programming Language source code# Python is portable and can be used on Linu>. ;indows. Macintosh. *olaris. FreeB*8. 5*=/. +miga. +'5*. +*="$$ and many more# Object-Oriented < Python is a full-featured object-oriented programming language. with features such as classes. inheritance. objects. and o&erloading# Python is Interactive <Python has an interactive console where you get a Python prompt (command line) and interact with the interpreter directly to write and test your programs# This is useful for mathematical programming# Interpreted < Python programs are interpreted. takes source code as input. and then compiles (to portable byte1code- each statement and e>ecutes it immediately# (o need to compiling or linking Extendable < Python is often referred to as a “glue” language. meaning that it is capable to work in mixed-language en&ironment# The Python interpreter is easily e>tended and can add a new built-in function or modules written in C/C++/Ja&a code# Libraries < Databases. web ser&ices. networking. numerical packages. graphical user interfaces. 0D graphics. others# Supports <*upport from online Python community# Need of Python programming: Python is a general-purpose language. which means it can be used to build just about anything. which will be made easy with the right tools/libraries# Professionally. Python is great for backend web de&elopment. data analysis. artificial intelligence. and scientific computing# Many de&elopers ha&e also used Python to build productivity tools. games. and desktop apps. so there are plenty of resources to help you learn how to do those as well # Major uses of Python: *ystem utilities (system admin tools. command line programs-# ;eb De&elopment# Graphical 9ser )nterfaces (Tkinter. gtk. Dt-# )nternet scripting# ?mbedded scripting# Database access and programming# Game programming# 'apid prototyping and de&elopment# Distributed programming Applications of Python: Web Development: Eahoo Maps. Eahoo Groups. %oogle. 4ope Corporation. 9ltra seek. Linu> ;eekly (ews. ?lastic Hosts Cloud *er&ers. Mojam#com. hunch. *hop3illa. Mo&ieplayer#it. and Multiplayer# )t# Games: Battle2eld /. Crystal *pace. *tar Trek Bridge Commander. The Temple of ?lemental ?&il, Fampire< The MasGuerade< Bloodlines. Civilization ". Du+rH ,Duake +rmy Hnife- VIJAY Page 3 Python Programming Language Graphics: )ndustrial Light & Magic. ;alt Disney Feature +nimation. CH*. )nc# ,+B+D9*/C+?-. 'oboFog. Caligari Corporation. Blender 08. Basc *oftware. Paint *hop Pro# Financial: +ltos )n&estment Management. +B( +M'5 Bank. Treasury *ystems. Belloc Credit 9nion. Journy> Timesheet and 'esource Management *oftware# Science: (ational ;eather *er&ice. 'adar 'emote *ensing Group. +pplied Maths. Biosoft. The (ational 'esearch Council of Canada. 7os +lamos (ational Laboratory ,7+(7- Theoretical Physics Division. +lphaGene. )nc#. 77(7. (+*+. *wedish Meteorological and Hydrological )nstitute ,*MC)-. ?n&ironmental *ystems 'esearch )nstitute ,?*')-. Obje>> ?ngineering. (mag Computational Micromagnetics ?lectronic Design +utomation < Cirano&a. Productivity Design Tools. Object 8omain. Pardus. 'ed Hat. *%). )nc#. MC) ;orldcom. (okia. Education < 9niversity of California. )r&ine. *meal College of Business. The Pennsyl&ania *tate 9niversity. (ew 4ealand Digital Library. )T Certification ?>am preparation. *choolTool, Business Software < 'a&en Bear *ystems Corporation. Thawte Consulting. +d&anced Management *olutions )nc#. )BM. +raknJ?9K. 'ealNetworks. d*P+C?. ?scom. TheTiny Company. (e>edi, Piensa Technologies - Bufete Consultor de Me>ico. (ektra. ;uBook# Role of Interpreter of Python: The word "interpreter" can be used in a &ariety of different ways when discussing Python# *ometimes interpreter refers to the Python '?PL (Read– Eval–Print Loop). the interactive prompt you get by typing python at the command line# *ometimes people use "the Python interpreter" more or less interchangeably with "Python" to talk about e>ecuting Python code from start to 2nish# There are four steps that python takes when you hit return< le>ing. parsing. compiling. and interpreting# Le>ing is breaking the line of code you just typed into tokens# The parser takes those tokens and generates a structure that shows their relationship to each other (in this case. an +bstract *ynta> Tree-# The compiler then takes the +*T and turns it into one ,or more- code objects# Finally. the interpreter takes each code object e>ecutes the code it represents# Basics of Python Programming Using the REPL(Shell): + 'ead1?&al-Print 7oop ,'?P7- is a simple. interacti&e computer programming en&ironment# The term @'?P7@ is usually used to refer to a 7)*P interacti&e en&ironment but can be applied to command line shells and similar en&ironments for programming languages like Python. 'uby etc# VIJAY Page 4 Python Programming Language Python IDLE: Interactive Mode Let us assume that we@&e already installed Python (here we installed Python 0#/ on a standard pc with windows ! 5*-# Click on start button and 2nd Python 0#/ tab in installed programs# (ow clicking on Python 0#/ tab you can see the Python program de&elopment tool named )87? (Python %9)-# To start )87? click on )87? (Python %9)) icon. you will see a new window opens up and the cursor is waiting beside @KKK@ sign which is called command prompt# This mode is called interacti&e mode as you can interact with )87? directly. you type something (single unit in a programming language- and press enter key Python will e>ecute it. but you cannot e>ecute your entire program here# +t the command prompt type copyright and press enter key Python e>ecutes the copyright information# (ow Python is ready to read new command# Let@s e>ecute the following commands one by one# Command -1 < print("Hello ;orld"- Command 1/ < primt("Hello ;orld"- The 2rst command is correct and but the second one has a synta> error. here is the response from Python# Python line structure: + Python program is divided into a number of logical lines and e&ery logical line is terminated by the token (?;7)(?# + logical line is created from one or more physical lines# + line contains only spaces. tabs. formfeeds possibly a comment. is known as a blank line. and Python interpreter ignores it# + physical line is a seGuence of characters terminated by an end-of-line seGuence (in windows it is called C' LF or return followed by a linefeed and in 9ni>. it is called LF or linefeed-# *ee the following e>ample# Comments in Python: + comment begins with a hash character,M- which is not a part of the string literal and ends at the end of the physical line# +ll characters after the M character up to the end of the line are part of the comment and the Python interpreter ignores them# *ee the following e>ample# )t should be noted that Python has no multi- lines or block comments facility# Joining two lines: ;hen you want to write a long code in a single line you can break the logical line in two or more physical lines using backslash character,N-# Therefore when a physical line ends with a backslash characters,N- and not a part of a string literal or comment then it can 6oin another physical line# *ee the following e>ample# VIJAY Page 5 Python Programming Language Multiple statements on a single line: Eou can write two separate statements into a single line using a semicolon ,O) character between two line# Running Python Scripts Python IDLE: Development mode +t 2rst. start with a new window# Clicking on "(ew window" under 2le menu a new window will come# Type print "Hello ;orld" in the new window# Let@s sa&e ,*a&e command is located under the File menu) the 2le now# ;e sa&e the Program as helloworld#py under ?</python1programs folder# To run the program select 'un menu# Python Variables: + &ariable is a memory location where a programmer can store a &alue# ?>ample < roll_no.