SlideShare a Scribd company logo
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
print("Enter first Number:")
a=int(input())
print("Enter second number: ")
b=int(input())
rem=a%b
while rem!=0:
a=b
b=rem
rem=a%b
print("GCD of given number is:", b)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter first Number:
5
Enter second number:
10
('GCDof given number is:', 5)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx
print('The square root is' ,newtonSqrt(81))
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
('The square root is', 9.0)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
n = int(input("Enter the number: "))
e = int(input("Enter the expo: "))
r=n
for i in range(1,e):
r=n*r
print("Exponent is :", r)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the number: 2
Enter the expo: 3
('Exponent is :', 8)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
alist = [-45,0,3,10,90,5,- 2,4,18,45,100,1,-266,706]
largest = alist[0]
for large in alist:
if large > alist:
largest = large
print(“The Largest Number is:”,largest)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
(‘The Largest of the List is:’, 706)
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def Linearsearch(alist,item):
pos=0
found=False
stop=False
while pos<len(alist) and not found and not stop:
if alist[pos]==item:
found=True
print("Element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
a=[]
n=int(input("Enter upper limit"))
for i in range(0,n):
e=int(input("Enter the elements"))
a.append(e)
x=int(input("Enter element to search"))
Linearsearch(a,x)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter upper limit:5
Enter the elements:1
Enter the elements:8
Enter the elements:9
Enter the elements:6
Enter the elements:7
Enter element to search:9
Element found in position: 2
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def binarysearch(a,n,k):
low = 0
high = n
while(low<=high):
mid=int((low+high)/2)
if(k==a[mid]):
return mid
elif(k<a[mid]):
high=mid-1
else:
low=mid+1
return -1
n=int(input("Enter the size of list"))
a=[i for i in range(n)]
print(n)
print("Enter the element")
for i in range(0,n):
a[i]=int(input())
print("Enter the element to be searched")
k=int(input())
position=binarysearch(a,n,k)
if(position!=-1):
print("Entered no:%d is present:%d"%(k,position))
else:
print("Entered no:%d is not present in list"%k)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the size of list
5
Enter the element
1
8
6
3
4
Enter the element to be searched
1
Entered no:1 is present:0
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
alist = [23, 6, 4 ,12, 9]
mergeSort(alist)
print(alist)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
('Splitting ', [23, 6, 4, 12, 9])
('Splitting ', [23, 6])
('Splitting ', [23])
('Merging ', [23])
('Splitting ', [6])
('Merging ', [6])
('Merging ', [6, 23])
('Splitting ', [4, 12, 9])
('Splitting ', [4])
('Merging ', [4])
('Splitting ', [12, 9])
('Splitting ', [12])
('Merging ', [12])
('Splitting ', [9])
('Merging ', [9])
('Merging ', [9, 12])
('Merging ', [4, 9, 12])
('Merging ', [4, 6, 9, 12, 23])
[4, 6, 9, 12, 23]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
i=1
x = int(input("Enter the Upper Limit:"))
for k in range (1, (x+1), 1):
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print (i)
else:
k = k-1
i=i+1
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter the Upper Limit:100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
X= [[1,2,7],
[4,5,6],
[1,2,3]]
Y= [[5,8,1],
[6,7,3],
[4,5,1]]
result= [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j]+=X[i][k]*Y[k][j]
for r in result:
print(r)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
[45, 57, 14]
[74, 97, 25]
[29, 37, 10]
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
import sys
if len(sys.argv)!=2:
print('Error: filename missing')
sys.exit(1)
filename = sys.argv[1]
file = open(filename, "r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word]+=1
for k,v in wordcount.items():
print (k, v)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
filename = input("Enter file name:")
inf = open(filename,'r')
wordcount={}
for word in inf.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for key in wordcount.keys():
print("%sn%sn"%(key, wordcount[key]))
linecount = 0
for i in inf:
paragraphcount = 0
if'n'in i:
linecount += 1
if len(i)<2: paragraphcount *= 0
elif len(i)>2: paragraphcount = paragraphcount + 1
print('%-4dn%4dn%sn'%(paragraphcount,linecount,i))
inf.close()
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
Enter file name:'text.txt'
a
1
used
1
for
1
language
1
Python
1
is
1
programming
2
general-purpose
1
widely
1
high-level
1
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
import pygame
import math
import sys
pygame.init()
screen = pygame.display.set_mode((600, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
xRadius = 250
yRadius = 100
for degree in range(0,360,10):
x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.display.flip()
clock.tick(5)
GE 8161 – Problem Solving and Python Programming Laboratory
Output:
GE 8161 – Problem Solving and Python Programming Laboratory
Program:
import pygame,sys,time,random
from pygame.locals import*
from time import*
pygame.init()
windowSurface=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption("BOUNCE")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
info=pygame.display.Info()
sw=info.current_w
sh=info.current_h
y=0
direction=1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface,GREEN,(250,y),13,0)
sleep(.006)
y+=direction
if y>=sh:
direction=-1
elif y<=0:
direction=1
pygame.display.update()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
GE 8161 – Problem Solving and Python Programming Laboratory
Output:

More Related Content

DOCX
Python lab manual all the experiments are available
PPTX
NUMPY [Autosaved] .pptx
PDF
Arrays in python
PPTX
PPTX
NumPy.pptx
PPTX
Counting Sort
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PDF
backtracking algorithms of ada
Python lab manual all the experiments are available
NUMPY [Autosaved] .pptx
Arrays in python
NumPy.pptx
Counting Sort
DESIGN AND ANALYSIS OF ALGORITHMS
backtracking algorithms of ada

What's hot (20)

PPTX
Object oriented programming in python
PPT
Data Structure and Algorithms
PPTX
Chapter 05 classes and objects
PPTX
Priority queue in DSA
PDF
Artificial Neural Networks Lect5: Multi-Layer Perceptron & Backpropagation
PPTX
Introduction to numpy
PPTX
PPTX
Bayanno-Net: Bangla Handwritten Digit Recognition using CNN
PPTX
Strassen's matrix multiplication
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
Heap Management
PPTX
Constructor in java
PPTX
Object oriented programming with python
PPTX
Method overloading
PPTX
Elements of Dynamic Programming
PPT
Two Dimensional Array
PDF
List,tuple,dictionary
PPTX
Introduction to datastructure and algorithm
PPTX
Friend functions
PDF
Lambda Expressions in Java
Object oriented programming in python
Data Structure and Algorithms
Chapter 05 classes and objects
Priority queue in DSA
Artificial Neural Networks Lect5: Multi-Layer Perceptron & Backpropagation
Introduction to numpy
Bayanno-Net: Bangla Handwritten Digit Recognition using CNN
Strassen's matrix multiplication
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Heap Management
Constructor in java
Object oriented programming with python
Method overloading
Elements of Dynamic Programming
Two Dimensional Array
List,tuple,dictionary
Introduction to datastructure and algorithm
Friend functions
Lambda Expressions in Java
Ad

Similar to Python Lab manual program for BE First semester (all department (20)

DOCX
Basic python laboratoty_ PSPP Manual .docx
PDF
Python summer course play with python (lab1)
PDF
Python summer course play with python (lab1)
PPTX
Python programming workshop session 3
PPTX
3 analysis.gtm
ODP
Scala as a Declarative Language
PPT
Matlab1
PPT
Pythonic Math
PPTX
4. functions
PDF
Data Structure: Algorithm and analysis
PPT
Profiling and optimization
PPTX
Testing for Educational Gaming and Educational Gaming for Testing
PPTX
Numerical_Analysis_Python_Presentation.pptx
PDF
Perm winter school 2014.01.31
PPT
how to calclute time complexity of algortihm
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPTX
07012023.pptx
PPTX
R Language Introduction
PPT
How to calculate complexity in Data Structure
Basic python laboratoty_ PSPP Manual .docx
Python summer course play with python (lab1)
Python summer course play with python (lab1)
Python programming workshop session 3
3 analysis.gtm
Scala as a Declarative Language
Matlab1
Pythonic Math
4. functions
Data Structure: Algorithm and analysis
Profiling and optimization
Testing for Educational Gaming and Educational Gaming for Testing
Numerical_Analysis_Python_Presentation.pptx
Perm winter school 2014.01.31
how to calclute time complexity of algortihm
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
07012023.pptx
R Language Introduction
How to calculate complexity in Data Structure
Ad

Recently uploaded (20)

PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
Glazing at Facade, functions, types of glazing
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Introduction to Data Science: data science process
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
web development for engineering and engineering
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
PPTX
TE-AI-Unit VI notes using planning model
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Glazing at Facade, functions, types of glazing
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Operating System & Kernel Study Guide-1 - converted.pdf
bas. eng. economics group 4 presentation 1.pptx
Introduction to Data Science: data science process
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Structs to JSON How Go Powers REST APIs.pdf
Simulation of electric circuit laws using tinkercad.pptx
ETO & MEO Certificate of Competency Questions and Answers
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
July 2025: Top 10 Read Articles Advanced Information Technology
TE-AI-Unit VI notes using planning model
Lesson 3_Tessellation.pptx finite Mathematics

Python Lab manual program for BE First semester (all department

  • 1. GE 8161 – Problem Solving and Python Programming Laboratory Program: print("Enter first Number:") a=int(input()) print("Enter second number: ") b=int(input()) rem=a%b while rem!=0: a=b b=rem rem=a%b print("GCD of given number is:", b)
  • 2. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter first Number: 5 Enter second number: 10 ('GCDof given number is:', 5)
  • 3. GE 8161 – Problem Solving and Python Programming Laboratory Program: def newtonSqrt(n): approx = 0.5 * n better = 0.5 * (approx + n/approx) while better != approx: approx = better better = 0.5 * (approx + n/approx) return approx print('The square root is' ,newtonSqrt(81))
  • 4. GE 8161 – Problem Solving and Python Programming Laboratory Output: ('The square root is', 9.0)
  • 5. GE 8161 – Problem Solving and Python Programming Laboratory Program: n = int(input("Enter the number: ")) e = int(input("Enter the expo: ")) r=n for i in range(1,e): r=n*r print("Exponent is :", r)
  • 6. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the number: 2 Enter the expo: 3 ('Exponent is :', 8)
  • 7. GE 8161 – Problem Solving and Python Programming Laboratory Program: alist = [-45,0,3,10,90,5,- 2,4,18,45,100,1,-266,706] largest = alist[0] for large in alist: if large > alist: largest = large print(“The Largest Number is:”,largest)
  • 8. GE 8161 – Problem Solving and Python Programming Laboratory Output: (‘The Largest of the List is:’, 706)
  • 9. GE 8161 – Problem Solving and Python Programming Laboratory Program: def Linearsearch(alist,item): pos=0 found=False stop=False while pos<len(alist) and not found and not stop: if alist[pos]==item: found=True print("Element found in position",pos) else: if alist[pos]>item: stop=True else: pos=pos+1 return found a=[] n=int(input("Enter upper limit")) for i in range(0,n): e=int(input("Enter the elements")) a.append(e) x=int(input("Enter element to search")) Linearsearch(a,x)
  • 10. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter upper limit:5 Enter the elements:1 Enter the elements:8 Enter the elements:9 Enter the elements:6 Enter the elements:7 Enter element to search:9 Element found in position: 2
  • 11. GE 8161 – Problem Solving and Python Programming Laboratory Program: def binarysearch(a,n,k): low = 0 high = n while(low<=high): mid=int((low+high)/2) if(k==a[mid]): return mid elif(k<a[mid]): high=mid-1 else: low=mid+1 return -1 n=int(input("Enter the size of list")) a=[i for i in range(n)] print(n) print("Enter the element") for i in range(0,n): a[i]=int(input()) print("Enter the element to be searched") k=int(input()) position=binarysearch(a,n,k) if(position!=-1): print("Entered no:%d is present:%d"%(k,position)) else: print("Entered no:%d is not present in list"%k)
  • 12. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the size of list 5 Enter the element 1 8 6 3 4 Enter the element to be searched 1 Entered no:1 is present:0
  • 13. GE 8161 – Problem Solving and Python Programming Laboratory Program: def selectionSort(alist): for i in range(len(alist)-1,0,-1): pos=0 for location in range(1,i+1): if alist[location]>alist[pos]: pos= location temp = alist[i] alist[i] = alist[pos] alist[pos] = temp alist = [54,26,93,17,77,31,44,55,20] selectionSort(alist) print(alist)
  • 14. GE 8161 – Problem Solving and Python Programming Laboratory Output: [17, 20, 26, 31, 44, 54, 55, 77, 93]
  • 15. GE 8161 – Problem Solving and Python Programming Laboratory Program: def insertionSort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue alist = [54,26,93,17,77,31,44,55,20] insertionSort(alist) print(alist)
  • 16. GE 8161 – Problem Solving and Python Programming Laboratory Output: [17, 20, 26, 31, 44, 54, 55, 77, 93]
  • 17. GE 8161 – Problem Solving and Python Programming Laboratory Program: def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 print("Merging ",alist) alist = [23, 6, 4 ,12, 9] mergeSort(alist) print(alist)
  • 18. GE 8161 – Problem Solving and Python Programming Laboratory Output: ('Splitting ', [23, 6, 4, 12, 9]) ('Splitting ', [23, 6]) ('Splitting ', [23]) ('Merging ', [23]) ('Splitting ', [6]) ('Merging ', [6]) ('Merging ', [6, 23]) ('Splitting ', [4, 12, 9]) ('Splitting ', [4]) ('Merging ', [4]) ('Splitting ', [12, 9]) ('Splitting ', [12]) ('Merging ', [12]) ('Splitting ', [9]) ('Merging ', [9]) ('Merging ', [9, 12]) ('Merging ', [4, 9, 12]) ('Merging ', [4, 6, 9, 12, 23]) [4, 6, 9, 12, 23]
  • 19. GE 8161 – Problem Solving and Python Programming Laboratory Program: i=1 x = int(input("Enter the Upper Limit:")) for k in range (1, (x+1), 1): c=0; for j in range (1, (i+1), 1): a = i%j if (a==0): c = c+1 if (c==2): print (i) else: k = k-1 i=i+1
  • 20. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter the Upper Limit:100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
  • 21. GE 8161 – Problem Solving and Python Programming Laboratory Program: X= [[1,2,7], [4,5,6], [1,2,3]] Y= [[5,8,1], [6,7,3], [4,5,1]] result= [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j]+=X[i][k]*Y[k][j] for r in result: print(r)
  • 22. GE 8161 – Problem Solving and Python Programming Laboratory Output: [45, 57, 14] [74, 97, 25] [29, 37, 10]
  • 23. GE 8161 – Problem Solving and Python Programming Laboratory Program: import sys if len(sys.argv)!=2: print('Error: filename missing') sys.exit(1) filename = sys.argv[1] file = open(filename, "r+") wordcount={} for word in file.read().split(): if word not in wordcount: wordcount[word] = 1 else: wordcount[word]+=1 for k,v in wordcount.items(): print (k, v)
  • 24. GE 8161 – Problem Solving and Python Programming Laboratory Output:
  • 25. GE 8161 – Problem Solving and Python Programming Laboratory Program: filename = input("Enter file name:") inf = open(filename,'r') wordcount={} for word in inf.read().split(): if word not in wordcount: wordcount[word] = 1 else: wordcount[word] += 1 for key in wordcount.keys(): print("%sn%sn"%(key, wordcount[key])) linecount = 0 for i in inf: paragraphcount = 0 if'n'in i: linecount += 1 if len(i)<2: paragraphcount *= 0 elif len(i)>2: paragraphcount = paragraphcount + 1 print('%-4dn%4dn%sn'%(paragraphcount,linecount,i)) inf.close()
  • 26. GE 8161 – Problem Solving and Python Programming Laboratory Output: Enter file name:'text.txt' a 1 used 1 for 1 language 1 Python 1 is 1 programming 2 general-purpose 1 widely 1 high-level 1
  • 27. GE 8161 – Problem Solving and Python Programming Laboratory Program: import pygame import math import sys pygame.init() screen = pygame.display.set_mode((600, 300)) pygame.display.set_caption("Elliptical orbit") clock = pygame.time.Clock() while(True): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() xRadius = 250 yRadius = 100 for degree in range(0,360,10): x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300 y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150 screen.fill((0, 0, 0)) pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35) pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1) pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15) pygame.display.flip() clock.tick(5)
  • 28. GE 8161 – Problem Solving and Python Programming Laboratory Output:
  • 29. GE 8161 – Problem Solving and Python Programming Laboratory Program: import pygame,sys,time,random from pygame.locals import* from time import* pygame.init() windowSurface=pygame.display.set_mode((500,400),0,32) pygame.display.set_caption("BOUNCE") BLACK=(0,0,0) WHITE=(255,255,255) RED=(255,0,0) GREEN=(0,255,0) BLUE=(0,0,255) info=pygame.display.Info() sw=info.current_w sh=info.current_h y=0 direction=1 while True: windowSurface.fill(BLACK) pygame.draw.circle(windowSurface,GREEN,(250,y),13,0) sleep(.006) y+=direction if y>=sh: direction=-1 elif y<=0: direction=1 pygame.display.update() for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit()
  • 30. GE 8161 – Problem Solving and Python Programming Laboratory Output: