Posts

Python List

List      List is data structure in python more like array in c or java. But in python list is mutable.      List is initialized in python using list class or just []. list1 = [] list2 = list() Below are some methods of list object list.append(item)      Add an Item to end of the list. list.count(item)      Returns number of time item appears in the list. list.extend(anotherList)      Extend the list by appending all the items from the given list. list.index(item, [start], [end])      Returns the first index of value. If specified optional arguments start or end, the item is searched within start and end. Raises ValueError if item not present. list.insert(index, item)      Insert an item at a given position, the element is inserted before the index. list.pop([index])      Remove an item from the ...

Defining Funtions And The range() Function

Defining Functions      We can define function by using def keyword, the function definition written by using def keyword with function name followed by parameters enclosed by simple parenthesis or can be blank. The next statement is at next line with indent. It can have docstring. Example:      Lets write function for checking number is prime or not. >>> def check_prime(num): ...  is_prime = True ...  for index in range(2, num): ...  if num % index == 0: ...  is_prime = False ...  break ...  index += 1 ...  if is_prime: ...  print "Given Number is Prime" ...  else: ...  print "Given Number is Not Prime" ... >>> check_prime(9) Given Number is Prime >>> check_prime(8) Given Number is Not Prime      Function parameters can have Default Argument, Variable Number ...

break, continue, pass Statements

break Statement     break can used to break the loops like for, while Example >>> for i in range(10): ...        if i%2 == 0: ...          break ...  continue Statement     continue can be used take control to starting of loop Example >>> for i in range(10): ...        if i%2 != 0: ...          continue ... pass Statement     pass can be used when statement require but without action, like we base class that provides base for inheriting class Example >>> def foo(): ...        pass

Loops From Python

     In Python, There are loops like for and while, Let's See them in detail. for Loop      The for statement from python iterates over any sequence it may be string or list. Syntax: for iterator in sequence: statement1 statement2 ... Example: >>> a = 3 >>> for char in 'python': ...  print char ...  p y t h o n while Loop      The while statement from python is used to check condition and then iterate if condition evaluates True. Syntax: while condition: statement1 statement2 ... Example:      Let's we will write function that calculates Fibonacci series. >>>def fibo(limit): ...  a, b = 0, 1 ...  while a < limit: ...  print a, ...  a, b = b, a + b ... >>>fibo(10) 0 1 1 2 3 5 8      Get in touch for more fun,...

Control Flows From Python

     As Other Languages Python also has control flow Statements. They are as follows:- if Statement      It used to check condition according to it control program flow. Syntax: if condition: statement1 statement2 ... if..else Statement      It is used control flow according to condition, If condition evaluates to True, It will execute if block, else block otherwise. Syntax: if condition: statement1 statement2 ... else: statement1 statement2 ... Example: >>> a = 3 >>> b = 9 >>> if b > a: ...  print "B is Max" ... else: ...  print "A is Max" ...  B is Max if..elif..else Statement      Python Doesn't supports switch...case like C or Java, it is replaced with if - elif - else. It first checks if condition if it evaluates true it execu...

Introduction To Python

     Python is simple, easy to use, interpreted programming language, offering much more structure and support for large programs than shell scripts or batch files can offer. Being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general data types Python is applicable to a much larger problem domain.      As Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs or as examples to start learning to program in Python. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk. Pytho...

Getting Started With Python

     Python is an easy to learn, powerful, general purpose programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.     The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/ and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.     The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension ...