Unit-3 10 Strings: Creating and Storing Strings; Accessing Sting Characters; the str() function;
Operations on Strings- Concatenation, Comparison, Slicing and Joining, Traversing; Format
Specifiers; Escape Sequences; Raw and Unicode Strings; Python String Methods; Illustrative
programs. Other data types: Basics of arrays, lists, tuples and related functions 11 Unit-4
Python Functions: Types of Functions; Function Definition- Syntax, Function Calling, Passing
Parameters/arguments, the return statement; Default Parameters; Command line
Arguments; Key Word Arguments; Illustrative programs
Below is the full text formatted as a document. You can copy and paste it into your word
processor (such as Microsoft Word, Google Docs, or LibreOffice Writer) and then save it as a
PDF or DOC file.
Unit 2: Python Built-in Functions, Libraries, and Control Flow
Table of Contents
1. Built-in Functions
1.1 Console Input and Output
1.2 Type Conversions
2. Python Libraries and Importing
2.1 What Are Python Libraries?
2.2 Importing Libraries
2.3 Illustrative Programs Using Libraries
2.4 Libraries for Graphics and Image Handling
3. Python Control Flow
3.1 Conditional Statements (if, elif, else)
3.2 Looping Statements
3.3 Loop Control: break and continue Statements
3.4 The range() Function
3.5 The exit() Function
4. Illustrative Programs
5. Summary
1. Built-in Functions
1.1 Console Input and Output
Console Input:
o Function: input()
o Purpose: Reads data from the user.
o Example:
o user_input = input("Enter your name: ")
Console Output:
o Function: print()
o Purpose: Displays data on the console.
o Example:
o print("Hello, World!")
Example Program:
# Greeting Program
name = input("Enter your name: ")
print("Hello, " + name + "!")
1.2 Type Conversions
Type conversion functions allow you to change the data type of a value:
int(): Converts a value to an integer.
age = int(input("Enter your age: "))
float(): Converts a value to a float.
price = float(input("Enter the price: "))
str(): Converts a value to a string.
num = 123
text = str(num)
Example Program:
# Sum Two Numbers Program
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
sum_value = int(num1) + int(num2)
print("The sum is:", sum_value)
2. Python Libraries and Importing
2.1 What Are Python Libraries?
Definition:
Libraries are collections of pre-written code that provide additional functionality,
making programming easier and faster.
Examples:
Libraries for math operations, data processing, file handling, graphics, and more.
2.2 Importing Libraries
Standard Import:
import library_name
Example:
import math
print([Link](16))
Importing Specific Functions:
from library_name import function_name
Example:
from math import sqrt
print(sqrt(25))
Alias Importing:
import library_name as alias
Example:
import numpy as np
a = [Link]([1, 2, 3])
print(a)
2.3 Illustrative Programs Using Libraries
Example Using the random Library:
import random
# Generate a random integer between 1 and 100
random_number = [Link](1, 100)
print("Random number:", random_number)
2.4 Libraries for Graphics and Image Handling
Pillow (PIL):
Used for image processing such as opening, editing, and saving images.
from PIL import Image
img = [Link]("[Link]")
[Link]() # Displays the image
Pygame:
Used for developing games and graphical applications.
import pygame
[Link]()
screen = [Link].set_mode((640, 480))
[Link].set_caption("My Pygame Window")
running = True
while running:
for event in [Link]():
if [Link] == [Link]:
running = False
[Link]((255, 255, 255)) # Fill screen with white
[Link]()
[Link]()
3. Python Control Flow
Control flow determines the order in which statements are executed in a program.
3.1 Conditional Statements (if, elif, else)
Purpose: Execute code based on conditions.
Syntax:
if condition1:
# Code block 1
elif condition2:
# Code block 2
else:
# Code block 3
Example:
age = int(input("Enter your age: "))
if age < 18:
print("Minor")
elif age == 18:
print("Just became an adult")
else:
print("Adult")
3.2 Looping Statements
Loops allow you to execute a block of code repeatedly.
For Loop
Purpose: Iterate over a sequence (list, tuple, range, etc.)
Syntax:
for variable in sequence:
# Code block
Example Using range():
for i in range(5): # i takes values 0,1,2,3,4
print("Iteration:", i)
While Loop
Purpose: Execute a block of code as long as a condition is true.
Syntax:
while condition:
# Code block
Example:
count = 1
while count <= 5:
print(count)
count += 1
3.3 Loop Control: break and continue Statements
break:
Exits the loop immediately.
for i in range(10):
if i == 3:
break
print(i)
# Output: 0, 1, 2
continue:
Skips the current iteration and moves to the next one.
for i in range(5):
if i == 2:
continue
print(i)
# Output: 0, 1, 3, 4
3.4 The range() Function
Purpose: Generates a sequence of numbers.
Syntax:
range(start, stop, step)
o start: Beginning of sequence (default is 0).
o stop: End of sequence (exclusive).
o step: Increment between numbers (default is 1).
Example:
for i in range(2, 10, 2):
print(i)
# Output: 2, 4, 6, 8
3.5 The exit() Function
Purpose: Terminates the program immediately.
Usage:
Usually, you import the sys module and then call [Link]().
Example:
import sys
print("Exiting now.")
[Link]()
print("This line will not be executed.")
4. Illustrative Programs
Program 1: Even or Odd Checker
# Check if a number is even or odd
num = int(input("Enter an integer: "))
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
Program 2: Loop with break and continue
# Print numbers 0 to 9, skip 5, and break at 8
for i in range(10):
if i == 5:
continue # Skip the number 5
if i == 8:
break # Exit loop when i equals 8
print(i)
5. Summary
Built-in Functions:
o Console Input/Output: Use input() to read data and print() to display output.
o Type Conversions: Use int(), float(), and str() to change data types.
Python Libraries:
o Definition: Libraries are collections of pre-written code that extend Python’s
functionality.
o Importing: Use import, from ... import ..., or aliasing with as.
o Examples: math, random, Pillow for image processing, and Pygame for
graphics.
Control Flow:
o Conditional Statements: Use if, elif, and else to execute code based on
conditions.
o Loops: Use for loops (with range()) and while loops for repeated execution.
o Loop Control: Use break to exit loops and continue to skip iterations.
o Program Termination: Use [Link]() to immediately end a program.
This document provides a comprehensive overview of Python built-in functions, libraries,
and control flow constructs. Feel free to adjust the formatting or add additional sections as
needed before converting it to your final document format.
Simply copy the text above into your word processor and save it as your document. Let me
know if you need any further modifications or additional details!