[go: up one dir, main page]

0% found this document useful (0 votes)
4 views4 pages

Edap Internal

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)
4 views4 pages

Edap Internal

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

Experiment 1: Experiment 5

Aim: To write a Python program to find the exponentiation of a number Write a Python program to find first n prime numbers.
Write a Python program to find GCD of two numbers. Program: Aim: To write a Python program to find first n prime
Aim: To write a Python program to find GCD of two numbers. def exponentiation(base,exponent): numbers
Program: result=1 Program:
def gcd(a,b): for _ in range(exponent): def isPrime(num):
while b!=0: result *= base if num <=1:
a,b=b,a%b return result return False
return a base=float(input("Enter the base number:")) for i in range(2,int(num**0.5)+1):
n1=int(input("Enter the first number")) exponent = int(input("Enter the exponent:")) if num%i==0:
n2=int(input("Enter the second number")) result = exponentiation(base,exponent) return False
result=gcd(n1,n2) print("Result of",base,"raised to the power of",exponent,"is",result) return True
print("the gcd of two numbers is:",result) Output: def printPrime(n):
Output: Enter the base number:2 count=0
Enter the first number36 Enter the exponent:3 num=2
Enter the second number48 Result of 2.0 raised to the power of 3 is 8.0 while count<n:
if isPrime(num):
the gcd of two numbers is: 12
Experiment 4 print(num,end=" ")
count += 1
Experiment 2:
Write a Python Program to find the maximum from a list num+=1
of numbers n=int(input("Enter the value of n:"))
Write a Python Program to find the square root of a number by Newton’s
Aim: To write a Python Program to find the maximum print("First",n,"prime numbers are:")
Method printPrime(n)
Aim: To write a Python Program to find the square root of a number by from a list of numbers.
Program: Output:
Newton’s Method. Enter the value of n:10
Program: def findMax(numbers):
if not numbers: First 10 prime numbers are:
def newtonSqrt(n): 2 3 5 7 11 13 17 19 23 29
return None
guess = n/2
max_num = numbers[0]
while True: Experiment 6
for num in numbers:
new_guess=0.5*(guess+n/guess) Program to find all prime numbers within a given x
if num>max_num:
if abs(new_guess-guess)<1e-9: number.
max_num=num
return new_guess Aim: To write a Python program to find all prime
return max_num
guess=new_guess num_list=[5,3,9,1,7,2] numbers within a given range
n=float(input("Enter a number:")) print(“list is:”, num_list) Program:
sqrt=newtonSqrt(n) maximum=findMax(num_list) def isPrime(num):
print("Square root of",n,"is approximately:",sqrt) print("Maximum number in the list:",maximum) if num <=1:
Output: Output: return False
Enter a number:25 list is: [5, 3, 9, 1, 7, 2] for i in range(2,int(num**0.5)+1):
Square root of 25.0 is approximately: 5.0 Maximum number in the list: 9 if num%i==0:
return False
Experiment 3: return True

def printPrime(start,end): Experiment 8 Slice 7: -> Python!


