0 ratings 0% found this document useful (0 votes) 10 views 24 pages Programming Concepts and Problem-Solving Using Python
The document is a workbook for a BSc program in Artificial Intelligence and Machine Learning at TISS, focusing on programming concepts and problem-solving using Python. It covers fundamental computing principles, algorithms, and exercises related to Python programming, including array manipulation, list comprehensions, and database interactions. The workbook also includes practical exercises to apply learned concepts in real-world scenarios.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
ms
* TATA INSTITUTE OF
= SOCIAL SCIENCES
TISS. SCHOOL OF SKILL EDUCATION
Why.
by,»
eas
BSc in Artificial Intelligence
and Machine Learning
Greereraduate Certificate - First Year
| SEMESTER-I
Programming Concepts and
Problem-Solving Using Python
PARTICIPANT’ S WORKBOOKTISS SSE
Session 1: Problem Solving, Introduction to Python IDE
Computing technology has changed, and is continuing to change the world. Essentially every
aspect of life has been impacted by computing. Just-in-time inventory allows companies to
significantly reduce costs. Universal digital medical records promise to save the lives of many
Of the estimated 100,000 people who die each year from medical errors. Vast information
resources, such as Wikipedia, now provide easy, quick access to a breadth of knowledge as
never before.
In the study of computer science, there are fundamental principles of computation to be
Jeamed that will never change. In addition to these principles, of course, there is always
changing technology. That is what makes the field of computer science so exciting. There is
constant change and advancement, but also a foundation of principles to draw from. What can \
be done with computation is limited only by our imagination, With that said, we begin our
journey into the world of computing,
Algorithms solve general problems (determining whether any given number is a prime
number), and not specific ones (determining whether 30753 is a prime number).
Exercise 1
Differentiate between a compiler and an interpreter.
__ QrrPiiex “Tarte Probe
poss ae = ; i
cress a ce “eit oe). Bgbrory ForSericty Reb,
ee
2 | Programming Concepts and Problem-Solving Using Python | PWa A a ‘array called zeros having 10 elements and all the elements are set to zero,
ae cE array called vowels having the elements ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’.
©) A 2-D array called ones having 2 rows and 5 columns and all the elements are
setto land dtype as int, =
© Use nested Python lists to create a 2-D array called myarrayl having 3 rows and
3 columns and store the following data: 2.7, -2,-19 0, 3.4, 99.9 10.6, 0, 13
©) A2-D array called myarray2 using arange() having 3 rows and 5 cohimns with
start value = 4, step size 4 and ae as float,
sales uy
OMiyoneny T= -KO-arange (+ BH, 4 Tape = Rook.) west fie(3, 8)
Exercise 9
Using the arrays created in Question 4 above, write NumPy commands for the
following:
a) Find the dimensions, shape, size, data type of the items and item size of arrays
zeros, vowels, ones, myarray] and myarray2.
'b) Reshape the array ones to have all the 10 elements in a single row.
©) Display the 2nd and 3rd element of the array vowels.
4) Display all elements in the 2nd and 3rd row of the array myarray1.
©) Display the elements in the Ist and 2nd column of the array myarray1. f) Display
the elements in the Ist column of the 2nd and 3rd row of the array myarray1. g)
poe Se fhe a0 array of ore
ae ee : as emer Is
pe a Seay Ne a
Tage a Fite « 7 re Seem
Hpac se st ae oe sel
___ Boney —rrerbafed 7 Ones Wares ie)
7 | Programming Concepts and Problem-Solving Using Python | PWTISS SSE
Exercise 14
For the following function, indicate whether each function call is proper of not. If improper,
explain why.
def ged(nl, n2): function ged calculates the greatest common divisor of nl and
n2, with the requirement that n1 be less than or equal to n2,
and nl and n2 are integer values.
(aj2510
bS20
result 5 gcd(a, b)
(b) a5 10.0
bS520
result $ ged(a, b)
(©) 2520
bSsi0
result 5 ged(b, a) |
(d)a5 10 \
b520
e530
result 5 ged(gcd(a, b), c)
(e)a5 10
b520
£530 7
print(ged(a, ged{e, b))) ~
Gin eNe See Sokegges
b= Fo 2X29 365 We Lovclign eo SRE
BOs = Qed GN) Mae Condon:TISS SSE
Session 6 - List comprehension, Generators
ist comprehensions are a thitd Way OF making lists, With this elegant a
ue js ts for loop. fou the first example in just a single line of code:
ssp air i [i i fori in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
PPTOACH, You coutg
Rather than creating. an empty list and adding each element to the end,
list and its contents at the same time by following this format.
new list = [expression for member in iterable]
‘you simply define the
Every list comprehension in Python includes three elements:
1. expression is the member itself, a call
Sob Of any other object that can retum its
clements one at atime. In the example above, the iterable is range(10),
Because the expression requirement is so flexible,
in many places where you would use map(), You o
list comprehension:
25> tans = [1.09, 23.56, 57.84, 4.56, 6.73]
>>> TAX_RATE =.08
>>> def get_price_with tax(txn):
retum tan “(1 +TAX_RATE)
22> final prices = [get_price_with_tax(i) for jin tens]
>>> final_prices
11.1772000000000002, 25.4448, 62.467200000000005, 4.9248,
a list comprehension in Python works well.
‘an rewrite the pricing example with its own
The only distinction between this implementation and
Python returns a list, not a map object,
Earlier, you saw this formula for how to ereate liste
new_list = [expression for member in iterable]
While this formula is accurate, it’s also a bit
comprehension formula adds support for o
add conditional _losic to a list comprehTISS SSE -.
Session 7 - Databases in Python, API, SQL queries
guages including
‘rieve and modify
Reading from Database Table
© fetchone() ~ It fetches the next row of a query result set, A result set is an object that
is retumed when a cursor object is used to query a table,
© fetchall() ~ It fetches all the rows in a result set. If some tows have already been
extracted from the result set, then it retrieves the remaining rows from the result set.
Exercise 18
CREATE TABLE Hospital (
Hospital_Id INT UNSIGNED NOT NULL, i
Hospital Name TEXT NOT NULL,
Bed_Count INT,
PRIMARY KEY (Hospital_Id) 4
ds
INSERT INTO Hospital (Hospital_Id, Hospital Name, Bed_Count)
VALUES
('I', Mayo Clinic’, 200),
(2, ‘Cleveland Clinic’, 400),
(3', Johns Hopkins', 1000),119 09% —
Exercise 24
Use functions avail
t-wise
lube in pandas to clean the entire dataset, clemen
Ay RE ne
Ee fats ae vols
TC ekente © DS Rowrunn Shab. _—— 7
Penta =} > Nota Ta
Exercise 25
Read data from a website and make sure all the columns have corrected data type. Store this
data as a CSV or Excel file on your computer.
“[erdog fd
aoe ee
Exercise 26
Read the data for cricket IPL statistics for last few years and perform exploratory dat
on this data with an objective of: ;
1. Testing some assumptions that you have
2. Creating a few hypotheses
3. Answering some relevant questions
4. Creating a few charts in PythonA regular expression , also known as regex and somet
expression, is a sequence of various characters wh;
textual data. In Python, such patterns are used by s
such as "find" or “find and replace” on text strings,
a built-in package called re, which can be used to
Basic functions in re
times: referred by Programmers as rational
lich specifies a Search pattern in ‘normal
tring-searching algorithms for operations
including for input validation. Python has
work with Regular Expressions,
Function
Description
findall
Returns a list containing all matches
search
the string
Returns a Match object if there is a match anywhere ip
split
match
Returns a list where the string has been split at each
sub
Replaces one or many matches with a string
Metacharacters are characters with a special meaning:
Use find all function
to search for multiple matches using regex
Character | Description Example
ou A set of characters *[a-ml
\ ‘Signals a special sequence (can also be used to escape | "\d"
special characters) —_
a Any character (except newline character) a oa :
ia Starts with a aS
$ Ends with ie =
* ‘Zero or more occurrences tere
+ One or more: occurences Teo
2 Zero or one oceurrens aad
‘ i ecurrences
0G Exactly the specified number of o See
| Either or hi -
0 ‘Capture and group
Exercise 27Exercise 29
Exercise 30
Write & Python program to find sequences of lowerease letters joined with a underscore
Exercise 31
Write a Python program to search some literals strit
Sample text : The quick brown fox jumps over th
Searched words : 'fox'; dog’, ‘horse! —a
taecein
2 | Programming Concepts and Problem-Solving Us