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.

😀😀😀✒✒

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. ***

Input method

Python program uses input method to take input from user and based upon that input it gives output or sometime use that input in further program. eg , the website , “Enter your name : “. so,it taking and storing the name in profile of user. also,sometime it check whether the given email address is present or not. if present it gives email addresd already exist.

Syntax : input(prompt)

name = input(“Enter your name: “)

Output: Enter your name : ■■■

The input method is in string method. In place of ■■■ we store our name :vipul,jack etc. then it give output vipul or jack .

***It also taking the integer or float , form of string.***

age = input(“Enter your age : “)

Output: Enter your age : 19

Here,we had put age 19. we have to check what is the data type of 19.

>>>age

’19’

So,we had seen 19 is in string .

Basically,19 is integer so we have to convert string 19 in integer 19.

age = int(age)

In this time,

>>> age

19

Example,To check person is applicable to voting or not.

age = input(“Enter your age: “)

# convert string number to integer

age = int(age)

# check that person is applicable for voting or not.

if age >18:

print(“you are applicable to voting !”)

else:

print(“you are not applicable to voting !”)

Output:

Enter your age: 19

you are applicable to voting !

🙂🙂✒✒

Dictionary inside dictionary.

Dictionary present inside dictionary, this is storing information about different users in one dictionary. with help of this we have not to store different dictionary in a list. eg,information about different scientists in one dictionary.

users = {

“aeinstein”:{

“fname” : “albert”,

“lname” : “einstein”,

“location” : “prienceton”,

},

“mcurie”:{

“fname” : “madam”,

“lname” : “curie”,

“location” : “france”,

},

}

for name,info in users.items():

print(“User name: “+name)

full_name = info[“fname”]+

info[“lname”]

l= info[“location”]

print(“/t”+”Full name: “+full_name)

print (“/t”+”location:”+l)

Output:

User name:aeinstein

Full name:Albert einstein

location:princeton

Usetname:mcurie

Full_name:madamcurie

location:france

😄😄

Python list inside dictionary.

       Sometime we have to provide list inside dictionary to give many option to key.

       Example. Bank is providing different loan option such as 1lac,10lac,50lac.

       So,what is purpose of this in applications ?

       In a website there is one service but users are many. example, website = {“service”:”music”,”users”:   [“aman”,”raj”,”jack”]}

# summarize the order

print(“The users are: “)

for s in website[“users”]:

          print(“\t”+s)

Output: The users are:

aman

raj

jack

If else statement

Suppose,we have to find that user exists in website or not. so most basic method is check new user by every exist user ,name by name.

But if there are millions of users in website . So,it will take to much time so in this condition we , can use if – else statement.

  1. If user exist it will display statement , user exist in website.
  2. else it will print user not exist in website.

Website = [“aman”,”ragay”,”john”,”sarah”, “edward”,”rose”]

if “sarah” in Website:

print(“user is exist in website! “)

else:

print(“user is not in website !”)