Tutorial on while loop

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

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

  • @anishbhattacharya9582
    @anishbhattacharya9582 9 месяцев назад +15

    # find the no. of digits in a number :)
    a=str(abs(int(input("give any integer:"))))
    print(len(a)) easy way

  • @avenumadhav3568
    @avenumadhav3568 3 года назад +9

    factorial: 2:40 3:17 4:00
    digits: 7:55 8:50 9:00 9:19 9:30
    reverse: 12:20 13:45 15:08 15:22 15:43 16:00 16:30 17:17 20:12 21:07 21:42
    palindrome: 25:45 26:28 26:50

  • @kizhakkan
    @kizhakkan 3 года назад +10

    In problem no.3 why cant we just multiply 'rev' by negative one (-1) to get the final result instead of subtracting two times 'rev' from 'rev'? Is there anything wrong in doing so? Is it not a normal convention? Am I missing here something?

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

      Yes I also did same

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

      No, you are correct. In this, he made it more complex than necessary. Do it once, and if the input number was less than 0, rev *= -1 (end of if) print(rev)

  • @1nsh
    @1nsh Год назад +2

    In question number 2 we could have also done ---> num = int(input("Enter the number :")) /n (in next line) print(len(str(num)))
    This would have been way easier than the solution tought in this lecture

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

      then -1 would give 2. you will have to add a if block to this.

    • @ShubhamKumar-ty6ly
      @ShubhamKumar-ty6ly Год назад

      Use abs(len(str(num)))

    • @saumilbhatkar2694
      @saumilbhatkar2694 4 месяца назад

      Easier way to do it:
      n=(str(input('Enter a Number:')))
      n=n.lstrip('-')
      print(len(n))

  • @meriid7017
    @meriid7017 Месяц назад +1

    Umm, in palindrome case, we just have to print 'Palindrome' or 'Not Palindrome'. So, can't we just drop the negative case and just go by the absolute (+ve) value as we don't have to bother the negative sign for checking palindrome?

  • @shreyapal6111
    @shreyapal6111 3 года назад +6

    In Problem 3 [Reverse the digits in the given number], why did we use _print(rev - 2 * rev)_ instead of _print(-rev)_ to print the negative of rev?

    • @bhaskartrivedi3114
      @bhaskartrivedi3114 3 года назад +6

      could also be (0 - rev)

    • @avenumadhav3568
      @avenumadhav3568 3 года назад +9

      can we also not use while loop and do it?
      num = int(input())
      s = str(abs(num))
      if num>=0:
      print(int(s[::-1]))
      else:
      print(int('-'+(s[::-1])))

    • @VENKATAKRISHNANG-h7c
      @VENKATAKRISHNANG-h7c Год назад

      ​@@avenumadhav3568thanks for this simple code.

  • @arifinonim4677
    @arifinonim4677 6 месяцев назад

    problem 3:
    n = int(input("Enter a number: "))
    num = abs(n)
    rev = num % 10
    num = num // 10
    while(num > 0):
    r = num % 10
    num = num // 10
    rev = rev*10 + r
    if(n >= 0):
    print(rev)
    else:
    print(rev * (-1))

  • @TKKARTIK-t4d
    @TKKARTIK-t4d 9 месяцев назад

    We can use this as well for reversing of the digits of the number for both postive or negative just using if & elif
    n = int(input("Enter the number:", ))
    if(n>0):
    str_ = str(n)
    print(str_[::-1])
    elif(n

  • @TheUnknownU
    @TheUnknownU 3 года назад +3

    print(rev * -1)
    can we use this too?

  • @SOUMAYGUPTA24
    @SOUMAYGUPTA24 4 месяца назад

    problem-3
    n = int(input("Enter a number: "))
    num = str(abs(n))
    num = num[::-1]
    if(n>0):
    print(num)
    else:
    print("-"+num)

  • @05-ajitkulkarni88
    @05-ajitkulkarni88 11 месяцев назад +5

    WE CAN ALSO SOLVE THE PROBLEM-3:
    num=int(input("enter the no."))
    rev=str(num)
    print(rev[: :-1]

    • @tauxic
      @tauxic 10 месяцев назад

      can you please explain the last line?

    • @bhaviniee
      @bhaviniee 9 месяцев назад +2

      its a shortcut used in strings to reverse it@@tauxic

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

    As someone new to coding, how to get acquainted with the logic of the code?

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

      i would recommend datacamp. There are python tutorials especially for data science and beginners.

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

      logic is a natural thing. first you brainstorm and try at least something, if you fail, think harder, if you still fail, look for solution. Learn thoroughly, repeat. Eventually, it will come naturally

  • @vjickstra
    @vjickstra Месяц назад

    20:10 Can't we just add a minus sign before printing rev. Like print(-rev). It seems to work fine.

  • @umarulf
    @umarulf 28 дней назад

    i have doubt question 3

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

    reverse digit program fails when i entered 0123 and 1230 why? how to rectify it?

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

    Short answer: -
    25:45 palindrome
    n=abs(int(input()))
    p=str(n)
    r=p[::-1]
    rint=int(r)
    if (n==rint):
    print("palindrome")
    else:
    print("not palindrome")

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

    Problem 2 i/p 0 Exo/p should be 0, right??

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

    4:49 the code now fails the test case (num = 0)

  • @AryanGupta-eb2tp
    @AryanGupta-eb2tp Год назад

    problem 3 code will not work on 10,20,30,800. Basically number ending with 0 will not work with this code

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

      Well! guess what? it's working. Kindly check before commenting.

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

      @@saunakroychowdhury5990 I have also Checked , But it is not working for me as well

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

      In case of 10 , it only prints 1 , technically it should be printing 01 right ???

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

    12:20 reverse the digits
    num = abs(int(input()))
    print(str(num)[::-1])
    test case for negative fails!!

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

      num = int(input())
      if(num == 0):
      print(0)
      elif(num > 0):
      print(str(num)[::-1])
      else:
      num = abs(num)
      print((int(str(num)[::-1])) * (-1))

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

      ye wala sayad work karenga :)

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

    Short answer: -
    7:55 number of digits
    n=abs(int(input()))
    import math
    result=int(math.log(n,10))+1
    print(result )

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

      More shorter :)
      n = abs(int(input ())
      b = str(n)
      Print(len(b))

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

    Problem 2: digit=digit+1
    Digit=1+1=2 please someone explain this

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

    My code literally, for q.2
    a=abs(int(input()))
    a=str(a)
    print(len(a))
    Can we use this? Is this Correct?

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

      Only the following is enough:
      a = input()
      if a[0] == '-':
      print(len(a) - 1)
      else:
      print(len(a))
      You don't have to convert the input into an integer and then into a string, because in Python the input is by default a string. P.S. Edited the code, I hadn't considered negative numbers before.

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

      @@rituparnadas8344 Yes, You're right.

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

      @@rituparnadas8344 No it's not working. We have to change it to string. Just now did it in replit

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

      This will not work for negative numbers. -2 is giving length=2 which is clearly false. We can take the length of abs(n) and then add 1 to length if n

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

      @@rituparnadas8344 The logic is correct but we are talking about numbers so maybe the approach is not correct. Btw, similar names.🤝

  • @_zitsu_k
    @_zitsu_k Месяц назад

    question no 4 easily is
    num = (int(input("enter your number: ")))
    int= abs(num)
    int = (str(int))
    if int == int[::-1]:
    print("Palindrome")
    else:
    print("NOT a palindrome")

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

    Which compiler is being used by Sir ?

  • @SOUMAYGUPTA24
    @SOUMAYGUPTA24 4 месяца назад +1

    problem-3
    n = int(input("Enter a number: "))
    num = str(abs(n))
    num = num[::-1]
    if(n>0):
    print(num)
    else:
    print("-"+num)

    • @SOUMAYGUPTA24
      @SOUMAYGUPTA24 4 месяца назад +1

      please let me know if this is a valid piece of code