Exp1.2
Exp1.2
Python Programming
• Operators are the constructs, which can manipulate the value of operands.
• Consider the expression 4 + 5 = 9.
• Here, 4 and 5 are called operands and + is called the operator.
• Types of Operator
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
• Arithmetic Operators
• If variable a holds the value 100 and variable b holds the value 200, then
• Logical Operators
• AND (&&)
• OR (||)
• NOT(Used to reverse the logical state of its operand) (!)
• Assume variable e1 holds True/False and variable e2 holds True/False then truth table
of logical operators:
• Bitwise Operators
• Bitwise operator works on bits and performs bit-by-bit operation.
• 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.
• Bitwise Operators
• Python supports two bitwise shift operators. They are shift left (<<) and shift right (>>).
These operations are used to shift bits to the left or to the right.
• Right Shift (>>)
• If we have to right shift 4 by 2 bits then
• 4>>2
• Bitwise Operators
• Left Shift (<<)
• If we have to left shift 4 by 2 bits then
• 4<<2
• Membership Operators
• Python’s membership operators test for membership in a sequence, such as strings,
lists or tuples.
• There are two membership operators as
• Identity Operators
• Identity operators compare the memory locations of two objects. There are two Identity
operators as
Highest precedence
Lowest precedence
Python Programming
Decision Statements
Decision Statements
• if statement
• An if statement consists of a Boolean expression followed by one or more
statements.
– Syntax:
if condition:
statements
• The body of the if statement is indicated by the indentation. Body starts with an
indentation and the first unindented line marks the end.
• Python interprets non-zero values as True.
• None and 0 are interpreted as False.
Decision Statements
a = 20
b = 15
if a > b:
print("a is greater than b")
Decision Statements
• if-else statement
• The if-else statement in python, is similar to the if statement, but in this case, there is an
else statement available to execute the statement in case if the expression evaluates to
false.
• Indentation is used to separate the blocks.
– Syntax:
if condition:
statements
else:
statements
Decision Statements
a = 15
b = 20
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
Decision Statements
• Nested if statements
• One if statement can be placed inside another if statement to form a nested if statement.
• The statement in an if or if-else statement can be any legal Python statement, including
another if or if-else statement.
• The inner if statement is said to be nested inside the outer if statement. The inner if
statement can contain another if statement; in fact, there is no limit to the depth of the
nesting.
• The nested if statement can be used to implement multiple alternatives
Decision Statements
– Syntax:
if condition1:
if condition2:
statement1
else:
statement2
else:
statement3
Decision Statements
Decision Statements
• if-elif-else statements
• The multi-way if statements uses the syntax if-elif-else; elif (short for else if ) is a
Python keyword
• If the condition for if is False, it checks the condition of the next elif block and so on. If
all the conditions are False, body of else is executed.
• Only one block among the several if...elif...else blocks is executed according to the
condition.
• The if block can have only one else block. But it can have multiple elif blocks.
Decision Statements
if condition:
statements
elif condition:
statements
else:
statements
Decision Statements
Conditional Expressions
• Alternatively, as in this next example, you can use a conditional expression to achieve the
same result.
y = 1 if x > 0 else -1
Conditional Expressions
Python Programming
• Python programming language provides the following types of loops to handle looping
requirements.
• while
• for
• Nested loops
• while Loop
• range( ) function
• The built-in function range() is the function to iterate over a sequence of numbers.
• It generates an iterator of arithmetic progressions.
• range() generates an iterator to progress integers starting with 0 upto n-1.
• The general form of the range function call is
– range( begin,end,step )
• Where begin is the first value in the range, if omitted, the default value is 0
• end is one past the last value in the range, the end value may not be omitted
• Step is change in the amount to increment or decrement, if the parameter is omitted, it
defaults to 1 (counts up by ones)
• begin, end, and step must all be integer values; floating-point values and other types
are not allowed.
• for loop
• The for statement in Python has the ability to iterate over the items of any sequence, such
as a list or a string.
• It repeats a set of statements over a group of values.
– Syntax:
for variableName in groupOfValues:
statements
• Indent the statements to be repeated with tabs or spaces.
• variableName gives a name to each value, so you can refer to it in the statements.
• groupOfValues can be a range of integers, specified with the range function.
• for loop
• Nested Loops
• Python programming language allows the use of one loop inside another
loop.
• break statement
• Syntax of break
– break
• The working of break statement in for loop and while loop is shown below.
• continue statement
• The continue statement is used to skip the rest of the code inside a loop for the
current iteration only. Loop does not terminate but continues on with the next
iteration.
• The continue statement rejects all the remaining statements in the current iteration of
the loop and moves the control back to the top of the loop.
• Syntax of Continue
– continue
• continue statement
Programs
Programs
THANKS…..