MSC Unit 1 Python Notes
MSC Unit 1 Python Notes
Python input/Output:
input (): This function first takes the input from the user and converts it into a string.
Syntax:input('STATEMENT')
Print(): print() function prints the message to the screen or any other standard output device.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
1 Letters A – – – Z or a – z
2 Digits 0,1,—-9
blank space,
4 White
horizontal tab,
spaces
carriage return,
new line,and
form feed.
type() function: using this function get the data type of any object or variable.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Python Variables: In programming, a variable is a container (storage area) to hold data.
For example,
number = 10
Variable in Python names are case-sensitive (name, Name, and NAME are three different
variables).
The reserved words(keywords) in Python cannot be used to name the variable in Python.
Constants: Constants are the fixed values that remain unchanged during the execution of aprogram
and are used in assignment statements.
Keywords:
Every language contains words and a set of rules that would make a sentence meaningful. Similarly,
in Python programming language, there are a set of predefined words, called Keywords which along
with Identifiers will form meaningful sentences when used together. Python keywords cannot be used
as the names of variables, functions, and classes.
Represents an
It is a Logical expression that It is a non-local
and False nonlocal
Operator will result in not variable
being true.
It is used to
It is used with It is a Logical
as create an alias finally not
exceptions Operator
name
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Keyword Description Keyword Description Keyword Description
It is used to Represents an
It is used to
def define the import True expression that
import a module
Function will result in true.
It is used to test if
It is used to Try is used to
del is two variables are try
delete an object handle errors
equal
While Loop is
Conditional To check if a
used to execute a
elif statements, same in value is present in while
block of
as else-if a Tuple, List, etc.
statements
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Changeable:
• The list is changeable, meaning that we can change, add, and remove items in
a list after it hasbeen created.
Allow Duplicates: Since lists are indexed, lists can have items with the same value.
List Length:
• To determine how many items a list has, use the len() function:
• A single list may contain DataTypes like Integers, Strings, as well as Objects.
Lists are mutable,and hence, they can be altered even after their creation.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Positive and Negative indexing:
Python Tuple: A tuple in Python is similar to a list. The difference between the two is that we
cannot change the elements of a tuple once it is assigned whereas we can change the elements of a
list.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Tuple methods:
Dictionary:
A Python dictionary is a collection of items that allows us to store data in key: value pairs.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Set: A Set in Python programming is an unordered collection data type that is iterable, mutable and
has no duplicate elements.
Set are represented by { } (values enclosed in curly braces)
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Python functions:
Python Functions is a block of statements that return the specific task. The idea is to
put somecommonly or repeatedly done tasks together and make a function so that
instead of writing the same code again and again for different inputs, we can do the
function calls to reuse code contained in it over and over again.
Some Benefits of Using Functions
• Increase Code Readability
• Increase Code Reusability
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Defining a Function in Python
o Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
o Any input parameters or arguments should be placed within these parentheses. You can
also define parameters inside these parentheses.
o The first statement of a function can be an optional statement; the documentation string
ofthe function or docstring.
o The code block within every function starts with a colon (:) and is indented.
o The statement return [expression] exits a function, optionally passing back an
expressionto the caller. A return statement with no arguments is the same as return
None.
Ex:
Python module
A Python module is a Python file containing a set of functions and variables to be used in an
application. The variables can be of any type (arrays, dictionaries, objects, etc.)
Modules can be either:
1. Built in
2. User-defined.
1) User-defined Modules
Create a Module
To create a module, create a Python file with a .py extension.
Call a Module
Modules created with a .py extension can be used in another Python source file, using
the import statement.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
To call this Python module myModule.py, create another Python file callModule.py file and use the
import statement.
import myModule.py:
myModule.myFunction(“Python Programming”)
When the Python interpreter encounters an import statement, it imports the module if it is present in
the search path.
A search path is a list of directories that the interpreter searches for importing a module.
2) Built-in Modules
There are several built-in modules in Python, which you can import whenever you like.
Ex:
import math
print("The value of cosine is", math.cos(3))
print("The value of sine is", math.sin(3))
print("The value of tangent is", math.tan(3))
print("The value of pi is", math.pi)
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
UNIT 2: EXCEPTION IN PYTHON
Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are problems in a
program due to which the program will stop the execution. On the other hand, exceptions are raised
when some internal events occur which change the normal flow of the program.
7. KeyboardInterrupt: raised when the user inputs interrupt keys (Ctrl + C or Delete).
8. RuntimeError: occurs when an error does not fall into any category.
9. NameError: raised when a variable is not found in the local or global scope.
11. ValueError: occurs when the operation or function receives an argument with the right
type but the wrong value.
12. ZeroDivisionError: raised when you divide a value or variable with zero.
13. SyntaxError: raised by the parser when the Python syntax is wrong.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.
Table difference between errors and exceptions in python:
Errors Exceptions
Errors are typically fatal and prevent Exceptions allow the program to
the program from running whereas continue to run and provide an
exceptionscan be caught and handled opportunity for the programmer to
within the program. handle and recover from the error.
Third Floor, BCIT, Tower 3-B (NON SEZ), Opposite to Leela Hotel Bhartiya City, Thanisandra Main Road, Kannuru, Bengaluru,
Karnataka-560064, India.