Sunday, April 20, 2025

Python Tuples

Think about tuples as collection of elements that you can't change. They are "fixed in time".

To create them use parentheses:


tuple_names = ("Samantha", "Madonna", "Michael")

print(tuple_names)
print(type(tuple_names))

Result: 


('Samantha', 'Madonna', 'Michael')
<class 'tuple'>
>>> 

Positions work here too:


tuple_names = ("Samantha", "Madonna", "Michael")

print(tuple_names[0])
print(tuple_names[1])
print(tuple_names[2])

Result: 


Samantha
Madonna
Michael
>>> 

And we can do very simple checks with if statement:


tuple_names = ("Samantha", "Madonna", "Michael")

if "Samantha" in tuple_names:
    print("Samantha is there")

Result: 


Samantha is there
>>> 

Ok, in next tutorial we will talk about tuple operations.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...