You Should'nt Skip 35 Python Snippets - Python Language

You Should'nt Skip 35 Python Snippets - Python Language

In this post, I’ve presented Some tips and tricks to help you code faster. It is 35 short Python code snippets that can be used as a reference in your work.

Python as a high-level programming language, allows you to focus on the core functionality of the application by taking care of common programming tasks. The simple syntax rules of the programming language further make it easier for you to keep the code base readable and application maintainable. In this post, I’ve presented Some tips and tricks to help you code faster. It is 35 short Python code snippets that can be used as a reference in your work.

1. Digitize

The following snippet will convert an integer into a list of digits.

num = 123456

# using map
list_of_digits = list(map(int, str(num)))

print(list_of_digits)
# [1, 2, 3, 4, 5, 6]

# using list comprehension
list_of_digits = [int(x) for x in str(num)]

print(list_of_digits)
# [1, 2, 3, 4, 5, 6]
2. Check if a string is a palindrome

The following function is used for checking if a string is a palindrome or not.

def palindrome(string):
    return string == string[::-1]
		
palindrome('python') # False

Check If a Given String Is a Palindrome or Not

We have already discussed how to reverse a string. So palindromes become a straightforward program in Python.

my_string = "abcba"

if my_string == my_string[::-1]:
    print("palindrome")
else:
    print("not palindrome")

# Output
# palindrome
3. Swap values between two variables

In other languages, to swap values between two variables without using a third variable, we either have to use arithmetic operators or bitwise XOR. In Python, it is much simpler, as shown below.

a = 5                               
b = 10                                                                
a, b = b, a                                                                
print(a) # 10                               
print(b) # 5

Python makes it quite simple to swap values between two variables without using another variable.

a = 1
b = 2

a, b = b, a

print(a) # 2
print(b) # 1
4. Check if the given number is even

The following function returns True if the given number is even, False otherwise.

def is_even(num):
  return num % 2 == 0
	
is_even(10) # True
5. Split a String Into a List of Substrings

The following function can be used for splitting a multiline string into a list of lines.

def split_lines(s):
  return s.split('\n')
split_lines('50\n python\n snippets') # ['50', ' python', ' snippets']

We can split a string into a list of substrings using the .split() method in the string class. You can also pass as an argument the separator on which you wish to split.

string_1 = "My name is Chaitanya Baweja"
string_2 = "sample/ string 2"

# default separator ' '
print(string_1.split())
# ['My', 'name', 'is', 'Chaitanya', 'Baweja']

# defining separator as '/'
print(string_2.split('/'))
# ['sample', ' string 2']
6. Find memory used by an object

The standard library’s sys module provides the getsizeof() function. That function accepts an object, calls the object’s sizeof() method, and returns the result, so you can make your objects inspectable.

import sys
print(sys.getsizeof(5)) # 28
print(sys.getsizeof("Python")) # 55
7. Reverse a string

Python string library doesn’t support the built-in reverse() as done by other Python containers like list. There are many approaches to reversing a string, of which the easiest way is making use of the slicing operator.

language = "python"                                
reversed_language = language[::-1]                                                                 
print(reversed_language) # nohtyp

The following snippet reverses a string using the Python slicing operation.

# Reversing a string using slicing

my_string = "ABCDE"
reversed_string = my_string[::-1]

print(reversed_string)

# Output
# EDCBA
8. Print a string n times

It is very easy to print a string n times without using loops, as shown below.

def repeat(string, n):
  return (string * n)
	
repeat('python', 3) # pythonpythonpython
n = 3 # number of repetitions

my_string = "abcd"
my_list = [1,2,3]

print(my_string*n)
# abcdabcdabcd

print(my_list*n)
# [1,2,3,1,2,3,1,2,3]
9. Combining a List of Strings Into a Single String

The next snippet combines a list of strings into a single string.

strings = ['50', 'python', 'snippets']
print(','.join(strings)) # 50,python,snippets

