Wednesday, 8 April 2026

April Python Bootcamp Day 5

 



Day 5: Conditional Statements in Python

Making Decisions in Your Code 


 Introduction

In real life, we make decisions all the time:

  • If it rains → take an umbrella
  • If marks ≥ 90 → Grade A
  • If balance is low → show warning

Similarly, in programming, we use conditional statements to control the flow of execution.


 What are Conditional Statements?

Conditional statements allow a program to make decisions based on conditions.

They help programs:

  • Execute different blocks of code
  • Respond dynamically to input
  • Implement logic like real-world systems

 Core Idea

# if condition is True -> run code
# else -> skip or run something else

 1. if Statement

 Syntax

if condition:
# code block

 Example

age = 18

if age >= 18:
print("You can vote")

 Runs only when condition is True


 2. if-else Statement

Syntax

if condition:
# True block
else:
# False block

 Example

num = 5

if num % 2 == 0:
print("Even")
else:
print("Odd")

 3. if-elif-else Statement

 Used when multiple conditions exist

 Syntax

if condition1:
# block1
elif condition2:
# block2
else:
# default block

 Example

marks = 94

if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")

 Important Rule

 Only ONE block executes
 First True condition wins


 4. Nested Conditions

 Condition inside another condition

 Example

age = 20
has_id = True

if age >= 18:
if has_id:
print("Entry Allowed")
else:
print("ID required")
else:
print("Underage")

 Important Concepts (Must Understand)

 Truthy & Falsy Values

if 0:
print("Hello")
else:
print("World")

 Output: World


 Truth Table

  • 0, None, False, "" → Falsy
  • Everything else → Truthy

 Order Matters

marks = 90

if marks >= 50:
print("Pass")
elif marks >= 90:
print("Topper")

 Output: Pass (because first condition matched)



 Practice Problems

 Basic Level
  1. Check whether a number is positive or negative.
  1. Check whether a number is even or odd.
  1. Find the greater number between two numbers.

 Intermediate Level
  1. Find the greatest among three numbers.
  1. Check whether a given year is a leap year.
  1. Create a grade system based on marks:
    • 90 and above → Grade A
    • 75 to 89 → Grade B
    • 50 to 74 → Grade C
    • Below 50 → Fail

 Advanced Level
  1. Check whether a given number is a palindrome.
  1. Build logic for an ATM withdrawal system:
    • Check if balance is sufficient
    • Check if amount is a multiple of 100
  1. Create a login system:
    • Validate username and password
    • Show success or error message

🚀 Day 14/150 – Convert Celsius to Fahrenheit in Python 🌡️

 


🚀 Day 14/150 – Convert Celsius to Fahrenheit in Python

Temperature conversion is one of the most common beginner-friendly problems in programming. It helps you understand formulas, user input, functions, and even advanced concepts like list comprehensions.

The formula used is:

F=(C×95)+32F = (C \times \frac{9}{5}) + 32

Let’s explore multiple ways to implement this in Python 👇

🔹 Method 1 – Direct Conversion

This is the simplest and most straightforward approach.

celsius = 25 fahrenheit = (celsius * 9/5) + 32 print("Temperature in Fahrenheit:", fahrenheit)


✅ Explanation:

  • We directly assign a value to celsius
  • Apply the formula
  • Print the result

👉 Best for: Quick calculations or testing

🔹 Method 2 – Using User Input

This makes your program interactive.

celsius = float(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9/5) + 32 print("Temperature in Fahrenheit:", fahrenheit)




✅ Explanation:
  • input() takes user input as string
  • float() converts it into a number
  • Formula is applied as usual

👉 Best for: Real-world programs where users provide input

🔹 Method 3 – Using a Function

Functions make your code reusable and cleaner.

def celsius_to_fahrenheit(c): return (c * 9/5) + 32 print(celsius_to_fahrenheit(25))



✅ Explanation:

  • Function takes input c
  • Returns converted value
  • Can be reused multiple times

👉 Best for: Clean and modular code

🔹 Method 4 – Using Lambda Function (One-liner)

A shorter version of functions.

convert = lambda c: (c * 9/5) + 32 print(convert(25))


✅ Explanation:
  • lambda creates an anonymous function
  • Useful for quick operations

👉 Best for: Short, one-time use functions

🔹 Method 5 – Using List Conversion

Convert multiple values at once.

celsius_values = [0, 10, 20, 30] fahrenheit_values = [(c * 9/5) + 32 for c in celsius_values] print(fahrenheit_values)



✅ Explanation:

  • Uses list comprehension
  • Converts each value in the list
  • Efficient and Pythonic

👉 Best for: Bulk data processing

⚡ Key Takeaways

  • Always remember the formula: (C × 9/5) + 32
  • Use float() when taking decimal inputs
  • Functions improve reusability
  • Lambda is great for quick operations
  • List comprehensions are powerful for handling multiple values

April Python Bootcamp Day 4

 



Day 4: Input & Output in Python

Making Your Programs Interactive 


 Introduction

