Python Unit 1
Python Unit 1
1
Notes for Chapter 1: Introduction to Python Pro-
gramming
Introduction to Python
Python is an interpreted, object-oriented, high-level programming language cre-
ated by Guido van Rossum. Python’s design philosophy emphasizes code read-
ability and simplicity. It has a simple and easy-to-use syntax, making it a
popular choice for beginners. It supports Object-Oriented programming, which
encapsulates code within objects. Python is dynamically typed and garbage-
collected. It has a large standard library that supports many common program-
ming tasks.
Python Variables
Variables are containers that store values. For example:
name = "John"
age = 25
• Can only contain alpha-numeric characters and underscores (A-z, 0-9, and
).
• Are case-sensitive.
• Reserved words (keywords) cannot be used as variable names.
def my_function():
x = "local"
print(x)
my_function()
print(x)
2
Python Basic Operators
Arithmetic Operators
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Exponentiation: **
Floor division: //
Comparison Operators
Equal: ==
Not equal: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Assignment Operators
Assign: =
3
Add and assign: +=
Subtract and assign: -=
Multiply and assign: *=
Divide and assign: /=
Modulus and assign: %=
Exponentiation and assign: **=
Floor division and assign: //=
Bitwise Operators
AND: &
OR: |
XOR: ^
NOT: ~
Zero fill left shift: <<
Signed right shift: >>
Logical Operators
AND: and
OR: or
NOT: not
4
Operator Description Example
and Logical AND True and False = False
or Logical OR True or False = True
not Logical NOT not True = False
Sequence Types:
• str: String type
• list: List type
Set Types:
• set: Set type
• frozenset: Immutable set type
Boolean Type:
• bool: Boolean type, can be True or False
Binary Types:
5
• bytes: Byte type
• bytearray: Byte array type
• memoryview: Memory view type
None Type:
Important Questions
1. What are the key features of Python?
2. Explain the difference between local and global variables with an example.
3. List and explain different arithmetic operators in Python with examples.
4. What is the significance of indentation in Python? Provide an example.
Answers
1. Key features of Python:
• Interpreted
• Object-oriented
• High-level language
6
• Dynamically typed
• Garbage-collected
• Large standard library
• Simple and readable syntax
x = "global"
def my_function():
x = "local"
print(x)
3. Arithmetic Operators:
# Addition
x = 5 + 3
print(x) # Output: 8
# Subtraction
x = 5 - 3
print(x) # Output: 2
# Multiplication
x = 5 * 3
print(x) # Output: 15
# Division
x = 6 / 3
print(x) # Output: 2.0
# Modulus
x = 5 % 3
print(x) # Output: 2
# Exponentiation
x = 2 ** 3
print(x) # Output: 8
# Floor Division
x = 7 // 2
7
print(x) # Output: 3
4. Significance of Indentation:
for i in range(5):
print(i)
print("Outside the loop")
# Numeric Types
x = 10 # int
y = 10.5 # float
z = 1 + 2j # complex
# Sequence Types
a = "Hello" # str
b = [1, 2, 3] # list
c = (1, 2, 3) # tuple
d = range(5) # range
# Mapping Type
e = {"a": 1, "b": 2} # dict
# Set Types
f = {1, 2, 3} # set
g = frozenset({1, 2, 3}) # frozenset
# Boolean Type
h = True # bool
# Binary Types
i = b"hello" # bytes
j = bytearray(5) # bytearray
k = memoryview(bytes(5)) # memoryview
# None Type
l = None # NoneType
8
Important Code Examples
Example 1: Basic Python Program
print("Hello, World!")
print(greet("Alice"))
Example 4: Looping
for i in range(5):
print(i)
Thank You
9
Figure 2: *
Thank You!
10