Description
stringlengths 9
105
| Link
stringlengths 45
135
| Code
stringlengths 10
26.8k
| Test_Case
stringlengths 9
202
| Merge
stringlengths 63
27k
|
---|---|---|---|---|
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for i in range(0, len(myList)):
result = result * myList[i]
return result
# Driver code
list1 = [1, 2, 3]
list2 = [3, 2, 4]
print(multiplyList(list1))
print(multiplyList(list2)) |
#Output : 6 | Python | Multiply all numbers in the list
# Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for i in range(0, len(myList)):
result = result * myList[i]
return result
# Driver code
list1 = [1, 2, 3]
list2 = [3, 2, 4]
print(multiplyList(list1))
print(multiplyList(list2))
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python3 program to multiply all values in the
# list using lambda function and accumulate()
from itertools import accumulate
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = list(accumulate(list1, (lambda x, y: x * y)))
result2 = list(accumulate(list2, (lambda x, y: x * y)))
print(result1[-1])
print(result2[-1]) |
#Output : 6 | Python | Multiply all numbers in the list
# Python3 program to multiply all values in the
# list using lambda function and accumulate()
from itertools import accumulate
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = list(accumulate(list1, (lambda x, y: x * y)))
result2 = list(accumulate(list2, (lambda x, y: x * y)))
print(result1[-1])
print(result2[-1])
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | def product_recursive(numbers):
# base case: list is empty
if not numbers:
return 1
# recursive case: multiply first element by product of the rest of the list
return numbers[0] * product_recursive(numbers[1:])
list1 = [1, 2, 3]
product = product_recursive(list1)
print(product) # Output: 6
list2 = [3, 2, 4]
product = product_recursive(list2)
print(product) # Output: 24 |
#Output : 6 | Python | Multiply all numbers in the list
def product_recursive(numbers):
# base case: list is empty
if not numbers:
return 1
# recursive case: multiply first element by product of the rest of the list
return numbers[0] * product_recursive(numbers[1:])
list1 = [1, 2, 3]
product = product_recursive(list1)
print(product) # Output: 6
list2 = [3, 2, 4]
product = product_recursive(list2)
print(product) # Output: 24
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | from functools import reduce
from operator import mul
list1 = [1, 2, 3]
result = reduce(mul, list1)
print(result) |
#Output : 6 | Python | Multiply all numbers in the list
from functools import reduce
from operator import mul
list1 = [1, 2, 3]
result = reduce(mul, list1)
print(result)
#Output : 6
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", list1[0]) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", list1[0])
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort(reverse=True)
# printing the first element
print("Smallest element is:", list1[-1]) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort(reverse=True)
# printing the first element
print("Smallest element is:", list1[-1])
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 1, 45, 99]
# printing the maximum element
print("Smallest element is:", min(list1)) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 1, 45, 99]
# printing the maximum element
print("Smallest element is:", min(list1))
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python program to find smallest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Smallest element is:", min(list1)) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python program to find smallest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Smallest element is:", min(list1))
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python program to find smallest
# number in a list
l = [int(l) for l in input("List:").split(",")]
print("The list is ", l)
# Assign first element as a minimum.
min1 = l[0]
for i in range(len(l)):
# If the other element is min than first element
if l[i] < min1:
min1 = l[i] # It will change
print("The smallest element in the list is ", min1) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python program to find smallest
# number in a list
l = [int(l) for l in input("List:").split(",")]
print("The list is ", l)
# Assign first element as a minimum.
min1 = l[0]
for i in range(len(l)):
# If the other element is min than first element
if l[i] < min1:
min1 = l[i] # It will change
print("The smallest element in the list is ", min1)
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python code to print smallest element in the list
lst = [20, 10, 20, 1, 100]
print(min(lst, key=lambda value: int(value))) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python code to print smallest element in the list
lst = [20, 10, 20, 1, 100]
print(min(lst, key=lambda value: int(value)))
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | lst = [20, 10, 20, 1, 100]
a, i = min((a, i) for (i, a) in enumerate(lst))
print(a) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
lst = [20, 10, 20, 1, 100]
a, i = min((a, i) for (i, a) in enumerate(lst))
print(a)
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # Python code to print smallest element in the list
from functools import reduce
lst = [20, 10, 20, 15, 100]
print(reduce(min, lst)) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# Python code to print smallest element in the list
from functools import reduce
lst = [20, 10, 20, 15, 100]
print(reduce(min, lst))
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | import heapq
def find_smallest(numbers):
# Build a min heap using the elements in the list
heap = [(x, x) for x in numbers]
heapq.heapify(heap)
# Return the root element (smallest element)
_, smallest = heapq.heappop(heap)
return smallest
# Test the function
numbers = [10, 20, 4, 45, 99]
print(find_smallest(numbers)) # Output: 4
# This code is contributed by Edula Vinay Kumar Reddy | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
import heapq
def find_smallest(numbers):
# Build a min heap using the elements in the list
heap = [(x, x) for x in numbers]
heapq.heapify(heap)
# Return the root element (smallest element)
_, smallest = heapq.heappop(heap)
return smallest
# Test the function
numbers = [10, 20, 4, 45, 99]
print(find_smallest(numbers)) # Output: 4
# This code is contributed by Edula Vinay Kumar Reddy
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | def Findsmall(itr, ele, list1): # recursive function to find smallest number
if itr == len(list1): # base condition
print("The smallest number in the list is ", ele)
return
if list1[itr] < ele: # check the current element less than minimum or not
ele = list1[itr]
Findsmall(itr + 1, ele, list1) # recursive function call
return
# driver code
lis = [5, 7, 2, 8, 9]
ele = lis[0]
Findsmall(0, ele, lis)
# This code is contributed by Vinay Pinjala | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
def Findsmall(itr, ele, list1): # recursive function to find smallest number
if itr == len(list1): # base condition
print("The smallest number in the list is ", ele)
return
if list1[itr] < ele: # check the current element less than minimum or not
ele = list1[itr]
Findsmall(itr + 1, ele, list1) # recursive function call
return
# driver code
lis = [5, 7, 2, 8, 9]
ele = lis[0]
Findsmall(0, ele, lis)
# This code is contributed by Vinay Pinjala
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # importing module
import numpy as np
# Initializing list
lis = [5, 7, 2, 8, 9]
# finding minimum value
minimum = np.min(lis)
# printing output
print("The smallest number in the list is", minimum)
# This code contributed by tvsk | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# importing module
import numpy as np
# Initializing list
lis = [5, 7, 2, 8, 9]
# finding minimum value
minimum = np.min(lis)
# printing output
print("The smallest number in the list is", minimum)
# This code contributed by tvsk
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | # defining the list
arr = [5, 2, 3, 2, 5, 4, 7, 9, 7, 10, 15, 68]
# converting the list into set
set_arr = set(arr)
# Now using the min function to get the minimum
# value from the set
print(min(set_arr)) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
# defining the list
arr = [5, 2, 3, 2, 5, 4, 7, 9, 7, 10, 15, 68]
# converting the list into set
set_arr = set(arr)
# Now using the min function to get the minimum
# value from the set
print(min(set_arr))
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find smallest number in a list | https://www.geeksforgeeks.org/python-program-to-find-smallest-number-in-a-list/ | arr = [2, 6, 8, 4, 9, 7, 52, 3, 6, 2, 4, 5, 6, 8, 2]
min_val = min(arr) # Finding the minimum value
values = {}
# print item with position
for pos, val in enumerate(arr):
if val == min_val:
values.update({pos: val}) # pos - Index of the smallest element
# val - The value of the smallest element
# get all min values
print(values) | #Input : list1 = [10, 20, 4]
#Output : 4 | Python program to find smallest number in a list
arr = [2, 6, 8, 4, 9, 7, 52, 3, 6, 2, 4, 5, 6, 8, 2]
min_val = min(arr) # Finding the minimum value
values = {}
# print item with position
for pos, val in enumerate(arr):
if val == min_val:
values.update({pos: val}) # pos - Index of the smallest element
# val - The value of the smallest element
# get all min values
print(values)
#Input : list1 = [10, 20, 4]
#Output : 4
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the last element
print("Largest element is:", list1[-1]) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the last element
print("Largest element is:", list1[-1])
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Python program to find largest
# number in a list
# List of numbers
list1 = [10, 20, 4, 45, 99]
# Printing the maximum element
print("Largest element is:", max(list1)) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Python program to find largest
# number in a list
# List of numbers
list1 = [10, 20, 4, 45, 99]
# Printing the maximum element
print("Largest element is:", max(list1))
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Python program to find largest
# number in a list
# Creating an empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# Iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# Printing maximum element
print("Largest element is:", max(list1)) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Python program to find largest
# number in a list
# Creating an empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# Iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# Printing maximum element
print("Largest element is:", max(list1))
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Python program to find largest
# number in a list
def myMax(list1):
# Assume first number in list is largest
# initially and assign it to variable "max"
max = list1[0]
# Now traverse through the list and compare
# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in list1:
if x > max:
max = x
# after complete traversing the list
# return the "max" value
return max
# Driver code
list1 = [10, 20, 4, 45, 99]
print("Largest element is:", myMax(list1)) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Python program to find largest
# number in a list
def myMax(list1):
# Assume first number in list is largest
# initially and assign it to variable "max"
max = list1[0]
# Now traverse through the list and compare
# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in list1:
if x > max:
max = x
# after complete traversing the list
# return the "max" value
return max
# Driver code
list1 = [10, 20, 4, 45, 99]
print("Largest element is:", myMax(list1))
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Python code to find the largest number
# in a list
def maxelement(lst):
# displaying largest element
# one line solution
print(max(lst))
# driver code
# input list
lst = [20, 10, 20, 4, 100]
# the above input can also be given as
# lst = list(map(int, input().split()))
# -> taking input from the user
maxelement(lst) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Python code to find the largest number
# in a list
def maxelement(lst):
# displaying largest element
# one line solution
print(max(lst))
# driver code
# input list
lst = [20, 10, 20, 4, 100]
# the above input can also be given as
# lst = list(map(int, input().split()))
# -> taking input from the user
maxelement(lst)
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # python code to print largest element in the list
lst = [20, 10, 20, 4, 100]
print(max(lst, key=lambda value: int(value))) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# python code to print largest element in the list
lst = [20, 10, 20, 4, 100]
print(max(lst, key=lambda value: int(value)))
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | from functools import reduce
lst = [20, 10, 20, 4, 100]
largest_elem = reduce(max, lst)
print(largest_elem) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
from functools import reduce
lst = [20, 10, 20, 4, 100]
largest_elem = reduce(max, lst)
print(largest_elem)
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | # Function to find the largest element in the list
def FindLargest(itr, ele, list1):
# Base condition
if itr == len(list1):
print("Largest element in the list is: ", ele)
return
# Check max condition
if ele < list1[itr]:
ele = list1[itr]
# Recursive solution
FindLargest(itr + 1, ele, list1)
return
# input list
list1 = [2, 1, 7, 9, 5, 4]
FindLargest(0, list1[0], list1) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
# Function to find the largest element in the list
def FindLargest(itr, ele, list1):
# Base condition
if itr == len(list1):
print("Largest element in the list is: ", ele)
return
# Check max condition
if ele < list1[itr]:
ele = list1[itr]
# Recursive solution
FindLargest(itr + 1, ele, list1)
return
# input list
list1 = [2, 1, 7, 9, 5, 4]
FindLargest(0, list1[0], list1)
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | import heapq
# list of numbers
list1 = [10, 20, 4, 45, 99]
# finding the largest element using heapq.nlargest()
largest_element = heapq.nlargest(1, list1)[0]
# printing the largest element
print("Largest element is:", largest_element) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
import heapq
# list of numbers
list1 = [10, 20, 4, 45, 99]
# finding the largest element using heapq.nlargest()
largest_element = heapq.nlargest(1, list1)[0]
# printing the largest element
print("Largest element is:", largest_element)
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-largest-number-in-a-list/ | import numpy as np
# given list
list1 = [2, 7, 5, 64, 14]
# converting list to numpy array
arr = np.array(list1)
# finding largest numbers using np.max() method
num = arr.max()
# printing largest number
print(num) | #Input : list1 = [10, 20, 4]
#Output : 20 | Python program to find largest number in a list
import numpy as np
# given list
list1 = [2, 7, 5, 64, 14]
# converting list to numpy array
arr = np.array(list1)
# finding largest numbers using np.max() method
num = arr.max()
# printing largest number
print(num)
#Input : list1 = [10, 20, 4]
#Output : 20
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | # Python program to find second largest
# number in a list
# list of numbers - length of
# list should be at least 2
list1 = [10, 20, 4, 45, 99]
mx = max(list1[0], list1[1])
secondmax = min(list1[0], list1[1])
n = len(list1)
for i in range(2, n):
if list1[i] > mx:
secondmax = mx
mx = list1[i]
elif list1[i] > secondmax and mx != list1[i]:
secondmax = list1[i]
elif mx == secondmax and secondmax != list1[i]:
secondmax = list1[i]
print("Second highest number is : ", str(secondmax)) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
# Python program to find second largest
# number in a list
# list of numbers - length of
# list should be at least 2
list1 = [10, 20, 4, 45, 99]
mx = max(list1[0], list1[1])
secondmax = min(list1[0], list1[1])
n = len(list1)
for i in range(2, n):
if list1[i] > mx:
secondmax = mx
mx = list1[i]
elif list1[i] > secondmax and mx != list1[i]:
secondmax = list1[i]
elif mx == secondmax and secondmax != list1[i]:
secondmax = list1[i]
print("Second highest number is : ", str(secondmax))
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | # Python program to find largest number
# in a list
# List of numbers
list1 = [10, 20, 20, 4, 45, 45, 45, 99, 99]
# Removing duplicates from the list
list2 = list(set(list1))
# Sorting the list
list2.sort()
# Printing the second last element
print("Second largest element is:", list2[-2]) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
# Python program to find largest number
# in a list
# List of numbers
list1 = [10, 20, 20, 4, 45, 45, 45, 99, 99]
# Removing duplicates from the list
list2 = list(set(list1))
# Sorting the list
list2.sort()
# Printing the second last element
print("Second largest element is:", list2[-2])
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | # Python program to find second largest number
# in a list
# List of numbers
list1 = [10, 20, 4, 45, 99]
# new_list is a set of list1
new_list = set(list1)
# Removing the largest element from temp list
new_list.remove(max(new_list))
# Elements in original list are not changed
# print(list1)
print(max(new_list)) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
# Python program to find second largest number
# in a list
# List of numbers
list1 = [10, 20, 4, 45, 99]
# new_list is a set of list1
new_list = set(list1)
# Removing the largest element from temp list
new_list.remove(max(new_list))
# Elements in original list are not changed
# print(list1)
print(max(new_list))
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | # Python program to find second largest
# number in a list
# creating list of integer type
list1 = [10, 20, 4, 45, 99]
"""
# sort the list
list1.sort()
# print second maximum element
print("Second largest element is:", list1[-2])
"""
# print second maximum element using sorted() method
print("Second largest element is:", sorted(list1)[-2]) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
# Python program to find second largest
# number in a list
# creating list of integer type
list1 = [10, 20, 4, 45, 99]
"""
# sort the list
list1.sort()
# print second maximum element
print("Second largest element is:", list1[-2])
"""
# print second maximum element using sorted() method
print("Second largest element is:", sorted(list1)[-2])
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | def findLargest(arr):
secondLargest = 0
largest = min(arr)
for i in range(len(arr)):
if arr[i] > largest:
secondLargest = largest
largest = arr[i]
else:
secondLargest = max(secondLargest, arr[i])
# Returning second largest element
return secondLargest
# Calling above method over this array set
print(findLargest([10, 20, 4, 45, 99])) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
def findLargest(arr):
secondLargest = 0
largest = min(arr)
for i in range(len(arr)):
if arr[i] > largest:
secondLargest = largest
largest = arr[i]
else:
secondLargest = max(secondLargest, arr[i])
# Returning second largest element
return secondLargest
# Calling above method over this array set
print(findLargest([10, 20, 4, 45, 99]))
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | def secondmax(arr):
sublist = [x for x in arr if x < max(arr)]
return max(sublist)
if __name__ == "__main__":
arr = [10, 20, 4, 45, 99]
print(secondmax(arr)) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
def secondmax(arr):
sublist = [x for x in arr if x < max(arr)]
return max(sublist)
if __name__ == "__main__":
arr = [10, 20, 4, 45, 99]
print(secondmax(arr))
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | # python code to print second largest element in list
lst = [10, 20, 4, 45, 99]
maximum1 = max(lst)
maximum2 = max(lst, key=lambda x: min(lst) - 1 if (x == maximum1) else x)
print(maximum2) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
# python code to print second largest element in list
lst = [10, 20, 4, 45, 99]
maximum1 = max(lst)
maximum2 = max(lst, key=lambda x: min(lst) - 1 if (x == maximum1) else x)
print(maximum2)
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | lst = [10, 20, 4, 45, 99]
m = max(lst)
x = [a for i, a in enumerate(lst) if a < m]
print(max(x)) |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
lst = [10, 20, 4, 45, 99]
m = max(lst)
x = [a for i, a in enumerate(lst) if a < m]
print(max(x))
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | import heapq
def find_second_largest(numbers):
# Build a max heap using the elements in the list
heap = [(-x, x) for x in numbers]
heapq.heapify(heap)
# Remove the root element (largest element)
heapq.heappop(heap)
# The new root element is the second largest element
_, second_largest = heapq.heappop(heap)
return second_largest
# Test the function
numbers = [10, 20, 4, 45, 99]
print(find_second_largest(numbers)) # Output: 45 |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
import heapq
def find_second_largest(numbers):
# Build a max heap using the elements in the list
heap = [(-x, x) for x in numbers]
heapq.heapify(heap)
# Remove the root element (largest element)
heapq.heappop(heap)
# The new root element is the second largest element
_, second_largest = heapq.heappop(heap)
return second_largest
# Test the function
numbers = [10, 20, 4, 45, 99]
print(find_second_largest(numbers)) # Output: 45
#Output : Second highest number is : 45
[END] |
Python program to find second largest number in a list | https://www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/ | import numpy as np
def find_second_largest(arr):
# creating numpy array
np_arr = np.array(arr)
# getting sorted indices
sorted_indices = np.argsort(np_arr)
# finding the second last index from sorted indices
second_last_index = sorted_indices[-2]
# returning the element at the second last index from original array
return np_arr[second_last_index]
# example usage
arr = [10, 20, 4, 45, 99]
print(find_second_largest(arr)) # Output: 45 |
#Output : Second highest number is : 45 | Python program to find second largest number in a list
import numpy as np
def find_second_largest(arr):
# creating numpy array
np_arr = np.array(arr)
# getting sorted indices
sorted_indices = np.argsort(np_arr)
# finding the second last index from sorted indices
second_last_index = sorted_indices[-2]
# returning the element at the second last index from original array
return np_arr[second_last_index]
# example usage
arr = [10, 20, 4, 45, 99]
print(find_second_largest(arr)) # Output: 45
#Output : Second highest number is : 45
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print Even Numbers in a List
# Initializing list and value
list1 = [10, 24, 4, 45, 66, 93]
num = 0
# Uing while loop
while num < len(list1):
# Cecking condition
if list1[num] % 2 == 0:
print(list1[num], end=" ")
# increment num
num += 1 | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print Even Numbers in a List
# Initializing list and value
list1 = [10, 24, 4, 45, 66, 93]
num = 0
# Uing while loop
while num < len(list1):
# Cecking condition
if list1[num] % 2 == 0:
print(list1[num], end=" ")
# increment num
num += 1
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print even Numbers in a List
# Initializing list
list1 = [10, 21, 4, 45, 66, 93]
# using list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print("Even numbers in the list: ", even_nos) | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print even Numbers in a List
# Initializing list
list1 = [10, 21, 4, 45, 66, 93]
# using list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print("Even numbers in the list: ", even_nos)
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print even no's using lambda exp.
even_nos = list(filter(lambda x: (x % 2 == 0), list1))
print("Even numbers in the list: ", even_nos) | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print even no's using lambda exp.
even_nos = list(filter(lambda x: (x % 2 == 0), list1))
print("Even numbers in the list: ", even_nos)
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print
# even numbers in a list using recursion
def evennumbers(list, n=0):
# base case
if n == len(list):
exit()
if list[n] % 2 == 0:
print(list[n], end=" ")
# calling function recursively
evennumbers(list, n + 1)
# Initializing list
list1 = [10, 21, 4, 45, 66, 93]
print("Even numbers in the list:", end=" ")
evennumbers(list1) | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print
# even numbers in a list using recursion
def evennumbers(list, n=0):
# base case
if n == len(list):
exit()
if list[n] % 2 == 0:
print(list[n], end=" ")
# calling function recursively
evennumbers(list, n + 1)
# Initializing list
list1 = [10, 21, 4, 45, 66, 93]
print("Even numbers in the list:", end=" ")
evennumbers(list1)
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | list1 = [2, 7, 5, 64, 14]
for a, i in enumerate(list1):
if i % 2 == 0:
print(i, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
list1 = [2, 7, 5, 64, 14]
for a, i in enumerate(list1):
if i % 2 == 0:
print(i, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | list1 = [2, 7, 5, 64, 14]
for i in list1:
if i % 2 != 0:
pass
else:
print(i, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
list1 = [2, 7, 5, 64, 14]
for i in list1:
if i % 2 != 0:
pass
else:
print(i, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python code To print all even numbers
# in a given list using numpy array
import numpy as np
# Declaring Range
temp = [2, 7, 5, 64, 14]
li = np.array(temp)
# printing odd numbers using numpy array
even_num = li[li % 2 == 0]
print(even_num) | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python code To print all even numbers
# in a given list using numpy array
import numpy as np
# Declaring Range
temp = [2, 7, 5, 64, 14]
li = np.array(temp)
# printing odd numbers using numpy array
even_num = li[li % 2 == 0]
print(even_num)
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # python program to print all even no's in a list
# defining list with even and odd numbers
list1 = [39, 28, 19, 45, 33, 74, 56]
# traversing list using for loop
for element in list1:
if not element & 1: # condition to check even or not
print(element, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# python program to print all even no's in a list
# defining list with even and odd numbers
list1 = [39, 28, 19, 45, 33, 74, 56]
# traversing list using for loop
for element in list1:
if not element & 1: # condition to check even or not
print(element, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Python program to print all even no's in a list
# Defining list with even and odd numbers
# Initializing list
list1 = [39, 28, 19, 45, 33, 74, 56]
# Traversing list using for loop
for element in list1:
# condition to check even or not
if element | 1 != element:
print(element, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Python program to print all even no's in a list
# Defining list with even and odd numbers
# Initializing list
list1 = [39, 28, 19, 45, 33, 74, 56]
# Traversing list using for loop
for element in list1:
# condition to check even or not
if element | 1 != element:
print(element, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Using the reduce function from the functools module
from functools import reduce
list1 = [39, 28, 19, 45, 33, 74, 56]
even_numbers = reduce(lambda x, y: x + [y] if y % 2 == 0 else x, list1, [])
for num in even_numbers:
print(num, end=" ")
# This code is contributed by Jyothi pinjala | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Using the reduce function from the functools module
from functools import reduce
list1 = [39, 28, 19, 45, 33, 74, 56]
even_numbers = reduce(lambda x, y: x + [y] if y % 2 == 0 else x, list1, [])
for num in even_numbers:
print(num, end=" ")
# This code is contributed by Jyothi pinjala
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | import numpy as np
# given list
list1 = [2, 7, 5, 64, 14]
# converting list to numpy array
arr = np.array(list1)
# finding even numbers using where() method
even_num = arr[np.where(arr % 2 == 0)]
# printing even numbers
print(even_num) | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
import numpy as np
# given list
list1 = [2, 7, 5, 64, 14]
# converting list to numpy array
arr = np.array(list1)
# finding even numbers using where() method
even_num = arr[np.where(arr % 2 == 0)]
# printing even numbers
print(even_num)
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print even numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/ | # Using the filterfalse function from the itertools module
from itertools import filterfalse
# Test list1
list1 = [39, 28, 19, 45, 33, 74, 56]
# filtering even number
even_numbers = filterfalse(lambda y: y % 2, list1)
# Printing result
for num in even_numbers:
print(num, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14] | Python program to print even numbers in a list
# Using the filterfalse function from the itertools module
from itertools import filterfalse
# Test list1
list1 = [39, 28, 19, 45, 33, 74, 56]
# filtering even number
even_numbers = filterfalse(lambda y: y % 2, list1)
# Printing result
for num in even_numbers:
print(num, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [2, 64, 14]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 != 0:
print(num, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 != 0:
print(num, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
i = 0
# using while loop
while i < len(list1):
# checking condition
if list1[i] % 2 != 0:
print(list1[i], end=" ")
# increment i
i += 1 | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
i = 0
# using while loop
while i < len(list1):
# checking condition
if list1[i] % 2 != 0:
print(list1[i], end=" ")
# increment i
i += 1
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
only_odd = [num for num in list1 if num % 2 == 1]
print(only_odd) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
only_odd = [num for num in list1 if num % 2 == 1]
print(only_odd)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print odd no's using lambda exp.
odd_nos = list(filter(lambda x: (x % 2 != 0), list1))
print("Odd numbers in the list:", odd_nos) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print odd no's using lambda exp.
odd_nos = list(filter(lambda x: (x % 2 != 0), list1))
print("Odd numbers in the list:", odd_nos)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd numbers in a List
lst = [10, 21, 4, 45, 66, 93, 11]
for i in lst:
if i % 2 == 0:
pass
else:
print(i, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd numbers in a List
lst = [10, 21, 4, 45, 66, 93, 11]
for i in lst:
if i % 2 == 0:
pass
else:
print(i, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print
# odd numbers in a list using recursion
def oddnumbers(list, n=0):
# base case
if n == len(list):
exit()
if list[n] % 2 != 0:
print(list[n], end=" ")
# calling function recursively
oddnumbers(list, n + 1)
list1 = [10, 21, 4, 45, 66, 93, 11]
print("odd numbers in the list:", end=" ")
oddnumbers(list1) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print
# odd numbers in a list using recursion
def oddnumbers(list, n=0):
# base case
if n == len(list):
exit()
if list[n] % 2 != 0:
print(list[n], end=" ")
# calling function recursively
oddnumbers(list, n + 1)
list1 = [10, 21, 4, 45, 66, 93, 11]
print("odd numbers in the list:", end=" ")
oddnumbers(list1)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | list1 = [2, 7, 5, 64, 14]
for a, i in enumerate(list1):
if i % 2 != 0:
print(i, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
list1 = [2, 7, 5, 64, 14]
for a, i in enumerate(list1):
if i % 2 != 0:
print(i, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
import numpy as np
# list of numbers
list1 = np.array([10, 21, 4, 45, 66, 93])
only_odd = list1[list1 % 2 == 1]
print(only_odd) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd Numbers in a List
import numpy as np
# list of numbers
list1 = np.array([10, 21, 4, 45, 66, 93])
only_odd = list1[list1 % 2 == 1]
print(only_odd)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # List of numbers
list1 = [9, 5, 4, 7, 2]
for ele in list1:
if ele & 1: # Checking the element odd or not
print(ele, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# List of numbers
list1 = [9, 5, 4, 7, 2]
for ele in list1:
if ele & 1: # Checking the element odd or not
print(ele, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # List of numbers
list1 = [9, 5, 4, 7, 2]
for ele in list1:
if ele | 1 == ele: # Checking the element odd or not
print(ele, end=" ") | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# List of numbers
list1 = [9, 5, 4, 7, 2]
for ele in list1:
if ele | 1 == ele: # Checking the element odd or not
print(ele, end=" ")
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | def is_odd(number):
return number % 2 == 1
def print_odd_numbers(numbers):
odd_numbers = list(filter(is_odd, numbers))
return odd_numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(print_odd_numbers(numbers)) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
def is_odd(number):
return number % 2 == 1
def print_odd_numbers(numbers):
odd_numbers = list(filter(is_odd, numbers))
return odd_numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(print_odd_numbers(numbers))
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | import numpy as np
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# convert list to numpy array
arr = np.array(list1)
# find indices where elements are odd
idx = np.where(arr % 2 != 0)
# extract elements at odd indices
only_odd = arr[idx]
# print only odd elements
print(only_odd) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
import numpy as np
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# convert list to numpy array
arr = np.array(list1)
# find indices where elements are odd
idx = np.where(arr % 2 != 0)
# extract elements at odd indices
only_odd = arr[idx]
# print only odd elements
print(only_odd)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-print-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
from functools import reduce
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# Using reduce method in list
odd_list = reduce(lambda a, b: a + [b] if b % 2 else a, list1, [])
print(odd_list) | Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5] | Python program to print odd numbers in a List
# Python program to print odd Numbers in a List
from functools import reduce
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# Using reduce method in list
odd_list = reduce(lambda a, b: a + [b] if b % 2 else a, list1, [])
print(odd_list)
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python program to print all even numbers in range
for even_numbers in range(4, 15, 2):
# here inside range function first no denotes starting,
# second denotes end and
# third denotes the interval
print(even_numbers, end=" ") | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python program to print all even numbers in range
for even_numbers in range(4, 15, 2):
# here inside range function first no denotes starting,
# second denotes end and
# third denotes the interval
print(even_numbers, end=" ")
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 == 0:
print(num, end=" ") | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 == 0:
print(num, end=" ")
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# creating even starting range
start = start + 1 if start & 1 else start
# create a list and printing element
# contains Even numbers in range
[print(x) for x in range(start, end + 1, 2)] | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# creating even starting range
start = start + 1 if start & 1 else start
# create a list and printing element
# contains Even numbers in range
[print(x) for x in range(start, end + 1, 2)]
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | def even(num1, num2):
if num1 > num2:
return
print(num1, end=" ")
return even(num1 + 2, num2)
num1 = 4
num2 = 15
even(num1, num2) | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
def even(num1, num2):
if num1 > num2:
return
print(num1, end=" ")
return even(num1 + 2, num2)
num1 = 4
num2 = 15
even(num1, num2)
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python code To print all even numbers
# in a given range using the lambda function
a = 4
b = 15
li = []
for i in range(a, b + 1):
li.append(i)
# printing odd numbers using the lambda function
even_num = list(filter(lambda x: (x % 2 == 0), li))
print(even_num) | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python code To print all even numbers
# in a given range using the lambda function
a = 4
b = 15
li = []
for i in range(a, b + 1):
li.append(i)
# printing odd numbers using the lambda function
even_num = list(filter(lambda x: (x % 2 == 0), li))
print(even_num)
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | x = [i for i in range(4, 15 + 1) if i % 2 == 0]
print(*x) | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
x = [i for i in range(4, 15 + 1) if i % 2 == 0]
print(*x)
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | a = 4
b = 15
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a % 2 == 0]) | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
a = 4
b = 15
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a % 2 == 0])
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | a = 4
b = 15
for i in range(a, b + 1):
if i % 2 != 0:
pass
else:
print(i, end=" ") | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
a = 4
b = 15
for i in range(a, b + 1):
if i % 2 != 0:
pass
else:
print(i, end=" ")
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python code To print all even numbers
# in a given range using numpy array
import numpy as np
# Declaring Range
a = 4
b = 15
li = np.array(range(a, b + 1))
# printing odd numbers using numpy array
even_num = li[li % 2 == 0]
print(even_num) | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python code To print all even numbers
# in a given range using numpy array
import numpy as np
# Declaring Range
a = 4
b = 15
li = np.array(range(a, b + 1))
# printing odd numbers using numpy array
even_num = li[li % 2 == 0]
print(even_num)
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python program to print even Numbers in given range
# using bitwise & operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if not (num & 1):
print(num, end=" ")
# this code is contributed by vinay pinjala. | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python program to print even Numbers in given range
# using bitwise & operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if not (num & 1):
print(num, end=" ")
# this code is contributed by vinay pinjala.
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all even numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-even-numbers-in-a-range/ | # Python program to print even Numbers in given range
# using bitwise | operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if not (num | 1) == num:
print(num, end=" ")
# this code is contributed by tvsk | Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14 | Python program to print all even numbers in a range
# Python program to print even Numbers in given range
# using bitwise | operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if not (num | 1) == num:
print(num, end=" ")
# this code is contributed by tvsk
Input: start = 4, end = 15
Output: 4, 6, 8, 10, 12, 14
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print odd Numbers in given range
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 != 0:
print(num, end=" ") | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print odd Numbers in given range
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 != 0:
print(num, end=" ")
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print Even Numbers in given range
start = int(input("Enter the start of range:"))
end = int(input("Enter the end of range:"))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 != 0:
print(num) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print Even Numbers in given range
start = int(input("Enter the start of range:"))
end = int(input("Enter the end of range:"))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num % 2 != 0:
print(num)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Uncomment the below two lines for taking the User Inputs
# start = int(input("Enter the start of range: "))
# end = int(input("Enter the end of range: "))
# Range declaration
start = 5
end = 20
if start % 2 != 0:
for num in range(start, end + 1, 2):
print(num, end=" ")
else:
for num in range(start + 1, end + 1, 2):
print(num, end=" ") | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Uncomment the below two lines for taking the User Inputs
# start = int(input("Enter the start of range: "))
# end = int(input("Enter the end of range: "))
# Range declaration
start = 5
end = 20
if start % 2 != 0:
for num in range(start, end + 1, 2):
print(num, end=" ")
else:
for num in range(start + 1, end + 1, 2):
print(num, end=" ")
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# create a list that contains only Even numbers in given range
even_list = range(start, end + 1)[start % 2 :: 2]
for num in even_list:
print(num, end=" ") | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print Even Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# create a list that contains only Even numbers in given range
even_list = range(start, end + 1)[start % 2 :: 2]
for num in even_list:
print(num, end=" ")
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python code To print all odd numbers
# in a given range using the lambda function
a = 3
b = 11
li = []
for i in range(a, b + 1):
li.append(i)
# printing odd numbers using the lambda function
odd_num = list(filter(lambda x: (x % 2 != 0), li))
print(odd_num) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python code To print all odd numbers
# in a given range using the lambda function
a = 3
b = 11
li = []
for i in range(a, b + 1):
li.append(i)
# printing odd numbers using the lambda function
odd_num = list(filter(lambda x: (x % 2 != 0), li))
print(odd_num)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | def odd(num1, num2):
if num1 > num2:
return
if num1 & 1:
print(num1, end=" ")
return odd(num1 + 2, num2)
else:
return odd(num1 + 1, num2)
num1 = 5
num2 = 15
odd(num1, num2) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
def odd(num1, num2):
if num1 > num2:
return
if num1 & 1:
print(num1, end=" ")
return odd(num1 + 2, num2)
else:
return odd(num1 + 1, num2)
num1 = 5
num2 = 15
odd(num1, num2)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | x = [i for i in range(4, 15 + 1) if i % 2 != 0]
print(*x) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
x = [i for i in range(4, 15 + 1) if i % 2 != 0]
print(*x)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | a = 4
b = 15
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a % 2 != 0]) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
a = 4
b = 15
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a % 2 != 0])
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | a = 4
b = 15
for i in range(a, b + 1):
if i % 2 == 0:
pass
else:
print(i, end=" ") | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
a = 4
b = 15
for i in range(a, b + 1):
if i % 2 == 0:
pass
else:
print(i, end=" ")
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print Even Numbers in given range
# Range declaration
a = 4
b = 15
# create a list that contains only Even numbers in given range
l = filter(lambda a: a % 2, range(a, b + 1))
print(*l) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print Even Numbers in given range
# Range declaration
a = 4
b = 15
# create a list that contains only Even numbers in given range
l = filter(lambda a: a % 2, range(a, b + 1))
print(*l)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print odd Numbers in given range
# using bitwise & operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num & 1 != 0:
print(num, end=" ")
# this code is contributed by vinay pinjala. | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print odd Numbers in given range
# using bitwise & operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num & 1 != 0:
print(num, end=" ")
# this code is contributed by vinay pinjala.
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python program to print odd Numbers in given range
# using bitwise | operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num | 1 == num:
print(num, end=" ") | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python program to print odd Numbers in given range
# using bitwise | operator
start, end = 4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num | 1 == num:
print(num, end=" ")
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | import numpy as np
# Range declaration
a = 3
b = 15
# Create an array containing numbers in the range
arr = np.arange(a, b + 1)
# Create a new array containing only even numbers
evens = arr[arr % 2 != 0]
# Print the array of even numbers
print(*evens)
# This code is contributed by Jyothi pinjala | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
import numpy as np
# Range declaration
a = 3
b = 15
# Create an array containing numbers in the range
arr = np.arange(a, b + 1)
# Create a new array containing only even numbers
evens = arr[arr % 2 != 0]
# Print the array of even numbers
print(*evens)
# This code is contributed by Jyothi pinjala
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | a = 4
b = 15
for i in range(a, b + 1):
if i % 2 == 0:
continue
else:
print(i, end=" ")
# This Code is contributed by Pratik Gupta (guptapratik) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
a = 4
b = 15
for i in range(a, b + 1):
if i % 2 == 0:
continue
else:
print(i, end=" ")
# This Code is contributed by Pratik Gupta (guptapratik)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to print all odd numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-odd-numbers-in-a-range/ | # Python 3 code to demonstrate
# print odd number in range
import itertools
# Range declaration
a = 3
b = 15
# using filterfalse function
evens = list(itertools.filterfalse(lambda x: x % 2 == 0, range(a, b + 1)))
# Print the array of even numbers
print(*evens) | Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15 | Python program to print all odd numbers in a range
# Python 3 code to demonstrate
# print odd number in range
import itertools
# Range declaration
a = 3
b = 15
# using filterfalse function
evens = list(itertools.filterfalse(lambda x: x % 2 == 0, range(a, b + 1)))
# Print the array of even numbers
print(*evens)
Input: start = 4, end = 15
Output: 5, 7, 9, 11, 13, 15
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
even_count, odd_count = 0, 0
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] % 2 == 0:
even_count += 1
else:
odd_count += 1
# increment num
num += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
even_count, odd_count = 0, 0
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] % 2 == 0:
even_count += 1
else:
odd_count += 1
# increment num
num += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
odd_count = len(list(filter(lambda x: (x % 2 != 0), list1)))
# we can also do len(list1) - odd_count
even_count = len(list(filter(lambda x: (x % 2 == 0), list1)))
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
odd_count = len(list(filter(lambda x: (x % 2 != 0), list1)))
# we can also do len(list1) - odd_count
even_count = len(list(filter(lambda x: (x % 2 == 0), list1)))
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
only_odd = [num for num in list1 if num % 2 == 1]
odd_count = len(only_odd)
print("Even numbers in the list: ", len(list1) - odd_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to print odd Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
only_odd = [num for num in list1 if num % 2 == 1]
odd_count = len(only_odd)
print("Even numbers in the list: ", len(list1) - odd_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# using recursion
even_count = 0 # even counter
i = 0 # index, so that we can check if list[i] is even or odd
odd_count = 0 # odd counter
def evenoddcount(lst):
# defining local counters as global variable
global even_count
global odd_count
global i
if lst[i] % 2 == 0: # check if number is even
even_count += 1
else: # if number is odd
odd_count += 1
if i in range(len(lst) - 1):
i += 1 # increment i
evenoddcount(lst) # calling fonction recursively
else:
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
list1 = [10, 21, 4, 45, 66, 93, 1]
evenoddcount(list1) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# using recursion
even_count = 0 # even counter
i = 0 # index, so that we can check if list[i] is even or odd
odd_count = 0 # odd counter
def evenoddcount(lst):
# defining local counters as global variable
global even_count
global odd_count
global i
if lst[i] % 2 == 0: # check if number is even
even_count += 1
else: # if number is odd
odd_count += 1
if i in range(len(lst) - 1):
i += 1 # increment i
evenoddcount(lst) # calling fonction recursively
else:
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
list1 = [10, 21, 4, 45, 66, 93, 1]
evenoddcount(list1)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# using Bitwise XOR
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if num ^ 1 == num + 1:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# using Bitwise XOR
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if num ^ 1 == num + 1:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# using Bitwise AND
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if not num & 1:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# using Bitwise AND
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if not num & 1:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# using Bitwise OR
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if num | 1 > num:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# using Bitwise OR
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
for num in list1:
# checking condition
if num | 1 > num:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | lst = [12, 14, 95, 3]
c = 0
c1 = 0
for i, a in enumerate(lst):
if a % 2 == 0:
c += 1
else:
c1 += 1
print("even number count", c, "odd number count", c1) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
lst = [12, 14, 95, 3]
c = 0
c1 = 0
for i, a in enumerate(lst):
if a % 2 == 0:
c += 1
else:
c1 += 1
print("even number count", c, "odd number count", c1)
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to Print Positive Numbers in a List
import numpy as np
# list of numbers
List = [10, 21, 4, 45, 66, 93, 11]
# using numpy Array
list1 = np.array(List)
Even_list = list1[list1 % 2 == 0]
print("Even numbers in the list: ", len(Even_list))
print("Odd numbers in the list: ", len(list1) - len(Even_list)) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to Print Positive Numbers in a List
import numpy as np
# list of numbers
List = [10, 21, 4, 45, 66, 93, 11]
# using numpy Array
list1 = np.array(List)
Even_list = list1[list1 % 2 == 0]
print("Even numbers in the list: ", len(Even_list))
print("Odd numbers in the list: ", len(list1) - len(Even_list))
#Output : Even numbers in the list: 3
[END] |
Python program to count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | import numpy as np
# list of numbers
list1 = [2, 7, 5, 64, 14]
# create numpy array from list
arr = np.array(list1)
# count even numbers
even_count = len(np.where(arr % 2 == 0)[0])
# count odd numbers
odd_count = len(np.where(arr % 2 == 1)[0])
# print counts
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
import numpy as np
# list of numbers
list1 = [2, 7, 5, 64, 14]
# create numpy array from list
arr = np.array(list1)
# count even numbers
even_count = len(np.where(arr % 2 == 0)[0])
# count odd numbers
odd_count = len(np.where(arr % 2 == 1)[0])
# print counts
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |