Posted on Leave a comment

How to Compress PDF Files Using Python?

Problem Formulation

Suppose you have a PDF file, but it’s too large and you’d like to compress it (perhaps you want to reduce its size to allow for faster transfer over the internet, or perhaps to save storage space). 

Even more challenging, suppose you have multiple PDF files you’d like to compress. 

Multiple online options exist, but these typically allow a limited number of files to be processed at a time.  Also of course there is the extra time involved in uploading the originals, then downloading the results.  And of course, perhaps you are not comfortable sharing your files with the internet.

Fortunately, we can use Python to address all these concerns.  But before we learn how to do this, let’s first learn a little bit about PDF files.

About Compressing PDF Files

According to Dov Isaacs, former Adobe Principal Scientist (see his discussion here) PDF documents are already substantially compressed. 

The text and vector graphics portions of the documents are already internally zip-compressed, so there is little opportunity for improvement there. 

Instead, any file compression improvements are achieved through compression of image portions of PDF documents, along with potential loss of image quality. 

So compression might be achievable, but the user must choose between how much compression versus how much image quality loss is acceptable.

Setup

A programmer going by the handle Theeko74 has written a Python script called “pdf_compressor.py”. This script is a wrapper for ghostscript functions that do the actual work of compressing PDF files. 

This script is offered under the MIT license and is free to use as the user wishes.

💡 Hint: make sure you have ghostscript installed on your computer. To install ghostscript, follow this detailed guide and come back afterward.

Now download pdf_compressor.py from GitHub here.

Ultimately we will be writing a Python script to perform the compression. 

So we create a directory to hold the script, and use our preferred editor or IDE to create it (this example uses Linux command line to make the directory, and uses vim as the editor to make script “bpdfc.py”; use your preferred choice for creating the directory and creating the script within it):

$ mkdir batchPDFcomp
$ cd batchPDFcomp
$ vim bpdfc.py

We won’t write out the script just yet – we’ll show some details for the script a little later in this article.

When we do write the script, within it we’ll import “pdf_compressor.py” as a module

To prepare for this we should create a subdirectory below our Python script directory. 

Also, we’ll need to copy pdf_compressor.py into that subdirectory, and we’ll need to create a file __init__.py within the same subdirectory (those are double underscores each side of ‘init’):

$ mkdir pdfc
$ cp ~/Downloads/pdf_compressor.py ~/batchPDFcomp/pdfc/
$ cd pdfc
$ vim __init__.py

What we have done here is created a local package pdfc containing a module pdf_compressor.py

💡 Note: The presence of file __init__.py indicates to Python that that directory is part of a package, and to look there for modules.

Now we are ready to write our script.

The PDF Compression Python Script

Here is our script:

from pdfc.pdf_compressor import compress
compress('Finxter_WorldsMostDensePythonCheatSheet.pdf', 'Finxter_WorldsMostDensePythonCheatSheet_compr.pdf', power=4)

As you can see it’s a very short script. 

First we import the “compress” function from “pdf_compressor” module. 

Then we call the “compress” function.  The function takes as arguments: the input file path, the output file path, and a ‘power’ argument that sets compression as follows, from least compression to most (according to the documentation in the script):

Compression levels:

  • 0: default
  • 1: prepress
  • 2: printer
  • 3: ebook
  • 4: screen

Running the Script

Now we can run our script:

$ python bpdfc.py
Compress PDF...
Compression by 51%.
Final file size is 0.2MB
Done.
$ 

We have only compressed one PDF document in this example, but by modifying the script to loop through multiple PDF documents one can compress multiple files at once. 

However, we leave that as an exercise for the reader!

We hope you have found this article useful. Thank you for reading, and we wish you happy coding!

Posted on Leave a comment

How to Swap List Elements in Python?

Problem Formulation

Given a list of size n and two indices i,j < n.

Swap the element at index i with the element at index j, so that the element list[i] is now at position j and the original element list[j] is now at position i.

Examples:

  • Swapping indices 0 and 2 in list [1, 2, 3] modifies the list to [3, 2, 1].
  • Swapping indices 1 and 2 in list [1, 2, 3] modifies the list to [1, 3, 2].
  • Swapping indices 1 and 3 in list ['alice', 'bob', 'carl', 'denis'] modifies the list to ['alice', 'denis', 'carl', 'bob'].

Method 1: Multiple Assignment

To swap two list elements by index i and j, use the multiple assignment expression lst[i], lst[j] = lst[j], lst[i] that assigns the element at index i to index j and vice versa.

lst = ['alice', 'bob', 'carl']
i, j = 0, 2 # Swap index i=0 with index j=2
lst[i], lst[j] = lst[j], lst[i] print(lst)
# ['carl', 'bob', 'alice']

The highlighted line works as follows:

  • First, it obtains the elements at positions j and i by running the right-hand side of the assignment operation.
  • Second, it assigns the obtained elements in one go to the inverse indices i and j (see left-hand side of the assignment operation).

To help you better understand this code snippet, I’ve recorded a quick video that shows you how the generalization of multiple assignment, i.e., slice assignment, works as a Python One-Liner:

Method 2: Swap Two Elements by Value Using indexof()

Let’s quickly discuss a variant of this problem whereby you want to swap two elements but you don’t know their indices yet.

To swap two list elements x and y by value, get the index of their first occurrences using the list.index(x) and list.index(y) methods and assign the result to variables i and j, respectively. Then apply the multiple assignment expression lst[i], lst[j] = lst[j], lst[i] to swap the elements.

The latter part, i.e., swapping the list elements, remains the same. The main difference is highlighted in the following code snippet:

lst = ['alice', 'bob', 'carl']
x, y = 'alice', 'carl' # Get indices i and j associated with elements x and y
i, j = lst.index(x), lst.index(y) # Swap element at index i with element at index j
lst[i], lst[j] = lst[j], lst[i] print(lst)
# ['carl', 'bob', 'alice']

Do you need a quick refresher on the list.index() method?

💡 Background: The list.index(value) method returns the index of the value argument in the list. You can use optional start and stop arguments to limit the index range where to search for the value in the list. If the value is not in the list, the method throws a ValueError.

Feel free to also watch the following quick explainer video:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!

Posted on Leave a comment

Flask Developer – Income and Opportunity

Before we learn about the money, let’s get this question out of the way:

What Is Flask?

Let’s have a look at the definition from the Flask wiki page (highlights by me):

“Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.

It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.”

Now that you know about what it is, let’s have a look at what it earns next!

Annual Income

How much does a Flask Developer make per year?

The average annual income of a Flask Developer in the United States is between $79,000 (25th percentile) and $123,000 (75th percentile) with an average of $103,000 per year according to Ziprecruiter (source). Top earners make $151,000 and more in the US!

Let’s have a look at the hourly rate of Flask Developers next!

Hourly Rate

Flask Developers are well-paid on freelancing platforms such as Upwork or Fiverr.

