Uploaded by Akande David

Python Programming Lab Manual - Redeemer's University

advertisement
REDEEMER’S UNIVERSITY, EDE
COS 102
PROBLEM SOLVING
LABORATORY PRACTICAL MANUAL
NAME: _________________________________________________________
LEVEL: _________________________________________________________
FACULTY: ______________________________________________________
DEPARTMENT: ________________________________________________
PROGRAMME: _________________________________________________
MATRIC NUMBER: ____________________________________________
Lab 1: Introduction to Python Programming
Aim: Introduce Python and basic programming concepts.
Objectives:

Learn the basics of Python.

Set up a Python environment.

Understand Python syntax.

Write simple programs.
Python Basics:

Python is a user-friendly programming language known for its simplicity.

Python is used in various fields, including web development and data analysis.

Python code is easy to read due to its clean and consistent syntax.
Setting Up Python:

You can start coding in Python by setting up a development environment.

Common choices include Python IDLE, Spyder IDE, Jupyter Notebook, or code editors
like Visual Studio Code.
Basic Python Syntax:
Python uses straightforward syntax, such as statements, indentation, and comments.

Statements: Each line of code is a statement.

Indentation: Indentation is used to define code blocks.

Comments: Comments start with # and provide explanations.
Code Example:
# This is a simple Python program
print("Hello, world!")
# Variables and calculations
name = "Blessing"
age = 25
is_student = True
print("Name:", name)
print("Age:", age)
print("Student?", is_student)
length = 5
width = 3
area = length * width
print("Rectangle Area:", area)
Exercise:
1. Write a Python program that takes user input for their name, matric number, and department
and displays a personalized greeting message.
2. Write a Python program that takes user input for the width and height of a parallelogram and
calculates its area. Display the result to the user.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Lab 2: Variables and Data Types
Aim: Teach students about variables and data types in Python.
Objectives:

Understand the concept of variables.

Learn about different data types in Python.

Practice assigning values to variables and performing basic operations.
Variables: Variables are used to store and manage data in Python.
Data Types: Python has various data types, including integers, floats, strings, and Booleans.
Code Example:
# Variables and Data Types
age = 25 # Integer
height = 5.8 # Float
name = "Blessing" # String
is_student = True # Boolean
# Printing Variable Values
print("Age:", age)
print("Height:", height)
print("Name:", name)
print("Is Student?", is_student)
# Basic Operations
x = 10
y=5
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y
# Printing the Results
Print ("Addition:", addition)
print ("Subtraction:", subtraction)
print ("Multiplication:", multiplication)
print ("Division:", division)
Exercise:
1. Create variables to store your name, age, and the city you live in.
2. Perform a mathematical operation using two variables (e.g., addition, subtraction,
multiplication, or division).
3. Display the results using the print function.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Lab 3: Conditional Statements
Aim: Introduce students to conditional statements in Python.
Objectives:

Understand the concept of conditional statements.

Learn how to use if statements for decision-making.

Practice writing code that executes different actions based on conditions.
Conditional Statements: Used to make decisions in a program. Actions are taken based on
whether a condition is true or false.
if Statements: The if statement allows you to execute a block of code if a specific condition is
met.
Code Example:
# Conditional Statements
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are not yet an adult.")
# Checking Multiple Conditions with elif
grade = 85
if grade >= 90:
print("A Grade")
elif grade >= 80:
print("B Grade")
elif grade >= 70:
print("C Grade")
else:
print("Failing Grade")
# Using Logical Operators
is_sunny = True
is_warm = True
if is_sunny and is_warm:
print("It's a sunny and warm day.")
elif is_sunny or is_warm:
print("At least one is true.")
else:
print("Neither is true.")
Exercise:
1. Write a program that asks the user for their name, score and course title.
2. Compute the student’s grade based on the grading criteria. Display the user’s name, course
title, and grade based on the score provided.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Download