Creating a Function which converts a Integer into a string - python-3.x

I'm currently struggling immensely with my python module and I need to "define" a function that converts an integer to a string. I'm really confused about programming in general and was wondering if anyone could help provide an analysis and description on how I would solve this problem / where I'm going wrong with my approach. I know the code is simple but i'm lost on where to even start.
x=12
def Converter():
str(x)
print (x)

def Converter(x):
return str(x)
x = 12
result = Converter(x)
print(result)

Two things: 1. You should pass the parameter to function.
2. You should print the result.
x = 12
def Converter(int_number):
return str(int_number)
print(Converter(x))

Related

Return value of a python function

I am new to python. Does all python function return some value? What will be the return value of the following function?
import math
def getQuadratic(a,b):
square = a**2 + b**2
squareRoot = math.sqrt(square)
return squareRoot
print("The square root of the sum of the squares of 3 and 4 is:", getQuadratic(3,4))
To evaluate the type returned by a Python function, you need to look at EVERY return statement, because each may return something different. And if a function doesn't have an explicit return, there's an implied return None at the end.
In your case there's only one return, and it's easy to figure out what type it's returning.
All python methods return something. Even if you have no return statement they will still return None.
def my_function():
pass
print(my_function())
>>> None
This function is definitely returning something. It's returning the value of the variable squareRoot. So when you're executing the print statement, the value that was returned is getting printed along with the string ahead of it.
Python being a dynamically typed language, does not require you to define any type for variables or functions.
Everything in python is a first class object.
On your example:
import math
def getQuadratic(a,b):
square = a2 + b2 # is this square? a**2?
squareRoot = math.sqrt(square)
return squareRoot
print("The square root of the sum of the squares of 3 and 4 is:", getQuadratic(3,4))
The variables a, b can take on any value.
Python does this by making everything a python object.
So, its easier to think of this way. int is your equivalent in C, however, in python it is treated as class <int>
However, static typing is now possible. It still depends on framework for static typing to be utilized. But your equivalent python program is interpreted as:
import math
def getQuadratic(a:int,b:int) -> float:
square = a2 + b2 # is this square? a**2?
squareRoot = math.sqrt(square)
return squareRoot
print("The square root of the sum of the squares of 3 and 4 is:", getQuadratic(3,4))
TL;DR
In direct answer to your question,
python does not require any types
a = 1 is valid.
a = 'SoF' is still valid
where a is a Python Object, and can be allocated to any other python object such as value, string, functions, or entire modules.
It really doesn't make much difference in types. Its the way Python is designed.

How to give a Subclip a new name in Python using PyAutoGUI?

Complete beginner here making my first program with PyAutoGui as I cannot get access to the software's API. My issues are currently being that I am unable to come up with a solution to name each subclip with a different appended letter on the end. The naming convention should go like so, MI899001~AA, MI899001~AB, MI899001~AC, MI899001~AD. The only thing that changes is the last letter.
Below is the relevant code I'm currently using for the program I am writing;
def naming_promo():
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][0])
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo() # See above Fn for method
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()
The naming_promo() takes the ascii_uppercase and types the first letter. I however can't figure out how to iterate through each character in the string as the For Loop repeats. I've googled many solutions, but I guess I can't get my head around how to do a loop in a loop and increment the x value used each time.
This is my first post so apologies for any etiquette I break. Any help and explanation would be greatly appreciated.
This is my first answer so apologies for any etiquette I break.
I'm not sure I understand everything here, since there's a few functions in the code that I don't know about. However, are you just looking for something like:
def naming_promo(n):
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][n])
and further down in your code, simply create a variable and increment it up one after use:
m = 0
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo(m) # See above Fn for method
m += 1
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()

Why does assigning lambdas not work as expected in Python 3.5?

