Department of Computer Science National Textile University Faisalabad Data Science - Lab Manual
Department of Computer Science National Textile University Faisalabad Data Science - Lab Manual
Department of Computer Science National Textile University Faisalabad Data Science - Lab Manual
Outline:
• Introduction to Python
• Variables and Types
• Data Structures in Python
• Functions & Packages
• Numpy package
1.1. Introduction to Python
Python is an easy to learn, powerful programming language. It has efficient high-level
data structures and a simple but effective approach to object-oriented programming.It
was created by Guido van Rossum during 1985- 1990. Python is very beginner-friendly.
The syntax (words and structure) is extremely simple to read and follow, most of which
can be understood even if you do not know any programming. Let’s take a look at one
example of this
Example 1.1:
Garage = "Ferrari", "Honda", "Porsche", "Toyota"
"print()" is a built-in Python function that will output some text to the console.
Looking at the code about cars in the garage, can you guess what will happen? You
probably have a general idea. For each_car in the garage, we're going to do something.
What are we doing? We are printing each car.
Since "printing" outputs some text to the "console," you can probably figure out that the
console will say something like "Ferrari, Honda, Porsche, Toyota."
Spyder IDE
Spyder is a powerful scientific environment written in Python, for Python, and designed
by and for scientists, engineers and data analysts. It offers a unique combination of the
advanced editing, analysis, debugging, and profiling functionality of a comprehensive
Department of Computer Science
National Textile University Faisalabad
Data Science – Lab Manual
development tool with the data exploration, interactive execution, deep inspection, and
beautiful visualization capabilities of a scientific package.
Beyond its many built-in features, its abilities can be extended even further via its plugin
system and API. Furthermore, Spyder can also be used as a PyQt5 extension library,
allowing developers to build upon its functionality and embed its components, such as
the interactive console, in their own PyQt software.
Some of the components of spyder include:
• Editor, work efficiently in a multi-language editor with a function/class browser,
code analysis tools, automatic code completion
• Ipython console, harness the power of as many IPython consoles as you like
within the flexibility of a full GUI interface; run your code by line, cell, or file; and
render plots right inline.
• Variable explorer,Interact with and modify variables on the fly: plot a
histogram or time series, edit a data frame or Numpy array, sort a collection, dig
into nested objects, and more!
• Debugger,Trace each step of your code's execution interactively.
• Help,instantly view any object's docs, and render your own.
Here is how Spyder IDE looks like (Windows edition) in action:
The first thing to note is how the Spyder app is organized. The application includes
multiple separate windows (marked with red rectangles), each of which has its own tabs
Department of Computer Science
National Textile University Faisalabad
Data Science – Lab Manual
(marked with green rectangles). You can change which windows you prefer to have open
from the View -> Windows and Toolbars option. The default configuration has the Editor,
Object inspector/Variable explorer/File explorer, and Console/History log windows open
as shown above.
The Console is where python is waiting for you to type commands, which tell it to load
data, do math, plot data, etc. After every command, which looks like >>> command, you
need to hit the enter key (return key), and then python may or may not give some output.
The Editor allows you to write sequences of commands, which together make up a
program. The History Log stores the last 100 commands you've typed into the Console.
The Object inspector/Variable explorer/File explorer windows are purely informational -
- if you watch what the first two display as we go through the tutorial, you'll see that they
can be quite helpful.
In almost every single Python program you write, you will have variables. Variables act as
placeholders for data. They can aid in short hand, as well as with logic, as variables can
change, hence their name.
Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable. The equal sign
(=) is used to assign values to variables.
Variables help programs become much more dynamic, and allow a program to always
reference a value in one spot, rather than the programmer needing to repeatedly type it
out, and, worse, change it if they decide to use a different definition for it.
Variables can be called just about whatever you want. You wouldn't want them
to conflictwith function names, and they also cannot start with a number.
Example 1.2:
weight=55
height=166
BMI=weight/(height*height)
print(BMI)
In this case, we will have a 0.001995935549426622printed out to console. Here, we were
able to store integers and their manipulations to different variables.
We can also find out the type of any variable by using type function.
print(type(BMI))
This will return the type of variable as <class 'float'>
Department of Computer Science
National Textile University Faisalabad
Data Science – Lab Manual
1.3. Data Structures in Python
The most basic data structure in Python is the sequence. Each element of a sequence is
assigned a number - its position or index. The first index is zero, the second index is one,
and so forth.
There are certain things you can do with all sequence types. These operations include
indexing, slicing, adding, multiplying, and checking for membership. In addition, Python
has built-in functions for finding the length of a sequence and for finding its largest and
smallest elements.
1.3.1. Python Lists
The list is a most versatile datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets. Important thing about a list is
that items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square
brackets. For example −
Example 1.3:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
List indices start at 0, and lists can be sliced, concatenated and so on.
List of lists can also be created
Example 1.4:
weight=[55,44,45,53]
height=[166, 150, 144,155]
demo_list=[weight, height]
print(demo_list)
1.3.2. Accessing Values in Lists
To access values in lists, use the square brackets for slicing along with the index or indices
to obtain value available at that index. For example −
Example 1.5:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
Example 1.8:
def printme( str ):
"This prints a passed string into this function"
print (str)
return;
#Calling function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower
rank,
# while using only slices yields an array of the same rank as
the
# original array:
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"
Question 3:
• Calculate the product of savings and factor. Store the result in year1.
• What do you think the resulting type will be? Find out by printing out the type
of year1.
• Calculate the sum of desc(string) and desc and store the result in a new
variable doubledesc.
• Print out doubledesc. Did you expect this?
• savings = 100, factor = 1.1, desc = "compound interest"
Python Lists
Question 4:
• Create a list, areas, that contains the area of the hallway (hall), kitchen (kit),
living room (liv), bedroom (bed) and bathroom (bath), in this order. Use the
predefined variables.
• Print areas with the print() function.
Question 5:
Department of Computer Science
National Textile University Faisalabad
Data Science – Lab Manual
• Finish the line of code that creates the areas list such that the list first contains the
name of each room as a string, and then its area. More specifically, add the
strings "hallway", "kitchen" and "bedroom" at the appropriate locations.
• Print areas again; is the printout more informative this time?
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Adapt list areas
areas = [hall, kit, "living room", liv, bed, "bathroom", bath]
# Print areas
Question 6:
As a data scientist, you'll often be dealing with a lot of data, and it will make sense to group
some of this data.
Instead of creating a flat list containing strings and floats, representing the names and
areas of the rooms in your house, you can create a list of lists. The script on the right can
already give you an idea.
• Finish the list of lists so that it also contains the bedroom and bathroom data. Make
sure you enter these in order!
• Print out house; does this way of structuring your data make more sense?
• Print out the type of house. Are you still dealing with a list?
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
Question 9:
• Use slicing to create a list, downstairs, that contains the first 6 elements of areas.
• Do a similar thing to create a new variable, upstairs, that contains the last 4
elements of areas.
• Print both downstairs and upstairs using print()
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0,
"bedroom", 10.75, "bathroom", 9.50]
Department of Computer Science
National Textile University Faisalabad
Data Science – Lab Manual
# Use slicing to create downstairs
# Calculate C
C =
# Calculate A
A =
# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))
Question 18:
• Perform a selective import from the math package where you only import
the radians function.
• Calculate the distance travelled by the Moon over 12 degrees of its orbit. Assign the
result to dist. You can calculate this as r∗ϕr∗ϕ, where rr is the radius and ϕϕ is the
angle in radians. To convert an angle in degrees to an angle in radians, use
the radians() function, which you just imported.
• Print out dist.
# Definition of radius
r = 192500
# Import numpy
import numpy as np
# Import numpy
import numpy as np
# Import numpy
import numpy as np
2D Numpy Arrays
Question 23: First 2D numpy array
• Use np.array() to create a 2D Numpy array from baseball. Name it np_baseball.
• Print out the type of np_baseball.
• Print out the shape attribute of np_baseball. Use np_baseball.shape.
# Create baseball, a list of lists
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
# Import numpy
import numpy as np
# Import numpy
import numpy as np
# Import numpy
import numpy as np