Import the module or File

module is the current file telling python to do the work of stored file in current file.

So,basically why we have to import the file ?

If there is large program so with help of module we can break in small files.

pizza.py

def make_pizza(
size,*toopings
):
""" make_pizza
function give
information about
size and toppings"""

print("The size of
toppings is
"+str(size)
+"!")
for topping in
toopings :
print(tooping)

Create new file im.py

im.py

import pizza

pizza.make_pizza(16,
"mushroom",
"peporoni")

Output:

The size of toppings is:
16

mushroom
peporoni

Arbitary arguments

Python program to show the arbitrary arguments. When program don’t know how many arguments are passed so arbitrary arguments are coming. Arbitrary arguments pack all arguments in tuple.

def show_magicians(         
*magicians
):
"""show_magicians
function show
information about
magicians """

print(magicians)

# calling a function
>>>show_magicians(
"Bob",
"alice")

Output: ("Bob","alice")


Here the values are come in tuple . But we need the information in series of list through loop. So we use for loop to pass arguments as a series of list.

def show_magicians(        
*magicians
):
""" show
information about
magicians """

for magician in
magicians :
print(
magician
)

>>>show_magicians(
"Bob",
"alice"
)

Output:

Bob
alice

Suppose we need positional argument with arbitrary argument. To add another argument with show_magicians. we need circus name with arbitrary arguments.

F♨️n🌜ti🎡n return a di🌜t💈onary !🙂🙂

Suppose , we have to add additional information about user. So,how can we add a additional information when we need? With help of optional argument which has default value string. We will return a dictionary which come additional information about a user.


 # creating a function
def build_person(fname,lname,    
                               age=” ):
         
           “”” Return a information

about a person “””

# storing dictionary in a variable

person     ={                            

                     “first “:fname,    

 
                     “last”:lname ,     


                       }  
 
# using if condition

# if this condition is true then

# age key add to dictionary.
         if age:
             person[“age”] = age
         return person

>>>musicians = build_person(“vipul”,”kunwar”,19)
>>>print(musicians)

Output:{“first”:”vipul “,”last “:“kunwar “,age:19}

Python function

😊😊😊😊🎸🎸!

Return a function

Python use the return statement to come up with a value . Sometimes, user don’t need direct Output, so it uses return statement. return statement ,using processed value. Suppose the user wants to come up with full name but we can’t come directly so,we use return statement.

def name(fname,lname):
""" return statement
using to come up
with full_name """

# store in a
variable
full_name =fname+"
"+lname
return
full_name.title()

>>>musicians
=name("vipul
","kunwar ")
print(musicians)

Output:

Vipul Kunwar

Function in python

       Python program to demonstrate the function.

       Suppose,you have to store user name repeatedly for one particular statement. So,we can create function.

Functions  are using for doing specific job repeatedly. Functions are using for different arguments.

# defining the function def greet():
    print("Hello!")

# calling the function
>>>greet()
Hello!

# Here, we have not passed any information.
# we have just called the function which doing specific job.

# the function called using parenthesis ().

# the first statement line of function called docstring .
# docstring define what function is telling.

# The statement which belong to body of function are indented using space.
Parameter: it is      
           variable of function which store
some value about function .

def greet(name):
    print("Hello"+name)

# calling the function
>>>greet("vipul")
Hello vipul

# here name is parameter.
     
Arguments:it is specific  
value related to function.

In above example,"vipul " is information about user.

Note :sometimes         
arguments and parameters are used as same. Because both are related to same object.

Positional arguments:                       

           The argument which specify position of arguments. So,basically why we need order in argument ?

The python need which parameter specify to which argument.  So,for that positional argument need.

def greet(name,age):
    print("user name
is :" +name+",age
is "+age)

# calling the function with passing arguments.

>>>greet("vipul","19")   

Output:

user name is vipul,age is 19

# Here python specify that name is combine with "vipul" and age is combine with "19".

😀😃🎸

Moving Item From one list to another list.

         Suppose, you  want  to  confirmed  the  user  from  one  list   to  another list  we  can  confirm  using  while  loop. we  will confirmed   the user  is  verified   or  not.

(1) unconfirmed_users =   ["alice","brian","cadace"]
confirmed_users =[]

# verify each user until # there are no more  #unconfirmed user

# move each verified user into the list of confirmed users

(2)
while unconfirmed_users:
      current_users = unconfirmed_users.pop()
print("verify user:"+current_users)

confirmed_users.append
(current_users)

# Dislay the #confirmed_users

(3) print("The following users have been confirmed : ")

(4)for confirmed_user in
confirmed_users:
print(confirmed_user)

Output:
verify user: cadace
verify user: brian
verify user: alice

The following users have been confirmed:
cadace
brian
alice

Explanation:

At (1) the list is unconfirmed_users list.
Then there are empty list of confirmed users.
We have to verify the user then transfer in confirmed_users.

At (2) the while loop is applied ,when the users are unconfirmed_users.
Remove the item from unconfirmed_users, store in current_users variable. The last item will come first because it has pop. Then,this verified users display.

At (3) the statement has display about the confirmed_users.

At (4) for loop has
used for display the confirmed_user.

😀😀😀✒✒

Flag in python.

Suppose there are many condition to check in a game. The conditions which stops the game. So we can do with while loop. But it becomes complex for while loop. so, python uses flag . Flag check if current position is true and many condition are true in game . So, python gives that program is active . Basically flag represent the all condition result.

https://stackoverflow.com/questions/59120347/what-is-a-flag-in-python-while-loops#:~:text=A%20flag%20in%20Python%20acts,of%20event%20makes%20it%20False.

prompt = "\nplease
enter the
message: "
prompt+="\nplease
tell me
something
about you: "

# game is currently
# active
active = True
while active:
a = input(prompt)

if a == "quit":
active = False
else:
print(a)

Output:
please enter the message:
please tell me something
about you:vipul
vipul


please enter the message:
please tell me something
about you:aman
aman


please enter the message:
please tell me something
about you:quit

# The player quit
# the game.

WHILE LOOP😇😇

Python program to demonstrate how the while loop is working. The difference between while and for loop is that for loop applied on every item of list , while loop applied till condition is true.

Flow chart of while loop

Suppose,you has to display number less than 5. so,how will you describe?

1. The starting number

2. The last number

3. Increments in number

# initial number

number = 0

# number less than 5 executed.

while number < 5:
number+=1
print(number)

Output:

1

2

3

4

5

*** you are supposing how the number 5 has come ?

Basically,it is number 4 . in number 4 increment has done . So it becomes 5. ***