0% found this document useful (0 votes)
115 views

Introduction To Python:: Import

Python is a high-level general purpose programming language suitable for scripting and web applications due to its automatic compilation to bytecode. It has built-in high-level data types, control structures, multiple levels of organizational structure like functions and classes, and compiles source code to bytecode without a separate compile step. Popular Python web frameworks include Django, TurboGears, and Zope, which provide reusable components and services for web development.

Uploaded by

Sree Nivas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Introduction To Python:: Import

Python is a high-level general purpose programming language suitable for scripting and web applications due to its automatic compilation to bytecode. It has built-in high-level data types, control structures, multiple levels of organizational structure like functions and classes, and compiles source code to bytecode without a separate compile step. Popular Python web frameworks include Django, TurboGears, and Zope, which provide reusable components and services for web development.

Uploaded by

Sree Nivas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to python: Python is a high-level general purpose programming language.

Because code is automatically compiled to byte code and executed, Python is suitable for use as a scripting language, Web application implementation language, etc. Importance of python:

Built-in high level data types: strings, lists, dictionaries, etc. The usual control structures: if, elif, else, while, plus a powerful collection iterator (for). Multiple levels of organizational structure: functions, classes, modules, and pac ages. These assist in organizing code. !n e"cellent and large e"ample is the #ython standard li$rary. %ompile on the fly to $yte code -- &ource code is compiled to $yte code without a separate compile step. &ource code modules can also $e 'pre-compiled' to $yte code files.
Statements: Print: The print statement sends output to stdout .
EX: print obj O/p:obj

Import: (These are just like ur import in java )


The import statement makes a module and its contents available for use.
---- import test mport module test. !efer to x in test "ith #test.x#.

---- from test import x mport x from test. !efer to x in test "ith #x#. ----- from test import * mport all ob$ects from test. !efer to x in test "ith #x#. This form of import is usually not recommended.

FUNCTIONS:

(se def to define a function. )ere is a simple e"ample:


def test(ms ! "ount): for idx in ran e("ount):

print #$s $d# $ (ms ! idx) test(#Test %#! &)

%omments: (this is for the a$ove program )


!fter evaluation def creates a function o$*ect. %all the function using the parentheses function call notation, in this case 'test(#Test %#! &)'. !s with other #ython o$*ects, you can stuff a function o$*ect into other structures such as tuples, lists, and dictionaries. )ere is an e"ample:
% 'reate a tuple: val ( (test! #) label:#! *) % 'all the fun"tion: val+,-(val+.-! val+/-)

2.5.1.2 A function with default arguments


#roviding default arguments allows the caller to omit some arguments. )ere is an e"ample:
def test0efault)r s(ar .(#default.#! ar /(#default/#): print #ar .:#! ar . print #ar /:#! ar / test0efault)r s(#Expli"it value#)

The a$ove e"ample prints:


ar .: Expli"it value ar /: default/ 1234T 35 625'T3O57: Function: 3n pro rammin a fun"tion is a portion of "ode 8ith in a lar er pro ram 8hi"h performs a spe"ifi" task 8hi"h is relativel9 independent of 8ritin "ode. :rint(;hello<) 3t prints the text. o/p: hello abs

x )

+eturn the a$solute value of a num$er. The argument may $e a plain or long integer or a floating point num$er. ,f the argument is a comple" num$er, its magnitude is returned.
cmp

x, y)