Till now, your programs were static — they always produced the same output.

But real-world programs don’t work like that.

They take input from users, process it, and then show output.

 This is the core idea of programming:

Input → Processing → Output

In this blog, you’ll learn how to:

  • Take input from users
  • Display output properly
  • Format output professionally

 What is Input?

Input is data provided to a program during execution.

This data can come from:

  • Keyboard (user typing)
  • Files
  • APIs (advanced)

But for now, we focus on user input from keyboard.


 Why Input is Important?

Without input:

  • Programs are fixed
  • No flexibility
  • Same output every time

Example:

 Static Program:

print("Hello Piyush")

 Dynamic Program:

name = input("Enter your name: ")
print("Hello", name)

 Now the output changes based on user → interactive program


 Taking Input in Python

Python provides a built-in function:

input()

 Basic Example:

name = input("Enter your name: ")
print(name)

 Important Concept (VERY IMPORTANT)

input() always returns a string

age = input("Enter your age: ")
print(type(age)) # Output: <class 'str'>

 Type Conversion (Revision)

To perform calculations, convert input:

age = int(input("Enter age: "))
print(age + 5)

Common Conversions:

int(input()) # integer
float(input()) # decimal
str(input()) # string (default)

 What is Output?

Output is the result displayed to the user after processing input.

Without output:

  • User cannot see results
  • Program has no visible effect

 Displaying Output in Python

Python uses:

print()

 Basic Example:

print("Hello World")

 Printing Multiple Values

name = "Piyush"
age = 21

print("Name:", name, "Age:", age)

 sep and end (Advanced Printing)

 Separator (sep)

print("Python", "Java", "C++", sep=" | ")

 End Parameter (end)

print("Hello", end=" ")
print("World")

 String Formatting

String formatting helps display output in a clean and professional way.


 1. f-Strings (Recommended)

name = "Piyush"
age = 21

print(f"My name is {name} and I am {age} years old")

 Fast, readable, modern


 2. .format() Method

print("My name is {} and I am {}".format(name, age))

 3. % Formatting (Old Style)

print("My name is %s and age is %d" % (name, age))

 Escape Characters

print("Hello\nWorld") # New line
print("Hello\tWorld") # Tab
print("He said \"Hi\"")

 Practice Questions

 Basic

Take name as input and print:
"Welcome, <name>"
Take two numbers and print their sum
Take age and print:
"You will be <age+5> after 5 years"

 Intermediate

Take name and marks, print:
"<name> scored <marks> marks"
Take 3 numbers and print their average

 Tricky

a = input("Enter number: ")
b = input("Enter number: ")
print(a + b)

 Why is output concatenation and not addition?


print("Hello", end="---")
print("World", end="***")

 Predict output


a, b = input().split()
print(int(a) + int(b))

 What if user enters only one value?



Tuesday, 7 April 2026

Python Coding Challenge - Question with Answer (ID -080426)

 


Code Explanation:

🔹 1. Creating the Tuple
t = (1, 2, 3)
Here, a tuple named t is created.
It contains three elements: 1, 2, and 3.
Tuples are ordered and immutable (cannot be changed after creation).

🔹 2. Accessing Tuple Elements
Each element in the tuple has an index:
t[0] → 1
t[1] → 2
t[2] → 3

🔹 3. Attempting to Modify the Tuple
t[0] = 10
This line tries to change the first element (1) to 10.

🔹 4. What Actually Happens

Python raises an error:

TypeError: 'tuple' object does not support item assignment
Reason: Tuples are immutable, meaning their values cannot be changed after creation.

Final Output:
Error

Book: AUTOMATING EXCEL WITH PYTHON



Python Coding challenge - Day 1126| What is the output of the following Python Code?

 


Code Explanation:

1️⃣ Defining the Class
class A:

Explanation

A class A is created.
It will contain variables and methods.

2️⃣ Defining Class Variable
x = 10

Explanation

x is a class variable.
It belongs to the class, not individual objects.

3️⃣ Defining @classmethod
@classmethod
def f(cls):

Explanation

f is a class method.
It receives the class itself as cls.

4️⃣ Accessing Class Variable via cls
return cls.x

Explanation

Accesses class variable x using cls.
Works for inheritance too (dynamic reference).

5️⃣ Defining @staticmethod
@staticmethod
def g():

Explanation

g is a static method.
It does NOT receive self or cls.

6️⃣ Accessing Class Variable Directly
return A.x

Explanation

Directly accesses class A.
Not flexible for inheritance (hardcoded).

7️⃣ Calling Methods
print(A.f(), A.g())

Explanation

Calls both methods using class A.

🔄 Method Execution
🔹 A.f()
cls = A
Returns:
A.x → 10
🔹 A.g()
Directly returns:
A.x → 10

📤 Final Output
10 10

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (237) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (3) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (21) data management (15) Data Science (339) Data Strucures (16) Deep Learning (144) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (277) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1286) Python Coding Challenge (1124) Python Mistakes (50) Python Quiz (465) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)