Python Data Types-Variables and Control structures
Python Data Types-Variables and Control structures
a=10 b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=10 b=1.5
print ("a=",a, "b=",b, "a**b=", a**b)
a=7.7 b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=1+2j b=4
print ("a=",a, "b=",b, "a**b=", a**b) a=12.4 b=0
print ("a=",a, "b=",b, "a**b=", a**b) print ("a=",a, "b=",b, "b**a=",
b**a)
Python Fundamentals
Python –Operators
Floor Division Operator (//)
• Floor division is also called as integer division.
Python uses // (double forward slash) symbol for
the purpose.
• Unlike the modulus or modulo which returns the
remainder, the floor division gives the quotient of
the division of operands involved.
Python Fundamentals
Python –Operators
Floor Division Operator (//)
• Example:
a=9 b=2
print ("a=",a, "b=",b, "a//b=", a//b)
a=9 b=-2
print ("a=",a, "b=",b, "a//b=", a//b)
a=10 b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
a=-10 b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
Python Fundamentals
Python –Operators
Python Fundamentals
Python –Python Comparison Operators
• Example
print ("Both operands are integer")
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
Python Fundamentals
Python - Assignment Operators
• Example
a = 10
b=5
a=a+b
print (a)
In the statement "a+=b", the two operators "+" and "=" can be
combined in a "+=" operator.
It is called as add and assign operator. In a single statement, it
performs addition of two operands "a" and "b", and result is assigned
to operand on left, i.e., "a".
Python Fundamentals
Python - Assignment Operators
• 5% if it is between 1 to 5000.
• no discount if amount<1000
Python Fundamentals
Python Control Statements
Nested if statements
• Python supports nested if statements which means we can
use a conditional if and if...else statement inside an existing if
statement.
• There may be a situation when you want to check for
additional conditions after the initial one resolves to true. In
such a situation, you can use the nested if construct.
• Additionally, within a nested if construct, you can include
an if...elif...else construct inside
another if...elif...else construct.
Python Fundamentals
Python Control Statements
Nested if statements-Syntax of Nested if Statement
Python Fundamentals
Python Control Statements
• Nested if Statement with else Condition-Syntax
Python Fundamentals
Python Control Statements
Python match-case Statement-Syntax
• Python match-case statement takes an expression and compares its value
to successive patterns given as one or more case blocks.
• Only the first pattern that matches gets executed.
• It is also possible to extract components (sequence elements or object
attributes) from the value into variables.
Python Fundamentals
Python Control Statements
Example
Hello, World!
Hello, World!
Python Fundamentals
Python –Loops
Python Fundamentals
Python –Loops
• Flowchart of a Loop
Hello, World!
Hello, World!
Python Fundamentals
Python –Loops
• Example
Python Fundamentals
Python –Nested Loops
• Syntax: Nested while loop
• Example
QUESTIONS?