No video

Writing a Simple Factorial Program. (Python 2)

  Рет қаралды 244,177

Khan Academy

Khan Academy

13 жыл бұрын

Writing a simple factorial program with a "for" loop

Пікірлер: 110
@timmytim9054
@timmytim9054 5 жыл бұрын
Omg I never knew Sal Khan taught this. The guy literally knows everything ....
@rouxgreasus
@rouxgreasus 3 жыл бұрын
He's wiser than solomon
@DHDemid
@DHDemid 4 жыл бұрын
Simply the best! My dream is to say you "Thank you " personally !
@MemoryDealer
@MemoryDealer 13 жыл бұрын
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.
@murtazaakbarreborn
@murtazaakbarreborn 3 жыл бұрын
This works sir. I was stuck on this. Thank you so much!
@dellbucket
@dellbucket 13 жыл бұрын
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 !
@gorkimcgregor8137
@gorkimcgregor8137 5 жыл бұрын
number= int(input("Enter the number= " )) product=1 for i in range(number): product= product*(i+1) print("Factorial of Number is :", product)
@hedonism13
@hedonism13 13 жыл бұрын
That fucking awesome feeling you get when something works. Thanks Sal!
@brHOLLAND
@brHOLLAND 13 жыл бұрын
Can't wait to see more programming vids. Thanks, Sal!
@dneelyep
@dneelyep 13 жыл бұрын
Very cool! A Computer Science set of videos sounds awesome.
@andrewvirtual
@andrewvirtual 10 жыл бұрын
thanks you're a life-saver!
@khanacademy
@khanacademy 13 жыл бұрын
@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_shopping
@attention_shopping 13 жыл бұрын
LOVE programming :) Amazing
@Swetlana0
@Swetlana0 13 жыл бұрын
OMG! Am I dreaming? Keep 'em coming!
@FireheadLazzo
@FireheadLazzo 12 жыл бұрын
@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_aa
@userr_aa 2 жыл бұрын
Thank u
@deskasoexe7216
@deskasoexe7216 12 жыл бұрын
Great tutorial. Thanks.
@systemofadown1986
@systemofadown1986 9 жыл бұрын
thanks a lot, it really helps me
@078matthijs
@078matthijs 13 жыл бұрын
awesome, thanks Sal!
@zkhan2023
@zkhan2023 4 жыл бұрын
you are truly legend sir
@mickstarify
@mickstarify 13 жыл бұрын
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?
@likewhatijustsaid
@likewhatijustsaid 11 жыл бұрын
Touché my good sir!
@Fighterftwful
@Fighterftwful 13 жыл бұрын
Keep it up.. Ill figure this out sooner or later.
@johnlyons7678
@johnlyons7678 6 жыл бұрын
My problem is get is that I cant seem to convert the string of range to an int. Anyone else get that problem too?
@woo216
@woo216 13 жыл бұрын
wow khan on programming now! omg genius much>?
@Omlet221
@Omlet221 3 жыл бұрын
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?
@inbanpythonic415
@inbanpythonic415 6 жыл бұрын
you can simple this: import math n = int(input("enter the number: ")) result = math.factorial (n) print(result)
@pogboy537
@pogboy537 4 жыл бұрын
Bruh
@ananthsb3955
@ananthsb3955 3 жыл бұрын
@@pogboy537 this is what i was looking through.
@FareehahAdeniran
@FareehahAdeniran Жыл бұрын
Good!
@itked3
@itked3 13 жыл бұрын
khan + programming? very cool. plus I've wanted to learn python for quite a while :)
@mikijasar2594
@mikijasar2594 6 ай бұрын
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
@vijaykavuri
@vijaykavuri 5 жыл бұрын
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-tc5nw
@RaviPrakash-tc5nw 6 жыл бұрын
sir is there any Increment decrement operator in python a++,a--
@Redexn
@Redexn 13 жыл бұрын
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.
@ThinkandCreate
@ThinkandCreate 7 жыл бұрын
I modified mine a little...to include the index values: def factorial (number): if number
@Fatima-xb6mp
@Fatima-xb6mp 2 ай бұрын
"Why isn't the translation feature enabled for some passages, including this one?"
@pedroalonsoraffoleon7781
@pedroalonsoraffoleon7781 4 жыл бұрын
Thanks bro!
@gryphonkrinsky2405
@gryphonkrinsky2405 11 жыл бұрын
my version of python prints all the steps how do I just get it to show the final answer only
@Grkgermn333
@Grkgermn333 13 жыл бұрын
Really awesome, but you should really do your tutorials in Python 3.xx
@nooro.o2156
@nooro.o2156 11 жыл бұрын
now it's clear . thanq
@Hector-bj3ls
@Hector-bj3ls 11 жыл бұрын
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.
@MemoryDealer
@MemoryDealer 13 жыл бұрын
@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.
@henik9
@henik9 13 жыл бұрын
If I ever have a chance of learning how to program, it'll be from Sal.
@AlderDragon
@AlderDragon 13 жыл бұрын
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
@annabelhuang4529
@annabelhuang4529 5 жыл бұрын
thanks a lot, but this program doesn't work on Pycharm. What is the correct program on Pycharm?
@williamhad
@williamhad 13 жыл бұрын
@xXSparky117Xx whats better for a beginner c++ or python?
@Callalily_
@Callalily_ 6 жыл бұрын
thank you
@jumung2012
@jumung2012 11 жыл бұрын
whoa, so is this how calculators work?
@makiolo2011
@makiolo2011 11 жыл бұрын
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
@vibol03
@vibol03 13 жыл бұрын
@ashconnor yeah i know thx for pointing that out
@noxure
@noxure 13 жыл бұрын
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
@vibol03
@vibol03 13 жыл бұрын
@woo216 yes absolutely
@777jimi777
@777jimi777 13 жыл бұрын
Sal please do a C++ playlist :D
@ethanabaker1
@ethanabaker1 13 жыл бұрын
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.
@SaniSensei
@SaniSensei 11 жыл бұрын
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.
@prithvib8662
@prithvib8662 7 жыл бұрын
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?
@manasansiva8878
@manasansiva8878 7 жыл бұрын
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)
@hedonism13
@hedonism13 13 жыл бұрын
Whenever I try to open PyScripter, I get an error box saying: "Python could not be properly initialized. We must quit." Wut do?
@hedonism13
@hedonism13 13 жыл бұрын
When will this be on the KA site?
@hz2229
@hz2229 2 жыл бұрын
Thank you Sir! O7
@sokhengchork6450
@sokhengchork6450 7 жыл бұрын
How about C++?
@wesselbindt
@wesselbindt 13 жыл бұрын
Nice, always wanted to learn python. The white background really throws me off, though.
@S3b1Videos
@S3b1Videos 12 жыл бұрын
I'm viewer number 55.555 :-)
@777jimi777
@777jimi777 13 жыл бұрын
@hedonism13 You are probably running x64 windows. You have to download the x86 python from Python's website.
@Anubispop2
@Anubispop2 13 жыл бұрын
SALMON? He's not a fish, dude.
@saligari666
@saligari666 13 жыл бұрын
we want to learn C,or C++ and some object focused programming
@hedonism13
@hedonism13 13 жыл бұрын
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.
@superdau
@superdau 13 жыл бұрын
@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.
@RiyadhElalami
@RiyadhElalami 13 жыл бұрын
Please do java or C++
@nullentrophy
@nullentrophy 7 жыл бұрын
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
@woo216
@woo216 13 жыл бұрын
@vibol03 dude they are all similar well at least thats what i have relised if you learn one you can easily learn the others
@zuberuber
@zuberuber 12 жыл бұрын
number = input() for i in range(1,number): [tab]number = number * i print number isn't it easier?
@ForgedEggs
@ForgedEggs 13 жыл бұрын
@riyadhelalami Get a book on the language you want.
@ali-emam-al-badi4790
@ali-emam-al-badi4790 6 жыл бұрын
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)
@xbl4ck7
@xbl4ck7 5 жыл бұрын
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....
@sansamman4619
@sansamman4619 4 жыл бұрын
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
@redMotif
@redMotif 12 жыл бұрын
You also have to download and install python
@vibol03
@vibol03 13 жыл бұрын
@khanacademy ahh i need to do more research before i comment next time!
@t3po7re54
@t3po7re54 13 жыл бұрын
Is this the first video of the series?
@rockingamingwiththesahit2145
@rockingamingwiththesahit2145 4 жыл бұрын
No
@jfmjaco5941
@jfmjaco5941 Жыл бұрын
Aren't all inputs in python, strings
@hedonism13
@hedonism13 13 жыл бұрын
@bka320 Thanks, 7 days later...
@8bitmagic
@8bitmagic 12 жыл бұрын
DOES IT MATTER, the point is to learn a programing language :/ it makes others easier to learn
@KaushikMishrakk
@KaushikMishrakk 9 жыл бұрын
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
@jiango
@jiango 8 жыл бұрын
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) ?
@sanjeetpal5343
@sanjeetpal5343 7 жыл бұрын
import math num = int(input("Enter the number:")) result = math.factorial(num) print("The factorial of the given number is", result)
@Schindlabua
@Schindlabua 13 жыл бұрын
I guess its fine in this example, but please DO NOT use eval() with actual code that you are releasing somewhere.
@xeztan
@xeztan 9 жыл бұрын
I'm lost :(
@Farzaneh58
@Farzaneh58 9 жыл бұрын
+xeztan see the next video about for loop
@ashconnor
@ashconnor 13 жыл бұрын
@vibol03 Quite an ignorant statement.
@silverreyes7912
@silverreyes7912 7 жыл бұрын
what does this guy not know how to do. lol. what did he get his degree in? Is he an engineer by trade?
@nelsonwetmore
@nelsonwetmore 9 жыл бұрын
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)
@jiango
@jiango 8 жыл бұрын
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) ?
@nelsonwetmore
@nelsonwetmore 8 жыл бұрын
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.
@jiango
@jiango 8 жыл бұрын
nelson wetmore thanks for replying, good explanation
@nelsonwetmore
@nelsonwetmore 8 жыл бұрын
You are welcome :D
@rouxgreasus
@rouxgreasus 3 жыл бұрын
Andrei = "Human" if Andrei == WokenUp: wake Up else: continue sleeping if Andrei == Hungry Eat else: continue doing current thing
@hz2229
@hz2229 2 жыл бұрын
Bruh
@rouxgreasus
@rouxgreasus 2 жыл бұрын
@@hz2229 why?
@hz2229
@hz2229 2 жыл бұрын
@@rouxgreasus actually is kinda makes sense, but you def cannot write that in the programme lol
@rouxgreasus
@rouxgreasus 2 жыл бұрын
@@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!
@hz2229
@hz2229 2 жыл бұрын
@@rouxgreasus yes much better lol
@rustemhuseynli5898
@rustemhuseynli5898 Жыл бұрын
error
@abhishekkaran1205
@abhishekkaran1205 4 жыл бұрын
This is wrong range value not in str
@eyebeebak
@eyebeebak 8 жыл бұрын
this is why python sucks and why php is better.
Stepping Through the Factorial Program
9:24
Khan Academy
Рет қаралды 75 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
Советы на всё лето 4 @postworkllc
00:23
История одного вокалиста
Рет қаралды 4,5 МЛН
Sigma Kid Hair #funny #sigma #comedy
00:33
CRAZY GREAPA
Рет қаралды 41 МЛН
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
R vs Python
7:07
IBM Technology
Рет қаралды 314 М.
What's Your ENGLISH LEVEL? Take This Test!
21:31
Brian Wiles
Рет қаралды 1,7 МЛН
While loops in Python are easy ♾️
6:58
Bro Code
Рет қаралды 335 М.
Recursive Factorial Program Tutorial | Python for Math
5:53
Wrath of Math
Рет қаралды 2,9 М.
Defining a Factorial Function
10:06
Khan Academy
Рет қаралды 90 М.
Python Tutorial for Absolute Beginners #1 - What Are Variables?
24:38
Recursive Fibonacci Example
7:37
Khan Academy
Рет қаралды 136 М.
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН