Syntax question

P

pmz

Dear Group,

It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.

Could anyone give some light on line, as following:
"ds = d[:]" ### where 'd' is an array

Let me guess, is it a declaration of two-dimension array?

Thanks a lot for help and all the best,
Przemek M. Zawada
 
C

Chris Rebert

Dear Group,

It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.

Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

Let me guess, is it a declaration of two-dimension array?

Nope; Python doesn't really have variable declarations.*
That line of code copies the list `d` ( `[]` is the slicing operator,
and the colon indicates the bounds are the entire list; this is a
Python idiom) and assigns the copy to the variable `ds`. Note that the
copying is shallow (i.e. not recursive, only 1 level deep).

*Well, almost: There are `global` and `nonlocal`, but they're only
needed to specify a variable's scope in certain circumstances when you
want to be able to assign to said variable.

Cheers,
Chris
 
G

geremy condra

Dear Group,

It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.

Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].

The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:
a = [1,2,3,4]
b = a
c = [:]
b[0] = 5
b [5,2,3,4]
# here's the issue
a [5,2,3,4]
# and the resolution
c
[1,2,3,4]

Hope this helps.

Geremy Condra
 
P

pmz

Dear Group,
It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.
Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].

The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:
a = [1,2,3,4]
b = a
c = [:]
b[0] = 5
b [5,2,3,4]
# here's the issue
a [5,2,3,4]
# and the resolution
c

[1,2,3,4]

Hope this helps.

Geremy Condra

Thank you for such fast answer! I quite catch, but:
As I see, the d[:] is equal to sentence "get the d array from the
first to the last element"? :)

P.
 
M

Matteo Landi

Anyway I suggest you to use a syntax like:

in order to copy a list, it should be better than slicing.

Dear Group,

It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.

Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].

The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:
a = [1,2,3,4]
b = a
c = [:]
b[0] = 5
b [5,2,3,4]
# here's the issue
a [5,2,3,4]
# and the resolution
c
[1,2,3,4]

Hope this helps.

Geremy Condra
 
P

pmz

Anyway I suggest you to use a syntax like:

in order to copy a list, it should be better than slicing.





Dear Group,
It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.
Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array
I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].
The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:
a = [1,2,3,4]
b = a
c = [:]
b[0] = 5
b [5,2,3,4]
# here's the issue
a [5,2,3,4]
# and the resolution
c [1,2,3,4]

Hope this helps.
Geremy Condra

In fact, that ain't my syntax, I'd rather use C++ for that project,
because that's my world is not Python, but thank you anyway for help -
I see that Python also has many fans and friends online :) I'll try
help her using your explanations.

THANK you again and all the best,
Przemek M. Zawada
 
M

Matteo Landi

Yes it is; d[i:j] is equal to "give me the array from the d to d[j
- 1]", and if you omit i and j then the i and j are respectively
assumed as 0 and len(d) - 1.

Dear Group,
It's really rookie question, but I'm currently helping my wife in some
python-cases, where I'm non-python developer and some of syntax-diffs
make me a bit confused.
Could anyone give some light on line, as following:
"ds = d[:]"  ### where 'd' is an array

I'm guessing you mean that d is a list. The square
braces with the colon is python's slicing notation,
so if I say [1,2,3,4][0] I get a 1 back, and if I say
[1,2,3,4][1:4] I get [2,3,4]. Python also allows a
shorthand in slicing, which is that if the first index
is not provided, then it assumes 0, and that if the
second index is not provided, it assumes the end
of the list. Thus, [1,2,3,4][:2] would give me [1,2]
and [1,2,3,4][2:] would give me [3, 4]. Here, neither
has been provided, so the slice simply takes the
items in the list from beginning to end and returns
them- [1,2,3,4][:] gives [1,2,3,4].

The reason someone would want to do this is
because lists are mutable data structures. If you
fire up your terminal you can try the following
example:
a = [1,2,3,4]
b = a
c = [:]
b[0] = 5
b [5,2,3,4]
# here's the issue
a [5,2,3,4]
# and the resolution
c

[1,2,3,4]

Hope this helps.

Geremy Condra

Thank you for such fast answer! I quite catch, but:
As I see, the d[:] is equal to sentence "get the d array from the
first to the last element"? :)

P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top