Python Forum
Super easy beginner question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Super easy beginner question
#1
Hello new to the forum and new to python,

my questions are:
- In step 3 of the below code why is 'members' recognized as a what i am guessing Boolean value, and if it is why is it considered to be 'True'
- In step 4 is the del function limited to deleting only one item from the list or was I doing something wrong? my original code was del Beatles[-1, -2]

# step 1:
Beatles = []
print("Step 1:", Beatles)

# step 2:
Beatles.append("John Lennon")
Beatles.append("Paul McCartney")
Beatles.append("George Harrison")
print("Step 2:", Beatles)

# step 3:
for members in range(2):
    Beatles.append(input("New band member: "))
print("Step 3:", Beatles)

# step 4:
del Beatles[-1]
del Beatles[-1]
print("Step 4:", Beatles)

# step 5:
Beatles.insert(0, "Ringo Starr")
print("Step 5:", Beatles)
print("The Fab:",len(Beatles))
I appreciate all of the help.

The above code is supposedly correct although it took a little longer to figure out, I guess I learn best by making mistakes but I will stick to it
Reply
#2
members is not a boolean, it is an integer of 0 or 1
you can find this out with (after line 12):
print(type(members))
Reply
#3
(Nov-06-2019, 11:15 PM)Larz60+ Wrote: members is not a boolean, it is an integer of 0 or 1
you can find this out with (after line 12):

the question is why is it automatically assigned an integer value and why is that value deemed true to continue the loop? or is that just down to compiler level code.

every time i read python code i see new variables being tossed in and I have no clue where they come from, I just want to be able to explain my thinking. I like to master basics.

I was also able to figure out how to delete more than one element at a time in my code by using the slicer

del Beatles [-2:]
but how do i do two random positions for example 0, 2
thanks
Reply
#4
range delivers numbers in the specified range, zero based so
range(2) delivers a 0 on 1st iteration, and 1 on the next
if range = 4, for example:
>>> for members in range(4):
...     print(f"members: {members}")
... 
members: 0
members: 1
members: 2
members: 3
>>>
Learn how to try things out with the interactive python shell.
to start, just type python from command line.
To exit, type quit()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() in class akbarza 1 403 Dec-19-2023, 12:55 PM
Last Post: menator01
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,380 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,538 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  Beginner question - storing values cybertron2 4 3,137 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  super newbie question: escape character tsavoSG 3 2,403 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,704 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  superclass and super() grkiran2011 1 1,693 Jun-20-2020, 04:37 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020