Python-Unit-1 Part1_1
Python-Unit-1 Part1_1
Definition:
Python is a high-level, interpreted, interactive and object-oriented scripting language.
Python is designed to be highly readable. It uses English keywords frequently where as other
languages use punctuation, and it has fewer syntactical constructions than other languages.
Python is Interpreted: Python is processed at runtime by the interpreter. You do not need
to compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
History of Python
Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, Unix shell, and other
scripting languages.
At the time when he began implementing Python, Guido van Rossum was also reading the
published scripts from "Monty Python's Flying Circus" (a BBC comedy series from the
seventies, in the unlikely case you didn't know). It occurred to him that he needed a name
that was short, unique, and slightly mysterious, so he decided to call the language Python.
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.
Python 1.0 was released on 20 February, 1991.
Python 2.0 was released on 16 October 2000 and had many major new features, including
a cycle detecting garbage collector and support for Unicode. With this release the
development process was changed and became more transparent and community- backed.
Python 3.0 (which early in its development was commonly referred to as Python 3000 or
py3k), a major, backwards-incompatible release, was released on 3 December 2008 after
a long period of testing. Many of its major features have been back ported to the
backwards-compatible Python 2.6.x and 2.7.x version series.
In January 2017 Google announced work on a Python 2.7 to go transcompiler, which The
Register speculated was in response to Python 2.7's planned end-of-life.
PYTHON PROGRAMMING UNIT-1
Python Features:
Python's features include:
Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax.
This allows the student to pick up the language quickly.
Easy-to-read: Python code is more clearly defined and visible to the eyes.
Easy-to-maintain: Python's source code is fairly easy-to-maintain.
A broad standard library: Python's bulk of the library is very portable and cross-
platform compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode which allows interactive
testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
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.
Databases: Python provides interfaces to all major commercial databases.
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.
Scalable: Python provides a better structure and support for large programs than shell
scripting.
Component integration
Python scripts can easily communicate with other parts of an application, using a
variety of integration mechanisms. Such integrations allow Python to be used as a product
customization and extension tool. Today, Python code can invoke C and C++ libraries, can
be called from C and C++ programs, can integrate with Java and .NET components, can
communicate over frameworks such as COM and Silverlight, can interface with devices
over serial ports, and can interact over networks with interfaces like SOAP, XML-RPC,
and CORBA. It is not a standalone tool.
Enjoyment
Because of Python‘s ease of use and built-in toolset, it can make the act of
programming more pleasure than chore. Although this may be an intangible benefit, its
effect on productivity is an important asset. Of these factors, the first two (quality and
productivity) are probably the most compelling benefits to most Python users, and merit a
fuller description.
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;
yet in the context of Python's dynamic typing, object-oriented programming (OOP) is
remarkably easy to apply. Python's OOP nature makes it ideal as a scripting tool for object-
oriented systems languages such as C++ and Java. For example, Python programs can
subclass (specialized) classes implemented in C++ or Java.
It's Free
Python is freeware—something which has lately been come to be called open
source software. As with Tcl and Perl, you can get the entire system for free over the
Internet. There are no restrictions on copying it, embedding it in your systems, or shipping
it with your products. In fact, you can even sell Python, if you're so inclined. But don't get
the wrong idea: "free" doesn't mean "unsupported". On the contrary, the Python online
community responds to user queries with a speed that most commercial software vendors
would do well to notice.
It's Portable
Python is written in portable ANSI C, and compiles and runs on virtually every
major platform in use today. For example, it runs on UNIX systems, Linux, MS-DOS, MS-
Windows (95, 98, NT), Macintosh, Amiga, Be-OS, OS/2, VMS, QNX, and more. Further,
Python programs are automatically compiled to portable bytecode, which runs the same on
any platform with a compatible version of Python installed (more on this in the section
"It's easy to use"). What that means is that Python programs that use the core language run
the same on UNIX, MS-Windows, and any other system with a Python interpreter.
It's Powerful
From a features perspective, Python is something of a hybrid. Its tool set places it
between traditional scripting languages (such as Tcl, Scheme, and Perl), and systems
languages (such as C, C++, and Java). Python provides all the simplicity and ease of use
of a scripting language, along with more advanced programming tools typically found in
systems development languages.
PYTHON PROGRAMMING UNIT-1
Applications of Python:
1. Systems Programming
2. GUIs
3. Internet Scripting
4. Component Integration
5. Database Programming
6. Rapid Prototyping
7. Numeric and Scientific Programming
Step 2: Click on Python 2.7.13 and download. After download open the file.
PYTHON PROGRAMMING UNIT-1
Step 4: After installation location will be displayed. The Default location is C:\Python27.
Click on next to continue.
PYTHON PROGRAMMING UNIT-1
Step 5: After the python interpreter and libraries are displayed for installation. Click on Next
to continue.
Right-click the My Computer icon on your desktop and choose Properties. And then
select Advanced System properties.
Goto Environment Variables and go to System Variables select Path and click on
Edit.
Add semicolon (;) at end and copy the location C:\Python27 and give semicolon (;) and
click OK.
PYTHON PROGRAMMING UNIT-1
Running Python:
a. Running Python Interpreter:
Python comes with an interactive interpreter. When you type python in your shell or
command prompt, the python interpreter becomes active with a >>> prompt and waits for your
commands.
Now you can type any valid python expression at the prompt. Python reads the typed
expression, evaluates it and prints the result.
Variables:
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what
can be stored in the reserved memory. Therefore, by assigning different data types to variables,
you can store integers, decimals or characters in these variables.
KEYWORDS
The following list shows the Python keywords. These are reserved words and you
cannot use them as constant or variable or any other identifier names. All the Python keywords
contain lowercase letters only.
INPUT Function:
To get input from the user you can use the input function. When the input function is
called the program stops running the program, prompts the user to enter something at the
keyboard by printing a string called the prompt to the screen, and then waits for the user to
press the Enter key. The user types a string of characters and presses enter. Then the input
function returns that string and Python continues running the program by executing the next
statement after the input statement.
Python provides the function input(). input has an optional parameter, which is the
prompt string.
For example,
PYTHON PROGRAMMING UNIT-1
OUTPUT function:
We use the print() function or print keyword to output data to the standard output
device (screen). This function prints the object/string written in function.
The actual syntax of the print() function is
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects is the value(s) to be printed.
The sep separator is used between the values. It defaults into a space character. After
all values are printed, end is printed. It defaults into a new line ( \n ).
Indentation
Code blocks are identified by indentation rather than using symbols like curly braces.
Without extra symbols, programs are easier to read. Also, indentation clearly identifies which
block of code a statement belongs to. Of course, code blocks can consist of single statements,
too. When one is new to Python, indentation may come as a surprise. Humans generally prefer
to avoid change, so perhaps after many years of coding with brace delimitation, the first
impression of using pure indentation may not be completely positive. However, recall that two
of Python's features are that it is simplistic in nature and easy to read.
Python does not support braces to indicate blocks of code for class and function
definitions or flow control. Blocks of code are denoted by line indentation. All the continuous
lines indented with same number of spaces would form a block. Python strictly follow
indentation rules to indicate the blocks.
PYTHON PROGRAMMING UNIT-1