Operators
Operators are symbols used to perform operations on variables and values.
- Arithmetic Operators Used for mathematical calculations. Operator Meaning Example
- Addition 10 + 5
- Subtraction 10 - 5
Multiplication 4 * 3
/ Division 10 / 2
% Modulus (Remainder) 10 % 3 → 1
** Exponent (Power) 2 ** 3 → 8
// Floor Division 10 // 3 → 3
Example:
a = 10
b = 3
print(a + b)
print(a % b)
print(a // b)-
Comparison Operators
Used to compare values — returns True/False
Operator Meaning
== Equal to
!= Not equal toGreater than
< Less than
= Greater or equal
<= Less or equal
print(5 > 3)
print(10 == 20) Logical Operators
Operator Meaning Example
and both conditions true a > 5 and a < 20
or any one true a > 5 or a < 2
not reverse value not True = False
x = 10
print(x > 5 and x < 15)
print(x > 5 or x > 100)
print(not(x > 5))Assignment Operators
Operator Meaning
= Assign value
+= a = a + 5
-= a = a – 5
= a = a × 5
/= a = a ÷ 5
**Example:*
x = 10
x += 5 # x = 15
x *= 2 # x = 30
String
1. What is a String?
A string is text inside quotes.
name = "Aruna"
2. String Indexing
Each character has a position (index).
A r u n a
0 1 2 3 4
name = "Aruna"
print(name[0]) # A
print(name[3]) # n
3. String Slicing
Extract part of string.
word = "Python"
print(word[0:3]) # Pyt
print(word[2:6]) # thon
print(word[:4]) # Pyth
print(word[2:]) # thon
4. Important String Functions
upper() – converts to uppercase
name = "aruna"
print(name.upper()) # ARUNA
lower()
print("HELLO".lower())
title()
print("welcome to python".title())
replace()
text = "I love python"
print(text.replace("love", "like"))
len() – length of string
print(len("Python"))
5. Loop Through String
for ch in "hello":
print(ch)
6. Check Substring
text = "I love Python"
print("Python" in text) # True
print("java" in text) # False
7. Count Vowels Program
word = "education"
count = 0
for ch in word:
if ch in "aeiou":
count += 1
print("Vowels =", count)
Top comments (0)