If you decide to go the route as a freelance Flask Developer, you can expect to make between $40 and $45 per hour on Upwork (source). Assuming an annual workload of 2000 hours, you can expect to make between $80,000 and $90,000 per year.

⚡ Note: Do you want to create your own thriving coding business online? Feel free to check out our freelance developer course — the world’s #1 best-selling freelance developer course that specifically shows you how to succeed on Upwork and Fiverr!

Industry Demand

But is there enough demand? Let’s have a look at Google trends to find out how interest evolves over time (source):

Work Description

So, you may wonder: Flask Developer – what’s the definition?

Flask Developer Definition: A Flask Developer developer creates, edits, analyzes, debugs, and supervises the development of software written in the Flask programming language. You should have a basic understanding of web technologies such as HTML, CSS, JavaScript, and of course Python.

You can check out this free email academy to take care of the important Python skills:

Learning Path, Skills, and Education Requirements

Do you want to become a Flask Developer? Here’s a step-by-step learning path I’d propose to get started with Flask:

You can find many additional computer science courses on the Finxter Computer Science Academy (flatrate model).

But don’t wait too long to acquire practical experience!

Even if you have little skills, it’s best to get started as a freelance developer and learn as you work on real projects for clients — earning income as you learn and gaining motivation through real-world feedback.

🚀 Tip: An excellent start to turbo-charge your freelancing career (earning more in less time) is our Finxter Freelancer Course. The goal of the course is to pay for itself!

You can find more job descriptions for coders, programmers, and computer scientists in our detailed overview guide:

The following statistic shows the self-reported income from 9,649 US-based professional developers (source).

💡 The average annual income of professional developers in the US is between $70,000 and $177,500 for various programming languages.

Question: What is your current total compensation (salary, bonuses, and perks, before taxes and deductions)? Please enter a whole number in the box below, without any punctuation. If you are paid hourly, please estimate an equivalent weekly, monthly, or yearly salary. (source)

The following statistic compares the self-reported income from 46,693 professional programmers as conducted by StackOverflow.

💡 The average annual income of professional developers worldwide (US and non-US) is between $33,000 and $95,000 for various programming languages.

Here’s a screenshot of a more detailed overview of each programming language considered in the report:

Here’s what different database professionals earn:

Here’s an overview of different cloud solutions experts:

Here’s what professionals in web frameworks earn:

There are many other interesting frameworks—that pay well!

Look at those tools:

Okay, but what do you need to do to get there? What are the skill requirements and qualifications to make you become a professional developer in the area you desire?

Let’s find out next!

General Qualifications of Professionals

StackOverflow performs an annual survey asking professionals, coders, developers, researchers, and engineers various questions about their background and job satisfaction on their website.

Interestingly, when aggregating the data of the developers’ educational background, a good three quarters have an academic background.

Here’s the question asked by StackOverflow (source):

Which of the following best describes the highest level of formal education that you’ve completed?

However, if you don’t have a formal degree, don’t fear! Many of the respondents with degrees don’t have a degree in their field—so it may not be of much value for their coding careers anyways.

Also, about one out of four don’t have a formal degree and still succeeds in their field! You certainly don’t need a degree if you’re committed to your own success!

Freelancing vs Employment Status

The percentage of freelance developers increases steadily. The fraction of freelance developers has already reached 11.21%!

This indicates that more and more work will be done in a more flexible work environment—and fewer and fewer companies and clients want to hire inflexible talent.

Here are the stats from the StackOverflow developer survey (source):

Do you want to become a professional freelance developer and earn some money on the side or as your primary source of income?

Resource: Check out our freelance developer course—it’s the best freelance developer course in the world with the highest student success rate in the industry!

Other Programming Languages Used by Professional Developers

The StackOverflow developer survey collected 58000 responses about the following question (source):

Which programming, scripting, and markup languages have you done extensive development work in over the past year, and which do you want to work in over the next year?

These are the languages you want to focus on when starting out as a coder:

And don’t worry—if you feel stuck or struggle with a nasty bug. We all go through it. Here’s what SO survey respondents and professional developers do when they’re stuck:

What do you do when you get stuck on a problem? Select all that apply. (source)

To get started with some of the fundamentals and industry concepts, feel free to check out these articles:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Posted on Leave a comment

Laravel Developer – Income and Opportunity

Before we learn about the money, let’s get this question out of the way:

What Is Laravel?

Let’s have a look at the definition from the official Laravel website:

“Laravel is a PHP-based web application framework with expressive, elegant syntax. […] Laravel strives to provide an amazing developer experience while providing powerful features such as thorough dependency injection, an expressive database abstraction layer, queues and scheduled jobs, unit and integration testing, and more.”

Ya, well, this is marketing speak for Laravel handles the back-end part when building websites with PHP.

Now that you know about what it is, let’s have a look at what it earns next!

Annual Income

How much does a Laravel Developer make per year?

The average annual income of a Laravel Developer in the United States is between $40,000 (25th percentile) and $150,000 (75th percentile) with an average of $92,000 per year according to Kinsta (source).

More specifically, ZipRecruiter reports an average income of a Laravel Developer of $92,285 per year, Payscale reports an average income of $78,000, and Salary.com reports an average income of $79,000 per year in the US.

Let’s have a look at the hourly rate of Laravel Developers next!

Hourly Rate

Laravel Developers are well-paid on freelancing platforms such as Upwork or Fiverr.

If you decide to go the route as a freelance Laravel Developer, you can expect to make between $35 and $55 per hour on Upwork (source). Assuming an annual workload of 2000 hours, you can expect to make between $70,000 and $110,000 per year.

⚡ Note: Do you want to create your own thriving coding business online? Feel free to check out our freelance developer course — the world’s #1 best-selling freelance developer course that specifically shows you how to succeed on Upwork and Fiverr!

Industry Demand

But is there enough demand? Let’s have a look at Google trends to find out how interest evolves over time (source):

Work Description

So, you may wonder: Laravel Developer – what’s the definition?

Laravel Developer Definition: A Laravel Developer developer creates, edits, analyzes, debugs, and supervises the development of PHP-based web applications written in the Laravel programming framework. You should have a basic understanding of web technologies such as HTML, CSS, JavaScript, and PHP.

Learning Path, Skills, and Education Requirements

Do you want to become a Laravel Developer? Here’s a step-by-step learning path I’d propose to get started with Laravel:

You can find many additional computer science courses on the Finxter Computer Science Academy (flatrate model).

But don’t wait too long to acquire practical experience!

Even if you have little skills, it’s best to get started as a freelance developer and learn as you work on real projects for clients — earning income as you learn and gaining motivation through real-world feedback.

🚀 Tip: An excellent start to turbo-charge your freelancing career (earning more in less time) is our Finxter Freelancer Course. The goal of the course is to pay for itself!

You can find more job descriptions for coders, programmers, and computer scientists in our detailed overview guide:

The following statistic shows the self-reported income from 9,649 US-based professional developers (source).

💡 The average annual income of professional developers in the US is between $70,000 and $177,500 for various programming languages.

