This question already has answers here:
python: what does the comma do in += s,?
(1 answer)
In threading.thread, why does args take a comma at the end [duplicate]
(1 answer)
Closed 1 year ago.
Why in this example we must put a comma after the pat and mat argument?
I searched several sites but did not find an answer
import _thread
import turtle
def f(painter):
for i in range(3):
painter.fd(50)
painter.lt(60)
def g(painter):
for i in range(3):
painter.rt(60)
painter.fd(50)
try:
pat=turtle.Turtle()
mat=turtle.Turtle()
mat.seth(180)
_thread.start_new_thread(f,(pat,))
_thread.start_new_thread(g,(mat,))
turtle.done()
except:
pass
Because its a tuple. Writing (pat) or (mat) is evaluated as part of an operation like x * (y + z). Adding the comma allows the runtime to understand you're providing a tuple or an immutable list.
Related
This question already has answers here:
How do i repeat regex
(1 answer)
Free text parsing using long regex formula leading to error: multiple repeat in python? Screenshot included
(1 answer)
Closed 2 months ago.
I try to do this:
① batRegex = re.compile(r'Bat(wo)*+man')
and this:
mo3 = batRegex.search('My name is Batwowowowowowomanman.')
mo3.group()
And I expect this:
'Batwowowowowowoman'
But I got this when I type ①:
re.error: multiple repeat at position 8
I wonder what that means.
Because * means any times of repeat and + means at least one time repeat, that's exactly what happened in the mo3. What's wrong.
Thanks:)
My python version is Python 3.10.5.
In your regex Bat(wo)*+man, the * already means to repeat the group (wo) zero or more times. Therefore, the following + is out of place. You probably intended to use Bat(?:wo)+man. Here is an updated script:
batRegex = re.compile(r'Bat(?:wo)+man')
mo3 = batRegex.search('My name is Batwowowowowowomanman.')
print(mo3.group()) # Batwowowowowowoman
This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))
Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).
This question already has answers here:
create a string of variable length in python
(3 answers)
Python Spaced Triangle
(4 answers)
Closed 6 years ago.
Here is my current code. The question is exactly in the title. (I'm looking to be able to align my triangle correctly)
def Triangle(height):
if height % 2 == 0:
print "Please enter a height that is odd"
else:
stringTriangle = "x"
for x in range(height):
print stringTriangle
for x in range(1):
stringTriangle+="xx"
To make a string containing n spaces, use " "*n. To concatenate two strings a and b, use a + b. To learn about other things that can be done with strings, see Python 2: Built-in Types: str and Other Sequence Types.
From there, you should be able to solve your homework problem by calculating how many spaces and how many asterisks you need in each line of the figure.
This question already has answers here:
calculate length of list recursively [duplicate]
(2 answers)
Closed 7 years ago.
So, my goal is to count the number of arguments in a list using recursion. However, as I'm only to use one argument in the function call, I don't know how to solve it without a second "count" argument.
So far I have this, where I accumulate the 1's together.
def countElements(a):
if a==[]:
return []
else:
return [1] + countElements(a[1:])
def main():
a=[3,2,5,3]
print(countElements(a))
main()
Instead of returning an empty list in the base case, return 0, and instead of [1] + countElements(a[1:]), return 1 + countElements(a[1:]). That way, you're just keeping the running count instead of getting a list back.
This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)