Python Input Output
Category Archives: Python
Python Exception Handling
Python Exception Handling
Python Module
Python Module
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:-
|
1 2 3 |
def <function_name>(<parameter_list>): """docstring""" statement(s) |
Example:-
|
1 2 3 4 5 |
def add(x, y): """Return x plus y""" return x + y print add(4,5) |
Output:-
|
1 |
9 |
Function with Optional Parameters
Example:-
|
1 2 3 4 5 6 |
def add(x, y=5): """Return x plus y, optional""" return x + y print add(3) print add(4,2) |
Output:-
|
1 2 |
8 6 |
Python Constructor
Python Constructor
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:-
|
1 2 3 4 5 |
class BaseClass # Body of base class class DerivedClass(BaseClass): #Body of derived class |
Python Multiple Inheritance
|
1 2 3 4 5 6 7 8 |
class BaseClass1 # Body of base class class BaseClass2 # Body of base class class DerivedClass(BaseClass1,BaseClass1): #Body of derived class |
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:-
|
1 2 3 4 5 6 |
class ClassName: <statement-1> . . . <statement-N> |
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:-
|
1 2 |
s1 = {5,2,3,1,4} print("s1 = ", s1) |
Output:-
|
1 |
s1 = {1, 2, 3, 4, 5} |
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:-
|
1 |
{'Day1': 'Mon', 'Day2': 'Tue', 'Day3': 'Wed', 'Day4': 'Thu'} |
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:-
|
1 2 |
t1 = ('sun','mon', 'tue') print t1 |
Output:-
|
1 |
('sun', 'mon', 'tue') |
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:-
|
1 2 3 4 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1+clr2 print(clr3) |
Output:-
|
1 |
['red', 'green', 'blue', 'white', 'yellow'] |
Python List Repetition
In Python, * operator can also be used to combine two lists.
Example:-
|
1 2 3 4 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1*3 print(clr3) |
Output:-
|
1 |
['red', 'green', 'red', 'green', 'red', 'green'] |
Python List Membership
In Python, in keyword can be used to check the existence of an item in a given list.
Example:-
|
1 2 3 4 5 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1+clr2 res='red' in clr3 print res |
Output:-
|
1 |
True |
Python List Iteration
In Python, for-in statement makes it easy to loop over the items in a given list.
Example:-
|
1 2 3 |
colors= ['red', 'green', 'blue', 'white', 'yellow'] # list of 5 strings. for clr in colors: print "I like",clr |
Output:-
|
1 2 3 4 5 |
I like red I like green I like blue I like white I like yellow |
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. |