Question: What is your current total compensation (salary, bonuses, and perks, before taxes and deductions)? Please enter a whole number in the box below, without any punctuation. If you are paid hourly, please estimate an equivalent weekly, monthly, or yearly salary. (source)

The following statistic compares the self-reported income from 46,693 professional programmers as conducted by StackOverflow.

💡 The average annual income of professional developers worldwide (US and non-US) is between $33,000 and $95,000 for various programming languages.

Here’s a screenshot of a more detailed overview of each programming language considered in the report:

Here’s what different database professionals earn:

Here’s an overview of different cloud solutions experts:

Here’s what professionals in web frameworks earn:

There are many other interesting frameworks—that pay well!

Look at those tools:

Okay, but what do you need to do to get there? What are the skill requirements and qualifications to make you become a professional developer in the area you desire?

Let’s find out next!

General Qualifications of Professionals

StackOverflow performs an annual survey asking professionals, coders, developers, researchers, and engineers various questions about their background and job satisfaction on their website.

Interestingly, when aggregating the data of the developers’ educational background, a good three quarters have an academic background.

Here’s the question asked by StackOverflow (source):

Which of the following best describes the highest level of formal education that you’ve completed?

However, if you don’t have a formal degree, don’t fear! Many of the respondents with degrees don’t have a degree in their field—so it may not be of much value for their coding careers anyways.

Also, about one out of four don’t have a formal degree and still succeeds in their field! You certainly don’t need a degree if you’re committed to your own success!

Freelancing vs Employment Status

The percentage of freelance developers increases steadily. The fraction of freelance developers has already reached 11.21%!

This indicates that more and more work will be done in a more flexible work environment—and fewer and fewer companies and clients want to hire inflexible talent.

Here are the stats from the StackOverflow developer survey (source):

Do you want to become a professional freelance developer and earn some money on the side or as your primary source of income?

Resource: Check out our freelance developer course—it’s the best freelance developer course in the world with the highest student success rate in the industry!

Other Programming Languages Used by Professional Developers

The StackOverflow developer survey collected 58000 responses about the following question (source):

Which programming, scripting, and markup languages have you done extensive development work in over the past year, and which do you want to work in over the next year?

These are the languages you want to focus on when starting out as a coder:

And don’t worry—if you feel stuck or struggle with a nasty bug. We all go through it. Here’s what SO survey respondents and professional developers do when they’re stuck:

What do you do when you get stuck on a problem? Select all that apply. (source)

To get started with some of the fundamentals and industry concepts, feel free to check out these articles:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Posted on Leave a comment

Is There A List Of Line Styles In Matplotlib?

You want to plot a series of data with unique styles. You need to pick various line styles from a list but not sure how to get started. This tutorial will help you out.

Yes, there is a list of line styles in matplotlib. To get the list, import the lines from the matplotlib library.

from matplotlib import lines

Next, get the keys from the lineStyle attribute.

lines.lineStyles.keys()

You can then print the styles and choose your preferred style per plot.

print(lines.lineStyles.keys())

The result is

dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])

As you will see in this tutorial, you can also generate line styles using colors and markers. What is more? Find out below.

Lab Setup To explore Line Styles In Matplotlib

Assume we want to plot the annual earnings of company X’s employees (designers, developers, and accountants) aged between 20 and 29. We store the ages in a list as follows.

ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

Next, we create lists for each group of employees’ salaries.

designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000]

Now that we have some data to practice various line styles in matplotlib, let’s go ahead and manipulate the data.

Practical Ways To Use Line Styles In Matplotlib

➥Get the required functions

Install the matplotlib library using pip.

pip install matplotlib
# OR
pip3 install matplotlib

After installing Matplotlib, create a file and import the library’s functions into it. I have created one called employees.py and imported lines and pyplot as follows.

from matplotlib import lines, pyplot as plt

We will get the line styles from lines and plot the lines with pyplot. It is a convention to shorten pyplot as plt. In simple words it is an alias.

➥Plot the data

Plotting a bunch of data on a curve requires specifying the values for the x-axis and y-axis.

plt.plot(<x-axis data list>, <y-axis data list>)

We can plot our employee values as follows.

ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers)
plt.plot(ages, developers)
plt.plot(ages, accountants) plt.show()

The plt.show() function instructs Python to reveal the graphical information cached in the memory. As soon as you run the file on the terminal, you get three solid lines of different colors.

python employees.py
List of Line Styles in Python - Figure 1

Now let us label the lines and add a title to the plots for easier identification. Insert the code before the plt.show() line.

plt.xlabel("Ages")
plt.ylabel("Annual salary in USD")
plt.title("Salaries of X employees for 20-29 ages")

Output:

List of Line Styles in Python - Figure 2

However, we cannot tell what the green, blue, and coral lines represent. So, let’s mark the lines using labels and a legend.

labels

plt.plot(ages, designers, label='designers')
plt.plot(ages, developers, label='developers')
plt.plot(ages, accountants, label='accountants')

legend represents the area that describes specific elements of the graph. In this case, we will use the legend() method to describe the labels assigned previously.

plt.legend()
plt.show()

Output:

Displaying Legends in Graph

Full code: Here’s the full code that helps to represent our data in the form of different lines.

from matplotlib import lines, pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers, label='designers')
plt.plot(ages, developers, label='developers')
plt.plot(ages, accountants, label='accountants') plt.xlabel("Ages")
plt.ylabel("Annual salary in USD")
plt.title("Salaries of X employees for 20-29 ages") plt.legend() plt.show()

Looks better, right? Yes, but we can improve the visuals by changing the line styles.

💡Change The Line Styles

We assigned different labels to distinguish different data but is there a way to change the style of the line? Yes! The first step is to grab the lines with the help of the lines module. You can import lines from matplotlib as we have done above or get them from Matplotlib line style docs.

Let’s print the styles from the imported lines. After commenting out the plt.show() method, run the print line below the import statement.

print(lines.lineStyles)

We get a dictionary which represents the type of styles in which the lines can be represented as follows:

{'-': '_draw_solid', '--': '_draw_dashed', '-.': '_draw_dash_dot', ':': '_draw_dotted', 'None': '_draw_nothing', ' ': '_draw_nothing', '': '_draw_nothing'}

Let’s find the keys and store them as list_styles.

line_styles = lines.lineStyles.keys()

We get a list on running printing line_styles.

print(line_styles)
dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])

Let’s use the result to change line styles for the plots.

Combining the lines.lineStyles.keys() and pyplot.linestyle helps us change the designers and developers plots as follows.

from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers, label='designers', linestyle='--')
plt.plot(ages, developers, label='developers', linestyle=':')
plt.show()

Output:

Here, lines.lineStyles show the styles, while pyplot‘s linestyle attribute changes the lines graphically.

😎 Tidbits

  • You can also style the lines using markers, width, and colors. For example, you can mark the accountants‘ plot using o as follows.
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000]
plt.plot(ages, accountants, label='accountants', marker='o')
plt.show()