The join() method combines a list of strings passed as an argument into a single string. In our case, we separate them using the comma separator.

list_of_strings = ['My', 'name', 'is', 'Chaitanya', 'Baweja']

# Using join with the comma separator
print(','.join(list_of_strings))

# Output
# My,name,is,Chaitanya,Baweja
10. Find the first element of a list

This function returns the first element of the passed list.

def head(list):
  return list[0]
	
print(head([1, 2, 3, 4, 5])) # 1

We also use the most_common() function to get the most_frequent element in the list.

# finding frequency of each element in a list
from collections import Counter

my_list = ['a','a','b','b','b','c','d','d','d','d','d']
count = Counter(my_list) # defining a counter object

print(count) # Of all elements
# Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})

print(count['b']) # of individual element
# 3

print(count.most_common(1)) # most frequent element
# [('d', 5)]
11. Find elements that exist in either of the two lists

This function returns every element that exists in either of the two lists.

def union(a,b):
  return list(set(a + b))
	
union([1, 2, 3, 4, 5], [6, 2, 8, 1, 4]) # [1,2,3,4,5,6,8]
12. Find all the unique elements present in a given list

This function returns the unique elements present in a given list.

def unique_elements(numbers):
  return list(set(numbers))
	
unique_elements([1, 2, 3, 2, 4]) # [1, 2, 3, 4]
13. Find the average of a list of numbers

This function returns the average of two or more numbers present in a list.

def average(*args):
  return sum(args, 0.0) / len(args)
	
average(5, 8, 2) # 5.0
14. Check if a list contains all unique values

This function checks if all the elements in a list are unique or not.

def unique(list):
    if len(list)==len(set(list)):
        print("All elements are unique")
    else:
        print("List has duplicates")
				
unique([1,2,3,4,5]) # All elements are unique
15. Track frequency of elements in a list

Python counter keeps track of the frequency of each element in the container. Counter() returns a dictionary with elements as keys and frequency of its occurrence as its values.

from collections import Counter
list = [1, 2, 3, 2, 4, 3, 2, 3]
count = Counter(list)
print(count) # {2: 3, 3: 3, 1: 1, 4: 1}
16. Find the most frequent element in a list

This function returns the most frequent element that appears in a list.

def most_frequent(list):
    return max(set(list), key = list.count)
		
numbers = [1, 2, 3, 2, 4, 3, 1, 3]
most_frequent(numbers) # 3
17. Convert an angle from degrees to radians

The next function is used for converting an angle from degrees to radians.

import math
def degrees_to_radians(deg):
  return (deg * math.pi) / 180.0
	
degrees_to_radians(90) # 1.5707963267948966
18. Calculate time taken to execute a piece of code

The following snippet is used for calculating the time taken to execute a piece of code.

import time
start_time = time.time()
a,b = 5,10
c = a+b
end_time = time.time()
time_taken = (end_time- start_time)*(10**6)
print("Time taken in micro_seconds:", time_taken) # Time taken in micro_seconds: 39.577484130859375
19. Find gcd of a list of numbers

This function calculates the greatest common divisor of a list of numbers.

from functools import reduce
import math
def gcd(numbers):
  return reduce(math.gcd, numbers)
gcd([24,108,90]) # 6
20. Find unique characters in a string

This snippet can be used to find all the unique characters present in a string.

my_string = "aavvccccddddeee"

# converting the string to a set
temp_set = set(my_string)

# stitching set into a string using join
new_string = ''.join(temp_set)

print(new_string)
21. Use lambda functions

Lambda is an anonymous function with the capability of holding a single expression only.

x = lambda a, b, c : a + b + c
print(x(5, 10, 20)) # 35
22. Use map functions

This function returns a list of the results after applying the given function to each item of a given iterable(list, tuple, etc.)

def multiply(n): 
    return n * n 
  
list = (1, 2, 3) 
result = map(multiply, list) 
print(list(result)) # {1, 4, 9}
23. Use filter functions

This function filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

arr = [1, 2, 3, 4, 5]
arr = list(filter(lambda x : x%2 == 0, arr))
print (arr) # [2, 4]
24. Use list comprehensions

List comprehensions provide us with a simple way to create a list based on some iterable. During the creation, elements from the iterable can be conditionally included in the new list and transformed as needed.

numbers = [1, 2, 3]
squares = [number**2 for number in numbers]
print(squares) # [1, 4, 9]

The following snippet creates a new list by multiplying each element of the old list by two.

# Multiplying each element in a list by 2

original_list = [1,2,3,4]

new_list = [2*x for x in original_list]

print(new_list)
# [2,4,6,8]
25. Use slicing operator

Slicing is used to extract a continuous sequence or subsequence of elements from a given sequence. The following function is used for concatenating the results of two slicing operations. First, we are slicing the list from index d to end, then from start to index d.

def rotate(arr, d):
  return arr[d:] + arr[:d]
  
if __name__ == '__main__':
  arr = [1, 2, 3, 4, 5]
  arr = rotate(arr, 2)
  print (arr) # [3, 4, 5, 1, 2]
26. Use chained function call

The final snippet is used to call multiple functions from a single line and evaluate the result.

def add(a, b):
    return a + b
def subtract(a, b):   
    return a - b
a, b = 5, 10
print((subtract if a > b else add)(a, b)) # 15
27. Using rhe Title Case (First Letter Caps)

The following snippet can be used to convert a string to title case. This is done using the title() method of the string class.

my_string = "my name is chaitanya baweja"

# using the title() function of string class
new_string = my_string.title()

print(new_string)

# Output
# My Name Is Chaitanya Baweja
28. Using the try-except-else Block

Error handling in Python can be done easily using the try/except block. Adding an else statement to this block might be useful. It’s run when there is no exception raised in the try block.

If you need to run something irrespective of exception, use finally.

a, b = 1,0

try:
    print(a/b)
    # exception raised when b is 0
except ZeroDivisionError:
    print("division by zero")
else:
    print("no exceptions raised")
finally:
    print("Run this always")
29. Using Enumerate to Get Index/Value Pairs

The following script uses enumerate to iterate through values in a list along with their indices.

my_list = ['a', 'b', 'c', 'd', 'e']

for index, value in enumerate(my_list):
    print('{0}: {1}'.format(index, value))

# 0: a
# 1: b
# 2: c
# 3: d
# 4: e
30. Check the Memory Usage of an Object

The following script can be used to check the memory usage of an object.

import sys

num = 21

print(sys.getsizeof(num))

# In Python 2, 24
# In Python 3, 28
31. Merging Two Dictionaries

While in Python 2, we used the update() method to merge two dictionaries; Python 3.5 made the process even simpler.

In the script given below, two dictionaries are merged. Values from the second dictionary are used in case of intersections.

dict_1 = {'apple': 9, 'banana': 6}
dict_2 = {'banana': 4, 'orange': 8}

combined_dict = {**dict_1, **dict_2}

print(combined_dict)
# Output
# {'apple': 9, 'banana': 4, 'orange': 8}
32. Time Taken to Execute a Piece of Code

The following snippet uses the time library to calculate the time taken to execute a piece of code.

import time

start_time = time.time()
# Code to check follows
a, b = 1,2
c = a+ b
# Code to check ends
end_time = time.time()
time_taken_in_micro = (end_time- start_time)*(10**6)

print(" Time taken in micro_seconds: {0} ms").format(time_taken_in_micro)
33. Flattening a List of Lists

Sometimes you’re not sure about the nesting depth of your list, and you simply want all the elements in a single flat list.

Here’s how you can get that:

from iteration_utilities import deepflatten

# if you only have one depth nested_list, use this
def flatten(l):
  return [item for sublist in l for item in sublist]

l = [[1,2,3],[3]]
print(flatten(l))
# [1, 2, 3, 3]

