Python Forum
difference between today, and today variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difference between today, and today variables
#1
Hi All,

I want to know what is the difference between variables " today," and "today" in the below example. First option is giving output in date format, while second one is not . Just by adding "," after the variable name, its printing correct format. both are holding tuple data.

>>> cursor.execute("select sysdate from dual")
<cx_Oracle.Cursor on <cx_Oracle.Connection to hr@localhost:1521>>
>>> today, = cursor.fetchone()
>>> print("The current date is", today)
The current date is 2018-11-25 03:36:57


>>> cursor.execute("select sysdate from dual")

<cx_Oracle.Cursor on <cx_Oracle.Connection to hr@localhost:1521>>
>>> today= cursor.fetchone()
>>> print("The current date is", today)
The current date is (datetime.datetime(2018, 11, 25, 3, 37, 52),)

>>> type (today)
<class 'tuple'>
>>> type (today,)
<class 'tuple'>
Reply
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

cursor.fetchone is returning a tuple of length 1. By using today = (no comma), you just get the tuple. But using today, = (with a comma) is using tuple assignment. As in:

>>> nums = (1, 2)
>>> one, two = nums
>>> one
1
>>> two
2
And they are not the same type. You are just checking the type of the second today twice. If you had check the type of the first today before reassigning it, you would see it was equal to datetime. Here's an example:

>>> one = (1,)
>>> one
(1,)
>>> uno, = (1,)
>>> uno
1
>>> type(one)
<class 'tuple'>
>>> type(uno)
<class 'int'>
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi, Thanks for the explanation i got it now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 555 Oct-03-2023, 11:15 AM
Last Post: snippsat
  Need to identify only files created today. tester_V 5 4,656 Feb-18-2021, 06:32 AM
Last Post: tester_V
  Make list of dates between today back to n days Mekala 3 2,398 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  Store Previous date to calculate delta from today Captain_Wolf 7 3,315 May-08-2020, 06:08 PM
Last Post: SheeppOSU
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,136 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020