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

Week 1 Lab - Overview of Python 2

The document outlines a programming lab for Python II, focusing on fundamental concepts through hands-on tasks. Each task requires students to create separate Python files, complete various programming exercises, and submit their work individually. The tasks cover topics such as Python features, reserved words, data types, mathematical functions, flow control, functions, file operations, exception handling, and object-oriented programming.

Uploaded by

sukhmansinghspsk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views4 pages

Week 1 Lab - Overview of Python 2

The document outlines a programming lab for Python II, focusing on fundamental concepts through hands-on tasks. Each task requires students to create separate Python files, complete various programming exercises, and submit their work individually. The tasks cover topics such as Python features, reserved words, data types, mathematical functions, flow control, functions, file operations, exception handling, and object-oriented programming.

Uploaded by

sukhmansinghspsk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Python II: Week 1 Programming Lab

Objective
The purpose of this lab is to provide hands-on experience with fundamental Python
concepts, as discussed in the lecture. By completing the exercises, students will
strengthen their understanding of Python’s features, syntax, and essential
programming constructs.

Instructions
1. Each task should be coded in a separate .py file. Each file name should be
“Task #.py”, where # is the task number. Eg for task 1, the file name should
be “Task 1.py”.
2. Add comments to explain your code.
3. Collaboration is encouraged, but submissions must be individual. You may
submit the same file as your collaborators.

Lab Tasks
Task 1: Python Features
Objective: Explore Python’s features and interactive mode.
1. Print a message using Python’s interactive mode. Include an example with
formatted strings.
2. Explore the help() function to view details about Python keywords.

Task 2: Reserved Words and Indentation


Objective: Familiarize yourself with Python’s reserved words and indentation rules.
1. Write a script to print all Python reserved words using the keyword module.
2. Write a properly indented Python program that includes:
o An if-else structure

o A nested block with a for loop

Task 3: Quotation and Comments


Objective: Practice Python’s string syntax and commenting styles.
1. Write a program that uses:
o Single, double, and triple quotes for strings.

o A multi-line comment to describe the program’s functionality.

Task 4: Variables and Data Types


Objective: Understand Python’s dynamic typing and core data types.
1. Create variables for each basic data type: int, float, string, list, tuple, and
dictionary.
2. Perform at least one operation on each data type (e.g., concatenation for
strings, slicing for lists).
3. Demonstrate type conversion using int(), float(), and str().

Task 5: Mathematical Functions and Operators


Objective: Utilize Python’s math capabilities and operators.
1. Use the math module to:
o Calculate the square root of a number.

o Compute the sine of an angle (in radians).

2. Demonstrate Python’s arithmetic, relational, and assignment operators with


examples.

Task 6: Flow Control


Objective: Practice decision and loop structures.
1. Write a program to check if a number is even or odd using an if-else
structure.
2. Use a for loop to iterate over a list of numbers and print their squares.
3. Implement a while loop that calculates the factorial of a given number.
4. Demonstrate the use of break and continue statements in loops.

Task 7: Functions
Objective: Work with functions, parameters, and return values.
1. Define a function that takes two numbers as parameters and returns their
product.
2. Write a function with default parameters and test it with different arguments.
3. Create a function that accepts a variable-length argument list (*args) and
returns the sum of all arguments.

Task 8: File Operations


Objective: Perform file I/O operations in Python.
1. Write a program to create a text file and write the following lines into it:
Hello, Python!
Welcome to File Operations.
2. Open the file, read its content, and print it to the console.
3. Handle file operations using a try-except block to ensure safe execution.

Task 9: Exception Handling


Objective: Implement Python’s exception handling.
1. Write a program that takes two numbers as input and divides the first by the
second.
2. Use a try-except block to handle the ZeroDivisionError exception.
3. Add an else block to print the result if no exception occurs and a finally block
to display a termination message.

Task 10: Object-Oriented Programming


Objective: Create and work with Python classes and objects.
1. Define a class Book with the following attributes:
o title

o author

o price

o A method display_info to print the book’s details.

2. Create an instance of the Book class and display its information.


3. Add a method to apply a discount to the book’s price and demonstrate its
usage.
Submission Guidelines
 Ensure your code runs without errors.
 Comment on your logic and approaches in the code.
 Submit your work by the end of class.

You might also like