Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new in Mathematica and I am trying to write numbers from 1 to 10 in txt file. But "Null" is everything that is written in my file. The code is:

       For[k = 1, k <= 10, k++, Print[List[k]]]]
share|improve this question
Have you seen this? – Dr. belisarius Sep 4 '12 at 15:06
@stevenvh It's the name that halted me. =:-O – Mr.Wizard Sep 15 '12 at 14:04

Here are some variants to achieve what you want using various notations and avoiding the use of the dreaded For loop.

Exporting as strings

Standard notation:

Export[ "/tmp/10.txt", Range@10]

Infix notation:


Exporting in Mathematica format

Save as a list, so it can easily be read back into mathematica using Get.

Range@10 >> "/tmp/"

Read it back in using the short form of the function Get:

<< "/tmp/"

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Why you might not want a For loop

For loops aren't used very much in Mathematica as there are more efficient ways to approach the need to repeat computations. Lists are fundamental to what Mathematica does and in conjunction with other techniques such as Map make For loops and Do,While redundant.


The Map function, shortform /@, which can apply a function to a list of items.

Sin/@{1,2,3,4,5,6} - > {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}


The ability of functions to apply themselves to a list,

Sin[{1,2,3,4,5,6}] -> {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}

Functions can Return Lists

( Here we use the short form of function invocation, f@arg instead of f[arg] )

Range@6 -> {1,2,3,4,5,6}


Sin@Range@6-> {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}

share|improve this answer

Recommendation: Read up on Table, Range etc. . Press F1 often!

Export[NotebookDirectory[] <> "test.txt", Range[10]]
share|improve this answer

Print is used for it's side effect (which is printing to the screen) but it returns Null. Also the For loop returns Null

Try this:

lst = {};
For[k = 1, k <= 10, k++, AppendTo[lst, {k}]]
(* {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}} *)
Export["C:\Users\Sealy\Desktop\list.txt", lst]
share|improve this answer
I have managed to export lists and to import them in Mathematica using ways you all suggested. but now, I have to sum two elements of those imported lists (for example, for {d_1, d_2, d_3} I have to find sums d_1+d_2, d_2+d_3 and d_1+d_3 and to deal with those sums that satisfy some conditions). How can I do that? I saw a lot of examples that use "#" as a variable of a list but I think I cannot use that two times (For example, If[#+#], Print[12] does not make sense)? Thank you very much! – WayneGacy Sep 4 '12 at 10:20
@WayneGacy, that's a new question. Function[in, in + RotateLeft[in]][{d1, d2, d3}] – user21 Sep 4 '12 at 10:24
Ups, thank you! – WayneGacy Sep 4 '12 at 10:27


(* Initiate an empty List *)
list = {};
(* Use AppendTo in order to add  the entries in each iteration *)
For[k = 1, k <= 10, k++, list = AppendTo[list, k]];
(* Then export the List *)
Export["C:\Users\Sealy\Desktop\list.txt", list]

However the simplest Mathematica way to do it will be by calling Table

Export["C:\Users\Sealy\Desktop\list.txt", list]


share|improve this answer
Thank you very, very much! – WayneGacy Sep 4 '12 at 9:45
Table[k,{k,1,10}] is waaay too long. Range[10] does exactly the same. – stevenvh Sep 4 '12 at 10:35
@stevenvh this was meant to introduce the very commonplace and still powerful MMA function Table to the OP as he claims to be a novice. I thought of explainign him a bit more in detail but image_dioctor has already done that... – PlatoManiac Sep 4 '12 at 11:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.