Problem Solving
1.# Swapping without using 3rd variable
a = float(input("Enter 1st data: "))
b = float(input("Enter 2nd data: "))
a = a+b
b = a-b
a = a-b
print(a)
print(b)
OUTPUT:
Enter 1st data: 6
Enter 2nd data: 2
2.0
6.0
2.# Simple Arithmetic Calculator
operator = input("Select Operator +, -, *, /, %, **: ")
a = float(input("Enter 1st data: "))
b = float(input("Enter 2nd data: "))
if operator == "+":
print("Addition: ", float(a+b))
elif operator == "-":
print("Subtraction: ", float(a-b))
elif operator == "*":
print("Multiplication: ", float(a*b))
elif operator == "/":
print("Division: ", float(a/b))
elif operator == "%":
print("Modulus: ", float(a%b))
elif operator == "**":
print("Exponentiation: ", float(a**b))
else:
print("Please Select valid operator!")
OUTPUT:
Select Operator +, -, *, /, %, **: +
Enter 1st data: 8
Enter 2nd data: 0
Addition: 8.0
Select Operator +, -, *, /, %, **: -
Enter 1st data: 9
Enter 2nd data: 7
Subtraction: 2.0
Enter 1st data: 5
Enter 2nd data: 8
Select Operator +, -, *, /, %, **: *
Multiplication: 40
Select Operator +, -, *, /, %, **: /
Enter 1st data: 8
Enter 2nd data: 4
Division: 2.0
Select Operator +, -, *, /, %, **: %
Enter 1st data: 6
Enter 2nd data: 4
Modulus: 2.0
Select Operator +, -, *, /, %, **: **
Enter 1st data: 7
Enter 2nd data: 2
Exponentiation: 49.0
3.Write a Python program which iterates the integers from 1 to 50.For multiples
of three print "Fizz" instead of the number and for the multiples of five print
"Buzz".For numbers which are multiples of both three and five print "FizzBuzz".
for fizzbuzz in range(51):
if (fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0):
print("fizzbuzz")
continue
elif fizzbuzz % 3 == 0:
print("fizz")
continue
elif fizzbuzz % 5 == 0:
print("buzz")
continue
else:
print(fizzbuzz)
OUTPUT:
fizzbuzz
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
Buzz
4.# Odd or Even
n = int(input("Enter data: "))
if (n %2)==0:
print(n, " is Even")
else:
print(n, " is Odd")
OUTPUT:
Enter data: 8
8 is Even
Enter data: 9
9 is Odd
5. Python: Insert a given(emp) string at the beginning of all items in a list
n=[1,2,3,4]
print(['emp'+ str(i) for i in n])
OUTPUT:
['emp1', 'emp2', 'emp3', 'emp4']
6.# Fibonacci Series
n = int(input("Enter the number: "))
a=0
b=1
print(a)
print(b)
for i in range(n-2):
c=a+b
a=b
b=c
print(c)
OUTPUT:
Enter the number: 6
0
1
5
7.# Palindrome Number
n = int(input("Enter the number: "))
temp = n
sum = 0
while temp>0:
a = temp % 10
sum = sum*10 + a
temp = temp//10
if sum == n:
print(n, " is Palindrome number")
else:
print(n, " is not a Palindrome Number")
OUTPUT:
Enter the number: 593
593 is not a Palindrome Number
Enter the number: 59395
59395 is Palindrome number
8.#Remove duplicate element from list
str1 = input("Enter the data: ")
a = list(str1)
b = []
for i in a:
if i not in b:
b.append(i)
print((b))
OUTPUT:
Enter the data: 6 2 8 6 1
['6', ' ', '2', '8', '1']
9.#Factorial Using Function
def factorial(n):
if n==1:
return n
else:
return ((n)*factorial(n-1))
n = int(input("Enter the number: "))
F = factorial(n)
for i in range(1,n+1):
print(factorial(i))
print("Factorial of ",n," is ",F)
OUTPUT:
Enter the number: 5
1
2
6
24
120
Factorial of 5 is 120
10 .panlindrome using function.
def itr(itr):
return s==s[::-1]
s="eyoe";
ans=itr(s);
if ans:
print("it is palindrome")
else:
print("it is not palindrome")
OUTPUT:
it is not palindrome
it is palindrome