Author: robertsgreibers

How Do You Repeat Code In Python?

One of my first tasks as a new QA engineer / Python dev back in the day was to figure out how do you repeat code in Python, how to iterate from 0 to 10 in Python or how do I repeat code in Python forever. Let’s look into possible solutions in this post.

How Do You Repeat Code In Python?

There are a few ways to repeat code in Python, but for loops are a very common way to repeat code in Python, especially when you need to iterate over a sequence of items (such as a list, tuple, or string).

They are easy to use and can be more efficient than using a while loop, especially when you know exactly how many times you need to repeat the code.

To repeat a block of code a specific number of times use:

# Repeat 5 times
for index in range(5):
    print(f'iteration index: {index}')
Continue reading

How Do I Open A Log File In Python?

In my early days as a QA engineer / Python developer, I was working on test-related projects a lot. For such projects, the focus is on logging and the question: “How do I open a log file in Python?” comes up on a daily basis. Let’s look into possible solutions in this post.

How Do I Open A Log File In Python?

In Python, you can use the open() function to open a log file.

This function needs to know the file path and the mode (like ‘read’ or ‘write’) that you want to use.

In Python, the r mode is used to open a file in read mode.

with open('log.txt', 'r') as log_file:
    content = log_file.read()

print('content: ', content)

This means that when you open a file with this mode, you can read the data from the file, but you cannot write to it.

Continue reading

What Is dict() In Python? (5 Examples)

Back when I used to work as a QA engineer and when I first got into Python (around 7 years ago), one of the key concepts I learned to use was dict() in Python. You might wonder what is dict()in Python and what are the main features of a dictionary in Python? Let’s discuss the answer to these questions in great detail below. 

What Is dict() In Python?

In Python, the dict() function is used to create a dictionary.

A dictionary is a collection of key-value pairs, where each key is associated with a value.

This function takes in a sequence of items and returns a dictionary with the items in the sequence as keys and values.

For example, you could use the dict() function to create a dictionary like this:

my_dict = dict(
    [
        ('key1', 'value1'),
        ('key2', 'value2')
    ]
)
Continue reading

How Do You Change A Specific Line In A Text File In Python?

I used to be a QA engineer and when I first got into Python, one of the first questions that came to my mind while working on Python scripts was: “How Do You Change A Specific Line In A Text File In Python?” We will discuss the answer to this question in much detail below. 

How Do You Change A Specific Line In A Text File In Python?

To change a specific line in a text file in Python, you can use the replace() method on strings, which allows you to specify the text you want to replace and the text you want to replace it with.

Here is a quick example:

# Open the file in read mode
with open('your_text_file_path.txt', 'r') as f:
    # Read the contents of the file into a list of lines
    lines = f.readlines()

# Use for loop to go through each line in a "your_text_file_path.txt"
for line_index, line in enumerate(lines):

    # Check each line for "old_text" and replace it
    # with "new_text" if it is found
    lines[line_index] = (
        lines[line_index]
        .replace('old_text', 'new_text')
    )

# Open a new file in writing mode (this will create a new file)
with open('your_new_text_file_path.txt', 'w') as f:
    # Write the modified lines to the new "your_new_text_file_path.txt"
    f.writelines(lines)
Continue reading

Can Learning Python Get You A Job?

I used to be a QA engineer and I used to think: “Can learning Python get you a job?” on a daily basis.. And by job, I mean a job as a software developer. So, can learning Python get you a job and your entry into the industry of software development? We will discuss the answer to this question in much detail below. 

Can Learning Python Get You A Job?

Yes, learning Python can be a good way to improve your job prospects and open up new career opportunities. Many companies are looking for developers who are proficient in Python and knowing Python can make you a more competitive job candidate. 

Python is a widely used, high-level programming language that is well-suited to many different types of programming tasks, from web development and data analysis to scientific computing and artificial intelligence. 

Additionally, learning Python can help you to develop a wide range of valuable skills, such as problem-solving, critical thinking, and attention to detail, which are highly sought after by employers. 

Overall, learning Python can be a great investment in your future career.

Continue reading

Can You Parse A String In Python?

If you’re an engineer (I used to be a QA engineer) you’ll have to extract information from a string and it’s likely you’re going to use Python for it. So, can you parse a string in Python? We will discuss the answer to this question in much detail below. 

Can You Parse A String In Python?

Yes, you can parse a string in Python. Parsing a string in Python is a common task that is performed when working with data. It involves taking a string and breaking it down into smaller pieces, or tokens so that it can be more easily manipulated and processed.

Python has a number of built-in functions and methods that can be used to build your own Python parser script. 

Continue reading

