Category: Python

List Comprehension in Python

There are several ways to create a list in python. Most of us know about for loop. But there is another way which is more pythonic. It is known as List Comprehension.

In for loop, we have to create an empty list first, then append items to the list. But in the list comprehension, we can do it just with a single line of code without creating an empty list.

Here is an example:

Let’s create a list with ‘hello’ using for loop.

char_list = []
for char in 'hello':
    char_list.append(char)

print(char_list)

At first, we created an empty list called char_list. After that we ran a for loop to iterate over each character of the ‘hello’. In the third line, we append each character to the char_list.

The output will look like this:

[ 'h', 'e', 'l', 'l', 'o' ]

let’s rewrite the the above code with list comprehension. We will see how short and nice it is.

char_list = [char for char in 'hello']
print(char_list)

We will get same result here. but it is more readable and elegant. It is also more pythonic.

Unit Testing Graphene-Django API with Pytest – 3 Steps

In modern days, automated software testing is extremely important to kill the bugs during development. Unit testing is the first level of software testing, which a developer can not and should not avoid. Because it helps the developer to write bug-free, secure and robust codes.

What is Unit Testing?

Unit testing is a software testing method where every individual component and every single unit of the software is tested. Most of the time, unit testing is performed by the developer. Some times it is done by a QA engineer.

When you are doing TDD (Test Driven Development), you will write the unit test before starting the actual code. You can also write unit tests after finishing the specific feature. However, unit testing is performed during the coding stage and before the integration test.

Read More

How to build GraphQL API with Django – 7 Steps

What is GraphQL?

Technology is evolving so fast. Every day, old technologies are being replaced by new ones. For many years, REST architecture was dominating web services and APIS. In 2012, Facebook developed GraphQL and open-sourced it in 2015. Since then, it is becoming so popular in the development world. GraphQL is a declarative, strongly typed, data-driven query language to build APIs.

With GraphQL, you can ask exactly what you need from the server with a single API endpoint. Moreover, It aggregates data from multiple related tables and sources going as much deep as needed. In addition, with a strong type checking mechanism, GraphQL helps developers to write more reliable, robust and bug-free codes.

Read More

Powered by WordPress & Theme by Anders Norén