Unit 1 Introduction on to Python Programming Language
Unit 1 Introduction on to Python Programming Language
Program execu
on Process, Built- in Data Types, Variables, Strings and String methods, Numbers, Basic Input, Output
and command line input, String forma ng, Pythonliterals, Operators: Arithme c, Comparison,
Assignment, Logical, Bitwise, Membership, and Iden ty, Comments, Indenta on, First Python
program, Styling Python code. Condi onal Statements- If, If-else, Nested If-else, Itera ve Statement –
For, While, Nested Loops, Control statements – Break, Con nue, Pass.
Why Python?
4. Extensive Libraries – Includes built-in modules for data science, machine learning, web
development, etc.
Steps:
📌 Example:
python
CopyEdit
print("Hello, World!")
💡 Run using:
nginx
CopyEdit
python filename.py
1. Numeric Types
complex (Imaginary) → 2 + 3j
📌 Example:
python
CopyEdit
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
2. Sequence Types
📌 Example:
python
CopyEdit
s = "Python"
l = [1, 2, 3]
t = (4, 5, 6)
3. Set Types
4. Mapping Type
📌 Example:
python
CopyEdit
name = "Alice"
age = 25
print(name, age)
String Methods:
Method Description
📌 Example:
python
CopyEdit
Numbers
📌 Example:
python
CopyEdit
a, b = 10, 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus
print(a ** b) # Exponentiation
📌 Example:
python
CopyEdit
print("Hello", name)
Command-Line Input
nginx
CopyEdit
String Formatting
📌 Example:
python
CopyEdit
name = "Alice"
age = 25
📌 Example:
python
CopyEdit
x = None
Operators in Python
Type Operators
Arithmetic + - * / // % **
Assignment = += -= *= /=
Bitwise `&
📌 Example:
python
CopyEdit
x = 10
y = 20
Multi-line comment:
python
CopyEdit
"""
This is a
multi-line comment
"""
Indentation
📌 Example:
python
CopyEdit
if True:
print("Indented block")
📌 Example:
python
CopyEdit
print("Hello, World!")
Conditional Statements
📌 Example:
python
CopyEdit
x = 10
if x > 5:
elif x == 5:
print("x is 5")
else:
Loops in Python
For Loop
📌 Example:
python
CopyEdit
for i in range(5):
print(i)
While Loop
📌 Example:
python
CopyEdit
x=5
while x > 0:
print(x)
x -= 1
Nested Loops
📌 Example:
python
CopyEdit
for i in range(3):
for j in range(3):
print(i, j)
Control Statements
Break Statement
📌 Example:
python
CopyEdit
for i in range(5):
if i == 3:
break
print(i)
Continue Statement
📌 Example:
python
CopyEdit
for i in range(5):
if i == 2:
continue
print(i)
Pass Statement
📌 Example:
python
CopyEdit
for i in range(5):
Func ons: How func ons communicate with their environment? Returning a result from func on,
Types of func on, func on crea on, types of func on crea on, calling, passing parameters, Func on
Scopes ,types of Arguments passed in func on, Lamda Func on. List: Basic List opera ons, Indexing,
Slicing, organizing a list, Built-in func ons of list, Working with list and a part of a list, Condi onal
Execu on, Boolean Expressions, Condi onal Statements with Lists, List Comprehension Expression,
While and For Loop,Itera ons, Documenta on Interlude. Tuple: defini on, Crea on, accessing, dele on,
Itera on, conver ng between list and tuple.
Functions in Python
1. Function Communication
📌 Example:
python
CopyEdit
message = greet("Alice")
📌 Example:
python
CopyEdit
return a + b
result = add(5, 3)
print(result) # Output: 8
python
CopyEdit
def get_person():
3. Types of Functions
python
CopyEdit
def square(n):
return n * n
print(square(4)) # Output: 16
a) Positional Arguments
python
CopyEdit
b) Default Arguments
python
CopyEdit
def greet(name="Guest"):
print(f"Hello, {name}!")
c) Keyword Arguments
python
CopyEdit
person(age=30, name="Alice")
d) Variable-Length Arguments
python
CopyEdit
def add(*numbers):
return sum(numbers)
python
CopyEdit
def info(**details):
6. Function Scope
a) Local Scope
python
CopyEdit
def example():
x = 10 # Local variable
print(x)
example()
b) Global Scope
python
CopyEdit
x = 10 # Global variable
def example():
example()
c) global Keyword
python
CopyEdit
x = 10
def modify():
global x
x += 5
modify()
print(x) # Output: 15
📌 Example:
python
CopyEdit
square = lambda x: x * x
print(square(4)) # Output: 16
📌 Multiple Arguments:
python
CopyEdit
add = lambda a, b: a + b
Lists in Python
📌 Creating a List:
python
CopyEdit
python
CopyEdit
📌 Accessing Elements:
python
CopyEdit
print(l[1]) # Output: 20
print(l[-1]) # Output: 40
📌 Slicing:
python
CopyEdit
3. List Comprehension
📌 Example:
python
CopyEdit
python
CopyEdit
print(item)
CopyEdit
i=0
print(l[i])
i += 1
Function Description
📌 Example:
python
CopyEdit
l = [3, 1, 4, 2]
Tuples in Python
📌 Creating a Tuple:
python
CopyEdit
t = (1, 2, 3, "Python")
python
CopyEdit
print(t[1]) # Output: 2
python
CopyEdit
for item in t:
print(item)
📌 Tuple → List:
python
CopyEdit
t = (1, 2, 3)
l = list(t)
📌 List → Tuple:
python
CopyEdit
l = [1, 2, 3]
t = tuple(l)
Summary