Python Unit 1
Python Unit 1
INTRODUCTION TO PYTHON
Python is a high-level, interpreted programming language known for its simplicity and readability. It was
created by Guido van Rossum and first released in 1991. Python supports multiple programming paradigms,
including procedural, object-oriented, and functional programming.
y = "Hello"
print(type(y)) # <class 'str'>
MEMBERSHIP OPERATORS
Used to check if a value exists in a sequence (e.g., list, tuple, string).
OPERATOR DESCRIPTION EXAMPLE
in True if value exists "a" in "apple" → True
not in True if value does not exist "b" not in "apple" → True
IDENTITY OPERATORS
Used to compare the memory locations of two objects.
OPERATOR DESCRIPTION EXAMPLE
is True if both objects are the same x is y
is not True if both objects are not the same x is not y
PYTHON BLOCKS
A block is a group of statements that are executed together as a unit. Blocks are defined by their indentation
level, which is a key feature of Python's syntax. Unlike other programming languages that use braces {} or
keywords like begin and end to define blocks, Python uses consistent indentation to group statements.
def greet(name):
message = f"Hello, {name}!" # This is part of the function block
print(message)
greet("Alice")
def bark(self):
print(f"{self.name} says woof!") # Part of the class block
5. Exception Handling Block (try-except)
try:
result = 10 / 0 # Part of the try block
except ZeroDivisionError:
print("Cannot divide by zero!") # Part of the except block
DECLARING AND USING NUMERIC DATA TYPES
1. INTEGER (INT)
# Declaring an integer
x = 10
y = -5
2. FLOAT (FLOAT)
# Declaring a float
a = 3.14
b = -0.001
3. COMPLEX (COMPLEX)
# Declaring a complex number
c = 1 + 2j
d = 3 - 4j