Skip to main content

Type Conversion in Python: int(), float(), str() and bool() Explained

Have you ever entered a number into a Python program but received an error when trying to perform a calculation? This is a common problem for beginners. It often happens because the data type of a value is different from what the program expects. For example, when you use Python's input() function, the value entered by the user is returned as a string. If you want to perform mathematical calculations with that value, you may need to convert it into a numeric type first. This process is called type conversion . In this article, we will learn what type conversion is, why it is useful, how to convert values using int() , float() , str() , and bool() , and how to avoid common errors. 1. What Is Type Conversion in Python? Type conversion is the process of converting a value from one data type to another. For example, you may convert: A string into an integer. An integer into a floating-point number. A number into a string. A value into a Boolean value. Consider this example: number = ...

Understanding Git Merge Conflicts: Why They Happen and How to Resolve Them

Imagine you and your friend are working on the same Java project.

Both of you edit the same file:

Login.java

You change the login message to:

System.out.println("Welcome User");

At the same time, your friend changes the same line to:

System.out.println("Welcome Student");

Now both of you try to merge your work into the main branch.

Git faces a problem.

It sees two different changes to the same part of the same file and cannot determine which version should be kept.

Instead of making a wrong decision, Git stops the merge and asks you to resolve the conflict manually.

This situation is called a Merge Conflict.

Although merge conflicts may seem intimidating at first, they are a normal part of collaborative software development. Every professional developer encounters them, and Git provides clear tools to resolve them safely.

In this article, you will learn:

  • What a merge conflict is

  • Why conflicts happen

  • When conflicts occur

  • How Git displays conflicts

  • How to resolve conflicts step by step

  • Useful commands during conflict resolution

  • How to avoid conflicts

  • Best practices

By the end of this article, you'll be able to resolve merge conflicts with confidence.


What is a Merge Conflict?

A merge conflict occurs when Git cannot automatically combine changes from two branches.

Git can merge different changes automatically.

However, if the same part of the same file has been modified differently, Git needs human input to decide which version is correct.


Why Do Merge Conflicts Occur?

Suppose the original code is:

System.out.println("Welcome");

Developer A changes it to:

System.out.println("Welcome User");

Developer B changes it to:

System.out.println("Welcome Student");

When Git tries to merge these branches, both versions cannot exist on the same line.

Git therefore pauses the merge and requests your decision.


Common Situations That Cause Conflicts

Merge conflicts commonly occur when:

  • Two developers edit the same line of a file.

  • One developer deletes a file while another modifies it.

  • A file is renamed in one branch and modified in another.

  • The same block of code is edited differently in multiple branches.

  • Large feature branches remain unmerged for a long time.


Situations That Usually Do NOT Cause Conflicts

Suppose:

Developer A edits:

login()

Developer B edits:

payment()

These are different parts of the file.

Git can usually merge these changes automatically without any conflict.


A Practical Example

Suppose your repository contains:

public class Login
{
    public static void main(String args[])
    {
        System.out.println("Welcome");
    }
}

Create a branch:

git switch -c feature-login

Modify the line:

System.out.println("Welcome User");

Commit the change.

Now switch back:

git switch main

Modify the same line differently:

System.out.println("Welcome Student");

Commit again.

Now merge:

git merge feature-login

Git displays something similar to:

Auto-merging Login.java
CONFLICT (content): Merge conflict in Login.java
Automatic merge failed; fix conflicts and then commit the result.

The merge pauses until the conflict is resolved.


What Happens Internally?

Git compares:

Common Ancestor
        │
        ├──────── main
        │
        └──────── feature-login

It tries to combine both branches.

When two incompatible changes affect the same section, Git cannot choose automatically.


Conflict Markers

Git inserts special markers into the affected file.

Example:

<<<<<<< HEAD
System.out.println("Welcome Student");
=======
System.out.println("Welcome User");
>>>>>>> feature-login

These markers are temporary and help you identify the conflicting changes.


Understanding the Conflict Markers

<<<<<<< HEAD

This marks the beginning of the changes from the branch you are currently on.

Example:

<<<<<<< HEAD

If you are on main, this section shows the main version.


