Def function and Star Pattern in Python

Creating star patterns is a common practice exercise in Python programming. These patterns use nested loops to control the arrangement of asterisks (*) to form various shapes.

Here are some examples of star patterns in Python:


Def function and Star Pattern in Python


Print 5 Star print without loop -


Multi line star pattern 


print("*")

print("*")

print("*")

print("*")

print("*")


Output-


*

*

*

*

*


Use  (end) single line star pattern 


print("*", end="")

print("*", end="")

print("*", end="")

print("*", end="")

print("*", end="")


Output - ( * * * * * )



Star 5 Pattern using Loop 


Using For loop multi line  -


for el in range(5):

    print("*")


Output -

*

*

*

*

*


Using For loop single line-


  for el in range(5):

    print("*", end="")


Output -( * * * * * )


Star Pattern to range star loop wise pattern -


Multi line -


for i in range(4):

    print("*1")

    for j in range(2):

       print("*2")

    

Output -


*1

*2

*2

*1

*2

*2

*1

*2

*2

*1

*2

*2


Star Pattern to range star loop wise pattern -


Single line


for i in range(4):

    print("*1",end="")

    for j in range(2):

       print("*2", end="")

   


Output - ( *1 *2 *2 *1 *2 *2 *1 *2 *2 *1 *2 *2 )


Serial Line pattern -


for i in range(4):

    print("*1",end="")

    for j in range(2):

       print("*2", end="")

    print()


Output -


*1 *2 *2

*1 *2 *2

*1 *2 *2

*1 *2 *2


Single line two range star pattern i (4) and j (4) cols pattern 


for i in range(4):

    for j in range(4):

        print("*", end="")

   


Output- ( * * * * * * * * * * * * * * * * )



Multi line two range star pattern i (5) and j (4) cols and rows pattern


1 - for i in range(5):

    for j in range(4):

        print("*", end="")

    print()


Output - 


* * * *

* * * *

* * * * 

* * * *

* * * * 




2- for i in range(1,5):

    for j in range(1,6):

        print("*", end="")

    print()

    

    

Output - 



* * * * * 

* * * * *

* * * * *

* * * * *


3 - for i in range(1,6):

    for j in range(1,6):

        if j <= i:

            print("*", end="")

        else:

            print("" , end="")

    print()

            


Output - 


*

* *

* * *

* * * *

* * * * *


4 - for i in range(1,6):

    for j in range(1,6):

        if j <=6 - i:

            print("*", end="")

        else:

            print("" , end="")

    print()

            


Output -


* * * * *

* * * *

* * *

* *

*


5 - for i in range(1,5):

    for j in range(1,8):

        if j <=5 - i and j<=3+i:

        

            print("*", end="")

        else:

            print("" , end="")

    print()

            

    

Output -


* * * *

* * *

* * 

*


6 - p= 1


for i in range(1,5):

    for j in range(1,8):

        if j <=5 - i and j<=3+i:

        

            print(p, end="")

            p=p+1

        else:

            print("" , end="")

    print()

            


Output -


1 2 3 4

5 6 7

8 9

10



Def Function in Python -


Python, the def keyword is used to define functions. Functions are reusable blocks of code that perform a specific task. They can take inputs (arguments) and optionally return outputs.


  1. Def: This keyword signals the start of a function definition.
  2. Function_name: This is a name you choose for your function. It should be descriptive and reflect what the function does.
  3. Parameters: These are inputs that the function can accept. They are listed within parentheses after the function name, separated by commas.
  4. Docstring: This is a brief explanation of what the function does and how to use it. It's enclosed in triple quotes (""").
  5. Function body: This is the indented block of code that defines the function's logic. It contains the statements that the function will execute when it's called.
  6. Return: This statement returns a value from the function. The function execution stops after the return statement is reached.



1 - Addition two Number 


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a + b

   print("Additon : ",c)

add()


2- Subtraction two number 


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a - b

   print("Additon : ",c)

   

add()   



3 - Multification two number -


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a * b

   print("Additon : ",c)

   

add()   



4 - Division two number -


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a / b

   print("Additon : ",c)

   

add()   


This function definition creates a function named greet that takes one parameter, name. The function body then prints a greeting message using the provided name.


Use two def function two input result


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a + b

   print("Additon : ",c)

   

add()

add()  



Use three def function and three input result


def add():

   a=int(input("Enter the number :"))

   b=int(input("Enter the Second Number :"))

   c = a + b

   print("Additon : ",c)

   

add()

add()  

add()



Use return keyword in def function 


def add(p,q): # formal parameters function definition

   

   r = p + q

   return r

a=int(input("Enter the number :"))

b=int(input("Enter the Second Number :"))

c = add(a,b) # actual parameters Function Calling

  

print("Addition" ,c)



Three parameters functions 


1 - Error Syntax -


def fun1(a,b,c):

   print(a,b,c)

   

fun1(3,6)

fun1(6,9,6) 



Correct Syntax -



"" first fun1 a=3 b=6 and c = 0

Second fun1 a= 6 b= 9 and c = 6""


def fun1(a,b,c = 0):

   print(a,b,c)

   

fun1(3,6)

fun1(6,9,6)   


2- def fun1(a,b,c=0):

   print(a,b,c)

   print(c)

   

   

fun1(c=6,a=9,b=6)   


Output -


9 6 6

6


3-  def fun1(a,b,c):

   print(a+b+c)

   

fun1(32, 65)

fun1(77, 57, 47)

fun1(87, 57, 86, 75, 68, 58,46 ,57)


Error syntax 



def fun1(*a):

   print(a)

   

fun1(32, 65)

fun1(77, 57, 47)

fun1(87, 57, 86, 75, 68, 58,46 ,57)


correct syntax 



Using list function 


def fun1(*u):

   print(u)

   h = list(u)

   print(h)

   

fun1(32, 65)

fun1(77, 57, 47)

fun1(87, 57, 86, 75, 68, 58,46 ,57)


Output -


(32, 65)

[32,65]

(77, 57, 47)

[77, 57, 47]

(87, 57, 86, 75, 68, 58,46 ,57)

[87, 57, 86, 75, 68, 58,46 ,57]






Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.