5
edits
(2 intermediate revisions by 2 users not shown) | |||
Line 25: | Line 25: | ||
Use [[VSCodium]] or any [[ | Use [[VSCodium]] or any [[source-code editor]] to edit your [[source code]]s. | ||
Line 201: | Line 201: | ||
== GUI calculator == | == GUI calculator == | ||
http://uoxqi4lrfqztugili7zzgygibs4xstehf5hohtkpyqcoyryweypzkwid.onion/?img=361615491111.png | |||
http://hostxvivwx3lzvfdnof2muv7q5fkcovkfa3nexlnl5zrelif2mawxkad.onion/image.php?di=631T | |||
http://pdogfxf7k6lyqe7uhmrokpc74nk2td75m4al5t6uvfhdvvxvng3nazid.onion\/tnibabrS30.jpg | |||
http://3b6clio4syptsnvvtzyxifqvtizyazgyowpp3v5f7dj3mzmfhyoy4iyd.onion/images/8f98719adf96e79c9647c790631c1c2e.png | |||
=== Source code === | === Source code === | ||
Line 394: | Line 403: | ||
app = MyApp() | app = MyApp() | ||
app.mainloop() | app.mainloop() | ||
==== return ==== | |||
Why would you use the return statement in Python? | |||
The print() function writes, i.e., "prints", a string or a number on the console. The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function. | |||
Functions that return values are sometimes called fruitful functions. In many other languages, a function that doesn’t return a value is called a procedure. | |||
In the given code the value returned (that is 2) when function foo() is called is used in the function bar(). These return values are printed on console only when the print statements are used as shown below. | |||
Example | |||
def foo(): | |||
print("Hello from within foo") | |||
return 2 | |||
def bar(): | |||
return 10*foo() | |||
print(foo()) | |||
print(bar()) | |||
Output | |||
Hello from within foo | |||
2 | |||
Hello from within foo | |||
20 | |||
We see that when foo() is called from bar(), 2 isn't written to the console. Instead it is used to calculate the value returned from bar(). | |||
https://www.tutorialspoint.com/Why-would-you-use-the-return-statement-in-Python | |||
==== functools.partial ==== | ==== functools.partial ==== | ||
Partial functions | |||
You can create partial functions in python by using the partial function from the functools library. | |||
Partial functions allow one to derive a function with x parameters to a function with fewer parameters and fixed values set for the more limited function. | |||
Import required: | |||
from functools import partial | |||
This code will return 21. | |||
from functools import partial | |||
def multiply(x,y): | |||
return x * y | |||
loli = partial(multiply,3) | |||
print(loli(7)) | |||
7*3 = 21 | |||
An important note: the default values will start replacing variables from the left. The 2 will replace x. y will equal 4 when dbl(4) is called. It does not make a difference in this example, but it does in the example below. | |||
Exercise | |||
Edit the function provided by calling partial() and replacing the first three variables in func(). Then print with the new partial function using only one input variable so that the output equals 26. | |||
#Following is the exercise, function provided: | |||
from functools import partial | |||
def func(u,v,w,x): | |||
return u*5 + v*4 + w*3 + x | |||
#Enter your code here to create and print with your partial function | |||
baby = partial(func,1,2,3) | |||
print(baby(4)) | |||
1*5 + 2*4 + 3*3 + 4 = 26 | |||
functools — Higher-order functions and operations on callable objects | functools — Higher-order functions and operations on callable objects | ||
Line 696: | Line 785: | ||
if not type(x) is int: | if not type(x) is int: | ||
raise TypeError("Only integers are allowed") | raise TypeError("Only integers are allowed") | ||
=== Python eval() === | |||
The eval() method parses the expression passed to this method and runs python expression (code) within the program. | |||
In simple terms, the eval() method runs the python code (which is passed as an argument) within the program. | |||
The syntax of eval() is: | |||
eval(expression, globals=None, locals=None) | |||
eval() Parameters | |||
The eval() takes three parameters: | |||
* expression - this string as parsed and evaluated as a Python expression | |||
* globals (optional) - a dictionary | |||
* locals (optional)- a mapping object. Dictionary is the standard and commonly used mapping type in Python. | |||
The use of "globals" and "locals" will be discussed later in this article. | |||
Return Value from eval() | |||
The eval() method returns the result evaluated from the "expression". | |||
Example 1: How eval() works in Python? | |||
x = 2 | |||
print(eval('x + 1')) | |||
When you run the program, the output will be: | |||
3 | |||
Here, the eval() evaluates the expression "x + 1" and print it. | |||
Example 2: Practical Example to Demonstrate Use of eval() | |||
# Calculate perimeter of square | |||
def calPeri(L): | |||
return 4*L | |||
# Calculate area of square | |||
def calArea(L): | |||
return L*L | |||
property = input("Type a function: ") | |||
for L in range(1, 5): | |||
if (property == 'calPeri(L)'): | |||
print("If length is ", L, ", perimeter is ", eval(property), ".") | |||
elif (property == 'calArea(L)'): | |||
print("If length is ", L, ", area is ", eval(property), ".") | |||
else: | |||
print('Wrong Function') | |||
break | |||
The output of the above program will be: | |||
Type a function: calPeri(L) | |||
If length is 1 , perimeter is 4 . | |||
If length is 2 , perimeter is 8 . | |||
If length is 3 , perimeter is 12 . | |||
If length is 4 , perimeter is 16 . | |||
or | |||
Type a function: calArea(L) | |||
If length is 1 , area is 1 . | |||
If length is 2 , area is 4 . | |||
If length is 3 , area is 9 . | |||
If length is 4 , area is 16 . | |||
Why you should be careful while using eval()? | |||
Consider a situation, you are using a [[Unix]] system ([[macOS]], [[Linux]] etc) and you have imported "os" module. The os module provides portable way to use operating system functionalities like: read or write a file. | |||
If you allow users to input a value using "eval(input())", the user may issue commands to change file or even delete all the files using command "os.system('rm -rf *')". | |||
https://www.programiz.com/python-programming/methods/built-in/eval | |||
== See also == | == See also == | ||
Line 715: | Line 895: | ||
:* [[Linux]] | :* [[Linux]] | ||
* [[Windows]] ([[Microsoft Windows]]) | * [[Windows]] ([[Microsoft Windows]]) | ||
* [[Artificial intelligence]] | |||
:* [[Statistics]] | |||
:* [[Machine learning]] | |||
== References == | == References == |
edits