Is this a bug or a feature in Python?

Поделиться
HTML-код
  • Опубликовано: 11 ноя 2024

Комментарии • 888

  • @mohamedmohamedy3085
    @mohamedmohamedy3085 2 года назад +3499

    explanation:
    adjacent string literals are implicitly concatenated.
    on lines 4 and 5, the two strings "three" and "four" are concatenated into "threefour"

    • @patloeber
      @patloeber  2 года назад +274

      exactly :)

    •  2 года назад +102

      yeah... this is not a bug... it is a string concatenation.

    • @infienite9215
      @infienite9215 2 года назад +89

      @ In other word, it is a *feature*

    • @MadRat_0
      @MadRat_0 2 года назад +16

      @@infienite9215 U seem to be a minecrafter

    • @ramakrishna4092
      @ramakrishna4092 2 года назад +16

      @@patloeber
      s="foo"
      id(s)=140542718184424
      id(s[0])= 140542719027040
      id(s[1])= 140542718832152
      id(s[2])= 140542718832152
      I did not understand how each character is getting stored in memory and and why id of ' s ' is not equal to id of s[0] (like it use to be in c) and why id of s1 and s2 are same? can do a video in this topic

  • @dhruvray1898
    @dhruvray1898 2 года назад +1666

    This guy is honestly just training us to spot errors when we have to fix our own code

    • @markow5062
      @markow5062 2 года назад +7

      where's a ","?

    • @simone1377
      @simone1377 2 года назад +7

      hes just trying to make you reproduce his video for more time possible in order to go into reccomendations

    • @christygeorge9938
      @christygeorge9938 2 года назад +1

      Missing coma

    • @sh4rpys
      @sh4rpys 2 года назад +5

      @@simone1377 You don’t need to run this to see the problem.

    • @sidimohamedkone1755
      @sidimohamedkone1755 2 года назад

      🤣🤣🤣🤣

  • @ohturry5591
    @ohturry5591 2 года назад +61

    Lines 3 and 4 are not separated.

  • @KananHasanov-bk2op
    @KananHasanov-bk2op 2 года назад +235

    yeah you forgot the comma after "three", so Python think that "three" and "four" is one element (and they both are concatenate)

    • @5omebody
      @5omebody 2 года назад +2

      important to note it only works for strings

    • @technicalmaster-mind
      @technicalmaster-mind 2 года назад +1

      But counting actually starts by 0
      So shouldn't it be 3

    • @TehXyllo
      @TehXyllo 2 года назад +5

      @@technicalmaster-mind It would be 0, 1, 2, 3 indexes but the total length would be 4

    • @prokopiidestroypizza2691
      @prokopiidestroypizza2691 2 года назад

      А теперь объясни, пожалуйста, на кириллице ))))

    • @thedude4254
      @thedude4254 2 года назад

      @@prokopiidestroypizza2691 он не поставил запятую после three, и компилятор объеденил 3 и 4.

  • @bluvesta3802
    @bluvesta3802 2 года назад +40

    line 3 and 4 are not separated they are one line therefore the answer is 4 if you put a comma after string "three" then the answer will be 5

  • @Magnymbus
    @Magnymbus 2 года назад +225

    Missed a "," so three and four concat. Lmao. I've done this so many times.

  • @vaishnavikhandare
    @vaishnavikhandare Год назад +1

    there is no comma between "three" and "four" so it is considered as a single string "threefour"

  • @upgradestudy4245
    @upgradestudy4245 Год назад +1

    In between 3 and 4 you don't separate it with comma (,) that's why it's considered as a one combined string ( "threefour")

  • @linusbrendel
    @linusbrendel 2 года назад +76

    I don't see an issue here. Between "three" and "four" there is no comma, thus concatenating them into one string. This will result in having four strings in the array.
    This is intended behaviour for very large strings, so that one can break them into multiple lines.
    Edit: I guess one could say: "It's not a bug. It's a feature. :)"

    • @bakane6030
      @bakane6030 2 года назад +7

      I don’t really like this implicit behavior though. I think that there should at least be a plus between the strings so they concat.

    • @pawelpow
      @pawelpow 2 года назад +6

      This implicit behaviour is the reason python is known for being error prone (especially the fact that it has no types). This is a serious issue

    • @ko-Daegu
      @ko-Daegu 2 года назад +1

      @@pawelpow type hinted by is a thing since 3.5
      It’s not the language is the dev

    • @MaxCE
      @MaxCE 2 года назад +1

      @@pawelpow string concatenation like this is in many languages. Including c and c++. So its not a python script

    • @Titere05
      @Titere05 2 года назад

      @@pawelpow I've been employed at a company developing a Python backend for 2 years now and this has never happened to me accidentally. So I think calling it a "serious" issue is a bit too dramatic. I'm not a Python fan though, but for other reasons

  • @trucookieq4469
    @trucookieq4469 2 года назад +64

    Explanation: “three” and “four” are treated as “three”+”four”
    Therefore there really is 4 items

    • @PartikleVT
      @PartikleVT 2 года назад +5

      Otherwise known as bad code. This video is clearly a bait to get attention

    • @se77er4
      @se77er4 2 года назад +1

      Wait, i was thinking because count : 0, 1, 2, 3, 4
      Edit: i just see that miss a comma

  • @maxmx8137083
    @maxmx8137083 2 года назад +197

    IF a Junior run that code:
    Python: "error at your brain"
    Sênior:
    Python: "Nothing wrong here"

    • @richardpaulhall
      @richardpaulhall 2 года назад +2

      Sorry, that is an example of Python being a bad language.

    • @yairkaz
      @yairkaz 2 года назад +3

      @@richardpaulhall no it's not, he didn't put a comma after 3 which concentrated the value to be one as it was never differentiated. Nothing shows it as a bad language here

    • @alexandropalacios7782
      @alexandropalacios7782 2 года назад

      @@yairkaz However, this python feature may cause some unexpected results when coding a big proyect. Imagine having this problem in thousands lines of code 😖

    • @yairkaz
      @yairkaz 2 года назад +1

      @@alexandropalacios7782 it may be annoying but I feel like in a huge project there would not be much lists like this

    • @Titere05
      @Titere05 2 года назад +1

      @@richardpaulhall It's arguable, by the same standard I could point to a hammer a person just used to bang his own finger, and say, "Sorry but that's an example of a bad tool"

  • @official.pythonista
    @official.pythonista 2 года назад

    Three n Four are concatenated into ThreeFour it’s counted as one string. Add a comma between then and it should print 5

  • @ayushyadav5164
    @ayushyadav5164 2 года назад +72

    He didn't put a comma to separate "three" and "four", thus they catenated to one and reported a list of 4 items 🚶‍♂️

    • @richardpaulhall
      @richardpaulhall 2 года назад

      And this is an example of why Python is a crappy language.

    • @ayushyadav5164
      @ayushyadav5164 2 года назад +1

      @@richardpaulhall thats why i love C. Python is confusing

    • @Ether_Void
      @Ether_Void Год назад +1

      @@ayushyadav5164 This is actually a feature copied directly from C.
      It's often used to concatenate string literals inside macros.

  • @alexander_adnan
    @alexander_adnan 2 года назад +1

    If you want to see length of list equal to 5 you need to add a comma after the string « three »
    Without a final comma, the string « Three » will be a literal that will be concatenated to the string « Four ».

  • @MaxProgramming
    @MaxProgramming 2 года назад +1

    Haha you got me there by missing that comma 🤣

  • @rudradhoundiyal8783
    @rudradhoundiyal8783 Год назад +1

    No u just got it wrong ... a comma is missing

  • @dnbndu
    @dnbndu 2 года назад +12

    Comma after "three" and "four" be like "Mujhe andar to lo"😂.

  • @carnavshorts
    @carnavshorts Год назад +1

    there is also supposed to be a comma after three

  • @taranveersingh05
    @taranveersingh05 2 года назад +6

    ", " missing after "three"
    Logical error detected

    • @derfrederikhd1931
      @derfrederikhd1931 2 года назад +1

      Yea, but why can’t python just throw an error. If u wanna concat strings it should require an plus or a function

    • @taranveersingh05
      @taranveersingh05 2 года назад +1

      @@derfrederikhd1931 yeah u are having good point.
      Look at the pinned comment, or we should try doing practical.
      It may help

    • @derfrederikhd1931
      @derfrederikhd1931 2 года назад +2

      @@taranveersingh05 I don’t use python. I prefer when u can set the type of a variable.

  • @MokshitArora.
    @MokshitArora. 2 года назад +1

    I am not a programmer but what I can see is that you missed a comma after "three" I think that's why it is showing 4 . Please correct me if I am wrong

  • @fardinahsan2069
    @fardinahsan2069 2 года назад +23

    I definitely think this should throw a compile time error. Its strange and unexpected behavior for most devs, even as someone who has been using python exclusively for the last two years, I spotted the missing comma but though its going to throw a syntax error, not implicitly concat them.
    On the same note, people should watch our for additional commas (not only missing commas) as well as commas are what syntactically create tuples not round brackets. You might mistakenly create tuples if you place a comma by mistake somewhere.

    • @zakinadhif
      @zakinadhif 2 года назад +1

      But python isn't a compiled language

    • @fardinahsan2069
      @fardinahsan2069 2 года назад

      @@zakinadhif syntax error

    • @zakinadhif
      @zakinadhif 2 года назад +1

      @@fardinahsan2069 ahh, imo syntax warning would suit better.

    • @richardpaulhall
      @richardpaulhall 2 года назад

      @@zakinadhif No, a syntax error.

  • @philippvolkmer2843
    @philippvolkmer2843 2 года назад +1

    Its counting from 0

  • @dbfuentes
    @dbfuentes 2 года назад +2

    The "three" and "four" concatenated in 1 line (miss the , in the end) so is correct.

  • @prashantwarghude3666
    @prashantwarghude3666 2 года назад +5

    ',' is Missing after "three"
    Which means "three"&"four" is a single element
    Thats why len(a) = 4
    ✌️

  • @vaxer.7749
    @vaxer.7749 2 года назад

    That is because of "string literal concatenation" in python; if you try to write codes in iterables like lists and tuples (or just regular parentheses) with the block structure like in the video and your data type is just string, if you miss comma, SLC whould happen.
    in the first look SLC maybe seem undesired but it could be usefull sometimes. for example you can split up long lines in two or more between parentheses:
    print("long line")
    print("long"
    "line" )
    the output whould be the same but it makes your code cleaner.

  • @defacube
    @defacube 2 года назад +1

    You didnt put a , before the four so it counts as 4 instead of 5

  • @Redyf
    @Redyf 2 года назад +1

    Whats the name of the color theme on vs code?

  • @CunningBard
    @CunningBard 2 года назад +75

    I'm laughing so hard, out of all the python jokes I've seen this was the funniest one I dont know why

  • @hikari1690
    @hikari1690 2 года назад

    God damn it. This is triggering PTSD flashbacks. When missing commas, fullstops, semicolons and typos escape the compiler without errors but breaks days of work...

  • @yashmali5754
    @yashmali5754 2 года назад +1

    In simple words index starts from 0

  • @biprarshichakraborty1255
    @biprarshichakraborty1255 2 года назад

    No comma seperation between 3 & 4, which results in concatenation. No sorcery here, just print the iterable and you'll figure out.

  • @-Rigged
    @-Rigged 2 года назад +1

    Missing coma in after "three"

  • @skewhartt
    @skewhartt 2 года назад +1

    You forgot a ,

  • @sebdie10
    @sebdie10 2 года назад

    No es un bug para contar toma como el primer objeto como 0 y ahí va sumando así es que 1 = 0 2= 1

  • @realbritishenglishoffice5346
    @realbritishenglishoffice5346 2 года назад +2

    Hey, can you tell me: which text editor is best for python

  • @ליאורו-ט3ו
    @ליאורו-ט3ו 2 года назад +1

    It's because you forgot the comma after the string three so it treats it and the four string as one and not strings

  • @MaximusMakesContent
    @MaximusMakesContent Год назад

    it is a feature because every start of a list will be 0, for example: var list = ["a", "b", "c"] print(list[0])

  • @quantumastrologer5599
    @quantumastrologer5599 2 года назад

    >>> a = [
    ... "one",
    ... "two",
    ... "three",
    ... "four",
    ... "five"
    ... ]
    >>> print(a)
    ['one', 'two', 'three', 'four', 'five']
    >>> len(a)
    5
    python3 --version
    Python 3.8.10
    mmmh
    oh, nvm i'm blind. Also gotta agree. The most annoying part of learning any programming language is my brain filtering out vital stuff or doing the good ol' "aww, come on. it's cool. what could go wrong?"
    .

  • @bilalbakr7817
    @bilalbakr7817 2 года назад

    This is because the length or the index starts from 0 so that is why it printed 4 instead of 5

  • @epicspree6279
    @epicspree6279 2 года назад

    No it's not a bug it is because the first list item is said to be in index 0, and the second item is said to be in index 1, and third item is index 2. The fourth is 3rd index. The fifth is index 4th

  • @MrEo89
    @MrEo89 2 года назад

    Python is c-like in that it is 0-based indexed. So “one” is 0 and “five” is 4 in memory. So when u print the length (len()) you are shown the length of the bytes(getting out of my depth here, (??)) in the variable a.

    • @raydorepp
      @raydorepp 2 года назад +1

      There are literally 4 objects in the list.
      .length doesn't start with 0.

  • @laur.an01
    @laur.an01 Год назад

    Can somebody tell me what shortcuts did he use to write this code. I'm at the very beginning, and I wanna pick up on how to do things efficiently

  • @FeLiXGamer21
    @FeLiXGamer21 2 года назад

    Creo que es porque len considera la lista desde posición 0 entocnes hay 5 elementos. Por eso se le resta uno y sale 4

  • @helpertech2316
    @helpertech2316 2 года назад

    Where is comma after string three?

  • @comedyclub333
    @comedyclub333 2 года назад

    There are only few things about python I really don't like. One of them is, that implicit behaviour is not only possible (which is acceptable), but also sometimes seems to be encouraged, although the zen of python clearly states that explicit is better than implicit.

  • @CodeShode
    @CodeShode 2 года назад

    Each time in list is separated by commas, a comma is logging and stings between comma are concatenated as one string.

  • @bakasenpaidesu
    @bakasenpaidesu 2 года назад +1

    Didn't give comma after 3

  • @DMH_Minecraft
    @DMH_Minecraft 2 года назад

    I wouldn't see that missed comma without reading the comments, but what I would definitely do next is printing all the values of the list and only then finding this mistake.

  • @hydropage2855
    @hydropage2855 2 года назад +1

    THE COMMAAAAAAAAA

  • @rajveer_2009
    @rajveer_2009 2 года назад +1

    It’s because the first word is 0 and second in 1 instead 1 it starts with 0

    • @raydorepp
      @raydorepp 2 года назад +1

      Yeah length doesn't work that way. Length says there are 4 objects, and there are indeed 4 objects.

  • @vincenthubschmann6512
    @vincenthubschmann6512 2 года назад

    It’s because there’s a missing comma after three, leading to implicit concatenation of “three” and “four” into “threefour”

  • @thegodlydemon2605
    @thegodlydemon2605 2 года назад

    Is it because when iterating the program starts at 0 and goes 0, 1, 2, 3, 4. Therefore its still counting 5 strings.

  • @nafyadmerga9270
    @nafyadmerga9270 2 года назад

    it's true because of in index 2 or three and four can count together

  • @exitedtolearn8458
    @exitedtolearn8458 2 года назад

    He also gave an explanation in the description.
    Really a Good and helpful short video in my opinion.thnak you

  • @callmelord
    @callmelord Год назад

    Since when the list index starts from 1

  • @patloeber
    @patloeber  2 года назад +4

    Did you spot the bug? And do you also know what's happening with the strings? I will pin a good explanation :)

    • @mefodymo7456
      @mefodymo7456 2 года назад +4

      Manipulation ,not a bug:)

    • @nirabindudas9013
      @nirabindudas9013 2 года назад

      use a come (,) after three

    • @Elijah_Lopez
      @Elijah_Lopez 2 года назад

      This is like the university researchers purposely inserting a bug into the Linux kernal.

  • @nocturn9x
    @nocturn9x 2 года назад +1

    I considered adding this feature to my parser and promptly decided not to for this exact reason

  • @b07x
    @b07x 2 года назад +1

    How did you create a list that fast?

  • @ohmygod3840
    @ohmygod3840 2 года назад +3

    I thought index value starters from 0

    • @dimitar.bogdanov
      @dimitar.bogdanov 2 года назад

      Index - yes, but no language will count starting from 0. The list is just 4 elements (almost every other comment explains why) long.

    • @bodhimian
      @bodhimian 2 года назад

      len doesn’t tell you the “ last index “ but total quantity of the items in a list (array)

  • @MuhammadRaffey-26
    @MuhammadRaffey-26 2 года назад

    You didn't add the comma after " three"

  • @noumanahmad360
    @noumanahmad360 2 года назад

    Please put a comma after a string three, your array is concatenation those two strings

  • @chiroyce
    @chiroyce 2 года назад +1

    I didn't get it at first - but after rewatching I immediately spotted the missing "," at the end of line #4

  • @vijayg3248
    @vijayg3248 Год назад

    But index start with 0 right

  • @root0z568
    @root0z568 2 года назад

    Whats your vscode theme?

  • @natew4724
    @natew4724 2 года назад

    You could type print(2+2) and even if it prints 5 your issue will still be closed as a wontfix

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 года назад

    Is implicit string concatenation a good idea? It’s in C, C++ and Python, but not in Java or PHP, or in C♯ that I can tell.
    I personally like it.

  • @trayambakrai
    @trayambakrai 2 года назад

    It's an intended feature in every language unlike some weirdos ( *cough cough, LUA* ), list/array objects' index ALWAYS starts at 0.

  • @leonardssh
    @leonardssh 2 года назад

    color scheme?

  • @yashrajagarwal6479
    @yashrajagarwal6479 2 года назад +4

    *Le me realising the comma he forgot after three and four after trying this in my computer 🤣😂

  • @ybgamer4777
    @ybgamer4777 2 года назад +2

    because three and four are not seperated by comma, so that are treated as single concatenated string. HappY coding! :)

  • @Kate-m5v1e
    @Kate-m5v1e 2 года назад +1

    "why do you like strict typing? Dynamic typing is so much easier"
    Least my code tells me when I made a super simple error like this

    • @stewartzayat7526
      @stewartzayat7526 2 года назад

      C and C++ have the same feature though. This has nothing to do with typing

    • @Kate-m5v1e
      @Kate-m5v1e 2 года назад

      @@stewartzayat7526 it would throw an error though. This some how turned "three" "four" into one object, not sure how, I don't use this language

    • @stewartzayat7526
      @stewartzayat7526 2 года назад

      @@Kate-m5v1e no, it wouldn't throw an error, it would run just fine. As I said, C and C++ have this feature too.

    • @Kate-m5v1e
      @Kate-m5v1e 2 года назад

      @@stewartzayat7526 just tried in it c++ "expected a ","" what compiler do you use that allows for this

    • @stewartzayat7526
      @stewartzayat7526 2 года назад

      @@Kate-m5v1e my cpp file looks as:
      #include
      int main(int argc, char **argv) {
      const char *arr[] = {
      "one", "two", "three" "four", "five"
      };
      printf("Size is: %zu
      ", sizeof(arr) / sizeof(arr[0]));
      return 0;
      }
      I compile just fine with
      g++ main.cpp -o main

  • @jakubtesarek2040
    @jakubtesarek2040 2 года назад

    I don't understand what's the problem. What else it should return for list of 4 elements? Someone please explain

  • @masterofcoding9278
    @masterofcoding9278 Год назад

    In python the index starts with zero thats why you are getting the output as '4'

  • @bonle3771
    @bonle3771 2 года назад

    What theme is it

  • @ziia0528
    @ziia0528 2 года назад

    Is it length or index of last item.

  • @ramireddyvajrala79
    @ramireddyvajrala79 Год назад

    List index starts from 0 so 0,1,2,3,4

  • @gabrielschons3784
    @gabrielschons3784 2 года назад +2

    between "three" and "four" dont have a separator (in this case ",") becouse this, "three" and "four" are considered one value

    • @ZelenoJabko
      @ZelenoJabko 2 года назад +1

      Wow python such mature language

  • @MillionviewsYT1
    @MillionviewsYT1 Год назад

    the string are concatenated b/c of you forget to add comma btwn line 3,4 so py reads 34

  • @rikkyplayz7390
    @rikkyplayz7390 2 года назад

    Correct me if I am wrong, but, doesn't the indexing start from 0 instead of 1?
    (I'm a beginner)
    Edit: I understand now, the strings in line 4 and 6 have no commas, so the strings are being concatenated.

    • @r0lfu_
      @r0lfu_ 2 года назад

      nope, if you have 3 strings you will get the result 3.

    • @rikkyplayz7390
      @rikkyplayz7390 2 года назад

      @@r0lfu_ oh, okay

  • @ferociousfeind8538
    @ferociousfeind8538 2 года назад +1

    "Finally, zero-indexed length operations...! Oh, nope, just a missing comma. Damn."

  • @sunitajaiswal6675
    @sunitajaiswal6675 2 года назад

    Please sir tell me which theme are you using

  • @gamytuber6903
    @gamytuber6903 2 года назад +1

    forgot to put a comma after three

  • @ImNotSion
    @ImNotSion 2 года назад

    Understandable it's concatenation but bruh do all of you understand the index starts. 0. So 0 1 2 3 4 5. It looks like 5 elements but machines always count from 0 and on which is why index is still 4.

  • @w0lfy89
    @w0lfy89 2 года назад

    Is is because there's no comma after three?

  • @rohanphilip2001
    @rohanphilip2001 2 года назад

    What ide do you use for Python,
    For me pycharm as problems

  • @freedom_aint_free
    @freedom_aint_free 2 года назад +4

    As I was trying to reproduce the error in my machine, my visual system's pattern recognition spotted a visually screaming absent comma, and so Bingo! String concatenation as other have rightly pointed out ! But was a gotcha for sure !

  • @Rafian1924
    @Rafian1924 2 года назад +4

    Hehe.. you missed the comma there.. purposely. I am learning python from you bro. Happy to see you.

  • @shawnhamby1380
    @shawnhamby1380 2 года назад

    Do you know python array starts at 0,1,2,3,4

  • @abhishekkumarsharma4460
    @abhishekkumarsharma4460 2 года назад

    There is no coma after three so thats why it is showing like that

  • @sjkgaming2174
    @sjkgaming2174 2 года назад

    Where is comma after "three"?

  • @sehajgaming7086
    @sehajgaming7086 2 года назад

    You didn't give a comma after 'three'

  • @Thomate1375
    @Thomate1375 2 года назад

    Not sure if the missing comma after the three is the problem... But most languages start to count at 0 so you have to add 1 to the result ^^

  • @w3bhook25
    @w3bhook25 2 года назад

    this is a bug depending on how you implement it, in this case it is the developers fault for concatenating the strings "three" and "four".

  • @TheFootballPlaya
    @TheFootballPlaya 2 года назад +1

    lol you got me, i thought there was a comma after "three". I typed it up in python 3.8 in my editor and it was working, i put the comma after "three". i was like what the heck?

  • @sparky7663
    @sparky7663 2 года назад

    You have to put comma betn three and four

  • @sriganthr4140
    @sriganthr4140 2 года назад

    List length is starting from 0 th index 0,1,2,3,4 ..

  • @DanielMo09
    @DanielMo09 2 года назад

    I think its because the 1st item is index 0 and item 2 is index 1 and so on

  • @rannyarcher29
    @rannyarcher29 2 года назад

    comma separated is missing thats why it shows 4 not 5

  • @span4ev
    @span4ev 2 года назад

    the author of the channel just bait people.
    Length 4 because the author deliberately did not put a comma and the strings were concatenated:
    ['one', 'two', 'threefour', 'five'],

  • @nordicus666
    @nordicus666 2 года назад

    You purposefully missed a comma, it’s a clickbait, not a but or a feature, it just works as it is written