Posts

Showing posts from July, 2024

Python Prime Number For & While Loop

  wap to create a function which display the factorial of five. def fact (n): if n == 0 : return 1 else : return n * fact(n - 1 ) print (fact( 3 )) also using whileeee loopp seee this # Factorial of 5 def fac (n): r= 1 i= 1 while i<=n: r=r*i i+= 1 return r print (fac( 3 )) wap to create a function for display n number of prime Numbers. def prime (n): c = 0 for i in range ( 1 , n + 1 ): if n % i == 0 : c += 1 if c == 2 : print ( "Its a Prime" ) else : print ( "compo" ) prime( 8 ) another method using while n = int ( input ( "Hello" )) c = 2 x = 0 while c < n: if n % c == 0 : x = 1 break c += 1 if x == 0 : print ( 'prim,' ) elif x == 1 : print ( "compo" ) def prime (n): c = 2 x = 0 while c < n: if n % c == 0 : x = 1 break c += 1 if x == 0 ...

Python Practise Function

Image
FUNCTIONS Introduction:  A function is a block of code which runs when it is called. Function helps to break the program into smaller chunks. As program grows larger and larger functions make it more organised and manageable. Functions avoids repetitions and makes the code reusable.  Syntax: def func_name(parameters): ---function definition Statements ---function body Return data ---return/exit point of function data = func_name(arguments) ---function call Types of functions: There are mainly two types of functions. 1. pre defined functions or built in functions: the functions which are designed while python designing are called  built in functions. ex:      id() --------returns address of the object type()------returns type of the object(data type) input()----accepts input from keyboard and returns to program print()----returns print statement to the console ord()-----returns ASCII value of any character chr(...