0% found this document useful (0 votes)
4 views6 pages

DM_Lab_01

The document provides an introduction to Python, covering its characteristics, mathematical and relational operations, logical operations, primitive data types, data structures, variables, comments, and type casting. It includes examples of code snippets for each topic and concludes with practice questions to reinforce learning. The content is aimed at first-year computing students in their second semester.

Uploaded by

akmalfsliit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

DM_Lab_01

The document provides an introduction to Python, covering its characteristics, mathematical and relational operations, logical operations, primitive data types, data structures, variables, comments, and type casting. It includes examples of code snippets for each topic and concludes with practice questions to reinforce learning. The content is aimed at first-year computing students in their second semester.

Uploaded by

akmalfsliit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Faculty of Computing

Year 1 Semester 2 (2025)

IT1160 – Discrete Mathematics Lab Sheet 01

What is Python?

• Python is a high-level, interpreted programming language known for its simplicity


and readability.

• It supports multiple programming paradigms like procedural, object-oriented, and


functional programming.

print(”Hello, World!”)

Mathematical Operations in Python

• Python supports standard mathematical operations like addition, subtraction, mul-


tiplication, division, and more.

• These can be performed using arithmetic operators.

a = 10 + 5

b = 10 − 5

c = 10 ∗ 5

d = 10/5

e = 10%3

f = 2 ∗ ∗3

g = 7//2

1
Relational Operations in Python

• Relational (comparison) operators are used to compare values, returning either True
or False

print(5 > 3)

print(5 < 3)

print(5 == 3)

print(5! = 3)

print(5 >= 3)

print(5 <= 3)

Logical Operations in Python

• Logical operators (and, or, not) are used to combine conditional statements.

print(5 > 3and10 > 5)

print(5 > 3or10 < 5)

print(not(5 > 3))

Primitive Data Types in Python

• Primitive data types are the most basic data types available in Python.

• They include integers, floats, strings, and booleans.

• Integers Ex-10

• Floats Ex-10.5

• Strings Ex-”Alice”

• Boolean Ex-True, False

2
Data Structures in Python

• Python has built-in data structures like lists, tuples, dictionaries, and sets, which
allow for the storage and manipulation of data.

List-

fruits = [”apple”, ”banana”, ”cherry”]

Tuple

coordinates = (10.0, 20.5)

Dictionary

person = {”name”: ”John”, ”age”: 30}

Set

uniquen umbers = {1, 2, 3, 3}

Variables in Python

• A variable is a symbolic name that refers to a value stored in memory.


• Variables in Python are dynamically typed, meaning you don’t need to declare the
type explicitly.

age = 25
name = ”John”
isa ctive = T rue

x = 10
y = 20
print(x + y)

Comments in Python

• Comments are lines in Python code that are ignored by the interpreter.
• They are used to explain the code and improve readability.

# This is a single-line comment


”””

This is a
multi-line comment
”””

x = 10 # This variable stores the age

3
Type Casting in Python

• Type casting is the process of converting one data type to another.

• In Python, you can cast between primitive data types (like int, float, str) and
convert data structures like lists to tuples, sets, etc.

Type Casting for Primitive Data Types

Integer to Float

a = 10
b = float(a)

Float to Integer (truncates the decimal part)

c = 12.34
d = int(c)

String to Integer (only if the string represents a number)

e = ”123”
f = int(e)

Integer to String

g = 100
h = str(g)

Boolean to Integer

i = True
j = int(i)

Integer to Boolean

k=0
l = bool(k)

Type Casting for Data Structures

• list(), tuple(), set(), dict() functions are used for converting the data structures for
specific types.

Ex-
List to Tuple

my list = [1, 2, 3]
my tuple = tuple(my list)

4
Tuple to List

my tuple = (4, 5, 6)
my list = list(my tuple)

List to Set (removes duplicates)

my list = [1, 2, 2, 3]
my set = set(my list)

Set to List

my set = {4, 5, 6}
my list = list(my set)

Practice Questions

1. Write a Python program to,

(a) Add 20 and 15, and print the result.


(b) Subtract 12 from 50, and print the result.
(c) Multiply 8 and 7, and print the result.
(d) Divide 100 by 25, and print the result.
(e) Find the remainder when 17 is divided by 5, and print the result.

2. Write a Python program that,

(a) Compares two numbers (x = 15, y = 10) and prints whether x is greater than
y.
(b) Checks if 25 is equal to 25 and prints the result.
(c) Checks if 30 is not equal to 25 and prints the result.
(d) Compares 10 and 20 using <= and prints the result.

3. Write a Python program to,

(a) Check if both 5 > 3 and 10 > 5 are true, and print the result.
(b) Check if 7 > 9 or 15 > 10 is true, and print the result.
(c) Use not to check if 8 == 8 and print the result.

4. Define variables for the following data types and print them,

(a) An integer x = 100.


(b) A float y = 45.67.
(c) A string name = ”Alice”.
(d) A boolean status = True.

5
5. Write a Python program to,

(a) Create a list of 5 numbers and print the list.


(b) Add a new number to the list and print it.
(c) Access and print the second item in the list.
(d) Remove the last item from the list and print the modified list.

6. Write a Python program to,

(a) Create a tuple with 3 elements and print it.


(b) Access and print the first element of the tuple.
(c) Convert the tuple to a list and print the list.

7. Write a Python program to,

(a) Create a dictionary with at least 3 key-value pairs (Ex- name, age, country).
(b) Retrieve and print the value of a specific key.
(c) Add a new key-value pair to the dictionary and print the updated dictionary.

8. Write a Python program to,

(a) Create a set of numbers and print it.


(b) Add a new number to the set and print it.
(c) Remove a number from the set and print the updated set.
(d) Find the union of two sets and print the result.

9. Write a Python program to,

(a) Convert a float number x = 45.67 to an integer and print the result.
(b) Convert an integer y = 100 to a string and print the result.
(c) Convert a string z = ”25” to an integer and print the result.
(d) Convert a boolean status = False to an integer and print the result.

10. Write a Python program to,

(a) Convert a list [1, 2, 3] to a tuple and print it.


(b) Convert a tuple (4, 5, 6) to a list and print it.
(c) Convert a list [1, 2, 2, 3] to a set (to remove duplicates) and print the result.
(d) Convert a set {4, 5, 6} to a list and print it.

You might also like