Conversation started Sep 15, 2019 at 15:18.
Sep 15, 2019 15:18
I need to print alterbate elements of a list
List is:
numbers=[67, 89,23,09,23,18,10,13]
I tried
for I in range (0, 8) :
I am getting syntax error.... What's my error
Wait I forgot semicolon after for..
But even after using it, I am getting error
if i % 2== 0 not for i % 2 == 0
Why if not for
for is a looping command i.e. it executes a block of code repeatedly.
So for i in range(0,9) executes the block code that follows it ten times, each time with i set to 0, 1, 2 etc up to i=9 the last time.
then for each repetition you want to check the value of i and print the list member if i is even.
In any case a better way to do it would be for i in range(0,9,2)
The final ,2 means increase i by 2 for each loop
Sep 15, 2019 15:27
Okay
Tbh I am still getting syntax error
What's wrong now
numbers = numbers[i]
That is doing something rather odd.
You're trying to set the whle list numbers to one of its elements.
Why not simply:
Didn't understand
= is an assignment operator
So numbers = numbers[i] sets the variable numbers to the value of numbers[i]
>>> numbers = [1,2,3]
>>> print(numbers)
[1, 2, 3]
>>> numbers = numbers[1]
>>> print(numbers)
2
Initially numbers is a list, but by setting numbers = numbers[1] it is changed to an int.
You just want to print alternate elements don't you?
Sep 15, 2019 15:35
Yes
numbers = [67,89,23,9,23,18,10,10]
for i in range(0, len(numbers))
    if i % 2 == 0:
        print(numbers[i])
Try that.
I tried differently and got it
oops, I missed a colon on the for line ...
Urs is shorter
Let me try
numbers = [67,89,23,9,23,18,10,10]
for i in range(0, len(numbers), 2):
    print(numbers[i])
is shorter still.
Sep 15, 2019 15:39
I tried this
That's a perfectly reasonable way to do it if you might need a list of the alternate numbers
But I think len(numbers) should have been more general
Instead of 8 . I made the list can be varying
It's always best to avoid hard coding numbers into your code if you can.
Where would numbers [I]be printed
How do u know it will print in list
@Aladdin I'm not sure what you are asking
Sep 15, 2019 15:45
Shouldn't the output be like:
6
23
You mean one number per line?
No ur program will print one person line right
But I want a list
D:\rhs\Python>python numbers.py
67
23
23
10
Yes, mine prints one number per line.
If you want all the numbers on one line you can use formatted printing or you can just do what you do and build up a new list.
Is there a way to convert to list
@Aladdin well that's what your code does. It builds up a new list from every alternate nuber.
Sep 15, 2019 15:49
So to make another list, we need an empty list
Without it, there's no way
That would be a good way to do it because it's nice clear code.
Ah OK
You could modify the list in place i.e. something like
for i in range(0,len(numbers)/2):
    numbers[i] = numbers[2*i]
Nice
I think this will work
I wouldn't do it that way because it looks confusing.
Clear and obvious code is always best.
Sep 15, 2019 15:53
Say I have to write a list to replace numbers with their square
Then I would need empty list for that?
Can we do modifications inside the list itself to get this condition
You could do, but I would create a new list because it's clearer
numbers = [1,2,3]
squares = []
for i in range(0,len(numbers)):
  squares.append(numbers[i]**2)
Yep
But the question says replace
OK if the question tells you to replace the number by their squares use:
numbers[i] = numbers[i]**2
Sep 15, 2019 16:13
@JohnRennie hi
@Aladdin hi
I need my list to contain numbers which have squares greater than 5 and less than 100
But my code is not working
Because the line list[i] = list[i] doesn't do anything. It leaves list[i] unchanged.
Are you allowed to create a new list, or does the question tell you to update the list in place?
I thought it would only be executed if condition is true
No new list
Wait I misinterpret the question
The question is
Create a list with numbers 0 snd 9 in which square of numbers are between 5 and 100
But I still don't understand what's wrong in my program
numbers = []
for i in range (0,10):
    if i**2 >= 5 and i**2 <= 100:
        numbers.append(i)
Sep 15, 2019 16:21
@JohnRennie can you explain what's wrong with bmy program
I didn't understand that line
Because the line list[i] = list[i] doesn't do anything. It leaves list[i] unchanged.
the line list[i]=list[i]sets the ith element of the list equal to the ith element of the list. It's like writing x=x.
Ah oops
Do u know the correction
@Aladdin Build up a new list using append
Guess that's the only way
@JohnRennie how did u learn coding
On ur own
Yes, but computers were far simpler in 1978 when my school got it's first computer :-)
Sep 15, 2019 16:31
1970's era
Yes :-)
How were teachers back in those days
We're they strict or kinda loose
My school was quite strict.
Though the teachers would encourage you if you were interested to learn.
I need to go now. I'll be back tomorrow.
Sure. Good night
 
