- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Tuples / of Python Tuple Operators
fruits = ("orange", "mangoes", "banana", "apples") #strings print(fruits) ranks = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #Numbers print(ranks) prices = (101.1, 233.44, 245.8, 290,9) #floats print(prices) students = ({"name":"ram", "marks":100}, {"name":"krish", "marks":90}) #dictionaries print(students) data = ((1,2), (3,4), (5,6)) #Tuples print(data) result = ("orange", 1, 10.2, {"name":"ram", "marks":100}, (1,2)) print(result) # Indexing, Slicing, and Matrixes # How to access tuples? student = ("krish",100,["maths", "physics", "social", "science"]) print(student[0]) print(student[1:]) # print(student[:2]) # print(student[:]) print(student[-1]) #Can we update the tuples student = ("krish",100,["maths", "physics", "social", "science"]) student[0] = "ram" # Delete Tuple Elements student = ("krish",100,["maths", "physics", "social", "science"]) del student[0] del student print(student)
# What is the use of + in tuple? student1 = ("krish", 100, 202.3) student2 = ("ram", 200, [1, 2, 3, 4]) result = student1 + student2 print(result) # What is the use of * in tuples? student = ("krish", 100, 202.3) result = student * 2 print(result) # How to check element in tuple? student = ("krish", 100, 202.3) if "krish" in student: print("Search element found in tuple") else: print("search element not found") # How to display tuples data? values = ("krish", 100, 202.3) for value in values: print(value)
# How to count no.of occurrences of element in a tuple? values = (0,1,2,3,1,3,4,2,4,6,1,2,5,3,4) result = values.count(4) print(result) values = (0,1,(2,3),1,3,4,2,(2,3),4,6,1,2,5,(2,3),3,4) result = values.count((2,3)) print(result) # How to find index of an element in tuple values = ("krish", 100, 202.3) result = values.index(100) print(result) # tuple(seq):Create tuples? result = tuple() print('t1=', result) # How to create a tuple from a list? result = tuple([1, 4, 6]) print('result=', result) # How to create a tuple from a string? result = tuple('Python') print('result=',result) # How to create a tuple from a dictionary? result = tuple({1: 'one', 2: 'two'}) print('result=',result)
import MySQLdb from beautifultable import BeautifulTable table = BeautifulTable() table.column_headers = ["Movie Name", "Movie Language"] table.column_alignments['Movie Name'] = BeautifulTable.ALIGN_LEFT try: connection = MySQLdb.connect(user='root',password='sairam143sairam',port=3306,host='localhost') except Exception as e: print(e) else: tutorialCursor = connection.cursor() tutorialCursor.execute("SELECT movie_name,movie_language FROM bulkratings_info.tutorialstution_movies_list;") Movies = tutorialCursor.fetchall() connection.close() print(list(Movies)) for Movie in Movies: try: print(Movie) except: pass