python module 2
python module 2
It allows developers to interactively run Python code line by line (Interpreter takes
single instruction as input).
Python interpreter and interactive mode
Compiler
To translate a program written in a high-level language into a low-level language all
at once, in preparation for later execution.
Interactive mode
Script mode
Interactive Mode
Interactive Mode, as the name suggests, allows us to interact with OS.
Advantages:
Working in interactive mode is convenient for beginners and for testing small
pieces of code.
Interactive Mode
Drawback:
We cannot save the statements and have to retype all the statements once again
to re-run them.
In interactive mode, you type Python programs and the interpreter displays the
result:
>>>1 + 1
>>> 2
>>>print ('Hello, World!')
Hello, World!
Interactive Mode
• The chevron, >>>, is the prompt the interpreter uses to indicate that
it is ready for you to enter code.
• If you type 1 + 1, the interpreter replies 2.
Data type
Every value in Python has a data type. It is a set of values, and the allowable operations on those
values.
VALUES AND DATA TYPES
VALUES AND DATA TYPES
NUMBERS
❖ Number data type stores Numerical Values.
▪
❖ This data type is immutable [i.e. values/items cannot be changed].
▪
❖ Python supports integers, floating point numbers and complex numbers. They are defined as,
STATEMENTS AND EXPRESSIONS:
Statements:
Instructions that a Python interpreter can
executes are called statements.
>>> n = 17
>>> print(n)
• Python has a very powerful tuple assignment feature that allows a tuple of variables on
the left of an assignment to be assigned values from a tuple on the right of the
assignment.
• The left side is a tuple of variables; the right side is a tuple of values.
Example:
• It is useful to swap the values of two variables. With conventional assignment statements,
we have to use a temporary variable. For example, to swap a and b:
TUPLE ASSIGNMENT
• Tuple assignment solves this problem neatly:
(a, b) = (b, a)
• In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into
the variables/names on the right:
SEQUENCE
❖▪ A sequence is an ordered collection of items, indexed by positive integers.
❖ It is a combination of mutable (value can be changed) and immutable (values cannot be changed) data
types.
❖ There are three types of sequence data type available in Python, they are
• Strings
• Lists
• Tuples
STRINGS
A String in Python consists of a series or sequence of characters - letters,
▪ numbers, and special characters.
Strings are marked by quotes:
∙ single quotes (' '), Eg, 'This a string in single quotes'
∙ double quotes (" "), Eg, "This a string in double quotes“
∙ triple quotes (""“ """), Eg, This is a paragraph. It is made up of
€ multiple lines and sentences."""
▪ Individual character in a string is accessed using a subscript (index).
Characters can be accessed using indexing and slicing operations
Strings are immutable i.e. the contents of the string cannot be changed
after it is created.
STRINGS
Indexing
Concatenation
Slicing
Repetition
Membership
• <class ‘list’>
Characteristics of Lists
▪
Benefit of Tuple:
❖ Tuples are faster than lists.
❖ If the user wants to protect the data from accidental changes, tuple can be
used.
❖ Tuples can be used as keys in dictionaries, while lists can't.
TUPLES
Indexing
Concatenation
Slicing
Repetition
• The indexing and slicing in the tuple are similar to lists. The indexing
in the tuple starts from 0 and goes to length(tuple) - 1.
• The items in the tuple can be accessed by using the index [] operator.
• Python also allows us to use the colon operator to access multiple
items in the tuple.
tuple = (1,2,3,4,5,6,7)
#element 1 to end
print(tuple[1:]) (2, 3, 4, 5, 6, 7)
#element 0 to 3 element (1, 2, 3, 4)
print(tuple[:4]) (1, 2, 3, 4)
#element 1 to 4 element (1, 3, 5)
print(tuple[1:5])
# element 0 to 6 and take step of 2
• print(tuple[0:6:2])
tuple1 = (1, 2, 3, 4, 5)
print(tuple1[-1]) 5
print(tuple1[-4])
2
print(tuple1[-3:-1])
print(tuple1[:-1])
(3, 4)
print(tuple1[-2:]) (1, 2, 3, 4)
(4, 5)
Deleting Tuple
• Unlike lists, the tuple items cannot be deleted by using the del keyword as
tuples are immutable.
• To delete an entire tuple, we can use the del keyword with the tuple name
tuple1 = (1, 2, 3, 4, 5, 6)
print(tuple1) (1, 2, 3, 4, 5, 6)
del tuple1[0] Traceback (most recent call last):
print(tuple1) File "tuple.py",
del tuple1 line 4, in <module> print(tuple1)
print(tuple1) NameError: name 'tuple1' is not defined
t = (1, 2, 3, 4, 5)
t1 = (6, 7, 8, 9)
Mapping and Dictionaries
❖ Unordered and Mutable data type
Dictionaries
❖
▪ Lists are ordered sets of objects, whereas dictionaries are unordered sets.
❖ Dictionary is created by using curly brackets. i,e. {}
❖
▪ Dictionaries are accessed via keys and not via their position.
❖ A dictionary is an associative array (also known as hashes). Any key of the dictionary is
▪ associated (or mapped) to a value.
❖ The values of a dictionary can be any Python data type. So dictionaries are unordered
key-value-pairs (The association of a key and a value is called a key-value pair )
Dictionaries don't support the sequence operation of the sequence data types like strings, tuples
and lists.
DICTIONARIES
Operations
Indexing
Creating a Dictionary
•Arithmetic Operators
•Comparison (Relational) Operators
•Assignment Operators
•Logical Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
Arithmetic operators
• They are used to perform mathematical operations like addition,
subtraction, multiplication etc. Assume, a=10 and b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition. Assume, a=10 and
b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition. Assume, a=10 and
b=5
Assignment Operators
• Assignment operators are used in Python to assign values to variables.
Assignment Operators
Assignment Operators
Logical Operators
• Logical operators are the and, or, not operators.
Bitwise Operators:
• A bitwise operation operates on one or more bit patterns at the level of individual bits
Dr. Jothiaruna N, VIT Bhopal University
Bitwise Operators
As the name suggests, bitwise operators perform operations at the bit level. These operators include bitwise AND,
bitwise OR, bitwise XOR, and shift operators. Bitwise operators expect their operands to be of integers and treat them
as a sequence of bits.
The truth tables of these bitwise operators are given below.
Examples:
• Let, x=[5,3,6,4,1]. To check particular item in list or not, in and not in operators are used.
Identity Operators
• They are used to check if two values (or variables) are located on the same part of the
memory
Expressions- Operator Precedence
Output:
Name:george
Age 56
Keyword Arguments:
• Python interpreter is able to use the keywords provided to match the
values with parameters even though if they are arranged in out of order.
• def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
Output:
• Name: george
• Age 56
Default Arguments:
Assumes a default value if a value is not provided in the function call for that
argument.
• def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george“,age)
Output:
• Name: george
• Age 40
Variable length Arguments
• If we want to specify more arguments than specified while defining
the function, variable length arguments are used. It is denoted by *
symbol before parameter.
• def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",ärjun")
Output:
• rajan rahul micheal ärjun
MODULES
• A module is a file containing Python definitions ,functions, statements and
instructions.
• Standard library of Python is extended as modules.
• To use these modules in a program, programmer needs to import the module.
• Once we import a module, we can reference or use to any of its functions or
variables in our code.
• There is large number of standard modules also available in python.
• Standard modules can be imported the same way as we import our user-defined
modules.
• Every module contains many function.
• To access one of the function , you have to specify the name of the module and the
name of the function separated by dot . This format is called dot notation.
Syntax
• import module_name
• module_name.function_name(variable)
Four Ways to Import Module