Friday, March 27, 2020

number guess game using python

'''
 You decide you want to play a game where you are hiding
 a number from someone.  Store this number in a variable
 called 'answer'.  Another user provides a number called
 'guess'.  By comparing guess to answer, you inform the user
 if their guess is too high or too low.

 Fill in the conditionals below to inform the user about how
 their guess compares to the answer.
'''
answer = int(input("enter a number as ans"))#provide answer
guess =int(input("enter a number as guess")) #provide guess

if answer > guess :
    #provide conditional
    result = "Oops!  Your guess was too low."
elif answer < guess : #provide conditional
    result = "Oops!  Your guess was too high."
elif answer == guess : #provide conditional
    result = "Nice!  Your guess matched the answer!"

print(result)

output:
enter a number as ans56

enter a number as guess47
Oops!  Your guess was too low.

if elif else in python

"""

Points Prize
1 - 50 wooden rabbit
51 - 150 no prize
151 - 180 wafer-thin mint
181 - 200 penguin
"""

num=int(input("enter a number"))
if num > 1 and num <=50 :
    print("Congratulations! You won a wooden rabbit!")
elif num > 50 and num <=150:
    print("Oh dear, no prize this time.")
elif num > 150 and num <=180:
    print("Congratulations! You won a wafer-thin mint!")
elif num > 180 and num <=200:
    print("Congratulations! You won a penguin")
else :
     print("Oh dear, no prize this time.")

output:
enter a number173
Congratulations! You won a wafer-thin mint!

dictionary in python


elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},
            'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

print(elements)
print("\n hydrogen:",elements['hydrogen'])
print("\n hydrogen_no:",elements['hydrogen']['number'])
print("\n hydrogen_weight:",elements['hydrogen']['weight'])
print("\n hydrogen_symbols:",elements['hydrogen']['symbol'])

print("\n helium:",elements['helium'])

output:
{'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'}, 'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

 hydrogen: {'number': 1, 'weight': 1.00794, 'symbol': 'H'}

 hydrogen_no: 1

 hydrogen_weight: 1.00794

 hydrogen_symbols: H

 helium: {'number': 2, 'weight': 4.002602, 'symbol': 'He'}


example 2:
cast = {
           "Jerry Seinfeld": "Jerry Seinfeld",
           "Julia Louis-Dreyfus": "Elaine Benes",
           "Jason Alexander": "George Costanza",
           "Michael Richards": "Cosmo Kramer"
       }
for key in cast:
    print(key)

output:
Jerry Seinfeld
Julia Louis-Dreyfus
Jason Alexander
Michael Richards

Bio nomial probability in datascience using python

from scipy.stats import binom
import matplotlib.pyplot as plt
import numpy as np

n,p=10,0.5

x=np.arange(0,10,0.001)
plt.plot(x,binom.pmf(x,n,p))

output:



exponential pdf/ power Law in datascience using python

from scipy.stats import expon
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(0,10,0.001)
plt.plot(x,expon.pdf(x))

output:

Thursday, March 26, 2020

Normal/Gaussian in datascience in python

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(-3,3,0.001)
plt.plot(x,norm.pdf(x))

output:


import matplotlib.pyplot as plt
import numpy as np

mu= 5.0
sigma = 2.0

values = np.random.normal(mu,sigma,10000)
#10000 is data points
plt.hist(values,50)
#50 is number of bars

plt.show()

output:

uniform distribution in datascience uisng python

import numpy as np
import matplotlib.pyplot as plt

values=np.random.uniform(-10.0,10.0,10000)

plt.hist(values,50)
plt.show()

output:

standard deviation in datascience using python matplot lib

import numpy as np
import matplotlib.pyplot as plt

income= np.random.normal(100.0,20.0,10000)

plt.hist(income,50)
plt.show()

output:

income.std()
19.99095786583713

import numpy as np 
import matplotlib.pyplot as plt

income= np.random.normal(100.0,30.0,10000)

plt.hist(income,50)
plt.show()


Mean ,Median and Mode in datascience using python programming

Example1:
import numpy as np
income= np.random.normal(27000,15000,10000)
np.mean(income)

output:
26908.89269950194

%matplotlib inline
import matplotlib.pyplot as plt
plt.hist(income,50)
plt.show()




np.median(income)

output:
26735.667830776893

