Python Microproject 5 by Campusify
Python Microproject 5 by Campusify
“Scientific Calculator”
Sr No Title Page.no
1 Introduction 4
2 Python Features 5
3 Code 7
4 Output 17
4 Conclusion 18
5 Evaluation Sheet 19
3
INTRODUCTION
Python is Interpreted −
Python is processed at runtime by the interpreter. You do not need to compile your
program before executing it. This is similar to PERL and PHP.
Python is Interactive −
You can actually sit at a Python prompt and interact with the interpreter directly to
write your programs.
Python is Object-Oriented −
Python supports Object-Oriented style or technique of programming that
encapsulates code within objects.
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at
the National Research Institute for Mathematics and Computer Science in the
Netherlands.
Python is derived from many other languages, including ABC, Modula-3,C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU
General Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido
van Rossum still holds a vital role in directing its progress.
4
Python Features Python's features include −
Easy-to-learn
− Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
Easy-to-read
− Python code is more clearly defined and visible to the eyes.
Easy-to-maintain
− Python's source code is fairly easy-tomaintain.
Interactive Mode
− Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
Portable
− Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
Extendable
− You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be
more efficient.
Databases
− Python provides interfaces to all major commercial databases.
GUI Programming
− Python supports GUI applications that can be created and ported
to many system calls, libraries and windows systems, such as Windows
MFC, Macintosh, and the X Window system of Unix.
5
Scalable
− Python provides a better structure and support for large
programs than shell scripting. Apart from the above-mentioned features,
Python has a big list of good features, few are listed below −
6
Code:-
switch = None
# Button on press
def btn1_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '1')
def btn2_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '2')
def btn3_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '3')
def btn4_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '4')
def btn5_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '5') 7
def btn6_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '6')
def btn7_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '7')
def btn8_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '8')
def btn9_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '9')
def btn0_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, '0')
def key_event(*args):
if disp.get() == '0':
disp.delete(0, END)
def btnp_clicked():
pos = len(disp.get())
disp.insert(pos, '+')
def btnm_clicked():
pos = len(disp.get())
disp.insert(pos, '-')
def btnml_clicked():
pos = len(disp.get())
disp.insert(pos, '*')
def btnd_clicked(): 8
pos = len(disp.get())
disp.insert(pos, '/')
def btnc_clicked(*args):
disp.delete(0, END)
disp.insert(0, '0')
def sin_clicked():
try:
ans = float(disp.get())
if switch is True:
ans = math.sin(math.radians(ans))
else:
ans = math.sin(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def cos_clicked():
try:
ans = float(disp.get())
if switch is True:
ans = math.cos(math.radians(ans))
else:
ans = math.cos(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def tan_clicked():
try:
ans = float(disp.get())
if switch is True:
ans = math.tan(math.radians(ans))
else:
ans = math.tan(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def arcsin_clicked(): 9
try:
ans = float(disp.get())
if switch is True:
ans = math.degrees(math.asin(ans))
else:
ans = math.asin(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def arccos_clicked():
try:
ans = float(disp.get())
if switch is True:
ans = math.degrees(math.acos(ans))
else:
ans = math.acos(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def arctan_clicked():
try:
ans = float(disp.get())
if switch is True:
ans = math.degrees(math.atan(ans))
else:
ans = math.atan(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def pow_clicked():
pos = len(disp.get())
disp.insert(pos, '**')
def round_clicked():
try:
ans = float(disp.get())
ans = round(ans)
disp.delete(0, END) 10
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def logarithm_clicked():
try:
ans = float(disp.get())
ans = math.log10(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def fact_clicked():
try:
ans = float(disp.get())
ans = math.factorial(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def sqr_clicked():
try:
ans = float(disp.get())
ans = math.sqrt(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def dot_clicked():
pos = len(disp.get())
disp.insert(pos, '.')
def pi_clicked():
if disp.get() == '0':
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, str(math.pi))
def e_clicked():
if disp.get() == '0': 11
disp.delete(0, END)
pos = len(disp.get())
disp.insert(pos, str(math.e))
def bl_clicked():
pos = len(disp.get())
disp.insert(pos, '(')
def br_clicked():
pos = len(disp.get())
disp.insert(pos, ')')
def del_clicked():
pos = len(disp.get())
display = str(disp.get())
if display == '':
disp.insert(0, '0')
elif display == '0':
pass
else:
disp.delete(0, END)
disp.insert(0, display[0:pos-1])
def conv_clicked():
global switch
if switch is None:
switch = True
conv_btn['text'] = "Deg"
else:
switch = None
conv_btn['text'] = "Rad"
def ln_clicked():
try:
ans = float(disp.get())
ans = math.log(ans)
disp.delete(0, END)
disp.insert(0, str(ans))
except Exception:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
def mod_clicked():
pos = len(disp.get())
disp.insert(pos, '%')
def btneq_clicked(*args): 12
try:
ans = disp.get()
ans = eval(ans)
disp.delete(0, END)
disp.insert(0, ans)
except:
tkinter.messagebox.showerror("Value Error", "Check your values and
operators")
# Label
data = StringVar()
disp = Entry(root, font="Verdana 20", fg="black", bg="mistyrose", bd=0,
justify=RIGHT, insertbackground="#abbab1", cursor="arrow")
disp.bind("<Return>", btneq_clicked)
disp.bind("<Escape>", btnc_clicked)
disp.bind("<Key-1>", key_event)
disp.bind("<Key-2>", key_event)
disp.bind("<Key-3>", key_event)
disp.bind("<Key-4>", key_event)
disp.bind("<Key-5>", key_event)
disp.bind("<Key-6>", key_event)
disp.bind("<Key-7>", key_event)
disp.bind("<Key-8>", key_event)
disp.bind("<Key-9>", key_event)
disp.bind("<Key-0>", key_event)
disp.bind("<Key-.>", key_event)
disp.insert(0, '0')
disp.focus_set()
disp.pack(expand=TRUE, fill=BOTH)
# Row 1 Buttons
btnrow1 = Frame(root, bg="#000000")
btnrow1.pack(expand=TRUE, fill=BOTH)
# Row 2 Buttons
btnrow2 = Frame(root)
btnrow2.pack(expand=TRUE, fill=BOTH)
# Row 3 Buttons
btnrow3 = Frame(root)
btnrow3.pack(expand=TRUE, fill=BOTH)
# Row 4 Buttons
btnrow4 = Frame(root)
btnrow4.pack(expand=TRUE, fill=BOTH)
Reference
www.google.com
www.campusify.co.in
18