text
stringlengths 0
312
|
---|
print(f"{num} x {i} = {num*i}") |
# Write a python function to print powers of 2, for given number of terms |
def two_power(terms): |
result = list(map(lambda x: 2 ** x, range(terms))) |
print(f"The total terms are: {terms}") |
for i in range(terms): |
print(f"2^{i} = {result[i]}") |
# Write a program to filter the numbers in a list which are divisible by a given number |
my_list = [11, 45, 74, 89, 132, 239, 721, 21] |
num = 3 |
result = list(filter(lambda x: (x % num == 0), my_list)) |
print(f"Numbers divisible by {num} are {result}") |
# Write a python function that returns the sum of n natural numbers |
def sum_natural(num): |
if num < 0: |
print("Please enter a positive number!") |
else: |
sum = 0 |
while(num > 0): |
sum += num |
num -= 1 |
return num |
# Write a program to swap first and last elements in a list |
my_list = [1, 2, 3, 4, 5, 6] |
my_list[0], my_list[-1] = my_list[-1], my_list[0] |
# Write a python function to find the area of a circle, whose radius is given |
def findArea(r): |
PI = 3.142 |
return PI * (r*r) |
# Write a program to print the sum of squares of first n natural numbers |
n = 21 |
sum_n = 0 |
for i in range(1, n+1): |
sum_n += i**2 |
print(sum_n) |
# Write a program to print the length of a list |
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
print(len(my_list)) |
# Write a pythno function to print the length of a given tuple |
my_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
print(len(my_tuple)) |
# Write a python function to print the elements of a given list, one element in a line |
def custom_print(l): |
for _ in l: |
print(_) |
# Write a python function to remove all the odd numbers from a list and return the remaining list |
def remove_odd(my_list): |
result = list(filter(lambda x: (x % 2 == 0), my_list)) |
return result |
# Write a python function to remove all the even numbers from a list and return the remaining list |
def remove_even(my_list): |
result = list(filter(lambda x: (x % 2 != 0), my_list)) |
return result |
# Write a function that takes two lists as input and returns a zipped list of corresponding elements |
def zip_list(list1, list2): |
return list(zip(list1, list2)) |
# Write a program to to print the contents of a given file |
file_name = 'temp.txt' |
with open(file_name, 'r') as f: |
print(f.read()) |
# Write a functin that returns the LCM of two input numbers |
def lcm(a, b): |
if a>b: |
min_ = a |