0% found this document useful (0 votes)
23 views

The Road To Python Mastery

The document provides a comprehensive guide on learning Python, covering basics like operators, functions, and comprehensions, alongside practical projects in machine learning and data science. It includes tutorials on using while loops to create various patterns such as left and right half pyramids, rhombuses, and equilateral triangles, with detailed explanations of the code. Additionally, it outlines a step-by-step approach to tackling while loops effectively.

Uploaded by

ecobikefm
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)
23 views

The Road To Python Mastery

The document provides a comprehensive guide on learning Python, covering basics like operators, functions, and comprehensions, alongside practical projects in machine learning and data science. It includes tutorials on using while loops to create various patterns such as left and right half pyramids, rhombuses, and equilateral triangles, with detailed explanations of the code. Additionally, it outlines a step-by-step approach to tackling while loops effectively.

Uploaded by

ecobikefm
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
You are on page 1/ 6

Python: How to Learn

●​ Learn the Basics


○​ Python Operators
○​ Python Functions
○​ Python Comprehensions
○​ Python Lists
○​ Python Tuples
○​ Python Directory and Dictionary Comprehension
○​ Decision Making in Python
○​ Loops in Python
●​ Instantly start projects and learn that way
○​ Machine learning and data science easy projects

While loops: A Tutorial

1. Left Half Pyramid


Code:
python
Copy code
rows = 5
i = 1
while i <= rows:
print("* " * i)
i += 1

Explanation:
Goal: Print a triangle that looks like this:​
markdown​
Copy code​
*
* *
* * *
* * * *
* * * * *

1.​
2.​ How it works:
○​ rows = 5: This is the total number of rows in the triangle.
○​ i = 1: This starts at 1 because we start building from the first row.
○​ while i <= rows: This loop runs as long as i (the current row) is less than or
equal to the total number of rows.
○​ print("* " * i): The * i means "repeat * (star with a space) i times."
○​ i += 1: This moves to the next row by increasing i by 1.
3.​ Flow:
○​ Row 1: i = 1, prints *.
○​ Row 2: i = 2, prints * *.
○​ Row 3: i = 3, prints * * *.
○​ ... and so on, until i = 5.

2. Right Half Pyramid


Code:

rows = 5
i = 1
while i <= rows:
spaces = rows - i
print(" " * spaces + "* " * i)
i += 1

Explanation:
Goal: Print a triangle aligned to the right, like this:​
markdown​
Copy code​
*
* *
* * *
* * * *
* * * * *

1.​
2.​ How it works:
○​ spaces = rows - i: Calculates how many spaces to print before the stars.
■​ Example: On row 1, spaces = 5 - 1 = 4. We print 4 spaces.
○​ " " * spaces: Prints the calculated number of spaces.
○​ "* " * i: Prints i stars.
○​ print(" " * spaces + "* " * i): Combines spaces and stars on the
same line.
3.​ Flow:
○​ Row 1: spaces = 4, i = 1 → prints *.
○​ Row 2: spaces = 3, i = 2 → prints * *.
○​ Row 3: spaces = 2, i = 3 → prints * * *.
○​ ... until i = 5.

3. Rhombus
Code:
python
Copy code
rows = 5
i = 1
while i <= rows:
print(" " * (rows - i) + "* " * rows)
i += 1

Explanation:
Goal: Print a rhombus like this:​
markdown​
Copy code​
* * * * *
* * * * *
* * * * *

1.​
markdown
Copy code

2. **How it works**:
- `rows = 5`: Total number of rows in the rhombus.
- `" " * (rows - i)`: Calculates spaces to indent each row.
- `"* " * rows`: Always prints the same number of stars (5 in this
case).
- `print(" " * (rows - i) + "* " * rows)`: Combines spaces and stars.

3. **Flow**:
- Row 1: `spaces = 4`, prints 4 spaces + 5 stars.
- Row 2: `spaces = 3`, prints 3 spaces + 5 stars.
- Row 3: `spaces = 2`, prints 2 spaces + 5 stars.
- ... and so on.

---

## **4. Equilateral Triangle**

### Code:
```python
rows = 5
i = 1
while i <= rows:
spaces = rows - i
print(" " * spaces + "* " * (2 * i - 1))
i += 1

Explanation:
Goal: Print a triangle like this:​
markdown​
Copy code​
*
* * *
* * * * *

1.​
markdown
Copy code

2. **How it works**:
- `spaces = rows - i`: Calculates spaces to align stars at the center.
- `" " * spaces`: Prints the calculated spaces.
- `"* " * (2 * i - 1)`: Calculates the number of stars for the row.
- Stars = `(2 * i - 1)` ensures an odd number of stars on each row.
- `print(" " * spaces + "* " * (2 * i - 1))`: Combines spaces and
stars.

3. **Flow**:
- Row 1: `spaces = 4`, stars = `1`.
- Row 2: `spaces = 3`, stars = `3`.
- Row 3: `spaces = 2`, stars = `5`.
- ... and so on.

---

## **Best Way to Tackle While Loops Step-by-Step**

1. **Define the Goal**:


- Visualize the output. For example, a triangle or pyramid.

2. **Break the Pattern into Rows**:


- Identify what needs to be printed on each row (spaces, stars,
numbers, etc.).

3. **Decide on Variables**:
- Use a variable like `i` for the current row.
- Calculate secondary elements like `spaces` or `stars` using simple
math.

4. **Use a While Loop**:


- `while i <= rows`: Keeps the loop running for each row.

5. **Construct the Row**:


- Use string multiplication to create patterns:
- `" " * spaces` for spaces.
- `"* " * stars` for stars.

6. **Combine Elements**:
- Concatenate spaces and stars into one line.

7. **Update the Counter**:


- Increment `i` by 1 to move to the next row.

8. **Test and Debug**:


- Start with small values for `rows` (e.g., 3) to verify the logic.

Let me know if you'd like further clarification or additional


examples!

You might also like