Input and Type casting in Python

Python is a powerful and beginner-friendly programming language. If you are just starting out, you might have come across the terms input and type casting. These concepts are fundamental to making your programs interactive and functional. In this blog, we will learn how to take input from users and how to make that data in an appropriate format or type using type casting.

Input and Type casting in Python
Input and Type casting in Python

What is the Input function?

When you want to take input from the user, you simply write input() and run it. Python will then prompt you to enter something. For example, if I type my name, Python will capture that input. But where does it store this information? To store the input, we can use a variable. Let’s say I create a variable called name and assign it the value from the input function. So, I can write:

name = input()
Python

When you run this, Python will pause and wait for you to input your name, which it then stores in the name variable.

Making Input Clear

But how does the user know what to enter? This is where we can add a message inside the input function. For example:

name = input("What is your name? ")
print("@", name)
Python

Now, when I run this code, it will clearly ask me, “What is your name?” This makes it easier for the user to understand what is expected.

What is your name? Inkita
@ Inkita
Bash

Check Data Types

Now, let’s talk about data types. When we take input, Python treats everything as a string by default. For example, if I enter a number, it is still considered a string. To check the type of the input, we can use the type() function.

age = input("Enter your age: ")
print(age, type(age))
Python

This will show that the type of age is a string, even I entered a number.

Enter your age: 12
12 <class 'str'>
Bash

Type Casting

When you use the input() function, all data is stored as a string by default. So, what if I want to use that age as a number? This is where type casting comes into play. Type casting allows us to convert one data type into another.

There are two types of typecasting:

  1. Implicit typecasting
  2. Explicit typecasting

1. Implicit typecasting

It automatically converts one data type to another when needed. It is automatically performed by the compiler or interpreter. For example:

x=5    # Integer
y=2.5   # Float
z=x+y   # X is implicitly converted to float
print(z)  # Output: 7.5 (float data type)
Python

Typecasting

Explicit type casting is when you convert a value from one type to another using functions. Python provides several built-in functions for type casting, so when I need use age as a number I will change it’s type.

1. int() Integer.

Converts data to an integer. Example

age = int(input("Enter your age: "))
print(age, type(age))

# Ouptut: 
# Enter yor age: 12
# 12 <class 'int'>
Python

2. float() Float Number

Converts data to a floating-point number. Example

pi = float(input("Enter pi value: "))
print(age, type(age))

# Ouptut: 
# Enter pi value: 3.14
# 3.14 <class 'float'>
Python

3. str() String

Converts data to a string. Example

num = 12
string = str(num)
print(string, type(string))

# Ouptut:   
# 12 <class 'str'>
Python

4. bool() Boolean

Converts data to a boolean (True/False). Example

is_hungry = bool(input("Are you hungry? (yes/no): "))
print(is_hungry, type(is_hungry))

# Ouptut: 
# Are you hungry? (yes/no): yes
# True <class 'bool'>
Python

But here is something wrong.

Are you hungry? (yes/no): no
True <class 'bool'>
Bash

Even if you enter no still it will give you True. But the question is why? In Python, the following values are treated as False when evaluated as a Boolean.

  1. None
  2. Numeric Zero of Any Type: 00.00j
  3. '' (empty string)
  4. [] (empty list)
  5. () (empty tuple)
  6. {} (empty dictionary)

Hence only above value inside bool() function gives False.

Advanced Type Casting Examples

  1. Converting to List
name = "Bulbul"
name_list = list(name)
print(name_list)  
# Outputs: ['B', 'u', 'l', 'b', 'u', 'l']
Python
  1. Converting to Set:
unique_chars = set(name)
print(unique_chars)  
# Outputs: {'B', 'u', 'l'}
Python
  1. Working with Complex Numbers:
complex_number = complex(4, 5)
print(complex_number)  
# Outputs: (4+5j)
Python

Summary

So we have learned how the input() function is used to gather user input, which is always stored as a string by default. To handle inputs as other data types, explicit type casting using functions like int()float(), and str() is demonstrated, along with practical examples.

The blog also covers implicit type casting, where Python automatically converts data types during operations. Advanced examples, including conversions to lists, sets, and complex numbers, are provided to showcase the versatility of type casting.

Input and Type casting in Python

Automatic type conversion

1 / 4

What does implicit typecasting mean in Python?

Returns a string

2 / 4

What is the default data type returned by the input() function?

non-empty string true

3 / 4

What is the result of bool("no") in Python?

Gathers user input

4 / 4

What does the input() function do in Python?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *