- 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
117 Views
A string contains two integers separated by comma. It is first split in a list of two strings having digits.>>> s="1,2".split(",") >>> s ['1', '2']Two items are then converted to integers and used as arguments for complex() function>>> complex(int(s[0]), int(s[1])) (1+2j)This results in unpacking of string of integers in a complex number
7K+ Views
Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=’ ‘ to print all items in a tuple in one line. Another print() introduces new line after each tuple.ExampleL=[(1,2,3), (4,5,6), (7,8,9,10)] for x in L: for y in x: print(y, end=' ') print()Output1 2 3 4 5 6 7 8 9 10
5K+ Views
There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −for var in tuple: stmt1 stmt2ExampleFollowing script will print all items in the listT = (10, 20, 30, 40, 50) for var in T: print (T.index(var), var)OutputThe output generated is −0 10 1 20 2 30 3 40 4 50Another approach is to iterate over range upto length of tuple, and use it as index of item in tupleExamplefor var in range(len(T)): ... Read More
2K+ Views
List is an ordered sequence of elements. Individual element in list is accessed using index starting with 0 and goes up to length-1. If index goes beyond this range, IndexError exception is encountered.In following example, an infinite loop is used to pop one element at a time. As loop tries to go even after last element is popped, IndexError exception will be encountered. We trap it using try – except mechanism.a=[1,2,3] while True: try: b=a.pop() print (b) except (IndexError): break
7K+ Views
In this article, we will explain nested/inner functions in Python and how they work with examples. Nested (or inner) functions are functions defined within other functions that allow us to directly access the variables and names defined in the enclosing function. Nested functions can be used to create closures and decorators, among other things. Defining an inner/nested Function Simply use the def keyword to initialize another function within a function to define a nested function. The following program is to demonstrate the inner function in Python − Example # creating an outer function def outerFunc(sample_text): sample_text ... Read More
1K+ Views
The function datetime.now() takes tzinfo as keyword argument but datetime.today() does not take any keyword arguments. Quoting the docs −datetime.now() returns the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).
44K+ Views
In this article, we will show you how to convert date and time with different timezones in Python. Using astimezone() function Using datetime.now() function The easiest way in Python date and time to handle timezones is to use the pytz module. This library allows accurate and cross−platform timezone calculations. pytz brings the Olson tz database into Python. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (datetime.tzinfo) Before you use it you'll need to install it using − pip install pytz ... Read More
5K+ Views
In this article, we will show you how to compare time to different timezones in Python using the below methods. Comparing the given Timezone with the local TimeZone Comparing the Current Datetime of Two Timezones Comparing Two Times with different Timezone Method 1: Comparing the given Timezone with the local TimeZone Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task – Use the import keyword, to import the datetime, pytz modules. Use the timezone() function (gets the time zone of a specific location) of the pytz module, to get the timezone ... Read More