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]]]]
  • 1
    $\begingroup$ Have you seen this? $\endgroup$ Sep 4 '12 at 15:06
  • 1
    $\begingroup$ @stevenvh It's the name that halted me. =:-O $\endgroup$
    – 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/10.mma"

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

<< "/tmp/10.mma"

{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]}


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

Export[NotebookDirectory[] <> "test.txt", Range[10]]

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]
  • $\begingroup$ 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! $\endgroup$
    – WayneGacy
    Sep 4 '12 at 10:20
  • $\begingroup$ @WayneGacy, that's a new question. Function[in, in + RotateLeft[in]][{d1, d2, d3}] $\endgroup$
    – user21
    Sep 4 '12 at 10:24
  • $\begingroup$ Ups, thank you! $\endgroup$
    – 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]


  • 1
    $\begingroup$ Table[k,{k,1,10}] is waaay too long. Range[10] does exactly the same. $\endgroup$
    – stevenvh
    Sep 4 '12 at 10:35
  • 2
    $\begingroup$ @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... $\endgroup$ Sep 4 '12 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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