Category Archives: Python

Python Functions

Python Functions

In Python, functions are one of the important part of any python program. Python function gives you way to wrap up the set of statements that means to perform any specific task and give it a name, so that it can be invoked later from any where in the program.

Functions makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program.

We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.

Advantages of function

  • It enhance the modularity of the program.
  • It enhance the re usability.
  • It makes development easy as development can be shared in team.
  • It reduces the coupling.
  • It reduces duplication.

Type Of Function

  • Built in function
  • User defined functions

Built In Function In Python

Built in functions are the functions available in Python Programming Language to perform some common and standard tasks that includes the functions for file access, mathematical computations, graphics, memory management etc.

User defined functions In Python

User defined function are custom function defined by user it self to perform as custom task that is not available as built in, in this way user can define and write subprograms as functions to perform a task relevant to their programs.

Defining Function In Python

Syntax:-

Example:-

Output:-

Function with Optional Parameters

Example:-

Output:-

 

Python Inheritance

Python Inheritance

The inheritance is one of the important feature in object oriented programming, that gives you a way to reuse of code. Inheritance is a process through which a classes can inherit properties and methods from other classes, know as super-classes. A class that inherits from super-classes is know as Sub-class or Derived-Class.

Python Single Inheritance

Syntax:-

Python Multiple Inheritance

 

Python Classes and Objects

Python Classes

Class can be defined as a blueprint or prototype of associated objects. Class wrapper that binds the data and methods which can be later accessed by objects of that class.

Defining a Class in Python

Syntax:-

 

Python Set

Python Set

In Python, set is an unordered collection of unique elements or items. A Set is defined by comma-separated values inside braces { }.

Example:-

Output:-

Python Dictionary

Python Dictionary

In Python, Dictionary is an unordered collection of key-value pairs. Dictionary is similar to associative arrays or hashes consist of key-value pairs. Dictionary is defined by using curly braces ({ }) and values can be assigned and accessed using square braces ([]).

Example:-

Python Tuple

Python Tuple

In Python, Tuple is an ordered sequence of comma-separated values (items or elements). Tuples are same as list, but the only difference is that tuples are immutable. Once a tuple is created their elements and size can not be changed. A Tuple is defined within parentheses () where items are separated by commas.

Example:-

Output:-

 

Python List Operations

Python List Operations

In Python, along with the operations like indexing, slicing and accessing, following operations can also be applied on Python list sequences.

Python List Concatenation

In Python, plus(+) operator can also be used to combine two lists.

Example:-

Output:-

Python List Repetition

In Python, * operator can also be used to combine two lists.

Example:-

Output:-

Python List Membership

In Python, in keyword can be used to check the existence of an item in a given list.

Example:-

Output:-

Python List Iteration

In Python, for-in statement makes it easy to loop over the items in a given list.

Example:-

Output:-

Python List Methods

Python Built In List Methods

Methods Description
index(object) It returns the index of the object.
count(object) It returns the number of times an object is exists in list.
pop()/pop(index) It returns the last object or the specified indexed object. It removes the popped object.
insert(index,object) It add an item at the given index.
extend(sequence) It adds the sequence to list.
remove(object) It removes the item from the given List.
reverse() It reverse the list items.
sort() It is used to sort the list items.