Posts

Showing posts from March, 2018

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,...