w3resource

Python Exercise: Create a tuple with numbers, and display a member


3. Create a Tuple of Numbers and Print One Item

Write a Python program to create a tuple of numbers and print one item.

Visual Presentation:

Python Tuple: Create a tuple with numbers, and display a member.

Sample Solution:

Python Code:

# Create a tuple containing a sequence of numbers
tuplex = 5, 10, 15, 20, 25
# Print the contents of the 'tuplex' tuple
print(tuplex)

# Create a tuple with a single item (note the comma after the item)
tuplex = 5,
# Print the contents of the 'tuplex' tuple
print(tuplex)

Sample Output:

(5, 10, 15, 20, 25)                                                                                           
(5,) 

Flowchart:

Flowchart: Create a tuple with numbers, and display a member

For more Practice: Solve these Related Problems:

  • Write a Python program to create a tuple of integers from 1 to 10 and print the fifth element.
  • Write a Python program to build a tuple of random numbers and then print the first and last items.
  • Write a Python program to generate a tuple using range() and print the item at an index specified by the user.
  • Write a Python program to create a tuple of even numbers and print a specific item determined by its modulo index.

Python Code Editor:

Previous: Write a Python program to create a tuple with different data types.
Next: Write a Python program to unpack a tuple in several variables.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.