Write a Python program to get the smallest odd number from a list

Python program to find smallest number in a list

Given a list of numbers, the task is to write a Python program to find the smallest number in given list.
Examples:

Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output : 1

Method 1 : Sort the list in ascending order and print the first element in the list.

Python3




# 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[:1])

Output:

smallest element is: 4

Method 2 : Using min() method

Python3




# 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))

Output:



Smallest element is: 1

Method 3 : Find min list element on inputs provided by user.

Python3




# 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))

Output:

Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 11 Enter elements: 99 Smallest element is: 11

Method 4: Find the smallest element in list.

Python3




# 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:

List: 23,-1,45,22.6,78,100,-5

Output:

The list is ['23', '-1', '45', '22.6', '78', '100','-5'] The smallest element in the list is -5




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Python program to print odd numbers in a List

Given a list of numbers, write a Python program to print all odd numbers in given list.

Example:

Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] Input: list2 = [12, 14, 95, 3, 73] Output: [95, 3, 73]
  1. Using for loop : Iterate each element in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.




    # 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 = " ")

    Output:

    21 45 93
  2. Using while loop :




    # 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

    Output:

    21 45 93
  3. Using list comprehension :




    # 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)

    Output:

    21 45 93
  4. Using lambda expressions :




    # 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)

    Output:

    Odd numbers in the list: [21, 45, 93, 11]




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Smallest odd number

In this program, we need to find out the smallest odd number from the given integer number.

  • We are provided with a random number, and from that number, we need to find the smallest odd digit present in it.
  • For implementing it, we must know the basic concepts of array and loops.
  • Whether you are implementing in C, C++, Java, or any other programming language, to solve this problem, you must have a basic understanding of the loops, and a linear data structure named an array.
  • The array consists of a homogenous set of elements; for example, if a declared array with an int data type, that array must contain all integer types of values.
  • We will use an array in this problem to store each digit of the given number.

Let us understand the concept of the above problem with the help of the below-mentioned examples:

Example 1: If we have given a number ' n ', n = 12345, we need to find the smallest odd number.

n = 12345

The number of odd numbers is: 1, 3, 5

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be 1.

Example 2: If we have given a number ' n ', n = 8745329, we need to find the smallest odd number.

n = 874532

The number of odd numbers is: 7, 5, 3, 9

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be 3.

Example 3: If we have given a number ' n ', n = 750, we need to find the smallest odd number.

n = 750

The number of odd numbers is: 7, 5

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be 7.

Example 4: If we have given a number ' n ', n = 220, we need to find the smallest odd number.

n = 220

The number of odd numbers is: none

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be -1.

Here, -1 indicates that no smallest odd number is found from the given number, which means that all the numbers present are even.

Example 5: If we have given a number ' n ', n = 97998, we need to find the smallest odd number.

n = 97998

The number of odd numbers is: 9, 7

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be 7.

Example 6: If we have given a number ' n ', n = 6620, we need to find the smallest odd number.

n = 6620

The number of odd numbers is: none

But among the list of odd numbers, we require to find the smallest odd number,

Therefore the output will be -1.

Here, -1 indicates that no smallest odd number is found from the given number, which means that all the numbers present are even.

With the help of the above examples, we have clearly understood what we need to do in this problem. Let us implement the above problem to find the smallest odd number using the approach mentioned below.

Python Program to find the Smallest Number in a List Example 1

The Python min function returns the minimum value in a List.

# Python Program to find Smallest Number in a List a = [10, 50, 60, 80, 20, 15] print("The Smallest Element in this List is : ", min(a))

Python smallest list number output

The Smallest Element in this List is : 10

Python: Get the smallest number from a list

Last update on June 23 2021 14:09:45 (UTC/GMT +8 hours)

Video liên quan

Postingan terbaru

LIHAT SEMUA