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

Python & NumPy Logic Notes

The document contains detailed logic notes on Python and NumPy, including solutions to various programming problems such as reversing a string, counting vowels, finding the maximum number, removing duplicates, and checking for prime numbers. It also covers NumPy operations like creating arrays, reshaping, calculating mean, median, standard deviation, performing element-wise addition, and extracting even numbers using masking. Each section includes code examples and explanations of the underlying logic.

Uploaded by

suphiyan40
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)
9 views4 pages

Python & NumPy Logic Notes

The document contains detailed logic notes on Python and NumPy, including solutions to various programming problems such as reversing a string, counting vowels, finding the maximum number, removing duplicates, and checking for prime numbers. It also covers NumPy operations like creating arrays, reshaping, calculating mean, median, standard deviation, performing element-wise addition, and extracting even numbers using masking. Each section includes code examples and explanations of the underlying logic.

Uploaded by

suphiyan40
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

DETAILED PYTHON & NUMPY LOGIC NOTES

---------------------------------------------------------

PYTHON QUESTIONS (DETAILED LOGIC)

---------------------------------------------------------

1) Reverse a string without slicing

Code:

s = "hello"

rev = ""

for ch in s:

rev = ch + rev

print(rev)

Logic:

- We create an empty string rev.

- Loop through each character.

- Add each new character to the FRONT of rev (left side).

- This reverses the original string step-by-step.

---------------------------------------------------------

2) Count vowels in a string

Code:

s = "education"

vowels = "aeiou"

count = 0

for ch in s:

if ch in vowels:

count += 1

Logic:

- vowels list created for fast checking.

- Loop through each character and check membership.

- Increase count when vowel found.


---------------------------------------------------------

3) Find maximum number without max()

Code:

nums = [10,25,3,40,7]

maxi = nums[0]

for n in nums:

if n > maxi:

maxi = n

Logic:

- Start by assuming first element is biggest.

- Compare each element; update maxi when a bigger number is found.

---------------------------------------------------------

4) Remove duplicates from list manually

Code:

nums = [1,2,2,3,4,4,5]

unique = []

for n in nums:

if n not in unique:

[Link](n)

Logic:

- Maintain a clean list of unique items.

- Add only when item not already present.

---------------------------------------------------------

5) Check if a number is prime

Code:

num = 17

is_prime = True

for i in range(2, num):

if num % i == 0:
is_prime = False

break

Logic:

- Prime numbers have no divisors other than 1 and itself.

- Loop from 2 to num-1; if any divides evenly → not prime.

---------------------------------------------------------

NUMPY QUESTIONS (DETAILED LOGIC)

---------------------------------------------------------

1) Create array 1 to 10

Code:

arr = [Link](1,11)

Logic:

- arange(start,end) gives a range but end is exclusive.

- So this returns 1 to 10.

---------------------------------------------------------

2) Reshape array to 3x3

Code:

arr = [Link](1,10)

reshaped = [Link](3,3)

Logic:

- Total elements must match (3×3 = 9).

- reshape changes structure, not data.

---------------------------------------------------------

3) Mean, median, std

Code:

arr = [Link]([1,2,3,4,5])

[Link]()

[Link](arr)
[Link]()

Logic:

- NumPy performs these computations efficiently.

- mean = average, median = middle value, std = spread of data.

---------------------------------------------------------

4) Element-wise addition

Code:

a = [Link]([1,2,3])

b = [Link]([4,5,6])

a+b

Logic:

- NumPy performs vectorized element-wise operations.

- Each element is added index-wise.

---------------------------------------------------------

5) Extract even numbers (masking)

Code:

arr = [Link](1,21)

evens = arr[arr % 2 == 0]

Logic:

- arr % 2 == 0 creates a boolean mask.

- True values are selected from the array.

---------------------------------------------------------

END OF NOTES

You might also like