ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Jul 2021 17:36:14 +0200How to multiply a list by an integer?https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/This is so basic I'm embarrassed to even ask the question, but I've tried all the possibilities I can think of, and searched two SM guides (SM Tutorial and SM for Undergrads), and I can't find out how to do this. I want to multiply a list of rational numbers (fractions) by an integer. I've tried with lists and sets (although sets wouldn't really work, because the list of fractions contains repetitions), and I've tried with the single integer as simply a number and as a list. But I always get an error message, saying "unsupported operand type(s)" or something similar. Here is an example of what I'm trying to do:
a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4,]
b = 840
print (a ^ b)
Help for a clueless newbie would be greatly appreciated. (Also, if anyone can recommend a tutorial or guide that will explain how to do these sorts of things in SM, that would be even better!)Tue, 13 Jul 2021 18:14:40 +0200https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/Answer by FrédéricC for <p>This is so basic I'm embarrassed to even ask the question, but I've tried all the possibilities I can think of, and searched two SM guides (SM Tutorial and SM for Undergrads), and I can't find out how to do this. I want to multiply a list of rational numbers (fractions) by an integer. I've tried with lists and sets (although sets wouldn't really work, because the list of fractions contains repetitions), and I've tried with the single integer as simply a number and as a list. But I always get an error message, saying "unsupported operand type(s)" or something similar. Here is an example of what I'm trying to do:</p>
<p>a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4,]</p>
<p>b = 840 </p>
<p>print (a ^ b)</p>
<p>Help for a clueless newbie would be greatly appreciated. (Also, if anyone can recommend a tutorial or guide that will explain how to do these sorts of things in SM, that would be even better!)</p>
https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?answer=57993#post-id-57993like this
sage: a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4]
sage: b = 840
sage: b * vector(a)
(840, 420, 280, 315, 168, 280, 180, 210, 140, 210)
sage: [b * n for n in a]
[840, 420, 280, 315, 168, 280, 180, 210, 140, 210]
Tue, 13 Jul 2021 21:31:47 +0200https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?answer=57993#post-id-57993Comment by Jerry Caveney for <p>like this</p>
<pre><code>sage: a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4]
sage: b = 840
sage: b * vector(a)
(840, 420, 280, 315, 168, 280, 180, 210, 140, 210)
sage: [b * n for n in a]
[840, 420, 280, 315, 168, 280, 180, 210, 140, 210]
</code></pre>
https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=58016#post-id-58016thank you.Wed, 14 Jul 2021 17:36:14 +0200https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=58016#post-id-58016Comment by FrédéricC for <p>like this</p>
<pre><code>sage: a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4]
sage: b = 840
sage: b * vector(a)
(840, 420, 280, 315, 168, 280, 180, 210, 140, 210)
sage: [b * n for n in a]
[840, 420, 280, 315, 168, 280, 180, 210, 140, 210]
</code></pre>
https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=58004#post-id-58004You may want to learn Python first. There are many books abut Python.Wed, 14 Jul 2021 08:19:33 +0200https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=58004#post-id-58004Comment by Jerry Caveney for <p>like this</p>
<pre><code>sage: a = [1, 1/2, 1/3, 3/8, 1/5, 1/3, 3/14, 1/4, 1/6, 1/4]
sage: b = 840
sage: b * vector(a)
(840, 420, 280, 315, 168, 280, 180, 210, 140, 210)
sage: [b * n for n in a]
[840, 420, 280, 315, 168, 280, 180, 210, 140, 210]
</code></pre>
https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=57995#post-id-57995Thank you! Do you know of a site where I can learn why this works, and how similar problems have to be programmed to work?Tue, 13 Jul 2021 21:47:56 +0200https://ask.sagemath.org/question/57992/how-to-multiply-a-list-by-an-integer/?comment=57995#post-id-57995