Arrays Concept in Python With Examples

By // No comments:
Python Arrays 
  • An array is a collection of elements that are stored in contiguous memory locations.
  • An array holds fixed number of elements of the same data types.
  • An array can store multiple elements of the same data types.
  • An array is used to store multiple values in single variable.
e.g.
arr_val1="horse"
arr_val2="lion"
arr_val3="man"
  • An array in python can be handled a module named array.
e.g.
import array as aar

Representation of array:-
An array can be declared in different ways and different languages.Here We are going to declared the array in python language. 
  • Python array starts with 0 index.
  • Python array's elements can be accessed via its index.
  • The length of an array = total number of index+1.
3

Modules and Package Concepts in Python

By // No comments:

 Today, we will learn about most important concepts of modules in python.

Modules in Python:-

  • A modules is a .py files containing definitions and statements. So all .py files that we created for our python programs are modules.
  • A python modules can be defined a python program file which contains .py files including python variable , class and functions etc.
  • A python modules provides us the flexibility to organize the code in a logical way.
e.g. Create a modules named as modules.py
def show ():
    print("Welcome to MNT LAB")
#calling the show() function
show()
Output:-
Welcome to MNT LAB
>>> 
3

Lambda and Recursive functions in python

By // No comments:

Lambda function in python:

  • Normal functions have their names and they are defined using the def keyword.An anonymous functions do not have names and they are defined using the lambda keyword.
Syntax:-
lambda arguments:expression
  • An anonymous function defined using lambda can take any number of arguments but can return only one value.
  • Lambda functions can be used wherever function objects are required usually.It is used as an argument to other functions.
  • An anonymous function contains a small piece of code.
3

Function concepts in python Part 2

By // No comments:

We have already explained some basic features of functions in our previous lectures.Before learn this part ,you have to learn function concepts in python part 1.

Types of Arguments:-

There are four types of arguments in python functions as given below:-

  1. Required arguments or Positional arguments.
  2. Keyword arguments
  3. Default arguments
  4. Variable Length arguments
3

Function Concepts in Python part 1

By // No comments:

What are functions:-

  • Python function is a block of code that performs a specific task.
  • Python function allows us to divide a large program into the basic building block.
  • Python function helps to break the program code  into smaller part.It is used to avoid the repetition of the code.
Types of functions

There are two types of the functions in python.

  1. Built-in Functions
  2. User defined functions
 1.) Built-in functions:- The Built-in functions are those functions which are predefined in python.I have already explained more built-in functions in our previous lectures.e.g. len(),min(),max(),range() ,print() etc.
2.) User defined functions:-The user defined functions are those functions which are defined by user for any specific tasks in python.e.g. show(),display(),calculate() etc.
Advantage of functions:-
There are two main advantage of functions as given below:-
  • Functions help us to divide our program into multiple tasks.For each task we can define a separate functions.This makes the code modular.
  • Functions provide us a reuse mechanism.
Properties of functions
  • The body of the function must be indented suitably.
  • A function can be called any number of times.
fun1()               #first function call
fun2()               #second function call
fun3()               #third function call
  • When a function is called ,control is transferred to the function and its statements are executed and control is returned to place from where the call originated.
  • Built-in Function Name always use lowercase characters.
  • User-defined function Name can be used lower or uppercase characters
  • Function names connect multiple words using  hyphen sign (-).

Creating a python Function:-

Python  provides the def keyword to define the function.

Syntax:-

def fun (parameter):
    Block of codes
    return

Descriptions:-

  • The def keyword is used to define the function in python.
  • The identifier rules must be follow to write the name of the function in python.
  • A function accepts the parameters(arguments) according to the requirements of the function.The parameters or arguments can be optional.
  • The function block is started with colon : and block of codes must be at the same indentation.
  • The function may have a return statement.It is used to return the value to the calling function.
  • A function can have only one return statement in python.It can be optional.
Arguments in Function:-
A arguments are the type of information which can be passed into the function.This comes under small brackets and separate with a comma.
e.g.
#define a function
def sum (a,b):
    result=a+b
    print ("The sum of two numbers is:",result)
