Python Programming Unit-1
Python Programming Unit-1
History of Python:
In February 1991, van Rossum published the code (labeled version 0.9.0) to alt.sources.
In 1994, Python 1.0 was released with new features like: lambda, map, filter, and reduce.
On October 16,2000 Python 2.0 added new features like: list comprehensions, garbage
collection system.
Page 1
7. Python is extensible: it is easy to add a new built-in function or module to the interpreter,
either to perform critical operations at maximum speed, or to link Python programs to
libraries that may only be available in binary form
8. Python is free and open source software
Applications of Python:
Python offers many choices for web development. ). Its standard library supports many
Internet protocols like http,FTP etc.Python supports a variety of modules to work with
the Hypertext Markup Language (HTML), and several interfaces for working with the
Extensible Markup Language (XML). It also provides Frameworks such as Django, Flask
etc to design and delelop web based applications.
Python provides Tk GUI library to develop user interface in python based application.
Some other useful toolkits wxWidgets, Kivy, pyqt that are useable on several platforms.
The Kivy is popular for writing multitouch applications.
Software Development
Python is helpful for software development process. It works as a support language and
can be used for build control and management, testing etc.
Business Applications
Python is also used to build ERP and e-commerce systems
Page 2
Basics of Python Programming:
when interpreter called with standard input connected to a tty device, it reads and executes
commands interactively; when called with a file name argument or with a file as standard input,
it reads and executes a script from that file.
Interactive Mode
When commands are read from a tty, the interpreter is said to be in interactive mode. In this
mode it prompts for the next command with the primary prompt, usually three greater-than signs
(>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...).
Ex:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
>>> 17 / 3 # classic division returns a float
5.666666666666667
Page 3
Using a Script file:
when interpreter called with a file name argument or with a file as standard input, it reads and
executes a script from that file. For this purpose follow the steps given below
step 2 : write the instructions and save the file with the extension .py (python used .py extension)
(ex: first.py)
step 3: now run the following command on the terminal to execute the python script file
Note:
Python script file can also be executed using python IDLE( IDLE is Python’s Integrated
Development and Learning Environment and is an alternative to the command line). On Windows
this comes with the Python interpreter, but in other operating systems it may be installed through
package manager.
Identifiers:
An identifier is a name given to a variable, function, class or module. The rules to name an
identifier are given below.
Page 4
Keywords:
Keywords are a list of reserved words that have predefined meaning. They cannot be used as
identifiers for variables, classes, functions etc. Attempting to use a keyword as an identifier name
will cause an error. The list of keywords in python are
Variable:
Variable is a name which is used to refer memory location. Variable can hold any type of data
which the program can use to assign and modify during its execution.
To create a variable in Python, all you need to do is specify the variable name, and then assign a
value to it.
Python uses = to assign values to variables. There's no need to declare a variable in advance
(or to assign a data type to it), assigning a value to a variable itself declares and initializes the
variable with that value. There's no way to declare a variable without assigning it an initial value.
Page 5
Ex:
>>>a = 2
>>>b = 9223372036854775807
>>>pi = 3.14
>>>c = 'A'
>>>name = 'John Doe'
>>>q = True
>>>print(a)
>>>print(type(a))
>>>print(b)
>>>print(type(b))
>>>print(pi)
>>>print(type(pi))
>>>print(c)
>>>print(type(c))
>>>print(name)
>>>print(type(name))
>>>print(q)
>>>print(type(q))
output:
2
<class 'int'>
9223372036854775807
<class 'int'>
3.14
<class 'float'>
A
<class 'str'>
John Doe
<class 'str'>
True
<class 'bool'>
Page 6
Note:
1. You can assign multiple values to multiple variables in one line. Note that
there must be the same number of arguments on the right and left sides of
the = operator:
Ex:
>>>a, b, c = 1, 2, 3
>>>print(a, b, c)
Output:
123
Ex:
a, b, c = 1, 2
Output:
=> Traceback (most recent call last):
=> File "name.py", line N, in <module>
=> a, b, c = 1, 2
=> ValueError: need more than 2 values to unpack
Ex:
>>>a = b = c = 1
>>>print(a, b, c)
Output:
111
>>>a = b = c = 1
When using such cascading assignment, it is important to note that all three
variables a, b and c refer to the same object in memory, an int object with the
value of 1. In other words, a, b and c are three different names given to the
Page 7
same int object. Assigning a different object to one of them afterwards doesn't
change the others, just as expected:
Ex:
>>>a = b = c = 1 all three names a, b and c refer to same int object with value 1
>>>print(a, b, c)
Output: 1 1 1
Input-Output:
In python, we have the input() function to take the input from the user. The input() function
takes the user input as a string.
syntax :
input([prompt])
Ex:
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
Page 8
Note:
In the above example, the entered value 10 is a string, not a number. To convert
this into a number we can use int() or float() functions as given below.
In python, We use the print() function to output data to the standard output device
(screen) or to a file.
Syntax:
Ex:
>>> print("SVIET")
SVIET
Page 9
Indentation:
Most of the programming languages like C, C++, Java use braces { } to define a block of code.
Python programs get structured through indentation, i.e. code blocks are defined by their
indentation.
A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line. The amount of indentation is up to you, but it must be consistent throughout that
block.
Generally four whitespaces are used for indentation and is preferred over tabs. Here is an
example.
Output:
1
2
3
4
5
The enforcement of indentation in Python makes the code look neat and clean. This results into
Python programs that look similar and consistent.
Indentation can be ignored in line continuation. But it's a good idea to always indent. It makes
the code more readable. Incorrect indentation will result into IndentationError.
Page 10
IDLE:
IDLE is Python’s Integrated Development and Learning Environment. The Python installer for
Windows contains the IDLE module by default. IDLE is not available by default in Python
distributions for Linux. It needs to be installed using the respective package managers.
IDLE can be used to execute a single statement just like Python Shell and also to create,
modify ,debug and execute Python scripts.
Q1: What happens if a semicolon (;) is placed at the end of a Python statement?
Python use semicolon as line terminator as in the case of any other programming language. If
you want to use multiple lines in the same line, you should use semicolon to separate
them.
Ex1: a = 5; #No Error
Ex2: b = 6; c = 7 #No Error
If you omitted the semicolon in Line 2, Python would throw you a SyntaxError.
Ex3: b=6 c=7
SyntaxError: invalid syntax
So, you can use semicolon in Python too!
But Python codes standard advices to avoid using multi-line statements in your program as
it reduces readability.
Page 11