Tutorial on while loop

  Рет қаралды 52,769

IIT Madras - B.S. Degree Programme

IIT Madras - B.S. Degree Programme

Күн бұрын

Пікірлер: 59
@anishbhattacharya9582
@anishbhattacharya9582 11 ай бұрын
# find the no. of digits in a number :) a=str(abs(int(input("give any integer:")))) print(len(a)) easy way
@avenumadhav3568
@avenumadhav3568 3 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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)
@meriid7017
@meriid7017 3 ай бұрын
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 жыл бұрын
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 жыл бұрын
could also be (0 - rev)
@avenumadhav3568
@avenumadhav3568 3 жыл бұрын
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.
@1nsh
@1nsh Жыл бұрын
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 6 ай бұрын
Easier way to do it: n=(str(input('Enter a Number:'))) n=n.lstrip('-') print(len(n))
@SamparkLakshya
@SamparkLakshya 17 күн бұрын
what about num = int(input('Enter a number: ')) num_string = str(num) length = len(num_string) if num >= 0 : print(length) else : print(length - 1)
@bharathraghu9195
@bharathraghu9195 2 жыл бұрын
As someone new to coding, how to get acquainted with the logic of the code?
@ABHARNAHRAJARAMMOHAN
@ABHARNAHRAJARAMMOHAN 2 жыл бұрын
i would recommend datacamp. There are python tutorials especially for data science and beginners.
@saunakroychowdhury5990
@saunakroychowdhury5990 Жыл бұрын
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 3 ай бұрын
20:10 Can't we just add a minus sign before printing rev. Like print(-rev). It seems to work fine.
@TheUnknownU
@TheUnknownU 3 жыл бұрын
print(rev * -1) can we use this too?
@05-ajitkulkarni88
@05-ajitkulkarni88 Жыл бұрын
WE CAN ALSO SOLVE THE PROBLEM-3: num=int(input("enter the no.")) rev=str(num) print(rev[: :-1]
@tauxic
@tauxic Жыл бұрын
can you please explain the last line?
@bhaviniee
@bhaviniee 11 ай бұрын
its a shortcut used in strings to reverse it@@tauxic
@nitinfaldoliya656
@nitinfaldoliya656 20 күн бұрын
here - will also be shifted in last
@sarthaksingh601
@sarthaksingh601 3 жыл бұрын
4:49 the code now fails the test case (num = 0)
@rohinibharti4463
@rohinibharti4463 3 жыл бұрын
We can print fact for that case
@umarulf
@umarulf 2 ай бұрын
i have doubt question 3
@TKKARTIK-t4d
@TKKARTIK-t4d 11 ай бұрын
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
@learnwithrr1230
@learnwithrr1230 2 жыл бұрын
reverse digit program fails when i entered 0123 and 1230 why? how to rectify it?
@saunakroychowdhury5990
@saunakroychowdhury5990 Жыл бұрын
No, it shouldn't fail. Perhaps you made some mistakes
@arifinonim4677
@arifinonim4677 8 ай бұрын
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))
@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 :)
@steveraphaelpulikottil5755
@steveraphaelpulikottil5755 2 жыл бұрын
Problem 2 i/p 0 Exo/p should be 0, right??
@SOUMAYGUPTA24
@SOUMAYGUPTA24 6 ай бұрын
problem-3 n = int(input("Enter a number: ")) num = str(abs(n)) num = num[::-1] if(n>0): print(num) else: print("-"+num)
@reddygopichand2002
@reddygopichand2002 2 жыл бұрын
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")
@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 ???
@CherryIITM
@CherryIITM 3 жыл бұрын
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 жыл бұрын
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.🤝
@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 жыл бұрын
More shorter :) n = abs(int(input ()) b = str(n) Print(len(b))
@ranjeet_sohanpal
@ranjeet_sohanpal 2 жыл бұрын
Which compiler is being used by Sir ?
@vishalmadhesia884
@vishalmadhesia884 2 жыл бұрын
spider
@selvamselvam7960
@selvamselvam7960 2 жыл бұрын
Problem 2: digit=digit+1 Digit=1+1=2 please someone explain this
@jussstdoiit
@jussstdoiit 3 ай бұрын
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")
@SOUMAYGUPTA24
@SOUMAYGUPTA24 6 ай бұрын
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 6 ай бұрын
please let me know if this is a valid piece of code
Introduction to for loop
10:54
IIT Madras - B.S. Degree Programme
Рет қаралды 35 М.
Tutorial on nested loops
20:56
IIT Madras - B.S. Degree Programme
Рет қаралды 36 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 257 М.
Introduction to while loop
13:06
IIT Madras - B.S. Degree Programme
Рет қаралды 45 М.
BachGPT is back: Can AI Compose Like the Greatest Musician of All Time?
14:06
Nobody & The Computer
Рет қаралды 29 М.
Tutorial on if, else and else-if (elif) conditions
25:10
IIT Madras - B.S. Degree Programme
Рет қаралды 48 М.
Formatted Printing
21:12
IIT Madras - B.S. Degree Programme
Рет қаралды 43 М.
for Loop vs. while Loop in Python
8:45
Neso Academy
Рет қаралды 48 М.
Birthday Paradox
25:39
IIT Madras - B.S. Degree Programme
Рет қаралды 40 М.
Tutorial on for loop and difference between while loop and for loop
20:15
IIT Madras - B.S. Degree Programme
Рет қаралды 37 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН