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

Python Variables

If you are learning Python for the first time, one of the first concepts you need to understand is variables.

Variables are used in almost every Python program. They allow us to store information such as numbers, names, marks, prices, and other values so that we can use them later in the program.

In this article, we will understand what variables are, how to create them, how to assign values, how to change values, and how variables are used in Python programs.


What Is a Variable in Python?

A variable is a name used to refer to a value stored in a program.

For example:

name = "Rahul"
age = 20

Here:

  • name is a variable.

  • "Rahul" is the value stored in name.

  • age is a variable.

  • 20 is the value stored in age.

We can use these variables later:

print(name)
print(age)

Output:

Rahul
20

So, a simple way to understand a variable is:

Variable = Name given to a value


Why Do We Need Variables?

Imagine you are writing a student management program.

You may need to store:

  • Student name

  • Student age

  • Student roll number

  • Student marks

  • Student percentage

Instead of writing the actual values repeatedly, we can store them in variables.

Example:

name = "Amit"
age = 19
marks = 85

Now we can use these values whenever required.

print(name)
print(age)
print(marks)

This makes programs easier to write, understand, and modify.


How to Create a Variable in Python

Python makes creating variables very simple.

The general syntax is:

variable_name = value

For example:

x = 10

Here:

  • x → variable name

  • = → assignment operator

  • 10 → value

Another example:

city = "Kolkata"

Here, city stores the value "Kolkata".


Variable Assignment

Giving a value to a variable is called assignment.

Example:

number = 50

The value 50 is assigned to the variable number.

We can then use:

print(number)

Output:

50

Variables Can Store Different Values

Python variables can store different kinds of values.

For example:

name = "Shivam"
age = 20
percentage = 85.5

Here:

  • name stores text.

  • age stores a whole number.

  • percentage stores a decimal number.

Python determines the type of the value automatically.


Changing the Value of a Variable

A variable's value can be changed during program execution.

For example:

age = 20

print(age)

age = 21

print(age)

Output:

20
21

Initially, age contains 20.

Later, we assign 21 to the same variable.

Therefore, the value of the variable changes.


Example: Storing Student Information

Consider this program:

name = "Rahul"
roll = 15
marks = 87

print("Name:", name)
print("Roll:", roll)
print("Marks:", marks)

Output:

Name: Rahul
Roll: 15
Marks: 87

Here, three variables are used:

name
roll
marks

Each variable stores different information.


Multiple Variables in Python

Python allows us to create multiple variables in a program.

Example:

name = "Anita"
age = 20
city = "Kolkata"

We can display all of them:

print(name)
print(age)
print(city)

Output:

Anita
20
Kolkata

Assigning Multiple Variables

Python also allows multiple variables to be assigned in a single statement.

Example:

a, b, c = 10, 20, 30

Now:

a = 10
b = 20
c = 30

We can print them:

print(a)
print(b)
print(c)

Output:

10
20
30

This is a convenient feature of Python.


Assigning the Same Value to Multiple Variables

The same value can also be assigned to multiple variables.

Example:

a = b = c = 100

Now all three variables contain 100.

print(a)
print(b)
print(c)

Output:

100
100
100

Variables Can Be Used in Calculations

Variables become particularly useful when performing calculations.

For example:

a = 10
b = 20

sum = a + b

print(sum)

Output:

30

The program stores values in a and b and then uses those values to calculate their sum.

Another example:

length = 10
breadth = 5

area = length * breadth

print(area)

Output:

50

Taking Input and Storing It in a Variable

Variables are also commonly used with the input() function.

Example:

name = input("Enter your name: ")

print("Hello", name)

If the user enters:

Rahul

The output will be:

Hello Rahul

Here, the value entered by the user is stored in the variable name.


Example: Add Two Numbers

Variables can be used to create a simple calculator.

a = 10
b = 20

sum = a + b

print("Sum =", sum)

Output:

Sum = 30

The calculation can also be performed using input:

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b

print("Sum =", sum)

For example, if the user enters:

Enter first number: 15
Enter second number: 25

Output:

Sum = 40

Important Rules for Variable Names

While creating variables, you should use proper variable names.

For example:

student_name = "Rahul"
student_age = 20

These names clearly describe the information being stored.

Good variable names make programs easier to understand.


Examples of Good Variable Names

Some meaningful variable names are:

student_name = "Amit"
student_age = 20
total_marks = 450
average_marks = 90
phone_number = 9876543210

These names clearly describe their purpose.


Avoid Unclear Variable Names

Consider:

x = "Rahul"
a = 20
p = 450

These names may work, but it is difficult to understand what they represent.

A better version would be:

student_name = "Rahul"
student_age = 20
total_marks = 450

Good naming is especially important when programs become large.


Variables Are Case-Sensitive

Python variable names are case-sensitive.

For example:

name = "Rahul"
Name = "Amit"

Python treats name and Name as two different variables.

Example:

name = "Rahul"
Name = "Amit"

print(name)
print(Name)

Output:

Rahul
Amit

Therefore, uppercase and lowercase letters matter in Python.


A Variable Can Store the Result of an Expression

A variable does not only have to store a direct value.

It can also store the result of a calculation.

Example:

a = 10
b = 20

result = a + b

Here, result stores the result of the expression:

a + b

Therefore:

print(result)

Output:

30

Example: Calculate Rectangle Area

Let's use variables in a practical program.

length = 20
breadth = 10

area = length * breadth

print("Length =", length)
print("Breadth =", breadth)
print("Area =", area)

Output:

Length = 20
Breadth = 10
Area = 200

This example shows how variables can store both input values and calculated results.


Common Mistakes with Variables

1. Using a variable before assigning a value

For example:

print(age)

If age has not been created earlier, Python will produce an error.

We should first assign a value:

age = 20
print(age)

2. Confusing uppercase and lowercase

Remember:

name = "Rahul"
Name = "Amit"

These are different variables.


3. Using unclear names

Instead of:

x = 500

you may use:

total_marks = 500

The second version is easier to understand.


Real-Life Example of Variables

Suppose we are developing a student result program.

We could use:

student_name = "Rahul"
english = 80
maths = 90
computer = 95

total = english + maths + computer

print("Student:", student_name)
print("Total Marks:", total)

Output:

Student: Rahul
Total Marks: 265

Here variables are used to store the student's name, marks, and calculated total.

This is one of the main reasons variables are so important in programming.


Quick Revision

Let's summarize what we learned.

ConceptExample
Creating a variableage = 20
Text valuename = "Rahul"
Decimal valueprice = 99.50
Changing a valueage = 21
Multiple assignmenta, b = 10, 20
Same valuea = b = 100
Calculationtotal = a + b
User inputname = input()

Practice Questions

Try solving these programs yourself.

Question 1

Create variables to store your name, age, and city and display them.

Question 2

Create two variables containing two numbers and find their sum.

Question 3

Create variables for length and breadth and calculate the area of a rectangle.

Question 4

Create variables for three subject marks and calculate the total marks.

Question 5

Create a variable containing your age. Change its value and display both values.


Final Thoughts

Variables are one of the fundamental building blocks of Python programming.

Whenever a program needs to remember information, variables provide a simple way to store and use that information.

Once you understand variables, concepts such as data types, input, calculations, conditions, loops, and functions become much easier to understand.

So, don't just memorize the syntax. Try creating small programs using variables and experiment with different values.

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