#callig  the function
sum(20,40) 
Output:-
The sum of two numbers is: 60
>>> 
Function Calling:

  • We first create function after that we can call it from another location.
  • A function must be defined first ,after that call it otherwise python interpreter gives an error.
e.g.
#define a function
def fun ():
    print("Welcome to MNT LAB")
#function calling    
fun()   
Output:-

Welcome to MNT LAB

>>> 

How to use return statement in function:-

The return statement is used at the end of the function and returns the result to the calling function.

Syntax:-

return [expression]

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    return result
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
print("Multiplication of three number is:",multiplication(x,y,z))

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

>>> 

Description:-

In above example,i have defined a multiplication function with three arguments.It has a statement result=a*b*c , which is used to compute the a,b and c  values and stored it in result variable.This result value is return by this function to the calling function.Here i have taken these three value by user as shown in above code.

Calling the Function Without Return statement:-

We can call the python functions without return statement as given below:-

e.g.

#define a function
def multiplication (a,b,c):
    result=a*b*c
    print("Multiplication of three number is:",result)
    print("Welcome to MNT LAB")
x=int(input("Enter first value: "))
y=int(input("Enter second value: "))
z=int(input("Enter third value: ")) 
#function calling
multiplication(x,y,z)

Output:-

Enter first value: 10

Enter second value: 20

Enter third value: 30

Multiplication of three number is: 6000

Welcome to MNT LAB

>>> 

Call by reference in Python

The Memory of Call by reference is to passing the actual value as an argument in the function.All the functions are called by reference,Means all the changes made to the reference inside the function revert back to the original value referred by the reference. 

e.g. 

Passing Mutable object(list1) in program

list1=[10,20,30,40]
def modify_list(list1):
    list1.append(50)
    list1.append(60)
    list1.append(70)
    print("The list inside the function is:",list1)
# calling the function
modify_list(list1)
print("The list outside the function is:",list1)

Output:-

The list inside the function is: [10, 20, 30, 40, 50, 60, 70]

The list outside the function is: [10, 20, 30, 40, 50, 60, 70]

>>>

e.g. 

Passing Immutable object(string) in program

string="Hello Friends "
#define the function
def modify_string(str1):
    str1=str1+"Welcome to MNT LAB"
    print("print the string inside function is: ",str1)
#calling the function
modify_string(string)    
print("print the function outside the function is ",string)

Output:-

print the string inside function is:  Hello Friends Welcome to MNT LAB

print the function outside the function is  Hello Friends 

>>> 

Watch Complete Lecture Video:-

For More...

  1. List concepts in python part 2
  2. How to save image in database and display in picture box.
  3. For oop concepts in python with examples
  4. WCF concepts in .net
  5. Algorithm concepts in python with examples

3

Dictionary concepts in python

By // No comments:

What is Dictionary:

  • Dictionary is a collection of key-value pairs.It is used to store the data in a key-value pairs.It is used to store the data in key-value format.
  • Dictionaries are indexed by keys.
  • Dictionary is the data type in python.
  • Dictionaries are mutable.So we can perform add/delete/modify operations on a dictionary.
  • The keys must be a single element in dictionary.
  • The values can be any type such as list,tuple,integer etc.
  • Dictionaries are also known as maps or associative arrays.
  • The keys in a dictionary must must be unique and immutable.So number,strings and tuples can be used as keys.
  • Though key values are unique,different keys may have same value.
3

Set Concepts in Python

By // No comments:
What are sets
  • A python set is the collection of unordered elements.Hence order of insertion is not same as the order of access. 
e.g.
set1=set() #create an empty set
set1={10,20,30,40,50}
print(set1)
Output:-
{40, 10, 50, 20, 30}
>>> 
  • A set does not contain duplicate items like list and tuple.
e.g.
#set1 contains duplicate items
set1={10,20,20,10,50,60,60,70,80,30,50}
print(set1)
Output:-
{70, 10, 80, 50, 20, 60, 30}
>>> 
  • A set is mutable (changeable),Means we can change the items after created but set items must be immutable as shown in below examples.
3
Powered by Blogger.