Python Forum
Multiplication between a list and a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplication between a list and a variable
#1
How do I code a multiplication between a list and a variable? The result would be all the values of the list multiplied by the variable. Is it possible? Example:

a=int(input("a=")) b=int(input("b=")) x=a-b l=[1,2,3,4,5] y=x*l print(y)
Reply
#2
>>> a = 7 >>> b = 11 >>> x = a-b >>> l = [1,2,3,4,5] >>> print([n * x for n in l]) [-4, -8, -12, -16, -20]
Reply
#3
(Oct-08-2019, 04:02 AM)Larz60+ Wrote:
>>> a = 7 >>> b = 11 >>> x = a-b >>> l = [1,2,3,4,5] >>> print([n * x for n in l]) [-4, -8, -12, -16, -20]
Great! Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string using variable found in a list japo85 2 2,683 Jul-11-2022, 08:52 AM
Last Post: japo85
  Multiplication Table code alexsendlegames100 3 2,870 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 4,242 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 2,970 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  An IF statement with a List variable dedesssse 3 27,188 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 8,113 Jan-26-2021, 07:14 PM
Last Post: quest_
Question Matching variable to a list index Gilush 17 10,771 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Print variable values from a list of variables xnightwingx 3 4,408 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  List conversion and multiplication johnkyp 5 5,273 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 2,867 Dec-19-2019, 06:16 PM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.