Mode:
ages=np.random.randint(18,high=90,size=500)
ages
output:
array([62, 66, 21, 39, 74, 56, 20, 35, 29, 37, 53, 88, 39, 54, 45, 34, 22,
       29, 46, 82, 38, 29, 36, 56, 87, 35, 39, 42, 67, 50, 60, 58, 64, 19,
       80, 18, 22, 84, 56, 65, 60, 38, 77, 21, 45, 88, 45, 49, 71, 72, 31,
       77, 41, 41, 27, 50, 51, 81, 29, 22, 41, 71, 78, 69, 41, 35, 43, 21,
       81, 80, 31, 73, 46, 44, 61, 78, 39, 72, 50, 28, 60, 19, 66, 73, 40,
       77, 42, 43, 80, 81, 86, 87, 37, 61, 53, 28, 74, 55, 53, 35, 35, 29,
       49, 82, 75, 19, 57, 53, 19, 55, 34, 45, 80, 62, 43, 56, 86, 69, 33,
       70, 52, 49, 32, 77, 73, 18, 50, 51, 73, 34, 50, 89, 48, 28, 35, 64,
       64, 35, 69, 53, 62, 63, 38, 80, 51, 67, 53, 72, 68, 50, 36, 80, 19,
       74, 39, 59, 53, 83, 82, 70, 63, 78, 25, 59, 22, 47, 39, 36, 60, 35,
       47, 87, 69, 54, 28, 51, 80, 43, 68, 61, 79, 61, 63, 36, 82, 42, 88,
       66, 71, 73, 27, 50, 68, 20, 74, 50, 55, 86, 87, 72, 76, 79, 76, 43,
       74, 19, 27, 60, 40, 61, 82, 26, 52, 62, 32, 20, 20, 25, 20, 84, 83,
       54, 56, 74, 68, 83, 68, 38, 86, 25, 26, 81, 58, 57, 68, 58, 20, 71,
       28, 51, 22, 63, 51, 19, 89, 89, 37, 46, 27, 77, 78, 83, 70, 38, 39,
       67, 18, 52, 85, 37, 31, 27, 85, 81, 86, 59, 49, 22, 26, 44, 32, 58,
       63, 21, 60, 35, 70, 39, 54, 52, 33, 18, 67, 44, 74, 31, 22, 60, 78,
       27, 68, 40, 59, 53, 20, 21, 26, 32, 86, 82, 54, 61, 64, 27, 64, 26,
       51, 55, 70, 30, 18, 40, 31, 44, 40, 64, 73, 89, 75, 39, 20, 85, 20,
       68, 29, 37, 83, 23, 28, 51, 82, 23, 26, 39, 36, 41, 57, 76, 27, 89,
       23, 42, 25, 44, 44, 41, 64, 32, 24, 27, 68, 52, 39, 19, 76, 40, 87,
       68, 66, 30, 53, 54, 32, 63, 28, 85, 36, 87, 66, 59, 80, 88, 53, 66,
       58, 40, 69, 53, 59, 74, 64, 71, 58, 69, 74, 37, 88, 31, 72, 66, 34,
       49, 80, 71, 75, 41, 40, 89, 49, 63, 86, 78, 34, 68, 21, 65, 61, 73,
       49, 35, 84, 23, 79, 64, 79, 65, 54, 75, 25, 82, 22, 73, 89, 58, 66,
       76, 53, 29, 27, 32, 33, 57, 81, 31, 43, 76, 46, 38, 47, 49, 61, 42,
       49, 28, 50, 54, 49, 22, 81, 81, 85, 55, 51, 20, 42, 52, 68, 47, 62,
       29, 75, 55, 55, 57, 21, 43, 52, 57, 47, 63, 46, 38, 73, 46, 72, 54,
       49, 54, 39, 63, 42, 59, 70, 81, 55, 74, 62, 71, 23, 26, 21, 25, 85,
       50, 38, 78, 68, 24, 38, 50])


Wednesday, March 25, 2020

mean,median,mode in datascience using python

Mean:

avg is called as mean
mean=sum of the numbers/total numbers
example:
numbers=1,2,3,4,5,6
sum of numbers=21
total numbers=6
mean=21/6
mean=3.5

Median:

set of numbers:0,1,2,2,0,0,3,2,0
after sorting:0,0,0,0,1,2,2,2,3

"1" is the median of the given set


Mode:

mode is most common number in the given series or set

given series :0,1,2,0,3,1,2,0,6,0

mode is "0"

most common number is zero in the given series





Tuesday, March 24, 2020

pygame colour filling in python


import pygame, sys
from pygame.locals import *
pygame.init()
displaysurf=pygame.display.set_mode((800,400),0,32)
pygame.display.set_caption('Drawing!')
#setup colour
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
SILVER = (192, 192, 192)
PINK=(255,105,180)

displaysurf.fill(WHITE)
pygame.draw.line(displaysurf, BLUE, [0, 0], [100, 100], 30)
pygame.draw.line(displaysurf, BLUE, [0, 0], [100, 100], 10)

#pygame.draw.line(displaysurf, GREEN, [0, 0], [60, 100], 5)
pygame.draw.line(displaysurf, GREEN, [0, 0], [0, 400], 100)
pygame.draw.line(displaysurf, RED, [100,0], [100, 400], 100)
pygame.draw.line(displaysurf, BLACK, [300,0], [300, 400], 100)
pygame.draw.line(displaysurf, BLUE, [400,0], [400, 400], 100)
pygame.draw.line(displaysurf, GREEN, [500, 0], [500, 400], 100)
pygame.draw.line(displaysurf, PINK, [600,0], [600, 400], 100)
pygame.draw.line(displaysurf, SILVER, [700,0], [700, 400], 100)
pygame.draw.line(displaysurf, BLUE, [800,0], [800, 400], 100)


pixobj=pygame.PixelArray(displaysurf)
pixobj[480][380]=BLACK
pixobj[482][382]=BLACK
pixobj[484][384]=BLACK
pixobj[486][386]=BLACK
pixobj[488][388]=BLACK
del pixobj


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
           
    pygame.display.update()

output:


python class topic video