python unit-1 programs
Program 1:
Aim: Write a python program to find the
largest element among three Numbers
Program:
a=int(input("Enter
a value :"))
b=int(input("Enter
b value :"))
c=int(input("Enter
c value :"))
if((a>b)&(a>c)):
print("a is big",a)
elif(b>c):
print("b is big",b)
else:
print("c is big",c)
Output:
Program 2:
Aim: Write a python program for all prime
numbers with in range.
Program:
min=int(input("Enter
min number"))
max=int(input("Enter
max number"))
for i in
range(min,max):
count=0
for j in range(1,i+1):
if(i%j==0):
count+=1
if(count==2):
print(i)
Output:
Program 3:
Aim: Write a python program to swap two
numbers without using a temporary variable.
Program:
a,b=10,20
a,b=b,a
print(a,b)
output:
Program 4.1:
Aim: Demonstrate the following Operators
in python with examples
a Arithmetic Operators
Program:
a=10
b=5
print("a+b=",a+b)
print("a-b=",a-b)
print("a*b=",a*b)
print("a/b=",a/b)
print("a//b=",a//b)
print("a**b=",a**b)
Output:
Program 4.2:
Aim: Demonstrate the following Operators
in python with examples
b Relational Operators
Program:
a=10
b=20
print("a<b=",a<b)
print("a>b=",a>b)
print("a<=b=",a<=b)
print("a>=b=",a>=b)
print("a==b=",a==b)
print("a!=b=",a!=b)
Output:
Program 4.3:
Aim: Demonstrate the following Operator
in python with examples
c)Assignment Operators:
Program:
#1. = (Simple Assignment)
x = 10
print(x)
#2. += (Addition Assignment)
x = 10
x += 5
print(x)
#3. -= (Subtraction Assignment)
x = 10
x -= 3
print(x)
#4. *= (Multiplication Assignment)
x = 4
x *= 2
print(x)
#5. /= (Division Assignment)
x = 20
x /= 4
print(x)
#6. //= (Floor Division Assignment)
x = 20
x //= 3
print(x)
#7. %= (Modulus Assignment)
x = 2
x **= 3
print(x)
#9. &=, |=, ^=, <<=,
>>= (Bitwise Assignment Operators)
x = 10
x &=
4
print(x)
x = 10
x |= 4
print(x)
x = 10
x ^= 4
print(x)
x = 10
x <<=
2
print(x)
x = 10
x >>=
2
print(x)
Output:
Program 4.4:
Aim: Demonstrate the following Operator
in python with examples
d)Logical Operators:
Program:
x = 5
y = 10
# Both conditions must be true for
the result to be true
result = (x
> 3) and (y < 20)
print(result)
result = (x
> 3) and (y > 20)
print(result)
# At least one condition must be true
for the result to be true
result = (x
> 3) or (y > 20)
print(result)
result = (x
< 3) or (y > 20)
print(result)
x = 5
# The not operator reverses the
result
result = not
(x > 3)
print(result)
result = not
(x < 3)
print(result)
Output:
Program 4.5:
Aim: Demonstrate the following Operator
in python with examples
e)Bit Wise Operators:
Program:
a=5
b=3
print("a&b=",a&b)
print("a|b=",a|b)
print("a^b=",a^b)
print("~a",~a)
print("a<<1=",a<<1)
print("a>>1=",a>>1)
Output:
Program 4.6:
Aim: Demonstrate the following Operator
in python with examples
f)Ternary Operators:
Program:
#Basic Ternary Operator
x = 10
result =
"Even" if x % 2 == 0 else "Odd"
print(result)
#Using Ternary Operator to Assign a Value
age = 18
status =
"Adult" if age >= 18 else "Minor"
print(status)
#Nested Ternary Operator
x = 5
y = 10
result =
"x is greater" if x > y else "x is equal to y" if x == y
else "x is less"
print(result)
# Using Ternary Operator with
Functions
def check_number(num):
return "Positive" if num > 0
else "Negative" if num < 0 else "Zero"
print(check_number(10))
print(check_number(-5))
print(check_number(0))
#Assigning Multiple Values
x = 15
y = 10
a, b =
("x is greater", "y is greater") if x > y else ("y
is greater", "x is greater")
print(a,
b)
Output:
Program 4.7:
Aim: Demonstrate the following Operator
in python with examples
g)Membership Operators:
Program:
#Using in
Operator
fruits =
["apple", "banana", "cherry"]
print("banana"
in fruits)
print("orange"
in fruits)
#Using not
in Operator
fruits =
["apple", "banana", "cherry"]
print("orange"
not in fruits)
print("banana"
not in fruits)
Output:
Program 4.8:
Aim: Demonstrate the following Operator
in python with examples
h)Identity Operators:
Program:
#Using is Operator
a = 5
b = 5
print(a is
b)
#Using is not Operator
a = 10
b = 20
print(a is
not b)
Output:
Program 5:
Aim: Write a python program to add and
multiply complex numbers
Program:
n1=complex(3,4)
n2=complex(1,2)
addr=n1+n2
print("Addcomplex+",addr)
mulr=n1*n2
print("Multiplycomplex+",mulr)
Output:
Program 6:
Aim: Write a python program to print
multiplication table of a given number
Program:
num =
int(input("Enter a number: "))
for i in
range(1, 11):
print(f"{num} x {i} = {num * i}")
Output:
Comments
Post a Comment