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 = ...

Creating, Listing, Switching, and Deleting Branches in Git (git branch & git switch)

In the previous article, you learned what a Git branch is and why branches are one of Git's most powerful features.

Now it's time to start using them.

Imagine you are developing a Student Management System. The current version of your project is stable, but you need to add a new feature called Student Attendance.

Instead of modifying the stable code directly, you decide to create a separate branch.

How do you create a branch?

How do you switch to it?

How do you see all available branches?

How do you delete a branch after it has been merged?

Git provides simple commands to perform all these tasks:

  • git branch

  • git switch

In this article, you will learn:

  • How to list branches

  • How to create branches

  • How to switch branches

  • How to create and switch in one step

  • How to rename branches

  • How to delete branches

  • The difference between git switch and git checkout

  • Common mistakes

  • Best practices

By the end of this article, you'll be able to manage branches confidently in your own projects.


Listing All Branches

To see all local branches in your repository, use:

git branch

Example output:

* main

The asterisk (*) indicates the branch you are currently working on.

In this example, you are on the main branch.


Understanding the Output

Suppose your repository contains three branches.

Running:

git branch

may display:

* main
  feature-login
  feature-payment

Here:

  • main is your current branch.

  • feature-login exists but is inactive.

  • feature-payment also exists but is inactive.

Only one branch can be checked out at a time.


Creating a New Branch

To create a new branch:

git branch feature-attendance

Git creates the branch.

However, something important happens...

You remain on your current branch.

Run:

git branch

Output:

* main
  feature-attendance

Notice that the asterisk is still next to main.

The new branch exists, but you are not using it yet.


Visual Representation

Before creating the branch:

main

↓

Commit A

↓

Commit B

After:

git branch feature-attendance

The repository looks like:

main
   │
   ▼
Commit B

feature-attendance
         │
         ▼
Commit B

Both branches point to the same commit.


Switching to Another Branch

To start working on the new branch:

git switch feature-attendance

Git displays:

Switched to branch 'feature-attendance'

Now:

git branch

Output:

  main
* feature-attendance

The asterisk has moved.

You are now working on the new branch.


Understanding HEAD

When you switch branches, Git moves the HEAD pointer.

Initially:

HEAD

↓

main

After switching:

HEAD

↓

feature-attendance

Every new commit will now be added to feature-attendance.


Creating and Switching in One Command

Instead of:

git branch feature-login

git switch feature-login

You can use:

git switch -c feature-login

The -c option means:

Create a new branch and immediately switch to it.

This is one of the most commonly used commands in modern Git workflows.


Working on Different Branches

Suppose:

main

contains:

System.out.println("Version 1");

You switch:

git switch feature-login

Now you modify the file:

System.out.println("Version 2");

Commit the change.

When you switch back:

git switch main

You'll see the original version again because main has not received the new commit yet.

This isolation is what makes branches so useful.


Listing Branches with More Information

Use:

git branch -v

Example output:

* main               a3f29b1 Initial project
  feature-login      d8e45c7 Add login page

This shows:

  • Branch name

  • Latest commit hash (short form)

  • Latest commit message


Showing Merged Branches

To see branches that have already been merged into the current branch:

git branch --merged

Example:

* main
  feature-login

These branches are generally safe to delete if you no longer need them.


Showing Unmerged Branches

Use:

git branch --no-merged

Example:

feature-payment
feature-dashboard

These branches still contain work that has not been merged into the current branch.


Renaming a Branch

Suppose you're currently on:

feature-login

Rename it:

git branch -m feature-authentication

Now:

git branch

Output:

* feature-authentication
  main

Renaming Another Branch

If you're not currently on the branch you want to rename:

git branch -m old-name new-name

Example:

git branch -m feature-ui feature-interface

Deleting a Branch

Suppose feature-login has already been merged into main.

Delete it:

git branch -d feature-login

Git removes the branch reference.

The commits remain in the repository because they have already been merged.


Safe Delete vs Force Delete

Safe deletion:

git branch -d feature-login

Git checks whether the branch has been merged.

If it has not, Git refuses to delete it.

Force deletion:

git branch -D feature-login

The uppercase -D forces the deletion even if the branch has not been merged.

Use this carefully because unmerged work may become harder to recover.


Can You Delete the Current Branch?

No.

Suppose you're currently on:

feature-login

Running:

git branch -d feature-login

will fail.

Switch to another branch first:

git switch main

Then delete:

git branch -d feature-login

git switch vs git checkout

Older versions of Git used:

git checkout feature-login

to switch branches.

Today, Git recommends:

git switch feature-login

Why?

Because git checkout had multiple responsibilities:

  • Switching branches

  • Restoring files

This confused many beginners.

Git introduced:

  • git switch → For switching branches

  • git restore → For restoring files

These commands are clearer and easier to understand.


Real-World Example

Suppose you work on an Online Shopping website.

Current branch:

main

Create:

git switch -c feature-cart

Develop the shopping cart.

Commit your work.

Switch back:

git switch main

Your stable version remains unchanged until you intentionally merge the new feature.


Common Beginner Mistakes

Mistake 1

Creating a branch but forgetting to switch to it.

Remember:

git branch feature-login

does not change your current branch.


Mistake 2

Using -D without understanding the consequences.

Always try:

git branch -d

before using:

git branch -D

Mistake 3

Making new feature changes directly on main.

Create a feature branch instead.


Mistake 4

Deleting branches before ensuring their work has been merged or backed up.


Best Practices

  • Keep main stable.

  • Create a separate branch for each feature or bug fix.

  • Use meaningful branch names such as:

    • feature-login

    • feature-payment

    • bugfix-authentication

  • Delete branches after they have been merged.

  • Use git branch -v to review branch status regularly.


Useful Branch Commands

CommandPurpose
git branchList local branches
git branch <name>Create a new branch
git switch <name>Switch to a branch
git switch -c <name>Create and switch to a new branch
git branch -vShow branches with latest commit
git branch --mergedList merged branches
git branch --no-mergedList unmerged branches
git branch -m <new-name>Rename the current branch
git branch -m <old> <new>Rename another branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force delete a branch

Summary

Managing branches is an essential Git skill. The git branch command lets you create, list, rename, and delete branches, while git switch provides a modern and straightforward way to move between them.

By creating a separate branch for each feature or bug fix, you can work safely without affecting the stable version of your project. Once your work is complete, the branch can be merged and, if no longer needed, deleted to keep the repository organized.

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...