Class Python Example: String Validation

Hello! It’s Roberts Greibers here. 

Having worked in IT industry as a QA (software engineer) and later as a Python developer for more than 7 years, I’ve gathered numerous Python examples of classes.

I’ll tell you more about my experience as a Software Engineer/Python developer later in the post..  ⚑️

But now, let me list what Python classes examples you’re about learn in this post..

Read and pay ATTENTION!

You’re about to explore the MOST important parts of what is a class in Python! πŸš€

One of the CORE understandings for objects in Python (Object Oriented Programming with Python)

In this post, you’re going to find out:

  • What is a class in Python?
  • How to define a Python class blueprint?
  • How to call Python class method?
  • How to execute Python class as a callable function?

I’ll do my best to make you understand classes in Python from this one blog post!

Also, if you’re coming from Javascript world, a keyword this might be familiar..

.. well in Python there isn’t “Python class this“, but there’s a similar concept we use.

And at the end, I’ll also share more about how my students are using classes in Python training – this might be an OPPORTUNITY FOR YOU to follow their steps!

More on that at the end of the post! πŸ‘‡πŸ»

Continue reading

Python Requests POST GET (HTTP Request Explained)

Welcome back or nice to see you for the first time!

Roberts Greibers here with another experience-based post about Python requests.

I’ve been working professionally as a Python developer for well over 7 years, I’ll share more details about my story later in the post.. (just scroll down the page to find out more about me). 

But before we talk about how qualified I am to write posts about Python requests, I’ll just give you all the answers you’ve been Googling for. πŸš€

WARNING: You’re about to see the most important Python requests library use cases with clearly defined examples. 

This is the BIGGEST breakthrough you’ll ever achieve as a Python developer.

Learning and implementing this way of calling API with a Python request is a major achievement in your career that you’ll use on a daily basis for years to come. 

In this post, you’re going to explore:

🚨 I’ll explain in detail everything you should know about Python3 requests (why Python2 requests are NOT used anymore)

And I’ll show you how API calls in Python are done in a real project (from Django projects I’ve worked on)

The intention here is to give you a real Python HTTP request example you could use in your own projects. 

Just a quick note before we start: At the end of the post I’ll share details about what helped my students to get hired as Python developers, read till the end and see if that helps you too!πŸ‘‡πŸ»

Continue reading

Python Read CSV File (Python Excel Reader)

Hey! It’s Roberts Greibers here.

I’ll share details about my 7+ years of experience as a Python developer with you a bit later in the post (you’ll have to scroll down to find them).

But now, let’s talk about how I came to write my “read from CSV Python example” script.. πŸš€

Firstly, understand that CSV is a file format supported by nearly all platforms..

That’s why for Python programming purposes I’d suggest you use CSV format instead of any other excel format. 

It’s possible to find a Python excel library and use Python for excel files in general, but in this post, we’re going to strictly focus on how to read CSV file in Python. 

Also, we’re going to check:

  • Should you learn Excel or Python? (using Python in Excel?)
  • Are there any ways you can use Python for excel? (e.g. Parsing CSV Python)
  • Any Python excel library you could find and use? (e.g. Python CSV file reader)
  • How to import CSV file in Python? (the process of importing CSV into Python)

If you’ve been Googling for “Python Read CSV” or the typical “Reading CSV file in Python” you’ve found the RIGHT place! πŸŽ‰

I’m about to explain the details of how I optimize my time by parsing spreadsheets in Python (e.g. CSV in Python) and how I avoid manual CSV reading tasks using Python. 

Building a quick CSV file reader in Python can take you up to 30 minutes and save you hours later assuming you’re using the script on a daily basis. 

Let’s get into it and I’ll also share a way that helped me and might even help you to become a professional Python developer at the end of the post πŸ‘‡πŸ»

Continue reading

Subprocess In Python (Run Command Line)

Good evening, Roberts Greibers here.

We will get into my Python subprocess story and how I even got the point of writing blog posts about Python a bit later. 

But at this moment, I want you to pay attention and read very carefully!

What I’m about share with you is a real subprocess Python example! 🚨

How to use subprocess in Python to run command line commands!

The difference between running a shell command in Python with:

Also, a real example of me actually executing a shell command in Python.

Most “Python subprocess example” tutorials you’ll find online won’t give you a REAL project example..

And they won’t tell you what steps you’ll need to take in your specific situation. 

In this post, I’ll share how you should THINK in order to be able to come up with a solution for your own “how to run shell command in Python?” problem.. πŸš€

If this sounds interesting to you, keep on reading, I’ll also give you an opportunity to become a Python developer at the end of the post πŸ‘‡πŸ»

Continue reading