- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34486 Articles for Programming
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
2K+ Views
To write an SQL in query, you need to ensure that you provide the placeholders in the query using so that the query is properly escaped. For example,Examplemy_tuple = ("Hello", "world", "John") placeholder= '?' placeholders= ', '.join(placeholder for _ in my_tuple) query= 'SELECT name FROM students WHERE id IN (%s)' % placeholders print(query)# now execute using the cursorcursor.execute(query, my_tuple)OutputThis will give the output'SELECT name FROM students WHERE id IN (?, ?, ?)'And when you call to execute, it'll replace them? placeholders correctly by the escaped values.
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
9K+ Views
In this article, we will show you how to append a tuple into another in python. Below are the various methods to accomplish this task − Using + operator. Using sum() function. Using list() & extend() functions. Using the unpacking(*) operator. Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using + operator Algorithm (Steps) Following are the Algorithm/steps to be followed to ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
243 Views
The direct way to subtract tuple of tuples from a tuple in Python is to use loops directly. For example, ifyou have a tuple of tuplesExample((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14))and want to subtract (1, 2, 3, 4, 5) from each of the inner tuples, you can do it as followsmy_tuple = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)) sub = (1, 2, 3, 4, 5) tuple(tuple(x - sub[i] for x in my_tuple[i]) for i in range(len(my_tuple)))OutputThis will give the output((-1, 0, 1), (1, 2, 3), (3, 4, 5), (5, 6, 7), (7, 8, 9))
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
907 Views
A multiple tuple is a tuple of tuples. example((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14))You can iterate over a multiple tuple using the python destructuring syntax in the following wayx = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)) for a, b, c in x: print(a + b + c)OutputThis will give the output3 12 21 30 39This structure is useful when you want to return a structure that has defined order and you want it to be immutable.
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
97 Views
If you want to check if an object, x is an instance of exactly a given type(not a subtype), you can use typeto get its type and check using is statement.examplex = "Hello" if type(x) is str: print("x is an instance of str")OutputThis will give the outputx is an instance of strIf you want to check if x is an instance of a MyClass or any subclass of MyClass, you can use the isinstance method call. examplex = "Hello" if isinstance(x, str): print("x is an instance of str")OutputThis will give the outputx is an instance of strRead More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
639 Views
You can directly enter duplicate items in a Python tuple as it doesn't behave like a set(which takes only unique items). examplemyTpl = (1, 2, 2, 2, 3, 5, 5, 4)You can also use operators on tuples to compose large tuples.For ExamplemyTpl = (1,) * 5 print(myTpl)OutputThis will give the output(1,1,1,1,1)You can also join tuples using + operator. examplemyTpl = (1,) * 3 + (2,) * 2 print(myTpl)OutputThis will give the output(1,1,1,2,2)
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
If you have a numeric library like numpy available, you should use the reshape method to reshape the tuple to a multidimensional array. exampleimport numpy data = numpy.array(range(1,10)) data.reshape([3,3]) print(data)OutputThis will give the output −array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])ExampleIf you prefer to do it in pure python, you can use a list comprehension −data = tuple(range(1, 10)) table = tuple(data[n:n+3] for n in xrange(0,len(data),3)) print(table)OutputThis will give the output −((1, 2, 3), (4, 5, 6), (7, 8, 9))
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
21K+ Views
In this article, we will show you how to represent a tuple in python in JSON format. We will see the below-mentioned methods in this article: Converting Python Tuple to JSON Converting Python Tuple with Different Datatypes to JSON String Parsing JSON string and accessing elements using json.loads() method. Convert the dictionary of tuples to JSON using json.dumps() What is JSON? JSON (JavaScript Object Notation) is a simple lightweight data-interchange format that humans can read and write. Computers can also easily parse and generate it. JSON is a computer language that is based on JavaScript. It is a ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
Tuples in python are immutable. If you want to remove items out of a Python tuple, you can use index slicing to leave out a particular index. For example,a = (1, 2, 3, 4, 5) b = a[:2] + a[3:] print(b)This will give the output:(1, 2, 4, 5)Or you can convert it to a list, remove the item and convert back to a tuple. For example,a = (1, 2, 3, 4, 5) ls_a = list(a) del ls_a[2] b = tuple(ls_a) print(b)This will give the output:(1, 2, 4, 5)
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
146 Views
You can create a tuple of unicode strings in python using the u'' syntax when defining this tuple. examplea = [(u'亀',), (u'犬',)] print(a)OutputThis will give the output[('亀',), ('犬',)]Note that you have to provide the u if you want to say that this is a unicode string. Else it will be treated as a normal binary string. And you'll get an unexpected output. examplea = [('亀',), ('犬',)] print(a)OutputThis will give the output[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]