I know Python is supposed to have first class functions, and I've written some Python which "confirms" that, but the following explodes (runs out of stack depth) my IDLE in 3.5:
k = lambda x:x+1
k = lambda x:k (x+1)
k(0)
Shouldn't it evaluate to 2?
k = lambda x:x+1
j = lambda x:k (x+1)
j(0)
certainly does.
Visualize with lambda
A little printing may help to understand what is going on:
k = lambda x: x+1
print('before:', id(k))
k = lambda x: print('in :', id(k))
k(0)
print('after :', id(k))
before: 4428659024
in : 4428658208
after : 4428658208
The id of the last lambda is the same as the one use inside it. This nicely demonstrates the late binding.
Translate into function statements
Things might a bit clearer if you translate the lambdas into function define with def:
def k(x):
return x + 1
def k(x):
return k(x+1)
k(0)
It is pretty clear that the first definition does make sense as it is overridden by the second one.
In addition, even you use different names for the functions and re-assign the function name:
def k(x):
return x + 1
def j(x):
return k(x+1)
k = j
it appears to be clearer that the first function definition doesn't make sense because the function can never be reached.
The name lookup of k is done at runtime, which means that the function calls itself an infinite number of times (and that the first line does nothing useful). If you want the result you expect then you will have to use the ternary operator to return immediately instead of recursing.

Creating a function that returns index of minimum value in a list?

def minimum_index(xs):
minimum_index=xs[0]
for i in range(len(xs)):
if xs[i]<xs[i+1]:
min_i=i
elif xs[i]>xs[i+1]:
min_i=i+1
continue
return minimum_index
This looks correct to me, but for some reason, I keep trying to change things around and I either get an incorrect return value or no return value.
Simplify the function
def minimum_index(xs):
ans = 0
for i in range(1, len(xs)):
if xs[i] < xs[ans]:
ans = i
return ans
or in a more pythonic way
minimum_index = lambda xs: xs.index(min(xs))
Your code has at least two issues: You seem to have two variables that stand for the minimal index, and you mix them up. Also, it is not enough to compare subsequent elements, you will have to compare to the minimal value. Try this:
def minimum_index(xs):
minx = xs[0]
mini = 0
for i in range(1,len(xs)):
if xs[i]<minx:
mini = i
minx = xs[i]
return mini
If you are using numpy, then you can simply use their numpy.argmin(xs).

Evaluating and modifying theano tensors when stuff is on GPU

I am seriously stuck with something for ages now. I need some help.
I am running a theano conv network on GPU.
The network has a loss function as such
def mse(x, t):
return T.mean((x - t) ** 2)
Here x is the predicted value of a rectified liner unit and t is the expected value.
Now for a particular learning problem I am trying to modify the function such that I want to threshold the value of x. So essentially something simple as this
x[x>ts] = ts
But I am really struggling with this. I tried so many things
ts = 0.91
Y = T.vector()
#x_update = (x, T.set_subtensor(x[(x > ts).eval()], Y))
#f = function([Y], updates=[x_update])
#v=np.empty(len((x > ts).eval()))
#v.fill(ts)
#f(v)
#x.shape.eval()
x_arr = x.flatten()
print type(x_arr)
print type(t)
print type(x)
#print T.shape(x_arr).eval()
#print x.shape.eval()
#print x_arr.shape.eval()
#print t.shape.eval()
#print x.eval()
#print x_arr.get_value()
#x_newarr = x_arr.eval()
#x_newarr[x_newarr>ts] = ts
#x = T.shared(x_newarr)
return T.mean((x - t) ** 2)
Apart from the three prints, which all print <class 'theano.tensor.var.TensorVariable' > everything else gives me error.
So I am at my wits end how to do this simple stuff.
Is it because this stuff is on GPU ?
I did test the code on local python prompt, by constructing a numpy array and converting it into a tensor shared variable. The different stuff above works.
But I am conscious that the type is theano.tensor.sharedvar.TensorSharedVariable and not theano.tensor.var.TensorVariable.
I would really appreciate if some one gives me a helping hand here.
Regards
Please find the answer to this question given by pascal at
https://groups.google.com/forum/#!topic/theano-users/cNnvw2rUHc8
The failures are correct because the input values are not being provided at the time the function is being called, since it is symbolic.
The answer is to use T.minimum(x,threshold)

Resources