- 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 10784 Articles for Python
208 Views
dicts in python are heavily optimized. Creating a dict from N keys or key/value pairs is O(N), fetching is O(1), putting is amortized O(1), and so forth. You don't need to optimize them explicitly. You can be sure of this as python under the hood implements its own classes using dicts.Don't compare lists/tuples to dicts/sets though as they solve different problems.
659 Views
When using the old style of string formatting in python, ie, "" % (), if the thing after the percent is a tuple, python tries to break it down and pass individual items in it to the string. For example, tup = (1, 2, 3) print("this is a tuple %s" % (tup))This will give the output:TypeError: not all arguments converted during string formattingThis is because of the reason mentioned above. If you want to pass a tuple, you need to create a wrapping tuple using the (tup, ) syntax. For example, tup = (1, 2, 3) print("this is a tuple ... Read More
567 Views
The str function converts an object in python to a string representation. There is another function called repr() in python that converts object to an expression string. __repr__'s goal is to be unambigous while __str__'s is to be readable. __repr__ is used to compute the “official” string representation of an object.ExampleLet's take an example of datetime to understand what these 2 produce.import datetime today = datetime.datetime.now() str(today) repr(today)OutputThis will give the output'2018-04-08 11:25:36.918979' 'datetime.datetime(2018, 4, 8, 11, 25, 36, 918979)'As you can see from the output, str gives a pretty, formatted result. Repr just throws an object constructor representation at ... Read More
3K+ Views
To convert a tuple to an array(list) you can directly use the list constructor. examplex = (1, 2, 3) y = list(x) print(y)OutputThis will give the output −[1, 2, 3]ExampleIf you have a multi-level tuple and want a flat array, you can use the following −z = ((1, 2, 3), (4, 5)) y = [a for b in z for a in b] print(y)OutputThis will give the output −[1, 2, 3, 4, 5]
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.
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
242 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))
903 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.
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
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)