22 hours later…
Sep 16, 2019 15:01
@JohnRennie hi
Sep 16, 2019 15:20
@Aladdin hi
How to reverse list elements
@Aladdin In place? Or into a new list?
In place
Suppose you have a list [a,b,c,d,e]
What you do is start at the end and work towards the centre swapping elements as you go.
I did
Sep 16, 2019 15:27
So we use a for loop for i in range (0,len(mylist)/2)
That goes from the left end to the middle.
I would set a new variable j = len(mylist) - i - 1
Lst=[1, 2,3,4]
Then you'll swap the elements [i] and [j].
for I in range (lena(lst)):
Lst[I]==Lst[-i-1]
print (lst)
@JohnRennie but this prints the same list...
Well let's go through it by hand. len(lst) is 4 so your four loop will execute 4 times with i set to 0, 1, 2 and 3. OK so far?
Sep 16, 2019 15:33
the second line is lst[i] = lst[-i-1]. Is that correct? On the right hand side you have a negative number as the index i.e. -i-1.
Did you mean lst[i] = lst[len(lst)-i-1] ?
Also, did you really mean == ?
That's the comparison operator not the assignment operator.
I meant =
So let's just check this. Your code is:
lst=[1,2,3,4]
for i in range(0,len(lst)):
    lst[i] = lst[len(lst)-i-1]
print(lst)
Is that definitely correct?
Lst[I]=Lst [-i-1]
My code had this
Sep 16, 2019 15:44
That can't be correct because -i-1 is a negative number. You can't have a negative number as a list index.
We need to agree on what code you are running, then you need to try running the code we've agreed on to see what happens.
Ah, my bad, Python does allow negative indexes.
Ignore what I said
A negative index means you count from the right end of the list.
@Aladdin OK. So the code is:
lst=[1,2,3,4]
for i in range(0,len(lst)):
    lst[i] = lst[-i-1]
print(lst)
Yes?
Yed
But the problem is it's not printing backwards
I thought it could...
OK. Let's start with lst=[1,2,3,4]. On our first pass through the loop i=0 so the command in the loop does lst[0] = lst[-1]. Yes?
That changes lst to [4,2,3,4]. OK so far?
Yed
Ah I can see my mistake
This won't work
Sep 16, 2019 15:53
Yes :-)
You need an intermediate variable to do the swap.
So we use a for loop for i in range (0,len(mylist)/2)
That goes from the left end to the middle.
I would set a new variable j = len(mylist) - i - 1
Why did u take my list/2
Can u explain ur code
Because you are swapping the left and right halves of the list, so you only need to work your way through half the list.
Let's take your list [1,2,3,4] and see how this works. The first swap is with i=0 so j = 3. That swaps elements 0 and 3 so the list is now: [4,2,3,1]. OK so far?
What if length of list is odd
Ok
If the length is odd we don't process the middle element. But the middle element won't move anyway because it's in the middle.
So len(lst)//2 always works.
(I forgot we need // to do integer division)
Sep 16, 2019 16:01
My full code is:
lst = [1,2,3,4]

for i in range(0,len(lst)//2):
    j = len(lst) - i - 1
    x = lst[i]
    lst[i] = lst[j]
    lst[j] = x

print(lst)
Do you want to try running that on your PC?
Yes . On it
Yep it's working
Tbh I think these codes are more maths than coding
They are teaching you how to analyse a problem and construct an algorithm to solve it.
Sep 16, 2019 16:26
@JohnRennie hi
@Aladdin hi
I need to add elements of l1 and l2 into p
Such that no elements repeat
Replace c with p
@JohnRennie hello?
@Aladdin hi, sorry I was busy for a moment.
Oh okay. Ping me when u are back
I'm free now!
The way I would do this is to first put all elements of l1 into c.
Then I would step through l2, and I would append the elements of l2 to c only if that element isn't in l1.
Sep 16, 2019 16:34
Ah right
No need to make it complicated like mine
So that's my algorithm, and it now just remains to turn it into code.
Mine was
To first put those elements which are not Same in both lists
And then put those which are equal
Guess my algorithm is right?
I'm not sure how your algorithm works. Your description of it is a bit unclear to me.
First take a list then put those elements which are not in other list
Take the other list and put those elements which are not in other list
Finally take the elements which are common
@JohnRennie is this ok
Ah, I see. Yes that will work though it seems a bit inefficient because you are having to do lots of checking of whether elements are in lists or not.
Sep 16, 2019 16:42
Figured so. But just for practice I am gonna apply both mine and urs
OK, yes that's good practice.
@JohnRennie little help
@Aladdin yes?
For my code, how do I take the value of index only once for a list if the element it represents is not common
I can't use if statement.... It's taking i multiple times
One of the reasons people like Python is that it makes lots of things easy. In particular to check if a value is in a list you can just do if x in mylist:
So suppose you are getting all the elements of l1 that are not in l2. The way to do this is:
for i in range(len(l1)):
    if not l1[i] in l2:
        c.append(l1[i])
Do you see how that works?
Sep 16, 2019 16:56
Hoe does if not work
Is it similar to if?
Yes. The operator not just converts true to false and false to true.
So:
if 1 == 1:
    print("Equal!")
else:
    print("Not equal!")
That prints "Equal!" which I guess is obvious.
If you stick a not in:
if not 1 == 1:
    print("Equal!")
else:
    print("Not equal!")
Not equal
That will then print "Not equal!.
@Aladdin yes.
 
Conversation ended Sep 16, 2019 at 16:59.