When you want to learn Data Science or Data Analysis, you have to learn either Python or R and after that using basic python/R functions with other statistical packages you can easily start analysing data and get some insight from your data. As i too have both the options, i chose Python over R as i felt its easy to learn and with the help of Python and statistical packages like Pandas, NumPy, Scikit-learn, Keras you can easily start building Machine Learning models but to make all the above things work, the first and foremost thing that you have to do is to learn some basic python fundamentals.
Here below, in this post, we have shared some basic python fundamentals which will help you in your process of becoming Data Scientist and Data Analyst.
Let’s check it our some basic Python Fundamentals:
Defining your own function:
The first thing while learning about python fundamentals, you have to learn how to define your own functions. To define a function, you have to use a keyword def. We use a def keyword to indicate python that we are defining a function here or we can say it’s a user-defined function.
When you want to take the value from the user and want to print the square of that value you can do so by passing a parameter in the function header:
square(4) Output - 16 square(5) #here 5 is an argument we passed to function Output - 25
Return a value from Function:
When you define a function in python, you sometimes or always want your function to return a value. To achieve this you have to use a return keyword as shown below:
def square(value): result = value ** 2 return result num = square(4) print(num) 16
Multiple function Parameters:
def square(value1, value2): result = value1 ** value2 return result num = square(2,4) print(num) 16
Returning multiple values from a function:
To return multiple values from a function, we have to use Tuples. They are similar to list but differ on one condition — they are immutables i.e. once they are defined, they can’t be changed like lists. Tuples are constructed using parenthesis ().
def square(value1, value2): result1 = value1 ** value2 result2 = value2 ** value1 result = (result1, result2) return result num = square(2,4) print(num) (16,16)
Iterators in Python
First, let’s learn about iterables and iterators.
Iterables are those objects which have a .iter() method associated with them. And applying iter() method to an iterable creates an iterator.Example of iterables: list(), string(), dictionaries etc. Iterator provides the next value when you call next() on it. Let’s understand this with an example:
Below you can see a String ‘Pankaj’ and also how we can iterate over it to print a letter using next():
String = 'Pankaj' Iterator = iter(String) next(Iterator) 'P' next(Iterator) 'a' next(Iterator) 'n' next(Iterator) 'k' next(Iterator) 'a' next(Iterator) 'j'
- Similarly, we can also iterate over a list as shown below:
Names = ['Pankaj', 'Rahul', 'Abhishek', 'Yedu'] Iterator = iter(Names) next(Iterator) 'Pankaj' next(Iterator) 'Rahul' next(Iterator) 'Abhishek' next(Iterator) 'Yedu'
Using For Loop:
We can iterate over the list that we defined above ‘Names’ using a For loop as well. Here’s how you can do it.
for name in Names: print(name) Output - Pankaj Rahul Abhishek Yedu
Similarly, we can iterate over a string as well:
Name = 'Pankaj' for letter in Name: print(letter) Output - P a n k a j
Using range() function with for loop:
Basically, you can think about range function as a number of iterations or how many times you want your loop to execute. The syntax of range() function is shown below:
range(start, stop [,step])
If we pass a single parameter in range function it will always start its index from 0 because python is based on 0 based indexing. For example, range(10) means [0,1,2,3,4,5,6,7,8,9]. If we do not want to start from index 0, we can pass a starting index in range() function.
Let’s see range() function in action:
for i in range(5,10): print(i) Output - 5 6 7 8 9
Say what if we do not only want to start with our index from 0, but we also want our index to increase by 2 every time, this we can do so by adding an optional parameter step as shown in range function syntax above. Our code looks like this:
for i in range(4,10,2): print(i) Output - 4 6 8
If else statement in Python:
An else statement can be combined with an if statement. An else statement contains the block of code that executes, only when if expression resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else statement following if. If you want to insert more condition, you can use elif with the if-else statement.
The syntax of if, else statements is:
if expression: statement(s) else: statement(s)
For example:
for i in range(6): if i < 5: print(i) else: print('i is greater than 5') Output- 0 1 2 3 4 i is greater than 5
Using enumerate() in Python:
In this basic python fundamentals post, the next we are going to use is python’s built-in function enumerate()
Employee = ['Mark', 1234, 'Active'] emp = enumerate(Employee) print(type(emp)) <class 'enumerate'> emp_list = list(emp) print(emp_list) [(0, 'Mark'), (1, 1234), (2, 'Active')]
Using for loop to unpack the values in enumerate():
for index, value in enumerate(Employee): print(str(index) + ' '+ str(value)) Output- 0 Mark 1 1234 2 Active
enumerate always take index values from 0 by default but if you want to start an index from some other number, we can do so by:
for index, value in enumerate(Employee, start=5): print(str(index) + ' '+ str(value)) Output- 5 Mark 6 1234 7 Active
Using zip() in Python:
In this basic python fundamentals post, the next we are going to learn is how to use Python’s built-in function zip(). Python’s zip function is mainly used to combining data of two iterable elements together. Have a quick look below for better understanding.
Emp_FirstName = ['James', 'Michael', 'Maria', 'Mary', 'Robert'] Emp_LastName = ['Smith','Smith','Garcia','Rodriguez','Smith'] Emp_FullName = zip(Emp_FirstName, Emp_LastName) print(type(Emp_FullName)) <class 'zip'> print(Emp_FullName) <zip object at 0x000001C703497BC> Emp_List = list(Emp_FullName) print(Emp_List) [('James', 'Smith'), ('Michael', 'Smith'), ('Maria', 'Garcia'), ('Mary', 'Rodriguez'), ('Robert', 'Smith')]
Say you want to zip the above two list, but want to unpack them without passing the zip to list, we can do so by using for loop as shown below:
Emp_FirstName = ['James', 'Michael', 'Maria', 'Mary', 'Robert'] Emp_LastName = ['Smith','Smith','Garcia','Rodriguez','Smith'] z = zip(Emp_FirstName, Emp_LastName) for z1, z2 in z: print(z1, z2) Output- James Smith Michael Smith Maria Garcia Mary Rodriguez Robert Smith
we can also achieve the same result as above without using a for loop, here’s how you can do so:
z = zip(Emp_FirstName, Emp_LastName) print(*z) Output- ('James', 'Smith') ('Michael', 'Smith') ('Maria', 'Garcia') ('Mary', 'Rodriguez') ('Robert', 'Smith')
Lamba Function in Python:
In this basic python fundamentals post, the next we are going to learn is how to use Python’s Lambda expression. Basically, lambda keyword is used to create anonymous functions and the important thing about this function that you have to keep in mind always is that this function can have any number of arguments but only one expression, which is evaluated and returned.
Basic Syntax of the lambda expression:
lambda arguments: expression
Below is a lambda code that will give you a square of a number as an output:
square = lambda x, y : x ** y square(2,3) 8
Iterating over dictionaries in Python:
Basically, Dictionaries in Python can be defined as a collection of keys-values pairs which are unordered, mutable and indexable. If you want to know how you can create and manipulate dictionaries in python, check our post creating and computing over Dictionaries in Python. Below is a code to show how you can iterate over a dictionary in python :
Employee = { 'Name' : 'Mark','Employee_ID' : 15613, 'Status' : 'Active'}
for k, v in Employee.items(): print(k +' : '+ str(v)) Name : Mark Employee_ID : 15613 Status : Active
There are other expressions which are handy while playing with data like list comprehension, dictionary comprehension etc. To know how to use list comprehension, dictionary comprehension in Python, check out our post on using List Comprehensions and Dictionary Comprehensions in Python.
For more information, you can check our post on 35 Pandas codes every data scientist aspirant must know and also you can view the official Python 3 Documentation. If you want to learn more like installing python and basic concepts, you can check here.
Leave a Reply