0

I know some basic C++ and I understand how to fill an array with user input via a for loop, but I would like to know how to accomplish that task in Python 3. I looked around here and there but could not really comprehend the way others were answering this question.

int size = 0;
cout << "Enter size: " << endl;
cin >> size;
array[size] = {0};
for (int i = 0; i < size; i++)
    cin >> array[i];

Hopefully someone can explain this to me in a simple way. As you can tell, I just started C++ about 5 to 6 months ago and Python3 around the middle of this month.

Thanks!

martineau
  • 119,623
  • 25
  • 170
  • 301
Bowen
  • 55
  • 8

2 Answers2

1

In C++ you need to define the array size first, in python lists you don't do that, they will grow accordingly while you add elements to them. For example to code that in python I would write the equivalent code like this (I added print statement to demonstrate a_list growing):

size = int(input("Enter size: "))  # for input 3
a_list = []
for i in range(size):
    a_list.append(input())  # for input 1, 2, 3

print('python list:', a_list)

>>> python list: ['1', '2', '3']
marcos
  • 4,473
  • 1
  • 10
  • 24
  • I ran this python code and it demonstrates this example very nicely! It is honestly crazy in how little lines I could get input into a list and sort that list, while in C++ you'd have like 4 loops just doing these tasks! I am also going to see what range does, I never seen that before! – Bowen Feb 22 '20 at 23:54
  • @Bowen check this out: `a_list = [ input() for i in range(int(input("Enter size: ")))]` – juanpa.arrivillaga Feb 23 '20 at 00:26
  • @juanpa.arrivillaga, lol thanks man your hilarious the one liner. Python is honestly crazy, I just started it, I have been learning C++ in my classes for the past 5 months and the amount of things you can do with Python in so little code is impressive. – Bowen Feb 23 '20 at 00:32
0

There is first of all one thing I want to say about the C++ part you wrote.

This part is not officially supported by C++:

cin >> size;
array[size] = {0};

Variable length arrays (VLA's) are valid in C99 and might work with some C++ compilers as a C extension, but it is not an official part of the language. In C++ they should not be used, because it might result in not portable code. Please use something like a std::vector instead.

Please keep in mind that the syntax used above in the highlighted code part is not valid. First of all a type is missing, and any form of default initialization is not valid for a VLA.

On topic:

size = int(input('Size:'))
my_array = []

for i in range(size):
    val = int(input())
    my_array.append(val)
gummy
  • 123
  • 1
  • 6
  • so are you saying that I should not use "using namespace std?" – Bowen Feb 23 '20 at 00:06
  • Oh sorry, I forgot to add the name where I was talking about. I was talking about VLA's (variable length arrays) which are part of the C language (version 99, called C99). These are NOT part of the C++ language, but are sometimes supported by some compilers as a C extension. You might want to read something more about it right here: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – gummy Feb 23 '20 at 00:39
  • `array[size] = {0}` is almost certainly invalid in C, including C99. It is an assignment, not a definition of a VLA. In C++, its meaning depends on how `array` was previously defined (e.g. what its elements are) but the result is almost certainly either a diagnosable error or undefined behaviour. – Peter Feb 23 '20 at 00:53
  • @Peter You're right about the initialization an the missing type, but working with VLA's is valid in C99. I tried to make that clear, but when reading my comment again I noticed I didn't even mention 'VLA'. In a comment above I already tried to explain the Topic Starter the point of my comment. – gummy Feb 23 '20 at 01:04