Slice a tuple

Python Practice › Tuple Programs › Program 20

Slice a tuple

EasyTuple Programs · 20 of 70

Write a Python program to slice a tuple.

Input
(5, 7, 3, 4, 9, 0, 2)
Output
(5, 7, 3)
(3, 4, 9)
(2, 0, 9, 4, 3, 7, 5)

Approach

  1. t[start:stop] works like list slicing.

Solution

Python
t = (5, 7, 3, 4, 9, 0, 2)print(t[:3])print(t[2:5])print(t[::-1])
Output
(5, 7, 3)
(3, 4, 9)
(2, 0, 9, 4, 3, 7, 5)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove an item from a tuple

Python Practice › Tuple Programs › Program 19

Remove an item from a tuple

EasyTuple Programs · 19 of 70

Write a Python program to remove an item from a tuple.

Input
("p", "y", "t", "h", "o", "n"), remove "h"
Output
('p', 'y', 't', 'o', 'n')

Approach

  1. Build a new tuple without the item (tuples cannot be changed).

Solution

Python
t = ("p", "y", "t", "h", "o", "n")i = t.index("h")print(t[:i] + t[i + 1:])
Output
('p', 'y', 't', 'o', 'n')

Line by line

Line 2

Find the position, then join the slices around it. This removes only the first “h”.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

List to tuple, doubled

Python Practice › Tuple Programs › Program 18

List to tuple, doubled

EasyTuple Programs · 18 of 70

Write a Python program to convert a list into a tuple with each element multiplied by 2.

Input
[12, 65, 34, 77]
Output
(24, 130, 68, 154)

Approach

  1. tuple(x * 2 for x in items).

Solution

Python
print(tuple(x * 2 for x in [12, 65, 34, 77]))
Output
(24, 130, 68, 154)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Multiply adjacent elements

Python Practice › Tuple Programs › Program 16

Multiply adjacent elements

EasyTuple Programs · 16 of 70

Write a Python program to multiply each pair of neighbouring elements of a tuple.

Input
(1, 2, 3, 4)
Output
(2, 6, 12)

Approach

  1. zip(t, t[1:]) gives the neighbours.

Solution

Python
t = (1, 2, 3, 4)print(tuple(a * b for a, b in zip(t, t[1:])))
Output
(2, 6, 12)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Python Projects For Beginners

This article has common Python Projects for beginners, which have all categories of projects from basic to advanced.

Basic Level Projects:

  • Basic Calculator
  • Show Calendar
  • Convert pdf to text
  • Convert image to pdf
  • Convert dictionary to JSON
  • Convert JPEG to PNG
  • Convert JSON to CSV
  • Convert XML to JSON
  • Convert book into audiobook 
  • Decimal to binary convertor and vice versa
  • DNS Records
  • Encrypt and decrypt text
  • Extract zip files
  • Fetch HTTP status code
  • Fetch open ports
  • Hashing passwords
  • Merge csv
  • Merge pdf
  • QR code generator
  • Random password generator

Intermediate Level Projects:

  • Capture screenshot
  • Calculate age
  • Chrome Automation
  • Convert numbers into words
  • Script to encrypt files and folders
  • Article Downloader
  • Duplicate file remover
  • Email GUI
  • Email verification
  • Facebook dp downloader
  • Get wifi password
  • Github repo automation
  • Image to speech
  • Language Translator
  • Password manager
  • Plagiarism checker
  • Python image compressor
  • Send an email with a python
  • Send mail from CSV
  • Set alarm
  • URL shortener

Advance Level Projects:

  • Auto birthday wisher.
  • Auto Backup.
  • Auto-fill the google form.
  • Automate Facebook bot.
  • Automatic certificate generator.
  • Automatic Facebook login
  • Calculator GUI
  • Calendar GUI
  • CLI todo
  • Simple Stopwatch
  • Dictionary GUI
  • Easy cartoonify the image
  • Video Player
  • Extract text from PDF
  • Gesture control media player
  • Google meeting bot
  • Image to sketch
  • Noise reduction script
  • Password manager GUI
  • PDF reader with voice
  • Quote scraper
  • Screen recorder
  • Speech to text
  • Text to speech
  • Text editor
  • Voice Translator
  • Video to audio converter

Tuple of squares from a list

Python Practice › Tuple Programs › Program 15

Tuple of squares from a list

EasyTuple Programs · 15 of 70

Write a Python program to create a tuple of the squares of a list’s elements.

Input

[1, 9, 5, 7, 6]
Output

(1, 81, 25, 49, 36)

Approach

  1. Pass a generator expression to tuple().

Solution

Python
print(tuple(n * n for n in [1, 9, 5, 7, 6]))

Output
(1, 81, 25, 49, 36)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sum of a tuple

Python Practice › Tuple Programs › Program 13

Sum of a tuple

EasyTuple Programs · 13 of 70

Write a Python program to find the sum of the elements of a tuple.

Input
(4, 6, 2)
Output
12

Approach

  1. sum().

Solution

Python
print(sum((4, 6, 2)))
Output
12

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Add a list’s items to a tuple

Python Practice › Tuple Programs › Program 12

Add a list's items to a tuple

EasyTuple Programs · 12 of 70

Write a Python program to add the items of a list to a tuple.

Input
list [12, 67], tuple (6, 8, 4)
Output
(6, 8, 4, 12, 67)

Approach

  1. Convert the list to a tuple and concatenate.

Solution

Python
items = [12, 67]t = (6, 8, 4)t = t + tuple(items)print(t)
Output
(6, 8, 4, 12, 67)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Is an element in a tuple?

Python Practice › Tuple Programs › Program 11

Is an element in a tuple?

EasyTuple Programs · 11 of 70

Write a Python program to check whether an element exists in a tuple.

Input
("p", "y", "t", "h", "o", "n"), check "p" and "z"
Output
True False

Approach

  1. in.

Solution

Python
t = ("p", "y", "t", "h", "o", "n")print("p" in t, "z" in t)
Output
True False

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

2nd from the front, 3rd from the back

Python Practice › Tuple Programs › Program 10

2nd from the front, 3rd from the back

EasyTuple Programs · 10 of 70

Write a Python program to get the 2nd element from the front and the 3rd element from the back of a tuple.

Input
("s", "q", "a", "t", "o", "o", "l", "s")
Output
q
o

Approach

  1. Index 1 from the front, index -3 from the back.

Solution

Python
t = ("s", "q", "a", "t", "o", "o", "l", "s")print(t[1])print(t[-3])
Output
q
o

Learn the concept

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

More tuple programs

← All 70 Tuple Programs