0

Hi I'm pretty new to coding world and I had a question about python versions. I'm watching online lectures and Youtube videos and learned that python 3 is the newest version. But codes that I am learning is what leetcode problems display as "python", not "python 3". For function headers, python 3 on leetcode shows

def twoSum(self, nums: List[int], target: int) -> List[int]:

instead of what I am introduced to,

def two_sum(list, int):

Can someone please help me with the confusion

  • Does this answer your question? [Use of colon in variable declaration](https://stackoverflow.com/questions/51639332/use-of-colon-in-variable-declaration) – luk2302 Jul 03 '22 at 05:54

2 Answers2

0

The first version in the Python3 lineage was released in 2008, so it is not entirely new any longer. Python3 was not compatible with the previous version of Python (i.e. Python2) and hence it became important to identify clearly whether you were considering the old version 2.x of Python or the new version 3.x. Nowadays Python2 does not receive any support and has been firmly discontinued, i.e. you can safely assume that when people talk about "Python" and "Python3" - they mean the same thing.

The notation you point to is an optional possibility to add type declarations to Python declarations.

user422005
  • 1,989
  • 16
  • 34
0

don't get confused between the python version. Both functions are based on python 3. just the difference between both of them is that

def twoSum(self, nums: List[int], target: int) -> List[int]:

in the first function, you start declaring the output type as in your case output will be list[int]. in easy words, u are restricting your output type and at the start, you declare what exact thing your function wants.

def two_sum(list, int):

in this case, you are not declaring any restrictions on your function.

wahid
  • 1
  • 2
  • Type annotations are just that: annotations so users and tools better understand the intent of the code. They are not enforced, so the first version doesn't actually "restrict" anything. – ChrisGPT was on strike Jul 03 '22 at 17:25