🐍 M3-R5: Programming and Problem Solving Through Python – Practical Notes
🐍 M3-R5: Programming and Problem Solving Through Python – Practical Notes
This module introduces the fundamentals of programming using Python. It emphasizes problem-solving techniques, algorithm development, flow of control, and working with functions, lists, strings, files, etc.
📖 Module Overview
| Topic | Skills Covered |
|---|---|
| Python Basics | Variables, data types, I/O |
| Control Flow | Conditional statements, loops |
| Functions and Recursion | Custom and built-in functions |
| Data Structures | Lists, tuples, dictionaries |
| File Handling | Reading/writing files |
| Problem Solving Techniques | Logic, flowcharts, pseudo code |
| Introduction to NumPy | Arrays, vectorized operations, basic numerical computing |
🧪 Practical Questions
Number Operations
- Check whether a number is even or odd
- Check whether a number is buzz or not
- Check whether a number is prime or not
- Print the factors of a number
- Print the sum of factors of a number
- Calculate the factorial of a number
- Calculate the factorial of a number using recursion
Printing Patterns
Number Series
- Print table of given number
- Print even and odd series up to a given number
- Print Fibonacci series up to a given number
- Print prime numbers up to a given number
Special Number Checks
- Check whether a number is Palindrome or not
- Check whether a number is perfect or not
- Check whether a number is Armstrong or not
Swap Variables
String Operations
File Operations
Number Operations
Check whether a number is even or odd
Concepts used: conditionals
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
Check whether a number is buzz or not
A buzz number is a number that is either divisible by 7 or ends with 7. For example, 7, 17, and 28 are buzz numbers.
num = int(input("Enter a number: "))
if num % 7 == 0 or num % 10 == 7:
print(num, "is a buzz number")
else:
print(num, "is not a buzz number")
Check whether a number is prime or not
Concepts used: Loops, conditionals, divisibility check
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Print the factors of a number
A factor of a number is an integer that divides the number without leaving a remainder. For example, factors of 12 are 1, 2, 3, 4, 6, and 12. Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
print("Factors of", num, ":")
for i in range(1, num + 1):
if num % i == 0:
print(i, end=' ')
Print the sum of factors of a number
Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
sum_of_factors = 0
for i in range(1, num + 1):
if num % i == 0:
sum_of_factors += i
print("Sum of factors of", num, "is", sum_of_factors)
Calculate the factorial of a number
Factorial of a number ( n ) is the product of all positive integers up to ( n ). For example, factorial of 5 is 5! = 5 x 4 x 3 x 2 x 1 = 120 .
Concepts used: Loops, conditionals
Without recursion:
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is", factorial)
Using recursion:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
Printing Patterns
1. Star squar pattern
* * * *
* * * *
* * * *
* * * *
for i in range(4):
for j in range(4):
print('*', end=' ')
print()
2.
# * * *
* # * *
* * # *
* * * #
for i in range(4):
for j in range(4):
if i == j:
print('#', end=' ')
else:
print('*', end=' ')
print()
Number Series
Print table of given number
Concepts used: Loops, multiplication
num = int(input("Enter a number: "))
print("table of", num, ":")
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Print even and odd series up to a given number
Concepts used: Loops, conditionals
Even numbers:-
num = int(input("Enter a number: "))
print("Even numbers up to", num, ":")
for i in range(0, num + 1, 2):
print(i, end=' ')
Odd numbers:-
num = int(input("Enter a number: "))
print("Odd numbers up to", num, ":")
for i in range(1, num + 1, 2):
print(i, end=' ')
Print Fibonacci series up to a given number
The Fibonacci series is a sequence where each number is the sum of the two preceding ones, usually starting with 0 and 1. For example, the series starts as 0, 1, 1, 2, 3, 5, 8, etc.
Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
a, b = 0, 1
print("Fibonacci series up to", num, ":")
print(a, end=' ')
print(b, end=' ')
while a <= num:
c = a + b
print(c, end=' ')
a = b
b = c
or
num = int(input("Enter a number: "))
a, b = 0, 1
print("Fibonacci series up to", num, ":")
print(a, end=' ')
print(b, end=' ')
while a <= num:
print(a+b, end=' ')
a, b = b, a + b
Print prime numbers up to a given number
Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. For example, the first few prime numbers are 2, 3, 5, 7, 11, etc. Concepts used: Loops, conditionals, prime checking
num = int(input("Enter a number: "))
print("Prime numbers up to", num, ":")
for n in range(2, num + 1):
is_prime = True
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break
if is_prime:
print(n, end=' ')
Special Number Checks
Check whether a number is Palindrome or not
A palindrome is a number that remains the same when its digits are reversed. For example, 121 and 12321 are palindromes. Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
temp = num
reverse = 0
while temp > 0:
digit = temp % 10
reverse = reverse * 10 + digit
temp //= 10
if num == reverse:
print(num, "is a palindrome.")
else:
print(num, "is not a palindrome.")
Check whether a number is perfect or not
Perfect numbers are those that are equal to the sum of their proper divisors (excluding themselves). For example, 6, which has divisors 1, 2, and 3, and their sum is 6.
Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print(num, "is a perfect number.")
else:
print(num, "is not a perfect number.")
Check whether a number is Armstrong or not
An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153).
Concepts used: Loops, conditionals
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
Swap Variables
Swap two variables using a third variable
Concepts used: Variable assignment
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("After swapping: a =", a, ", b =", b)
Swap two variables without using a third variable
Concepts used: Arithmetic operations
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a = a + b
b = a - b
a = a - b
print("After swapping: a =", a, ", b =", b)
String Operations
Count the number of vowels in a given string
Concepts used: Loops, conditionals, string manipulation
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print("Number of vowels:", count)
File Operations
Read a text file and count the number of words
Concepts used: File handling, string splitting
filename = input("Enter file name: ")
try:
with open(filename, 'r') as file:
content = file.read()
words = content.split()
print("Total number of words:", len(words))
except FileNotFoundError:
print("File not found.")
⏭ Next: M4-R5 – Internet of Things and Its Applications
Return to Home Page