Software engineering and personal development

Category: Uncategorized (Page 1 of 2)

How to Quickly Get the Index of an Element in a Tuple in Python

In Python, tuples are an ordered and immutable collection of elements. They are often used to store related pieces of information together, such as the x and y coordinates of a point or the name and age of a person. Sometimes, we may need to find the position of a particular element within a tuple. Python provides a built-in method called index() that makes it easy to accomplish this task. In this article, we will explore how to use the index() method to get the index of an element in a tuple.

The index() method is a built-in method in Python that returns the index of the first occurrence of a specified element in a tuple. The method takes a single argument, which is the element to search for. Here’s an example:

my_tuple = ('a', 1, 'f', 'a', 5, 'a')
print(my_tuple.index('f'))  # 2

In this example, we created a tuple called my_tuple that contains six elements. We then called the index() method on the tuple, passing in the string 'f' as the argument. The method returns the index of the first occurrence of 'f' in the tuple, which is 2. We printed the result to the console using the print() function.

If the specified element is not present in the tuple, the index() method raises a ValueError exception. For example:

my_tuple = ('a', 1, 'f', 'a', 5, 'a')
print(my_tuple.index('z'))  # ValueError: tuple.index(x): x not in tuple

In this example, we called the index() method on the my_tuple tuple, passing in the string 'z' as the argument. Since 'z' is not present in the tuple, the method raises a ValueError exception.

That’s basically it.

I hope you find this useful.

Understanding False Values in Python: None, “False”, and 0

When working with conditional statements in Python, it’s important to understand what values evaluate to True and what values evaluate to False. While some values are obviously True, such as any non-zero number or a non-empty string, there are a few other values that can sometimes trip up programmers. In particular, the values None, "False", and the number 0 are all examples of values that evaluate to False in Python.

Continue reading

Turn your JSONs into an easy-to-read format with this Chrome and Firefox extension

Img source: Chrome web store

JSON is a very popular file format. Sometimes we may have a JSON object inside a browser tab that we need to read and it can be difficult. We may need to go and search for an online tool that turns it into an easy-to-read format, so that we can understand it. Now here is a Chrome and Firefox extension that does the formatting and makes your JSONs instantly pretty inside your browser, without having to go and do many unnecessary steps. 

Continue reading

Fix the hanged  on new application creation process in React Native by installing uws separately

Img source: geekycentral.com

I recently changed my laptop and while I was trying to create a new React Native application in my new development environment, the process of creating the app seemed to hang on forever. It did not finish at all, and took a lot of my time, as I was hoping that it will ultimately finish. I was worried about this and this way, I tried to find solutions online in as many places as I could. It took a lot of test and error cases, and a lot of browsing until I finally stumbled upon a solution that will hopefully be beneficial to you as well.

The last message that showed up on the terminal when I was trying to create a new React Native was: “Building fresh packages react native”.

What I had to do was run the following command in the terminal:

yarn add uws

Then I was able to create new React Native application using the React Native command line interface within seconds. 

There may be other solutions to this problem, and this is the one that worked for me. If you are having the same issue, then chances are this solution can hopefully work for you as well.

Our different selves through time that prevent us from a qualitative lifestyle

Source: lookbody.top

Source: lookbody.top

In a recent email, the author of the book The Best Place to Work: The Art and Science of Creating an Extraordinary Workplace, Rod Friedman mentioned something very interesting. He told a story that happened to him and his daughter recently. A couple of days ago he went to the cinema with his daughter and although they had an unpleasant experience while watching the movie, they did not recall that particular story that way. They initially did not like the movie and considered it as not worth their time, but later on, as they were thinking a bit differently. They actually did change their opinion about the movie and thought that it was not that bad, and that it did deserve worth a watch. Rod considered this occurrence to happen as a result of our different selves that might change throughout different times. Continue reading

« Older posts

© 2024 Fatos Morina

Theme by Anders NorenUp ↑