what is python programming language? python is a general-purpose interpreted, interactive,...

31
What is Python What is Python Programming Language? Programming Language? Python is a general-purpose Python is a general-purpose interpreted, interactive, object- interpreted, interactive, object- oriented and high-level programming oriented and high-level programming language. language. It is defined as It is defined as object-oriented scripting object-oriented scripting language a definition that blends language a definition that blends support for OOPs with an over all support for OOPs with an over all orientation towards scripting orientation towards scripting roles. roles.

Upload: lee-porter

Post on 29-Jan-2016

309 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

What is Python Programming What is Python Programming Language?Language?

Python is a general-purpose interpreted, Python is a general-purpose interpreted, interactive, object-oriented and high-level interactive, object-oriented and high-level programming language. programming language.

It is defined as It is defined as object-oriented scripting object-oriented scripting language a definition that blends support for language a definition that blends support for OOPs with an over all orientation towards OOPs with an over all orientation towards scripting roles.scripting roles.

Page 2: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

What does Scripting Language mean?What does Scripting Language mean?A scripting language is a programming language

designed for integrating and communicating with other programming languages. Some of the most widely used scripting languages are JavaScript, VBScript, PHP, Perl, Python, Ruby, ASP and Tcl

Scripting language is normally used in conjunction with another programming language, like HTML or Java, C++.

Page 3: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Features of PythonFeatures of Python PythonPython is a general-purpose high-level programming language is a general-purpose high-level programming language

whose design philosophy emphasizes code readability.whose design philosophy emphasizes code readability.

PythonPython supports multiple programming paradigms, primarily but supports multiple programming paradigms, primarily but

not limited to object oriented, imperative and, to a lesser extent, not limited to object oriented, imperative and, to a lesser extent,

functional programming styles.functional programming styles.

PythonPython aims to combine "remarkable power with very clear aims to combine "remarkable power with very clear

syntax", and its standard library is large and comprehensive.syntax", and its standard library is large and comprehensive.

Its use of indentation for block delimiters is unusual among Its use of indentation for block delimiters is unusual among

popular programming languages.popular programming languages.

Page 4: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Introduction to PythonIntroduction to Python

Development of PythonDevelopment of PythonPythonPython was developed in the late 1980s and its was developed in the late 1980s and its

implementation was started in December 1989 by Guido implementation was started in December 1989 by Guido

van Rossum at CWI (Centrum Wiskunde & Informatica) van Rossum at CWI (Centrum Wiskunde & Informatica)

in the Netherlands.in the Netherlands.

Page 5: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Introduction to PythonIntroduction to Python

Python 2.0Python 2.0 was released on 16 October 2000, with many was released on 16 October 2000, with many

major new features including a full garbage collector and major new features including a full garbage collector and

support for Unicode.support for Unicode.

Python 3.0Python 3.0, a major, backwards-incompatible release with , a major, backwards-incompatible release with

Python 2.xPython 2.x, was released on 3 December 2008 after a long , was released on 3 December 2008 after a long

period of testing.period of testing.

Many of its major features have been backported to the Many of its major features have been backported to the

backwards-compatible backwards-compatible Python 2.7Python 2.7..

Page 6: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

4 Major Versions of Python4 Major Versions of Python

“Python” or “CPython” is written in C/C++

- Version 2.7 came out in mid-2010

- Version 3.1.2 came out in early 2010

“Jython” is written in Java for the JVM“IronPython” is written in C# for the .Net

environment

Page 7: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Technical Strengths of PythonTechnical Strengths of Python

It's Object-OrientedIt's Object-Oriented: Python is an object-oriented language, from the : Python is an object-oriented language, from the

ground up. Its class model supports advanced notions such as ground up. Its class model supports advanced notions such as

polymorphism, operator overloading, and multiple inheritance.polymorphism, operator overloading, and multiple inheritance.

It's FreeIt's Free: Python is freeware - something which has lately been come : Python is freeware - something which has lately been come

to be called open source software.to be called open source software.

It's PortableIt's Portable: Python is written in portable ANSI C, and compiles and : Python is written in portable ANSI C, and compiles and

runs on virtually every major platform in use today.runs on virtually every major platform in use today.

It's PowerfulIt's Powerful: Dynamic typing, Automatic memory management, : Dynamic typing, Automatic memory management,

programming-in-the-large support, Built-in object types. Built-in programming-in-the-large support, Built-in object types. Built-in

tools, Library utilities.tools, Library utilities.

Page 8: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Technical Strengths of PythonTechnical Strengths of Python

It's MixableIt's Mixable: Python programs can be easily : Python programs can be easily

"glued" to components written in other languages."glued" to components written in other languages.It's Easy to UseIt's Easy to Use: as with other interpreted : as with other interpreted

languages, Python executes programs immediately, languages, Python executes programs immediately,

which makes for both an interactive programming which makes for both an interactive programming

experience and rapid turnaround after program experience and rapid turnaround after program

changes.changes.

Page 9: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Development Environment of PythonDevelopment Environment of Python

PythonWin 2.6.4PythonWin 2.6.4 It is an It is an interpreterinterpreter which reads a high-level program which reads a high-level program

and executes it, and processes the program a little at a and executes it, and processes the program a little at a

time (statement by statement).time (statement by statement).

Page 10: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

What can I do with Python?What can I do with Python? Python role’s are virtually unlimited: you can use Python role’s are virtually unlimited: you can use

it for everything from web site development and it for everything from web site development and gaming to robotics and space craft control.gaming to robotics and space craft control.

System ProgrammingSystem Programming GUIsGUIs Internet ScriptingInternet Scripting Database ProgrammingDatabase Programming Rapid PrototypingRapid Prototyping Numeric and Scientific ProgrammingNumeric and Scientific Programming Natural LanguageNatural Language Games, Images, AI, XML and moreGames, Images, AI, XML and more

Page 11: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Who Uses Python Today?Who Uses Python Today? Google and Yahoo currently use Python in Internet Google and Yahoo currently use Python in Internet

serviceservice Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and

IBM use Python for hardware testing.IBM use Python for hardware testing. Industrial Light and Magic use Python in the production Industrial Light and Magic use Python in the production

of movie animation of movie animation The YouTube video sharing service is largely written in The YouTube video sharing service is largely written in

Python.Python. The NSA uses Python for cryptography and intelligence The NSA uses Python for cryptography and intelligence

analysis.analysis. (Web) Applications• Google PageRank algorithm and (Web) Applications• Google PageRank algorithm and

Interfaces to the Google APIInterfaces to the Google API For more details, visit For more details, visit www.python.org

Page 12: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

C Program Java Program Python Program

main() { printf("hello, world\n"); }

class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); }}

print (“Hello World!!”)

Comparison with other languagesComparison with other languages

Page 13: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Comparing Python to Other Languages

Java : Python programs are generally expected to run

slower than Java programs, but they also take much less

time to develop. Python programs are typically 3-5 times

shorter than equivalent Java programs. This difference

can be attributed to Python's built-in high-level data types

and its dynamic typing. For example, a Python

programmer wastes no time declaring the types of

arguments or variables, and Python's powerful

polymorphic list and dictionary types, for which rich

syntactic support is built straight into the language, find a

use in almost every Python program. 

Page 14: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Comparing Python to Other Languages

JavascriptPython's "object-based" subset is roughly equivalent to JavaScript. Like JavaScript (and unlike Java), Python supports a programming style that uses simple functions and variables without engaging in class definitions. However, for JavaScript, that's all there is. Python, on the other hand, supports writing much larger programs and better code reuse through a true object-oriented programming style, where classes and inheritance play an important role.

Page 15: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

PerlPython and Perl come from a similar background (Unix scripting) and sport many similar features, but have a different philosophy. Perl emphasizes support for common application-oriented tasks, e.g. by having built-in regular expressions, file scanning and report generating features. Python emphasizes support for common programming methodologies such as data structure design and object-oriented programming, and encourages programmers to write readable (and thus maintainable) code by providing an elegant but not overly cryptic notation. As a consequence, Python comes close to Perl but rarely beats it in its original application domain.

Comparing Python to Other Languages

Page 16: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

First Python Program

Page 17: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Built-in data structures Numbers• decimal e.g. 631, 3.14• octal e.g. O631• hexadecimal e.g. oxABC• complex e.g. 1 + 3j• long e.g. 122233445656455L• Normal Arithmetic and Bit operators• Integer division truncates e.g. 1/2 = 0•int(x) converts x to an integer :e.g int(2.0)=2•float(x) converts x to a floating point e.g float(2) = 2.0

Python Basics

Page 18: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

NumbersNumbers

ComplexComplex Built into PythonBuilt into Python Same operations are Same operations are

supported as integer and supported as integer and floatfloat

>>> x = 3 + 2j>>> y = -1j>>> x + y(3+1j)>>> x * y(2-3j)

•Numbers are immutable•Many math functions in the math module

>>> import math>>> dir(math)['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']>>>

Page 19: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

StringsStrings are immutableThere is no char type like in C++ or Java• ConcatenationHello” + “World” -> “HelloWorld”• Repetition“UMBC” * 3 -> “UMBCUMBCUMBC”• Indexing : “UMBC”[0] -> “U”• Slicing : “UMBC”[1:3] -> “MB”• Size : len(“UMBC”) -> 4

Python Basics

Page 20: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Python BasicsComparison : “UMBC” < “umbc” -> 0• Search : “M” in “UMBC” -> 1

Can also be enclosed in single quotese.g. ‘UMBC’

•Can use single or double quotes, and three double quotes for a multi-line string

>>> 'I am a string''I am a string'>>> "So am I!"'So am I!'>>> """And me too!... though I am much longer... than the others :)"""'And me too!\nthough I am much longer\nthan the others :)'

Page 21: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

The String Data Type

>>> greet = "Hello Bob"

>>> greet[0]

'H'

>>> print(greet[0], greet[2], greet[4])

H l o

>>> x = 8

>>> print(greet[x - 2])

B

H e l l o B o b

0 1 2 3 4 5 6 7 8

Page 22: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Python program structurePython program structure

The ‘#’ starts a line commentThe ‘#’ starts a line comment

•VariablesVariables

Are not declared, just assigned

The variable is created the first time you assign it a value

Are references to objects

Type information is with the object, not the reference

Everything in Python is an object

Page 23: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

WhitespaceWhitespace

• Whitespace is meaningful in Python: especially indentation and placement of newlines. – Use a newline to end a line of code.

(Not a semicolon like in C++ or Java.)(Use \ when must go to next line prematurely.)

– No braces { } to mark blocks of code in Python… Use consistent indentation instead. The first line with a new indentation is considered outside of the block.

– Often a colon appears at the start of a new block. (We’ll see this later for function and class definitions.)

Page 24: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Look at a sample of code…Look at a sample of code…

x = 34 - 23 # A comment.

y = “Hello” # Another one.

z = 3.45

if z == 3.45 or y == “Hello”:

x = x + 1

y = y + “ World” # String concat.

print x

print y

Page 25: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Python program hello.pyPython program hello.py

Page 26: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

#!//usr/bin/python versus #!/usr/bin/env python#!//usr/bin/python versus #!/usr/bin/env python#!/usr/local/bin/python •You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with. You are pointing to python is located at /usr/local/bin/python •Consider the possibilities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail. For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly. •Thus, #/usr/bin/env python Will figure out the correct location of python ( /usr/bin/python or /bin/python from $PATH) and make that as the interpreter for rest of the script. - ( env is almost always located in /usr/bin/ so one need not worry what is env is not present at /usr/bin)

Page 27: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

Example

s="abcba"mylen=len(s)for char in range(0,mylen): if (s[char] != s[(mylen-1-char)]): print ("not a plaindrome") break else: print("palindrome") break

Program to check whether a string is palindrome or not

Page 28: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

SummarySummary

Python is a general-purpose high-level Python is a general-purpose high-level programming languageprogramming languagePython supports multiple programming Python supports multiple programming paradigms, primarily but not limited to object paradigms, primarily but not limited to object oriented, imperative, and functional oriented, imperative, and functional programming stylesprogramming stylesPython aims to combine "remarkable power Python aims to combine "remarkable power with very clear syntax", and its standard library with very clear syntax", and its standard library is large and comprehensive.is large and comprehensive.

Page 29: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

SummarySummary Its use of indentation for block delimiters is unusual Its use of indentation for block delimiters is unusual

among popular programming languages.among popular programming languages.

PythonPython was conceived in the late 1980s and its was conceived in the late 1980s and its

implementation was started in December 1989 by implementation was started in December 1989 by

Guido van RossumGuido van Rossum at CWI in the Netherlands. at CWI in the Netherlands.

PythonPython’s advantages include ’s advantages include object-oriented, free, object-oriented, free,

portable, powerful, mixable, easy to use, portable, powerful, mixable, easy to use, andand easy to easy to

learn learn propertiesproperties..

PythonWin 2.6.4 is a perfect development PythonWin 2.6.4 is a perfect development

environment for environment for PythonPython under Windows. under Windows.

Page 30: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

ExercisesExercises

1.Write a python program to add number (take input from the user)

2.Write a python program to concatenate string and integer

3. Write a python program that asks the user how many coins of various types they have, and then prints the total amount of money in rupees.

Page 31: What is Python Programming Language?  Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.  It

http://172.1.2.200:81/python