DEV Community

Saravanan Cn
Saravanan Cn

Posted on

My Python Learning Journey: Operators with Simple Examples 🐍

I'm learning Python step by step as a beginner. Today, I practised different types of operators and learned how they work with simple examples.

1. Identifiers

Identifiers are names used for variables, functions, and other objects in Python.

name = "Ajith"
age = 25

print(name)
print(age)
Enter fullscreen mode Exit fullscreen mode

2. Keywords

Keywords are reserved words that have special meanings in Python.

if True:
    print("Hello, Python!")
Enter fullscreen mode Exit fullscreen mode

Here, if is a keyword.

3. Data Types and Literals

Data types describe the kind of value, while literals are values written directly in the code.

age = 25
name = "Ajith"
price = 99.5

print(type(age))
print(type(name))
print(type(price))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'int'>
<class 'str'>
<class 'float'>
Enter fullscreen mode Exit fullscreen mode

4. Zen of Python

The Zen of Python contains guiding principles for writing Python code.

import this
Enter fullscreen mode Exit fullscreen mode

This displays the Zen of Python in the Python interpreter.

5. Comparison Operators

Comparison operators compare two values and return True or False.

no1 = 10
no2 = 20

print(no1 < no2)  # True
print(no1 > no2)  # False
print(no1 == no2) # False
Enter fullscreen mode Exit fullscreen mode

6. Chained Comparisons

Chained comparisons allow us to combine multiple comparisons.

no1, no2, no3 = 100, 200, 300

print(no3 > no2 > no1)  # True
Enter fullscreen mode Exit fullscreen mode

Python checks both 300 > 200 and 200 > 100.

7. Logical Operators

Python has three logical operators: and, or, and not.

print(10 and 20)  # 20
print(10 or 20)   # 10
print(not 5)      # False
Enter fullscreen mode Exit fullscreen mode
  • and returns the first falsy value, or the last value if all are truthy.
  • or returns the first truthy value, or the last value if all are falsy.
  • not reverses a value's truthiness.

8. Assignment Operators

Assignment operators assign values or update existing variables.

no1 = 10

no1 += 5
print(no1)  # 15

no1 -= 3
print(no1)  # 12

no1 *= 2
print(no1)  # 24
Enter fullscreen mode Exit fullscreen mode

Other assignment operators include /=, //=, %=, and **=.

9. Bitwise AND (&)

Bitwise AND compares corresponding binary digits. The result is 1 only when both bits are 1.

print(5 & 3)  # 1
Enter fullscreen mode Exit fullscreen mode

Binary calculation:

5 = 0101
3 = 0011
    ----
    0001
Enter fullscreen mode Exit fullscreen mode

10. Bitwise OR (|)

Bitwise OR returns 1 when at least one corresponding bit is 1.

print(5 | 3)  # 7
Enter fullscreen mode Exit fullscreen mode

Binary calculation:

5 = 0101
3 = 0011
    ----
    0111
Enter fullscreen mode Exit fullscreen mode

Binary 0111 equals decimal 7.

11. Binary to Decimal Conversion

Binary uses only two digits: 0 and 1.

For example, binary 0101 equals decimal 5.

Binary:      0101
Place value: 8421
Enter fullscreen mode Exit fullscreen mode

Calculation:

0 × 8 = 0
1 × 4 = 4
0 × 2 = 0
1 × 1 = 1

Total = 0 + 4 + 0 + 1 = 5
Enter fullscreen mode Exit fullscreen mode

What I Learned Today

I practised identifiers, keywords, data types, literals, comparison operators, chained comparisons, logical operators, assignment operators, bitwise operators, and binary-to-decimal conversion.

I'm continuing to practise with small programs to strengthen my Python fundamentals.

Learning step by step! 🚀

Top comments (0)