# if you don't know how deep the list is nested
l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]

print(list(deepflatten(l, depth=3)))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
34. Sampling From a List

The following snippet generates n number of random samples from a given list using the random library.

import random

my_list = ['a', 'b', 'c', 'd', 'e']
num_samples = 2

samples = random.sample(my_list,num_samples)
print(samples)
# [ 'a', 'e'] this will have any 2 random values

I have been recommended the secrets library for generating random samples for cryptography purposes. The following snippet will work only on Python 3.

import secrets                              # imports secure module.
secure_random = secrets.SystemRandom()      # creates a secure random object.

my_list = ['a','b','c','d','e']
num_samples = 2

samples = secure_random.sample(my_list, num_samples)

print(samples)
# [ 'e', 'd'] this will have any 2 random values
35. Check for Uniqueness

The following function will check if all elements in a list are unique or not.

def unique(l):
    if len(l)==len(set(l)):
        print("All elements are unique")
    else:
        print("List has duplicates")

unique([1,2,3,4])
# All elements are unique

unique([1,1,2,3])
# List has duplicates

I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

Python Programming for Data Science and Machine Learning

Python Programming for Data Science and Machine Learning

This article provides an overview of Python and its application to Data Science and Machine Learning and why it is important.

Originally published by Chris Kambala  at dzone.com

Python is a general-purpose, high-level, object-oriented, and easy to learn programming language. It was created by Guido van Rossum who is known as the godfather of Python.

Python is a popular programming language because of its simplicity, ease of use, open source licensing, and accessibility — the foundation of its renowned community, which provides great support and help in creating tons of packages, tutorials, and sample programs.

Python can be used to develop a wide variety of applications — ranging from Web, Desktop GUI based programs/applications to science and mathematics programs, and Machine learning and other big data computing systems.

Let’s explore the use of Python in Machine Learning, Data Science, and Data Engineering.

Machine Learning

Machine learning is a relatively new and evolving system development paradigm that has quickly become a mandatory requirement for companies and programmers to understand and use. See our previous article on Machine Learning for the background. Due to the complex, scientific computing nature of machine learning applications, Python is considered the most suitable programming language. This is because of its extensive and mature collection of mathematics and statistics libraries, extensibility, ease of use and wide adoption within the scientific community. As a result, Python has become the recommended programming language for machine learning systems development.

Data Science

Data science combines cutting edge computer and storage technologies with data representation and transformation algorithms and scientific methodology to develop solutions for a variety of complex data analysis problems encompassing raw and structured data in any format. A Data Scientist possesses knowledge of solutions to various classes of data-oriented problems and expertise in applying the necessary algorithms, statistics, and mathematic models, to create the required solutions. Python is recognized among the most effective and popular tools for solving data science related problems.

Data Engineering

Data Engineers build the foundations for Data Science and Machine Learning systems and solutions. Data Engineers are technology experts who start with the requirements identified by the data scientist. These requirements drive the development of data platforms that leverage complex data extraction, loading, and transformation to deliver structured datasets that allow the Data Scientist to focus on solving the business problem. Again, Python is an essential tool in the Data Engineer’s toolbox — one that is used every day to architect and operate the big data infrastructure that is leveraged by the data scientist.

Use Cases for Python, Data Science, and Machine Learning

Here are some example Data Science and Machine Learning applications that leverage Python.

  • Netflix uses data science to understand user viewing pattern and behavioral drivers. This, in turn, helps Netflix to understand user likes/dislikes and predict and suggest relevant items to view.
  • Amazon, Walmart, and Target are heavily using data science, data mining and machine learning to understand users preference and shopping behavior. This assists in both predicting demands to drive inventory management and to suggest relevant products to online users or via email marketing.
  • Spotify uses data science and machine learning to make music recommendations to its users.
  • Spam programs are making use of data science and machine learning algorithm(s) to detect and prevent spam emails.

This article provided an overview of Python and its application to Data Science and Machine Learning and why it is important.

