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

How to Run Python Programs

Learning Python is not only about writing code. You also need to understand how to run a Python program and see the result produced by your code.

After learning about the Python interpreter, IDLE, VS Code, and Python extensions, the next important step is learning how Python programs are executed.

In this article, we will understand what it means to run a Python program, how to run Python code using different environments, and what happens when you execute a Python file.


What Does It Mean to Run a Python Program?

When you write Python code, the computer does not automatically execute it.

For example:

print("Hello, Python!")

When you run this program, Python processes the code and produces the output:

Hello, Python!

So, in simple terms:

Running a Python program means asking Python to execute the instructions written in your Python source code.


What Is a Python Program?

A Python program is a collection of instructions written using the Python programming language.

For example:

name = "Python"
print("Hello", name)

These instructions tell Python to:

  1. Store "Python" in the variable name.

  2. Display a message using that variable.

The program can be saved in a file such as:

hello.py

What Is a .py File?

Python source-code files normally use the .py extension.

Examples:

hello.py
calculator.py
student.py
prime.py
factorial.py

The .py extension indicates that the file contains Python source code.

For example:

print("Welcome to Python")

can be saved as:

welcome.py

Different Ways to Run Python Programs

There are several ways to run Python programs.

Some common environments include:

  1. Python Shell

  2. IDLE

  3. Command Line / Terminal

  4. VS Code

  5. Other Python development environments

The method may be different, but the basic purpose remains the same: execute Python code and obtain the result.


1. Running Python Code in the Python Shell

The Python Shell is useful for executing small pieces of code immediately.

For example:

>>> 10 + 20
30

Another example:

>>> print("Hello Python")
Hello Python

Here, Python executes the command immediately after you enter it.

This is useful for testing small expressions and learning Python concepts.


2. Running a Program Using IDLE

IDLE provides both an interactive Shell and an Editor.

Suppose you create a file called:

hello.py

with:

print("Hello World")

You can run the program from IDLE.

The result will appear in the Python Shell:

Hello World

This is particularly convenient when learning basic Python programming.


3. Running Python from the Terminal

Python programs can also be executed from a command-line terminal.

Suppose your file is:

hello.py

You can run it using:

python hello.py

On some systems, the command may be:

python3 hello.py

If the program contains:

print("Hello Python")

the output will be:

Hello Python

What Is a Terminal?

A terminal is a text-based interface where you can enter commands.

Instead of clicking buttons, you type commands.

For example:

python hello.py

The command tells the Python environment to execute the file.

Terminals are widely used by programmers because they provide a direct way to work with programs and development tools.


4. Running Python Programs in VS Code

VS Code provides several ways to run Python programs.

Suppose you have created:

calculator.py

with:

a = 10
b = 20

print("Sum =", a + b)

You can run the file using the Python execution options available in VS Code.

You can also use the integrated terminal:

python calculator.py

The output will be:

Sum = 30

A Complete Example

Let's create a simple Python program.

Save the following code as:

addition.py

Code:

a = 25
b = 15

result = a + b

print("First number:", a)
print("Second number:", b)
print("Sum:", result)

When you run the program, you get:

First number: 25
Second number: 15
Sum: 40

Running a Python Program Step by Step

Whether you are using IDLE, VS Code, or a terminal, the basic process is similar.

Step 1: Write the Code

For example:

print("Welcome to Python")

Step 2: Save the Program

Save it as:

welcome.py

Step 3: Execute the Program

Run the Python file using your chosen environment.

Step 4: View the Output

You should see:

Welcome to Python

This is the basic cycle of programming:

Write Code
    ↓
Save Code
    ↓
Run Program
    ↓
Get Output

Running a Program That Takes Input

Python programs can also ask the user for information.

Consider:

name = input("Enter your name: ")

print("Hello", name)

When the program runs, it may display:

Enter your name:

If the user enters:

Shivam

the program displays:

Hello Shivam

Here, the program waits for the user to provide input before continuing.


Running a Program with Numbers

Consider this program:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

sum = num1 + num2

print("Sum =", sum)

Example execution:

Enter first number: 50
Enter second number: 30
Sum = 80

Notice that int() is used to convert the input into an integer.


What Happens When Python Runs a Program?

At a basic level, the process can be represented as:

Python Source Code
        ↓
Python Interpreter
        ↓
Python Code Is Processed
        ↓
Instructions Are Executed
        ↓
Output / Result

For example:

x = 10
y = 20

print(x + y)

Python processes these instructions and produces:

30

Running a Program with an Error

Not every program will run successfully.

For example:

