Lab 1

Part 4 Programming Exercise

  1. The Python Standard Library: Built-in Types and Operations
  2. variabletype()id()print()
    frame<class 'tkinter.Tk'>48065104<tkinter.Tk object .>
    canvas<class 'tkinter.Canvas'>58130640<tkinter.Canvas object .!canvas>
    coord<class 'tuple'>47863104(100, 100, 200, 200)
  3. lab1q3.py
    
    import tkinter as tk
    
    def main():
        
        root = tk.Tk()
        canvas = tk.Canvas()
        canvas.create_rectangle(100,100,200,200)
        canvas.create_text(150,150,text="your name")
        canvas.create_line(120,160,180,160)
        canvas.pack()
        root.mainloop()  
    
    if __name__ == "__main__":
        main() 
    
  4. lab1q4.py
    
    '''
    Program to determine whether or not a number is prime 
    
    Created on dd/mm/yy
    
    @author: yourname
    
    Algorithm: 
    
    1. input the number 
       Read(NUM)
    
    2. determine that the NUM is prime or has divisors other than 1 
       2.1 determine if number is divisible by 2
           If NUM = 2                       
           Then NUM is a prime number
           Else If NUM is even
                Then NUM is not a prime number and halt
                Else 2.11  determine if (NUM divisible by odd integers >= 3 and  <=  (square root of NUM))
                           For LOOP := 3 to sqrt(NUM) by 2
                           Do If (NUM mod LOOP) = 0
                              Then NUM is not a prime number and halt
    
    3. if we get here, print a message that NUM is a prime number 
       Write(NUM,"is a prime number");
    
    4. finished
       Halt
    
    '''
    import math
    num = int(input( "Enter an integer larger than 2: " )) 
    
    loopFlag = True
    isPrime = True
    
    if num == 2:
        loopFlag = False
    elif num % 2 == 0: 
        isPrime = False
        loopFlag = False
    else:
        i = 3
        while loopFlag and i <= int(math.sqrt(num)):
            if num % i == 0:
                loopFlag = False
                isPrime = False
            else: 
                i += 2
    
    if isPrime: 
        print (str(num) + ' is a prime number')
    else: print (str(num) + ' is not prime number')
    
  5. lab1q5.py
    
    import sys
    import math
    
    def main():
            a = float(input("Enter a "))
            b = float(input("Enter b "))
            c = float(input("Enter c "))
    
            print("When a = %5.2f,b = %5.2f,and c = %5.2f: X = %5.2f or %5.2f"%
                  (a,b,c,
                  (-b + math.sqrt(b*b-4*a*c))/(2*a),
                  (-b - math.sqrt(b*b-4*a*c))/(2*a)))
    
    if __name__ == "__main__":
        main()
    
  6. lab1q6.py
    
    #!%python path%/python.exe
    import time
    import cgi
    
    html5top='''
    <!-- {fname} -->
    <!DOCTYPE html>
    <html>
     <head>
      <title>{title}</title>
     </head>
     <body>
      <h1>{header}</h1>
    '''
    html5bottom='''
     </body>
    </html>
    '''
    
    print (html5top.format(fname='cgilogon.py', title='CGI',header='Logon'))
    
    form = cgi.FieldStorage()
    
    if 'name' in form:
        if form['password'].value == 'abc':
            print ("<h1>Welcome, %s!</h1>" % form[ 'name' ].value)
            print ("My PDP")
        else: print ("<h1>password incorrect %s!</h1>" % form[ 'password' ].value)
    
    print (time.ctime( time.time() ))
    print (html5bottom)