Originally published by Chris Kambala  at dzone.com

============================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Jupyter Notebook for Data Science

☞ Data Science, Deep Learning, & Machine Learning with Python

☞ Deep Learning A-Z™: Hands-On Artificial Neural Networks

☞ Machine Learning A-Z™: Hands-On Python & R In Data Science

☞ Python for Data Science and Machine Learning Bootcamp

☞ Machine Learning, Data Science and Deep Learning with Python

☞ [2019] Machine Learning Classification Bootcamp in Python

☞ Introduction to Machine Learning & Deep Learning in Python

☞ Machine Learning Career Guide – Technical Interview

☞ Machine Learning Guide: Learn Machine Learning Algorithms

☞ Machine Learning Basics: Building Regression Model in Python

☞ Machine Learning using Python - A Beginner’s Guide

Machine Learning, Data Science and Deep Learning with Python

Machine Learning, Data Science and Deep Learning with Python

Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks. Introducing Tensorflow, Using Tensorflow, Introducing Keras, Using Keras, Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Learning Deep Learning, Machine Learning with Neural Networks, Deep Learning Tutorial with Python

Machine Learning, Data Science and Deep Learning with Python

Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks

Explore the full course on Udemy (special discount included in the link): http://learnstartup.net/p/BkS5nEmZg

In less than 3 hours, you can understand the theory behind modern artificial intelligence, and apply it with several hands-on examples. This is machine learning on steroids! Find out why everyone’s so excited about it and how it really works – and what modern AI can and cannot really do.

In this course, we will cover:
• Deep Learning Pre-requistes (gradient descent, autodiff, softmax)
• The History of Artificial Neural Networks
• Deep Learning in the Tensorflow Playground
• Deep Learning Details
• Introducing Tensorflow
• Using Tensorflow
• Introducing Keras
• Using Keras to Predict Political Parties
• Convolutional Neural Networks (CNNs)
• Using CNNs for Handwriting Recognition
• Recurrent Neural Networks (RNNs)
• Using a RNN for Sentiment Analysis
• The Ethics of Deep Learning
• Learning More about Deep Learning

At the end, you will have a final challenge to create your own deep learning / machine learning system to predict whether real mammogram results are benign or malignant, using your own artificial neural network you have learned to code from scratch with Python.

Separate the reality of modern AI from the hype – by learning about deep learning, well, deeply. You will need some familiarity with Python and linear algebra to follow along, but if you have that experience, you will find that neural networks are not as complicated as they sound. And how they actually work is quite elegant!

This is hands-on tutorial with real code you can download, study, and run yourself.

Python for Data Science and Machine Learning

Python for Data Science and Machine Learning

This Python tutorial for Data Science and Machine Learning will kick-start your learning of Python concepts needed for data science, as well as programming in general. Understand how to use the Jupyter Notebook, Understanding of Python from the beginning, Learn to use Object Oriented Programming with classes, Learn how to use NumPy, Pandas, Seaborn, Matplotlib, Plotly, Scikit-Learn, Machine Learning, Tensorflow, and more!

Master Python Complete Course

Python for Data Science and Machine Learning

This course will teach you from Python basics to advanced concepts in a practical manner, with Hands on exercises covered as well.

This Python tutorial for data science will kick-start your learning of Python concepts needed for data science, as well as programming in general. Python is required for data science because, Python programming is a versatile language commonly preferred by data scientists and big tech giant companies around the world, from startups to behemoths.

Whether you are a newbie in data science or already know about basic python for data science, this course is for you. In this python certification course, you will Learn Python programming in a practical manner with hands on coding assignments at the end of each section.

What you’ll learn

  • Get a complete understanding of Python from the beginning
  • Understand how to use the Jupyter Notebook
  • Master basics like variables, functions, tuples etc
  • Get hands-on with carefully designed coding assignments
  • Learn to use Object Oriented Programming with classes
  • Special Features and functions
  • Loops and condition formatting