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

Python Practical

Uploaded by

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

Python Practical

Uploaded by

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

1) use print in gcd to produce the outputs.

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

=== Code Execution Successful ===

2) use print in exponentiation(power) to produce the outputs.


code:
def calculate_power(N,X):
P=1
for i in range(1,X+1):
P=P*N
return P

N,X = 2,3
print(calculate_power(N,X))
N,X = 3,4
print(calculate_power(N,X))

output:
8
81

=== Code Execution Successful ===

3) use print fibonacci series in python to produce output.


code:
def fibonacci_recursive(n):
if n<=1:
return n
else:
return fibonacci_recursive(n-1)+fibonacci_recursive(n-2)

n=10
print(f"fibonacci number at position {n}:{fibonacci_recursive(n)}")

output:
fibonacci number at position 10:55

=== Code Execution Successful ===

4) use print factorial of non-negative integer in python to produce output.


code:
def factorial_iterative(n):
if n<0:
raise("inputt must be non-negative integer")
result = 1
for i in range(2,n+1):
result*=i
return result

n=5
print(f"Factorial of {n}: {factorial_iterative(n)}")

output:
Factorial of 5: 120

=== Code Execution Successful ===

5) write a python program for simple caclulator.


code:
print("Simple Calculator")

print("Select an operation:")
print("1.Addition(+)")
print("2.Subtraction(-)")
print("3.Multiplication(*)")
print("4.Division(/)")

choice=input("Enter your choice (1/2/3/4) : ")

num1=float(input("Enter the first number:"))


num2=float(input("Enter the second number:"))

if choice == "1":
result = num1+num2
print(f"{num1} + {num2} = {result}")

elif choice == "2":


result = num1-num2
print(f"{num1} - {num2} = {result}")

elif choice == "3":


result = num1*num2
print(f"{num1} * {num2} = {result}")

elif choice == "4":


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

=== Code Execution Successful ===


6) to write a python program to perform swapping of two numbers.
code:
def swap_numbers(a,b):
a,b = b,a
return a,b
num1 = 5
num2 = 10

print("before swapping: num1= ",num1,"num2= ",num2)

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

=== Code Execution Successful ===

7) write a python program to print all substrings of a given string.


code:
def print_substrings(string):
length = len(string)
for i in range(length):
for j in range(i + 1, length + 1):
print(string[i:j])

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

=== Code Execution Successful ===

8) use print command line arguments in python to produce output.


code:
import sys
n=len(sys.argv)
print("total argumrnts passed: ",n)
print("inName of python script: ",sys.argv[0])
print("in arguments passed: ",end=" ")
for i in range(1,n):
print(sys.argv[i],end=" ")
sum = 0
for i in range(1,n):
sum+=int(sys.argv[i])
print("\nresult: ",sum)

output:
total argumrnts passed: 1
inName of python script: c:\users\khushi\.spyder-py3\temp.py
in arguments passed:

9) use print most frequent words in a text read date in python.


code:
file = open("gfg.text","r")
frequent_word = " "
frequency = 0
words = []

for linr in file:


line_word = line.lower().replace(',','').replace('.','').split(" ");

for w in line_word:
words.append(w);

for i in range(0, len(words)):


count = 1;

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];

print("most repeated word: "+frequency_word)


print("frequency: "+str(frequency))
file.close();

output:
Most repeated word: Well frequency: 3

10) use print stimulate python mysql.


code:
import mysql.connector

dataBase = mysql.connector.connect(
host="localhost",
user="user",
passwd="password"
)

cursorObject = dataBase.cursor()

cursorObject.execute("CREATE DATABASE test_database")

print("Database created successfully!")

11) use print simulate bouncing ball in python to produce output.


code:
import pygame
pygame.init()

width = 1000
height = 600

screen_res = (width, height)


pygame.display.set_caption("GFG bouncing game")
screen = pygame.display.set_mode(screen_res)

red = (255, 0, 0)
black = (0, 0, 0)

ball_pos = [100, 100]


ball_speed = [1, 1]
ball_radius = 40

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]

if ball_pos[0] - ball_radius <= 0 or ball_pos[0] + ball_radius >= width:


ball_speed[0] = -ball_speed[0]
if ball_pos[1] - ball_radius <= 0 or ball_pos[1] + ball_radius >= height:
ball_speed[1] = -ball_speed[1]

pygame.draw.circle(surface=screen, color=red, center=ball_pos,


radius=ball_radius)
pygame.display.flip()

You might also like