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

type casting and operators

The document explains type casting in Python, detailing both implicit and explicit conversions between data types, along with examples. It also covers various operators in Python, including arithmetic, comparison, logical, assignment, membership, identity, and bitwise operators, providing descriptions and examples for each. Overall, it serves as a comprehensive guide to understanding type casting and operators in Python programming.

Uploaded by

pradeep702661
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

type casting and operators

The document explains type casting in Python, detailing both implicit and explicit conversions between data types, along with examples. It also covers various operators in Python, including arithmetic, comparison, logical, assignment, membership, identity, and bitwise operators, providing descriptions and examples for each. Overall, it serves as a comprehensive guide to understanding type casting and operators in Python programming.

Uploaded by

pradeep702661
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

2.

Type Casting in Python


Type casting (type conversion) is the process of converting one data type into
another.

2.1 Implicit Type Casting


Python automatically converts one data type to another when necessary.

Example:

x = 5 # Integer
y = 2.5 # Float

z = x + y # Integer is implicitly converted to float


print(z) # Output: 7.5
print(type(z)) # Output: <class 'float'>

2.2 Explicit Type Casting


You can manually convert types using built-in functions:

Function Description Example


int() Converts to integer int(3.5) → 3
float() Converts to float float(5) → 5.0
str() Converts to string str(10) → "10"
bool() Converts to Boolean bool(1) → True
list() Converts to list list((1, 2, 3)) → [1, 2, 3]
tuple() Converts to tuple tuple([1, 2, 3]) → (1, 2, 3)
set() Converts to set set([1, 2, 3, 3]) → {1, 2, 3}
dict() Converts to dictionary dict([(1, 'a'), (2,
'b')]) → {1: 'a', 2: 'b'}

Example:

# Convert float to int


a = 5.7
b = int(a) # Output: 5
print(b)

# Convert int to float


x = 10
y = float(x) # Output: 10.0
print(y)

# Convert number to string


num = 100
text = str(num) # Output: "100"
print(text)

# Convert string to int


s = "50"
n = int(s) # Output: 50
print(n)

# Convert list to tuple


lst = [1, 2, 3]
tpl = tuple(lst) # Output: (1, 2, 3)
print(tpl)

# Convert list to set


lst = [1, 2, 3, 3]
st = set(lst) # Output: {1, 2, 3}
print(st)

2.3 Type Casting Boolean Values


You can convert other data types into Boolean using bool():

Example:

print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("Hi")) # True
print(bool([])) # False
print(bool([0])) # True (Non-empty list)

Operators in Python (Detailed Explanation)


Operators are special symbols that perform operations on variables and values.
Python provides several types of operators:

Arithmetic Operators
Comparison (Relational) Operators
Logical Operators
Assignment Operators
Membership Operators
Identity Operators
Bitwise Operators

1. Arithmetic Operators
These operators perform mathematical operations.

Operator Description Example Result


+ Addition 10 + 5 =15
- Subtraction 10 - 5 =5
* Multiplication 10 * 5 =50
/ Division 10 / 5 =2.0 (always float)
// Floor Division 10//3 =3 (removes decimal)
% Modulus (Remainder) 10 % 3 =1
** Exponentiation 2 ** 3 =8

Example:
a = 10
b = 3

print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000

2. Comparison (Relational) Operators


These operators compare values and return a Boolean (True or False).

Operator Description Example Result


== Equal to 10 == 5 False
!= Not equal to 10 != 5 True
> Greater than 10 > 5 True
< Less than 10 < 5 False
>= Greater than or equal to 10 >= 5 True
<= Less than or equal to 10 <= 5 False

Example:
x = 10
y = 5

print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False

3. Logical Operators
These operators are used to combine conditional statements.

Operator Description Example Result


and Returns True if both statements are True (10 > 5) and (5 < 8) True
or Returns True if at least one statement is True (10 > 5) or (5 > 8) True
not Reverses the Boolean result not (10 > 5) False

Example:
x = 10
y = 5
z = 8
print(x > y and y < z) # True
print(x > y or y > z) # True
print(not(x > y)) # False

4. Assignment Operators
These operators assign values to variables.

Operator Description Example Equivalent To


= Assign x = 5 x = 5
+= Add and assign x += 3 x = x + 3
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 3 x = x * 3
/= Divide and assign x /= 3 x = x / 3
//= Floor divide and assign x //= 3 x = x // 3
%= Modulus and assign x %= 3 x = x % 3
**= Exponentiate and assign x **= 3 x = x ** 3

Example:
x = 10
x += 5 # x = x + 5
print(x) # 15

x *= 2 # x = x * 2
print(x) # 30

x //= 4 # x = x // 4
print(x) # 7

5. Membership Operators
These operators check if a value is in a sequence (like a list, string, or tuple).

Operator Description Example Result


in Returns True if a value exists in a sequence 'a' in 'apple'
True
not in Returns True if a value does not exist in a sequence 'b' not in
'apple' True

Example:
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits) # True


print("grape" not in fruits) # True

6. Identity Operators
These operators check if two objects have the same memory location.

Operator Description Example Result


is Returns True if two variables point to the same object x is y
True if x and y reference the same object
is not Returns True if two variables point to different objects x is not y
True if x and y reference different objects

Example:

a = [1, 2, 3]
b = a # Both refer to the same object
c = [1, 2, 3] # Different object with the same values

print(a is b) # True
print(a is c) # False
print(a is not c) # True

7. Bitwise Operators
These operators work at the binary level.

Operator Description Example Binary Representation Result


& Bitwise AND 5 & 3 5 → 101, 3 → 011 1 (001)
` ` Bitwise OR `5 3`
^ Bitwise XOR 5 ^ 3 5 → 101, 3 → 011 6 (110)
~ Bitwise NOT ~5 5 → 101 -6
<< Left Shift 5 << 1 101 → 1010 10
>> Right Shift 5 >> 1 101 → 10 2

Example:
x = 5 # 101 in binary
y = 3 # 011 in binary

print(x & y) # 1 (001)


print(x | y) # 7 (111)
print(x ^ y) # 6 (110)
print(~x) # -6
print(x << 1) # 10 (1010)
print(x >> 1) # 2 (10)

You might also like