Python Data Types, Type Casting, Input(), Print()

by | Apr 11, 2024 | Python | 0 comments

In Python, data types define the type of data that can be stored in variables. Here are the main data types in Python:

1. Numeric Types:

  • int: Integer values (e.g., 5, -3, 1000)
  • float: Floating-point values (e.g., 3.14, -0.001, 2.0)

2. Sequence Types:

  • str: Strings, sequences of characters (e.g., “hello”, ‘Python’, “123”)
  • list: Ordered, mutable collections of items (e.g., [1, 2, 3], [‘a’, ‘b’, ‘c’])
  • tuple: Ordered, immutable collections of items (e.g., (1, 2, 3), (‘a’, ‘b’, ‘c’))

3. Mapping Type:

  • dict: Unordered collections of key-value pairs (e.g., {‘name’: ‘John’, ‘age’: 30})

4. Set Types:

  • set: Unordered collections of unique items (e.g., {1, 2, 3}, {‘a’, ‘b’, ‘c’})
  • frozenset: Immutable set (similar to set but cannot be changed after creation)

5. Boolean Type:

  • bool: Represents truth values, either True or False.

6. None Type:

  • None: Represents the absence of a value or a null value.

Additional Types:

  • bytes: Immutable sequence of bytes (e.g., b’hello’)
  • bytearray: Mutable sequence of bytes (similar to bytes but can be modified)
  • memoryview: Memory view objects used to expose the buffer interface to Python code.
  • complex: Complex numbers with a real and imaginary part (e.g., 3 + 4j)

Python is dynamically typed, meaning you don’t need to explicitly declare the data type of a variable. The interpreter automatically assigns data types based on the value assigned to the variable. Additionally, Python supports type conversion functions to convert between different data types (e.g., int(), float(), str()).

Understanding these data types is crucial for effectively working with data and writing Python programs. Each data type has its own set of operations and methods for manipulation and processing.

Type casting in Python

Type casting in Python refers to converting a value from one data type to another. Python provides built-in functions for explicit type conversion. Here are the commonly used type casting functions:

1. int():

Converts a value to an integer data type.

x = int("10")  # x will be 10

2. float():

Converts a value to a floating-point data type.

y = float("3.14")  # y will be 3.14

3. str():

Converts a value to a string data type.

z = str(42)  # z will be "42"

4. bool():

Converts a value to a boolean data type.

a = bool(0)  # a will be False
b = bool(1) # b will be True

5. list(), tuple(), set(), dict():

Converts a sequence or mapping type to the respective data type.

seq = [1, 2, 3]
seq_tuple = tuple(seq) # seq_tuple will be (1, 2, 3)
seq_set = set(seq) # seq_set will be {1, 2, 3}

6. bytes(), bytearray():

Converts a value to a bytes or bytearray data type.

b = bytes("hello", 'utf-8')  # b will be b'hello'
ba = bytearray(b) # ba will be bytearray(b'hello')

7. complex():

Converts a value to a complex number.

c = complex(2, 3)  # c will be 2 + 3j


  • Type casting may result in loss of precision or data if the conversion is not possible.
  • It’s essential to ensure that the value being casted can be validly converted to the target data type to avoid errors.
  • Some implicit type conversions may also occur in Python, especially in arithmetic operations, where Python will automatically convert operands to a common data type before performing the operation.

The input() function in Python

The input() function in Python is used to get user input during the execution of a program. It pauses the program and waits for the user to type something and then press Enter. Whatever the user types is then returned by the input() function as a string.

Here are some key points about input():

  • User Input: Pauses the program and waits for the user to enter some text.
  • Returns a String: The user’s input is always returned as a string, even if the user enters numbers.
  • Optional Prompt: You can optionally provide a message to be displayed before the user input, to instruct the user what kind of input is expected. This message is passed as an argument to the input() function.

Here’s an example of how to use input():


name = input("What is your name? ")
print("Hello, " + name)

In this example, the program prompts the user with the message “What is your name? “. The user then types their name and presses Enter. The input() function then stores the user’s input in the variable name. Finally, the program prints a greeting message that includes the user’s name.

Type Casting:

Since input() always returns a string, if you want to use the user’s input as a number, you’ll need to convert it to the appropriate numerical data type (e.g., int or float) using type casting. Here’s an example:


age = int(input("How old are you? "))
print("You are", age, "years old.")

In this example, the program prompts the user for their age. The input() function returns the user’s input as a string. We then use int() to convert the string to an integer before storing it in the variable age. Finally, the program prints a message that includes the user’s age.

Written by HintsToday Team

Related Posts

Python input function in Detail- interesting usecases

The input() function in Python is primarily used to take input from the user through the command line. While its most common use is to receive text input, it can be used creatively for various purposes. Here are some interesting uses of the input() function along with...

read more

Python Strings Interview Questions

Python Programming Strings Interview Questions Write a Python program to remove a Specific character from string? Here's a Python program to remove a specific character from a string: def remove_char(text, char): """ Removes a specific character from a string. Args:...

read more

Get the latest news

Subscribe to our Newsletter


Submit a Comment

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