r/learnpython • • Aug 27 '18

Create an empty list

Good monday everyone!

I just started university and is learning python. I have no experience with the language whatsoever so i was hoping on getting some help here :)
We got an assignment to get used to some of the basic functions. The task is:

Create a program which starts with an empty list and ends up with a list

containing the numbers 1-10.000. This should not be done manually like

mylist=[1,2,...,10.000]. Hint use a for-loop and append.

Been stuck at this for hours now.

appreciate all answers :)

0 Upvotes

11 comments sorted by

8

u/julsmanbr julsmanbr Aug 27 '18

What have you tried already?

Have you learned about for loops and the append method for lists?

0

u/sillycups Aug 27 '18

I haven't really tried anything yet. I have no idea where to start :)

I have only had one lecture where they showed a powerpoint with different 'commands'. We haven't typed anything yet.

7

u/julsmanbr julsmanbr Aug 27 '18 edited Aug 27 '18

If you haven't really typed a single Python command before, I suggest you try just that. Open up a Python shell, go through the presentation and see how the different commands work.

To open a Python shell in Windows, search for the IDLE application. If you can't locate it, chances are you don't have Python installed in your machine and must download it from python.org.

Most Linux/MacOS come with a bundled Python distribution, so you don't need to download it. For example, in Ubuntu you simply need to open a terminal (Ctrl + Alt + T) and type python3 to open the shell (here I assume your lectures focus on Python 3, since Python 2 is mostly legacy at this point).

Once in the shell, try assigning variables to different values. For example, type name = 'John' to assign the variable name to the string John. Press enter, type name and press enter again. Try with different variables, using numbers (number = 14), lists (cats =['bob', 'james', 'whiskers']). See what happens when you add/multiply two of the same data type together, then try adding different data types.

The reason why I'm telling you to "play" with the shell is not because I'm mean and don't want to answer your question. I could post an answer, but if you never even tried to type any Python code (let alone code related to your question), I'm afraid you would not truly "get" the answer. Hopefully as you go on training with the shell, some ideas on how to tackle your assignment will come up. But these things come only with practice: try, try again, fail, get angry at why you're failing, read some information about how Python works, get new idea, try once more, get it right. Rinse and repeat. Even advanced Python programmers only get so far by thinking about the code and not trying out their ideas.

Some questions you can google to lead you in the right direction for your assignment:

  • how do you create an empty list?

  • how do you add an element to a list?

  • how do you count up to 100 in python?

  • how do you repeat a command for an arbitrary number of times?

  • how do you show the result of a calculation to the user (show it on screen)?

5

u/sillycups Aug 27 '18

Thanks! I have played around with some of the other commands. This task was the last one on the assignment sheet so I got almost all of them :) I have gotten some good answers on what to search for and I agree it's better to figure it out for myself. Excited to learn python (and C this year) :)

5

u/[deleted] Aug 27 '18

Also, Python Crash Course has an excellent chapter on Lists and associated methods. Plus, and apologies if this sounds cryptic, but if you know how to create a list with elements, you already know the syntax to create an empty list.

Edit: Grammar

0

u/sillycups Aug 27 '18

I will be sure to check it out! :)

2

u/sheep1996 Aug 27 '18

Try googling "python populate list". You'll see how basic loops work in answers there.

1

u/sillycups Aug 27 '18

Thanks! At least now I know what I have to google :)

0

u/noodlesource Aug 27 '18

You can create an empty list:

my_list = []    

You can then use a for loop to iterate over a block of code, appending a number to your list each iteration. I'd suggest taking a look at how "for" loops work if you have never coded before.

0

u/[deleted] Aug 27 '18

[deleted]

1

u/sillycups Aug 27 '18

I got some good advice on what to google now. I will try to figure it out for myself first :)

0

u/[deleted] Aug 27 '18

[deleted]

3

u/Adantingtask Aug 27 '18

Try to guide OP to a solution instead of providing one directly.

in case you are wondering why you are being downvoted, this is taken from the commenting guidlines.