How to make an array of n elements? (n being and input number from the user)

How to make an array of n elements? (n being and input number from the user)

Attached: python.png (200x200, 8K)

Other urls found in this thread:

docs.python.org/3.5/library/array.html
twitter.com/NSFWRedditVideo

an* input number

arr = [0] * 30

You don't need to do this. Arrays are mutable objects without a fixed size. Append as many elements to the array as you want, as you need them.

If you really want to do this:

list = range(n)

I don't know ughhh fuck

var dicks = range(input ("google is hard do my homework"))

What's an array to you and why are you using it

I barely know anything about programming but I would hazard a guess that you:
>Create array
>Get input n from user
>Loop for range n+1
>Append array with whatever element you want

Is it really more complicated than that?

>Append array
Think about what the formal definition of an array offers. It's immutable. Typically this behavior is forbidden.
Also allocation doesn't require you initialize every value.

Many languages have fixed-size arrays that you need to initialize before populating them. Python lists are a bit unique. They're incredibly simple, but you pay for it in speed.

Although some more modern languages like go have slices, which are an abstraction layer on top of arrays that make them similar to python lists in some aspects.

for for OP's homework it would technically work but not be a fast option or translate well into other languages?

How would you accomplish this in something like C?

scanf and malloc

you need to define what those elements are, surely. Your code structure is probably weird and not suited to Python.
Perhaps when you get to putting elements in the array, loop over adding to array a user inputted amount of times maybe? I guess that's assuming you can get the elements to be correct from within a for loop.
Can you provide more context?

Not him but python wants to speed things up for you. Giving it an array size and letting python handling it is more than fast enough.
I'm on my phone from a long car ride but the gist of this in C would be
scanf("%d", num)
int a[num]

Holy shit, are you guys fucking stupid?

Using numpy:

import numpy as np
n = raw_input("Enter your number: ")
my_arr = np.zeros(n)

Without using numpy:

n = raw_input("Enter your number: ")
my_list = [0 for i in range(n)]

It's literally a one liner. Do you guys not know how to program?

That's what I suggested but I didn't know what OP was trying to put in the for loop.

You're clearly a high schooler, college freshman, or CS wasn't your major. What does an array being immutable have to do with anything when it comes to Python? The behavior of appending to an array is not forbidden. Hell, even if we're not talking about Python, you can do this shit in Java with an ArrayList or C++ with a vector. Also, allocation DOES require you to initialize every value in C++ because it doesn't clear the memory values once you allocate.

Are you kidding? Dude... are you really worried about optimizing the creation of an array? And you're worried THAT MUCH that you would want to create this in C? This is what's wrong with '''programmer''' wannabes. You don't need to fucking optimize the creation of an array. Python will do just fine.

How do you know it's weird and not suited to Python? Jesus christ. It's not that hard of a question and I'm not even OP. It's just a simple question. This is what's wrong with people that think they know what they're doing. They overcomplicate the question and don't answer simple fucking questions like in the OP. You guys remind me of fucking stackoverflow.

Do you wanna chill, you edgy fuck?
I assumed his structure was weird because I thought that there weas a possibility he was used to having to define how many variables would be in an array before doing things to it, but that's not how Python works. It was a guess because OP's question is literally one line.

Were you the 3rd post in this thread? If so, I congratulate you. You were the only sensible, logical, and straight-to-the-point answer in this thread (along with my post, of course).

I was

Don't name variables after types.
No "var", and in Python 3 range is an iterator and does not return a list.
Correct.

>Don't name variables after types.
This was just quick pseudocode. If you actually write python, you should be using pylint or flake8 in your editor and it will yell at you for this.

Ayy, that was me. All those years of self-doubt and late nights weren't for nothing.

>They're incredibly simple, but you pay for it in speed.
No you don't. A Python list behaves, performance-wise, just like an array in any other language.

Came here to post this. Rangelets BTFOd.

>he thinks python doesn't perform like shit

>he thinks Python is as slow as a computer from the 90s
>he doesn't know about language bindings for python libraries
>he doesn't know about language bindings for python libraries, of which there is an extensive list of
>he doesn't know about C++ LANGUAGE BINDINGS for python libraries
>he doesn't know many libraries allow Python to run almost as fast as C++

>he uses for loops to iterate over his lists 1 million times

No wonder losers like you think Python is slow.

CPython is slow. C is fast and you can call it from any language. Hell, you can call it from languages with a good FFI with almost no effort.

That's not at all what I said, I know more than well that Python performs like shit. What I said was that lists in Python perform the same way as arrays in other languages. Constant factors aside.

>hey, if you just don't write your code in python, but just call it from python, it doesn't run as slow as python programs do

