0% found this document useful (0 votes)
2 views

Exp1.2

The document provides an overview of Python programming concepts, focusing on operators, expressions, decision statements, and loop control statements. It details various types of operators such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, along with their usage in Python. Additionally, it covers decision-making structures like if statements and loops, including while and for loops, along with control statements like break and continue.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exp1.2

The document provides an overview of Python programming concepts, focusing on operators, expressions, decision statements, and loop control statements. It details various types of operators such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators, along with their usage in Python. Additionally, it covers decision-making structures like if statements and loops, including while and for loops, along with control statements like break and continue.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Department of Computer Science and Engineering (CSE)

Python Programming

• Operators and Expressions


• Control Structures

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• 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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• Arithmetic Operators
• If variable a holds the value 100 and variable b holds the value 200, then

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• Comparison (Relational) Operators


• If variable a holds the value 100 and variable b holds the value 200, then

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions


• Assignment Operators
• Assigns values from right side operands to left side operand i.e. c = a + b assigns
value of a + b into c
• Python allows you to combine assignment operators with other operators known as
an augmented (or compound) assignment operator.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• 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:

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• 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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• Bitwise Operators
• Left Shift (<<)
• If we have to left shift 4 by 2 bits then
• 4<<2

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• Membership Operators
• Python’s membership operators test for membership in a sequence, such as strings,
lists or tuples.
• There are two membership operators as

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

• Identity Operators
• Identity operators compare the memory locations of two objects. There are two Identity
operators as

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Operators and Expressions

Highest precedence

Lowest precedence

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Python Programming

Decision Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

a = 20
b = 15
if a > b:
print("a is greater than b")

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

a = 15
b = 20
if a > b:
print("a is greater than b")
else:
print("b is greater than a")

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

– Syntax:
if condition1:
if condition2:
statement1
else:
statement2
else:
statement3

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

if condition:
statements
elif condition:
statements
else:
statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Decision Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Conditional Expressions

• A conditional expression evaluates an expression based on a condition.


• For example,
• the following statement assigns 1 to y if x is greater than 0, and -1 to y if x is less than or
equal to 0.

• Alternatively, as in this next example, you can use a conditional expression to achieve the
same result.
y = 1 if x > 0 else -1

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Conditional Expressions

• Conditional expressions are in a completely different style.


• The syntax is:
– expression1 if boolean-expression else expression2
or
– expression1 if condition else expression2
• The result of this conditional expression is expression1 if boolean-expression is
true; otherwise, the result is expression2.
• Conditional expressions should probably be used sparingly because they can lead to
confusion (especially if they are nested or mixed with other complicated
expressions).

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Python Programming

Loop Control Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• Python programming language provides the following types of loops to handle looping
requirements.
• while
• for
• Nested loops

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• while Loop Statements


• The syntax for the while loop is:
while test-condition:
# Loop body
Statement(s)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• while Loop

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• for loop

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• Nested Loops
• Python programming language allows the use of one loop inside another
loop.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• break and continue statements


• In Python, break and continue statements can alter the flow of a normal loop. Loops
iterate over a block of code until test expression is false, but sometimes you wish to
terminate the current iteration or even the whole loop without checking test
expression. The break and continue statements are used in these cases.
• break statement
• The break statement terminates the loop containing it.
• Control of the program flows to the statement immediately after the body of the loop.
• If break statement is inside a nested loop (loop inside another loop), break will
terminate the innermost loop.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• break statement

• Syntax of break
– break

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• The working of break statement in for loop and while loop is shown below.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• 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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

• continue statement

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Loop Control Statements

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Programs

• Program to demonstrate the use of floor division and division.


• Program to create a simple calculator that can perform basic arithmetic operations
based on user input.
• Program to find the ASCII value of the given character.
• Program to check if a number is Odd or Even.
• Program to calculate the sum of all numbers from 1 to a given number.
• Program to check whether a given year is a leap year or not.
• Program to check if a given number is an Armstrong number or not.
• Program to find largest of three numbers.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Programs

• Program to find the reverse of a given number.


• Program to count the total number of digits in a number.
• Program to print a multiplication table of a given number.
• Program to display CGPA of student based on the obtained marks.
• Program to find the factorial of a number.
• Program to print the Fibonacci series.
• Program to create different pyramid patterns of stars.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

THANKS…..

University Institute of Engineering (UIE)

You might also like