Data Science with Python
Data Science with Python Programming Language has evolved in recent times as the most preferred Certification Course for professionals who want to make their career as a Data Scientist. Being one of the most in-demand career courses for IT professionals, Data Science along with the addition of Python Programming Language helps professionals in excelling in the field of Data Science and Machine Learning insights. In other words, Python Programming is a must-know language for the ones, who want to make their career as a Data Scientist.
In this Data Science with Python Certification Course, learners will be able to understand and get an introduction to Data Science using Python, the basic constructs of Python, Object-Oriented Programming (OOP)’s in Python, Data visualization with Matplotlib, Machine Learning using Python and much more. Not only the theoretical explanations, but students will also get hands-on exercises on different concepts related to the Python Programming Language.
At the end of this Data Science with Python training course by Uplatz, students will be able to confidently apply for different Data Science with Python jobs, which is always in demand in many multi-national companies and big organizations.
-----------------------------------------------------------------------------------------------------------
Data Science with Python
Module 01 - Introduction to Data Science using Python
1.1 What is Data Science, what does a data scientist do
1.2 Various examples of Data Science in the industries
1.3 How Python is deployed for Data Science applications
1.4 Various steps in Data Science process like data wrangling, data exploration and selecting the model.
1.5 Introduction to Python programming language
1.6 Important Python features, how is Python different from other programming languages
1.7 Python installation, Anaconda Python distribution for Windows, Linux and Mac
1.8 How to run a sample Python script, Python IDE working mechanism
1.9 Running some Python basic commands
1.10 Python variables, data types and keywords.
Hands-on Exercise – Installing Python Anaconda for the Windows, Linux and Mac
Module 02 - Python basic constructs
2.1 Introduction to a basic construct in Python
2.2 Understanding indentation like tabs and spaces
2.3 Python built-in data types
2.4 Basic operators in Python
2.5 Loop and control statements like break, if, for, continue, else, range() and more.
Hands-on Exercise –
1. Write your first Python program
2. Write a Python function (with and without parameters)
3. Use Lambda expression
4. Write a class
5. Create a member function and a variable
6. Create an object and write a for loop to print all odd numbers
Module 03 - Maths for DS-Statistics & Probability
3.1 Central Tendency
3.2 Variabiltiy
3.3 Hypothesis Testing
3.4 Anova
3.5 Correlation
3.6 Regression
3.7 Probability Definitions and Notation
3.8 Joint Probabilities
3.9 The Sum Rule, Conditional Probability, and the Product Rule
3.10 Baye’s Theorem
Hands-on Exercise –
1. We will analyze both categorical data and quantitative data
2. Focusing on specific case studies to help solidify the week’s statistical concepts
Module 04 - OOPs in Python
4.1 Understanding the OOP paradigm like encapsulation, inheritance, polymorphism and abstraction
4.2 What are access modifiers, instances, class members
4.3 Classes and objects
4.4 Function parameter and return type functions
4.5 Lambda expressions.
Hands-on Exercise –
1. Writing a Python program and incorporating the OOP concepts
Module 05 - NumPy for mathematical computing
5.1 Introduction to mathematical computing in Python
5.2 What are arrays and matrices, array indexing, array math, Inspecting a numpy array, Numpy array manipulation
Hands-on Exercise –
1. How to import numpy module
2. Creating array using ND-array
3. Calculating standard deviation on array of numbers and calculating correlation between two variables.
Module 06 - Scipy for scientific computing
6.1 Introduction to scipy, building on top of numpy
6.2 What are the characteristics of scipy
6.3 Various subpackages for scipy like Signal, Integrate, Fftpack, Cluster, Optimize, Stats and more, Bayes Theorem with scipy.
Hands-on Exercise:
1. Importing of scipy
2. Applying the Bayes theorem on the given dataset.
Module 07 - Data manipulation
7.1 What is a data Manipulation. Using Pandas library
7.2 Numpy dependency of Pandas library
7.3 Series object in pandas
7.4 Dataframe in Pandas
7.5 Loading and handling data with Pandas
7.6 How to merge data objects
7.7 Concatenation and various types of joins on data objects, exploring dataset
Hands-on Exercise –
1. Doing data manipulation with Pandas by handling tabular datasets that includes variable types like float, integer, double and others.
2. Cleaning dataset, Manipulating dataset, Visualizing dataset
Module 08 - Data visualization with Matplotlib
8.1 Introduction to Matplotlib
8.2 Using Matplotlib for plotting graphs and charts like Scatter, Bar, Pie, Line, Histogram and more
8.3 Matplotlib API
Hands-on Exercise –
1. Deploying Matplotlib for creating pie, scatter, line and histogram.
2. Subplots and Pandas built-in data visualization.
Module 09 - Machine Learning using Python
9.1 Revision of topics in Python (Pandas, Matplotlib, numpy, scikit-Learn)
9.2 Introduction to machine learning
9.3 Need of Machine learning
9.4 Types of machine learning and workflow of Machine Learning
9.5 Uses Cases in Machine Learning, its various arlogrithms
9.6 What is supervised learning
9.7 What is Unsupervised Learning
Hands-on Exercise –
1. Demo on ML algorithms
Module 10 - Supervised learning
10.1 What is linear regression
10.2 Step by step calculation of Linear Regression
10.3 Linear regression in Python
10.4 Logistic Regression
10.5 What is classification
10.6 Decision Tree, Confusion Matrix, Random Forest, Naïve Bayes classifier (Self paced), Support Vector Machine(self paced), xgboost(self paced)
Hands-on Exercise – Using Python library Scikit-Learn for coming up with Random Forest algorithm to implement supervised learning.
Module 11 - Unsupervised Learning
11.1 Introduction to unsupervised learning
11.2 Use cases of unsupervised learning
11.3 What is clustering
11.4 Types of clustering(self-paced)-Exclusive clustering, Overlapping Clustering, Hierarchical Clustering(self-paced)
11.5 What is K-means clustering
11.6 Step by step calculation of k-means algorithm
11.7 Association Rule Mining(self-paced), Market Basket Analysis(self-paced), Measures in association rule mining(self-paced)-support, confidence, lift
11.8 Apriori Algorithm
Hands-on Exercise –
1. Setting up the Jupyter notebook environment
2. Loading of a dataset in Jupyter
3. Algorithms in Scikit-Learn package for performing Machine Learning techniques and training a model to search a grid.
4. Practice on k-means using Scikit
5. Practice on Apriori
Module 12 - Python integration with Spark
12.1 Introduction to pyspark
12.2 Who uses pyspark, need of spark with python
12.3 Pyspark installation
12.4 Pyspark fundamentals
12.5 Advantage over mapreduce, pyspark
12.6 Use-cases pyspark and demo.
Hands-on Exercise:
1. Demonstrating Loops and Conditional Statements
2. Tuple – related operations, properties, list, etc.
3. List – operations, related properties
4. Set – properties, associated operations, dictionary – operations, related properties.
Module 13 - Dimensionality Reduction
13.1 Introduction to Dimensionality
13.2 Why Dimensionality Reduction
13.3 PCA
13.4 Factor Analysis
13.5 LDA
Hands-on Exercise –
Practice Dimensionality reduction Techniques: PCA, Factor Analysis, t-SNE, Random Forest, Forward and Backward feature
Module 14 - Time Series Forecasting
14.1 White Noise
14.2 AR model
14.3 MA model
14.4 ARMA model
14.5 ARIMA model
14.6 Stationarity
14.7 ACF & PACF
Hands-on Exercise –
1. Create AR model
2. Create MA model
3. Create ARMA model
---------------------------------------------------------------------------------------------------
Data Science with Python Interview Questions
---------------------------------------------------------------------------------------------------
Q.1. What is the difference between a list and a tuple?
I’ve been asked this question in every python / data science interview I’ve ever had. Know the answer like the back of your hand.
• Lists are mutable. They can be modified after creation.
• Tuples are immutable. Once a tuple is created it cannot by changed
• Lists have order. They are an ordered sequences, typically of the same type of object. Ie: all user names ordered by creation date, ["Seth", "Ema", "Eli"]
• Tuples have structure. Different data types may exist at each index. Ie: a database record in memory, (2, "Ema", "2020–04–16") # id, name, created_at
Q.2. How is string interpolation performed?
Without importing the Template class, there are 3 ways to interpolate strings.
name = 'Chris'# 1. f strings
print(f'Hello {name}')# 2. % operator
print('Hey %s %s' % (name, name))# 3. format
print(
"My name is {}".format((name))
)
Q.3. What is the difference between “is” and “==”?
Early in my python career I assumed these were the same… hello bugs. So for the record, is checks identity and == checks equality.
We’ll walk through an example. Create some lists and assign them to names. Note that b points to the same object as a in below.
a = [1,2,3]
b = a
c = [1,2,3]
Check equality and note they are all equal.
print(a == b)
print(a == c)
#=> True
#=> True
But do they have the same identity? Nope.
print(a is b)
print(a is c)
#=> True
#=> False
We can verify this by printing their object id’s.
print(id(a))
print(id(b))
print(id(c))
#=> 4369567560
#=> 4369567560
#=> 4369567624
c has a different id than a and b.
Q.4. What is a decorator?
Another questions I’ve been asked in every interview. It’s deserves a post itself, but you’re prepared if you can walk through writing your own example.
A decorator allows adding functionality to an existing function by passing that existing function to a decorator, which executes the existing function as well as additional code.
We’ll write a decorator that that logs when another function is called.
Write the decorator function. This takes a function, func, as an argument. It also defines a function, log_function_called, which calls func() and executes some code, print(f'{func} called.'). Then it return the function it defined
def logging(func):
def log_function_called():
print(f'{func} called.')
func()
return log_function_called
Let’s write other functions that we’ll eventually add the decorator to (but not yet).
def my_name():
print('chris')def friends_name():
print('naruto')my_name()
friends_name()
#=> chris
#=> naruto
Now add the decorator to both.
@logging
def my_name():
print('chris')@logging
def friends_name():
print('naruto')my_name()
friends_name()
#=> called.
#=> chris
#=> called.
#=> naruto
See how we can now easily add logging to any function we write just by adding @logging above it.
Q.5. Explain the range function
Range generates a list of integers and there are 3 ways to use it.
The function takes 1 to 3 arguments. Note I’ve wrapped each usage in list comprehension so we can see the values generated.
range(stop) : generate integers from 0 to the “stop” integer.
[i for i in range(10)]
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(start, stop) : generate integers from the “start” to the “stop” integer.
[i for i in range(2,10)]
#=> [2, 3, 4, 5, 6, 7, 8, 9]
range(start, stop, step) : generate integers from “start” to “stop” at intervals of “step”.
[i for i in range(2,10,2)]
#=> [2, 4, 6, 8]
Thanks Searge Boremchuq for suggesting a more pythonic way to do this!
list(range(2,10,2))
#=> [2, 4, 6, 8]
Q.6. Define a class named car with 2 attributes, “color” and “speed”. Then create an instance and return speed.
class Car :
def __init__(self, color, speed):
self.color = color
self.speed = speedcar = Car('red','100mph')
car.speed
#=> '100mph'
Q.7. What is the difference between instance, static and class methods in python?
Instance methods : accept self parameter and relate to a specific instance of the class.
Static methods : use @staticmethod decorator, are not related to a specific instance, and are self-contained (don’t modify class or instance attributes)
Class methods : accept cls parameter and can modify the class itself
We’re going to illustrate the difference around a fictional CoffeeShop class.
class CoffeeShop:
specialty = 'espresso'
def __init__(self, coffee_price):
self.coffee_price = coffee_price
# instance method
def make_coffee(self):
print(f'Making {self.specialty} for ${self.coffee_price}')
# static method
@staticmethod
def check_weather():
print('Its sunny') # class method
@classmethod
def change_specialty(cls, specialty):
cls.specialty = specialty
print(f'Specialty changed to {specialty}')
CoffeeShop class has an attribute, specialty, set to 'espresso' by default. Each instance of CoffeeShop is initialized with an attribute coffee_price . It also has 3 methods, an instance method, a static method and a class method.
Let’s initialize an instance of the coffee shop with a coffee_price of 5. Then call the instance method make_coffee.
coffee_shop = CoffeeShop('5')
coffee_shop.make_coffee()
#=> Making espresso for $5
Now call the static method. Static methods can’t modify class or instance state so they’re normally used for utility functions, for example, adding 2 numbers. We used ours to check the weather.Its sunny. Great!
coffee_shop.check_weather()
#=> Its sunny
Now let’s use the class method to modify the coffee shop’s specialty and then make_coffee.
coffee_shop.change_specialty('drip coffee')
#=> Specialty changed to drip coffeecoffee_shop.make_coffee()
#=> Making drip coffee for $5
Note how make_coffee used to make espresso but now makes drip coffee!
Q.8. What is the difference between “func” and “func()”?
The purpose of this question is to see if you understand that all functions are also objects in python.
def func():
print('Im a function')
func
#=> function __main__.func>func()
#=> Im a function
func is the object representing the function which can be assigned to a variable or passed to another function. func() with parentheses calls the function and returns what it outputs.
Q.9. Explain how the map function works
map returns a map object (an iterator) which can iterate over returned values from applying a function to every element in a sequence. The map object can also be converted to a list if required.
def add_three(x):
return x + 3li = [1,2,3][i for i in map(add_three, li)]
#=> [4, 5, 6]
Above, I added 3 to every element in the list.
A reader suggested a more pythonic implementation. Thanks Chrisjan Wust !
def add_three(x):
return x + 3li = [1,2,3]
list(map(add_three, li))
#=> [4, 5, 6]
Also, thanks Michael Graeme Short for the corrections!
Q.10. Explain how the reduce function works
This can be tricky to wrap your head around until you use it a few times.
reduce takes a function and a sequence and iterates over that sequence. On each iteration, both the current element and output from the previous element are passed to the function. In the end, a single value is returned.
from functools import reducedef add_three(x,y):
return x + yli = [1,2,3,5]reduce(add_three, li)
#=> 11
11 is returned which is the sum of 1+2+3+5.
Q.11. Explain how the filter function works
Filter literally does what the name says. It filters elements in a sequence.
Each element is passed to a function which is returned in the outputted sequence if the function returns True and discarded if the function returns False.
def add_three(x):
if x % 2 == 0:
return True
else:
return Falseli = [1,2,3,4,5,6,7,8][i for i in filter(add_three, li)]
#=> [2, 4, 6, 8]
Note how all elements not divisible by 2 have been removed.
Q.12. Does python call by reference or call by value?
Be prepared to go down a rabbit hole of semantics if you google this question and read the top few pages.
In a nutshell, all names call by reference, but some memory locations hold objects while others hold pointers to yet other memory locations.
name = 'object'
Let’s see how this works with strings. We’ll instantiate a name and object, point other names to it. Then delete the first name.
x = 'some text'
y = x
x is y #=> Truedel x # this deletes the 'a' name but does nothing to the object in memoryz = y
y is z #=> True
What we see is that all these names point to the same object in memory, which wasn’t affected by del x.
Here’s another interesting example with a function.
name = 'text'def add_chars(str1):
print( id(str1) ) #=> 4353702856
print( id(name) ) #=> 4353702856
# new name, same object
str2 = str1
# creates a new name (with same name as the first) AND object
str1 += 's'
print( id(str1) ) #=> 4387143328
# still the original object
print( id(str2) ) #=> 4353702856
add_chars(name)
print(name) #=>text
Notice how adding an s to the string inside the function created a new name AND a new object. Even though the new name has the same “name” as the existing name.
Q.13. How to reverse a list?
Note how reverse() is called on the list and mutates it. It doesn’t return the mutated list itself.
li = ['a','b','c']print(li)
li.reverse()
print(li)
#=> ['a', 'b', 'c']
#=> ['c', 'b', 'a']
Q.14. How does string multiplication work?
Let’s see the results of multiplying the string ‘cat’ by 3.
'cat' * 3
#=> 'catcatcat'
The string is concatenated to itself 3 times.
Q.15. How does list multiplication work?
Let’s see the result of multiplying a list, [1,2,3] by 2.
[1,2,3] * 2
#=> [1, 2, 3, 1, 2, 3]
A list is outputted containing the contents of [1,2,3] repeated twice.
Q.16. What does “self” refer to in a class?
Self refers to the instance of the class itself. It’s how we give methods access to and the ability to update the object they belong to.
Below, passing self to __init__() gives us the ability to set the color of an instance on initialization.
class Shirt:
def __init__(self, color):
self.color = color
s = Shirt('yellow')
s.color
#=> 'yellow'
Q.17. How can you concatenate lists in python?
Adding 2 lists together concatenates them. Note that arrays do not function the same way.
a = [1,2]
b = [3,4,5]a + b
#=> [1, 2, 3, 4, 5]
Q.18. What is the difference between a shallow and a deep copy?
We’ll discuss this in the context of a mutable object, a list. For immutable objects, shallow vs deep isn’t as relevant.
We’ll walk through 3 scenarios.
i) Reference the original object. This points a new name, li2, to the same place in memory to which li1 points. So any change we make to li1 also occurs to li2.
li1 = [['a'],['b'],['c']]
li2 = li1li1.append(['d'])
print(li2)
#=> [['a'], ['b'], ['c'], ['d']]
ii) Create a shallow copy of the original. We can do this with the list() constructor, or the more pythonic mylist.copy() (thanks Chrisjan Wust !).
A shallow copy creates a new object, but fills it with references to the original. So adding a new object to the original collection, li3, doesn’t propagate to li4, but modifying one of the objects in li3 will propagate to li4.
li3 = [['a'],['b'],['c']]
li4 = list(li3)li3.append([4])
print(li4)
#=> [['a'], ['b'], ['c']]li3[0][0] = ['X']
print(li4)
#=> [[['X']], ['b'], ['c']]
iii) Create a deep copy. This is done with copy.deepcopy(). The 2 objects are now completely independent and changes to either have no affect on the other.
import copyli5 = [['a'],['b'],['c']]
li6 = copy.deepcopy(li5)li5.append([4])
li5[0][0] = ['X']
print(li6)
#=> [['a'], ['b'], ['c']]
Q.19. What is the difference between lists and arrays?
Note: Python’s standard library has an array object but here I’m specifically referring to the commonly used Numpy array.
• Lists exist in python’s standard library. Arrays are defined by Numpy.
• Lists can be populated with different types of data at each index. Arrays require homogeneous elements.
• Arithmetic on lists adds or removes elements from the list. Arithmetic on arrays functions per linear algebra.
• Arrays also use less memory and come with significantly more functionality.
I wrote another comprehensive post on arrays.
Q.20. How to concatenate two arrays?
Remember, arrays are not lists. Arrays are from Numpy and arithmetic functions like linear algebra.
We need to use Numpy’s concatenate function to do it.
import numpy as npa = np.array([1,2,3])
b = np.array([4,5,6])np.concatenate((a,b))
#=> array([1, 2, 3, 4, 5, 6])
Q.21. What do you like about Python?
Note this is a very subjective question and you’ll want to modify your response based on what the role is looking for.
Python is very readable and there is a pythonic way to do just about everything, meaning a preferred way which is clear and concise.
I’d contrast this to Ruby where there are often many ways to do something without a guideline for which is preferred.
Q.22. What is you favorite library in Python?
Also subjective, see question 21.
When working with a lot data, nothing is quite as helpful as pandas which makes manipulating and visualizing data a breeze.
Q.23. Name mutable and immutable objects
Immutable means the state cannot be modified after creation. Examples are: int, float, bool, string and tuple.
Mutable means the state can be modified after creation. Examples are list, dict and set.
Q.24. How would you round a number to 3 decimal places?
Use the round(value, decimal_places) function.
a = 5.12345
round(a,3)
#=> 5.123
Q.25. How do you slice a list?
Slicing notation takes 3 arguments, list[start:stop:step], where step is the interval at which elements are returned.
a = [0,1,2,3,4,5,6,7,8,9]print(a[:2])
#=> [0, 1]print(a[8:])
#=> [8, 9]print(a[2:8])
#=> [2, 3, 4, 5, 6, 7]print(a[2:8:2])
#=> [2, 4, 6]
Q.26. What is pickling?
Pickling is the go-to method of serializing and unserializing objects in Python.
In the example below, we serialize and unserialize a list of dictionaries.
import pickleobj = [
{'id':1, 'name':'Stuffy'},
{'id':2, 'name': 'Fluffy'}
]with open('file.p', 'wb') as f:
pickle.dump(obj, f)with open('file.p', 'rb') as f:
loaded_obj = pickle.load(f)print(loaded_obj)
#=> [{'id': 1, 'name': 'Stuffy'}, {'id': 2, 'name': 'Fluffy'}]
Q.27. What is the difference between dictionaries and JSON?
Dict is python datatype, a collection of indexed but unordered keys and values.
JSON is just a string which follows a specified format and is intended for transferring data.
Q.28. What ORMs have you used in Python?
ORMs (object relational mapping) map data models (usually in an app) to database tables and simplifies database transactions.
SQLAlchemy is typically used in the context of Flask, and Django has it’s own ORM.
Q.29. How do any() and all() work?
Any takes a sequence and returns true if any element in the sequence is true.
All returns true only if all elements in the sequence are true.
a = [False, False, False]
b = [True, False, False]
c = [True, True, True]print( any(a) )
print( any(b) )
print( any(c) )
#=> False
#=> True
#=> Trueprint( all(a) )
print( all(b) )
print( all(c) )
#=> False
#=> False
#=> True
Q.30. Are dictionaries or lists faster for lookups?
Looking up a value in a list takes O(n) time because the whole list needs to be iterated through until the value is found.
Looking up a key in a dictionary takes O(1) time because it’s a hash table.
This can make a huge time difference if there are a lot of values so dictionaries are generally recommended for speed. But they do have other limitations like needing unique keys.
Q.31. What is the difference between a module and a package?
A module is a file (or collection of files) that can be imported together.
import sklearn
A package is a directory of modules.
from sklearn import cross_validation
So packages are modules, but not all modules are packages.
Q.32. How to increment and decrement an integer in Python?
Increments and decrements can be done with +- and -= .
value = 5value += 1
print(value)
#=> 6value -= 1
value -= 1
print(value)
#=> 4
Q.33. How to return the binary of an integer?
Use the bin() function.
bin(5)
#=> '0b101'
Q.34. How to remove duplicate elements from a list?
This can be done by converting the list to a set then back to a list.
a = [1,1,1,2,3]
a = list(set(a))
print(a)
#=> [1, 2, 3]
Note that sets will not necessarily maintain the order of a list.
Q.35. How to check if a value exists in a list?
Use in.
'a' in ['a','b','c']
#=> True'a' in [1,2,3]
#=> False
Q.36. What is the difference between append and extend?
append adds a value to a list while extend adds values in another list to a list.
a = [1,2,3]
b = [1,2,3]a.append(6)
print(a)
#=> [1, 2, 3, 6]b.extend([4,5])
print(b)
#=> [1, 2, 3, 4, 5]
Q.37. How to take the absolute value of an integer?
This can be done with the abs() function.
abs(2)
#=> 2abs(-2)
#=> 2
Q.38. How to combine two lists into a list of tuples?
You can use the zip function to combine lists into a list of tuples. This isn’t restricted to only using 2 lists. It can also be done with 3 or more.
a = ['a','b','c']
b = [1,2,3][(k,v) for k,v in zip(a,b)]
#=> [('a', 1), ('b', 2), ('c', 3)]
Q.39. How can you sort a dictionary by key, alphabetically?
You can’t “sort” a dictionary because dictionaries don’t have order but you can return a sorted list of tuples which has the keys and values that are in the dictionary.
d = {'c':3, 'd':4, 'b':2, 'a':1}sorted(d.items())
#=> [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Q.40. How does a class inherit from another class in Python?
In the below example, Audi, inherits from Car. And with that inheritance comes the instance methods of the parent class.
class Car():
def drive(self):
print('vroom')class Audi(Car):
passaudi = Audi()
audi.drive()
Q.41. How can you remove all whitespace from a string?
The easiest way is to split the string on whitespace and then rejoin without spaces.
s = 'A string with white space'''.join(s.split())
#=> 'Astringwithwhitespace'
2 readers recommended a more pythonic way to handle this following the Python ethos that Explicit is better than Implicit. It’s also faster because python doesn’t create a new list object. Thanks Евгений Крамаров and Chrisjan Wust !
s = 'A string with white space'
s.replace(' ', '')
#=> 'Astringwithwhitespace'
Q.42. Why would you use enumerate() when iterating on a sequence?
enumerate() allows tracking index when iterating over a sequence. It’s more pythonic than defining and incrementing an integer representing the index.
li = ['a','b','c','d','e']for idx,val in enumerate(li):
print(idx, val)
#=> 0 a
#=> 1 b
#=> 2 c
#=> 3 d
#=> 4 e
Q.43. What is the difference between pass, continue and break?
pass means do nothing. We typically use it because Python doesn’t allow creating a class, function or if-statement without code inside it.
In the example below, an error would be thrown without code inside the i > 3 so we use pass.
a = [1,2,3,4,5]for i in a:
if i > 3:
pass
print(i)
#=> 1
#=> 2
#=> 3
#=> 4
#=> 5
continue continues to the next element and halts execution for the current element. So print(i) is never reached for values where i < 3.
for i in a:
if i < 3:
continue
print(i)
#=> 3
#=> 4
#=> 5
break breaks the loop and the sequence is not longer iterated over. So elements from 3 onward are not printed.
for i in a:
if i == 3:
break
print(i)
#=> 1
#=> 2
Q.44. Convert the following for loop into a list comprehension.
This for loop.
a = [1,2,3,4,5]
a2 = []
for i in a:
a2.append(i + 1)print(a2)
#=> [2, 3, 4, 5, 6]
Becomes.
a3 = [i+1 for i in a]print(a3)
#=> [2, 3, 4, 5, 6]
List comprehension is generally accepted as more pythonic where it’s still readable.
Q.45. Give an example of the ternary operator.
The ternary operator is a one-line if/else statement.
The syntax looks like a if condition else b.
x = 5
y = 10'greater' if x > 6 else 'less'
#=> 'less''greater' if y > 6 else 'less'
#=> 'greater'
Q.46. Check if a string only contains numbers.
You can use isnumeric().
'123a'.isnumeric()
#=> False'123'.isnumeric()
#=> True
Q.47. Check if a string only contains letters.
You can use isalpha().
'123a'.isalpha()
#=> False'a'.isalpha()
#=> True
Q.48. Check if a string only contains numbers and letters.
You can use isalnum().
'123abc...'.isalnum()
#=> False'123abc'.isalnum()
#=> True
Q.49. Return a list of keys from a dictionary.
This can be done by passing the dictionary to python’s list() constructor, list().
d = {'id':7, 'name':'Shiba', 'color':'brown', 'speed':'very slow'}list(d)
#=> ['id', 'name', 'color', 'speed']
Q.50. How do you upper and lowercase a string?
You can use the upper() and lower() string methods.
small_word = 'potatocake'
big_word = 'FISHCAKE'small_word.upper()
#=> 'POTATOCAKE'big_word.lower()
#=> 'fishcake'
Q.51. What is the difference between remove, del and pop?
remove() remove the first matching value.
li = ['a','b','c','d']li.remove('b')
li
#=> ['a', 'c', 'd']
del removes an element by index.
li = ['a','b','c','d']del li[0]
li
#=> ['b', 'c', 'd']
pop() removes an element by index and returns that element.
li = ['a','b','c','d']li.pop(2)
#=> 'c'li
#=> ['a', 'b', 'd']
Q.52. Give an example of dictionary comprehension.
Below we’ll create dictionary with letters of the alphabet as keys, and index in the alphabet as values.
# creating a list of letters
import string
list(string.ascii_lowercase)
alphabet = list(string.ascii_lowercase)# list comprehension
d = {val:idx for idx,val in enumerate(alphabet)} d
#=> {'a': 0,
#=> 'b': 1,
#=> 'c': 2,
#=> ...
#=> 'x': 23,
#=> 'y': 24,
#=> 'z': 25}
Q.53. How is exception handling performed in Python?
Python provides 3 words to handle exceptions, try, except and finally.
The syntax looks like this.
try:
# try to do this
except:
# if try block fails then do this
finally:
# always do this
In the simplistic example below, the try block fails because we cannot add integers with strings. The except block sets val = 10 and then the finally block prints complete.
try:
val = 1 + 'A'
except:
val = 10
finally:
print('complete')
print(val)
#=> complete
#=> 10
---------------------------------------------------------------------------------------------------