Output:

  • You can increase the width of the designers plot from 1 to 3 as follows
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
plt.plot(ages, designers, label='designers', linestyle='-.', linewidth=3)
plt.show()

Output:

  • Lastly you can customize the plot colors using the color aliases, hex colors or names.
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000]
developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000]
accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000]
plt.plot(ages, designers, label='designers', linestyle='-.', color='b')
plt.plot(ages, developers, label='developers', linestyle=':', linewidth=3, color='#6dee6d')
plt.plot(ages, accountants, label='accountants', marker='o', color='gold')
plt.show()

Output:

Conclusion

You can get a list of styles in matplotlib using the lines attribute. After storing the target styles, you can change the lines’ appearance using pyplot‘s built-in linestyle attribute. Additionally, as illustrated in this tutorial, you can differentiate lines using markers, width, and colors.

Recommended: Matplotlib — A Simple Guide with Videos

Please stay tuned and subscribe for more interesting discussions in the future. Happy learning!

Posted on Leave a comment

Python os.walk() – A Simple Illustrated Guide

According to the Python version 3.10.3 official doc, the os module provides built-in miscellaneous operating system interfaces. We can achieve many operating system dependent functionalities through it. One of the functionalities is to generate the file names in a directory tree through os.walk().

If it sounds great to you, please continue reading, and you will fully understand os.walk through Python code snippets and vivid visualization.

In this article, I will first introduce the usage of os.walk and then address three top questions about os.walk, including passing a file’s filepath to os.walk, os.walk vs. os.listdir, and os.walk recursive.

How to Use os.walk and the topdown Parameter?

Syntax

Here is the syntax for os.walk:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

Input

1. Must-have parameters:

  • top: accepts a directory(or file) path string that you want to use as the root to generate filenames.

2. Optional parameters:

  • topdown: accepts a boolean value, default=True. If True or not specified, directories are scanned from top-down. Otherwise, directories are scanned from the bottom-up. If you are still confused about this topdown parameter like I first get to know os.walk, I have a nicely visualization in the example below.
  • onerror: accepts a function with one argument, default=None. It can report the error to continue with the walk, or raise the exception to abort the walk.
  • followlinks: accepts a boolean value, default=False. If True, we visit directories pointed to by symlinks, on systems that support them.

💡 Tip: Generally, you only need to use the first two parameters in bold format.

Output

Yields 3-tuples (dirpath, dirnames, filenames) for each directory in the tree rooted at directory top (including top itself).

Example

I think the best way to comprehend os.walk is walking through an example.

Our example directory tree and its labels are:

By the way, the difference between a directory and a file is that a directory can contains many files like the above directory D contains 4.txt and 5.txt.

Back to our example, our goal is to 

  • Generate filenames based on the root directory, learn_os_walk
  • Understand the difference between topdown=True and topdown=False

To use the os.walk() method, we need to first import os module:

import os

Then we can pass the input parameters to the os.walk and generate filenames. The code snippet is:

a_directory_path = './learn_os_walk' def take_a_walk(fp, topdown_flag=True): print(f'\ntopdown_flag:{topdown_flag}\n') for pathname, subdirnames, subfilenames in os.walk(fp, topdown=topdown_flag): print(pathname) print(subdirnames) print(subfilenames) print('--------------------------------') print('What a walk!') # *Try to walk in a directory path
take_a_walk(a_directory_path)
# Output more than Just 'What a walk!'
# Also all the subdirnames and subfilenames in each file tree level.
# BTW if you want to look through all files in a directory, you can add
# another for subfilename in subfilenames loop inside.

The above code has a function take_a_walk to use os.walk along with a for loop. This is the most often usage of os.walk so that you can get every file level and filenames from the root directory iteratively.

For those with advanced knowledge in Python’s generator, you would probably have already figured out that os.walk actually gives you a generator to yield next and next and next 3-tuple……

Back in this code, we set a True flag for the topdown argument. Visually, the topdown search way is like the orange arrow in the picture below:

And if we run the above code, we can the below result:

If we set the topdown to be False, we are walking the directory tree from its bottom directory D like this:

The corresponding code snippet is:

a_directory_path = './learn_os_walk' def take_a_walk(fp, topdown_flag=False): print(f'\ntopdown_flag:{topdown_flag}\n') for pathname, subdirnames, subfilenames in os.walk(fp, topdown=topdown_flag): print(pathname) print(subdirnames) print(subfilenames) print('--------------------------------') print('What a walk!') # *Try to walk in a directory path
take_a_walk(a_directory_path)
# Output more than Just 'What a walk!'
# Also all the subdirnames and subfilenames in each file tree level.
# BTW if you want to look through all files in a directory, you can add
# another for subfilename in subfilenames loop inside.

And if we run the above code, we can the below result:

Now, I hope you understand how to use os.walk and the difference between topdown=True and topdown=False. 🙂

Here’s the full code for this example:

__author__ = 'Anqi Wu' import os a_directory_path = './learn_os_walk'
a_file_path = './learn_os_walk.py' # same as a_file_path = __file__ def take_a_walk(fp, topdown_flag=True): print(f'\ntopdown_flag:{topdown_flag}\n') for pathname, subdirnames, subfilenames in os.walk(fp, topdown=topdown_flag): print(pathname) print(subdirnames) print(subfilenames) print('--------------------------------') print('What a walk!') # *Try to walk in a file path
take_a_walk(a_file_path)
# Output Just 'What a walk!'
# Because there are neither subdirnames nor subfilenames in a single file !
# It is like:
# for i in []:
# print('hi!') # We are not going to execute this line. # *Try to walk in a directory path
take_a_walk(a_directory_path)
# Output more than Just 'What a walk!'
# Also all the subdirnames and subfilenames in each file tree level.
# BTW if you want to look through all files in a directory, you can add
# another for subfilename in subfilenames loop inside. # *Try to list all files and directories in a directory path
print('\n')
print(os.listdir(a_directory_path))
print('\n')

How to Pass a File’s filepath to os.walk?

Of course, you might wonder what will happen if we pass a file’s filepath, maybe a Python module filepath string like './learn_os_walk.py' to the os.walk function.

This is exactly a point I was thinking when I started using this method. The simple answer is that it will not execute your codes under the for loop.

For example, if you run a code in our learn_os_walk.py like this:

import os a_file_path = './learn_os_walk.py' # same as a_file_path = __file__ def take_a_walk(fp, topdown_flag=False): print(f'\ntopdown_flag:{topdown_flag}\n') for pathname, subdirnames, subfilenames in os.walk(fp, topdown=topdown_flag): print(pathname) print(subdirnames) print(subfilenames) print('--------------------------------') print('What a walk!') # *Try to walk in a file path
take_a_walk(a_file_path)

The only output would be like this:

Why is that?

Because there are neither subdirnames nor subfilenames in a single file! It is like you are writing the below code:

for i in []: print('hi!')

And you will not get any 'hi' output because there is no element in an empty list.

Now, I hope you understand why the official doc tells us to pass a path to a directory instead of a file’s filepath 🙂

