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

Python programming exp 8 9 11 12

Pspp lab manual

Uploaded by

sugimpt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python programming exp 8 9 11 12

Pspp lab manual

Uploaded by

sugimpt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Installing numpy command:


py -m pip install -U numpy –user or pip install numpy
program for nump 1:
import numpy as np
f = np.arange(0, 30, 5)
print ("A sequential array with steps of 5:\n", f)
output:
A sequential array with steps of 5:
[ 0 5 10 15 20 25]
Program 2 for numpy:
import numpy as np
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a)
print(a.shape)
b = a[1:, 2:]
print(b)
print(b.shape)
Output:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
(3, 4)
[[ 7 8]
[11 12]]
(2, 2)
2.Installing pandas command:
py -m pip install -U pandas –user or pip install pandas
Program for pandas 1:
import pandas as pd
S = pd.Series([11, 28, 72, 3, 5, 8])
print(S)
output:
0 11
1 28
2 72
3 3
4 5
5 8
dtype: int64
Program for pandas 2:
import pandas as pd
import numpy as np
# Creating empty series
ser = pd.Series()
print("Pandas Series: ", ser)
# simple array
data = np.array(['g', 'e', 'e', 'k', 's'])
ser = pd.Series(data)
print("Pandas Series:\n", ser)
output:
Pandas Series: Series([], dtype: object)
Pandas Series:
0 g
1 e
2 e
3 k
4 s
dtype: object

3.Installing matplot command:


py -m pip install -U matplot –user or pip install matplot
program for matplot:
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('My first graph!')
# function to show the plot
plt.show()
output:
4.Installing scipy command:
py -m pip install -U scipy –user or pip install scipy
1.program for scipy:
from scipy import special
a =special.exp10(3)
print(a)
b =special.exp2(3)
print(b)
c =special.sindg(90)
print(c)
d =special.cosdg(45)
print(d)
output:
1000.0
8.0
1.0
0.7071067811865475
5.Installation for pygame

Step -1 py -m pip install -U pygame –user or pip install pygame


1.Program for screen size:
import pygame

pygame.init()
screen =pygame.display.set_mode([720,1064])
pygame.display.set_caption('Hello World')
screen.fill([0, 0, 0])
pygame.display.flip()
time.sleep(20)
output:

2.Program for rectangle display:


import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.draw.rect(screen, (0, 125, 255),pygame.Rect(60,60, 60, 60))
pygame.display.flip()

6. Bouncing ball program


import pygame
# initialize pygame
pygame.init()

# define width of screen


width = 1000
# define height of screen
height = 600
screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")


screen = pygame.display.set_mode(screen_res)

# define colors
red = (255, 0, 0)
black = (0, 0, 0)

# define ball
ball_obj = pygame.draw.circle(
surface=screen, color=red, center=[100, 100], radius=40)
# define speed of ball
# speed = [X direction speed, Y direction speed]
speed = [1, 1]

# game loop
while True:
# event loop
for event in pygame.event.get():
# check if a user wants to exit the game or not
if event.type == pygame.QUIT:
exit()

# fill black color on screen


screen.fill(black)

# move the ball


# Let center of the ball is (100,100) and the speed is (1,1)
ball_obj = ball_obj.move(speed)
# Now center of the ball is (101,101)
# In this way our wall will move

# if ball goes out of screen then change direction of movement


if ball_obj.left <= 0 or ball_obj.right >= width:
speed[0] = -speed[0]
if ball_obj.top <= 0 or ball_obj.bottom >= height:
speed[1] = -speed[1]

# draw ball at new centers that are obtained after moving ball_obj
pygame.draw.circle(surface=screen, color=red,
center=ball_obj.center, radius=40)

# update screen
pygame.display.flip()

You might also like