Separate even and odd numbers

Python Practice › List Programs › Program 6

Separate even and odd numbers

EasyList Programs · 6 of 100

Write a Python program to separate the even and odd numbers of a list.

Input
[5, 8, 3, 12, 7, 10]
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Approach

  1. Two comprehensions with opposite conditions.

Solution

Python
numbers = [5, 8, 3, 12, 7, 10]evens = [n for n in numbers if n % 2 == 0]odds = [n for n in numbers if n % 2 != 0]print("Even:", evens)print("Odd:", odds)
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Minimum and maximum

Python Practice › List Programs › Program 5

Minimum and maximum

EasyList Programs · 5 of 100

Write a Python program to find the smallest and largest elements of a list.

Input
[45, 12, 89, 3, 67]
Output
Min: 3 Max: 89
3 89

Approach

  1. Track both in one loop, or use min() and max().

Solution

Python
numbers = [45, 12, 89, 3, 67]smallest = largest = numbers[0]for n in numbers:    if n < smallest:        smallest = n    if n > largest:        largest = nprint("Min:", smallest, "Max:", largest)print(min(numbers), max(numbers))
Output
Min: 3 Max: 89
3 89

Line by line

Line 2

Both start at the first item, which works for negative numbers too.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Product of all elements

Python Practice › List Programs › Program 4

Product of all elements

EasyList Programs · 4 of 100

Write a Python program to find the product of all elements in a list.

Input
[2, 3, 4, 5]
Output
120 120

Approach

  1. Start at 1 and multiply, or use math.prod().

Solution

Python
import math numbers = [2, 3, 4, 5]product = 1for n in numbers:    product *= nprint(product, math.prod(numbers))
Output
120 120

Line by line

Line 4

Starting at 1 (not 0) keeps the product correct.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Sum of all elements

Python Practice › List Programs › Program 3

Sum of all elements

EasyList Programs · 3 of 100

Write a Python program to calculate the sum of all elements in a list.

Input
[12, 7, 33, 5, 21]
Output
78 78

Approach

  1. Add each item to a total, or use sum().

Solution

Python
numbers = [12, 7, 33, 5, 21]total = 0for n in numbers:    total += nprint(total, sum(numbers))
Output
78 78

Line by line

Lines 3-4

The loop adds each number to the running total.

Line 5

The built-in sum() gives the same answer.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Combine two lists

Python Practice › List Programs › Program 2

Combine two lists

EasyList Programs · 2 of 100

Write a Python program to combine two lists.

Input
[1, 2, 3], [4, 5, 6]
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Approach

  1. + creates a new list; extend() adds to an existing one.

Solution

Python
list1 = [1, 2, 3]list2 = [4, 5, 6]print(list1 + list2)list1.extend(list2)print(list1)
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Line by line

Line 3

+ returns a new list and leaves both originals unchanged.

Line 4

extend changes list1 in place.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Square of each number in a list

Python Practice › List Programs › Program 1

Square of each number in a list

EasyList Programs · 1 of 100

Write a Python program to calculate the square of each number in a list.

Input
[2, 4, 6, 8, 10]
Output
[4, 16, 36, 64, 100]

Approach

  1. Loop over the list and append each number squared, or use a list comprehension.

Solution 1: loop

Python
numbers = [2, 4, 6, 8, 10]squares = []for n in numbers:    squares.append(n ** 2)print(squares)
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

Start with an empty list for the results.

Lines 3-4

n ** 2 squares each number and append adds it to the end.

Solution 2: list comprehension

Python
numbers = [2, 4, 6, 8, 10]print([n ** 2 for n in numbers])
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

The comprehension does the same loop in one expression.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Python NumPy Programs with Solutions

Python Practice › NumPy Programs

Python NumPy Programs with Solutions

100 programs100 easy0 medium0 hard

100 NumPy programs: creating arrays and matrices, element-wise tests and comparisons, indexing, reshaping, sorting, set operations, NaN handling, statistics and rounding. The outputs come from NumPy 2.5; the in-browser Try it uses an older NumPy, so a few printouts may look slightly different.

Before you start

Read the NumPy lesson first if the topic is new to you.

All programs

Python Pandas Programs with Solutions

Python Practice › Pandas Programs

Python Pandas Programs with Solutions

95 programs95 easy0 medium0 hard

95 pandas programs: Series arithmetic and statistics, building and filtering DataFrames, sorting, adding and renaming columns, string methods, joining tables and handling missing values. Outputs come from pandas 3; the in-browser Try it uses an older pandas, so some printouts (such as column types) may differ slightly.

Before you start

Read the Pandas lesson first if the topic is new to you.

All programs

Python Tuple Programs with Solutions

Python Practice › Tuple Programs

Python Tuple Programs with Solutions

70 programs65 easy5 medium0 hard

70 tuple programs: creating, indexing, unpacking, sorting lists of tuples, converting between tuples, lists, strings and dicts, and filtering tuples by their contents. Each solution is explained and shows its real output.

Before you start

Read the Tuples and List vs Tuple lesson first if the topic is new to you.

All programs

Python File Handling Programs with Solutions

Python Practice › File Handling Programs

Python File Handling Programs with Solutions

38 programs38 easy0 medium0 hard

38 file handling programs: reading, writing and appending, working line by line, counting words and characters, copying with changes, and moving the cursor. Every solution first creates its own sample file, so you can run it anywhere, including with the Try it button.

Before you start

Read the File Handling and Read/Write JSON and Read/Write CSV lesson first if the topic is new to you.

All programs