Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with Tuples in python #24

Merged
merged 1 commit into from
Oct 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions python basics/tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""This is example for describing work with tuple.

Author: mhrownaghi
Github Page: https://github.com/mhrownaghi
"""

# create a tuple
fruits = ("apple", "banana", "orange", "cherry")

# access to tuple's elements
print(fruits[0]) # print -> apple
print(fruits[1]) # print -> banana
print(fruits[2]) # print -> orange
print(fruits[3]) # print -> cherry
print(fruits[-1]) # print -> cherry
print(fruits[-2]) # print -> orange
print(fruits[2:4]) # print -> orange cherry
print(fruits[:3]) # print -> apple banana orange
print(fruits[2:]) # print -> orange cherry

# Tuples are ReadOnly
# fruits[0] = "tomato" # this will raise an error

grades1 = (10, 20, 15, 14)
grades2 = (5, 10, 7, 6)
print(grades2 < grades1) # print -> True
print(grades1 > grades2) # print -> True
print(grades1 == grades2) # print -> False
print(grades1 != grades2) # print -> True
# (grades1 <= grades2) == (grades1 == grades2 or grades1 < grades2)
print(grades1 <= grades2) # print -> False
print(grades1 < grades2 or grades1 == grades2) # print -> False
# (grades1 >= grades2) == (grades1 == grades2 or grades1 > grades2)
print(grades1 >= grades2) # print -> True
print(grades1 > grades2 or grades1 == grades2) # print -> True

(math, english, physics, sport) = grades1 # unpacking
print(math) # print -> 10
print(english) # print -> 20
print(physics) # print -> 15
print(sport) # print -> 14

numbers = (1, 2, 6, 2, 3, 4, 1, 4, 7, 2, 4, 2)
# Print the number of times the value 2 appears in the tuple:
print(numbers.count(2)) # print -> 4
# Search for the first occurrence of the value 7, and return its position:
print(numbers.index(7)) # print -> 8