What is integers?(python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is integers?(python)

What is integers,float and def?

23rd Jun 2018, 8:39 AM
NAWZ KADRI
NAWZ KADRI - avatar
3 Answers
+ 1
Integers are whole numbers. Floats are decimal numbers. def is a statement, that is used to define own functions. Functions are objects that takes returns a value depending on their arguments, functions are written as function name followed by a parenthesis pair having comma separated arguments inside; func(arg, arg) Own functions are defined started with a def keyword following function name followed by a parenthesis pair, items inside the parentheses of function definition are called parameter rather than arguments, after def, and function name followed by parenthesis pair having comma separated parameters you should create an indent with a colon followed by newline and white space, then you can write statements there, you can use those parameters like any other variable, then you return the result with the return statement, when you use return, the function stops being executed. This is how to define a function: def Addition(par1, par2): return par1 + par2 print("this won't be printed") ...
23rd Jun 2018, 10:16 AM
Seb TheS
Seb TheS - avatar
0
Text limit occurred! ... in the example we defined a function called Addition using 2 parameter (par1 and par2) that will return the sum of those parameters, to run that code, you have to call that function with 2 arguments, those arguments must be of same type, because of adding 2 different types together causes a TypeError, (Python allows Integers and Floats to be added). When would call it like: Addition(1, 2) those integers inside the Addition call are the arguments of addition and will be put to the corresponding parameters of the definition of Addition. In the body of Addition we returned par1 + par2, in the function call we used arguments 1 and 2, so par1 corresponds 1 and par2 corresponds 2, we return par1 + par2, which is 1 + 2 by the Addition call and results 3, the Addition(1, 2) call resulted to that the function returned the sum of those arguments, when you type X = Addition(1, 2), X would be 3, same works for any numbers, if we used Addition(4, 9) it would return 13. ...
23rd Jun 2018, 10:57 AM
Seb TheS
Seb TheS - avatar
0
... If you call a function with wrong amount of arguments it would result in TypeError but you can use = to make a default value in case you want to allow function to be called with less than "right amount" of arguments: def Sample(x, y=0): ... to allow a default value for a parameter inside function definition, that is a default value, if you call Sample with 1 argument, the second parameter (y) takes it's default value which is 0 in case that Sample takes 2 arguments, the default value (0) will be overwritten. you can also use * in case you want to call a function with more than "right amount" of arguments: def Sample2(x, *y); ... In Sample2, *y will take rest of the arguments left from normal parameters (which is x), *y would take rest of items as Tuple, when you want to use *y as variable inside function statements, you use it as y. Those special parameters having default values and * in front of them must be put after normal parameters of the function definition.
23rd Jun 2018, 10:59 AM
Seb TheS
Seb TheS - avatar