os.walk vs os.listdir — When to Use Each?

A top question of programmers concerns the difference between os.walk vs os.listdir.

The simple answer is:

The os.listdir() method returns a list of every file and folder in a directory. The os.walk() method returns a list of every file in an entire file tree.

Well, if you feel a little bit uncertain, we can then use code examples to help us understand better!

We will stick to our same example directory tree as below:

In this case, if we call os.listdir() method and pass the directory path of learn_os_walk to it like the code below:

import os a_directory_path = './learn_os_walk' # *Try to list all files and directories in a directory path
print('\n')
print(os.listdir(a_directory_path))
print('\n')

And we will get an output like:

That’s it! Only the first layer of this entire directory tree is included. Or I should say that the os.listdir() cares only about what is directly in the root directory instead of searching through the entire directory tree like we see before in the os.walk example.

Summary

Summary: If you want to get a list of all filenames and directory names within a root directory, go with the os.listdir() method. If you want to iterate over an entire directory tree, you should consider os.walk() method.

Now, I hope you understand when to use os.listdir and when to use os.walk 🙂

os.walk() Recursive — How to traverse a Directory Tree?

Our last question with os.walk is about how to literally iterate over the entire directory tree.

Concretely, we have some small goals for our next example:

  • Iterate over all files within a directory tree
  • Iterate over all directories within a directory tree

All examples below are still based on our old friend, the example directory tree:

Iterate over all files within a directory tree

First, let’s head over iterating over all files within a directory tree. This can be achieved by a nested for loop in Python.

The potential application could be some sanity checks or number counts for all files within one folder. How about counting the number of .txt files within one folder? Let’s do it!

The code for this application is:

import os a_directory_path = './learn_os_walk'
total_file = 0 for pathname, subdirnames, subfilenames in os.walk(a_directory_path): for subfilename in subfilenames: if subfilename.endswith('.txt'): total_file += 1
print(f'\n{total_file}\n')

As you can see, we use another for loop to iterate over subfilenames to get evey file within a directory tree. The output is 7 and is correct according to our example directory tree.

The full code for this example can be found here.

Iterate over all directories within a directory tree

Last, we can also iterate over all directories within a directory tree. This can be achieved by a nested for loop in Python.

The potential application could be also be some sanity checks or number counts for all directories within one folder. For our example, let’s check if all directories contains __init__.py file and add an empty __init__.py file if not.

💡 Idea: The __init__.py file signifies whether the entire directory is a Python package or not.

The code for this application is:

import os a_directory_path = './learn_os_walk' for pathname, subdirnames, subfilenames in os.walk(a_directory_path): for subdirname in subdirnames: init_filepath = os.path.join(pathname, subdirname, '__init__.py') if not os.path.exists(init_filepath): print(f'Create a new empty [{init_filepath}] file.') with open(init_filepath, 'w') as f: pass

As you can see, we use another for loop to iterate over subdirnames to get evey directory within a directory tree.

Before the execution, our directory tree under the take_a_walk function mentioned before looks like this:

After the execution, we can take a walk along the directory tree again and we get result like:

Hooray! We successfully iterate every directory within a directory tree and complete the __init__.py sanity check.

The full code for this example can be found here.

In summary, you can use os.walk recursively traverse every file or directory within a directory tree through a nested for loop.

Conclusion

That’s it for our os.walk() article!

We learned about its syntax, IO relationship, and difference between os.walk and os.listdir.

We also worked on real usage examples, ranging from changing the search direction through topdown parameter, .txt file number count, and __init__.py sanity check.

Hope you enjoy all this and happy coding!


About the Author

Anqi Wu is an aspiring Data Scientist and self-employed Technical Consultant. She is an incoming student for a Master’s program in Data Science and builds her technical consultant profile on Upwork.

Anqi is passionate about machine learning, statistics, data mining, programming, and many other data science related fields. During her undergraduate years, she has proven her expertise, including multiple winning and top placements in mathematical modeling contests. She loves supporting and enabling data-driven decision-making, developing data services, and teaching. 

Here is a link to the author’s personal website: https://www.anqiwu.one/. She uploads data science blogs weekly there to document her data science learning and practicing for the past week, along with some best learning resources and inspirational thoughts.

Hope you enjoy this article! Cheers!

Posted on Leave a comment

Agile Software Development with Scrum

💡 Abstract: Is Agile software development with Scrum effective for businesses and tech companies? Here are the benefits of Scrum methodology and everything you should know about it! 


In the field of software development, agile software development with Scrum is one of the most popular methodologies. Flexible and low-cost, Agile Scrum is a powerful project management framework to increase teamwork and break down tasks, maximizing quality results.

Already in 2018, 94% of developers were using or used Scrum in their agile practice – of which, 78% use it with other frameworks, and 16% only work with it. 

But what is Scrum in Agile methodology? Here is an overview of the Scrum framework, including methods, benefits, and practices to know.

What Is Scrum?

Scrum is an Agile lifecycle subset method. The lightweight framework focuses on maximizing productivity while ensuring teamwork. Scrum teams work in collaborative environments with effective communication processes. 

The term Scrum was inspired by the passage of play in rugby that sees the whole team combine their collective strength in an attempt to gain ground, which is the perfect metaphor for the approach.

Scrum methodology enables last-minute adjustments and rapidly-changing requirements, increasing deliverables qualities and market trends compared to traditional waterfall processes. It’s an effective and low-cost solution for startups and small teams of 7-9 people. 

Benefits of Agile Software Development With Scrum

The main benefit of Scrum methodologies is flexibility. The team can adjust their workflow on the go based on market trends and users’ needs. Because of its nature, this methodology isn’t the most efficient option for plan-driven approaches or large and complex projects. However, it’s an efficient and valuable solution for startups and team environments, facilitating collaborative tasks and innovative solutions to improve products and services. 

Here are the core benefits of Agile software development with Scrum:  

  • Flexible and Adaptable: Scrum framework is one of the most effective solutions for new projects and startups. This Agile development methodology allows changes and edits on the go, without effective output. For this reason, it’s one of the most effective solutions for projects for companies in analyzing their customers’ requirements or improving their services over time. 
  • Time to Market: Scrum Agile methodologies require a short setup time, ensuring a fast and quality delivery to break into the market with new products and services. 
  • Lower Costs: Agile Scrum reduces a company’s costs because of automated documentation and control processes and increased productivity in the workflow. 
  • Transparency: Scrum methodology increases transparency between clients and the company. Any minor change is visible to all members, enforcing trust among consumers. 
  • Increase productivity: Agile Scrum provides a set system for deadlines and performance indicators. In addition, team members get rewards when meeting KPIs and quality checks, increasing team motivation and productivity. 
  • Feedback system: This methodology requires daily check-in and feedback for progress reports, ensuring a smoother and more efficient workflow in the long run.

Scrum Methodologies and Processes

As a part of Agile development methodologies, the Scrum framework consist of iterative processes to ensure teamwork and communication during the process. 

To put it simply, Scrum methods break the waterfall process delivery into smaller cycles. As a result, product teams and the end-customer can review the working software to ensure quality requirements at each stage – for the business and clients alike. 

In short, Agile Scrum method consists of Scrum roles, events, and artifacts: 

Roles  Artifacts Events 
Product owner Development team Scrum master  Increment Product backlogSpring backlog Sprint planning Sprint review Sprnt retrospective Daily Scrum 

Let’s have a closer look at the meaning and function elements in Agile software development with Scrum. 

Agile Software Development With Scrum: Roles 

In the Scrum framework, team members work to improve the software quality based on the product’s characteristics and business requirements. 

In general, a Scrum team consists of three leading roles: 

  • Scrum Master: This role guides the team in complying with rules and method processes. A Scrum master monitor bugs and development process to reduce obstacles and maximize the ROI with the Product Owner. In addition, this role takes care of Scrum updates while coaching, mentoring, and training the team for better results. 
  • Product owner (PO): The Product owner represents stakeholders and customers using the software. The focus is on the business requirements and the project ROI for this role. In short, they support the dev team in translating the project vision into a compelling product and service. 
  • Team: The team consists of professionals combining their tech knowledge to improve and implement the product during several cycles and stages. 

Agile Software Development With Scrum: Events 

Events in Scrum are a series of cycles (usually 2/3 weeks). Called Sprints, these cycles are a timebox to complete a set amount of tasks. The Scrum team combines multiple Sprints before the final Release, when the software enters the market and the product arrives at customers. 

Usually, the Product Owner breaks down the whole product functionality into smaller Epics or User Stories features. Prioritizing different stories helps the team monitor and test demos and prototypes and ensure the product’s complete functionality. 

Any Scrum event aims to simplify the adaptation and implementation of the process, the product, progress, or relationships. 

Here are the main Sprint in Agile software development with Scrum:

  • Sprint is the basic work unit for a Scrum team. It is the main feature that marks the difference between Scrum and other models for agile development.
  • Sprint Planning aims to define what and how it will be done during the current Sprint. Meeting at the beginning of each Sprint, the Scrum Master and team plan and determine how to approach the project coming from the Product Backlog stages and deadlines.
  • Daily Scrum concerns the project evaluation and trend until the end of the Sprint. In addition, the team synchronizes activities and creates a plan for the following 24 hours. 
  • Sprint Review aims to provide an overview of what has been done about the product backlog for future deliveries. At the end of each Sprint, team members report obstacles and implementation to the client. 
  • During the Sprint Retrospective, the team reviews goals analyzing positive and negative input. This stage aims to identify improvement steps and generate an effective plan for the next cycle. 

Agile Software Development With Scrum: Artifacts

Finally, Scrum Artifacts ensures transparency decision making and key information between customers and stakeholders: 

  • Product Backlog (PB): It is the process of listing what a product needs to satisfy potential customers. The Product Owner prioritizes what is important for the business, indicating what should be done to achieve requested quality standards for the product. 
  • Sprint Backlog (SB): As a subset of product backlog items, the team selects which type of tasks to perform during the Sprint, establishing the duration of the cycle and final goals on a shared Scrum board.
  • Increment: This Scrum artifact sums up tasks, user cases and stories, product backlogs, and any relevant element, and it makes them visible to software end-users.

Conclusion 

Agile software development with Scrum ensures a transparent and efficient workflow in mid-sized development teams. This framework allows breaking the whole development cycle in smaller breaks, simplifying the implementation and debugging process. Especially for startups and growing teams, it’s one of the best methodologies to improve development processes and ensure high-quality outcomes, especially for startups and growing teams.


This is a guest post contributed by:

Author’s Bio

Costanza Tagliaferri is a Writer and Content Marketer at DistantJob. She has covered a wide range of topics. Now, she is focussing on technology, traveling, and remote work.

Linkedin

Affiliated company: DistantJob, Remote IT Staffing Agency 


Posted on Leave a comment

Python enumerate() — A Simple Illustrated Guide with Video

If you’re like me, you want to come to the heart of an issue fast. Here’s the 1-paragraph summary of the enumerate() function—that’s all you need to know to get started using it:

Python’s built-in enumerate(iterable) function allows you to loop over all elements in an iterable and their associated counters. Formally, it takes an iterable as an input argument and returns an iterable of tuples (i, x)—one per iterable element x. The first integer tuple value is the counter of the element x in the iterable, starting to count from 0. The second tuple value is a reference to the element x itself. For example, enumerate(['a', 'b', 'c']) returns an iterable (0, 'a'), (1, 'b'), (2, 'c'). You can modify the default start index of the counter by setting the optional second integer argument enumerate(iterable, start).

I’ve created a short visual guide into enumerate in the following graphic:

Python enumerate()

Usage Example

Learn by example! Here are some examples of how to use the enumerate() built-in function:

fruits = ['apple', 'banana', 'cherry']
for counter, value in enumerate(fruits): print(counter, value) # OUTPUT:
# 0 apple
# 1 banana
# 2 cherry

The enumerate(iterable, start) function takes an optional second argument that is the start value of the counter.

fruits = ['apple', 'banana', 'cherry']
for counter, value in enumerate(fruits, 42): print(counter, value) # OUTPUT:
# 42 apple
# 43 banana
# 44 cherry

You can use the enumerate function to create a list of tuples from an iterable where the first tuple value is the index of the element:

fruits = ['apple', 'banana', 'cherry']
fruits_with_indices = list(enumerate(fruits))
print(fruits_with_indices)
# [(0, 'apple'), (1, 'banana'), (2, 'cherry')]

Video enumerate()

Syntax enumerate()

Syntax: 
enumerate(iterable) -> loop over all elements in an iterable and their counters, starting from 0. 
enumerate(iterable, start) -> loop over all elements in an iterable and their counters, starting from start. 
Arguments iterable The iterable you want to enumerate.
start The start counter of the first element iterable[0].
Return Value enumerate object An iterable that allows you to iterate over each element associated to its counter, starting to count from start.

Interactive Shell Exercise: Understanding enumerate()

Consider the following interactive code:

Exercise: Change the start value of the enumerate function to your personal age and run the code. What’s the associated counter to the last fruit in the list?

Next, you’re going to dive deeper into the enumerate() function.


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


What is the Return Value of Python’s enumerate() function?

The return value of enumerate(iterable) is an object of type enumerate. The enumerate class definition implements the iterable interface—the __next__() function—which means that you can iterate over it.

fruits = ['apple', 'banana', 'cherry']
print(type(enumerate(fruits)))
# <class 'enumerate'>

How is Python’s enumerate() Function Implemented?

The default implementation of enumerate() is done in C++, assuming you use cPython as your Python engine. However, the documentation shows an equivalent implementation of enumerate() in Python code that helps you understand how it works under the hood:

def enumerate(sequence, start=0): counter = start for element in sequence: yield counter, element counter += 1

