My Python Learning Journey: Week 6 - Steps I Took, Problems I Faced, and How I Solved Them.

My Python Learning Journey: Week 6 - Steps I Took, Problems I Faced, and How I Solved Them.

Hey everyone, in this week, I learned about the following Python concepts:


  • Virtual Environment (Installation and pip freeze command)

  • Lambda Functions

  • String Methods (join and format)

  • Functional Programming with map, filter, and reduce

These are the steps I took to grasp the concepts:


  1. Virtual Environment:

    • Initially, I understood the usage of a virtual environment and learned how to create and activate a virtual environment in Python using pip install virtualenv.

    • Next, I learned how to install packages inside the virtual environment.

    • Finally, I used the pip freeze command to generate a requirements.txt file that returns all the packages installed in a given Python environment and the versions.

  2. Lambda Functions:

    • Practiced writing single-line functions for simple tasks like squaring a number or filtering values from a list.
  3. String Methods:

    • Experimented with the .join method for concatenating strings with separators.

    • I also learned the old .format method to understand and work with older code written before f-strings were introduced.

  4. Functional Programming:

    • Applied the map function to transform elements in a list.

    • Used the filter function to extract values based on conditions.

    • Explored the reduce function to aggregate values (e.g., summing a list).

These are the problems that I encountered:


  1. Installation Errors for Older Python Package Versions in Virtual Environment.

  2. Fixing the TypeError While using .join() Method.

  3. Fixing the ValueError When Switching Placeholders using .format() Method.

This is how I solved those problems:


1. Installation Errors for Older Python Package Versions in Virtual Environment:

Problem: When trying to install pandas==2.1.0 in my virtual environment (while already having the latest version in my global Python interpreter), I encountered an error because certain build tools, like Microsoft Visual Studio, were not properly installed to compile older versions of pandas.

(virtual_environment) PS F:\Python Learning Journey\WEEK 6> pip install pandas==2.1.0

metadata-generation-failed  
ERROR: Could not find C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe

Solution: To resolve this issue, I followed these steps:

  1. Installed Microsoft C++ Build Tools from here, selecting C++ CMake tools and Windows SDK.

  2. Installed meson and ninja using the following command:

pip install meson ninja
  1. Reinstalled pandas without building from the source:
pip install --only-binary :all: pandas==2.1.0

2. Fixing the TypeError While using .join() Method:

Problem: While trying to create a multiplication table and get the results into a vertical string from a list using the .join() method, I encountered a TypeError error because the .join() method expects a list of strings, but list lst contains integers.

Error Code:

num = int(input("Enter the number to multiply: "))
lst = []
for item in range(1,11):
    lst.append(num*item)
print(lst)

string = "\n".join(lst)
print(string)

# Output: 
Enter the number to multiply: 5
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50] # List of Integers
TypeError: sequence item 0: expected str instance, int found

Solution: To fix this error, I converted each result in the list lst into a string before using the .join() method.

num = int(input("Enter the number to multiply: "))
lst = []
for item in range(1, 11):
    lst.append(str(num * item))  # Convert the result to string
print(lst)

string = "\n".join(lst)  # Join the list of strings with newlines
print(string)

# Output:
Enter the number to multiply: 5
['5', '10', '15', '20', '25', '30', '35', '40', '45', '50'] # List of String
5
10
15
20
25
30
35
40
45
50

3. Fixing the ValueError When Switching Placeholders using .format() Method:

Problem: While trying to switch the order of names in the .format() method, I encountered an error because automatic field numbering ({}) and manual field numbering ({0}) got mixed. The .format() method does not allow this mix.

a = "{} is a good {0}".format("Boy","Abdul")
print(a)

# Output: 
ValueError: cannot switch from automatic field numbering to manual field specification

Solution: Use {1} for the first placeholder and {0} for the second one, so that the order of names in the .format() method gets switched.

a = "{1} is a good {0}".format("Boy", "Abdul") # Manual Field Numbering
print(a)

# OR

a = "{} is a good {}".format("Abdul","Boy") # This is default Automatic Field Numbering
print(a)

# Output: 
Abdul is a good Boy

These are the resources that helped me learn: