day3
day3
bin()
Python Operators
• True, False : True and False are truth values in Python. They
are the results of comparison operations or logical (Boolean)
operations in Python.
• True and False in python is same as 1 and 0
Logical operators
Logical operators
Logical operators
Assignment operators
x1 5
y1
x3
y3
Membership operators
• in and not in are the membership operators in Python.
• They are used to test whether a value or variable is found in a
sequence (string, list, tuple and dictionary).
• In a dictionary we can only test for presence of key, not the value.
Membership operators
Order Of Operation
• First level of precedence: top to bottom
• Second level of precedence
– If there are multiple operations that are on the same level then
precedence goes from left to right.
** Exponent
+, - Addition, subtraction
Python Operators Precedence
Operators from highest precedence to lowest
Highest
lowest
Python Operators Precedence
Python Lists:
• The most basic data structure in Python is the sequence.
• Each element of a sequence is assigned a number - its
position or index.
• The first index is zero, the second index is one, and so forth.
• Most common sequences are lists and tuples.
• There are certain things you can do with all sequence types.
• These operations include indexing, slicing, adding,
multiplying, and checking for membership.
• In addition, Python has built-in functions for finding the
length of a sequence and for finding its largest and smallest
elements.
Python Lists:
• To access values in lists, use the square brackets for slicing along
with the index or indices to obtain value available at that index
Accessing Values in Lists
0 1
0 1 23 4 0 1 2 3
Updating Lists
• You can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator, and you
can add to elements in a list with the append() method.
Python List append() Method
• The method append() appends a passed obj into the existing list.
Delete List Elements
• To remove a list element, you can use either the del statement if
you know exactly which element(s) you are deleting or the
remove() method if you do not know
Python List remove() Method
Basic List Operations
• max(): The method max returns the elements from the list with maximum
value.
• Note: in Python3.x version max is used only for same kind of values at a time
Built-in List Functions & Methods:
• min() : The method min() returns the elements from the list with minimum value.
• Note: in Python3.x version min is used only for same kind of values at a time
Built-in List Functions & Methods:
• insert() : The method insert() inserts object obj into list at offset
index.
• Syntax:
Built-in List Functions & Methods:
• Tuples and list look quite similar except the fact that one is
immutable and the other is mutable.
• We generally use tuple for heterogeneous (different)
datatypes and list for homogeneous (similar) datatypes.
• There are some advantages of implementing a tuple than a
list. Here are a few of them: