Python Forum
Basic if/then help (I'm sorry)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic if/then help (I'm sorry)
#1
Basic question from a complete noob. I'm sorry.

Testing if statement and for loops. Populating a list, creating an empty list, finding some values and appending that new empty list. I then want to just print that list.

If I print within the 'if' statement it outputs but I don't want to see the entire iteration.

I can also print the list after the program has completed.

But, I'd like the if statement to finish and then as the next command print the list.

Missing something simple / dumb here. Sorry. Suggestions / corrections?


>>> fruits=['apples', 'bananas', 'oranges']
>>> vowel_names=[]
>>> print(vowel_names)
[]
>>> for i in fruits:
...     if i[0] in "aeiou":
...         vowel_names.append(i)
... print(vowel_names)
  File "<stdin>", line 4
    print(vowel_names)
        ^
SyntaxError: invalid syntax
Reply
#2
>>> fruits=['apples', 'bananas', 'oranges']
>>> vowel_names=[]
>>> print(vowel_names)
[]
>>> for i in fruits:
...     if i[0] in "aeiou":
...         vowel_names.append(i)
...
>>> print(vowel_names)
['apples', 'oranges']
>>>
need to press enter an extra time to exit the for loop, which gets back the >>>, then put in the print
Reply
#3
I have the updated code that for testing purposes I am cutting / pasting from a editor to the console and it is still producing that error.

But, if I place the code into a file (test.py) and run the code the results are fine.

Would I need to implicitly place a CR to force a line or is there an easier way to do this from editor to console? Want to ensure I can iterate through code changes.

Thanks.

fruits=['apples', 'bananas', 'oranges']
vowel_names=[]
print(vowel_names)
for i in fruits:
    if i[0] in "aeiou":
        vowel_names.append(i)
        
print(vowel_names)
Reply
#4
My advice: DonĀ“t use IDLE
Use a text editor e.g. Sublime Text, Atom, Notepad++, etc
write your code, save file as a .py file
run that file on the command prompt with python name.py

or
If you really want to evolve your knowledge have a watch here:
Getting Started with JupyterLab (Beginner Level) | SciPy 2018 Tutorial | Jason Grout
Reply
#5
I prefer to take advantage of Python syntax which is very similar to spoken language. Easier to read and understand:

>>> fruits = ['apples', 'bananas', 'oranges']
>>> vowel_names = []
>>> for fruit in fruits:
...     if fruit.startswith((*'aeiou',)):
...         vowel_names.append(fruit)
From that is obvious that list comprehension can be even more concise:

>>> vowel_names = [fruit for fruit in fruits if fruit.startswith((*'aeiou',))]
>>> vowel_names
['apples', 'oranges']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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