Operators and Strings in Python: A beginner's guide.


Did you know that in Python, the +
operator can make words fall in love? Welcome to the world of strings and operators!
What is an Operator in Python?
An operator is a special symbol that performs operations on variables and values.
Think of it like a tool that helps you manipulate data.
Types of Operators in Python:
- Arithmetic Operator: Used to perform mathematical calculations(e.g., Add, Subtract, Multiply, Divide). There are seven types of Arithmetic operators.
X = 7
Y = 3
print(x+y) # Addition (+) : Adds two numbers.
print(x-y) # Subtraction (−) : Subtracts the second number from the first.
print(x*y) # Multiplication (*) : Multiplies two numbers.
print(x/y) # Division (/) : Divides the first number by the second.
print(x//y) # Floor Division (//) : Divides and returns the integer (floor) part.
print(x%y) # Modulus (%) : Returns the remainder after division.
print(x**y) #Exponentiation (**) : Raises a number to the power of another.
- Comparison Operator: Comparison operators are used to compare two values. They return a true or false result based on whether the comparison condition is met. There are six types of comparison operators.
x = 5
y = 9
print(x == y) # Equal to
print(x != y) # Not Equal to
print(x > y) # Greater than
print(x < y) # Less than
print(x >= y) # Greater than or equal to
print(x <= y) # Less than or equal to
- Logical Operator: Logical operators help combine or check conditions that can be either true or false. There are three types of logical operators.
x = 3
y = 7
z = 12
print(x<y or y<x) # OR : True if at least one condition is true.
print(x<y and y<x) # AND : True only if both conditions are true.
print(not(x<y)) # NOT : Reverses the condition,Turns true to false and false to true.
- Bitwise Operator: Bitwise operators work on integers and perform operations on their binary representations. There are six types of bitwise operators.
x = 10 # 1010 in Binary
y = 6 # 110 in Binary
print(x & y) #OR Operation
print(x | y) #AND Operation
print(x ^ y) #XOR Operation
print(~x) # NOT Operation
print(x << 1) #Left Shift Operation
print(x >> 1) # Right Shift Operation
Assignment Operator: The assignment operator (
=
) is used to assign a value to a variable. There are two types of assignment operators.Basic Assignment: X = 5 assigns the value 5 to the variable X.
Augmented Assignment: These operators modify the value of the variable and assign it back in a shorthand way.
+=
:x += 5
is equivalent tox = x + 5
.-=
:x -= 5
is equivalent tox = x - 5
.*=
:x *= 5
is equivalent tox = x * 5
./=
:x /= 5
is equivalent tox = x / 5
.//=
:x //= 5
is equivalent tox = x // 5
.%=
:x %= 5
is equivalent tox = x % 5
.**=
:x **= 5
is equivalent tox = x ** 5
.&=
:x &= 5
is equivalent tox = x & 5
.|=
:x |= 5
is equivalent tox = x | 5
.^=
:x ^= 5
is equivalent tox = x ^ 5
.<<=
:x <<= 5
is equivalent tox = x << 5
.>>=
:x >>= 5
is equivalent tox = x >> 5
.
x = 10
y = 7
x += y # x = x + y
print(x) # Output: 17
x -= y # x = x - y
print(x) # Output: 10
x *= y # x = x * y
print(x) # Output: 70
x /= y # x = x / y
print(x) # Output: 10.0
x %= y # x = x % y
print(x) # Output: 3.0
x **= y # x = x ** y
print(x) # Output: 2187.0
x &= y # x = x & y
print(x) # Output: 2
x |= y # x = x | y
print(x) # Output: 7
x ^= y # x = x ^ y
print(x) # Output: 5
x <<= 2 # x = x << 2
print(x) # Output: 20
x >>= 2 # x = x >> 2
print(x) # Output: 5
- Membership Operator: This operator is used to test if a value is a member of a sequence (such as a string, list, tuple, set, or dictionary)
print('s' in 'science') # Checks if 's' is available in 'science' and gives output as True or False
#Output = True
print('s' not in 'science') # Checks if 's' is not available in 'science' and gives output as True or False
#Output = False
- Identity Operator: It is used to compare the memory location of two objects.
x = 10
y = 10
z = 3
s = 7
## Checks if two variables are pointing on a same object or not ( Output : True/False)
print(x is y)
print(x is not y)
print(s is z)
print(s is not z)
## Checking Memory Address
print(id(x))
print(id(y))
print(id(z))
print(id(s))
What is a String?
A String is a sequence of characters wrapped in single or double quotes.
# Creating a string in Python using doble quotes.
name = "Sohan Kumar Das"
print(type(name)) # Output = <class 'str'>
# Creating a string in Python using single quotes.
profession = 'Data Scientist'
print(type(profession)) # Output = <class 'str'>
Inbuilt functions of string in Python.
- Case Conversion
s = "hello World123"
print(s.upper()) # Converts all characters to uppercase. Output = "HELLO WORLD123"
print(s.lower()) # Converts all characters to lowercase. Output = "hello world123"
print(s.capitalize()) # Capitalizes first character. Output = "Hello world123"
print(s.title()) # Capitalizes first character of each word. Output = "Hello World123"
print(s.swapcase()) # Swaps case of each character. Output = "HELLO WORLD123"
- Concatenation Operation: It means joining two or more strings together using the
+
operator.
str4 = "Radhe"
str5 = "Krishna"
result = str4 + " " + str5
print(result) # Output: Radhe Krishna
Mutable vs Immutable in Python
In Python, data types are classified into mutable and immutable types based on whether their values can be changed in place after creation.
Mutable: Can be changed after creation
Examples:
list
dict
set
bytearray
a = [1, 2, 3]
a[0] = 100
print(a) # [100, 2, 3]
Immutable: Cannot be changed after creation
Examples:
int
float
str
tuple
bool
frozenset
x = "hello"
x[0] = "H"
print(x) # 'TypeError: 'str' object does not support item assignment'
Thank You
Linkedin : sohankr03
Source Code : Google Colab
Subscribe to my newsletter
Read articles from Sohan Kumar Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
