Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

C 1 UNIT CH3 Operators in Python

Here are the steps to check if a character is uppercase or lowercase and convert it to uppercase: 1. Take input from the user as a character: char = input("Enter a character: ") 2. Check if the character code is between 65-90 (ASCII code for uppercase characters): if ord(char) >= 65 and ord(char) <= 90: print(char "is uppercase") 3. Else it must be lowercase: else: print(char "is lowercase") 4. Convert it to uppercase using upper() method: print("Converted to uppercase: ", char.upper()) So the full code would be: char

Uploaded by

Nippun Bobbili
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C 1 UNIT CH3 Operators in Python

Here are the steps to check if a character is uppercase or lowercase and convert it to uppercase: 1. Take input from the user as a character: char = input("Enter a character: ") 2. Check if the character code is between 65-90 (ASCII code for uppercase characters): if ord(char) >= 65 and ord(char) <= 90: print(char "is uppercase") 3. Else it must be lowercase: else: print(char "is lowercase") 4. Convert it to uppercase using upper() method: print("Converted to uppercase: ", char.upper()) So the full code would be: char

Uploaded by

Nippun Bobbili
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Operators in python

int213
Operators and Operands
• Operators are special symbols that represent computations like addition
and multiplication.
• The values the operator uses are called operands
• When both of the operands are integers, the result must also be an
integer, and by convention, integer division always rounds down.
• +, -, *, /, %, **,//
• ** is used for exponential.
• / is also used for float division
• // is used to integer division (only selects left part i.e. int part )
>>> 2/3
0.6666666666666666
>>> 2.0/3
0.6666666666666666
>>> 2.0/3.0
0.6666666666666666
>>> 20//3 What are division operators in
6 Python?
>>> 2//3
In Python programming, you can
0 perform division in two ways.
>>> 2.0//3
0.0 1. Float Division("/")
>>> 2.0//3.0 and
0.0 2. Floor Division or Integer
Division("//")
>>> 2.0//10
0.0 5/2 = 2.5 (right or wrong)
>>> 2.0//10.0
Order of Operations
• When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence.
• Parentheses have the highest precedence and can be used to force
an expression to evaluate in the order you want.
• Exponentiation has the next highest precedence.
• Multiplication ,Float Division, Integer Division and remainder have
the same precedence, which is higher than Addition and Subtraction,
which also have the same precedence.
• Operators with the same precedence are evaluated from left to
right.
2 * (3-1)
so sol is 4 or 5 ?

(1+1)**(5-2)
sol is what ?
8
True
False

print((3 ** 2) ** 2)
result 81
or
18

a + b * c is treated as a + (b * c) ,
a * b + c is treated as (a * b) + c .
NOTE:
print(2 ** 3 ** 2) multiplication and division have the same precedence,
ans= 512 and the same goes for addition and subtraction.
## the right-most ** operator gets done first!
if Division operator appears before Multiplication, division goes first.
Arithmentic Operators
Operations on Strings
• Mathematical operations can’t be performed on strings, even if the
strings look like numbers.
• the + operator represents concatenation. s1="Hello"
• For Example: s2="Python"
• fruit = "banana"
• bakedGood = " nut bread“
print(s1+s2)
• print (fruit + bakedGood)

• The * operator also works on strings; it performs repetition.


• For eg:
• "Fun"*3 is "FunFunFun"
The modulus operator
• The modulus operator works on integers (and integer expressions)
and yields the remainder when the first operand is divided by the
second.
• In Python, the modulus operator is a percent sign (%).
Example
• The syntax is the same as for other operators:
>>> quotient = 7 / /3
>>> print (quotient)
2
>>> remainder = 7 % 3
>>> print (remainder)
1
• So 7 divided by 3 is 2 with 1 left over.
Uses
• Check whether one number is divisible by another if x % y is zero,
then x is divisible by y.
• you can extract the right-most digit or digits from a number.
• For example,
x % 10 yields the right-most digit of x (in base 10). Similarly x % 100
yields the last two digits.
Boolean expressions
• A Boolean expression is an expression that is either true or false.
• One way to write a Boolean expression is to use the operator ==,
which compares two values and produces a Boolean value:
>>> 5 == 5
True
>>> 5 == 6
False
• True and False are special values that are built into Python.
a=3
Comparison Operators b=6
• x != y x is not equal to y
print(a<=b)
• x>y # x is greater than y
Result is:
• x<y # x is less than y True
• x >= y # x is greater than or equal to y
• x <= y # x is less than or equal to y
NOTE: “= is an assignment operator and == is a
comparison operator”. Also, there is no such thing as =<
or =>.
Logical operators
• There are three logical operators:
 and,
 or
 not
• For example, x > 0 and x < 10 is true only if x is greater than 0 and less
than 10.
• n%2 == 0 or n%3 == 0
• not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
Identity operators
• Identity operators compare the memory locations of two objects.
There are two Identity operators as explained below
Bitwise Operators
Continue…
• Any nonzero number is interpreted as “true."
>>> x = 5
>>> x and 1
1
>>> y = 0
>>> y and 1
0
Membership
Operators

a = 10
b = 20
x = [1, 2, 3, 4, 5, 20, 30, 40, 21 ] #list
print(a in x) # False
print(b in x) #True
Keyboard Input
• input(): built in function to get data from keyboard.

•Takes data in the form of string.
• Eg:
>>> x = input ()

What are you waiting for?

>>> print (x)

What are you waiting for?

• Before calling input, it is a good idea to print a message telling the user what to input.
This message is called a prompt.
• A prompt can be supplied as an argument to input.
• Eg:
>>> name = input ("What...is your name? ")

What...is your name? Arthur, King of the Britons!


>>> print(name)
Arthur, King of the Britons!

• If we expect the response to be an integer, then type conversion needs


to be done.
• Eg:
prompt = "What is the airspeed velocity of an unladen swallow?"
speed =int(input(prompt))
Composition
• One of the most useful features of programming languages is their ability
to take small building blocks and compose them. uses Has-a relationship.

Comments
• It is a good idea to add notes to your programs to explain in natural
language what the program is doing. These notes are called comments.
• they are marked with the # symbol:
•For eg:
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
Precedence of
Operators

2*5 >= 10 and 3-6 <=


20
True
QUIZ
What is the output of the expression  print(-18 // 5)

a) -4
b) 4
c) -5
d) 5
print(14//3)
4
print(-14//3)
-5
In the case of floor division operator(//), when the result is
negative, the result is rounded down to the next smallest (big
negative) integer.
Logical Operators USE in Python
example:
x=3

print (x > 3 and x < 10)

# returns False

example:
x=5
print (x > 3 and x < 10)

# returns True because 5 is greater than 3 AND 5 is less than 10

Do exercise for or and not


x=5

print(not (x > 3 and x < 10) )


What is the output of the following code
x = 2000
y = 400
print(x and y)

a) True
b) False
c) 2000
d) 400
• if 'and' is used,
• the second expression must also be evaluated if the first is True.
• Note that, if the first expression is evaluated to be true the return value is
always the result of the second expression.
• If the first expression is evaluated to be False, then the result returned is the
result of the first expression.
• In ‘or’,
• if first expression is true then it will result into first value only and it will not
evaluate second value (property of ‘or’).
What is the output of print(2 * 3 ** 3 * 4)
a) 216
b) 566
c) 872
d) 864
In the Python statement a = x + 15 - y:
x and y are ________
x + 15 - y is ________

a) terms, a group
b) operands, an equation
c) operators, a statement
d) operands, an expression
Which of the following operators has the
highest precedence?
a) Not
b) and
c) or
• x=int(input("enter something"))
• print(x *4)
• o/p: enter something2
•8
• o/p: enter somethinga
• aaaa
• #FIND an entered Character is in Upper CASE or LOWER
• x=input("enter a character")
• if (x>='A' and x<='Z'):
• print(x, "is CAPITAL CASE")
• else:
• print(x, "is in LOWER CASE")
• y=x.upper()
• print("NOW it is converted in Upper CASE ehich is-->>", y)

You might also like