Data Mining Lab 01
Data Mining Lab 01
2 Python Programming
Python is an interpreted high-level general-purpose programming language. Python’s design philosophy priori-
tizes code readability, as evidenced by its extensive use of indentation. Its language elements and object-oriented
approach are aimed at assisting programmers in writing clear, logical code for both small and large-scale projects.
3 Basics of Python
Python is a high-level, dynamically typed multi-paradigm programming language. Python code is often said
to be almost like pseudo-code, since it allows you to express very powerful ideas in very few lines of code while
being very readable.
1 print("Hello world!")
2
3 # Hello world with a variable
4 msg = "Hello world!"
5 print(msg)
6
7 # Concatenation (combining strings)
8 first_name = ’albert’
9 last_name = ’einstein’
10 full_name = first_name + ’ ’ + last_name
11 print(full_name)
3.1.2 Input/Output
Output of the program is given below.
Hello world!
Hello world!
albert einstein
3.1.3 Casting
Casting is a method for specifying the data type of a variable. The type() method is used to find the data
type of a variable.
<class ’str’>
<class ’int’>
<class ’float’>
All input is stored as a string. Then it has to type cast to the requited data type
3.2.2 Input/Output
Output of the programs is given below.
3.3 Operators
Variables are used to store values. A string is a series of characters, surrounded by single or double quotes.
1 x = 10
2 y = 5
3
4 print(x + y) # Addition
5
6 print(x − y) # Subtraction
7
8 print(x * y) # Multiplication
9
10 print(x / y) # Division
11
12 print(x % y) # Modulus
13
14 print(x ** y) # Exponentiation
15
16 print(x // y) # Floor division
1 x = 10
2 y = 5
3
4 print(x == y) # Equal
5
6 print(x != y) # Not equal
7
8 print(x > y) # Greater than
9
10 print(x < y) # Less than
11
12 print(x >= y) # Greater than or equal to
13
14 print(x <= y) # Less than or equal to
1 x = 10
2
3 print(x < 5 and x < 10) # Returns True if both statements are true
4
5 print(x < 5 or x < 4) # Returns True if one of the statements is true
6
7 print(x < 5 and x < 10) # Reverse the result, returns False if the result is
true
3.3.5 Lab Task (Please implement yourself and show the output to the instructor)
• Write a Python program which accepts the user’s first and last name and print them in a single line as
full name.
• Write a Python program that accepts an integer (n) and computes the value of n + nn + nnn.
• Write a Python program which accepts the radius of a circle from the user and compute the area.
• Write a Python program to find whether a given number (accept from the user)
3.4 List
A list stores a series of items in a particular order. You access items using an index, or within a loop.
1 # Create a List
2 thislist = ["apple", "banana", "cherry"]
3 print(thislist)
4
5 # Lists allow duplicate values:
6 thislist = ["apple", "banana", "cherry", "apple", "cherry"]
7 print(thislist)
3.4.2 Input/Output
Output of the program is given below.
3.4.5 Input/Output
Output of the program is given below.
3
5
3.4.8 Input/Output
Output of the program is given below.
apple
cherry
3.4.11 Input/Output
Output of the program is given below.
3.4.14 Input/Output
Output of the program is given below.
3.4.20 Input/Output
Output of the program is given below.
3.4.23 Input/Output
Output of the program is given below.
3.4.25 Lab Task (Please implement yourself and show the output to the instructor)
• Write a Python program which accepts a sequence of comma-separated numbers from user and generate
a list with those numbers.
• Write a Python program to display the first and last colors from the following list. Go to the editor
colorList == ["Red","Green","White" ,"Black"]
3.5.1 If statement
1 a = 10
2 b = 20
3 if b > a:
4 print("b is greater than a")
Python uses indentation (the white space at the starting of a line) to define scope in its code. Curly brackets
are commonly used in other computer languages for this reason.
1 a = 20
2 b = 10
3 if b > a:
4 print("b is greater than a")
5 elif a == b:
6 print("a and b are equal")
7 else:
8 print("a is greater than b")
3.5.3 Input/Output
Output of the program is given below.
a is greater than b
3.5.4 Nested If Statement
1 x = 15
2
3 if x > 5:
4 print("Above ten,")
5 if x > 10:
6 print("and also above 20!")
7 else:
8 print("but not above 20.")
1 a = 20
2 b = 5
3
4 if a > b: print("a is greater than b")
1 a = 2
2 b = 3
3
4 print("A") if a > b else print("B")
3.5.7 Lab Task (Please implement yourself and show the output to the instructor)
• Write a Python program to find the largest among three numbers.
4 Discussion Conclusion
Based on the focused objective(s) to understand about the python program, the additional lab exercise made
me more confident towards the fulfilment of the objectives(s).
6 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.