PYTHON
An object oriented programming language.
Contents
● Python Overview
● Fundamental
AniketGoswami
PGT – Computer Science.
Vivekananda Kendra Vidyalaya, Hurda.
1
Dist – Bhilwara
Mo. 9829906113
PYTHON OVERVIEW
Python is a high-level, interpreted, interactive and objects
oriented-scripting language. Python was designed to be highly
readable which uses English keywords frequently where as other
languages use punctuation and it has less syntactical
constructions than other languages.
Python is interpreted: This means that it is processed at runtime
by the interpreter and you do not need to compile your program
before executing it.
Interpreter Vs Compiler
Interpreter:
This language processor converts a HLL program onto machine
language by converting and executing it line by line. If there is
any error in any line, it reports it at the same time and program
execution cannot resume until the error is rectified. Its
debugging speed is fast but execution speed is slow.
Compiler:
It also converts the HLL program into machine language but the
conversion manner is different. It converts the entire HLL
program in one go, and reports all the error of the program along
with the line numbers. After all the errors are removed. The
program is recompiled. Its debugging speed is slow but execution
speed is fast.
2
Python is Interactive: This means that you can actually sit at a
Python prompt and interact with the interpreter directly to write
your programs.
Python is Object-Oriented: This means that Python supports
Object-Oriented style or technique of programming that
encapsulates code within objects.
Python is Beginner's Language: Python is a great language for the
beginner 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++, SmallTalk, and UNIX shell and other scripting
languages.
Python Features:
Easy-to-learn: Python has relatively few keywords, simple
structure, and a clearly defined syntax.
Easy-to-read: Python code is much more clearly defined.
3
Easy-to-maintain: Python's success is that its source code is
fairly easy-to-maintain.
A broad standard library: One of Python's greatest strengths is
the bulk of the library is very portable and cross-platform
compatible on UNIX, Windows.
Interactive Mode: Support for an interactive mode in which you
can enter results from a terminal right to the language, allowing
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 or customize
their tools to be more efficient.
Databases: Python provides interfaces to all major commercial
databases.
Running Python:
Once you have installed python on your computer, you are ready to
work on it. You can work in python in following different ways:
(1) Interactive Mode:
Interactive mode of working means you type the command one by one
at a time, and the Python executes the given command then gives
you output.
4
In interactive mode, you can type the command in front of python
prompt >>>.
Example:
>>> 2+5
7.
Example:
>>> print “Hello”
Hello
Try there on python prompt in interactive mode:
>>>6*3
>>>3**3
>>>6+2*4
>>>(6+2)*4
>>>5-3-3
>>>k=5-(3-3)
(2) Script Mode:
if you want to save all the commands in the form of program file
and want to see all output lines together in one go. You can use
script mode of python.
5
Example:
Do it in the Practical Lab.
A=10
B=20
C = A + B
Print(c)
Output:
30
Note : Extension of python script file is .py.
Ex: Python.py
First Python Program:
>>> print (‘Hello World’)
Hello World
Script Mode Programming:
Example:
A=10
B=20
C=A+B
print (C)
6
Python Comments
In python interpreter does not execute comments. Comments are
included for the programmers for short explanations or
observation.
Python supports two types of comments:
1) Single line comment:
In case user wants to specify a single line comment, then comment
must start with #.
Example:
# This is single line comment.
Example:
#it is my first program in python.
A=10
B=20
C = A + B
Print(c)
Output:
7
30
2) Multi line Comment:
Multi lined comment can be given inside triple quotes.
Example:
''' This
Is
Multiline comment'''
Example:
#single line comment
print ("Hello Python")
'''This is
Multiline comment'''
Let us first learn the fundamentals of python:
Python Fundamentals
Python character set:
Character set is a set of valid character that a language can
recognize.
8
A character represent any letter, digit, or any other sign. The
python has the following character set.
Letters - A-Z, a-z.
Digit - 0-9
Special character +, -, *, /, ( ), [ ], { }, =, ! =, < ,>
, #, <=, >=, ‘ , “ , : etc.
Key Words:
Keywords are the words that can convey a special meaning to the
language compiler. These are reserved for special purpose and
must not be used as normal identifier names.
falsedel and exec not assert finally or break for
pass class from printcontinue global raise def if
elif return del import try elif in while else
is with except lambda yield ect.
Python Identifiers:
Definition - A Python identifier is a name used to identify a
variable, function, class, module, or other object. An identifier
starts with a letter A to Z or a to z.
Identifiers forming rules of python are being specified below:
Python does not allow punctuation characters such as @, $, and %
within identifiers.
9
An identifier is a long sequence of letter and digit.
The first character must be a letter, the underscore (_)
counts as a letter.
Upper and lower case are different. All character are
significant.
The digit 0 to 9 can be part of the identifier except for
the first character.
First character of an identifier can be character,
underscore ( _ ) but not digit.
Keyword should not be used as an identifier name.
Example:
Valid identifiers: Myfile, Z123_3, _CHK, _DS.
Invalid identifiers: DATA-REC, while, 7myfile.
Python Literals
Literals can be defined as a data that is given to a variable or
constant.
Python support the following literals:
I. String literals:
String literals can be formed by enclosing a text in the quotes.
We can use both single as well as double quotes for a String
literals.
Example:
"Aman" , '12345'
10
Types of Strings:
There are two types of Strings supported in Python:
a) Single line String- Strings that are terminated within a
single line are known as Single line Strings.
Example:
>>> text1='hello'
b) Multiline String- A piece of text that is spread along
multiple lines is known as multiple line String.
There are two ways to create Multiline Strings:
1) Adding back slash ( \ ) at the end of each line.
Example:
>>>text1="hello\
user"
print (text1)
2) Using triple quotation marks:-
Example:
str2='''welcome
to
SSSIT'''
11
print (str2)
Output:
welcome
to
SSSIT
II. Numeric literals:
Numeric Literals are immutable. Numeric literals can belong to
following four different numerical types.
Note: Mutable Vs Immutable object.
A mutable object can be changed after it is created, and an
immutable object cannot be changed after it is created. Like
built in types ( int, float, Boolean, str etc.).
III. Boolean literals:
A Boolean literal can have any of the two values: True or False.
IV. Special literals.
12
Python contains one special literal i.e., None.
None is used to specify to that field that is not created. It is
also used for end of lists in Python.
>>> a=10
>>> b=None
>>>print (a)
>>>10
>>>print (b)
None
V. Literal Collections.
Collections such as tuples, lists and Dictionary are used in
Python.
List:
List contain items of different data types. Lists are mutable
i.e., modifiable.
The values stored in List are separated by commas (,) and
enclosed within a square brackets ([]). We can store different
types of data in a List.
Value stored in a List can be retrieved using the slice operator
([] and [:]).
13
The plus sign (+) is the list concatenation and asterisk (*) is
the repetition operator.
Example:
List=['aman',678,20.4,'saurav']
List1=[456,'rahul']
print (List)
print (List[1:3]) # print
(List[start : stop])
print (List+List1)
Output:
['aman', 678, 20.4, 'saurav']
[678, 20.4]
['aman', 678, 20.4, 'saurav', 456, 'rahul']
14