Python tutorials for beginners : Part - III

in #utopian-io8 years ago (edited)

What Will I Learn?

  • Dictionary in python
  • Functions in python
  • Global and Local Variables

Requirements and Difficulty

Same as previous tutorials. I will leave links to the previous tutorials at the bottom of this tutorial.

Tutorial Contents

Dictionary:

Dictionary is a data type consisting of the two-values pair: key and value. Dictionary is indexed by key. Keys are immutable and unique whereas value can be duplicate and of any type.Dictionary is enclosed in curly brackets '{}', keys and values are separated by a colon(:) and items are separated by a comma.

dictionary = {key1:value1,  key2:value2, key3:value3, .........}

Some of the inbuilt functions of dictionary are:

dict.items() gives list of key and value pairs in dict

dict.keys() gives list of keys in dict

dict.values() gives list of values in dict

# dictionary declaration
dict = {'name' : 'SCB', 'branch' : 'London','staffs' : 30 }
# printing all items from dictionary
print(dict.items())
# printing all keys from dictionary
print(dict.keys())
# printing all values from dictionary
print(dict.values())
# updating value of key 'name'
dict['name']='SB'
# printing all items after update
print(dict.items())
# adding element to dict
dict['counters'] = 'three'
# printing all items after adding element
print(dict.items())

Output:

[('staffs', 30), ('name', 'SCB'), ('branch', 'London')]
['staffs', 'name', 'branch']
[30, 'SCB', 'London']
[('staffs', 30), ('name', 'SB'), ('branch', 'London')]
[('staffs', 30), ('name', 'SB'), ('branch', 'London'), ('counters', 'three')]

Functions:
Now, we have used many inbuilt functions of python like print(), len(),etc. We can also make our own custom function. Functions are the block of codes which can be reused if we need to perform same action. It saves time by reusing code.

Rules to define a function:

  • It starts with keyword def which is followed by function name and parenthesis.
  • The code block starts after a colon(:) which is indented properly.
  • Arguments or parameters are passed inside parenthesis if necessary.
  • At the end of the function, return statement is written which may pass back value to the caller.
    If there is nothing to pass back no arguments are passed to return statement and is same as return None.
  • The return statement exits the function.
def func_name( parameters ):
   code block
   return [value]

Calling a function:
To call a function we will write the function's name followed by parenthesis. If we need to pass parameter then parameter is placed inside the parenthesis.

func_name(parameters)
# Defining function with no parameter
def hello_world():
    return "Hello World"
# Defining function with parameter
def area_rect(x, y):
    return x * y
# printing Hello World
print(hello_world())
# printing area of rectangle
print(area_rect(4, 5))

In above example code, there are two functions defined one without parameter and another with parameters. First one will return "Hello World" and is printed calling hello_world() function. The second one is defined with parameters and the value of parameters is passed 4 & 5, the result is printed what the area_rect() function has returned.

Output:

Hello World
20

Global and Local variables:
Variables which are defined outside of the functions are known as global variables and the variables which are defined inside the function are known as local variables. Local variables can't be accessed outside the function but global variables can be accessed anywhere in the program.

# defining global variable.
peri = 0;
def peri_rect(l, b):
   peri = 2 * (l + b);
   print "Perimeter inside function : ", peri
   return peri
# calling peri_rect() function
peri_rect( 2, 3);
print "Perimeter outside the function : ", peri

Output:

Perimeter inside function :  10
Perimeter outside the function :  0

In above output we can see clearly that perimeter value that was initialized before defining functions remain unchanged.

For more details visit Python Docs

Above codes can be found on my github link.

Curriculum

We will wrap up here the begginers series here. We will move to more advance level from next tutorials series.
Link of Part I
Link of Part II



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @programminghub I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Up voted and followed.
Please follow me on Steemit: https://steemit.com/@labwork

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Congratulations @programminghub! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got your First payout

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!