Python Bitwise Operators
Python Bitwise operator works on bits and performs bit by bit operation. These
operators are used to compare binary numbers.
The following table contains all bitwise operators with their symbols, names, and
examples −
Operator Name Example
& AND a&b
| OR a|b
^ XOR a^b
~ NOT ~a
<< Zero fill left shift a << 3
>> Signed right shift a >> 3
Example of Python Bitwise Operators
a = 20
b = 10
print ('a=',a,':',bin(a),'b=',b,':',bin(b))
c=0
c = a & b;
print ("result of AND is ", c,':',bin(c))
c = a | b;
print ("result of OR is ", c,':',bin(c))
c = a ^ b;
print ("result of EXOR is ", c,':',bin(c))
c = ~a;
print ("result of COMPLEMENT is ", c,':',bin(c))
c = a << 2;
print ("result of LEFT SHIFT is ", c,':',bin(c))
c = a >> 2;
print ("result of RIGHT SHIFT is ", c,':',bin(c))
Output
a= 20 : 0b10100 b= 10 : 0b1010
result of AND is 0 : 0b0
result of OR is 30 : 0b11110
result of EXOR is 30 : 0b11110
result of COMPLEMENT is -21 : -0b10101
result of LEFT SHIFT is 80 : 0b1010000
result of RIGHT SHIFT is 5 : 0b101
Python Logical Operators
Python logical operators are used to combile two or more conditions and check the final
result. There are following logical operators supported by Python language. Assume
variable a holds 10 and variable b holds 20 then
The following table contains all logical operators with their symbols, names, and
examples −
Operator Name Example
and AND a and b
or OR a or b
not NOT not(a)
Example of Python Logical Operators
var = 5
print(var > 3 and var < 10)
print(var > 3 or var < 4)
print(not (var > 3 and var < 10))
Output
True
True
False
Python Membership Operators
Python's membership operators test for membership in a sequence, such as strings, lists,
or tuples.
There are two membership operators as explained below −
Operator Description Example
Returns True if it finds a variable in the specified
in a in b
sequence, false otherwise.
returns True if it does not finds a variable in the
not in a not in b
specified sequence and false otherwise.
Example of Python Membership Operators
a = 10
b = 20
list = [1, 2, 3, 4, 5 ]
print ("a:", a, "b:", b, "list:", list)
if ( a in list ):
print ("a is present in the given list")
else:
print ("a is not present in the given list")
if ( b not in list ):
print ("b is not present in the given list")
else:
print ("b is present in the given list")
c=b/a
print ("c:", c, "list:", list)
if ( c in list ):
print ("c is available in the given list")
else:
print ("c is not available in the given list")
Output
a: 10 b: 20 list: [1, 2, 3, 4, 5]
a is not present in the given list
b is not present in the given list
c: 2.0 list: [1, 2, 3, 4, 5]
c is available in the given list
Python Identity Operators
Python identity operators compare the memory locations of two objects.
There are two Identity operators explained below −
Operator Description Example
Returns True if both variables are the same object
is a is b
and false otherwise.
Returns True if both variables are not the same
is not a is not b
object and false otherwise.
Example of Python Identity Operators
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c=a
print(a is c)
print(a is b)
print(a is not c)
print(a is not b)
Output
True
False
False
True
Python Basic Input and Output:
Python Output
In Python, we can simply use the print() function to print output.
For example,
print('Python is powerful')
# Output: Python is powerful
Run Code
Here, the print() function displays the string enclosed inside the single quotation.
Syntax of print()
In the above code, the print() function is taking a single parameter.
However, the actual syntax of the print function accepts 5 parameters
print(object= separator= end= file= flush=)
Here,
object - value(s) to be printed
sep (optional) - allows us to separate multiple objects inside print().
end (optional) - allows us to add add specific values like new line "\n", tab "\t"
file (optional) - where the values are printed. It's default value is [Link] (screen)
flush (optional) - boolean specifying if the output is flushed or buffered.
Default: False
Python is known for its clean syntax and readability, making it an ideal language for
beginners. In this tutorial, we’ll explore:
1. Python Syntax — The basic structure of Python programs.
2. Indentation — How Python uses whitespace to define code blocks.
3. Comments — How to add explanations in your code.
1. Python Syntax
python syntax refers to the rules and structures you must follow when writing Python
[Link] you break these rules, Python will raise an error.
Basic Python Program
here is the simplest python program:
# This is a simple Python program
print(“Hello, World!”)
Output: Hello, World
Explanation:
print() is a built-in function that outputs text.
# is used to write a comment
Python Syntax Rules
1. case-sensitive — name and Name are treated differently.
2. statements — Each instruction is a statement(for example print()).
3. Line Breaks — Each statement is typically on a new line.
4. Colon(:) — used to define blocks(e.g., after if, for, def).
Example:
name = “ram”
print(name)
output: ram
Multiple Statements on One Line
You can write multiple statements on one line using a semicolon(;).
x=5; y=4; print(x+y)
Output: 9
Multi-Line Statements
if a statement is too long, you can break it across lines using a backslash(\).
Example:
result = 10 + 20 + 30 + \
40 + 50 + 60
print(result)
Output: 210
2. Python Indentation
In Python, indentation defines the structure of code blocks.
In other languages like c or Java, we use curly braces{ }, but Python relies on
indentation.
Why Indentation Matters
Indentation improves code readability and is mandatory in Python.
Correct Example:
if True:
print(“Indented correctly!”)
Output:
Indented correctly!
Indentation Guidelines
use 4 spaces for one level of indentation
Be consistent — never mix tabs and spaces.
Nested Indentation
you can have multiple indentation levels.
Example:
age = 18
if age >= 18:
print(“You are an adult.”)
if age == 18:
print(“You are exactly 18”)
Output:
You are an adult.
You can vote