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

1.2python

This document is a worksheet for a Python programming assignment for a Computer Science and Engineering student. It includes tasks such as checking if a number is even or odd, printing multiples of 3, creating a pattern with asterisks, using a break statement in loops, and implementing a simple calculator. Each task is accompanied by the corresponding source code.

Uploaded by

imamamehedi
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)
5 views

1.2python

This document is a worksheet for a Python programming assignment for a Computer Science and Engineering student. It includes tasks such as checking if a number is even or odd, printing multiples of 3, creating a pattern with asterisks, using a break statement in loops, and implementing a simple calculator. Each task is accompanied by the corresponding source code.

Uploaded by

imamamehedi
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/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.2

Student Name: Sejal Singh UID: 23BCS12699


Branch: CSE Section/Group: 717-B
Semester: 3rd Date of Performance: 27th July
Subject Name: PYTHON Subject Code: 23CSP-201

1. Aim:

Python Operators and Control Structures

2. Source Code:

a. Write a program that checks if a number is even or odd using the


modulo operator and an if-else statement.

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


if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

b. Create a loop that prints the first 10 multiples of 3.


count = 10
for i in range(1, count + 1):
multiple = i * 3 print(multiple)

c. Use a nested loop to print a simple pattern of asterisks (e.g., a


right-angled triangle).
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print('*', end='')
print()

d. Write a program that uses the break statement to exit a loop when
a specific condition is met.

print("Enter numbers to keep looping. Enter 0 to exit.")

while True:

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

if number == 0:
print("You entered 0. Exiting the loop.")
break
print(f"You entered: {number}")
print("Loop has been exited.")
DEPARTMENT OF
COMPUTER SCIENCE

e. Implement a simple calculator that performs basic arithmetic


operations based on user input.

def add(x, y):


return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y != 0:
return x / y
else:
return "Error! Division by zero."

print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = input("Enter choice (1/2/3/4): ")

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input! Please select a valid operation.")

3. Screenshot of Outputs:

You might also like