Python Lab Manual 01
Python Lab Manual 01
Lab Manual
I. Table of Contents
Lab Breakdown
Lab Contents/Topics
Mid-term exam.
Lab#07: Python Modules and Libraries, Pandas Data Manipulation(CSV, Images ,Text)
Lab#11: Managing Freelance Projects, Financial aspects of freelancing, and building a sustainable
freelance career
Final Exam
pg. 2
II. Lab Outline
Course Introduction
The Python Programming and Freelancing Essentials course offers a comprehensive introduction to Python, covering fundamental syntax,
file handling, exception handling, and data structures. Students will gain practical experience with essential libraries for data analysis, web
development, and automation, building a solid foundation in programming applicable across various fields. Additionally, the course
includes an overview of popular freelancing platforms and best practices for freelancing, helping students leverage their Python skills to
find opportunities and succeed in the gig economy.
Course Contents
The course covers Python fundamentals, gradually progressing to more advanced topics, including data handling with NumPy and Pandas,
data visualization, and machine learning with Scikit-Learn. The curriculum also explores Natural Language Processing and Computer Vision,
culminating in a comprehensive project that integrates all the skills taught in the course. By the end, students will have a solid foundation in
Python programming and practical skills applicable to AI technologies. Additionally, the course includes an overview of popular freelancing
platforms and best practices, equipping students to effectively utilize their Python skills to secure opportunities and thrive in the freelance
marketplace.
1. Eric Matthes, “Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming” No Starch Press, 2023.
2. Charles Severance, “Python for Everybody: Exploring Data in Python 3”, CreateSpace Independent Publishing Platform, 2016.
3. Alison Grade, “The Freelance Bible: Every Thing You Need to Go Solo in Any Industry” Portfolio Penguin, 2020.
Other Sources:
1. https://www.kaggle.com/learn/python
2. https://www.screenskills.com/developing-your-career/freelance-toolkit/core-skills-of-freelancing/
•
Administrative Instruction
▪ According to institute policy, 80% attendance is mandatory to appear in the final examination.
▪ Assignments must be submitted as per instructions mentioned in the assignments.
▪ For queries, kindly follow the office hours in order to avoid any inconvenience.
• Computer Usage
▪ VS code
▪ Google Colab
▪ Freelancing Platforms
Lecture Breakdown
HARDWARE REQUIREMENT
• Core i3 or Above
• 32- or 64-bit computer
• 4 GB RAM
• 3 GB HDD
SOFTWARE REQUIREMENT
pg. 6
Anaconda System Requirements link:
https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html
Weekly Evaluation
Error/exception Handling:
Correctness:
* Accurate methodology: 1
pg. 7
1. Lab#01 - Introduction to Python
Learning Objectives
➢ Installation of Anaconda Software
➢ Environment of Jupyter and Run the Program in Jupyter
➢ To learn the basic data types and variables
➢ To learn the mathematical operators
➢ To learn the input and output
Outcomes:
➢ Students should be able to install the Anaconda Software
➢ Students should be able Run their program in Jupyter
• Students should be able to know Python variables, their types and arithmetic operators.
➢ Students should be able to use input and print methods in the programs.
Follow the steps below to install the Anaconda distribution of Python on Windows.
Steps:
• Visit Anaconda.com/downloads
• Select Windows
• Download the .exe installer
• Open and run the .exe installer
• Open the Anaconda Navigator and run some Python code
pg. 8
1.2.1 Visit Anaconda.com/downloads
Go end of the page and the Anaconda Downloads Page will look something like this:
1.2.3 Downloads
pg. 9
Download the most recent Python 3 release. If you are unsure if your computer is running a 64-bit or 32-bit version of
Windows, select 64-bit as 64-bit Windows is most common.
You may be prompted to enter your email. You can still download Anaconda if you click [No Thanks] and don't enter your
Work Email address.
The download is quite large (over 500 MB) so it may take a while for Anaconda to download.
pg. 10
1.2.4 Open and run the installer
Once the download is complete, open and run the .exe installer.
At the beginning of the installation, you need to click Next to confirm the installation.
pg. 11
At the Advanced Installation Options screen, I recommend that you do not check "Add Anaconda to my PATH environment
variable".
1.2.5 Open the Anaconda Navigator from the Windows start menu
After the installation of Anaconda is complete, you can go to the Windows start menu and search the Navigator and select
the Anaconda Navigator.
pg. 12
After installation the Anaconda Navigator looks like:
Open the Notebook from Anaconda Navigator and launch the Jupyter notebook.
pg. 13
When Jupyter notebook in load in any browser. Then click on new and create a notebook file by clicking on python3.9 (your
python version)
pg. 14
After creating the notebook, write print (“hello”) and click on run and check the output.
pg. 15
You’ll learn about several basic numeric, string, and Boolean types that are built into Python. By the end of this lab, you’ll
be familiar with what objects of these types look like, and how to represent them.
You’ll also get an overview of Python’s built-in functions. These are pre-written chunks of code you can call to do useful
things.
1.4.1 Integers
In Python 3, there is effectively no limit to how long an integer value can be. Of course, it is constrained by the amount of
memory your system has, as are all things, but beyond that an integer can be as long as you need it to be:
Python interprets a sequence of decimal digits without any prefix to be a decimal number:
The following strings can be prepended to an integer value to indicate a base other than 10:
For example:
pg. 16
For more information on integer values with non-decimal bases, see the following Wikipedia sites: Binary, Octal, and
Hexadecimal. The underlying type of a Python integer, irrespective of the base used to specify it, is called int. Use type
method to check the type of any variable.
The float type in Python designates a floating-point number. float values are specified with a decimal point. Optionally, the
character e or E followed by a positive or negative integer may be appended to specify scientific notation:
pg. 17
1.4.3 Complex Numbers
1.4.4 Strings
Strings are sequences of character data. The string type in Python is called str. String literals may be delimited using either
single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the
string:
What if you want to include a quote character as part of the string itself? Your first impulse might be to try something like
this:
pg. 18
As you can see, that doesn’t work so well. The string in this example opens with a single quote, so Python assumes the next
single quote, the one in parentheses which was intended to be part of the string, is the closing delimiter. The final single
quote is then a stray and causes the syntax error shown.
If you want to include either type of quote character within the string, the simplest way is to delimit the string with the other
type. If a string is to contain a single quote, delimit it with double quotes and vice versa:
Sometimes, you want Python to interpret a character or sequence of characters within a string differently. This may occur
in one of two ways:
• You may want to suppress the special interpretation that certain characters are usually given within a string.
• You may want to apply special interpretation to characters in a string which would normally be taken literally.
You can accomplish this using a backslash (\) character. A backslash character in a string indicates that one or more
characters that follow it should be treated specially. (This is referred to as an escape sequence, because the backslash causes
the subsequent character sequence to “escape” its usual meaning.)
pg. 19
You have already seen the problems you can come up against when you try to include quote characters in a string. If a string
is delimited by single quotes, you can’t directly specify a single quote character as part of the string because, for that string,
the single quote has special meaning—it terminates the string:
Specifying a backslash in front of the quote character, in a string “escape” it and causes Python to suppress its usual special
meaning. It is then interpreted simply as a literal single quote character:
The following is a table of escape sequences which cause Python to suppress the usual special interpretation of a character
in a string:
pg. 20
Ordinarily, a newline character terminates line input. So, pressing Shift + Enter (jupyter) in the middle of a string will cause
Python to think it is incomplete:
To break up a string over more than one line, include a backslash before each newline, and the newlines will be ignored:
Next, suppose you need to create a string that contains a tab character in it. Some text editors may allow you to insert a tab
character directly into your code. But many programmers consider that poor practice, for several reasons:
• The computer can distinguish between a tab character and a sequence of space characters, but you can’t. To a human
reading the code, tab and space characters are visually indistinguishable.
pg. 21
• Some text editors are configured to automatically eliminate tab characters by expanding them to the appropriate
number of spaces.
• Some Python REPL environments will not insert tabs into code.
In Python (and almost all other common computer languages), a tab character can be specified by the escape sequence \t:
The escape sequence \t causes the t character to lose its usual meaning, that of a literal t. Instead, the combination is
interpreted as a tab character.
Here is a list of escape sequences that cause Python to apply special meaning instead of interpreting literally:
Examples:
pg. 22
This type of escape sequence is typically used to insert characters that are not readily generated from the keyboard or are
not easily readable or printable.
A raw string literal is preceded by r or R, which specifies that escape sequences in the associated string are not translated.
The backslash character is left in the string:
There is yet another way of delimiting strings in Python. Triple-quoted strings are delimited by matching groups of three
single quotes or three double quotes. Escape sequences still work in triple-quoted strings, but single quotes, double quotes,
and newlines can be included without escaping them. This provides a convenient way to create a string with both single and
double quotes in it:
Because newlines can be included without escaping them, this also allows for multiline strings:
pg. 23
the Python Boolean type has only two possible values:
• True
• False
No other value will have bool as its type. You can check the type of True and False with the built-in type():
1.5 Variables
Think of a variable as a name attached to a particular object. In Python, variables need not be declared or defined in advance,
as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it.
Assignment is done with a single equals sign (=):
This is read or interpreted as “n is assigned the value 300.” Once this is done, n can be used in a statement or expression,
and its value will be substituted:
Just as a literal value can be displayed directly from the jupyter notebook cell in a REPL session without the need for print(),
so can a variable:
Later, if you change the value of n and use it again, the new value will be substituted instead:
pg. 24
Python also allows chained assignment, which makes it possible to assign the same value to several variables
simultaneously:
The chained assignment above assigns 300 to the variables a, b, and c simultaneously.
In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific
data type, and any value assigned to it during its lifetime must always have that type.
Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then
later re-assigned a value of a different type:
What actually happens when you make a variable assignment? This is an important question in Python, because the answer
differs somewhat from what you’d find in many other programming languages.
Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a
specific type or class.
pg. 25
Consider this code:
When presented with the statement print (300), the interpreter does the following:
A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable,
you can refer to the object by that name. But the data itself is still contained within the object.
For example:
This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.
pg. 26
What happens when it is executed? Python does not create another object. It simply creates a new symbolic name or
reference, m, which points to the same object that n points to.
Now Python creates a new integer object with the value 400, and m becomes a reference to it.
Now Python creates a string object with the value "foo" and makes n reference that.
There is no longer any reference to the integer object 300. It is orphaned, and there is no way to access it.
An object’s life begins when it is created, at which time at least one reference to it is created. During an object’s lifetime,
additional references to it may be created, as you saw above, and references to it may be deleted as well. An object stays
alive, as it were, so long as there is at least one reference to it.
pg. 27
When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python
will eventually notice that it is inaccessible and reclaim the allocated memory so it can be used for something else. In
computer lingo, this process is referred to as garbage collection.
In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will
have the same identifier during any period in which their lifetimes overlap. Once an object’s reference count drops to zero
and it is garbage collected, as happened to the 300 object above, then its identifying number becomes available and may be
used again.
The built-in Python function id () returns an object’s integer identifier. Using the id () function, you can verify that two
variables indeed point to the same object:
After the assignment m = n, m and n both point to the same object, confirmed by the fact that id(m) and id(n) return the
same number. Once m is reassigned to 400, m and n points to different objects with different identities.
The examples you have seen so far have used short, terse variable names like m and n. But variable names can be more
verbose. In fact, it is usually beneficial if they are because it makes the purpose of the variable more evident at first glance.
Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits
(0-9), and the underscore character (_). An additional restriction is that, although a variable name can contain digits, the
first character of a variable name cannot be a digit.
But this one is not, because a variable name can’t begin with a digit:
pg. 28
Note that case is significant. Lowercase and uppercase letters are not the same. Use of the underscore character is significant
as well. Each of the following defines a different variable:
There is nothing stopping you from creating two different variables in the same program called age and Age, or for that
matter agE. But it is probably ill-advised.
It would certainly be likely to confuse anyone trying to read your code, and even you yourself, after you’d been away from
it awhile.
It is worthwhile to give a variable a name that is descriptive enough to make clear what it is being used for.
For example, suppose you are tallying the number of people who have graduated college. You could conceivably choose
any of the following:
pg. 29
The most used methods of constructing a multi-word variable name are the last three examples:
• Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see. (Presumably,
it struck someone at some point that the capital letters strewn throughout the variable name vaguely resemble camel
humps.)
Example: numberOfCollegeGraduates
• Pascal Case: Identical to Camel Case, except the first word is also capitalized.
Example: NumberOfCollegeGraduates
There is one more restriction on identifier names. The Python language reserves a small set of keywords that designate
special language functionality. No object can have the same name as a reserved word. Python keywords mention below:
You can check python reserve keywords list by importing keyword mention below:
Trying to create a variable with the same name as any reserved word results in an error.
pg. 30
1.7 Arithmetic Operations
In Python, operators are special symbols that indicate that some sort of computation should be performed. The values that
an operator acts on are called operands.
Here is an example:
In this case, the + operator adds the operands a and b together. An operand can be either a literal value or a variable that
references an object:
A sequence of operands and operators, like a + b - 5, is called an expression. Python supports many operators for combining
data objects into expressions.
pg. 31
Here are some examples of these operators in use:
The result of standard division (/) is always a float, even if the dividend is evenly divisible by the divisor:
pg. 32
When the result of floor division (//) is positive, it is as though the fractional portion is truncated off, leaving only the integer
portion. When the result is negative, the result is rounded down to the next smallest (greater negative) integer:
pg. 33
Here are examples of the comparison operators in use:
The logical operators (not, or, and) modify and join together expressions evaluated in Boolean context to create more
complex conditions.
pg. 34
Examples:
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the
same memory location:
1.8 Tasks:
1. Chandler is creating a sarcastic bot in Python and wants to include both single quotes and double quotes in
pg. 35
the bot's witty responses without causing syntax errors. How can he handle this using Python's string formatting
features?
2. Monica is dividing her famous lasagna equally among 6 friends. If she has made 28 pieces, what Python
operator should she use to determine how many pieces each person gets, and how many are left over?
3. Phoebe has a list of her songs: ["Smelly Cat", "Holiday Song", "Little Black Curly Hair"].
How can she use Python to find the second song on her list?
4. Write a Python statement to check if both of the following are true: Joey loves food, and Phoebe loves
singing. Use Boolean operators.
pg. 36