How to remove duplicates from list in python 3

How to remove duplicates from list in python 3 :

Hello friends, In this article, we will see How to remove duplicates items or elements from the list in python so Let’s start :
How to remove duplicates from list in python 3
How to remove duplicates from list in python 3
 
There are different way to remove duplicates from list :
  1. Method 1 : Using list comprehension method
  2. Method 2 : Using list comprehension + enumerate() method
  3. Method 3 : Using set() method
  4. Method 4 : Naive method
  5. Method 5 : Using collections.OrderedDict.fromkeys() method

Method 1 : Using list comprehension method

This method has working similar to the Naive method , but small difference is it just a liner shorthand of longer methods done with the help of lists comprehension method.

For example :

# Demonstrate Python 3 code 
# remove duplicated from list using list comprehension
#  first we initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# then we use using list comprehension method  
# to remove from list  
rest = [] 
[rest.append(x) for x in use_list if x not in rest] 
  
# printing list output after removal  
print (“The unique list after removing dupli is follow: ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow : [4, 3, 7, 6, 5, 1]
This time to learn different python terminologies in python.

Method 2 : Using list comprehension + enumerate()

list comprehension coupled with enumerate function can also achieve this task. It basically looks for already occurred elements and skips adding them. It preserves the list ordering.
# removing duplicated from list using list comprehension + enumerate() 
# first initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# using list comprehension + enumerate() method to remove duplicated  
# from  the list  
rest = [i for n, i in enumerate(use_list) if i not in test_list[:n]]  
print (“TThe unique list after removing dupli is follow : ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

Method 3 : Naive method

In this method (naive method), we simply append the first occurrence of the element in new existing list and ignore all the other occurrences of the list.
# Demonstrate Python program 
# remove duplicated element from list using list Naive method
#  first we initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# then we use using list Naive method  
rest = [] 
for i in test_list: 
    if i not in rest: 
        rest.append(i) 
  
# printing output after removal  
print (“The unique list after removing dupli is follow: ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

Read more

Modulo Operator python-modulus in python

Modulo Operator python-modulus in python

Welcome everyone, Today we will see Modulo Operator python,Modulo Operator python for negative number,How does a modulo operator work?,What is the use of modulo operator?,How do you find modulo?,What is the symbol of modulus operator?
so let’s start:

Modulo Operator python
Modulo Operator python

Modulo operator is show by the percent sign (%).
The syntax for Modulo Operator python is :


numx % numy


output:


For example:


11 % 2


output:


1

If any case the divisor result is equal to zero then output show ZeroDivisionError:


11 % 0


output:


ZeroDivisionError: modulo by zero.

The modulo operator also work on floating numbers as arguments:
for example:


4.8 % 1.2


Copy


0.0

Remember % character represents the interpolation operator.(formatting strings)
Examples
modulo operator is used for different purpose like One of the most common use is to check whether a number is even or odd.
If a given number divided by 2 and has no remainder, then this number is even number. Otherwise it is odd.
EXAMPLE 01) Check for even or odd number:


numx = 15


if (numx % 5) == 0:


   print(num, "is even no.")


else:


   print(num, "is odd no.")


output:


15 is even no.

If you run the code above, 15 % 5 leaves a remainder of 0 and the code inside the else statement is executed:
15 is even no.
EXAMPLE 02) Check for prime number:


def is PrimeNumber(numb):


  if numb < 1:


    return False


  for i in range(2, numb):


    if (numb % i) == 0:


      return False


  else:


    return True

In this example first, we have to checking if the number, num is a positive number or negative(it’s required positive number). Next step is checking whether the number is divisible by another number in the range from 2 to number without any reminder. If none of the conditions are satisfy, the result is prime number.

Modulo Operator python for negative number:

Most complex mathematics task is taking modulo of a negative number, which is done behind the program of Python.
(x+y)mod z = [(x mod z)+(y mod z)]mod z
To apply this math to given statement as –


-3 % 7 = ( -1*7 + 4 ) % 7 = 4 

This was done so that the (-1*7)%7 will give the answer as 4
Let’s see more examples for better understanding.
Example #1 :
By using this mathematics, we can see able to perform and understand the negative modulo.


11 % 0


output:


ZeroDivisionError: modulo by zero.

filter_none
edit
play_arrow
brightness_4
# Using negative modulo in python


res1 = -35% 5


res2 = - 17 % 5


print(result1) 


print(result2)


Output :


0


2

Read more

what are python identifiers with example

what are python identifiers with example : 

An identifier is a simple concept which is used to identify entities. ( i.e. class, variables,  functions,module or other object ) Identifier used to differentiate one entity from another entityAn identifier starts with an underscore (_), letter A to Z or small letter a to z or  simply follow by zero or more letter and digits (0 to 9).

Python identifier simple example :

what are python identifiers
what are python identifiers

In the following example we have to used three variables. 
The name of the variables
  •  rollno.,
  •  _sachin
  • and sachin_samprada are the identifiers.
# Basic examples on identifiers

rollno
= 1
print(rollno)

_sachin
= 5
print(_x)

sachin_samprada
= 143
print(sachin_samprada)

OUTPUT :

>> 1
>> 5
>>  143

Rules to Create Python Identifiers :

There are different no. of rules that must need to be followed to create a python identifier.

  • An identifier starts with an underscore (_), letter A to Z or small letter a to z or  simply follow by zero or more letter and digits (0 to 9)
  • sachin_SAMPRADA : contains all the valid character
  • _: underscore is also valid identifier
  • _sachin: it is valid to identifier can start with an underscore symbol 

Some python Invalid Identifiers Example :

  • 143: identifier cannot be only digit
  • 143samprada: identifier cannot start with number
  • sachin+samprada: the only special character is valid an underscore
  • from: it is a reserved keyword

How to check if a String is a Valid Identifier in python? :

  • Identifier name cannot begin with a digit or number.
  • Python identifier cannot contain only digits or number.
  •  Identifier name can start with an underscore is valid.
  • There is no any limit on the length of the identifier.
  • Identifier name are case sensitive in nature.
  • sachin143samprada: Identifier contain only letters and numbers

We can use python string identifier() Functions to check identifier name is valid or not. But, this method doesn’t take reserved keywords into consideration. So, we can use this function with keyword.iskeyword() to check if the name is valid or not.
print(“_”.fallinlove()) # True
print(“for”.inlove()) # True – wrong output(error)
print(“sahin”.inlove())  # True
print(“143sach”.isidentifier())  # False

list of the Python keywords :

...
help> keywords

Here is a list of the Python keywords. Enter any keyword to get more help.

False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass

Read more

comparison python vs r vs sas vs spss

python  vs r vs sas vs spss :

Welcome everyone, Today we will see comparison between  r vs python vs sas vs spss . So Let’s start :
what is Python ?
python  vs r vs sas vs spss
  python  vs r vs sas vs spss

1. python is best languages when learning to code :
If you are beginner to learn coding then mostly Python is your language of choice because It is one of the easy programming language to learn (it read and write like to plain English)

2. Python is heavily used in the IOT sector :
Most popular company use python algorithms platform for there services.

Like
  • Google
  • Youtube
  • Quora 
  • amazon etc. 
Advantages :
Powerful, fully-functional programming language
  1.  Latest availability of library Data science Learning and Machine Learning method
  2. Very easy to automate through scripts
  3. Extremely smart community support 
  4. Visualizations algorithms are easy to create
  5. Professional IDE environments are available
  6. object-oriented, functional and structured concepts
  7. A large number of stable package available
  8. Readable and clean syntax 
Disadvantages :
  1. Due to being a “full” programming language high bar of entry
  2. More licensing cost
  3. Only few statistical method are available
There are no user fees for the use of Python. However, in some special areas (e.g. text mining) not all packages are released for commercial use.
Conclusion
For Learning Python language we requires to learn a complete programming language
 Many good tutorials and trainings are available on the python tutorials point due to the language’s popularity. 

What is R?

R is a so popular and open-source language, it was developed in 1995 by Ross Robert Gentleman and Ihaka. R is used by statisticians and data miners for performing statistical computations and modeling hence it is most widely used. it is commonly used with RStudio
Advantages :
  1.  It is provide Very well community support, as also fee-based support via third-party provider.
  2. Easy free help resources also available like tutorials, exercise/solution.
  3. R is Very powerful and flexible  language e.g. support oop and Windows, Linux, MacOS
  4. R include very large range of functions 
  5. New statistical function are quickly implemented
  6. R is easy to automate and integrate 
Disadvantages :
  1. when we working with very large data sets then it’s required powerful hardware
  2. very costly Licensing mode
  3. R stores it object files in a physical memory so it’s produce problem.
Conclusion :
 R is a basically vey good choice for different user that plan to deal more extensively with statistics and not restricted by their statistical program.

SAS (Statistical Analysis System) :

SAS is one of the best Institute they offers a professional statistics software.
This software commonly used in banking sector,biometrics and in the clinical research.SAS is popular for data management, multivariate analysis,predictive analytics and business intelligence etc.
Advantage :
  1. In SAS numerous module and interface are available for free.
  2. It’s ability to handling large data sets
  3. very stable and reliable routines and fast integration of new statistical methods
  4. It’s provide very professional support and good documentation
  5. SAS offers good security to its users.
  6. It provides to load large volume of data for scalable and stable software that allow the companies
Disadvantage :
  1. SAS is a not open source software (i.e.you have to buy a licence for using it)
  2. SAS not contained most features in graphical visualisations. 
  3. but mostly some of the features in SAS are very limited. 
  4. It’s supported different, partly complicated program languages
  5. costly licensing model
Conclusion :
SAS offers good security to its users.It provides to load large volume of data for scalable and stable software that allow the companies

Read more

Event handling in python tutorials point

Event handling in python tutorials point

Event handlers is used for change device configuration,restart crashed service.  Event handlers are like as application which is also based on the results of service checks in Monitors. In this article we will see in detailed event handling in python tutorials point .

What is event handler in Python tutorials point?

Event handling class in python, which manage callbacks sender to be executed. It is used as attribute in events-aware class object in python but it does not kept as separate instance.
Event handling decides what should exactly happen if an any one event occur.This process have the code which is called as event handler. you know that Listener python is responsible for generating response to the event during this mechanism hence it is also known as event handler.

Most important function in event handling :

Event handling in python tutorials point
Event handling in python tutorials point
  1. callbacks
  2. sender
  3. add(callback : Callable)
  4. remove(callback : Callable)
  5. __call__(*args) → [ … ]
For example :
>>> def myfunct(sender):
…     print(“Show the event triggered by sender)
>>> class MyClass(object):
…     def __init__(self):
…         Handlerns = EventHandler(self)
>>> myobj = MyClass()
>>> myobj.anevent += myfunct
>>> myobj.anevent()
event triggered by the <__main__.MyClass object at 0x805457e50>
#1 callbacks
A callbacks function is currently bound to the EventHandlers.
#2 sender
The function is responsible object that executes the EventHandlers.
#3 add(callback : Callable)
add function adds a callback to the EventHandlers.
#4 remove(callback : Callable)
Remove function removes a callback from the EventHandlers.
#5 call__(*args) → [ ]
Executes all connect callback in the order of passing the sender of the EventHandler 

Python range() function in python-range of int

Python range() function Hello everyone, Today we will learn with the help of different example how to use Python’s range() functions. range() function is in built function in python.I t is generatesd the integer number between the  start to the stop integers means it return the object. It is used when a users also need to perform an action for a  specific … Read more

python read from stdin-write to file

python read from stdin : Welcome to python tutorials point, In this post we are see in detailed How to python read from stdin.  Before going to this post if you want to learn python tutorials for free then click on python tutorials point. so Let’s start: There are three methods for python read from … Read more