Posts: 5
Threads: 1
Joined: Jan 2020
Jan-12-2020, 12:24 PM
(This post was last modified: Jan-12-2020, 12:32 PM by snippsat.)
Hello everyone I hope someone can explain this cod to me  there is the variable y [ ] – list how this code works. hope I'm not too boring. Here we have an input number but since Python is the default input string then it is a string and if i type 10 for example the result is 6 which is true but do not understand how y takes this number and also what does works hier “.split” bult-in functions and in the next line we attribute the same later to the variable ‘’d’. Thanks a lot in advance
1 2 3 4 5 6 7 8 9 10 |
c = 50
h = 30
import math
x = []
y = [i for i in input ( 'give me a number: ' ).split( ',' )]
for d in y:
x.append( str ( int ( round (math.sqrt(( 2 * c * float (d) / h))))))
print ( ',' .join(x))
|
Posts: 12,031
Threads: 485
Joined: Sep 2016
program will remain in for loop until enter button pressed, then contents will be split.
Think of it in its expanded form
1 2 3 4 5 |
>>> i = input ( 'Give me a number' )
Give me a number1, 2 , 3
>>> y = i.split( ',' )
>>> y
[ '1' , ' 2' , ' 3' ]
|
Message should specify that numbers should be separated by ',' and terminated on enter
Posts: 5
Threads: 1
Joined: Jan 2020
Thanks for your reply what bothers me most about this code is line 7 this part y = [i for i in input ('give me a number:') i know this is list comprehensions but i don't understand it how this line of code number 7 works ??
Posts: 158
Threads: 27
Joined: Jan 2019
(Jan-12-2020, 03:43 PM)Armin Wrote: Thanks for your reply what bothers me most about this code is line 7 this part y = [i for i in input ('give me a number:') i know this is list comprehensions but i don't understand it how this line of code number 7 works ??
1 |
[i for i in input ( 'give me a number: ' ).split( ',' )]
|
1 2 |
number_string = input ( 'give me a number: ' )
numbers = number_string.split( ',' )
|
It's really overkill
1 |
input ( 'give me a number: ' ).split( ',' )
|
Is functionally equivalent
Posts: 15
Threads: 0
Joined: Jan 2020
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
y = [i for i in input ( 'give me a number: ' ).split( ',' )]
^this is the expression i that will replace every instance of i where the condition is true
y = [i for i in input ( 'give me a number: ' ).split( ',' )]
< - - - - - - > setting up a for loop with i
y = [i for i in input ( 'give me a number: ' ).split( ',' )]
< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > this is actually two things stuck together.
input () returns string or empty string always.
the .split method is something you throw on the end of an object such as a string.
the .split() method always returns string, a list of strings or empty string if there is no error from type mismatch or missing arguments.
because input () returns a string, input ().split is totally fine just like "1,2,3" .split( ',' )
also something = "1,2,3"
print (something.split( ',' ))
output
1 2 3
|
for loops work on an object that is iterable. the list ['1', ' 2', ' 3'] is iterable. 1 is not iterable.
Posts: 5
Threads: 1
Joined: Jan 2020
Thanks a lot for helping me understand this but now if I enter say 10 in this case the string it will be .split () will be output to '1' and '0' as then in the following line I assign y the value of 10 variables di then inside the formula I get result 6? I apologize in advance if I'm bored, but I've been messing with this code all day and can't handle it completely.
Thank you all very much
Posts: 12,031
Threads: 485
Joined: Sep 2016
Quote:if I enter say 10 in this case the string it will be .split () will be output to '1' and '0'
That's not how split works.
split requires a delimiter. If none is supplied space is assumed.
the results of a split are returned in a list
remember, the best way to learn is to try.
bring up the python interpreter so you can try for yourself (just type python3 at command line)
some examples:
example 1, no delimiter specified, so default space is used
1 2 3 |
>>> string1 = "This is a string"
>>> string1.split()
[ 'This' , 'is' , 'a' , 'string' ]
|
example 2 specifing a delimiter:
1 2 3 4 |
>>> string2 = "This string has a hyphenated-word in it"
>>> string2.split( "-" )
[ 'This string has a hyphenated' , 'word in it' ]
>>>
|
useful for separating items delimited with special characters:
example3:
1 2 3 4 5 |
>>> data = "10/18/2016|ALA|16|970|827|540|2337"
>>> dlist = data.split( '|' )
>>> dlist
[ '10/18/2016' , 'ALA' , '16' , '970' , '827' , '540' , '2337' ]
>>>
|
Posts: 158
Threads: 27
Joined: Jan 2019
Are you trying to split something like 752 into [7, 5, 2]?
If so then read about integer math.
You will find the modulo and integer division or particular interest.
Posts: 5
Threads: 1
Joined: Jan 2020
Posts: 5
Threads: 1
Joined: Jan 2020
I understand split nouw but what i can't understand is this part of the code in line 7
“y = [i for i in input “
this part of the code and why this code is there because the program works without this part of the code if we put in line 7 above, for example
y = input('give me a number: ').split(',')
we get the same result. I understand that this is the work of codo o comprehensive list but not understood why it's there"i in i for input " in lijn 7.
Thanks in advance
|