Skip to main content

Data Types, Len & Round off Function of Python | Python Programming By Akash

 1.   Data Types:

Definition: It is a term used to categorize what kind of data is stored inside a variable. Python needs to know the 'type' (is it text? is it a number?) to understand how to process it correctly. The four main types used in basic Python are:

a.   String (str): It is a data type used to store simple text or characters. We must write strings inside single or double quotes. (e.g., "Akash", 'Hello World')

b.   Integer (int): It is a data type used to store whole numbers (complete numbers) without any decimal points. It can be positive or negative.(e.g., 10, 500, -5)

c.   Float (float): It is a data type used to store numbers that have decimal points (fractional parts). (e.g., 10.5, 3.14, -0.01)

d.   Boolean (bool): It is a data type used to store logical values. It can only hold two specific values: True or False. (e.g., True, False)

For Example,

2.   Type Checking (Type Function):

Definition: It is a built-in function used to check or identify the specific data type of a variable or value. By using the command type() and placing the variable inside the round brackets, Python tells us if the data is a String, Integer, Float, or Boolean.

For Example,

3.   Type Conversion (Type Casting):

Definition: It is a term used to change or convert one data type into another data type. This is necessary because sometimes Python sees a number as text (String), and we cannot do math with it unless we convert it into a number (Integer or Float) first. We use specific functions to force these changes.

The Main Conversion Functions:

a. str() : Converts the value into a String (Text).

b. int() : Converts the value into an Integer (Whole Number).

c. float() : Converts the value into a Float (Decimal Number).

For Example,

4.   Len Function (Length):

Definition: It is a built-in function used to count the total length of an item. By using the command len() and placing a variable or text inside the round brackets, Python counts and returns the total number of characters (including spaces) in a string, or the number of items in a list.

For Example,

5.   Round Function:

Definition: It is a mathematical function used to simplify a floating-point number (decimal) by rounding it to the nearest whole number. By writing round() and placing the number inside the brackets, it removes the decimal part based on standard math rules. You can also add a comma and a second number to specify how many decimal places you want to keep.

For Example,


Comments