%ompare the two o$*ects x and y and return an integer according to the outcome. The return value is negative if x = y, zero if x (( y and strictly positive if x > y. float(-x. ) %onvert a string or a num$er to floating point. list -sequence.) ( +eturn a list whose items are the same and in the same order as sequence/s items. 0or instance, list(#ab"#) returns +#a#! #b#! #"#- and list( (.! /! ?) ) returns +.! /! ?-. ,f no argument is given, returns a new empty list, +-. raw_input(-prompt.) ,f the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. round(x-, n.) +eturn the floating point value x rounded to n digits after the decimal point. ,f n is omitted, it defaults to zero. The result is a floating point num$er. 1alues are rounded to the closest multiple of 23 to the power minus n4 if two multiples are e5ually close, rounding is done away from 3 (so. for e"ample, round(,.*) is .., and round(-,.*) is -..,). len s) ( +eturn the length (the num$er of items) of an o$*ect.

try-except and raise -- Exceptions


(se a tr9:ex"ept: $loc to catch an e"ception. (se raise to raise an e"ception. %omments and hints:

%atch all exceptions "ith a #bare# ex"ept:. &or example:


>>> tr9: ... x ( 9 ... ex"ept: ... print #9 not defined# ... 9 not defined

6ote, however, that it is usually $etter to catch specific e"ceptions.

%atch a specific error by refering to an exception class in the ex"ept:. To determine "hat error or exception you "ant to catch, generate it and try it. Because Python reports errors "ith a "alk-back that ends "ith reporting the exception, you can learn "hich exception to catch. &or example, suppose "ant to learn "hich exception is thro"n "hen a Python can't open a file. can try the follo"ing from the interactive prompt:
5ote that multiple ex"eptions to be "au ht b9 a sin le ex"ept: "lause are in parentheses@ the9 are a tuple. (ou can customi)e your error handling still further by passing an ob$ect on the raise and catching that ob$ect in the ex"ept:. By doing so, you can pass information up from the raise statement to an exception handler. *ne "ay of doing this is to pass an ob$ect.

+ reasonable strategy is to define a sub-class of a standard exception. &or example:

f you catch an exception using tr9:ex"ept:, but then find that you do not "ant to handle the exception at that location, you can #re-raise# the same exception ,"ith the same arguments- by using raise "ith no arguments.

Frameworks
The process of creating code to run we$ sites involves writing code to provide various services. The code to provide a particular service often wor s the same way regardless of the comple"ity or purpose of the we$ site in 5uestion. !$stracting these common solutions into reusa$le code produces what are called 7framewor s8 for we$ development. #erhaps the most well- nown framewor for we$ development is +u$y on +ails, $ut #ython has its own framewor s. &ome of these were partly inspired $y +ails, or $orrowed ideas from +ails, $ut many e"isted a long time $efore +ails. 9riginally #ython we$ framewor s tended to incorporate all of the services needed to develop we$ sites as a giant, integrated set of tools. 6o two we$ framewor s were interopera$le: a program developed for one could not $e deployed on a different one without considera$le reengineering wor . This led to the development of 7minimalist8 we$ framewor s that provided *ust the tools to communicate $etween the #ython code and the http protocol, with all other services to $e added on top via separate components. &ome ad hoc standards were developed that allowed for limited interopera$ility $etween framewor s, such as a standard that allowed different template engines to $e used interchangea$ly. &ince the advent of :&;,, the #ython we$ framewor world has $een evolving toward interopera$ility $ased on the :&;, standard. 6ow many we$ framewor s, whether 7full stac 8 (providing all the tools one needs to deploy the most comple" we$ sites) or minimalist, or anything in $etween, are $uilt from collections of reusa$le components that can $e used with more than one framewor . The ma*ority of users will pro$a$ly want to select a 7full stac 8 framewor that has an active community. These framewor s tend to $e well documented, and provide the easiest path to producing a fully functional we$ site in minimal time.

Some notable frameworks

There are an incredi$le num$er of framewor s, so they cannot all $e covered here. ,nstead we will $riefly touch on some of the most popular.
Django

<*ango is a framewor consisting of several tightly coupled elements which were written from scratch and wor together very well. ,t includes an 9+M which is 5uite powerful while $eing simple to use, and has a great online administration interface which ma es it possi$le to edit the data in the data$ase with a $rowser. The template engine is te"t-$ased and is designed to $e usa$le for page designers who cannot write #ython. ,t supports template inheritance and filters (which wor li e (ni" pipes). <*ango has many handy features $undled, such as creation of +&& feeds or generic views, which ma e it possi$le to create we$ sites almost without writing any #ython code. ,t has a $ig, international community, the mem$ers of which have created many we$ sites. There are also a lot of add-on pro*ects which e"tend <*ango=s normal functionality. This is partly due to <*ango=s well written online documentation and the <*ango $oo . 6ote !lthough <*ango is an M1%-style framewor , it names the elements differently, which is descri$ed in the <*ango 0!>.
TurboGears

!nother popular we$ framewor for #ython is Tur$o;ears. Tur$o;ears ta es the approach of using already e"isting components and com$ining them with glue code to create a seamless e"perience. Tur$o;ears gives the user fle"i$ility in choosing components. 0or e"ample the 9+M and template engine can $e changed to use pac ages different from those used $y default. The documentation can $e found in the Tur$o;ears wi i, where lin s to screencasts can $e found. Tur$o;ears has also an active user community which can respond to most related 5uestions. There is also a Tur$o;ears $oo pu$lished, which is a good starting point. The newest version of Tur$o;ears, version ?.3, moves even further in direction of :&;, support and a component-$ased architecture. Tur$o;ears ? is $ased on the :&;, stac of another popular component-$ased we$ framewor , #ylons.
Zope

The @ope framewor is one of the 7old original8 framewor s. ,ts current incarnation in @ope? is a tightly integrated full-stac framewor . 9ne of its most interesting feature is its tight integration with a powerful o$*ect data$ase called the @9<B (@ope 9$*ect <ata$ase). Because of its highly integrated nature, @ope wound up in a somewhat isolated ecosystem: code written for @ope wasn=t very usa$le outside of @ope, and vice-versa. To solve this pro$lem the @ope A effort was started. @ope A re-engineers @ope as a set of more cleanly isolated components. This

effort was started $efore the advent of the :&;, standard, $ut there is :&;, support for @ope A from the +epoze pro*ect. @ope components have many years of production use $ehind them, and the @ope A pro*ect gives access to these components to the wider #ython community. There is even a separate framewor $ased on the @ope components: ;ro . @ope is also the infrastructure used $y the #lone content management system, one of the most powerful and popular content management systems availa$le.
Other notable frameworks

9f course these are not the only framewor s that are availa$le. There are many other framewor s worth mentioning. !nother framewor that=s already $een mentioned is #ylons. #ylons is much li e Tur$o;ears, $ut with an even stronger emphasis on fle"i$ility, which comes at the cost of $eing more difficult to use. 6early every component can $e e"changed, which ma es it necessary to use the documentation of every single component, of which there are many. #ylons $uilds upon #aste, an e"tensive set of tools which are handy for :&;,.

Modules
,f you 5uit from the #ython interpreter and enter it again, the definitions you have made (functions and varia$les) are lost. Therefore, if you want to write a somewhat longer program, you are $etter off using a te"t editor to prepare the input for the interpreter and running it with that file as input instead. This is nown as creating a script. !s your program gets longer, you may want to split it into several files for easier maintenance. Bou may also want to use a handy function that you=ve written in several programs without copying its definition into each program. To support this, #ython has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. &uch a file is called a module4 definitions from a module can $e imported into other modules or into the main module (the collection of varia$les that you have access to in a script e"ecuted at the top level and in calculator mode). ! module is a file containing #ython definitions and statements. The file name is the module name with the suffi" .py appended. :ithin a module, the module=s name (as a string) is availa$le as the value of the glo$al varia$le CCnameCC. 0or instance, use your favorite te"t editor to create a file called fi$o.py in the current directory with the following contents:
% 6ibona""i numbers module def fib(n): % 8rite 6ibona""i series up to n a! b ( ,! . 8hile b = n: print b! a! b ( b! aAb

def fib/(n): % return 6ibona""i series up to n result ( +a! b ( ,! . 8hile b = n: result.append(b) a! b ( b! aAb return result

6ow enter the #ython interpreter and import this module with the following command:
>>> import fibo

This does not enter the names of the functions defined in fibo directly in the current sym$ol ta$le4 it only enters the module name fibo there. (sing the module name you can access the functions:
>>> fibo.fib(.,,,) . . / ? * B .? /. ?& ** BC .&& /?? ?DD E., CBD >>> fibo.fib/(.,,) +.! .! /! ?! *! B! .?! /.! ?&! **! BC>>> fibo.FFnameFF #fibo#

DDD 0or methods it is *ust as the *ava or oop concepts DDDD

Everything here is related to the concepts of 99# . Because this is a related to 99#& concept. ,t is a high level language .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy