1.14k likes | 3.61k Views
What is Python Programming Language?. Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language.
E N D
What is Python Programming Language? • Python is a general-purpose interpreted, interactive, object-oriented and high-level programming language. • It is defined as object-oriented scripting language a definition that blends support for OOPs with an over all orientation towards scripting roles.
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++.
Features of Python • Python is a general-purpose high-level programming language whose design philosophy emphasizes code readability. • Python supports multiple programming paradigms, primarily but not limited to object oriented, imperative and, to a lesser extent, functional programming styles. • Python aims to combine "remarkable power with very clear syntax", and its standard library is large and comprehensive. • Its use of indentation for block delimiters is unusual among popular programming languages.
Introduction to Python • Development of Python • Python was developed in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI (Centrum Wiskunde & Informatica) in the Netherlands.
Introduction to Python • Python 2.0 was released on 16 October 2000, with many major new features including a full garbage collector and support for Unicode. • Python 3.0, a major, backwards-incompatible release with Python 2.x, was released on 3 December 2008 after a long period of testing. • Many of its major features have been backported to the backwards-compatible Python 2.7.
4 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
Technical Strengths of Python • It's Object-Oriented: Python is an object-oriented language, from the ground up. Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance. • It's Free: Python is freeware - something which has lately been come to be called open source software. • It's Portable: Python is written in portable ANSI C, and compiles and runs on virtually every major platform in use today. • It's Powerful: Dynamic typing, Automatic memory management, programming-in-the-large support, Built-in object types. Built-in tools, Library utilities.
Technical Strengths of Python • It's Mixable: Python programs can be easily "glued" to components written in other languages. • It's Easy to Use: as with other interpreted languages, Python executes programs immediately, which makes for both an interactive programming experience and rapid turnaround after program changes.
Development Environment of Python • PythonWin 2.6.4 • It is an interpreter which reads a high-level program and executes it, and processes the program a little at a time (statement by statement).
What can I do with Python? • Python role’s are virtually unlimited: you can use it for everything from web site development and gaming to robotics and space craft control. • System Programming • GUIs • Internet Scripting • Database Programming • Rapid Prototyping • Numeric and Scientific Programming • Natural Language • Games, Images, AI, XML and more
Who Uses Python Today? • Google and Yahoo currently use Python in Internet service • Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware testing. • Industrial Light and Magic use Python in the production of movie animation • The YouTube video sharing service is largely written in Python. • The NSA uses Python for cryptography and intelligence analysis. • (Web) Applications• Google PageRank algorithm and Interfaces to the Google API • For more details, visit www.python.org
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.
Comparing Python to Other Languages Javascript Python'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.
Comparing Python to Other Languages Perl Python 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.
Python Basics • 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
Numbers • Numbers are immutable • Many math functions in the math module Complex • Built into Python • Same operations are supported as integer and float >>> x = 3 + 2j >>> y = -1j >>> x + y (3+1j) >>> x * y (2-3j) >>> 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'] >>>
Python Basics • Strings • Strings are immutable • There is no char type like in C++ or Java • Concatenation Hello” + “World” -> “HelloWorld” • Repetition “UMBC” * 3 -> “UMBCUMBCUMBC” • Indexing : “UMBC”[0] -> “U” • Slicing : “UMBC”[1:3] -> “MB” • Size : len(“UMBC”) -> 4
Python Basics • Comparison : “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 :)'
H e l l o B o b 0 1 2 3 4 5 6 7 8 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
Python program structure The ‘#’ starts a line comment • Variables • 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
Whitespace • 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.)
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
Python program hello.py #!/usr/bin/python # This program says hello and asks for your name. hello = 'Hello world! ' print(hello) print('What is your name?') myName = raw_input() print('It is good to meet you, ' + myName) • Steps to to compile $python hello.py or $chmod 700 hello.py ./hello.py
#!//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)
Example Program to check whether a string is palindrome or not
Summary • Python is a general-purpose high-level programming language • Python supports multiple programming paradigms, primarily but not limited to object oriented, imperative, and functional programming styles • Python aims to combine "remarkable power with very clear syntax", and its standard library is large and comprehensive.
Summary • Its use of indentation for block delimiters is unusual among popular programming languages. • Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands. • Python’s advantages include object-oriented, free, portable, powerful, mixable, easy to use, and easy to learn properties. • PythonWin 2.6.4 is a perfect development environment for Python under Windows.
Exercises Write a python program to add number (take input from the user) Write a python program to concatenate string and integer 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.