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

Programming Activity (Part 2)

The document outlines a series of programming activities in Python, focusing on error identification, flow control, conditional statements, iterative statements, and string manipulation. Each activity includes specific tasks such as fixing code errors, checking number properties, calculating absolute values, sorting numbers, and generating patterns or series. The document provides example inputs and outputs for clarity on expected results.

Uploaded by

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

Programming Activity (Part 2)

The document outlines a series of programming activities in Python, focusing on error identification, flow control, conditional statements, iterative statements, and string manipulation. Each activity includes specific tasks such as fixing code errors, checking number properties, calculating absolute values, sorting numbers, and generating patterns or series. The document provides example inputs and outputs for clarity on expected results.

Uploaded by

rio161218
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Activity (Part 2)

1. Errors in Python:

Activity 1: Identifying and Fixing Errors

Given the following Python code, identify and fix the errors (syntax, logical, or runtime).​
def factorial(n):
result = 1
for i in range(n):
result = result * i
return result

num = "5"
print(factorial(num))

●​ Question:
○​ What type of errors do you see in the code?
○​ How can you fix the errors? Write the corrected code

2. Flow of Control:

Activity 2: Basic Flow Control

●​ Write a Python program that checks whether a number entered by the user is positive,
negative, or zero using if, elif, and else.​

○​ Prompt the user to input a number.


○​ Use appropriate flow control structures to determine if the number is positive,
negative, or zero.

Example Input:​

number = 5
Example Output:​

The number is positive.

3. Conditional Statements:

Activity 3: Absolute Value Calculation


●​ Write a Python program to find the absolute value of a number using conditional
statements.​

○​ Use if-else or if-elif-else to calculate and print the absolute value of the
number.

Example Input:​
number = -10
Example Output:​

Absolute value is 10

●​

Activity 4: Sort Three Numbers

Write a Python program that takes three numbers as input and sorts them in ascending order
using if-else statements.​

Example Input:​

num1 = 5
num2 = 3
num3 = 8
Example Output:​

Sorted numbers: 3, 5, 8

Activity 5: Divisibility Check

Write a Python program to check whether a number is divisible by 5 and 3 or not using
if-else statements.​

Example Input:​

number = 15
Example Output:​

The number is divisible by 5 and 3.

4. Iterative Statements:
Activity 6: For Loop Pattern

Write a Python program using a for loop to print the following pattern:​
*
**
***
****
*****

Hint: Use range() to control the number of rows.

Activity 7: Summation of Series

Write a Python program using a for loop to calculate the sum of the first n natural numbers.
The value of n should be taken as input from the user.​

Example Input:​

n=5
Example Output:​

The sum of the first 5 natural numbers is 15.

Activity 8: Factorial Using While Loop

Write a Python program to find the factorial of a positive integer using a while loop.​

○​ Ensure that the input number is a positive integer.

Example Input:​

num = 4
Example Output:​

The factorial of 4 is 24.

Activity 9: Fibonacci Series

Write a Python program using a while loop to generate the first n Fibonacci numbers.​

Example Input:​

n=5
Example Output:​

Fibonacci series: 0 1 1 2 3

Activity 10: Use of break and continue

Write a Python program using break and continue:

○​ Print numbers from 1 to 10.


○​ Skip printing numbers that are divisible by 3 using the continue statement.
○​ Stop the loop if the number is greater than 7 using the break statement.

Example Output:​
1
2
4
5
7

5. Strings in Python:

Activity 11: String Concatenation

Write a Python program that takes two strings as input and concatenates them.​

Example Input:​

str1 = "Hello"
str2 = "World"
Example Output:​

HelloWorld

Activity 12: String Repetition


Write a Python program that takes a string and a number as input and prints the string repeated
that many times.​

Example Input:​

str1 = "abc"
n=3
Example Output:​

abcabcabc

Activity 13: String Membership

Write a Python program to check if a substring exists within a given string using the in operator.​

Example Input:​

str1 = "Hello, World!"
substring = "World"
Example Output:​

"World" is found in "Hello, World!"

Activity 14: String Slicing

Write a Python program to slice a string and print the first 5 characters, the last 5 characters,
and every alternate character.​

Example Input:​

text = "PythonProgramming"
Example Output:​

First 5 characters: Pytho
Last 5 characters: amming
Alternate characters: PtoPormig

Activity 15: String Functions


Write a Python program that:​

○​ Takes a string as input.


○​ Capitalizes the string.
○​ Converts the string to lowercase.
○​ Finds the index of a character.
○​ Checks if the string is alphanumeric.

Example Input:​

text = "hello world 123"
Example Output:​

Capitalized: Hello world 123
Lowercase: hello world 123
Index of 'o': 4
Is alphanumeric: False

You might also like