0% found this document useful (0 votes)
2 views10 pages

Lab Cyclepython

The document outlines a series of lab exercises focused on Python programming, covering topics such as basic arithmetic operations, decision-making with if statements, string manipulation, list and tuple operations, and dictionary handling. Each lab includes code examples demonstrating the concepts, such as input/output, indexing, looping, and modifying data structures. The exercises aim to provide hands-on experience with Python syntax and functionality.

Uploaded by

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

Lab Cyclepython

The document outlines a series of lab exercises focused on Python programming, covering topics such as basic arithmetic operations, decision-making with if statements, string manipulation, list and tuple operations, and dictionary handling. Each lab includes code examples demonstrating the concepts, such as input/output, indexing, looping, and modifying data structures. The exercises aim to provide hands-on experience with Python syntax and functionality.

Uploaded by

seethalprince02
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab cycle :1

1) Running instructions in Interactive interpreter and a PythonScript.

• Generate output with print statements

• Read input, including casting that input to the appropriate type

• Perform calculations involving integers and floating point numbers using Python operators like
+, -, *, /, //, %, and **

• Call functions residing in the math module make a program in python

Program code:

# Import math module

import math

# --- Input section ---

x = int(input("Enter an integer value for x: "))

y = float(input("Enter a floating-point value for y: "))

# --- Output section ---

print("\n--- Calculations using Python operators ---")

# Basic arithmetic operations

print("x + y =", x + y)

print("x - y =", x - y)

print("x * y =", x * y)

print("x / y =", x / y) # Division (float)

print("x // y =", x // y) # Floor division

print("x % y =", x % y) # Modulus

print("x ** 2 =", x ** 2) # Exponentiation

# --- Math functions ---

print("\n--- Using math module ---")

print("Square root of x:", [Link](x))

print("Value of sin(y):", [Link](y))


print("Value of cos(y):", [Link](y))

print("x raised to the power y (using [Link]):", [Link](x, y))

print("The value of pi:", [Link])

print("The value of e:", math.e)

print("\nProgram executed successfully!")

output:

Lab 2

• Make a decision with an if statement

b)Select one of two alternatives with an if-else statement

c) Select from one of several alternatives by using an ifelif or if-elif-else statement

age = int(input("Enter your age: "))

if age >= 18:

print("You are eligible to vote.")


num = int(input("Enter a number: "))

if num % 2 == 0:

print("The number is even.")

else:

print("The number is odd.")

• Select from one of several alternatives by using an ifelif or if-elif-else statement

marks = int(input("Enter your marks: "))

if marks >= 90:

print("Grade: A")

elif marks >= 75:

print("Grade: B")

elif marks >= 50:

print("Grade: C")

else:

print("Grade: Fail")

lab 3:

1. Create a string
text = "Python Programming"

print("Original String:", text)

# 2. String Indexing
print("\nString Indexing:")
print("First character:", text[0])
print("Last character:", text[-1])
print("Character at index 7:", text[7])

# 3. Looping through a String


print("\nLooping through the string:")
for ch in text:
print(ch, end=" ")

# 4. String Slicing
print("\n\nString Slicing:")
print("First 6 characters:", text[0:6])
print("From index 7 to end:", text[7:])
print("Last 11 characters:", text[-11:])
print("Every second character:", text[::2])
NEXT PGM-4

# 1. Create a list

numbers = [10, 20, 30, 40, 50]

print("Original List:", numbers)

# 2. List Indexing

print("\nList Indexing:")

print("First element:", numbers[0])

print("Last element:", numbers[-1])

print("Element at index 2:", numbers[2])

# 3. Looping through a list

print("\nLooping through the list:")

for num in numbers:

print(num, end=" ")

# 4. Adding items to a list

[Link](60) # Add at end

[Link](2, 25) # Insert at index 2

print("\n\nAfter Adding Elements:", numbers)

# 5. Modifying items of a list


numbers[1] = 15

print("\nAfter Modifying an Element:", numbers)

# 6. Removing elements from the list

[Link](40) # Remove specific element

removed_item = [Link]() # Remove last element

print("\nAfter Removing Elements:", numbers)

print("Removed Item using pop():", removed_item)

# 7. List Slicing

print("\nList Slicing:")

print("First three elements:", numbers[0:3])

print("Elements from index 2 to end:", numbers[2:])

print("Last three elements:", numbers[-3:])

print("Alternate elements:", numbers[::2])


Next pgm :5

# 1. Create a tuple

numbers = (10, 20, 30, 40, 50)

print("Original Tuple:", numbers)

# 2. Tuple Indexing

print("\nTuple Indexing:")

print("First element:", numbers[0])

print("Last element:", numbers[-1])

print("Element at index 2:", numbers[2])

# 3. Looping through a tuple

print("\nLooping through the tuple:")

for num in numbers:

print(num, end=" ")

# 4. Adding items to a tuple

# Tuples are immutable, so convert to list, add items, then convert back

numbers_list = list(numbers)

numbers_list.append(60)

numbers = tuple(numbers_list)

print("\n\nAfter Adding an Element:", numbers)

# 5. Tuple Slicing

print("\nTuple Slicing:")
print("First three elements:", numbers[0:3])

print("Elements from index 2 to end:", numbers[2:])

print("Last three elements:", numbers[-3:])

print("Alternate elements:", numbers[::2])

Next pgm :6

# 1. Create a dictionary and access values using keys


student = {
"name": "Arun",
"age": 20,
"course": "BCA"
}

print("Student Dictionary:", student)


print("Name:", student["name"])
print("Age:", student["age"])
# 2. Adding a key-value pair
student["grade"] = "A"
print("\nAfter Adding a Key-Value Pair:", student)

# 3. Adding to an empty dictionary


marks = {} # Empty dictionary
marks["Maths"] = 85
marks["Python"] = 90
marks["DBMS"] = 88

print("\nMarks Dictionary:", marks)

# 4. Modifying values in a dictionary


student["age"] = 21
print("\nAfter Modifying Value:", student)

# 5. Removing key-value pair


removed_item = [Link]("course")
print("\nAfter Removing 'course':", student)
print("Removed Item:", removed_item)

# 6. Looping through a dictionary

# (a) Looping through all key-value pairs


print("\nLooping through key-value pairs:")
for key, value in [Link]():
print(key, ":", value)

# (b) Looping through all keys


print("\nLooping through all keys:")
for key in [Link]():
print(key)

# (c) Looping through all values


print("\nLooping through all values:")
for value in [Link]():
print(value)

lab:7

You might also like