print("Prime numbers Write a Python program demonstrate use of slicing in string. Slice -7:-1 -> Python
between",start,"and",end,"are:") Aim: To demonstrate use of slicing in string. Slice 0:10:2 -> Hlo y
for num in range(start,end+1): Description: Like other programming languages, it’s possible to access Reversed string -> !nohtyP ,olleH
if isPrime(num): individual characters
print(num,end=" ") of a string by using array-like indexing syntax. In this we can access each Experiment 9
start = int(input("Enter the starting value of the and every element Aim: Python program that accepts a sequence of whitespace separated
range:")) of string through their index number and the indexing starts from 0. Python words as input and prin
end = int(input("Enter the ending value fo the range:")) does index out of ts the words after removing all duplicate words and sorting them
printPrime(start,end) bound checking. So, we can obtain the required character alphanumerically.
Output: usingsyntax,string_name[index_position]: The positive index_position Program:
Enter the starting value of the range:10 denotes the element s = input()
Enter the ending value fo the range:30 from the starting(0) and the negative index shows the index from the end(- words = [word for word in s.split(" ")]
Prime numbers between 10 and 30 are: 1). print (" ".join(sorted(list(set(words)))))
11 13 17 19 23 29 Program: Output:
def demonstrate_string_slicing(): hello world and hello 123 hello 34ab
Experiment 7
text = "Hello, Python!" 123 34ab and hello world
Python program to print ‘n terms of Fibonacci series using iteration
# Basic slicing
Aim: Python program to print ‘n terms of Fibonacci series using iteration
# Extract characters from index 0 to index 4 Experiment 10
Program:
slice1 = text[0:5] Write a Python program to implement
def fibonacci(n):
print("Slice 0:5 ->", slice1) # Outputs 'Hello' stack using list. Aim: To implement stack
fibSeries=[0,1]
# Slicing with a start and end using list Description:
for i in range(2,n):
# Extract characters from index 7 to the end of the string Stack is a linear data structure which follows a particular order in which the
nextTerm=fibSeries[-1]+fibSeries[-2]
slice2 = text[7:] operations are performed. The order may be LIFO(Last In First Out) or
fibSeries.append(nextTerm) FILO(First In Last Out). Mainly the following three basic operations are
print("Slice 7: ->", slice2) # Outputs 'Python!'
return fibSeries[:n] performed in the stack:
# Slicing with negative indices
n=int(input("Enter the number of terms in the Fibonacci Series:")) • Push: Adds an item in the stack. If the stack is full, then it is said to be
# Extract characters from index -7 to -1
print("The first",n,"terms of the fibonacci series are:",fibonacci(n)) an Overflow condition.
slice3 = text[-7:-1]
Output: • Pop: Removes an item from the stack. The items are popped in the
print("Slice -7:-1 ->", slice3) # Outputs 'Python'
Enter the number of terms in the Fibonacci Series:7 reversed order in which they are pushed. If the stack is empty, then it
# Slicing with steps
The first 7 terms of the fibonacci series are: [0, 1, 1, 2, 3, 5, 8] is said to be an Underflow condition.
# Extract every second character between index 0 and index 10
• Peek or Top: Returns top element of stack.
slice4 = text[0:10:2]
• isEmpty: Returns true if stack is empty, else false. How to understand
print("Slice 0:10:2 ->", slice4) # Outputs 'Hlo y'
# Using negative step to reverse the string a stack practically? There are many real life examples of stack.
Consider the simple example of plates stacked over one another in
reverse = text[::-1]
canteen. The plate which is at the top is the first one to be removed,
print("Reversed string ->", reverse) # Outputs '!nohtyP ,olleH'
i.e. the plate which has been placed at the bottommost position
if _name_ == "_main_":
remains in the stack for the longest period of time. LIFO/FILO order.
demonstrate_string_slicing()
• Program:
Output:
Slice 0:5 -> Hello
# using list Experiment 12
stack = []
# append() function to Experiment 11 Various types of turtle programs using python Program
push # element in the Write a Python program to implement Aim: program to create a rotating ninja
stack stack.append('a') queue using list. Aim: To implement queue blade
stack.append('b') using list Program:
stack.append('c') Description: import turtle
print('Initial stack') In a Queue data structure, we maintain two pointers, front and rear. The # Create a turtle object named ninja
print(stack) front points the first item of queue and rear points to last item. enQueue() ninja = turtle.Turtle()
# pop() fucntion to pop This operation adds a new node after rear and moves rear to the next node.
# element from deQueue() This operation removes the front node and moves front to the next # Set the drawing speed
stack in # LIFO node. ninja.speed(10)
order
print('\nElements poped from # Loop to create the rotating effect
stack:') print(stack.pop()) for i in range(180): # Rotate the blade
print(stack.pop()) 180 times ninja.forward(100) # Move
print(stack.pop()) Program: forward ninja.right(30) # Turn right
print('\nStack after elements are from collections import deque by 30 degrees ninja.forward(20) #
poped:') print(stack) Move forward
Output: queue = deque(["Ram", "Tarun", ninja.left(60) # Turn left by 60
Initial stack "Asif", "John"]) degrees
['a', 'b', 'c'] print(queue) queue.append("Ak bar") ninja.forward(50) #Move forward
print(queue) queue.append("Bir bal") ninja.right(30) # Turn right by 30
Elements poped from stack: print(queue) print(queue.popleft()) degrees ninja.penup() # Don't draw
c print(queue.popleft()) print(queue) while moving to the center
b Output: ninja.setposition(0, 0) # Move to the
a deque(['Ram', 'Tarun', 'Asif', 'John']) center ninja.pendown() # Start
deque(['Ram', 'Tarun', 'Asif', 'John', drawing again
Stack after elements are 'Ak bar'])
poped: [] deque(['Ram', 'Tarun', 'Asif', 'John', 'Ak bar', 'Bir ninja.right(2) # Small right turn to create the rotating blade effect
bal']) RamTarun # Finish drawing
deque(['Asif', 'John', 'Ak bar', 'Bir turtle.done()
bal'])

Experiment 14
Drawing random dots in
order Aim: To draw a random
dots in order Program:
import turtle

bob =
turtle.Turtle()
dot_distance =
25 width = 5
height = 7
bob.penup()

for y in range(height): # Loop over the


height for i in range(width): # Loop
Experiment 13 over the width
Drawing a rainbow bob.dot() # Place a dot
hexagon Aim: To draw a bob.forward(dot_distance) # Move forward by dot_distance
rainbow hexagon Program: bob.backward(dot_distance * width) # Move back to the start of the
import turtle row
bob.right(90) # Turn right
# Set up the screen and turtle bob.forward(dot_distance) # Move down to the next row
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow'] bob.left(90) # Realign to the original direction
my_pen = turtle.Turtle()
turtle.bgcolor("black") turtle.done()
Output:
# Drawing loop
for x in
range(360):
my_pen.pencolor(colors[x % 6])
# Cycle through the colors
my_pen.width(x // 100 + 1) # Increase the width based on
the iteration my_pen.forward(x) # Move the turtle
forward
my_pen.left(59) # Turn the turtle left by 59 degrees

# Finish drawing
turtle.done()
Output:
Experiment 15 Python program to demonstrate tangent circle
Drawing a spiral drawing
square Aim: To draw a Program:
spiral square Program: import turtle t =
import turtle turtle.Turtle() #
radius for smallest
# Set up the window circle r = 10 #
my_wn = number of circles n =
turtle.Screen() 10
my_wn.bgcolor("light # loop for printing tangent
blue") circles for i in range(1, n +
my_wn.title("Turtle") Experiment 16 1, 1): t.circle(r * i)
Aim: Turtle program to draw coloured spiral Output:
# Set up the turtle octagon
my_pen = Program:
turtle.Turtle() import turtle as t
my_pen.color("black") colors=["red", "blue", "yellow", "green", "purple", "orange"]
#t.reset()
def my_sqrfunc(size): for i in t.tracer(0,0)
range(4): for i in
# Loop to draw a square range(45):
my_pen.fd(size) t.color(colors[i%6])
my_pen.left(90) t.pendown()
size = size - 5 # Decrease the size for the next square t.forward(2+i*5)
t.left(45)
# Drawing squares of decreasing sizes t.width(i)
my_sqrfunc(146) t.penup()
my_sqrfunc(126) t.update()
my_sqrfunc(106) t.done()
my_sqrfunc(86) Experiment 18
Output: Aim: Program to draw colored rose using spiral
my_sqrfunc(66)
my_sqrfunc(46) hexagons
my_sqrfunc(26) Program:
# Finish and close the turtle window import turtle
t=
turtle.done()
turtle.Turtle(
Output: )
list1 =
["purple","red","orange","blue","gr
Experiment een"] turtle.bgcolor("black") for i in
17 Aim: range(200): t.color(list1[i%5])

t.pensize(i/10+1) tr.pendown() pen.forward(50)


t.forward(i) tr.circle(45) pen.right(90)
t.left(59) tr.color("green") pen.penup()
Output: tr.penup() pen.back(20)
tr.goto(55, -75) pen.pendown()
tr.pendown() for square
tr.circle(45) inrange(80):
# set thikness for each ring draw_square()
tr.pensize(5) pen.forward(5)
tr.color("blue") pen.left(5)
tr.penup() pen.hideturtle()
tr.goto(-110, -25) Output:
tr.pendown()
tr.circle(45)
Output:

Experiment 19
Aim: Turtle program to draw Olympics
symbol Experiment 21
Program: Aim: Drawing stars and moon
#program to draw Olympic
Experiment Program:
symbol import turtle #
20 Aim:
object tr for turtle tr = # importing libraries
Turtle design of spiral square and hexagon
turtle.Turtle()
Program:
tr.color("black") import turtle
import turtle as t pen=t.Turtle()
tr.penup()
tr.goto(0, -25) import random
pen.color("cyan")
tr.pendown()
pen.speed(0) # creating turtle object
tr.circle(45)
def
tr.color("red") t = turtle.Turtle()
draw_square():
tr.penup()
for side in # to activate turtle graphics
tr.goto(110, -25)
range(4): Screen
tr.pendown()
pen.forward(100)
tr.circle(45)
pen.right(90) w = turtle.Screen()
tr.color("yellow")
for side in
tr.penup() # setting speed of turtle
range(4):
tr.goto(-55, -75)
t.speed(0) # stars at random x,y value t.end_fill()

# giving the background color stars() # after drawing hidding the


of turtle turtle from
# took up the turtle's pen
# graphics screen # the window
t.up()
w.bgcolor("black") t.hideturtle()
# go at the x,y coordinate
# giving the color of pen to our generated above # terminated the window after
turtle clicking
t.goto(x, y)
# for drawing w.exitonclick()
# took down the pen to draw
t.color("white") Output:
t.down()
# giving title to our turtle
graphics window # for making our moon tooking
up the pen
w.title("Starry Sky")
t.up()
# making function to draw the
stars # going at the specific
coordinated
def stars():
t.goto(0, 170)
for i in range(5):
# took down the pen to start
t.fd(10) drawing

t.right(144) t.down()

# loop for making number of # giving color to turtle's pen


stars
t.color("white")
for i in range(100):
# start filling the color
# generating random integer
values for x and y t.begin_fill()

x = random.randint(-640, 640) # making our moon

y = random.randint(-330, 330) t.circle(80)

# calling the function stars to # stop filling the color


draw the

You might also like