Learnory


Гео и язык канала: Эфиопия, Английский
Категория: Технологии


Helps you grow by sharing skill-building Resources and updates on scholarships, Internships, and Career Opportunities

Связанные каналы

Гео и язык канала
Эфиопия, Английский
Категория
Технологии
Статистика
Фильтр публикаций


🌟 Day 7: Nested Loops and Loop Control Statements🌟

Congratulations on making it to Day 7 of the Python for Beginners series! 🎉 Today, we’re going to master nested loop and learn more about loop control statements like break and continue. Let’s make loops your superpower! 💻💪

1. Nested Loops
A nested loop is a loop inside another loop. This is helpful when working with grids, patterns, or multiple levels of iteration.

Example: Printing a 3x3 Grid
For I in range(1, 4): # Outer loop
For j in range(1, 4): # Inner loop
Print(f”({i}, {j})”, end=” “) # Print in one line
Print() # Start a new line after the inner loop finishes


Output:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3, 3)


2. Using break in Nested Loops
The break statement can be used to exit the innermost loop it’s in.

For I in range(1, 4): # Outer loop
For j in range(1, 4): # Inner loop
If j == 2:
Break # Exits the inner loop when j == 2
Print(f”({i}, {j})”, end=” “)
Print()


Output:
(1, 1)
(2, 1)
(3, 1)



3. Using continue in Nested Loops
The continue statement skips the current iteration of the loop but doesn’t stop the entire loop.

For I in range(1, 4): # Outer loop
For j in range(1, 4): # Inner loop
If j == 2:
Continue # Skips when j == 2
Print(f”({i}, {j})”, end=” “)
Print()


Output:
(1, 1) (1, 3)
(2, 1) (2, 3)
(3, 1) (3, 3)



4. Triangle Pattern Example
Here’s a creative way to use nested loops to print a pattern.

Rows = 5

For I in range(1, rows + 1): # Outer loop for rows
For j in range(1, I + 1): # Inner loop for columns
Print(“*”, end=” “)
Print() # Move to the next row


Output:
*
* *
* * *
* * * *
* * * * *



Practice Challenge
1. Create a program to print a multiplication table (from 1x1 to 10x10) using nested loops.
2. Write a program to display all combinations of two dice rolls (e.g., (1,1), (1,2)).
3. Build a pattern with numbers, like:

1
1 2
1 2 3
1 2 3 4




Starting tomorrow, we’ll dive into data structures in Python, beginning with lists and their powerful operations! Get ready for another exciting week. 🚀✨


What is the output of 5 + 3 * 2 in Python?
Опрос
  •   16
  •   11
  •   13
  •   10
2 голосов


Machine learning algorithms .pdf
14.0Мб
🌟 Unlock the basics of Machine Learning with this easy-to-follow guide! 🤖📘 Perfect for curious minds ready to explore. ✨




Good morning! 🌞☕ Wishing you a peaceful and joyful start to your day! ✨💛


GN🌙🌃


When you realize your debugging skills are powered by equal parts confusion and luck. 🤣💻




🌟Day 6: While Loops in Python 🌟

Welcome to Day 6 of the Python for Beginners series! Today, we’re diving deeper into loops by exploring while loops. These are perfect for situations where the number of iterations is unknown and based on a condition. Let’s break it down!

---

What is a while Loop?
A while loop repeatedly executes a block of code as long as a condition is True. It's great for dynamic, condition-based iterations.

Basic Syntax:
while condition:
# Code to execute


---

1. Basic Example
# Print numbers from 1 to 5
num = 1

while num


Interested in Machine Learning? 🤖✨ Let’s explore ML in a simple and beginner-friendly way! 🚀


100+ SQL Commands (2).pdf
4.9Мб
🌟 Master 100+ SQL commands effortlessly for smarter database management! 💻✨


Don't judge people by their interests just because they don't align with yours. What brings them joy might not be your cup of tea and that's perfectly okay.🍸


GM with the Weather

Sunny ❤

Rainy 😭

I don't Care 📱


GN😴 🥱




🌟 Day 5: Introduction to Loops and for Loops in Python🌟

Welcome to Day 5 of the Python for Beginners series! Today, we’ll begin exploring loops, which are used to automate repetitive tasks. We’ll focus specifically on for loops and provide detailed examples to ensure you master this concept.

What Are Loops?
Loops are used to execute a block of code repeatedly, saving time and reducing redundant code. Loops can:
- Automate repetitive tasks.
- Make programs more efficient.
- Help process sequences like lists, strings, and ranges.

Python has two main types of loops:
1. for loops – Used when you know the number of iterations.
2. while loops – Used when you repeat until a condition is met (we’ll cover these later).

The for Loop
A for loop iterates over a sequence (like a list, tuple, string, or range) and executes a block of code for each element.

Basic Syntax:
For variable in sequence:
# Code to execute


1. Looping Through a List
Fruits = [“apple”, “banana”, “cherry”]

For fruit in fruits:
Print(fruit)


Output:
Apple
Banana
Cherry


2. Using range() in Loops
The range() function generates a sequence of numbers. It’s commonly used with for loops.

# Example 1: Loop from 0 to 4
For I in range(5):
Print(i)

# Example 2: Loop from 2 to 9
For I in range(2, 10):
Print(i)

# Example 3: Loop with a step
For I in range(1, 10, 2):
Print(i) # Prints odd numbers from 1 to 9


3. Iterating Through Strings
You can use a for loop to iterate through each character in a string.

Message = “Hello”

For char in message:
Print(char)


Output:
H
E
L
L
O


4. Using break and continue with for Loops
- break: Exits the loop when a condition is met.
- continue: Skips the current iteration and proceeds to the next.

# Example with break
For num in range(10):
If num == 5:
Break
Print(num) # Stops printing when num equals 5

# Example with continue
For num in range(10):
If num % 2 == 0:
Continue
Print(num) # Skips even numbers


Practice Challenge
Here are some tasks to practice using for loops:
1. Write a program to calculate the sum of all numbers from 1 to 50.
2. Create a program that prints all the characters in a string, but stops when it encounters a specific character (e.g., “e”).
3. Build a program that prints the squares of numbers from 1 to 10.


Tomorrow, we’ll continue learning about loops by focusing on while loops, which are perfect for dynamic and condition-based iterations. Get ready to take your loop skills to the next level! 🚀✨


Check out the [IBM X-Force Exchange](https://exchange.xforce.ibmcloud.com/). It's a threat intelligence sharing platform where you can access a huge database of global security threats and insights. Perfect for strengthening your cybersecurity defenses! 🌍🛡️


Hey everyone! ✨👋

I noticed loops can feel a little tricky at first, so I’ve decided to extend the lessons on them! 🚀 By breaking the topic down into smaller, more detailed parts, we’ll make it easier to understand and give you extra time to practice. 💻💡

If there’s anything specific about loops you’d like me to focus on, feel free to share! Let’s tackle this together—step by step! 💪😊


GM🌅


GN 😴

Показано 20 последних публикаций.