visit
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 :
_#multiply two numbers using a python function_def multiply(x,y):z = x*yreturn z
Output : 12
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 previouslyOutput : 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
Output : tuple
Output : list
Let’s append a number to the list b created above.
b.append(2.5) #append to list using this functionprint(b)
Output : [1, 2, 3, 4, 2.5]
Loop through the list and print the numbers
for number in b: #looping through listprint(number)
Output :
12342.5
Now, let’s concatanate two lists [1,2,3] + [5,'bc','de'] #concatenate listsOutput : [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.
Output : bcdef
Output : ‘M’
x[0:2] #Accesses two lettersOutput : ‘My’
x[:-1] #Accesses everything except last letterOutput : ‘My name is shimant’
x[10:] #returns all the characters from 10th position till endOutput : ‘ Shimanto’
Now, let’s concatenate two strings.
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 stringName.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
Output : dict
Print data contained within a dictionary print(c)Output : {‘Name’: ‘Harun’, ‘Height’: 175}
Access dictionary values based on keysc['Name'] #Access Name
Output : ‘Harun’
c['Height']Output : 175
Print all the keys in the dictionary
#print all the keysfor i in c:print(i)
Output : NameHeight
Print all the values in the dictionary
for i in c.values():print(i)
Output : Harun175
Iterate over all the items in the dictionary
for name, height in c.items():print(name)print(height)
Output : NameHarunHeight175
import datetime as dtimport 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
Output : 2018
Get today’s date
Output : datetime.date(2018, 8, 4)
Subtract 100 days from today’s date
Output : datetime.date(2018, 4, 26)
for item in c:print(item) #print the minimum of the pairs
Output : 1235
function = lambda a,b,c : a+b+c #function to add three numbersfunction(5,6,8) #call the function
Output : 19
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 lambdacan be used. As an example, let’s filter out only the numbers greater than 5 from a list
Output : [7,8,9,10]
Output : 30240
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.
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 listsb = [5,6,7,8]
c = zip(a,b) #Use the zip functionprint(c)
Output : [(1,5), (2,6)]
even=[]for i in range(50):if i%2 ==0:even.append(i)else:None
print(even) #print the listOutput : [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]
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.