Python Practical
Python Practical
code:
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=90
b=58
print("the gcd of 90 and 58 is:",gcd(a,b))
output:
the gcd of 90 and 58 is: 2
N,X = 2,3
print(calculate_power(N,X))
N,X = 3,4
print(calculate_power(N,X))
output:
8
81
n=10
print(f"fibonacci number at position {n}:{fibonacci_recursive(n)}")
output:
fibonacci number at position 10:55
n=5
print(f"Factorial of {n}: {factorial_iterative(n)}")
output:
Factorial of 5: 120
print("Select an operation:")
print("1.Addition(+)")
print("2.Subtraction(-)")
print("3.Multiplication(*)")
print("4.Division(/)")
if choice == "1":
result = num1+num2
print(f"{num1} + {num2} = {result}")
else:
print("invalid input please enter a valid operation.")
output:
Simple Calculator
Select an operation:
1.Addition(+)
2.Subtraction(-)
3.Multiplication(*)
4.Division(/)
Enter your choice (1/2/3/4) : 4
Enter the first number:10
Enter the second number:2
10.0 / 2.0 = 5.0
num1,num2 = swap_numbers(num1,num2)
print("after swapping: num1= ",num1,"num2= ",num2)
output:
before swapping: num1= 5 num2= 10
after swapping: num1= 10 num2= 5
input_string = "abc"
print("All substrings of the given string are:")
print_substrings(input_string)
output:
All substrings of the given string are:
a
ab
abc
b
bc
c
output:
total argumrnts passed: 1
inName of python script: c:\users\khushi\.spyder-py3\temp.py
in arguments passed:
for w in line_word:
words.append(w);
for j in range(i+1,len(words)):
if(words[i] == words[j]):
count = count+1;
if(count>frequency):
frequency = count;
frequency_word = words[i];
output:
Most repeated word: Well frequency: 3
dataBase = mysql.connector.connect(
host="localhost",
user="user",
passwd="password"
)
cursorObject = dataBase.cursor()
width = 1000
height = 600
red = (255, 0, 0)
black = (0, 0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill(black)
ball_pos[0] += ball_speed[0]
ball_pos[1] += ball_speed[1]