Writing a simple factorial program with a "for" loop
Пікірлер: 110
@timmytim90545 жыл бұрын
Omg I never knew Sal Khan taught this. The guy literally knows everything ....
@rouxgreasus3 жыл бұрын
He's wiser than solomon
@DHDemid4 жыл бұрын
Simply the best! My dream is to say you "Thank you " personally !
@MemoryDealer13 жыл бұрын
This makes me infinitely happy Sal is posting computer science videos. To those wanting other languages, they'll probably come later. Python is an excellent starting language.
@murtazaakbarreborn3 жыл бұрын
This works sir. I was stuck on this. Thank you so much!
@dellbucket13 жыл бұрын
legendary, thanks Sal, im a python hacker myself and its good to see you popularizing a great language. I have also gone through alot of your mathematics videos and finance videos and man , i hope KA runs for a very long time !
@gorkimcgregor81375 жыл бұрын
number= int(input("Enter the number= " )) product=1 for i in range(number): product= product*(i+1) print("Factorial of Number is :", product)
@hedonism1313 жыл бұрын
That fucking awesome feeling you get when something works. Thanks Sal!
@brHOLLAND13 жыл бұрын
Can't wait to see more programming vids. Thanks, Sal!
@dneelyep13 жыл бұрын
Very cool! A Computer Science set of videos sounds awesome.
@andrewvirtual10 жыл бұрын
thanks you're a life-saver!
@khanacademy13 жыл бұрын
@vibol03 Google uses it for a lot of their development (I believe it is their language of choice). Khan Academy's server side code is all Python.
@attention_shopping13 жыл бұрын
LOVE programming :) Amazing
@Swetlana013 жыл бұрын
OMG! Am I dreaming? Keep 'em coming!
@FireheadLazzo12 жыл бұрын
@ImUncreative100 These are the fundamental skills that every computer programmer MUST know and be familiar with. No, you probably won't use these exact programs in your day-to-day life, but you need to familiar with For Loops and passing arguments in order to build even basic programs. Sal is just laying down the basics of the language. Once you get the basics down, you can begin practicing with larger projects.
@userr_aa2 жыл бұрын
Thank u
@deskasoexe721612 жыл бұрын
Great tutorial. Thanks.
@systemofadown19869 жыл бұрын
thanks a lot, it really helps me
@078matthijs13 жыл бұрын
awesome, thanks Sal!
@zkhan20234 жыл бұрын
you are truly legend sir
@mickstarify13 жыл бұрын
would it be slightly faster if you made it 'for i in range (1, number + 1)' and not add 1 to i in the loop?
@likewhatijustsaid11 жыл бұрын
Touché my good sir!
@Fighterftwful13 жыл бұрын
Keep it up.. Ill figure this out sooner or later.
@johnlyons76786 жыл бұрын
My problem is get is that I cant seem to convert the string of range to an int. Anyone else get that problem too?
@woo21613 жыл бұрын
wow khan on programming now! omg genius much>?
@Omlet2213 жыл бұрын
Would adding the eval around input be backwards and forwards compatible with both python 2 and 3? It would just be redundant on 2 right?
@inbanpythonic4156 жыл бұрын
you can simple this: import math n = int(input("enter the number: ")) result = math.factorial (n) print(result)
@pogboy5374 жыл бұрын
Bruh
@ananthsb39553 жыл бұрын
@@pogboy537 this is what i was looking through.
@FareehahAdeniran Жыл бұрын
Good!
@itked313 жыл бұрын
khan + programming? very cool. plus I've wanted to learn python for quite a while :)
@mikijasar25946 ай бұрын
Thank you for your video, if I may have a short question. How does your program work in general, given that the input returns a string, and how do you not get an error in the program when you did not convert the input string to an integer?? Thanks in advance for your reply
@vijaykavuri5 жыл бұрын
I am able to get the program to display the answer, however I need it to also display the actual calculation, for example if the user enters 4 I need it to display as: 4! = 1 x 2 x 3 x 4 = 24 any help on how to add this in to the code?? Thanks in advance!
@RaviPrakash-tc5nw6 жыл бұрын
sir is there any Increment decrement operator in python a++,a--
@Redexn13 жыл бұрын
I hate that everyone in the comments is complaining. Are you serious? You should be glad he is teaching programming at all. Maybe in the future he will do a C++ playlist, for now this should be suffice for all of you.
@ThinkandCreate7 жыл бұрын
I modified mine a little...to include the index values: def factorial (number): if number
@Fatima-xb6mp2 ай бұрын
"Why isn't the translation feature enabled for some passages, including this one?"
@pedroalonsoraffoleon77814 жыл бұрын
Thanks bro!
@gryphonkrinsky240511 жыл бұрын
my version of python prints all the steps how do I just get it to show the final answer only
@Grkgermn33313 жыл бұрын
Really awesome, but you should really do your tutorials in Python 3.xx
@nooro.o215611 жыл бұрын
now it's clear . thanq
@Hector-bj3ls11 жыл бұрын
Well yes, but that doesn't help you understand how you would write your own version. While I was learning to program I found it good to practice by writing my own versions of built-in functions. This way I would have something to compare the output to, to make sure my code worked properly.
@MemoryDealer13 жыл бұрын
@williamhad If you want to ease your way into programming start with Python. However, if you want to get straight down to business start learning C and C++. Just try both and after a while you'll decide.
@henik913 жыл бұрын
If I ever have a chance of learning how to program, it'll be from Sal.
@AlderDragon13 жыл бұрын
I did it the opposite way. I just made my for-loop start at 1. for i in range(1, number+1): product = product * i
@annabelhuang45295 жыл бұрын
thanks a lot, but this program doesn't work on Pycharm. What is the correct program on Pycharm?
@williamhad13 жыл бұрын
@xXSparky117Xx whats better for a beginner c++ or python?
@Callalily_6 жыл бұрын
thank you
@jumung201211 жыл бұрын
whoa, so is this how calculators work?
@makiolo201111 жыл бұрын
how can I make the program ask me constantly. for example; I put a 5, so i tells me 240, and then gives me the option to put a new number without closing the program. I know I have to use a while, but I don't know how :S
@vibol0313 жыл бұрын
@ashconnor yeah i know thx for pointing that out
@noxure13 жыл бұрын
omg, Sal is teaching computer science. I would be really pissed off if it turns out you're better than me at this too... but then I have something good to watch, so it's all good. :p
@vibol0313 жыл бұрын
@woo216 yes absolutely
@777jimi77713 жыл бұрын
Sal please do a C++ playlist :D
@ethanabaker113 жыл бұрын
When I do this, no window shows up for input...the program just runs until I kill it...why? I'm using CodeRunner on OS X 10.6.8.
@SaniSensei11 жыл бұрын
it works, but I wouldn't say it's easier. You are passing the "number" variable to the range() function and at the same time change "number" inside the loop. It works and it's okay, if you know that python passes the "number" variable to a local variable inside of range(), but for someone less experienced it might be hard to make out, what's actually going to happen, since changing the variables, that are used inside of the loop header, is generally a nasty thing. So no: not recommended.
@prithvib86627 жыл бұрын
How would you modify this so that it would only do even numbers? E.g. 6! would equal 48 instead of 720 since 6*4*2 = 48?
@manasansiva88787 жыл бұрын
number=int(input('Type your number: ')) product=1 for i in range (0,number+1,2): # 2 means stteping by 2. if i==0: continue else:product=product*i print(product)
@hedonism1313 жыл бұрын
Whenever I try to open PyScripter, I get an error box saying: "Python could not be properly initialized. We must quit." Wut do?
@hedonism1313 жыл бұрын
When will this be on the KA site?
@hz22292 жыл бұрын
Thank you Sir! O7
@sokhengchork64507 жыл бұрын
How about C++?
@wesselbindt13 жыл бұрын
Nice, always wanted to learn python. The white background really throws me off, though.
@S3b1Videos12 жыл бұрын
I'm viewer number 55.555 :-)
@777jimi77713 жыл бұрын
@hedonism13 You are probably running x64 windows. You have to download the x86 python from Python's website.
@Anubispop213 жыл бұрын
SALMON? He's not a fish, dude.
@saligari66613 жыл бұрын
we want to learn C,or C++ and some object focused programming
@hedonism1313 жыл бұрын
Sadly, once this computer science playlist (which I've seen people drooling for on his channel since he revealed his CS background) is finished, I'm pretty sure that's just about everything he can teach us. Then we'll probably start getting new lecturers, unless Sal wants to go back to University to learn engineering or philosophy.
@superdau13 жыл бұрын
@riyadhelalami If you mastered one programming language, it's easy to start another (at least most of them). The concepts are the same. So just follow these videos and there'll be no need for a Java or C-Tutorial.
@RiyadhElalami13 жыл бұрын
Please do java or C++
@nullentrophy7 жыл бұрын
here mine : i=0 fenter=input("eded:") for i in range(1,fenter+1): for a in range(2,fenter): i=i*a print i
@woo21613 жыл бұрын
@vibol03 dude they are all similar well at least thats what i have relised if you learn one you can easily learn the others
@zuberuber12 жыл бұрын
number = input() for i in range(1,number): [tab]number = number * i print number isn't it easier?
@ForgedEggs13 жыл бұрын
@riyadhelalami Get a book on the language you want.
@ali-emam-al-badi47906 жыл бұрын
There will be a little correction .. number=input('enter a number:') fact=1 for i in range(int(number)): fact=fact*(i+1) print(fact)
@xbl4ck75 жыл бұрын
I just need to Enter a numerator for an improper fraction: Enter a denominator Write improper fraction as a mixed number How do i do that? i know its probably simple....
@sansamman46194 жыл бұрын
I think you have learned how to do it by now, but still: #from and import are two python concepts you should study prperly from fractions import Fraction #we will be using fractions so you want to import fractions # ( basically to change decimals to fractions ...etc ) def dec_to_proper_frac(dec): #we use this as our main way of converting a fraction to a decimal sign = "-" if dec < 0 else "" #we use this so we no longer need to deal with negative numbers frac = Fraction(abs(dec)) #abs (absolute value) #and this part deals with the actual conversion #pretty simple stuff, you can see what's happening just by looking at it return (f"{sign}{frac.numerator // frac.denominator} " f"{frac.numerator % frac.denominator}/{frac.denominator}") #this is the main way of doing it so you can find it easily online as well
@redMotif12 жыл бұрын
You also have to download and install python
@vibol0313 жыл бұрын
@khanacademy ahh i need to do more research before i comment next time!
@t3po7re5413 жыл бұрын
Is this the first video of the series?
@rockingamingwiththesahit21454 жыл бұрын
No
@jfmjaco5941 Жыл бұрын
Aren't all inputs in python, strings
@hedonism1313 жыл бұрын
@bka320 Thanks, 7 days later...
@8bitmagic12 жыл бұрын
DOES IT MATTER, the point is to learn a programing language :/ it makes others easier to learn
@KaushikMishrakk9 жыл бұрын
You can even do this! number = int(raw_input('Enter the value:')) product = 1 for i in range (1,number+1): product = product * i print product
@jiango8 жыл бұрын
explain please, the code works but I don't understand. If user inputs 5 so the range is 1 to 6, why isn't the outcome 1x1x2x3x4x5x6 resulting in 6! (720) ?
@sanjeetpal53437 жыл бұрын
import math num = int(input("Enter the number:")) result = math.factorial(num) print("The factorial of the given number is", result)
@Schindlabua13 жыл бұрын
I guess its fine in this example, but please DO NOT use eval() with actual code that you are releasing somewhere.
@xeztan9 жыл бұрын
I'm lost :(
@Farzaneh589 жыл бұрын
+xeztan see the next video about for loop
@ashconnor13 жыл бұрын
@vibol03 Quite an ignorant statement.
@silverreyes79127 жыл бұрын
what does this guy not know how to do. lol. what did he get his degree in? Is he an engineer by trade?
@nelsonwetmore9 жыл бұрын
OOOOR a function :D def factorial(x): '''Returns the factorial of x (x!)''' ans = 1 for i in range(1,x+1): ans = ans*i return ans # factorial(5)
@jiango8 жыл бұрын
explain please, the code works but I don't understand. If user inputs 5 so the range is 1 to 6, why isn't the outcome 1x1x2x3x4x5x6 resulting in 6! (720) ?
@nelsonwetmore8 жыл бұрын
On "pythoncentral.io/pythons-range-function-explained/" " range([start], stop[, step]) start: Starting number of the sequence. stop: Generate numbers up to, but not including this number. " So for 5!, I want range to be 1 to 6 because it won't include 6. Just Pythons Syntax is all I can say.
@jiango8 жыл бұрын
nelson wetmore thanks for replying, good explanation
@nelsonwetmore8 жыл бұрын
You are welcome :D
@rouxgreasus3 жыл бұрын
Andrei = "Human" if Andrei == WokenUp: wake Up else: continue sleeping if Andrei == Hungry Eat else: continue doing current thing
@hz22292 жыл бұрын
Bruh
@rouxgreasus2 жыл бұрын
@@hz2229 why?
@hz22292 жыл бұрын
@@rouxgreasus actually is kinda makes sense, but you def cannot write that in the programme lol
@rouxgreasus2 жыл бұрын
@@hz2229 let's fix it... Andrei = "Human" if Andrei == "WokenUp": print("wake Up") else: print("continue sleeping") if Andrei == "Hungry" print("Eat") else: print("continue doing current thing") there we go!