i want to create a list of random integers
I want the value of num to be a random integer The following line of code gives me a syntax error invalid syntax
i just want to know that is there any way to do this using list comprehension
numbers = [num for num in range(1,6) num = random.randint(1,10)]
CodePudding user response:
Looking at your requirements, you aren't required to assign num
.
To generate a list of random numbers simply do:
numbers = [random.randint(1,10) for _ in range(1,6)]
You can alternatively do:
numbers = random.sample(range(1,11),k=5) # k is the repeating count
CodePudding user response:
You don't need the num
variable, just use the random.randint()
call.
numbers = [random.randint(1,10) for _ in range(1,6)]
CodePudding user response:
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.