You can see that the return value of enumerate() is not a list but a generator that issues the (counter, element) tuples as they appear in the sequence. Thus, the implementation is memory efficient—it doesn’t generate all (counter, element) pairs in advance and holds them in memory, but generates them as they’re needed.

How to Use enumerate() on Strings?

The enumerate(iterable) function takes an iterable as an input argument. A string is an iterable, so you can pass the string as an input. The return value of the function enumerate(string) will be an enumerate object that associates a counter to each character in the string for a series of tuples (counter, character). Here’s an example:

>>> list(enumerate('finxter'))
[(0, 'f'), (1, 'i'), (2, 'n'), (3, 'x'), (4, 't'), (5, 'e'), (6, 'r')]

You can also set the optional second argument start:

>>> list(enumerate('finxter', 42))
[(42, 'f'), (43, 'i'), (44, 'n'), (45, 'x'), (46, 't'), (47, 'e'), (48, 'r')]

How to Make Your Loop More Pythonic With enumerate()?

Beginner Python coders and coders coming from other programming languages such as Java or C++, often think in indices when creating loops such as this one:

# NON_PYTHONIC
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)): print(i, fruits[i])

The output of this correct, but unpythonic code is:

0 apple
1 banana
2 cherry

While the code does what it needs to do, it shouts into the world that its creator is not an experienced Python coder, but a newbie in Python. Why? Because an experienced Python coder will always prefer the enumerate() function due its more idiomatic and crisp functionality:

# PYTHONIC
fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits): print(i, fruit)

You don’t have to use a single indexing mechanism—which reduces the likelihood of a bug and improves readability of your code.

Python enumerate() step

How to set a step in the indices used by the enumerate() function? For example, you may want to use only every third counter:

0 element_0
3 element_1
6 element_2

The answer is to multiply the returned counter value from a default call of the enumerate() function with the step size like this:

lst = ['element_0', 'element_1', 'element_2']
step = 3
for i, x in enumerate(lst): print(i*step, x)
OUTPUT:
0 element_0
3 element_1
6 element_2

Summary

Python’s built-in enumerate(iterable) function allows you to loop over all elements in an iterable and their associated counters.

Formally, it takes an iterable as an input argument and returns an iterable of tuples(i, x)—one per iterable element x.

  • The first integer tuple value is the counter of the element x in the iterable, starting to count from 0.
  • The second tuple value is a reference to the element x itself.

For example, enumerate(['a', 'b', 'c']) returns an iterable (0, 'a'), (1, 'b'), (2, 'c').

print(*enumerate(['a', 'b', 'c']))
# (0, 'a'), (1, 'b'), (2, 'c')

You can modify the default start index of the counter by setting the optional second integer argument enumerate(iterable, start).

print(*enumerate(['a', 'b', 'c'], 10))
# (10, 'a') (11, 'b') (12, 'c')

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

References:

The post Python enumerate() — A Simple Illustrated Guide with Video first appeared on Finxter.

Posted on Leave a comment

Python divmod() — A Simple Guide with Video

