Copy a List
RYB_color = ["Red","Yellow",Blue"] Example 1 Copy_RYB = RYB_color.copy() Example 2 Copy_RYB = list(RYB_color)
|
Join Two Lists
RYB_color = ["Red","Yellow",Blue"] Second_color = ["Green","Orange","Purple"] Example 1 RYB_Second = RYB_color.extend(Second_color) Example 2 RYB_Second = RYB_color + Second_color
|
The list() Constructor
RYB_color = list(("Red","Yellow",Blue"))
|
Tuple
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets. |
Tuple Example
RYB_color = ("Red","Yellow","Blue") print(RYB_color) >>>('Red','Yellow','Blue')
|
Access Tuple Items
Same as access list items. Please check "Access list items" in "Python Lists - Part I" cheat sheet. |
Change Tuple Values
Since tuples are inchangeable, then we cannot change any value. But, we can convert a tuple to a list, change the values that we want within the list and convert the list back into a tuple. Example `RYB_color = ("Red","Yellow","Blue") L = list(RYB_color) L[0] = "Magenta" RYB_color = tuple(X)
|
|
|
Check if Item Exists
Same as checking item witin a list . Please check "Check if Item Exists" in "Python Lists - Part I" cheat sheet. |
Tuple Length
Same as list length. Please check "List Length" in "Python Lists - Part I" cheat sheet. |
Add Items
Once a tuple is created, we cannot add items to it. Tuples are unchangeable. |
Create Tuple With One Item
To create a tuple with only one item, we have add a comma after the item, unless Python will not recognize the variable as a tuple. |
|
Remove Items
Tuples are unchangeable, so we cannot remove items from it, but we can delete the tuple completely. |
RYB_color = ("Red","Yellow","Blue") del RYB_color
|
Join Two Tuples
RYB_color = ("Red","Yellow","Blue") Second_color = ("Orange","Green","Purple") RYB_Second = RYB_color + Second_color
|
The tuple() Constructor
RYB_color = tuple(("Red","Yellow","Blue")) |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Nouha_Thabet