0% found this document useful (0 votes)
13 views

Py3 Type Conversion

Type conversion in Python is the process of converting one data type into another, categorized into implicit and explicit conversions. Implicit conversion occurs automatically by the interpreter, while explicit conversion requires manual commands using functions like int(), float(), and str(). The document also includes examples of both types of conversion and methods for swapping variables in Python.

Uploaded by

rubyjeyamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Py3 Type Conversion

Type conversion in Python is the process of converting one data type into another, categorized into implicit and explicit conversions. Implicit conversion occurs automatically by the interpreter, while explicit conversion requires manual commands using functions like int(), float(), and str(). The document also includes examples of both types of conversion and methods for swapping variables in Python.

Uploaded by

rubyjeyamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Type Conversion in python

Future Bytes Solutions


A.Ruby jeyamani
What Is Type Conversion In Python?

• Type conversion in the Python programming language, also known as data type
conversion, is a fundamental concept where one data type is converted into another.

Type conversion in Python can be categorized into two main types:


• Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion In Python
• Implicit type conversion, also known as automatic type conversion,
occurs when Python automatically changes the data type of a variable
during operations to maintain consistency and prevent errors.
• For example, if we perform arithmetic operations involving distinct
data types, Python will convert the lower precision type to a higher
precision type to ensure accurate results.

Key Characteristics Of Implicit Type Conversion In Python:


• Automatic: Performed by the Python interpreter automatically.
• No Programmer Input: This does not require explicit commands
from the programmer.
• Type Promotion: Usually promotes to a higher or more inclusive
data type.
Implicit Conversion

Example 1: Implicit Conversion From Int To Float


In this code example, we will implicitly convert an integer (Int) to a float
(Float).

Code:

num_int = 5 # Integer
num_float = 3.5 # Float
result = num_int + num_float # Implicit conversion
print("Result:", result)
print("Data type of result:", type(result))

Output:

Result: 8.5
Data type of result: <class 'float'>
Explicit Type Conversion In Python

• Explicit type conversion, also known as type casting, requires us to


manually convert one data type to another using specific functions or
operators.
• We use functions like int(), float(), str(), and list() to perform explicit
type conversion in Python.

Key Characteristics Of Explicit Type Conversion In Python:

• Manual: Performed by the programmer using specific commands.


• Requires Instructions: Needs explicit commands to perform the
conversion.
• Specified Type: Converts data to a specific target data type.
Example 2: Addition of string and integer Using Explicit
Conversion

num_string = '12'
num_integer = 23
# explicit type conversion
num_string = int(num_string)
num_sum = num_integer + num_string
print("Sum:",num_sum)
print("Data type of num_sum:",type(num_sum))

OUTPUT:
Sum: 35
Data type of num_sum: <class 'int'>
Functions Used For Explicit Data Type Conversion In Python

Functio
Purpose Syntax / Example
n Name
num_str = "42“
int(a, Converts a string or number to an
num_int = int(num_str)
base) integer
Output: 42
num_str = "3.14”
Converts a value to a floating-point
float() num_float = float(num_str)
number
Output: 3.14
char = 'A‘
Converts a character to its Unicode
ord() unicode_value = ord(char)
integer value
Output: 65
num = 16
Converts an integer to a lowercase
hex() hex_string = hex(num)
hexadecimal string
Output: '0x10'
num = 8
Converts an integer to an octal
oct() oct_string = oct(num)
string
Output: '0o10'
list_data = [1, 2, 3]
tuple() Converts a sequence to a tuple tuple_data = tuple(list_data)
Output: (1, 2, 3)
Functions Used For Explicit Data Type Conversion In Python

Function Name Purpose Syntax / Example


list_data = [1, 2, 2, 3, 3, 3]
set() Converts a sequence to a set set_data = set(list_data)
Output: {1, 2, 3}
tuple_data = (1, 2, 3)
list() Converts a sequence to a list list_data = list(tuple_data)
Output: [1, 2, 3]
num_int = 42
str() Converts a value to a string num_str = str(num_int)
Output: '42'
list_data = [('a', 1), ('b', 2)]
Converts a sequence of key-value pairs to
dict() a dictionary
dict_data = dict(list_data)
Output: {'a': 1, 'b': 2}
real_part = 3
imag_part = 4
complex(real, Creates a complex number with the given
complex_num = complex(real_part,
imag) real and imaginary parts
imag_part)
Output: (3+4j)
unicode_value = 65
Converts an integer to its corresponding
chr(number) Unicode character
char = chr(unicode_value)
Output: 'A'
Example
Key Points to Remember

1. Type Conversion is the conversion of an object from one data type to


another data type.

2. Implicit Type Conversion is automatically performed by the Python


interpreter.

3. Python avoids the loss of data in Implicit Type Conversion.

4. Explicit Type Conversion is also called Type Casting, the data types of
objects are converted using predefined functions by the user.

5. In Type Casting, loss of data may occur as we enforce the object to a


specific data type.
Python program to swap two variables
Method 1: By using Native Approach

P = int( input("Please enter value for P: "))


Q = int( input("Please enter value for Q: "))
OUTPUT
# To swap the value of two variables
Please enter value for P: 13
# we will user third variable which is a temporary variable
Please enter value for Q: 43
temp_1 = P The Value of P after swapping: 43
The Value of Q after swapping: 13
P=Q
Q = temp_1

print ("The Value of P after swapping: ", P)


print ("The Value of Q after swapping: ", Q)
Python program to swap two variables

Method 2: By using XOR Method

P = int( input("Please enter value for P: "))


Q = int( input("Please enter value for Q: "))
OUTPUT
# To Swap the values of two variables using XOR
Please enter value for P: 13
P=P^Q Please enter value for Q: 43
Q=P^Q The Value of P after swapping: 43
The Value of Q after swapping: 13
P=P^Q

print ("The Value of P after swapping: ", P)


print ("The Value of Q after swapping: ", Q)
Python program to swap two variables

Method 3: By using Arithmetic operation

P = int( input("Please enter value for P: "))


Q = int( input("Please enter value for Q: "))

# To Swap the values of two variables using Addition and subtraction operator
P=P+Q
Q=P-Q
OUTPUT
P=P-Q
Please enter value for P: 13
Please enter value for Q: 43
print ("The Value of P after swapping: ", P)
The Value of P after swapping: 43
print ("The Value of Q after swapping: ", Q) The Value of Q after swapping: 13
Python program to swap two variables

Method 4: By using multiplication and division


operator

P = int( input("Please enter value for P: "))


Q = int( input("Please enter value for Q: "))

# To Swap the values of two variables using multiplication and division operator
P=P*Q
OUTPUT
Q = P /Q
P=P/Q Please enter value for P: 13
Please enter value for Q: 43
The Value of P after swapping: 43
print ("The Value of P after swapping: ", P) The Value of Q after swapping: 13
print ("The Value of Q after swapping: ", Q)
Python program to swap two variables

Method 4: By using comma operator

P = int( input("Please enter value for P: "))


Q = int( input("Please enter value for Q: "))

# To Swap the values of two variables

P, Q = Q, P
OUTPUT
print ("The Value of P after swapping: ", P)
Please enter value for P: 13
print ("The Value of Q after swapping: ", Q) Please enter value for Q: 43
The Value of P after swapping: 43
The Value of Q after swapping: 13

You might also like