Python unit 1
Python unit 1
Unit 1
Janhavi Vadke
Contents
• Introduction
• Variables and Expressions
• Conditional Statements
• Looping
• Control statements
History
• Python is general purpose
programming language also
used as scripting language. It is
Open source language.
• The Python programming
language was developed in the
late 1980s and its
implementation was started in
December 1989 by Guido Van
Rossum in the Netherlands as a
successor to the ABC
programming language. (named
after Monty Python’s Flying
circus)
• 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 became more transparent.
• Python 3.0 (initially called Python 3000 or py3k)
was released on 3 December 2008 after a long
testing period. It is a major revision of the
language.
• Releases of Python 3 include the 2to3 utility,
which automates the translation of Python 2 code
to Python 3.
• Unlike C/C++ or Java, Python statements do
not end in a semicolon
• In Python, indentation is the way you
indicate the scope of a conditional,
function, etc.
• No braces!
• Python is interpretive, meaning you don’t
have to write programs. You can just enter
statements into the Python environment
and they’ll execute
Python Features
1) Easy to Use:
Python is very easy to use and high level
language. Thus it is programmer-friendly
language
2) Expressive Language:
Python language is more expressive. The sense
of expressive is the code is easily
understandable
3) Interpreted Language:
Python is an interpreted language i.e.
interpreter executes the code line by line at a
time. This makes debugging easy and thus
suitable for beginners
4) Cross-platform language:
Python can run equally on different platforms
such as Windows, Linux, Unix , Macintosh etc.
Thus, Python is a portable language
5) Free and Open Source:
Python language is freely available
(www.python.org). The source-code is also
available. Therefore it is open source.
6) Object-Oriented language:
Python supports object oriented language.
Concept of classes and objects comes into
existence.
7) Extensible:
It implies that other languages such as C/C++
can be used to compile the code and thus it can
be used further in your python code
8) Large Standard Library:
Python has a large and broad library. It can help
to do regular expressions, documentation
generation, unit testing, threading, databases,
web browsers, email, xml, html, GUI, Tk ect.
9) GUI Programming:
Graphical user interfaces can be developed
using Python.
10) Integrated:
It can be easily integrated with languages like C,
C++, JAVA etc.
How to install python
• Visit www.python.org and navigate to
Downloads > Windows and click Python
3.6.5.
Running Python program
• use idle
Debugging
• Three kinds of errors can occur in a program: syntax
errors, runtime errors, and semantic errors.
1) Syntax error: “Syntax” refers to the structure of a
program and the rules about that structure
• For example, parentheses have to come in matching
pairs, so (1 + 2) is legal, but 8) is a syntax error
2) Runtime error: These condtype of error is a runtime
error, so called because the error does not appear until
after the program has started running. These errors are
also called exceptions because they usually indicate that
something exceptional (and bad) has happened.
3) Semantic error: The third type of error is
“semantic”, which means related to meaning. If
there is a semantic error in your program, it will
run without generating error messages, but it
will not do the right thing. It will do something
else.
Formal and Natural language
1) Terminating loops
break statement terminates the current loop and
resumes execution at the next statement. It is used
to exit a for loop or a while loop
count = 0
while True:
print(count)
count += 1
if (count >= 5):
break
2) Skipping specific conditions
• continue statement is used to skip the
remaining statements in the current iteration
of the loop and to move the control back to
the top of the loop.
• for i in range(1,11):
If (i==5):
continue
print (i)
Interactive mode and Script mode
1) Script mode- it is the mode where the scripted
and finished .py files are run in the Python
interpreter.
2) Interactive mode is command line commands
which gives immediate feedback for each
statement, while running previously fed
statements in active memory. As new lines are
fed into the interpreter, the fed program is
evaluated both in part and in whole.
>>> symbol indicates interactive mode
Using else Statement with Loops