UNIT-1 Notes
UNIT-1 Notes
Using Python
UNIT - 1
DDDM using Python Unit 1
Contents
4 Conditional Logic 16
4.1 Introduction to Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2 If-Else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6 Introduction to Functions 22
6.1 What Are Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1
DDDM using Python Unit 1
7 Unit 1 Summary 25
7.1 Key Concepts Covered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.2 Skills Gained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.3 Business Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.4 Study Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
8 Appendix 27
8.1 Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.3 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.4 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.2.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8.3 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2
DDDM using Python Unit 1
3
DDDM using Python Unit 1
Output:
Code Explanation
Let’s break down this code:
• print - Command that tells Python to display something
• () - Parentheses hold what you want to display
• "Hello, Welcome to Python!" - Text inside quotes
• The quotation marks tell Python this is text, not code
Example
Printing Numbers:
1 print (100)
2 print (2024)
3 print (3.14)
Output:
100
2024
3.14
Example
Printing Multiple Items:
1 print ( " Company Revenue : " , 1000000)
2 print ( " Year : " , 2024 , " Quarter : " , " Q4 " )
Output:
4
DDDM using Python Unit 1
Example
Creating Variables:
1 # Storing business information
2 company_name = " TechCorp "
3 annual_revenue = 5000000
4 growth_rate = 0.15
5 is_profitable = True
6
7 # Displaying the information
8 print ( " Company : " , company_name )
9 print ( " Revenue : " , annual _revenue )
10 print ( " Growth Rate : " , growth_rate )
11 print ( " Profitable : " , is_profitable )
Output:
Company: TechCorp
Revenue: 5000000
Growth Rate: 0.15
Profitable: True
Code Explanation
Understanding the code:
• company name - Variable name (the label on the box)
• = - The equals sign means ”store this value”
• "TechCorp" - This is the value being stored
• # - Lines starting with # are comments (ignored by Python)
Example
Business Scenario - Calculating Total Sales:
1 # Monthly sales data
2 january_sales = 50000
3 february_sales = 62000
4 march_sales = 58000
5
6 # Calculate quarterly sales
7 q1_sales = january_sales + februar y_sales + march_sales
8
9 # Display results
10 print ( " January Sales : " , january_sales )
11 print ( " February Sales : " , february _sales )
12 print ( " March Sales : " , march_sales )
13 print ( " Q1 Total Sales : " , q1_sales )
Output:
Business Insight
Variables in Business Context:
• Store customer information (names, IDs, purchase amounts)
• Track financial metrics (revenue, costs, profit)
• Monitor performance indicators (sales targets, conversion rates)
• Manage inventory (product names, quantities, prices)
Practice Exercise
Try it yourself:
1. Create a variable called product name
5
DDDM using Python Unit 1
6
DDDM using Python Unit 1
Output:
Output:
7
DDDM using Python Unit 1
Output:
<class ’str’>
2.3.1 String Operations
Example
String Concatenation:
1 first_name = " John "
2 last_name = " Smith "
3 full_name = first_name + " " + last_name
4 print ( " Full Name : " , full_name )
5
6 # Creating product codes
7 category_code = " ELC "
8 product_number = " 1045 "
9 ful l_p rodu ct_ c o d e = category_code + " -" + p roduct_ number
10 print ( " Product Code : " , f u l l _ p r o d u c t _ c o d e )
Output:
Example
String Formatting for Reports:
1 # Sales report data
2 sales_person = " Emily Davis "
3 monthly_sales = 125000
4 target = 100000
5
8
DDDM using Python Unit 1
Output:
Example
Business Calculations:
1 # Revenue calculation
2 units_sold = 150
3 price_per_unit = 299.99
4 total_revenue = units_sold * pric e_per_un it
5
6 print ( " Units Sold : " , units_sold )
7 print ( " Price per Unit : $ " , price_ per_uni t )
8 print ( " Total Revenue : $ " , total_revenue )
9
10 # Profit calculation
11 revenue = 50000
12 cost = 32000
13 profit = revenue - cost
14 profit_margin = ( profit / revenue ) * 100
15
16 print ( " \ nProfit Analysis : " )
17 print ( " Revenue : $ " , revenue )
18 print ( " Cost : $ " , cost )
19 print ( " Profit : $ " , profit )
20 print ( " Profit Margin : " , profit_margin , " % " )
Output:
Units Sold: 150
Price per Unit: $ 299.99
9
DDDM using Python Unit 1
Profit Analysis:
Revenue: $ 50000
Cost: $ 32000
Profit: $ 18000
Profit Margin: 36.0 %
Practice Exercise
Practice Problems:
1. Calculate total cost: product costs $50, buy 12 units, 18% tax
2. Salesperson earns $3000 base + 7% commission on $80,000 sales
3. Create variables for customer name, email, and print welcome message
10
DDDM using Python Unit 1
Output:
Example
Accessing Items:
1 products = [ " Laptop " , " Mouse " , " Keyboard " , " Monitor " , " Headphones " ]
2 prices = [899.99 , 25.50 , 79.99 , 299.99 , 149.99]
3
4 first_product = products [0]
5 third_product = products [2]
6 last_product = products [4]
11
DDDM using Python Unit 1
7
8 print ( " First product : " , first_product )
9 print ( " Third product : " , third_product )
10 print ( " Last product : " , last_product )
11
12 # Access using negative indexing ( from the end )
13 last_item = products [ -1]
14 second_last = products [ -2]
15
16 print ( " \ nUsing negative indexing : " )
17 print ( " Last product : " , last_item )
18 print ( " Second to last : " , second_last )
19
20 # Accessing prices
21 first_price = prices [0]
22 print ( " \ nPrice of " , products [0] , " is $ " , first_price )
Output:
Code Explanation
Understanding List Indexing:
12
DDDM using Python Unit 1
Output:
Output:
Initial order: []
After adding laptop: [’Laptop’]
After adding more items: [’Laptop’, ’Mouse’, ’Keyboard’]
After inserting Monitor at position 1: [’Laptop’, ’Monitor’,
’Mouse’, ’Keyboard’]
After extending: [’Laptop’, ’Monitor’, ’Mouse’, ’Keyboard’,
’USB Cable’, ’Laptop Bag’]
13
DDDM using Python Unit 1
Output:
14
DDDM using Python Unit 1
Output:
Number of products: 5
Lowest price: $ 79.99
Highest price: $ 499.99
Total value: $ 1229.95
Average: $ 245.99
Practice Exercise
Practice:
1. Create a list of 5 employee salaries and calculate total payroll
2. Create a list of temperatures and find highest and lowest
3. Create a shopping cart, add 5 items, remove 2, display final cart
15
DDDM using Python Unit 1
4 Conditional Logic
4.1 Introduction to Conditionals
In business, we make decisions based on conditions.
Key Point
Conditional statements let your program make decisions based on whether conditions are true or
false.
Output:
Example
If-Else Statement:
1 current_stock = 15
2 minimum_stock = 20
3
4 if current_stock >= minimum_stock :
5 print ( " Stock level is adequate " )
6 else :
7 print ( " LOW STOCK ALERT ! " )
8 shortage = minimum_stock - current_stock
9 print ( f " Please reorder : { shortage } units " )
Output:
Example
If-Elif-Else:
1 sal es_ achi eve m e n t = 120
2
3 if sal es_a chi e v e m e n t >= 150:
4 rating = " Outstanding "
5 bonus = 15000
6 elif sa les _ac h i e v e m e n t >= 120:
7 rating = " Excellent "
8 bonus = 10000
9 elif sa les _ac h i e v e m e n t >= 100:
10 rating = " Good "
11 bonus = 5000
12 else :
13 rating = " Needs Improvement "
14 bonus = 0
15
16 print ( " Rating : " , rating )
17 print ( " Bonus : $ " , bonus )
Output:
Rating: Excellent
16
DDDM using Python Unit 1
Bonus: $ 10000
Output:
LOAN APPROVED
Interest Rate: 7.5%
Practice Exercise
Practice:
1. Check if student passed (grade ¿= 60)
2. Discount: 10% if purchase ¿ $100, 20% if ¿ $500
3. Check voting eligibility (age ¿= 18)
17
DDDM using Python Unit 1
Output:
Counting from 1 to 5:
1
2
3
4
5
Code Explanation
Understanding the For Loop:
• for - keyword that starts the loop
• i - variable that holds the current value
• in - keyword meaning ”from”
• range(1, 6) - generates numbers from 1 to 5
• : - marks the end of the loop declaration
• Indented code - executes for each value
Range Function Variations:
• range(5) - generates 0, 1, 2, 3, 4
• range(1, 6) - generates 1, 2, 3, 4, 5
• range(0, 10, 2) - generates 0, 2, 4, 6, 8 (step of 2)
18
DDDM using Python Unit 1
Output:
========================================
Mon: $1200
Tue: $1350
Wed: $1100
Thu: $1450
Fri: $1600
Sat: $1800
Sun: $1400
========================================
Example
Calculating Totals with Loops:
1 months = [ " Jan " , " Feb " , " Mar " , " Apr " , " May " , " Jun " ]
2 sales = [45000 , 52000 , 48000 , 61000 , 58000 , 63000]
3
4 print ( " === MONTHLY SALES ANALYSIS ===\ n " )
5
6 # Calculate running total
19
DDDM using Python Unit 1
7 total_sales = 0
8 for i in range ( len ( months ) ) :
9 total_sales += sales [ i ]
10
11 print ( f " { months [ i ]}: $ { sales [ i ]: ,} " )
12 print ( f " Running Total : $ { total_sales : ,} " )
13
14 # Calculate growth from previous month
15 if i > 0:
16 growth = sales [ i ] - sales [i -1]
17 growth_ percent = ( growth / sales [i -1]) * 100
18
19 if growth > 0:
20 print ( f " Growth : + $ { growth : ,} ({ gr owth_pe rcent :.1 f }%) " )
21 else :
22 print ( f " Decline : $ { growth : ,} ({ gr owth_pe rcent :.1 f }%) " )
23 print ()
24
25 # Final summary
26 average_sales = total_sales / len ( sales )
27 print ( " = " *40)
28 print ( f " Total Sales (6 months ) : $ { total_sales : ,} " )
29 print ( f " Average Monthly Sales : $ { average_sales : ,.2 f } " )
Output:
Jan: $45,000
Running Total: $45,000
Feb: $52,000
Running Total: $97,000
Growth: +$7,000 (15.6%)
Mar: $48,000
Running Total: $145,000
Decline: $-4,000 (-7.7%)
Apr: $61,000
Running Total: $206,000
Growth: +$13,000 (27.1%)
May: $58,000
Running Total: $264,000
Decline: $-3,000 (-4.9%)
Jun: $63,000
Running Total: $327,000
Growth: +$5,000 (8.6%)
========================================
Total Sales (6 months): $327,000
Average Monthly Sales: $54,500.00
20
DDDM using Python Unit 1
Output:
Countdown:
5
4
3
2
1
Done!
Practice Exercise
Practice:
1. Calculate factorial of 5 using a loop
2. Apply 15% discount to a list of prices
3. Create multiplication table for numbers 1-5
21
DDDM using Python Unit 1
6 Introduction to Functions
6.1 What Are Functions
A function is a reusable block of code that performs a specific task.
Key Point
Benefits of Functions:
• Reusability: Write once, use many times
• Organization: Break complex problems into smaller pieces
• Maintainability: Update code in one place
• Readability: Give meaningful names to operations
Output:
Output:
Welcome Sarah!
Thank you for shopping with us.
Welcome Michael!
Thank you for shopping with us.
22
DDDM using Python Unit 1
14 tax_rate = 0.18
15
16 tax_amount = calculate_tax ( product_price * qty , tax_rate )
17 print ( f " Tax Amount : $ { tax_amount } " )
18
19 final_total = ca lc ul a te _t o ta l ( product_price , qty , tax_rate )
20 print ( f " Final Total : $ { final_total } " )
Output:
Output:
23
DDDM using Python Unit 1
==================================================
PORTFOLIO ANALYSIS
==================================================
Stock A:
ROI: +25.00%
Profit: $+2,500
Stock B:
ROI: -6.67%
Profit: $-1,000
Real Estate:
ROI: +25.00%
Profit: $+50,000
Bonds:
ROI: +5.00%
Profit: $+2,500
==================================================
PORTFOLIO SUMMARY:
Total Invested: $275,000
Current Value: $329,000
Overall ROI: 19.64%
Total Profit: $54,000
Practice Exercise
Practice:
1. Create a function to calculate rectangle area
2. Write a function to convert Celsius to Fahrenheit
3. Create a function that applies discount to a list of prices
24
DDDM using Python Unit 1
7 Unit 1 Summary
7.1 Key Concepts Covered
1. Python Basics
• What Python is and why it’s valuable
• Using Google Colab
• Print statements and variables
2. Data Types
• Integers and floats
• Strings
• Basic arithmetic operations
3. Lists
• Creating and accessing lists
• Adding and removing items
• List operations
4. Conditional Logic
• If-else statements
• Comparison operators
• Logical operators
5. Loops
• For loops
• While loops
• Processing data
6. Functions
• Creating functions
• Parameters and return values
• Business calculations
25
DDDM using Python Unit 1
Key Point
Programming improves with practice. Every expert started as a beginner!
26
DDDM using Python Unit 1
8 Appendix
8.1 Common Errors
Error Solution
SyntaxError Check colons, parentheses, quotes
IndentationError Use consistent indentation
NameError Variable doesn’t exist
TypeError Incompatible types
IndexError List index out of range
ZeroDivisionError Dividing by zero
8.2.2 Lists
1 items = [1 , 2 , 3 , 4 , 5]
2 items [0] # First item
3 items . append (6) # Add to end
4 items . remove (3) # Remove value
5 len ( items ) # Length
6 sum ( items ) # Sum
8.2.3 Conditionals
1 if condition :
2 # code
3 elif an oth er_ co n d i t i o n :
4 # code
5 else :
6 # code
8.2.4 Loops
1 for item in items :
2 print ( item )
3
4 for i in range (5) :
5 print ( i )
6
7 while condition :
8 # code
8.2.5 Functions
1 def function_name ( param1 , param2 ) :
2 result = param1 + param2
3 return result
4
5 value = function_name (5 , 10)
8.3 Resources
27