Built-in Data Types in Python

Built-in data types in python are fundamental data structures provided by the Python programming language. Pre-defined and available for use without requiring any additional libraries or modules. Python offers several built-in data types, including:

  • Numeric Data Types: Numeric data types in Python are used to represent numerical values. Python provides three primary numeric datatype in python:
    1. Integer (int): Integers are whole numbers without any decimal points. They can be positive or negative.
    2. Floating-Point (float): Floating-point numbers represent decimal values. They can be positive or negative and may contain a decimal point.
    3. Complex (complex): People use complex numbers to represent numbers with a real and imaginary part. You write them in the form of a + bj, where a is the real part and b is the imaginary part.
  • String Data Type(str): Represents a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (” “), such as “Hello, World!”, ‘Python’.
  • Boolean Data Type(bool): Represents either True or False, used for logical operations and conditions.
  • Collection Data Types:
    1. list: Represents an ordered and mutable collection of items, enclosed in square brackets ([]).
    2. tuple: Represents an ordered and immutable collection of items, enclosed in parentheses ().
    3. dict: Represents a collection of key-value pairs enclosed in curly braces ({}) with unique keys.
    4. set: Represents an unordered and mutable collection of unique elements, enclosed in curly braces ({}) or using the set() function.

Numeric Data Types

People use numeric data types in Python to represent numerical values. Python provides three primary numeric data types – integer (int), floating-Point (float) and complex (complex). These numeric data types allow for performing various arithmetic operations, such as addition, subtraction, multiplication, and division. They provide the necessary flexibility to work with numerical data in Python programs.

  • Int – It stores the integers values that can be positive or negative and do not contain any decimal point. Example: num1=10, num2 = 15
  • Float – These are floating-point real numbers that stores the decimal values. It consists of integer and fraction parts. Example: fnum = 25.4, fnum1=67.8
  • Complex – These are complex numbers specified as a real part and an imaginary part. They are stored in the form of a + bj where a is the real part and j represents the imaginary part. Example: num3= 2 + 3j, numcom = 5 –

Integers (int)

Python’s integer data type (int) represents whole numbers without any decimal points. It stores positive and negative whole numbers. Integers have immutability, meaning you cannot change their value once assigned.

Example1:

# Assigning integer values to variables
x = 5
y = -10
# Performing arithmetic operations
sum_result = x + y
difference_result = x - y
multiplication_result = x * y
division_result = x / y
# Printing the results
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)

Output

Sum: -5
Difference: 15
Multiplication: -50
Division: -0.5

Example2:

# Using integer values in comparisons
a = 10
b = 20
# Comparing the values
greater_than = a > b
less_than_or_equal = a <= b
equal_to = a == b
not_equal_to = a != b
# Printing the results
print("Greater than:", greater_than)
print("Less than or equal to:", less_than_or_equal)
print("Equal to:", equal_to)
print("Not equal to:", not_equal_to)

Output:

Greater than: False
Less than or equal to: True
Equal to: False
Not equal to: True

Floating-Point Numbers (float)

In Python, people use the float datatype in python to represent floating-point numbers, which are numbers with decimal points. Floats offer more precision than integers when it’s needed. Floats are immutable like integers and follow the IEEE 754 standard for representing real numbers.

Example1:

# Assigning float values to variables
x = 3.14
y = 2.5
# Performing arithmetic operations
sum_result = x + y
difference_result = x - y
multiplication_result = x * y
division_result = x / y
# Printing the results
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)

Output:

Sum: 5.64
Difference: 0.64
Multiplication: 7.85
Division: 1.256

Example2:

# Using float values in comparisons
a = 1.2
b = 2.7
# Comparing the values
greater_than = a > b
less_than_or_equal = a <= b
equal_to = a == b
not_equal_to = a != b
# Printing the results
print("Greater than:", greater_than)
print("Less than or equal to:", less_than_or_equal)
print("Equal to:", equal_to)
print("Not equal to:", not_equal_to)

Output:

Greater than: False
Less than or equal to: True
Equal to: False
Not equal to: True

Complex Numbers (complex)

In Python, people use the complex datatype in python to represent numbers with both real and imaginary parts. You write it in the form of a + bj, where a represents the real part and b represents the imaginary part. Complex numbers are useful in mathematical calculations and scientific computations that involve imaginary quantities.

Example1:

# Assigning complex values to variables
x = 2 + 3j
y = -1 + 2j
# Performing arithmetic operations
sum_result = x + y
difference_result = x - y
multiplication_result = x * y
division_result = x / y
# Printing the results
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)

Output:

Sum: (1+5j)
Difference: (3+1j)
Multiplication: (-8+1j)
Division: (0.8-1.4j)

Example2:

# Using complex values in comparisons
a = 1 + 2j
b = 3 + 4j
# Comparing the values
equal_to = a == b
not_equal_to = a != b
# Printing the results
print("Equal to:", equal_to)
print("Not equal to:", not_equal_to)

Output:

Equal to: False
Not equal to: True

Textual Data Types – Strings

People use textual python data types in Python to represent and manipulate sequences of characters, such as words, sentences, or even larger blocks of text. The primary textual data type in Python is the string (str). You enclose strings in quotes (‘ ‘, ” “, or “”” “””) and can manipulate them using various string methods. They are immutable, meaning you cannot change their values once assigned. People commonly use string data types for tasks like text processing, input/output operations, and data manipulation.

Accessing String Values

You can express each character of a string using a technique called indexing. In indexing, each character has an index value represented by either a positive or negative integer starting from 0.

Syntax:- stringname[index]

Example1:

Str1=”Python
#accessing second character 
Str1[1]
#accessing first four characters
Str1[0:4]  # it will print the value from index 0 to 3
str1="Python"
>>> str1[1]
'y'
>>> str1[0:4]
'Pyth'
Positive indexing    0   1     2     3     4     5
String   P    y     t      h     o     n
Negative indexing   -6   -5    -4    -3    -2    -1

String Operations

  • Concatenation: Python allows us to join two different strings using the concatenation operator ‘+’ 
Syntax:– str1 + str2

Example1:

>>> str1="Analytic"
>>> str2="Vidya"
>>> str3=str1 +str2
>>> print(str3)
AnalyticVidya
  • RepetitionsPython allows us to repeat a given string with the help of ‘ * ‘ operator.

M.Sarulatha
Assistant Professor
Dept. of CA., JJC

Comments

Popular posts from this blog

Operating System Structure

Asymptotic Notations

ASP.NET Events Handling