…which I find hard to believe. Here are 10 ways to make python a dangerous tool for data science. -------------------------------------------------------------------------------------------------  [WordCloud](https://medium.com/@nohmanjanjua) Python has numerous applications — web development, desktop GUIs, software development, business applications and scientific/numeric computing. In this series we will be focusing on how to use numeric computing in Python for data science and ML. This is not a comprehensive Python tutorial but instead is intended to highlight the parts of the language that will be most important to us(some of which are often not the focus of Python tutorials). In this tutorial, we will be looking at the following basic features of Python : 1. Python function 2\. Data types and sequences 3\. Date and time 4\. Lambda 5\. Map 6\. Filter 7\. Reduce 8\. Zip 9\. For loop 10\. List comprehension ### 1\. Python function A function is a block of code which only runs when it is called. You can pass data, known as parameters into a function. Let’s write a function to multiply two numbers. _#multiply two numbers using a python function _**def** multiply(x,y): z = x\*y **return** z #call the function to multiply the numbers 2 and 3 multiply(4,3) **Output : 12** ### 2\. Python data types and sequences Python has built-in data types to store numeric and character data. Let us take a look at a few common types. type(' My name is Shimanto') **Output : str** type(5) **Output : int** type(5.0) **Output : float** type(**None**) #None signifies 'no value' or 'empty value' **Output : NoneType** type(multiply) #multiply is a function we created previously **Output : function** Now, let’s take a look at how we can store a list of numbers and characters, and how to perform few basic manipulations.  Photo on [Unsplash](https://unsplash.com/search/photos/python?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) ### i. Tuples : They are immutable data structures which cannot be altered unlike lists a = (1,2,3,4) type(a) **Output : tuple** ### ii. Lists : They are mutable objects b = \[1,2,3,4\] type(b) **Output : list** Let’s append a number to the list b created above. b.append(2.5) _#append to list using this function_ print(b) **Output : \[1, 2, 3, 4, 2.5\]** Loop through the list and print the numbers **for** number **in** b: _#looping through list_ print(number) **_Output :_** **_1 2 3 4 2.5_** Now, let’s concatanate two lists \[1,2,3\] + \[5,'bc','de'\] #concatenate lists **Output : \[1, 2, 3, 5, ‘bc’, ‘de’\]** Create a list with repeating numbers. \[1,2\]\*3 _#repeat lists_ **Output : \[1, 2, 1, 2, 1, 2\]** Check if an object you are searching for is in the list. 3 **in** b #in operator to check if required object is in list **Output : True** Unpack a list into separate variables. a,b = ('bc','def') print(a) print(b) **Output : bc def** ### iii. Strings : A string stores character objects x = 'My name is Shimanto' Access characters from string : x\[0\] #Access first letter **_Output : ‘M’_** x\[0:2\] #Accesses two letters **_Output : ‘My’_** x\[:-1\] #Accesses everything except last letter **_Output : ‘My name is shimant’_** x\[10:\] #returns all the characters from 10th position till end **_Output : ‘ Shimanto’_** Now, let’s concatenate two strings. first = 'Harun' last = 'Shimanto' Name = first + ' ' + last _#string concatenation _print(Name) **Output : Harun Shimanto** Show only the first word. Name.split(' ')\[0\] _#Show the first word_ **Output : ‘Harun’** Now, show only the second word in the string Name.split(' ')\[1\] _#Show the second word_ **Output : ‘Shimanto’** For concatenating numeric data to string, convert the number to a string first _#for concatenation convert objects to strings_ 'Harun' + str(2) **Output : Harun2** ### iv. Dictionary : A dictionary is a collection which is not ordered, but is indexed — and they have keys and values. c = {"Name" : "Harun", "Height" : 175} type(c) **Output : dict** Print data contained within a dictionary print(c) **_Output :_ {‘Name’: ‘Harun’, ‘Height’: 175}** Access dictionary values based on keys c\['Name'\] _#Access Name_ **Output : ‘Harun’** c\['Height'\] **Output : 175** Print all the keys in the dictionary _#print all the keys_ **for** i **in** c: print(i) **Output : Name Height** Print all the values in the dictionary **for** i **in** c.values(): print(i) **Output : Harun 175** Iterate over all the items in the dictionary **for** name, height **in** c.items(): print(name) print(height) **Output : Name Harun Height 175** ### 3\. Python Date and Time The following modules helps us in manipulating date and time variables in simple ways. **import** **datetime** **as** **dt** **import** **time** **as** **tm** Print the current time in seconds (starting from January 1, 1970) tm.time() _#print current time in seconds from January 1, 1970_ **Output :** **1533370235.0210752** _#convert timestamp to datetime _dtnow = dt.datetime.fromtimestamp(tm.time()) dtnow.year **Output : 2018** Get today’s date today = dt.date.today() today **Output : datetime.date(2018, 8, 4)** Subtract 100 days from today’s date delta = dt.timedelta(days=100) today - delta **Output : datetime.date(2018, 4, 26)** ### 4\. Map function Map function returns a list of the results after applying the given function to each item of a given sequence. For example, let’s find the minimum value between two pairs of lists. a = \[1,2,3,5\] b = \[8,9,10,11\] c = map(min,a,b) #Find the minimum between two pairs of lists **for** item **in** c: print(item) #print the minimum of the pairs **Output : 1 2 3 5** ### 5\. Lambda function Lambda function is used for creating small, one-time and anonymous function objects in Python. function = **lambda** a,b,c : a+b+c #function to add three numbersfunction(5,6,8) #call the function **Output : 19** ### 6\. Filter function Filter offers an easy way to filter out all the elements of a list. Filter (syntax : filter(function,list)) needs a function as its first argument, for which **lambda**can be used. As an example, let’s filter out only the numbers greater than 5 from a list x = \[0,2,3,4,5,7,8,9,10\] #create a list x2 = filter(lambda a : a>5, x) #filter using filter function print(list(x2)) **Output : \[7,8,9,10\]** ### 7\. Reduce function Reduce is a function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. As an example, let’s calculate the product of all the numbers in a list. from functools import reduce #import reduce function y = \[6,7,8,9,10\] #create list reduce(lambda a,b : a\*b,y) #use reduce **Output : 30240** ### 8\. Zip function Zip function returns a list of tuples, where the _i_\-th tuple contains the _i_\-th element from each of the sequences. Let’s look at an example. a = \[1,2,3,4\] #create two lists b = \[5,6,7,8\] c = zip(a,b) #Use the zip function print(list(c)) **Output : \[(1,5), (2,6), (3,7), (4,8)\]** If the sequences used in the zip function is unequal, the returned list is truncated in length to the length of the shortest sequence. a = \[1,2\] _#create two lists_ b = \[5,6,7,8\] c = zip(a,b) _#Use the zip function_ print(c) **Output : \[(1,5), (2,6)\]** ### 9\. For loop For loops are usually used when you have a block of code which you want to repeat a fixed number of times. Let us use a for loop to print the list of even numbers from 1 to 50. #return even numbers from 1 to 50 even=\[\] **for** i **in** range(50): **if** i%2 ==0: even.append(i) **else**: **None** print(even) #print the list **_Output :_ \[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48\]** ### 10\. List comprehension List comprehension provides an easier way to create lists. Continuing the same example, let’s create a list of even numbers from 1 to 50 using list comprehension. even = \[i for i in range(50) if i%2==0\] print(even) **_Output :_ \[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48\]** The features we looked at help in understanding the basic features of Python which are used for numerical computing. Apart from these in-built functions, there are other libraries such as _Numpy_ and _Pandas (which we look at in the upcoming articles)_ which are used extensively in data science and Machine learning. #### Resources : 1. [Python 3.7.0 documentation](https://docs.python.org/3/) 2. [Applied Data Science with Python Specialization.](https://www.coursera.org/specializations/data-science-python) Connect on [LinkedIn](https://www.linkedin.com/in/harun-ur-rashid6647/) and, check out Github (below) for the complete notebook. [**harunshimanto/Python-The-Dangerous-Tool-For-ML-Data-Science** _GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over…_github.com](https://github.com/harunshimanto/Python-The-Dangerous-Tool-For-ML-Data-Science/blob/master/Python%20for%20Data%20Science%20and%20ML%20-%20Part%201.ipynb "https://github.com/harunshimanto/Python-The-Dangerous-Tool-For-ML-Data-Science/blob/master/Python%20for%20Data%20Science%20and%20ML%20-%20Part%201.ipynb")[](https://github.com/harunshimanto/Python-The-Dangerous-Tool-For-ML-Data-Science/blob/master/Python%20for%20Data%20Science%20and%20ML%20-%20Part%201.ipynb) You can [tell me](http://harunspeedy1995@gmail.com) what you think about this, if you enjoy writing, click on the clap 👏 button. Thanks to everyone.