Python’s built-in divmod(a, b) function takes two integer or float numbers a and b as input arguments and returns a tuple (a // b, a % b). The first tuple value is the result of the integer division a//b. The second tuple is the result of the remainder, also called modulo operation a % b. In case of float inputs, divmod() still returns the division without remainder by rounding down to the next round number.

Python divmod() visual explanation

Usage

Learn by example! Here are some examples of how to use the divmod() built-in function with integer arguments:

# divmod() with integers
>>> divmod(10, 2)
(5, 0)
>>> divmod(10, 3)
(3, 1)
>>> divmod(10, 4)
(2, 2)
>>> divmod(10, 5)
(2, 0)
>>> divmod(10, 10)
(1, 0)

You can also use float arguments as follows:

# divmod() with floats
>>> divmod(10.0, 2.0)
(5.0, 0.0)
>>> divmod(10.0, 3.0)
(3.0, 1.0)
>>> divmod(10.0, 4.0)
(2.0, 2.0)
>>> divmod(10.0, 5.0)
(2.0, 0.0)
>>> divmod(10.0, 10.0)
(1.0, 0.0)

Video divmod()

Syntax divmod()

Syntax: 
divmod(a, b) -> returns a tuple of two numbers. The first is the result of the division without remainder a/b. The second is the remainder (modulo) a%b. 
Arguments integer The dividend of the division operation.
integer The divisor of the division operation.
Return Value tuple Returns a tuple of two numbers. The first is the result of the division without remainder. The second is the remainder (modulo).

Interactive Shell Exercise: Understanding divmod()

Consider the following interactive code:

Exercise: Guess the output before running the code.


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Exact Mathematical Definition divmod()

You can generally use the divmod(a, b) function with two integers, one integer and one float, or two floats.

Two integers. Say you call divmod(a, b) with two integers a and b. In this case, the exact mathematical definition of the return value is (a // b, a % b).

a = 5
b = 2
print((a // b, a % b))
print(divmod(a, b))
# OUTPUT:
# (2, 1)
# (2, 1)

One integer and one float. Say you call divmod(a, b) with an integer a and a float b. In this case, the exact mathematical definition of the return value is the return value of converting the integer to a float and calling divmod(a, float(b)).

a = 5.0
b = 2
print((a // b, a % b))
print(divmod(a, b))
# OUTPUT:
# (2.0, 1.0)
# (2.0, 1.0)

Two floats. Say you call divmod(a, b) with two floats a and b. In this case, the exact mathematical definition of the return value is (float(math.floor(a / b)), a % b).

import math a = 5.0
b = 2.0
print((float(math.floor(a / b)), a % b))
print(divmod(a, b))
# OUTPUT:
# (2.0, 1.0)
# (2.0, 1.0)

Note that because of the imprecision of floating point arithmetic, the result may have a small floating point error in one of the lower decimal positions. You can read more about the floating point trap on the Finxter blog.

Related Tutorial: Floating Point Error Explained

Python divmod() Negative Numbers

Can you use the divmod() method on negative numbers for the dividend or the divisor?

You can use divmod(a, b) for negative input arguments a, b, or both. In any case, if both arguments are integers, Python performs integer division a // b to obtain the first element and modulo division a % b to obtain the second element of the returned tuple. Both operations allow negative inputs a or b. The returned tuple (x, y) is calculated so that x * b + y = a.

Here’s an example of all three cases:

>>> divmod(-10, -3)
(3, -1)
>>> divmod(-10, 3)
(-4, 2)
>>> divmod(10, -3)
(-4, -2)

Python divmod() Performance — Is It Faster Than Integer Division // and Modulo % Operators?

There are two semantically identical ways to create a tuple where the first element is the result of the integer division and the second is the result of the modulo operation:

  • Use the divmod(a, b) function.
  • Use the (a // b, a % b) explicit operation with Python built-in operators.

Next, we measure the performance of calculating the elapsed runtime in milliseconds when performing 10 million computations for relatively small integers. Let’s start with divmod():

import time
import random # Small Operands
operands = zip([random.randint(1, 100) for i in range(10**7)], [random.randint(1, 100) for i in range(10**7)]) start = time.time() for i, j in operands: divmod(i, j) stop = time.time()
print('divmod() elapsed time: ', (stop-start), 'milliseconds')
# divmod() elapsed time: 1.7654337882995605 milliseconds

Compare this to integer division and modulo:

import time
import random # Small Operands
operands = zip([random.randint(1, 100) for i in range(10**7)], [random.randint(1, 100) for i in range(10**7)]) start = time.time() for i, j in operands: (i // j, i % j) stop = time.time()
print('(i // j, i % j) elapsed time: ', (stop-start), 'milliseconds')
# (i // j, i % j) elapsed time: 1.9048900604248047 milliseconds

The result of this performance benchmark is that divmod() requires 1.76 milliseconds and the explicit way of using integer division and modulo requires 1.90 milliseconds for 10,000,000 operations. Thus, divmod() is 8% faster. The reason is that the explicit way performs many duplicate operations to calculate the result of the integer division and the modulo operation which internally uses integer division again. This effect becomes even more pronounced if you use larger integers.

Performance difference divmod() vs Integer Division and Modulo

Python divmod() Implementation

For integer input arguments, here’s a semantically equivalent divmod() implementation:

>>> def divmod_own(x, y): return (x // y, x % y) >>> divmod_own(10, 3)
(3, 1)
>>> divmod(10, 3)
(3, 1)

But note that this implementation still performs redundant computations (e.g., integer division) and, therefore, is less efficient than divmod().

Summary

Python’s built-in divmod(a, b) function takes two integer or float numbers a and b as input arguments and returns a tuple (a // b, a % b).

In case of float inputs, divmod() still returns the division without remainder by rounding down to the next round number.


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python divmod() — A Simple Guide with Video first appeared on Finxter.

Posted on Leave a comment

How to Get Started With Python Dash on PyCharm [Absolute Beginners]

This is a chapter draft for our upcoming book “Python Dash” with NoStarch—to appear in 2021. Stay tuned!

Why an IDE

Using an integrated development environment (IDE) has the potential to significantly accelerate your programming productivity. Many programmers do not unlock their full potential until they finally decide to switch from a simple code editor to an IDE—and mastering the advanced functionality provided by the IDE. Some advantages of IDEs over simple text editors are code highlighting, tooltips, syntax checker, code linters that check for style issues, version control to safeguard the history of programming edits, debugging with the help of breakpoints, visual aids such as flowcharts and block diagrams, performance optimization tools and profilers—just to name a few.

PyCharm for Dash Apps

In this book about dashboard applications, we recommend that you also take your time to switch to an IDE, if you haven’t already. In particular, we recommend that you use the PyCharm IDE to follow along with the provided code examples. Apart from the benefits of using IDEs, you’ll also develop web applications that can quickly grow by adding more and more features. As your Python dashboard applications grow, so will your need to aggregate all source code at a single spot and in a single development environment. Increasing complexity quickly demands the use of an IDE.

In the following, we’ll describe how to download and install PyCharm, and create your first simple dashboard application that you can view in your browser. After you’ve completed those steps, you’re well-prepared to duplicate the increasingly advanced applications in the upcoming chapters.

Download PyCharm

First, let’s start with downloading the latest PyCharm version. We assume you have a Windows PC, but the steps are very similar on a macOS and Linux computer. As soon as you’ve launched the PyCharm application, the similarity of usage increases even more across the different operating systems.

You can download the PyCharm app from the official website.

Click the download button of the free community version and wait for the download to complete.

Install PyCharm on Your Computer

Now, run the executable installer and follow the steps of the installation application. A sensible approach is to accept the default settings suggested by the PyCharm installer.

Congratulations, you’ve installed PyCharm on your system!

Open PyCharm

Now type “PyCharm” into the search bar of your operating system and run the IDE!

Create a New Dash Project in PyCharm

After choosing “New Project”, you should see a window similar to this one:

This user interface asks you to provide a project name, a virtual environment, and a Python interpreter. We call our project firstDashProject, use a virtual environment with the standard Python installation, and don’t create a main.py welcome script:

Create the project and you should see your first PyCharm dashboard project!

Create Your Dash File app.py in Your PyCharm Project

Let’s create a new file app.py in your project and copy&paste the code from the official documentation into your app.py file:

# -*- coding: utf-8 -*- # Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser. import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) # assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({ "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"], "Amount": [4, 1, 2, 2, 4, 5], "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
}) fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group") app.layout = html.Div(children=[ html.H1(children='Hello Dash'), html.Div(children=''' Dash: A web application framework for Python. '''), dcc.Graph( id='example-graph', figure=fig )
]) if __name__ == '__main__': app.run_server(debug=True)

You can get the code from the official Dash tutorial: https://dash.plotly.com/layout

Your PyCharm dashboard project should now look like this:

Debug Your Dash App Using PyCharm’s Tooltips

Now, let’s try to run our project by using the top menu and select Run > app.py. Unfortunately, it doesn’t already work—PyCharm doesn’t recognize dash!

You can easily fix this by hovering over the red underlined “dash” library import in your app and choosing the “install package dash” option.

This is one great advantage of an IDE is that installing dependencies in your Python projects is as simple as accepting the tooltips provided by your intelligent development environment.

Install Dash in Your Virtual Environment

Installing the dash library will take a few moments. Note that the library will be installed only in a virtual environment which means that it’ll install it not on your global operating system but only on a project level. For a different project, you may have to install dash again. While this may sound tedious, it’s actually the most Pythonic way because it keeps dependency management simple and decentralized. There won’t be any version issues because your first project needs version 1 and your second project needs version 2 of a given library. Instead, each project installs exactly the version it needs.

Install Pandas in Your Virtual Environment

PyCharm will tell you when it is done with installing the dash library in the virtual environment. Now repeat the same procedure for all red-underlined libraries in the project. If you used the code given above, you’ll have to install the pandas library (see Chapter 3) as well in your local environment. A few moments later, the pandas installation will also successfully complete. The red underlined error messages in your code will disappear and you’re ready to restart the project again by clicking “Run”.

Exploring Your First Dash App in Your Browser

On my machine, the output after running the app.py file in PyCharm is:

C:\Users\xcent\Desktop\Python\firstDashProject\venv\Scripts\python.exe C:/Users/xcent/Desktop/Python/firstDashProject/app.py
Dash is running on http://127.0.0.1:8050/ * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on

Note the highlighted line (in bold). You can now copy the URL http://127.0.0.1:8050/ and paste it into your browser—the dashboard app runs on a local server that is hosted on your machine with IP address 127.0.0.1 and port 8050.  

When you visit this URL in your browser, you should see your first Dash application!

Congratulations, you’re now well-prepared to run all dashboard apps in this book—and beyond it as well—using similar steps. For further reading on PyCharm, feel free to check out our multi-site blog tutorial on https://academy.finxter.com/course/introduction-to-pycharm/

The post How to Get Started With Python Dash on PyCharm [Absolute Beginners] first appeared on Finxter.