X=email
Y=5
$X = Y
Value of X i.e, email = Y
I want to assign Y value to value of X I.e, email
To assign variables, python does not use $, it is as simple as
X = Y
As an additional note, if you wanted X to be set to the text email, use quotes around it, like so:
X = "email"
Related
var y;
function modify(x) {
var z = 5;
x += 2;
y += x + z
}
var x = 1,
y = 2,
z = 3
modify(x)
Above snippet result is x=1,y=10,z=3 Please explain this code. Thanks in advance
In the modify function, "x" is in the scope of modify, and is not going to use the x in the global scope. In addition, primitive types, such as numbers, are passed by value instead of reference. Therefore, x is always going to remain 1.
Also in the modify function, you are declaring a new variable of z in the scope of the modify function, so just like x, z is going to remain as 3.
Since y isn't declared in the function, it's going to use the global scope, so that's the only one that's going to change. In this case it's 2 + ((1 + 2) + 5), which is how you get 10.
I am learning the basic usage of python,and I'm confusing about how the variable runs in a practice question. Here are the code below:
x = 1
def change(a):
a = x + 1
print(x)
change(x)
x = 1
def change(a)
x = x + 1
print(x)
change(x)
This is how I think the process:
in the first code:change(x) means: x = x + 1 - print (x) - output:2
but in fact the result is 1.So the real process is: x(symbol in the function) = x(global variable) + 1, print(x), this x is the global variable.
is that right?
in the second code,I think still the output should be 2,but it shows me that UnboundLocalError: local variable 'x' referenced before assignment
so in the python,we can't use function to change the global variable?
Inside a function, you can read global variables but you cannot modify them without explicitly declaring them as global like this:
x = 1
def change(a):
global x # <- this is required since you're assigning a new value to x
x = x + 1
print(x)
change(x)
In the first case, with a = x + 1, the global declaration is not required since you're only reading the value of x, not modifying it. Also, the output is 1 in the first case, since you're printing x not a.
I'm trying to create a range between two variables. The variables contain string and number characters.
For example P9160-P9163 or P360-P369.
The P is not constant and could be any character(s)/multiple, but i'm trying to generate a list that would contain all values in between.
i tried with looking at ASCII characters but didn't work for me.
Any thoughts?
x = 'P9160'
y = 'P9163'
x = re.match(r"([a-z]+)([0-9]+)", x, re.I)
y = re.match(r"([a-z]+)([0-9]+)", y, re.I)
for i in range(int(x.groups()[1]), int(y.groups()[1])+1):
print("{}{}".format(x.groups()[0], i))
Using a reusable regex pattern, and a generator expression, does certainly improves the code performance.
import re
x = 'P9160'
y = 'P9173'
# resuable regex pattern
regex = re.compile(r"([a-zA-Z]+)(\d+)")
x, y = regex.match(x), regex.match(y)
# generator expression
xy = (x.groups()[0]+str(i) for i in range(int(x.groups()[1]), int(y.groups()[1])+1))
# list of all values from the generator
print(list(xy))
I have the following code in a Python 3 http server parse out a URL and then parse out a query string:
parsedURL = urlparse(self.path)
parsed = parse_qs(parsedURL.query)
say that parsedURL.query in this case turns about to be x=7&=3.I want to get the 7 and the 3 out and set them equal to variables x and y. I've tried both
x = parsed['x']
y = parsed['y']
and
x = parsed.get('x')
y = parsed.get('y')
both of these solutions come up with x = ['7'] and y = ['3'] but I don't want the brackets and single quotes, I want just the values 7 and 3, and I want them to be integers. How do I get the values out and get rid of the brackets/quotes?
Would simply:
x = int(parsed['x'][0])
y = int(parsed['y'][0])
or
x = int(parsed.get('x')[0])
y = int(parsed.get('y')[0])
serve your purpose? You should of course have suitable validation checks, but all you want to do is convert the first element of the returned array to an int, so this code will do the business.
This is because the get() returns an array of values (I presume!) so if you try parsing url?x=1&x=2&x=foo you would get back a list like ['1', '2', 'foo']. Normally there is only one (or zero, of course) instance of each variable in a query string, so we just grab the first entry with [0].
Note the documentation for parse_qs() says:
Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.
I want x to take the value 4; why doesn't this work? What would the correct syntax be?
x=3
y=5
z=[:x; :y]
:(z[1])=4
The equivalent of &x in C++ in Julia, is to use a Ref.
x = Ref(1)
x[] # get value of x, it's 1
x[] = 2 # set value of x to 2
What you want to do is
x = Ref(3)
y = Ref(5)
z = [x, y]
z[1][] = 4
For more information, see the section on Ref in the documentation.