Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Intermediate Python Concept

Intermediate Python Concept

Become More Then A coder | Learn & Start Coding Now.

📦 1. Functions with Return Values

A function is like a machine. You give it input, it gives you output.

# This function takes two numbers and returns their sum
def add_numbers(a, b):
    result = a + b
    return result # sends the result back to the caller

# Calling the function and storing the output
total = add_numbers(5, 3)
print("The total is:", total)

💡 Explanation:

def means you're defining a function.

return gives the result back to the place it was called from.

You can call this function anytime to add numbers.

🧱 2. Default Parameters in Functions

def greet(name="friend"):
    print("Hello", name)

greet("Arvind") # Hello Arvind
greet() # Hello friend (default value is used)

💡 Explanation:

If the user doesn’t provide a name, it uses "friend".


🧠 3. Dictionaries (key-value pairs)

Dictionaries are used to store data in pairs (like a mini database).

student = {
    "name": "Arvind",
    "age": 25,
    "grade": "A"
}

# Accessing values
print(student["name"]) # Arvind
print(student.get("age")) # 25

# Adding a new key
student["passed"] = True

# Looping through keys and values
for key, value in student.items():
    print(key, ":", value)

💡 Explanation:

{} defines a dictionary.

You can get values using keys, e.g., student["name"].

.items() gives both key and value in a loop.

🔁 4. Looping with Index – Using enumerate()

colors = ["red", "blue", "green"]

for index, color in enumerate(colors):
    print("Index:", index, "→ Color:", color)

💡 Explanation:

enumerate() gives both index and item.

Helps when you need the position of each item in a list.


🚫 5. Error Handling (try-except)

Sometimes your code may fail (e.g., dividing by 0). You can handle it:

try:
    number = int(input("Enter a number: "))
    result = 10 / number
    print("Result:", result)
except ZeroDivisionError:
    print("You can't divide by zero.")
except ValueError:
    print("That wasn't a valid number.")

💡 Explanation:

try: Try running the code.

except: If error happens, catch and handle it.

This prevents your program from crashing.


🔃 6. Nested Loops

When you need a loop inside another loop (e.g., printing patterns):

for i in range(1, 4): # Outer loop
    for j in range(1, 4): # Inner loop
        print(f"({i}, {j})", end=" ")
    print() # for a new line

💡 Output:

(1, 1) (1, 2) (1, 3) 
(2, 1) (2, 2) (2, 3) 
(3, 1) (3, 2) (3, 3)

🔄 7. Using range() with Loops

# Print numbers 0 to 4
for i in range(5):
    print(i)

# Print 1 to 10
for i in range(1, 11):
    print(i)

# Print every 2nd number from 2 to 10
for i in range(2, 11, 2):
    print(i)

💡 Explanation:

range(start, end, step)

Python’s range() is very useful for loops.


✅ Quick Review

Concept Explanation

return- Sends back result from a function
Dictionary -Key-value storage
try-except -Handle errors safely
enumerate()- Loop with index
range() -Create number sequences
Nested loops- Loops inside loops

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.