Linked Questions

264 votes
13 answers
256k views

I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n? def f(n, x): n = 2 x....
FMc's user avatar
  • 42.5k
2 votes
1 answer
386 views

Example1: x =[] def func(): print('x value is:', x) x.append('member1') print('x value is now:', x) func() print('x value is', x) After run, the result will ('x value is:', []) ('x ...
Niuya's user avatar
  • 424
0 votes
1 answer
154 views

The code below imports a linked list from LinkedQfile and creates a list object with some node objects. If I run this code the output from check_something() becomes CD . I thought linked_list in ...
Nekroz's user avatar
  • 169
0 votes
0 answers
58 views

I'm learning Python, and trying to figure out why the language treats numbers and arrays differently as global vs. local variables. Here's an example: when I write a function to increment a numeric ...
0 votes
0 answers
57 views

I use python 2.7. Say I have an array X and a function that centers this array according to means : import numpy as np X = np.array([[float(i+j) for i in range(3)] for j in range(3)]) If we print X ...
nsaura's user avatar
  • 331
2 votes
0 answers
45 views

Why in the first code the function update created a local variable with the same name (integ) without affecting the global integ while in the second code it changed the global value of list and didn't ...
1 vote
2 answers
2k views

I was testing with something and I wrote this code for example purposes.I felt the output I got was weird.I expected that function calls are executed one after the other but according to this code ...
ubuntu_noob's user avatar
  • 2,375
2 votes
2 answers
449 views

I am a bit confused about, changing (mutating or appending) variables from within functions. For reference, i found this question about functions that mutate the argument. which describes doing just ...
0 votes
1 answer
828 views

I am trying to implement a minimax algorithm in python, but I am having some issues creating the branching tree structure. I am still an amateur programmer so please don't mind if my code seems bad. ...
3 votes
3 answers
199 views

I have a question about the name scope of variables, I've tried to find the answer here and I got a similar one but still little confused here Function changes list values and not variable values in ...
Enkri_'s user avatar
  • 181
2 votes
4 answers
108 views

I have the following example in my book, which is supposed to be a program whch calculates the new balance after interest is added on multiple accounts: def addInterest(balances, rate): for i in ...
Ovi's user avatar
  • 573
0 votes
1 answer
74 views

I am making new program in Python (Mastermind). I have a problem with references of variables: def user_turn(): try_counter = 1 user_code = [] guessed_code = random_code() print("...
-1 votes
1 answer
58 views

I've got this function that removes the second data value from a list (simplified). However when executing the function, the original list seems to get modified even though I'm only doing something to ...
0 votes
1 answer
47 views

Function 1 def scale(data, factor): for j in data: j = j * factor target = [1,2,3,4,5] factors = 4 scale(target, factors) print(target) Output = [1, 2, 3, 4, 5] Function 2 def scale(...
1 vote
1 answer
30 views

In Python 3, I want to define a function that takes in a list a, does some operations and then returns some property of a. The problem is that I want a to stay unchanged. Take for example the ...

15 30 50 per page
1
2