Top answers


Write a function s(n) that will return a list of the first n squares.

def s(n): ans=[] #This variable stores the list we will return. for i in range(n): #This is a for loop that will iterate n times. ans.append((i+1)**2) #This adds the (i+1)th square to the list, because range...
BM
Answered by Brodie M. • Python tutor
1605 Views

What is the difference between using range() and a list of values?

The main differences between the two boil down to the difference between greedy and lazy evaluation. With a list, each indexed object takes up some space in memory. Range, however, calculates the next value ...
LS
Answered by Luke S. • Python tutor
3669 Views

What can I do with Python?

Systems programming e.g. searching for files, launching other programs etc (sometimes called shell tools)GUI’s -tkinterInternet Scripting – web scraping (getting information from websites so you can use it y...
JD
Answered by Joshua D. • Python tutor
1875 Views

Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .

A simple solution to this problem would be as follows [x for x in list[::2] if x%2 == 0] For example, given the following list: list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ] The list comprehension [x for...
WA
Answered by Waseem A. • Python tutor
3535 Views

how to compute the factorial of a number.

def factorial(n): fact = 1 for i in range(2,n+1): fact*=i; return factx=input("Enter a number :")print("The factorial of "+x+" is "+factorial(x))
NR
Answered by Niveda R. • Python tutor
1433 Views