Posts

Showing posts from August, 2020

Interpolation and graph

import numpy as np from scipy import interpolate import matplotlib.pyplot as plt Kc=np.array([ 0.35 , 0.35 , 0.75 , 1.15 , 0.45 ]) Days=np.array([ 1 , 15 , 40 , 90 , 120 ]) f=interpolate.interp1d(Days , Kc) print (f( 20 )) plt.xlabel( 'Days' ) plt.ylabel( 'Kc' ) plt.title( "Kc vs Days" ) plt.plot(Days , Kc , ) plt.show()

Inheritance in Python

  from ntn import chef from chinesechef import chinesechef mychef()=chef() mychef.make_chickpeas() mychinesechef()=chinesechef() mychinesechef.make_tinde() chef.make_chickpeas() class chinesechef: from ntn import chef def make_tinde ( self ): print ( "The chef made tinde" ) class chef: def make_spinach ( self ): print ( "The Chef made spinach" ) def make_chickpeas ( self ): print ( "The Chef made chickpeas" ) def make_special_dish ( self ): print ( "The Chef made paneer" )

To check if a student has a honor's degree (Using Class in python)

  class student: def __init__ ( self , name , gpa , study_field): self .name=name self .gpa=gpa self .study_field=study_field def on_honor_call ( self ): if self .gpa>= 3.5 : return True else : return False then create new file in pycharm from next import student student1=student( "Komal" , 4.2 , "Science" ) student2=student( "Mike" , 3.2 , "Arts" ) print (student2.on_honor_call())

MCQ using Class in Python

  class question: def __init__ ( self , prompt , answer): self .prompt=prompt self .answer=answer then in next sheet from next import question question_prompt = [ "What is the color of apples? \n (a) Red/Green \n (b) Yellow \n (c) White \n\n " , "What is the color of Bananas? \n (a) Red/Green \n (b) Yellow \n (c) White \n\n " , "What is the color of Berry? \n (a) Red/Green \n (b) Yellow \n (c) White \n\n " ] questions = [ question(question_prompt[ 0 ] , "a" ) , question(question_prompt[ 1 ] , "b" ) , question(question_prompt[ 2 ] , "a" ) , ] def run_test (questions): score = 0 for Question in questions: answer = input (question_prompt) if answer == question.answer: score += 1 print ( "You got" + str (score) + "/" + str ( len (Question_prompt) + " correct" )) run_test(questions)

Class in Python

  class student: def __init__ ( self , name , rollno , gpa , is_a_boy): self .name=name self .rollno=rollno self .gpa=gpa self .is_a_boy=is_a_boy Then create new file in pycharm, then type this code from next import student student1=student( "jim" , 24 , 3.2 ,True ) student2=student( "Pam" , 25 , 2.2 ,False ) print (student1.gpa)

calculate power of any number

def raise_to_power(base_num, pow_num): result=1 for index in range(pow_num): result = result * base_num return result print(raise_to_power(2,8))  

Write on a file

w means write a means append r means read r+ means read + write using w will overwrite the entire file while a will just add data at the end of the existing data  If in name of file u add any number or alphabet it will create another file with just the data u added but only in w mode. Check if file is readable, writeable, use readline and readlines to see the data in the file    f= open ( "sdfsf.html" , "w" ) f.write( " \n Pama 100K" ) f.close()

Building a blind guessing game

secret_word= "carrt" guess= "" guess_count = 0 limit= 3 out_of_guessess= False while guess != secret_word and not (out_of_guessess): if guess_count < limit: guess= input ( "Enter the guess" ) guess_count += 1 else : out_of_guessess= True if out_of_guessess: print ( "Out of guesses" ) else : print ( "Winner" )