Final Lab Manual
Final Lab Manual
MANUAL
Course: Programming Language for Business
Analytics
Department of Management
Sciences
In this course, we are going to learn and practice concepts related to python. In this lab manual, to demonstrate
various python concepts, we are going to use Jupiter Notebook within VS Code
I. Tools/Software Requirements
Following are the software that we will need for this lab manual:
Python
VS Code
A. Installing Python
First, check whether Python is installed on your system. Open a command window by entering command into the
Start menu and clicking the Command Prompt app. In the terminal window, enter python in lowercase. If you get
a Python prompt (>>>) in response, Python is installed on your system. If you see an error message telling you
that python is not a recognized command, or if the Microsoft store opens, Python isn’t installed. Close the
Microsoft store if it opens; it’s better to download an official installer than to use Microsoft’s version.
If Python is not installed on your system, or if you see a version earlier than Python 3.9, you need to download a
Python installer for Windows. Go to https://python.org and hover over the Downloads link. You should see a
button for downloading the latest version of Python. Click the button, which should automatically start
downloading the correct installer for your system. After you’ve downloaded the file, run the installer. Make sure
you select the option Add Python to PATH, which will make it easier to configure your system correctly. Figure
1-1 shows this option selected.
C. Installing VS Code
You can download an installer for VS Code at https://code.visu alstudio.com. Click the Download for Windows
button and run the installer.
Configuring VS Code
VS Code is a great text editor, with some features that you'd typically see in an IDE. There are some helpful
recommendations for configuring VS Code and using it efficiently in Appendix B of the book. This section shows
some of those same configuration steps, with more screenshots.
Simplifying output
The output we're really interested in is the single line Hello Python world! The rest of the output shows where the
file is being saved, and the path to the Python interpreter that's being used to run the program. At this point you
probably just want to see the output of your program.
To see just the output, do the following:
Close all open tabs in VS Code, and quit VS Code.
Launch VS Code again and open the folder that contains the Python files you're working on.
Click the Run/ Debug icon:
A. Purpose
The purpose of this lab is to introduce the different types of data that are used in python programming.
B. Outcomes
After completing this lesson, students should be able to do the following:
Identify different data types of data
Run different methods on data types
C. Instructor Note
As a pre-lab activity, please attend the theory classes on data types.
D. Stage J (Journey)
Data Types
int, float,
Numeric holds numeric values
complex
Methods Description
isnumeric(
checks numeric characters
)
E. Apply
Lab Activity 1: identify the type of numeric data
This lab activity demonstrates the use of type method to identify the data type which is numeric.
Solution:
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 2.0
print(num2, 'is of type', type(num2))
num3 = 1+2j
print(num3, 'is of type', type(num3))
Output
Python
Python for beginners
In the above example, we have created string-type variables: name and message with values 'Python' and 'Python
for beginners' respectively.
Here, we have created a string variable named string1. The variable is initialized with the string Python
Programming.# create string type variables
name = "Python"
print(name)
In the above example, we have created string-type variables: name and message with values "Python" and "I love
Python" respectively. Here, we have used double quotes to represent strings but we can use single quotes too.
Access String Characters in Python
We can access the characters in a string in three ways.
Indexing: One way is to treat strings as a list and use index values.
Negative Indexing: Similar to a list, Python allows negative indexing for its strings. For example,
greet = 'hello'
greet = 'hello'
Slicing: Access a range of characters in a string by using the slicing operator colon :. For example,
greet = 'Hello'
Note: If we try to access an index out of the range or use numbers other than an integer, we will get errors.
Python Strings are immutable
In Python, strings are made immutable so that programmers cannot alter the contents of the object (even by
mistake).
message = 'Hola Amigos'
message[0] = 'H'
print(message)
However, we can assign the variable name to a new string. For example,
message = 'Hola Amigos'
print(message)
Run Code
In the above example, anything inside the enclosing triple-quotes is one multiline string.
A. Purpose
The purpose of this lab is to practice the concepts of list hat are used in python programming.
B. Outcomes
After completing this lesson, students should be able to do the following:
Identify and know the different list methods
C. Instructor Note
As a pre-lab activity, please attend the theory classes on data types.
D. Stage J (Journey)
The list functions are of the following types
Commonly Used Python List Built-in Functions
range() returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number
index() returns the index of the first element with the specified value
E. Apply
1. Create a list of five of your favorite foods.
2. From your list of favorite foods, select the second item.
3. "my name is john".upper() returns the string “MY NAME IS JOHN”. Given that fact, create an
uppercase list of your favorite foods. Hint: You would need to convert your list in to string first.
4. Add another food item in your favorite food list.
5. Delete the first element of your list.
6. Using the .pop() command, pop the food that you don’t like anymore and place it in a variable
called not_liked
7. Sort your list in reverse order.
8. Use a function to get the length of the letters list. Display it.
9. Get the last item in the list, using a positive index.
10. Get the last item in the list, using a negative index.
11. How could you return a list that looks like the list below:
['G', 'F', 'E', 'D', 'C', 'B', 'A']
F. Assignment 1
1.Guest List: If you could invite anyone, living or deceased, to dinner, who would you invite?
Make a list that includes at least three people you’d like to invite to dinner.
Then use your list to print a message to each person, inviting them to dinner.
2.Changing Guest List: You just heard that one of your guests can’t make the dinner, so you need to send out a
new set of invitations. You’ll have to think of someone else to invite.
Start with your program from Exercise 1.
Add a print() call at the end of your program, stating the name of the guest who can’t make it.
Modify your list, replacing the name of the guest who can’t make it with the name of the new person you are
inviting.
Print a second set of invitation messages, one for each person who is still in your list.
3.More Guests: You just found a bigger dinner table, so now more space is available.
Think of three more guests to invite to dinner. Start with your program from Exercise 1 or 2
A. Purpose
The purpose of this lab is to introduce the for loops that are used in python programming.
B. Outcomes
After completing this lesson, students should be able to do the following:
Understand application for Python Loops
C. Instructor Note
As a pre-lab activity, please attend the theory classes on for loops.
D. Stage J (Journey)
In Python programming, we use for loops to repeat some code a certain number of times. It allows us to execute a
statement or a group of statements multiple times by reducing the burden of writing several lines of code.
print(i)
print(j)
for x in AnimalList:
print(x)
b2 = [1,2,3]
print(i,j)
for f in flowers:
print(f)
else:
print('Done')
Qs. What is an else statement in python: Google and write your answer here.
list2 = ['Tomatoes','Potatoes','Carrots','Cucumbers']
for y in list2:
print(x,y)
A nested loop has one loop inside of another. These are typically used for working with two dimensions such as
printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop runs
many times inside the outer loop.
for v in vehicles:
if v == "Bus":
break
print(v)
for v in vehicles:
if v == "Bus":
continue
print(v)
count = 0
for n in numbers:
count += 1
print(count)
list2 = []
for i in list1:
list2.append(i)
print(list2)
for i in range(5,20,5):
print(i)
print(i)
Q. Quiz 1
1. Counting to Twenty: Use a for loop to print the numbers from 1 to 20, inclusive.
2. Summing a Million: Make a list of the numbers from one to one million, and then use min() and max() to make
sure your list actually starts at one and ends at one million. Also, use the sum() function to see how quickly
Python can add a million numbers.
3. Odd Numbers: Use the third argument of the range() function to make a list of the odd numbers from 1 to 20.
Use a for loop to print each number.
4. Threes: Make a list of the multiples of 3, from 3 to 30. Use a for loop to print the numbers in your list.
5. Cubes: A number raised to the third power is called a cube. For example, the cube of 2 is written as 2**3 in
Python. Make a list of the first 10 cubes (that is, the cube of each integer from 1 through 10), and use a for loop
to print out the value of each cube.
6. Cube Comprehension: Use a list comprehension to generate a list of the first 10 cubes.
A. Purpose
The purpose of this lab is to introduce the concept of tuples that are used in python programming.
B. Outcomes
After completing this lesson, students should be able to do the following:
Understand tuples and their usage
C. Instructor Note
As a pre-lab activity, please attend the theory classes on tuples
D. Stage J (Journey)
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and
Dictionary, all with different qualities and usage.
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses,
whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-
separated values between parentheses also. For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing −
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there is only one value −
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
We can use the type() function to know which class a variable or a value belongs to.
var1 = ("hello")
print(type(var1)) # <class 'str'>
# Parentheses is optional
var3 = "hello",
print(type(var3)) # <class 'tuple'>
Here,
("hello") is a string so type() returns str as class of var1 i.e. <class 'str'>
("hello",) and "hello", both are tuples so type() returns tuple as class of var1 i.e. <class 'tuple'>
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value
available at that index.
Slicing
We can access a range of items in a tuple by using the slicing operator colon
print(my_tuple.count('p')) # prints 2
print(my_tuple.index('l')) # prints 3
Slicing
We can access a range of items in a tuple by using the slicing operator colon
We use the in keyword to check if an item exists in the tuple or not. For example,
list1 = [0, 1, 2]
print(tuple(list1))
Since tuples are quite similar to lists, both of them are used in similar situations.
1. We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data types.
3. If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
E. Apply
1. Write a Python program to create a tuple
3. Write a Python program to get the 4th element from the last element of a tuple
8. Write a program to copy elements 44 and 55 from the following tuple into a new tuple.
expected output