Attached: retard-3.png (263x200, 21K)

Holy fuck stop helping this kid with his homework, or if you're gonna help him throw some red herrings in there like a few posts in this thread.

>What I said was that lists in Python perform the same way as arrays in other languages.
But this is also patently false. Think about the memory management. An array allocates a block of memory. Any value can be referenced by a single pointer and an index. A list might initially be represented by a single pointer, but as it grows, it will outgrow the block of memory it started in. So now you need to either copy the entire array to a new block of memory, or write a layer of abstraction around the memory management to combine an arbitrary number of arrays into a single array for the user. Both options come at a performance cost.

>Holy shit, are you guys fucking stupid?
t."""pythonista""" who is tok stupid to use codetags

It was not homework (OP here)

You don't magically append shit to an array in vectors, you rewrite all the shit and include the new shit when you run out of memory

are you in computer science kindergarten or something?

>but as it grows, it will outgrow the block of memory it started in
This is also true for any other language. As long as you don't change the size of the list, it will perform just like any other array, and when you do, it'll need to be reallocated just like any other flexible array implementation.

I was talking in general about an array. Once you create an array, you don't get to "append to it". Python arrays are atypical from the formal definition since they ARE mutable. I'm trying to point out that this one of those special things python does without using special data structures.
Was this about C++? Typically, you don't need to initialize every element. Some languages may give reference to garbage data, others may calloc for you.

int a[num]
has to evaluate at compile time, you can't just provide num later

variable length arrays were added in c99

yes you can declare your array like that, but when you come to compile it, the actual numerical value of num has to be known, it cannot be provided at runtime

class NumberGetter:
def __init__(self):
self.number = input("n> ")
def __int__(self):
return int(self.number)

def generateList(size):
outList = []
for i in range(0, size):
outList.append(i)
return outList

myNumber = int(NumberGetter())
myList = generateList(myNumber)

Attached: 1421712849160.png (942x739, 93K)

There are no arrays in python. The most common datatype you'll be working with are lists. Since lists are mutable, they don't need to be initialized in any way. Pythons dynamic typing allows you to be pretty lazy with this. Also, learn how to use google you lazy fuck.

I'm and don't mean to bump this shitty thread further, but dammit user, that deserves a (you).

>There are no arrays in python.
docs.python.org/3.5/library/array.html
For non-changing arrays only.
>The most common datatype you'll be working with are lists.
Which are also dynamic arrays under the hood and not linked list.

what kind of brainlet question is that

Yes, there are libraries for arrays - what I mean is,for the most part, lists replace arrays. And the datatype is called list in python, eventhough it's not a linked list, I am aware of that.
I am not saying you're incorrect, but the specifics will likely confuse a beginner.

While you're right from a "theoretical" point of view, a language that is itself very easy and dynamic while at the same time having very tight, well-integrated bindings to any "fast" library you need is, in a holistic sense, very fast.

No one cares about pure python.

Attached: enterprise-3.gif (320x236, 2.93M)

Isn't the discussion about Python?

for loops?

l=[]
for i in range(input()):
l.append('Put whatever you want here')


python is for soyboys so why are you asking here

That's better than rewriting everything from scratch.

Ideal software is not practically attainable. It's not even theoretically attainable. Modular components will never be a perfect fit, there will always be shoehorning and inefficiency. There will always be legacy code and ugly dependencies.

It's still vastly prefereable to use what you have to get things done, making incremental improvements along the way, than masturbating over "ideal software" in your head, or worse, starting off on grandiose "correct" rewrites from "first principles".

While true in theory to some extent, this is the exact arguments that bad programmers use to excuse their poor software, and is also the reason why we need 4 GHz computers with 16 GB RAM to run text editors. Keep your laziness in check, senpai.

Why are so many people posting how to make lists? Arrays are immutable, and the equivalent data structure in Python is a tuple, not a list. Sure, you can use a list and you’re probably better off with lists if you’re adapting code from another language, but the formal equivalent is a tuple.

> While you're right from a "theoretical" point of view
> It's not even theoretically attainable.

you sure do like to abuse the term "theoretical"

This is also a valid point. I think the biggest failure of "good enough" is that too often "enough" is interpreted from the developer's perspective rather than the user's.

In the first instance, the term was in quotations and used figuratively. In the second instance, I was referring specifically to the fact that, given desideratum X of a program (i.e. that it is guaranteed to be free of faults, that it runs faster than any other equivalent program, etc.) is it mathematically impossible to guarantee optimality in the general case. The wider point is that, all things considered, it's very often not worth trying for optimality.

gj user. even a javascript loser like myself can understand this.