UNIT-2-INTRODUCTION TO PYTHON PROGRAMMING
GETTING STARTED WITH P Y T H O N
INTRODUCTION:
Python was developed by Guido Van Rossum (Netherlands) in February 1991. Python got
its name from a BBC comedy series from seventies called “Monty Python s Flying Circus”. It is
based on two programming languages called ABC (replacement of BASIC) and Modula–3.
It is used in a variety of fields, including software development, web development,
scientific computing, big data and Artificial Intelligence.
ADVANTAGES OF PYTHON:
1. Easy to Use: Python is compact and very easy to use Object Oriented language with very
simple syntax rules. It is programmer–friendly
2. Expressive Language: Because of simple syntax and fewer lines of code, it is more
capable to express code's purpose than many other languages
C Language Code Python Code
for swapping values for swapping values
int a = 2, b = 3, temp; a, b = 2, 3
temp = a; a, b = b, a
a = b;
b = temp;
3. Interpreted Language: Python interprets and executes the code line by line at a time. It
makes Python an easy–to–debug language and thus suitable for beginners and advanced
users
4. Completeness: Python Standard Library provides various modules for different
functionalities. For example, it can be used for different functionalities like email, web–
pages, databases, GUI development, network connections and many more. Hence no
additional libraries to be installed
5. Cross–Platform Language: Python can run on different platforms like Windows, Linux /
Unix, Macintosh, Super Computers, Smart Phones etc. Hence it portable language
6. Free and Open Source: Python is freely available at free of cost and its source code is
available to every body for further improvement
7. Variety of Usage: Python can be used for a variety of applications like Scripting, Web
Applications, Game development, System Administration, Rapid Prototyping, GUI
Programs, Database Applications etc.
Limitations of Python:
1. Not the Fastest Language: Fully compiled languages are faster than interpreted
languages. Python is an interpreted language. It is first semi–compiled into an internal
byte–code, which is then executed by a Python interpreter. Hence Python is not faster
compared to fully compiled languages
2. Lesser Libraries: Python offers library support for almost all computing programs, but its
library is still not competent with languages like C, Java, Perl as they have larger collection
of libraries
3. Not Strong on Type–Binding: Python is not strong on catching 'Type Mismatch'. For
example, a String value can be stored in a variable declared as an integer, where Python
never complain about it
4. Not Easily Convertible: The syntax of Python language is simple but different compared
to other programming languages. Hence it is not easy to convert a program which is in
Python into other language
WORKING IN PYTHON:
To write and run Python program, install Python interpreter in computer. IDLE (GUI
integrated) is the standard, most popular Python development environment. IDLE is an acronym of
Integrated Development Environment. It lets edit, run, browse and debug Python Programs from a
single interface. This environment makes it easy to write programs.
Python IDLE (python shell)can be used in two ways (i) Interactive Mode (ii) Script Mode
Interactive Mode: As the name suggests, this mode allows interacting with OS. This mode does not
save commands in form of a program. Some points to remember while working in interactive mode
are,
The symbol '>>>' is called Python prompt, indicates that interpreter is ready to accept
command
At the prompt, either a command or statement or expression can be given
The secondary prompt is '...', indicates that interpreter is waiting for additional input
Any statement starts with the symbol "#" is called comment
Alt+P and Alt+N are used to invoke and repeat prior commands provided interactive
window
The quit( ) or Ctrl+D is used to leave the interpreter
The command help( ), with empty parenthesis, will provides an interactive help
The command help( ), with a command inside parenthesis, will provide an
exclusive help about required command
To leave help and return to interactive mode, quit command can be used
Ex:
>>> 2+5
>>>7
>>>print(‘Hi’)
>>>Hi
Script Mode: In script mode, Python program is to be typed in a file and then interpreter to be used
to execute the content from the file. Working in interactive mode is convenient for beginners and
for testing small pieces of code. But for coding more than few lines, code is to be saved so that it
can be modified and reused.
To create and run a Python script, the following steps to be used in IDLE
1. FileNew Window (for creating a new script file)
2. FileOpen window (for opening already existing file)
3. Write the Python code as function i.e. script
4. Save it (Ctrl+S) , it will save with .py extension
5. Execute it in interactive mode, by using RUN option (Ctrl+F5)
Understanding print( ):
To print or display output, Python provides print( ) statement. It can be used as follows
print (<objects to be printed>...)
Ex: print ("Hello World")
In Python, a string may be provided in either double quotes or single quotes
Ex:
Valid strings: “hi”, ‘how are you’
Invalid strings: ‘hi”, “how do you do’, {hi}
Ex: 1.
print(“Hi”)
print(‘Welcome to Python Programming’)
Output:
Hi
Welcome to Python Programming
2. Write a python program to display Good Morning?
Ans: print(“Good Morning”)
3. Write a program to display following output
Take every chance
Drop every fear
Ans:
print(“Take every chance”)
print(“Drop every fear”)