Iterator python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was trying to do something like this for example
def functionx(x):
while x > 0:
x = 2 + 2
x -= 1
for x in functionx(x):
print(x)
Well, in for I wanted to print x = 2 + 2 but it just give me the direction where the function is.
Also just wanted to use x = 2 + 2, use it in another function but then use the stored number again and so on but I don't know how to do that.

Use the yield keyword.
Example
def functionx(x):
while x > 0:
x += 1
yield x
for i in functionx(1):
print i
This creates the functionx as an iterator.

Related

My code is running fine but I don't know why it shows 16 instead of (16, 2)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
The answer always comes out to be 16 but what happens to the x in function sq(func,x) because func turned into 16 but x remains 2. So it should show something like 16, 2 when I try to print it.
def sq(func,x):
y = x**2
return func(y)
def f(x):
return x**2
Calc = sq(f,2)
print(Calc)
Your print statement prints cal, which is the number returned by the function f(x), which is what is returned by the function sq(f,y).
If you want to print the structure that you need, try to do the following:
def sq(func,x):
y = x**2
return (func(y), x)
def f(x):
return x**2
Calc = sq(f,2)
print(Calc)

Replace every letter in an input with the numbers 123 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Here is my current code. I would like it to be in one line I don't know how to though so I came here.
if int(input("enter num")) %2 == 0:
print("yay")
Since your question wasn't "completely clear". Such as what the output is supposed to be, and if you wanted to compress your code into 1 line or not...
But try this:
#1st code~ returns output like: ['123ello', 'h123llo', 'he123123o', 'he123123o', 'hell123']
#2nd code ~ returns output like: 123123123123123 (#Tim Roberts's comment)
1 line versions
#1
i = input(">");print([i.replace(i[d],"123") for d in range(len(i))])
#2
x = input(">");print('123' * len(i))
Multiline versions
#1
i = input(">")
x = []
for d in range(len(i)):
x.append(i.replace(i[d],"123"))
print(x)
#2
x = input(">")
print('123' * len(i))
The following code works with regex. Does it give the desired result?
import re
i = 'i2t'
print(re.sub('[^\W\d_]', '123', i))
The output from above will be: 1232123

How Values are assigned in Anonymous function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I don't understand whether 15 is sent to x or n
def func_compute(n):
return lambda x : x * n
result = func_compute(2)
print("Double the number of 15 =", result(15))
Assume a function f(x, n) = xn. Now you want to have this function for a fixed n, i.e., f_n(x) = xn. This is what func_compute(n) does, it returns f_n(x), a function, for the given n.
result(15) is then f_2(15) = f(15, 2) = 30.

Convert a List to integer without using map() or join() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
if I have
list=[1,2,3,4,5,6]
How can i make it
list=123456
Thanks for your help in advance!
You can do this:
inlist=[1,2,3,4,5,6]
length = len(inlist)
s = 0
for i in range(length):
s += (inlist[i] * ( 10 ** (length-1-i)))
inlist = s
print(inlist)
This will give you:
123456
You need to utilize the power of 10 to multiply it with each number.
Note that you shouldn't use list as a variable name as it is a Python keyword.
Another version (without using any built-in functions at all):
inlist=[1,2,3,4,5,6]
count = 1
s = 0
for elem in inlist[::-1]:
s += (elem * ( 10 ** (count-1)))
count += 1
inlist = s
print(inlist)
you can do it by for and join likes the following:
int(''.join([str(i) for i in my_list]))

writing code - a compare function with two values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to write a program that takes two values x and y and returns
1 if x > y
0 if x == y
-1 if x < y
I cannot figure out how to start this. I know how to do one value, but not sure where to add the second.
This should work:
def func (x, y):
if x>y:
return 1
elif x==y:
return 0
else:
return -1
To get return:
output=func(4, 6)
Or whatever

Categories

Resources