Translate

Tuesday 30 January 2024

Integer, Float, and Boolean data types in Python


Integer, Float, and Boolean data types in Python:

Integers (int):

  • Represent whole numbers without decimal points.

  • Examples:


Python

age = 25  # Stores the value 25 as an integer
year = 2024  # Stores the year as an integer
number_of_apples = 10  # Stores the number of apples as an integer

Floats (float):

  • Represent numbers with decimal points.

  • Used for precise measurements or calculations.

  • Examples:


Python

price = 19.99  # Stores the price as a float
pi = 3.14159  # Stores the value of pi as a float
distance = 123.456  # Stores the distance as a float

Booleans (bool):

  • Represent logical values: True or False.

  • Used for decision-making in code.

  • Examples:


Python

is_active = True  # Stores a True boolean value
is_logged_in = False  # Stores a False boolean value
has_completed_task = True  # Stores a True boolean value

Key points:

  • Choose the appropriate data type based on the information you need to store.

  • Python automatically determines the data type based on the assigned value.

  • You can explicitly convert between data types using functions like int(), float(), and bool().




type(), float(), and int() functions in Python with examples:

type() function:

  • This function is used to check the data type of a variable.

  • It returns a class object representing the data type of the variable.

Examples:


Python

age = 30
print("Data type of age:", type(age))  # Output: <class 'int'>

pi = 3.14159
print("Data type of pi:", type(pi))  # Output: <class 'float'>

is_active = True
print("Data type of is_active:", type(is_active))  # Output: <class 'bool'>

float() function:

  • This function is used to convert a value to a floating-point number (float).

  • It can convert integers, booleans, and strings (if they represent a valid floating-point number) to floats.

Examples:


Python

converted_age_to_float = float(age)
print("Converted age to float:", converted_age_to_float)  # Output: 30.0

converted_pi_to_int = int(pi)
print("Converted pi to integer (truncates decimals):", converted_pi_to_int)  # Output: 3

# Boolean conversion (True remains True, False becomes 0)
converted_is_active_to_int = int(is_active)
print("Converted is_active to integer:", converted_is_active_to_int)  # Output: 1

int() function:

  • This function is used to convert a value to an integer.

  • It can convert floats (truncating the decimal part) and booleans (True becomes 1, False becomes 0) to integers.

  • It raises a ValueError if the argument cannot be converted to an integer.

Examples:


Python

converted_age_to_float = float(age)
print("Converted age to float:", converted_age_to_float)  # Output: 30.0

converted_pi_to_int = int(pi)
print("Converted pi to integer (truncates decimals):", converted_pi_to_int)  # Output: 3

# Boolean conversion (True remains True, False becomes 0)
converted_is_active_to_int = int(is_active)
print("Converted is_active to integer:", converted_is_active_to_int)  # Output: 1

I hope this helps!


03 Int float boolean Data types and int float type Functions In Telugu 03

No comments:

Post a Comment

Note: only a member of this blog may post a comment.