PYTHON PROGMAMMING
Python is an open source, object oriented, high level programming language. Python is a general-purpose programming language that can
be used effectively to build any kind of program that does not require direct access to the computer’s hardware.
Features of the language:
1. It is an interactive, interpreted language.
2. It is an interpreted language means it is processed at runtime by the interpreter and one need not compile the program before
executing.
3. It is a portable language having a large repository of libraries.
4. It takes less time to develop because of its built-in high-level data types and its dynamic typing.
5. It is highly efficient as there is no wastage of types of variables or arguments.
6. It supports GUI and better memory management.
7. It can be easily compatible with other languages like C, C++ etc.
8. It is used for scientific and non-scientific programming.
9. The programs in python are clear, simple and English like instructions that are easy to read and understand even by non-programmers.
10. It also allows to distinguish between input, output and error messages in different colour codes.
Few areas where Python does not offer much:
1. Python is an interpreted language not a fully compiled one. Fully compiled languages are faster than their interpreted counterparts. So
here Python is little weaker though it offers faster development times but execution times are not that fast compared to some
compiled languages.
2. Python library is still not competent with languages like C, JAVA as they have larger collection of libraries.
3. Because of its lack of syntax, Python is an easy language to program in but is becomes disadvantage when it is to be translated in other
programming language. This is because other programming language have structured defined syntax.
Output and Input in Python:
a. print() statement- It is a function which is used to display the specified content on the screen . The syntax is
print(value1,value2,...,sep=’ ‘ ,end=’\n’)
where sep is a string inserted between values, default a space.
Where end is a string appended after the last value, default a new line
b. Python provides three functions for getting user’s input:
i. input() It is used to get data from the user while working with the script mode . It enables the user to accept an input
string from the user until it encounters a new line. It is not always recommended as it always accepts input in the form
of strings and hence manipulates number as strings only and does not generate the desired result.
for example, the following code
Taking input using input() function (numbers) in n1 and n2. When n1+n2 is displayed it displays concatenated values as n1 and
n2 are handled as strings.
int() function can be used to convert the type of data inputted by input() function to numeric type .
The result is desired and displaying numeric summation.
ii. eval() : This function is used to evaluate the value of a string .It takes the string as an argument ,evaluates this string as a
number and returns the desired numeric result.
As it can be seen in above code the value entered is string because of input() function and treated as numeric because of eval() function.
- eval() interprets the value as the intended types
if integer is entered by eval() it will store as int
if float is entered, it will store the string as float
- it can also be used to input list and tuple
to enter list
l1=eval (input (“enter list”)) and entered as [1,2,3],
l1 will be used as list
to enter tuple
t1=eval(input(“enter tuple”))and entered as (1,2,3),
t1 will be used as tuple
However use of eval() should not be always used , there are other functions to enter list and tuple. If the given argument in eval() is not
string or if it cannot be evaluated as number, then eval() results in an error.
5.1.3 Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual
statement instantaneously. Whereas, Script mode
allows us to write more than one instruction in a file
called Python source code file that can be executed.
(B) Script Mode
In the script mode, we can write a Python program in
a file, save it and then use the interpreter to execute it.
Python scripts are saved as files where file name has
extension “.py”. By default, the Python scripts are saved
in the Python installation folder.
Keywords are reserved words. Each keyword has a
specific meaning to the Python interpreter, and we can
use a keyword in our program only for the purpose for
which it has been defined. As Python is case sensitive,
keywords must be written exactly as they are like for, while None , input etc.
Variables
A variable in Python denotes a memory location. In that memory location is the address of where the actual
value is stored in memory. Consider this assignment:
x=1
A memory location is set aside for the variable x. The value 1 is stored in another place in memory. The address
of the location where 1 is stored is placed in the memory location denoted by x. Later you can assign another
value to x like so. Unlike other languages like Java that is strongly typed you may assign a different type to value
to x like a floating point number. There are no types in Python.
x = 3.45
In this case, the value of 3.45 is stored in memory and the address of that value is stored in the memory location
denoted by x.
A delimiter is a sequence of one or more characters used to specify the boundary between
separate, independent regions in plain text or other data streams. An example of a delimiter is the
comma character, which acts as a field delimiter in a sequence of comma-separated values.