0% found this document useful (0 votes)
2 views

python file

Uploaded by

Ayesha Nagma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python file

Uploaded by

Ayesha Nagma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

2323007

PRACTICAL
of
“PYTHON LAB-I”
SUBJECT CODE: PC-CS-AIML-217LA

SUBMITTED IN PARTIAL FULFILLMENT OF THE


REQUIREMENTS FOR THE AWARD OF
BACHELORS OF TECHNOLOGY
(B.TECH.) IN
Artificial Intelligence and Machine Learning
(SESSION: 2023-27)

SUBMITTED BY:
AYESHA NAGMA
2323007

UNDER THE SUPERVISION OF:


Er. SHIVANI
Assistant Professor AIML Department, ACE

Department of Artificial Intelligence and Machine Learning


Ambala College of Engineering and Applied Research, Devsthali, Ambala
(Haryana)
2323007

PRACTICAL-1
AIM: Write a program to demonstrate basic data types in python.
SOFTWARE USED: vs code
THEORY:
1. Numeric Types:
a. int: Integer values (e.g., 5, -3).
b. float: Floating-point numbers (e.g., 3.14, -0.001).
c. complex: Complex numbers (e.g., 2+3j).
2. Boolean Type:
a. bool: Represents True or False.
3. Binary Types:
a. bytes: Immutable sequence of bytes (e.g., b'hello').
b. bytearray: Mutable sequence of bytes

SOU
RCE CODE:
# 1. Numeric Types
# int
x_int = 5
print("Integer:", x_int)

# float
x_float = 3.14
print("Float:", x_float)

# complex
x_complex = 2 + 3j
print("Complex:", x_complex)
# 5. Boolean Type
x_bool = True
print("Boolean:", x_bool)

# 6. Binary Types
# bytes
x_bytes = b"Hello"
print("Bytes:", x_bytes)

# bytearray
x_bytearray = bytearray(5)
print("Bytearray:", x_bytearray)

OUTPUT:
2323007

PRACTICAL-2
AIM: Write a program to implement input, output operations and logical, mathematical
operations.

SOFTWARE USED: vs code


THEORY:
 Logical Operations
Logical operators are used to combine or compare conditional expressions and return True or
False.
and: Returns True if both operands are True.
or: Returns True if at least one operand is True.
not: Reverses the result (True becomes False and vice versa).
 Mathematical Operations
Python provides common mathematical operations:
Addition (+): Adds two values.
Subtraction (-): Subtracts the second value from the first.
Multiplication (*): Multiplies two values.
Division (/): Divides two values and returns a float.
Floor Division (//): Divides and returns the integer value (ignores the remainder).
Modulus (%): Returns the remainder of division.
Exponentiation ()**: Raises one value to the power of another
SOURCE CODE:
# Input example
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # converting string input to int
print("Hello,", name, "you are", age, "years old.")
# Output example
print("This is an example of output.")

# Logical Operations example


a = True
b = False

print(a and b) # False


print(a or b) # True
print(not a) # False

# Mathematical Operations example


x = 10
y=3
print("Addition:", x + y) # 13
print("Subtraction:", x - y) #7
print("Multiplication:", x * y) # 30
print("Division:", x / y) # 3.3333
print("Floor Division:", x // y) # 3
print("Modulus:", x % y) #1
print("Exponentiation:", x ** y) # 1000 (10^3)

OUTPUT:
2323007
PRACTICAL-3
AIM: Write a program for checking whether the given number is an even number or not.
SOFTWARE USED: vs code.
PROCEDURE:
Input: The user is asked to input a number.
Condition: The program checks whether the number is divisible by 2 using the modulus operator
(%).
Output: If the remainder is 0, the number is even; otherwise, it is odd.

SOURCE CODE:
# Program to check if a number is even or not
# Input: Get the number from the user
number = int(input("Enter a number: "))
# Check if the number is even
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is not an even number.")

OUTPUT:
2323007

PRACTICAL-4
AIM: Write a program to demonstrate string, list, tuple and dictionaries in python.
SOFTWARE USED: vs code
PROCEDURE:
 String Demo: Demonstrates string operations like converting to uppercase, slicing,
and concatenation.
 List Demo: Demonstrates list operations such as appending, slicing, and removing
elements.
 Tuple Demo: Shows how to access and slice elements in a tuple (tuples are
immutable, so no append or remove).
 Dictionary Demo: Demonstrates dictionary operations like accessing, updating,
adding, and deleting key-value pairs.

SOURCE CODE:
# String Example
my_string = "Hello, Python!"
print("String Example:", my_string)
# Accessing individual characters in a string
print("First character of string:", my_string[0])
# String concatenation
greeting = "Hello"
name = "Alice"
print("Concatenated String:", greeting + ", " + name)
# List Example
my_list = [1, 2, 3, 4, 5]
print("\nList Example:", my_list)
# Accessing list elements
print("First element of list:", my_list[0])
# Modifying list element
my_list[1] = 20
print("Modified List:", my_list)
# Adding an element to the list
my_list.append(6)
print("List after adding an element:", my_list)
# Tuple Example (Immutable)
my_tuple = (10, 20, 30, 40)
print("\nTuple Example:", my_tuple)
# Accessing tuple elements
print("First element of tuple:", my_tuple[0])
# Tuples are immutable, so you can't modify them, but you can concatenate them
new_tuple = my_tuple + (50, 60)
print("Concatenated Tuple:", new_tuple)
# Dictionary Example
my_dict = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print("\nDictionary Example:", my_dict)
# Accessing dictionary values by key
print("Name:", my_dict["name"])
# Modifying a value in the dictionary
my_dict["age"] = 26
print("Modified Dictionary:", my_dict)
# Adding a new key-value pair to the dictionary
my_dict["profession"] = "Engineer"
print("Dictionary after adding a key-value pair:", my_dict)

OUTPUT:
2323007

PRACTICAL-5
AIM: Write a program using a for loop that loops over a sequence.
SOFTWARE USED: vs code
PROCEDURE:
my_list: This is a list that contains a sequence of numbers [10, 20, 30, 40, 50].
The for loop: It iterates through each element in the sequence my_list, and the current element is
stored in the variable item.

print(item): For each iteration, the value of item is printed.

SOURCE CODE:
#define a sequence (list of numbers)
my_list= [10,20,30,40,50]
#using a for loop to iterate over the sequence
Print (“looping through the list:”)
For item in my_list:
Print(item)
OUTPUT:

2323007

PRACTICAL-6
AIM: Write a program to implement function and recursion.
SOFTWARE USED: vs code
THEORY:
 Function:
The factorial(n) function is defined to calculate the factorial of a number. The base case ensures
that the recursion stops when n is 0 or 1.
 Recursion:
The function calls itself with a reduced value of n (i.e., n-1), building up the solution by
multiplying the current n by the factorial of (n-1).

SOURCE CODE:
def fibonacci(n):
if n==0 or n==1:
return 1
else:
return (fibonacci(n-2)+fibonacci(n-1))
num=int(input("Enter a number: "))
if num<=0:
print("Enter a positive number")
else:
print("Fibonacci series: ")
for i in range(num):
print(fibonacci(i))

OUTPUT:

You might also like