print("Hello"

This program has a syntax problem because the closing parenthesis is missing.

When you try to run it, Python reports an error.

This is why programming involves a repeated process:

Write
  ↓
Run
  ↓
Check Result
  ↓
Find Errors
  ↓
Fix Code
  ↓
Run Again

This process is a normal part of programming.


Running Programs and Debugging

Suppose you write:

a = 10
b = 20

print(a - b)

The program runs successfully and produces:

-10

However, suppose you intended to calculate the sum.

The problem here may not be a syntax error. The program is valid Python, but the logic does not produce the result you wanted.

You would need to inspect the program and change:

print(a - b)

to:

print(a + b)

This demonstrates why running a program is important: it allows you to check whether your program behaves as expected.


Source Code and Output

It is useful to distinguish between source code and output.

Source Code

The instructions written by the programmer:

a = 10
b = 20
print(a + b)

Output

The result produced when the program runs:

30

The programmer writes the source code, while Python executes it and produces the result.


Running Multiple Python Programs

You can create many Python files in a project.

For example:

PythonPrograms
│
├── hello.py
├── addition.py
├── calculator.py
├── even_odd.py
└── prime.py

Each file can contain a different program.

You can run the required file separately.

For example:

python prime.py

or:

python calculator.py

Running Python Programs in a Project Folder

It is good practice to keep related programs together.

For example:

PythonLearning
│
├── Basics
│   ├── variables.py
│   ├── input.py
│   └── operators.py
│
└── Programs
    ├── calculator.py
    ├── prime.py
    └── factorial.py

This organization becomes helpful when you have written many programs.


Common Problems While Running Python Programs

1. Python Is Not Recognized

If you enter:

python hello.py

and the system cannot find Python, check whether Python is installed and correctly configured.


2. Wrong File Name

Make sure the file name is correct.

For example, if your file is:

calculator.py

you should run:

python calculator.py

not:

python calculate.py

3. Wrong Folder

The terminal needs to be working in the appropriate folder or you need to provide the correct path to the Python file.


4. Syntax Error

Check your Python syntax carefully.

For example:

print("Hello"

should be:

print("Hello")

5. Program Runs but Gives the Wrong Result

This may indicate a logical error.

Check your:

  • Variables

  • Operators

  • Conditions

  • Loops

  • Calculations

  • Program logic


Running Python Programs: Practical Example

Let's create a small program that checks whether a number is even or odd.

Save it as:

even_odd.py

Code:

num = int(input("Enter a number: "))

if num % 2 == 0:
    print("Even number")
else:
    print("Odd number")

Suppose the user enters:

Enter a number: 24

The output will be:

Even number

If the user enters:

Enter a number: 17

the output will be:

Odd number

This is an example of a complete Python program that accepts input, processes it, and produces output.


Input, Processing and Output

Many Python programs follow this basic pattern:

Input
  ↓
Processing
  ↓
Output

For example, in the even/odd program:

Input

24

Processing

Python checks:

24 % 2 == 0

Output

Even number

Understanding this structure will become useful throughout your Python learning journey.


IDLE vs VS Code vs Terminal

Let's compare the common ways of running Python programs.

MethodMain Use
Python ShellQuick commands and experiments
IDLELearning and running Python programs
TerminalRunning programs using commands
VS CodeWriting, managing, debugging, and running projects

You can learn Python using any of these environments.

The important thing is to understand what happens when your Python code is executed.


A Beginner's Practice Task

Create a file named:

student.py

Write the following program:

name = input("Enter your name: ")
marks = int(input("Enter your marks: "))

print()
print("Student Details")
print("Name:", name)
print("Marks:", marks)

Run the program.

Try different names and marks.

For example:

Enter your name: Rahul
Enter your marks: 85

Student Details
Name: Rahul
Marks: 85

This simple exercise will help you practice the complete process:

Write → Save → Run → Input → Process → Output

Frequently Asked Questions

How do I run a Python program?

Save the program with a .py extension and execute it using an environment such as IDLE, VS Code, or the terminal.

What command is used to run a Python file?

A common command is:

python filename.py

Depending on the system, you may use:

python3 filename.py

Can I run Python without VS Code?

Yes. VS Code is not required to run Python. You can use IDLE, the Python interpreter, or a terminal.

Why does my Python program show an error?

The program may contain a syntax error, runtime problem, or logical error. Read the error message and check the relevant part of your code.

What happens after I run a Python program?

Python processes the source code using the Python environment and executes the instructions. The program may produce output, request input, or report an error.


Key Takeaways

In this article, we learned:

  • What it means to run a Python program

  • What a .py file is

  • How Python programs can be executed

  • Running code in the Python Shell

  • Running programs using IDLE

  • Running Python from a terminal

  • Running Python programs in VS Code

  • How input and output work during execution

  • What happens when a program contains an error

  • The difference between source code and output

  • Basic troubleshooting while running Python programs

Running a program is one of the most important habits to develop as a programmer. Write the code, run it, observe the result, find problems, and improve the program.

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