Posts

Showing posts from March, 2022

Python - Functions

Image
 Functions Function in python it's same as the functions in any other programming language. Concept is same the syntax is differ. As we all know function is a block of code that perform some task. There are 3 types of function in python. In-built function - for e.g:- min(),  print(), add(). User defined function - That will be defined or created by the user. Anonymous function - is also known as lambda function because they are not declared with the def keyword. How to define a function in python use def  statement. followed by  function name . Add parameter to the function. All the parameter should enclosed with the parenthesis or  bracket and at the end of the function used 'colon' : When to use a function. Function should use when there is duplication of a task. It increase readability of the program. Return Statement  A return statement is used to return something from the function. It's only work for the function. If any other want to work wit...

Python - Dictionary

  Python Built-in Data Structure -- Dictionary 1.  Dictionary items are ordered, changeable, and does not   allow duplicates. 2.   Dictionary items are presented in key:value pairs, and   can be referred to by using the key name. 3.   Dictionary items are mutable. 4.   Dictionary are iterable. Dictionary Methods It has total of 11 methods.  Clear () – It’s used to remove all the items from the dictionary. Copy () – It’s used to return the copies of the given dictionary. Keys () - The keys () method returns a view object. The view object contains the keys of the dictionary, as a list. The view object will reflect any changes done to the dictionary. Values () - The values () method returns a view object. The view object contains the values of the dictionary, as a list. The view object will reflect any changes done to the dictionary. Items () – The items () method returns a view object. The view...

Python - Set

Image
 Built-in Data Structure -- Set Introduction to Set We all are studied Set in our class 10th. The same concept is applied here. All the set operations you will get in this. Stay with me. You will know the Set and able to teach anyone about it. So, very first question is What is Set? Let's answer this question later just recall what you know about Set. Think for minute or 5 ......... Tick, Tick, Tick. Have you recalled it. I guess so. If not leave on me. Kidding. Pay attention here guys.  Set is an unique collection of item. Set has no duplicate elements. It's an unordered data structure. Since sets are unordered, we cannot access items using indexes like we do in a list and tuple. Slicing is not possible. It is Iterable. It is Mutable. Creation of Set Set is created using { } curly bracket or set constructor . To create an empty set , use set function . As shown in image. Important Set functions. As, we all not basic set operations is like, Union, Intersection, Discard, Diff...

Python - Interview Questions basics.

Image
Interview Questions. Difference between  Iteration, Iterable and Iterator? Iteration - Repetition of a process. Iterable -  A python object which supports iteration e.g list, tuple, string, range, set. Iterator -  A python object which enable us to iterate over the iterable.  Difference between tuple and list? List is mutable. Tuple is immutable. List consume more memory and slow. Tuple consume less memory and fast. When you are returning something from the function the returned value is tuple.  Difference between map, filter and reduce with example? Map -  The map() function is a higher-order function. As previously stated, this function accepts another function and a sequence of ‘iterables’ as parameters and provides output after applying the function to each iterable in the sequence. It has the following syntax: SYNTAX:   map(function, iterables) map() function returns  a map object(which is an iterator) of the results after applying the g...

Python - Tuples

Image
 Introduction to Tuple. Just like list, tuple is also used to store sequence of element. Tuple elements can be of any data types. Tuple are immutable. Tuple are iterable. Tuple are ordered built-in data types and unchangeable.  Tuple is created using the round brackets i.e, () or tuple() constructor. Tuple allows duplicate elements. Tuple supports indexing. Tuple supports slicing. Concatenation and Replication is possible. Tuple are Unchangeable. It's means once we have created a tuple using round bracket or  tuple constructor. We can't change , add, or remove item of tuple. If we try to do that, it's gives error that Tuple object does not support item assignment. Tuple Methods. Built-In Function Len() - Returns the total number of elements in a tuple. Sum() - Returns the sum of all the elements in a tuple. Min() - Returns the minimum elements in a tuple. Max() - Returns the maximum elements in a tuple. Function used to count the number of time specif...

Python - Advanced list.

Image
  Python - Advanced  list. LIST COMPREHENSIONS It is used to create a new list from the existing list. It consists of a bracket followed by a for loop than if (optional). It will make your code more pythongenic or optimal. Syntax of list comprehensions. [ expression for loop  than if condition] In layman language [ return statement for loop followed by if condition (optional)] Simple example :-  Now, writing this piece of code using list comprehension. It's  easy guys stay on this page. If you  are not able to understand just stay you will get it. Using list Comprehensions The return one will come  first. After that for loop will come. You don't have to give colon ":"   symbol at the last. If there is an if condition between return and for loop it will come after the for loop in list comprehension. See the syntax you will get it. Steps to create list comprehensions. Now, we know it's consist of bracket . put the bracket first - [] After brac...

Python - Basics of Python list.

Image
  Python - Introduction to list. List  is a sequence of elements or values. In an ordered way. The element can be of any type like integer, float, string.  We can declare a list in python using square brackets [] or by using list() constructor. List is mutable - means we can change or modify the elements. List allows duplicate elements. List are Heterogeneous.  Questions asked in interview. 1- How to create an empty list? Ans - list_object = list() 2 - Create a list with integer elements? Ans :- list_object = list([1, 2, 3]) 3 - Create a list using range function() ? Ans :- list_object = list(range(3)) 4- Why list is mutable object? Ans - After defining or creating a list. We can accessed or change the element of a list in place.   Note : - list() can take Iterable as an argument for e.g :-  range, string, set, tuple, dictionary.  In, the above question we create a list using constructor of a list class. What if interviewer asked to create list witho...