Unit - 2 Python
Unit - 2 Python
PREPARED BY
Mr. P. NANDAKUMAR
ASSISTANT PROFESSOR,
DEPARTMENT OF INFORMATION TECHNOLOGY,
SVCET.
COURSE CONTENT
if Statement
BOOLEAN EXPRESSIONS (CONDITIONS)
The Boolean data type contains two Boolean values, denoted as True
and False in Python.
A Boolean expression is an expression that evaluates to a Boolean
value.
Boolean expressions are used to denote the conditions for selection and
iterative control statements.
Relational Operators
Membership Operators
Boolean Operators
Operator Precedence and Boolean Expressions
Short-Circuit (Lazy) Evaluation
RELATIONAL OPERATORS
The relational operators in Python perform the usual comparison
operations.
Relational expressions are a type of Boolean expression, since they
evaluate to a Boolean result.
Note the use of the comparison operator , = = , for determining if two
values are equal. This, rather than the (single) equal sign, = , is used
since the equal sign is used as the assignment operator.
This is often a source of confusion for new programmers,
if n ! 5 0:
if 1/n , tolerance:
LOGICALLY EQUIVALENT BOOLEAN
EXPRESSIONS
cars.remove("Volvo")
ARRAY METHODS
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
extend() Add the elements of a list (or any iterable), to the end of the current
list
index() Returns the index of the first element with the specified value
Example:
Create a List:
print(thislist)
LISTS
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has
index [1] etc.
Ordered
When we say that lists are ordered, it means that the items have a
defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of
the list.
LISTS
Changeable
The list is changeable, meaning that we can change, add, and remove
items in a list after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
print(thislist)
LISTS
List Length
To determine how many items a list has, use the len() function:
print(len(thislist))
list2 = [1, 5, 7, 9, 3]
type()
From Python's perspective, lists are defined as objects with the data type
'list':
<class 'list’>
print(type(mylist))
LISTS
The list() Constructor
It is also possible to use the list() constructor when creating a new list.
Example
print(thislist)
TUPLE
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections
of data, the other 3 are List, Set, and Dictionary, all with different
qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
print(thistuple)
TUPLE
Tuple Items
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Ordered
When we say that tuples are ordered, it means that the items have a
defined order, and that order will not change.
Unchangeable
Since tuples are indexed, they can have items with the same value:
print(thistuple)
Tuple Length
To determine how many items a tuple has, use the len() function:
print(len(thistuple))
TUPLE
Create Tuple With One Item
To create a tuple with only one item, you have to add a comma after the
item, otherwise Python will not recognize it as a tuple.
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
TUPLE
Tuple Items - Data Types
tuple2 = (1, 5, 7, 9, 3)
print(type(mytuple))
print(thistuple)
TUPLE METHODS
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a specified value occurs in a
tuple
index() Searches the tuple for a specified value and returns the
position of where it was found
DICTIONARY
Dictionaries are used to store data values in key:value pairs.
Dictionaries are written with curly brackets, and have keys and values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict)
DICTIONARY
Dictionary Items
Dictionary items are ordered, changeable, and does not allow duplicates.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(thisdict["brand"])
DICTIONARY
Ordered or Unordered:
When we say that dictionaries are ordered, it means that the items have a
defined order, and that order will not change.
Unordered means that the items does not have a defined order, you cannot
refer to an item by using an index.
Changeable
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
print(thisdict)
DICTIONARY
Dictionary Length - To determine how many items a dictionary has, use
the len() function: Example: Print the number of items in the dictionary:
print(len(thisdict))
"electric": False,
"year": 1964,
}
DICTIONARY
type()
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
print(type(thisdict))
DICTIONARY
The dict() Constructor
print(thisdict)
DICTIONARY METHODS
Python has a set of built-in methods that you can use on dictionaries.
Method Description
clear() Removes all the elements from the dictionary
items() Returns a list containing a tuple for each key value pair
setdefault() Returns the value of the specified key. If the key does not
exist: insert the key, with the specified value
print(thisset)
SET
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and
cannot be referred to by index or key.
Unchangeable
Set items are unchangeable, meaning that we cannot change the items after
the set has been created.
SET
Duplicates Not Allowed
print(thisset)
print(thisset)
SET
Get the Length of a Set
To determine how many items a set has, use the len() function.
print(len(thisset))
set2 = {1, 5, 7, 9, 3}
type()
From Python's perspective, sets are defined as objects with the data type
'set’:
<class 'set'>
print(type(myset))
SET
The set() Constructor
print(thisset)
SET METHODS
Python has a set of built-in methods that you can use on sets.
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two
or more sets
difference_update() Removes the items in this set that are also included
in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other
sets
intersection_update() Removes the items in this set that are not present in
other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two
sets
symmetric_difference_up inserts the symmetric differences from this set and
date another
()
union() Return a set containing the union of sets