My Learning Notes – Python Basics (Day Learning Blog)
Today’s class was focused on basic Python concepts and some logical problems. Even though the topics are simple, they form the foundation for programming. I am writing this blog to revise what I learned in my own words.
sep and end in Python
In Python, the print() function has default behavior:
- It adds space between multiple values
- It moves to the next line after printing
We can control this using sep and end.
-
sep(separator): used to define what comes between values -
end: used to define what comes at the end of the output
Example:
print("hi", "hello", sep=" ", end="*")
print(5)
Output:
hi hello*5
This means:
-
sep=" "keeps space between words -
end="*"prevents new line and adds*instead
This concept is useful when formatting output.
Functions in Python
A function is a reusable block of code designed to perform a specific task.
Instead of writing the same logic again and again, we use functions. This improves code readability and reduces duplication.
A function can take input values called arguments and can return output.
Arguments in Functions
Arguments are values passed to a function when it is called.
Types of arguments (basic idea):
- Required arguments
- Default arguments
- Variable-length arguments
Arguments make functions flexible and reusable.
Polymorphism
Polymorphism means “many forms”.
In programming, it means:
- A single function or operation behaves differently based on input
Example:
- Adding two numbers → numeric addition
- Adding two strings → string concatenation
So, same operation but different behavior.
Method Overloading
Method overloading means:
- Same function name
- Different number or type of arguments
Python does not support traditional method overloading like some languages, but we can achieve similar behavior using default arguments or conditions.
Sum of First n Natural Numbers
We learned a mathematical formula:
n(n + 1) / 2
This formula gives the sum of first n numbers.
Example:
For n = 10
Sum = 10 × 11 / 2 = 55
Using loop:
bag = 0
day = 1
while day <= 10:
bag = bag + day
day = day + 1
print(bag)
This loop keeps adding numbers one by one.
Identity Elements in Mathematics
Two important concepts:
Additive Identity:
Adding 0 does not change the value
Example: 5 + 0 = 5Multiplicative Identity:
Multiplying by 1 does not change the value
Example: 5 × 1 = 5
These are basic but important in logic building.
Multiplication of First n Numbers
This is similar to sum, but instead of addition we use multiplication.
total = 1
no = 1
while no <= 5:
total = total * no
no = no + 1
print(total)
Factorial Concept
Factorial of a number means multiplying all numbers from 1 to that number.
Example:
5! = 5 × 4 × 3 × 2 × 1
Code:
factorial = 1
no = 1
while no <= 5:
factorial = factorial * no
no = no + 1
print(factorial)
Reverse approach:
factorial = 1
no = 5
while no >= 1:
factorial = factorial * no
no = no - 1
print(factorial)
Both methods give the same result.
Logic Problem – Frog Climbing
This problem is about simulation using loops.
Given:
- Frog starts at 50 feet
- Climbs 2 feet every time
- Slips down 1.25 feet
We need to find how many steps or days it takes.
feet = 50
up = 2
down = 1.25
day = 0
while feet > 0:
feet = feet - up + down
day = day + 1
print(day)
This type of problem improves logical thinking and loop understanding.
Top comments (0)