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

Session 3.ipynb

Uploaded by

Naman Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Session 3.ipynb

Uploaded by

Naman Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/20/23, 10:56 AM Session 3.

ipynb - Colaboratory

Comparison Operators

= equal to
== to check whether LHS = RHS
!= not equal to
> greater than
< Less than
>= greater than equal to
<= less than equal to

*Comparison Operator *

a=5
b=4
s= a==b
print(s)
#print(a==b)

False

#Not Equal to Operator !=


a=5
b=4
s=a!=b
print(s)

output True

#Less than to operator <


a=5
b=4
s=a<b
print(s)

False

#Greater than to operator >


a=5
b=4
s=a>b
print(s)

True

#Greater than equal to operator >=


a=5
b=4
s=a>=b
print(s)

True

#Less than equal to operator <=


a=5
b=4
s=a<=b
print(s)

False

1. WAP to take 2 input from the user ad check whther they are equal or different

x=int(input("Enter the value of x="))


y=int(input("Enter the value of y="))
if x==y:
print("Both the numbers are equal")
else:
print("Numbers are different")
print("Successfully Completed")

Enter the value of x=20


Enter the value of y=21
Numbers are different
Successfully Completed

https://colab.research.google.com/drive/1RnIqJzojDBzOyNVG4XMalzF1nTOfrxam#printMode=true 1/4
10/20/23, 10:56 AM Session 3.ipynb - Colaboratory

*2. WAP to check whether the entered number is even or odd *

number=int(input("Enter the number to check for even n odd"))


if number%2 == 0:
print("Entered number is even number")
else:
print("Entered number is odd number")

Enter the number to check for even n odd3


Entered number is odd number

3. WAP to take 2 string as input znd check whether they are equal or different

x=int(input("Enter the first string="))


y=int(input("Enter the second string="))
if x==y:
print("Both the string are equal")
else:
print("strings are different")
print("Successfully Completed")

Enter the first string=25


Enter the second string=29
strings are different
Successfully Completed

4. WAP to check if a number falls within a range

x=int(input("Enter the number="))

if 25<=x<=75:
print("Entered number is in between 25 to 75")
else:
print("Entered number is out of range")

Enter the number=95


Entered number is out of range

WAP to check if a entered character is a vowel

x=input("Enter a vowel=")

if x in 'aeiou':
print("Entered character is a vowel")
else:
print("Entered character is not a vowel")

Enter a vowel=a
Entered character is a vowel

*Assignment Operator *

x = 5
x += 3 # x is now 8
print(x)
x -= 2 # x is now 6
print(x)
x *= 4 # x is now 24
print(x)
x /= 3 # x is now 8.0
print(x)
x %= 5 # x is now 3.0
print(x)

8
6
24
8.0
3.0

https://colab.research.google.com/drive/1RnIqJzojDBzOyNVG4XMalzF1nTOfrxam#printMode=true 2/4
10/20/23, 10:56 AM Session 3.ipynb - Colaboratory

1. WAP using assignment operator to concatenate strings

text=input("Enter the 1st string")


text=input("Enter the 2ns string")
print("Concatenated Strings=",text)
# Another way
text="Hello"
text="World"
print("Concatenated String",text)
#Another Way
text1="Hello"
text2="world"
print("Concatenated strings=",text1+" "+text2)

Enter the 1st string Hello


Enter the 2ns string World
Concatenated Strings= World
Concatenated String World
Concatenated strings= Hello world

LOGICAL OPERATOR

#Logical "and" operator

x=True
y=False
if x and y:
print("Both x and y are true")
else:
print("At least one of x and y is false")

At least one of x and y is false

x=3
y=9
if x<5 and y>5:
print("Both conditions are true")
else:
print("Any one of the conditions is not true")

Both conditions are true

#Logical "or" operator


x=True
y=False
if x or y:
print("Atleast one of x and y is true")
else:
print("Both x and y are false")

Atleast one of x and y is true

x=3
y=9
if x<5 or y==5:
print("Any one of the condition is true")
else:
print("Both conditions are false")

Any one of the condition is true

#not operator
x= True
if not x:
print("x is false")
else:
print("x is true")

x is true

https://colab.research.google.com/drive/1RnIqJzojDBzOyNVG4XMalzF1nTOfrxam#printMode=true 3/4
10/20/23, 10:56 AM Session 3.ipynb - Colaboratory

WAP for validating the user input with logical operator

username="user123"
password="secure@123"
username=input("Enter the user name:")
password=input("Enter the user password:")
if username=="user123" and password=="secure@123":
print("Access Granted. Welcome User123!")
else:
print("Access Denied. Please check your name and password.")

Enter the user name:user123


Enter the user password:secure@123
Access Granted. Welcome User123!

age=16
has_permission=True
is_resident=True
if age>=18 or (has_permission and is_resident):
print("You are eligible to participate in the contest")
else:
print("You are not eligible for the contest")

You are eligible to participate in the contest

Membership operator

list1 = [1, 2, 3, 4, 5]
is_present = 3 in list1 # True
print(is_present)
is_not_present = 6 not in list1 #True
print(is_not_present)

True
True

Temporary Operator

age = 20
can_vote = "Yes" if age >= 18 else "No"
print(can_vote)

Yes

https://colab.research.google.com/drive/1RnIqJzojDBzOyNVG4XMalzF1nTOfrxam#printMode=true 4/4

You might also like