=======

This separates the two versions.

Everything above it belongs to the current branch.

Everything below it belongs to the branch being merged.


>>>>>>> feature-login

This marks the end of the conflicting section and identifies the incoming branch.


How to Resolve the Conflict

Read both versions carefully.

Decide which one should remain.

Possible choices:

Option 1

Keep the current branch:

System.out.println("Welcome Student");

Option 2

Keep the incoming branch:

System.out.println("Welcome User");

Option 3

Combine both ideas:

System.out.println("Welcome User");
System.out.println("Welcome Student");

or perhaps:

System.out.println("Welcome User and Student");

Choose the version that best fits your application.


Remove the Conflict Markers

After deciding, remove:

<<<<<<< HEAD
=======
>>>>>>> feature-login

The file should contain only valid source code.

For example:

public class Login
{
    public static void main(String args[])
    {
        System.out.println("Welcome User");
    }
}

Completing the Merge

After editing the file:

Stage it:

git add Login.java

Complete the merge:

git commit

Git creates the merge commit.

The conflict has now been resolved.


Checking the Repository

Run:

git status

If everything has been resolved successfully, Git reports a clean working tree after the merge commit.


Viewing the Merge History

Use:

git log --oneline --graph

Example:

*   Merge branch 'feature-login'
|\
| * Update login message
* | Update welcome text
|/
* Initial commit

The graph shows both branches joining into a merge commit.


What if You Want to Cancel the Merge?

Suppose you start a merge but decide you are not ready.

Before completing the merge, you can abort it:

git merge --abort

Git attempts to return your repository to the state it was in before the merge started.

This command works only if the merge has not yet been completed.


Merge Conflict vs Syntax Error

These are different problems.

Merge ConflictSyntax Error
Caused by incompatible changes between branchesCaused by invalid program code
Git detects it during a mergeCompiler or interpreter detects it while building or running the program
Resolved by editing conflicting sectionsResolved by fixing the program logic or syntax

Real-World Example

Suppose four developers are working on an E-Commerce website.

Developer A:

feature-login

Developer B:

feature-payment

Developer C:

feature-cart

Developer D:

feature-orders

Developer A and Developer B both modify the same authentication method.

When the branches are merged, Git reports a conflict.

The team discusses the correct implementation, edits the file, stages it, and completes the merge.

This collaborative decision helps ensure the final code is correct.


How to Reduce Merge Conflicts

Although conflicts cannot always be avoided, you can reduce them by following good practices.

  • Pull the latest changes frequently.

  • Merge or rebase feature branches regularly.

  • Keep feature branches small and focused.

  • Avoid making unrelated changes in the same branch.

  • Communicate with teammates before modifying shared code.

  • Review code before merging.


Common Beginner Mistakes

Mistake 1

Deleting the conflict markers incorrectly and leaving invalid code behind.

Always verify that the file still compiles.


Mistake 2

Choosing one version without reading the other.

Review both changes carefully before deciding.


Mistake 3

Forgetting to stage the resolved file.

Remember:

git add <filename>

is required after resolving the conflict.


Mistake 4

Thinking Git is broken.

Merge conflicts simply mean Git needs human guidance.

They are a normal part of collaborative development.


Best Practices

  • Commit your work before starting a merge.

  • Read both conflicting versions carefully.

  • Test the application after resolving conflicts.

  • Use meaningful commit messages.

  • Keep branches short-lived when possible.

  • Communicate with teammates about shared code.


Useful Commands During Conflict Resolution

CommandPurpose
git statusShow conflicted files
git merge feature-nameStart the merge
git add <file>Mark a conflict as resolved
git commitComplete the merge
git merge --abortCancel an unfinished merge
git log --oneline --graphView merge history

Summary

Merge conflicts occur when Git cannot safely combine changes from different branches automatically. Rather than making an incorrect decision, Git pauses the merge and asks the developer to resolve the conflicting sections.

By understanding conflict markers, reviewing both versions carefully, and completing the merge with git add and git commit, you can confidently resolve conflicts while preserving the correct functionality of your project.

Merge conflicts are not errors in Git—they are an expected part of collaborative software development. 

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...