Python
Python
1. What is Python?
2. History of Python
5. Setting Up Python
8. Python Applications
Python Syntax
Step 1: Define syntax as the set of rules for writing Python code.
Step 2: Emphasize Python's simplicity and readability, resembling plain English.
2. Indentation
Step 1: Explain that Python uses indentation to define code blocks (instead of braces or keywords).
Step 2: Highlight that improper indentation results in IndentationError.
Example: if True:
print("This is indented correctly.")
3. Comments
5. Case Sensitivity
8. Line Continuation
9. Quoting Strings
Variables
Variables in Python
A variable in Python is a name that refers to a value stored in memory, allowing you to store and manipulate data during program execution.
1. Example: x = 10
Description: Assigns an integer value to x.
When to Use: Use this when storing an integer for calculations or comparisons.
3. Example: pi = 3.14
Description: Assigns a floating-point value to the variable pi.
When to Use: Use this when representing decimal values, like measurements or prices.
4. Example: a, b = 5, 10
Description: Assigns 5 to a and 10 to b using multiple assignment.
When to Use: Use this when assigning values to multiple variables in one line for brevity.
5. Example: x, y, z = 1, 2, 3
Description: Assigns 1 to x, 2 to y, and 3 to z using multiple assignment.
When to Use: Use this when you want to assign values to multiple variables simultaneously.
Practice Questions
4. Write a Python code snippet to assign values 20 and 30 to variables x and y in a single line.
5. Assign the values 1, 2, and 3 to variables a, b, c simultaneously.
6. Create a variable language and assign the string 'Python Programming' to it.
11. Write a Python code snippet to store the value True in a variable is_active.
12. Assign multiple variables x, y, and z with the same value 10.
13. Write Python code to assign the value of x = 5 and then double it in the next statement.
14. Assign the string 'Learning Python' to a variable subject and print it.
15. Define a variable price to store the value 49.99 and then print its type.
20. Create a variable coordinates and assign a tuple (5, 10) to it.
Constants
Constants in Python
A constant in Python is a variable whose value is not meant to be changed once it is assigned. By convention, constants are written in uppercase letters
with words separated by underscores. Python doesn't enforce immutability of constants, but following this convention helps developers recognize values
that should remain constant.
Examples
o Code: PI = 3.14159
o Output: PI = 3.14159
o When to Use: Use when setting application limits like thresholds or size caps.
Practice Questions
1. Write a Python program that defines a constant for the speed of light (SPEED_OF_LIGHT = 299792458 m/s) and prints its value.
2. Create a constant DAYS_IN_A_WEEK and write a program that calculates the number of days in a given number of weeks.
3. Define a constant EARTH_RADIUS_KM and write a program that calculates the circumference of Earth using the formula 2 * PI * EARTH_RADIUS_KM.
4. Use a constant FREEZING_POINT_C (0°C) and write a program that checks if a given temperature is above or below freezing.
5. Write a Python program that uses MAX_SCORE = 100 to validate if a student’s score is within a valid range.
6. Define a constant GRAVITY and write a program that calculates the weight of an object given its mass.
7. Create a constant MONTHS_IN_A_YEAR and write a program that calculates how many months are there in a given number of years.
8. Write a program that uses a constant MIN_AGE_TO_VOTE = 18 to check if a person is eligible to vote.
9. Use a constant TAX_RATE = 0.18 and write a program to calculate the total price of a product after tax.
10. Define a constant BOILING_POINT_C (100°C) and write a program that determines if a given temperature is above boiling.
11. Create a constant HOURS_IN_A_DAY and use it in a program to calculate the number of hours in a given number of days.
12. Write a program that uses the constant PI to calculate the area of a circle for a given radius.
13. Define a constant MAX_ATTEMPTS = 3 and write a program that terminates after 3 unsuccessful login attempts.
14. Create a constant MILLISECONDS_IN_A_SECOND and write a program that converts seconds to milliseconds.
15. Define a constant LIGHT_YEAR_KM = 9.461e12 and write a program to calculate the distance light travels in a given number of years.
16. Use a constant GRAVITATIONAL_CONSTANT = 6.674e-11 to write a program that calculates the gravitational force between two objects.
17. Define a constant WATER_DENSITY = 997 kg/m³ and use it to calculate the mass of water given its volume.
18. Write a program that uses MAX_SIZE to validate the size of a list before adding more elements.
19. Create a constant EXCHANGE_RATE = 82.5 and write a program that converts dollars to Indian rupees.
20. Use a constant DISCOUNT_RATE = 0.15 and write a program to calculate the discounted price of an item.
Comments
Comments in Python
In Python, comments are used to annotate code with explanations, making it easier to understand. They are ignored during program execution.
1. Single-Line Comments
o When to Use: Use it for short explanations or notes on specific lines of code.
2. TODO Comments
o When to Use: Use this to remind yourself or others about sections of code that need improvement.
3. Multi-Line Comments
o Example:
o """
o """
o When to Use: Use them for detailed explanations or documentation for a block of code.
Practice Questions
1. Write a Python program that includes single-line comments to explain each line of code.
2. Use a multi-line comment to describe the purpose of a function that takes user input and prints a greeting message.
3. Add comments to an existing Python program (or write a new one) to explain the logic behind a loop.
4. Write a Python program that includes TODO comments indicating where improvements can be made in the code.
5. Add comments to a Python class that explain its methods and purpose.
6. Write a Python script with comments explaining how to import and use an external library (e.g., math or random).
7. Use single-line comments to annotate a simple program that calculates the area of a rectangle.
8. Use a multi-line comment to explain the overall functionality of a program that checks if a number is prime.
9. Add comments to a program that uses conditionals (if, elif, else) to determine if a number is positive, negative, or zero.
10. Write a program that includes comments explaining the purpose of each variable.
11. Write a Python function that includes comments to describe its arguments and return value.
12. Use TODO comments in a Python script to mark areas where error handling should be added.
13. Write a Python program that includes comments explaining the purpose of a list comprehension.
14. Add comments to a program that demonstrates the use of a try...except block.
15. Write a Python program with comments that explain how to read from and write to a file.
16. Use a multi-line comment to document the steps of a program that calculates the Fibonacci sequence.
17. Write a Python script with comments explaining how to use a for loop to iterate over a dictionary.
18. Add comments to a program that generates random numbers and explains the purpose of the seed function.
20. Write a Python class that includes comments explaining its attributes, constructor, and methods.
In Python, input() is used to receive data from the user, and print() is used to display data on the screen. These functions make Python programs
interactive by allowing dynamic data handling.
Examples
1. Getting User Input and Storing It in a Variable
o Description: The input() function prompts the user for their name and stores it in the variable name.
o When to Use: When you want the program to accept user input dynamically.
o Description: The print() function displays the message along with the user input.
o When to Use: Use print() to communicate results, instructions, or feedback to the user.
o Description: The input() function accepts a string, and int() converts it to an integer before storing it in the variable age.
o When to Use: When numeric input is expected, like for calculations or comparisons.
o Description: Combines text and variable data in a single print() statement for better readability.
o When to Use: When displaying results or summaries to the user.
Practice Questions
1. Write a program that asks the user for their favorite color and prints a message including their response.
2. Create a program that takes a number input from the user and prints its square.
3. Write a program that takes two numbers as input and displays their sum.
4. Ask the user to input their first name and last name, then display a message saying "Hello, [First Name] [Last Name]!"
5. Write a program that asks the user for their birth year and calculates their age.
6. Create a program that takes a string input and prints the number of characters in it.
7. Write a program that takes a user's height and weight as input and calculates their Body Mass Index (BMI).
8. Create a program that prompts the user to enter three numbers and displays their average.
9. Write a program that asks the user for their city and country, then prints "You live in [City], [Country]."
10. Ask the user to input a number and check if it is even or odd.
11. Create a program that asks the user for a temperature in Celsius and converts it to Fahrenheit.
12. Write a program that takes a username and password as input and confirms if the login is successful.
13. Create a program that prompts the user for the length and width of a rectangle, then calculates its area.
14. Write a program that takes the user's hourly wage and hours worked in a week, then calculates their weekly salary.
15. Ask the user to input a number and display its multiplication table up to 10.
16. Create a program that takes a list of five numbers as input (separated by spaces) and prints the largest number.
17. Write a program that asks the user for their exam scores in three subjects and calculates the average score.
18. Ask the user to enter a word and print whether it is a palindrome (reads the same backward).
19. Create a program that asks for a temperature in Fahrenheit and converts it to Celsius.
20. Write a program that takes a sentence as input and counts the number of words in it.
Data Types
Data Types in Python
Python supports several data types to handle different kinds of data. These data types are divided into mutable (changeable) and immutable
(unchangeable) categories. Here are the most commonly used data types in Python:
o Example: x = 5
o Example: y = 3.14
o When to use: When you need to store numbers with fractional values.
complex (Complex numbers): Numbers with a real and an imaginary part. Represented as a + bj.
o Example: z = 2 + 3j
o When to use: When working with complex numbers in mathematics, like in electrical engineering or signal processing.
str (String): A sequence of characters enclosed in quotes (single, double, or triple quotes).
list: Ordered, mutable collection of items. Lists can contain items of different data types.
o When to use: When you need a collection of items that may change during the program execution.
o When to use: When you need a collection of items that should not change.
o When to use: When you need a sequence of numbers, typically used in loops.
4. Mapping Data Types
o When to use: When you need to associate a value with a specific key.
o When to use: When you need to store unique values without duplicates.
o When to use: When you need to store binary values or states (True/False).
memoryview: A view object that exposes an array's buffer interface, enabling you to access slices of data.
o When to use: When you need to handle slices of large data efficiently.
8. None Type
o Example: x = None
Practice Questions
1. Write a Python program that stores an integer, float, and complex number in separate variables, and print them.
2. Create a program that checks the data type of a variable and prints it.
3. Write a Python program to store user information (name, age, and is_active) in a dictionary and print it.
4. Create a set of fruits and print the set without duplicates.
7. Write a program that uses a tuple to store coordinates and prints them.
8. Create a list of numbers, and calculate and print the sum and average of the numbers.
9. Write a Python program that takes a string input and converts it to a list of characters.
10. Demonstrate using a frozenset by converting a list to a frozenset and printing it.
11. Write a Python program that takes two numbers as input and returns their product as a float.
12. Create a program that stores the names of five cities in a dictionary with their populations and print them.
14. Create a set of numbers and check whether a given number exists in the set.
17. Demonstrate the use of bytearray by creating a bytearray and printing it.
18. Write a program that accepts user input and stores the information in a tuple.
19. Create a Python program that stores and prints a person's name, age, and marital status as a dictionary.
20. Write a program to calculate the length of a string and print it.
Strings
Strings in Python
In Python, a string is a sequence of characters enclosed within either single ('), double ("), or triple quotes (''' or """). Strings are used to represent text-
based data, and you can perform various operations like indexing, slicing, concatenation, and applying methods to manipulate them.
Examples
o When to Use: Use strings to represent text such as names, sentences, or any other characters.
o When to Use: Use single quotes when you prefer not to use double quotes.
3. Multi-line String
o Output:
o When to Use: Use triple quotes for multi-line strings, especially for docstrings or long text.
o Code: print(text[0])
o Output: H
5. String Slicing
o Code: print(message[7:12])
o Output: is fu
o When to Use: Use slicing to get a portion of the string between specific indices.
o Code: print(message.upper())
o When to Use: Use string methods like upper() to manipulate or format the string content.
o Code: print(message.lower())
o Output: python is fun!
o When to Use: Use string methods like lower() to manipulate or format the string content.
8. String Concatenation
9. String Repetition
o Code: print("Python" * 3)
o Output: PythonPythonPython
o Code: print("Python"[2:5])
o Output: tho
o When to Use: Use slicing to extract a portion of the string using the start and end indices.
Practice Questions
1. Create a program that takes a string input from the user and displays the first and last character.
2. Write a program that takes a string and prints the substring from the middle of it.
3. Concatenate two strings given by the user and print the result.
5. Use string slicing to extract the word "Python" from the string "I love Python programming".
7. Create a program that uses repetition to print a string multiple times, like "Python" repeated 5 times.
8. Create a multi-line string that contains a poem or a quote and prints it.
9. Write a program that checks if the string "Python" is present in a given text.
10. Create a program that takes a string and replaces all occurrences of "Python" with "Java".
12. Use string methods to remove any whitespace at the beginning or end of a string.
13. Create a program that splits a sentence into words and prints them individually.
18. Take two strings as input and compare them to check if they are equal.
19. Create a program that formats a string to display the name and age of a person in the format "Name: John, Age: 25".
20. Create a program that takes a string and prints the number of characters in it.
Lists
Lists in Python
In Python, a list is an ordered collection of items that can hold multiple elements, which can be of any data type. Lists are mutable, meaning their
content can be modified after creation. Lists are created using square brackets, and they can store values such as numbers, strings, booleans, and even
other lists.
Examples
o Description: A list containing three strings. Lists are created using square brackets.
o When to Use: Use a list when you need to store multiple values in an ordered collection.
o Output: [1, 2, 3, 4, 5]
o When to Use: Use a list for storing numerical values that need to be accessed or modified later.
3. Creating a Mixed Data Type List
o Description: A list containing elements of different data types (string, integer, float, boolean).
o Code: fruits[1]
o Output: 'banana'
o Code: fruits[-1]
o Output: 'cherry'
o Description: Accessing the last element of the list using negative indexing.
o When to Use: Use negative indexing to access elements from the end of the list.
6. List Slicing
o Code: fruits[1:3]
o Code: fruits.append('orange')
o When to Use: Use append() to add a new item to the end of the list.
o Code: fruits.remove('banana')
o Description: Inserting an item at a specific index in the list using insert() method.
o When to Use: Use insert() to add an item at a specific position in the list.
o Code: fruits.pop()
o Output: 'orange'
o Description: Removing and returning the last item of the list using pop().
o When to Use: Use pop() to remove and return the last item or a specific item from the list.
o Code: numbers.sort()
o Output: [1, 2, 3, 4, 5]
o Description: Sorting the list using the sort() method (ascending order by default).
o Code: numbers.reverse()
o Output: [5, 4, 3, 2, 1]
o Code: len(fruits)
o Output: 4
o Description: Getting the length of the list using the len() function.
o When to Use: Use len() to find the number of elements in the list.
Practice Questions
1. Create a list of your favorite fruits and access the second element in the list.
2. Write a program that takes a list of numbers and finds the largest number.
3. Create a mixed data type list and display the third element.
4. Use list slicing to get the first 3 items from a list of numbers.
6. Create a list of colors and insert a new color at the second index.
7. Add a new item to the list and display the updated list.
10. Create a list with some items and use the pop() method to remove and print the last item.
11. Use the len() function to print the number of items in a list of animals.
12. Create a list of integers and check if a specific integer is present in the list.
14. Use the append() method to add a new item to an existing list of strings.
19. Write a program that removes a specific element from the list.
20. Create a list, sort it, and print both the original and sorted lists.
Tuples
Tuples in Python
A tuple is a collection of ordered, immutable elements. Tuples are similar to lists, but unlike lists, once a tuple is created, its elements cannot be modified
(immutable). Tuples are defined using parentheses (()).
Examples
o When to Use: Use a tuple when you need to store multiple values that should not be modified (immutable).
o Output: (1, 2, 3, 4, 5)
o When to Use: Use a tuple to store numerical values that should not change during the program's execution.
o Description: A tuple containing elements of different data types (string, integer, float, boolean).
o When to Use: Use a tuple when you need to store values of different types that remain constant.
o Code: fruits[1]
o Output: 'banana'
o Code: fruits[-1]
o Output: 'cherry'
o Description: Accessing the last element of the tuple using negative indexing.
o When to Use: Use negative indexing to access elements from the end of the tuple.
6. Slicing a Tuple
o Code: fruits[1:3]
o Output: 3
o Description: Getting the length of the tuple using the len() function.
o Code: a = (1, 2, 3)
o Description: Tuples are immutable, so assigning a new value to an existing element is not allowed.
o When to Use: Tuples are useful when you want to ensure the integrity of the data by preventing modification.
9. Nested Tuple
o When to Use: Use nested tuples to store multiple tuples as a single tuple.
o Code: a = (1, 2, 3)
o Output: (1, 2, 3)
o When to Use: Use a tuple to hold elements that don't need to be changed, such as coordinates or settings.
Common Operations with Tuples
Negative Indexing: Access elements from the end using negative indices.
Slicing: Extract a part of the tuple using a start and end index.
Concatenation: You can concatenate tuples using the + operator, but the tuples themselves remain immutable.
Here are 20 practice questions to help you understand tuples and how to work with them in Python:
1. Creating a Tuple
Create a tuple called fruits that contains three elements: 'apple', 'banana', and 'cherry'. Print the tuple.
3. Negative Indexing
Using the tuple fruits = ('apple', 'banana', 'cherry'), print the last element using negative indexing.
4. Slicing a Tuple
Given numbers = (1, 2, 3, 4, 5), slice the tuple to extract the last three elements.
5. Tuple Length
Create a tuple ages = (25, 30, 35, 40) and print its length.
6. Nested Tuple
Create a tuple called person that contains the following: ('John', 30, ('Python', 'Java')). Access and print the second language from the nested tuple.
7. Concatenating Tuples
Create two tuples: tuple1 = ('a', 'b', 'c') and tuple2 = ('d', 'e', 'f'). Concatenate them and print the result.
8. Repeating a Tuple
Given repeat_tuple = (1, 2, 3), repeat it three times and print the result.
Sets
Here’s an improved version of the set examples with explanations and usage suggestions, followed by practice questions:
Description: A set containing three unique strings. Sets are created using curly braces.
When to Use: Use a set when you need to store multiple unique values, ensuring that no duplicates are allowed.
numbers = {1, 2, 3, 4, 5}
Output: {1, 2, 3, 4, 5}
Description: A set containing elements of different data types (string, integer, float, boolean).
When to Use: Use a set to store elements of mixed types, but remember it will not allow duplicates.
len(fruits)
Output: 3
Description: Getting the length of the set using the len() function.
When to Use: Use len() to find the number of elements in the set.
'apple' in fruits
Output: True
fruits.add('orange')
When to Use: Use add() to insert an item into a set if it is not already present.
fruits.remove('banana')
Description: Removing an item from the set using remove(). If the item does not exist, an error occurs.
When to Use: Use remove() to delete a specific item from the set.
fruits.discard('banana')
Description: Removing an item from the set using discard(). Does not raise an error if the item is absent.
When to Use: Use discard() when you want to remove an item safely (no error if item is missing).
Example 9: Clearing All Items from a Set
fruits.clear()
Output: set()
When to Use: Use clear() to remove all items from the set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.union(set2)
Output: {1, 2, 3, 4, 5}
Description: Getting the union of two sets (combining both sets, no duplicates).
set1.intersection(set2)
Output: {3}
When to Use: Use intersection() to find common elements between two sets.
Example 12: Performing Set Difference
set1.difference(set2)
Output: {1, 2}
Description: Getting the difference between two sets (elements in set1 but not in set2).
When to Use: Use difference() to find items present in one set but not the other.
3 in set1
Output: True
set1.update({6, 7, 8})
Output: {1, 2, 3, 6, 7, 8}
When to Use: Use update() to add multiple elements to a set in one operation.
Example 15: Set Symmetric Difference
set1.symmetric_difference(set2)
Output: {1, 2, 4, 5}
Description: Getting the symmetric difference between two sets (elements that are in either set but not both).
When to Use: Use symmetric_difference() to find elements that are in one set but not both.
list_data = [1, 2, 3, 4, 5]
set_from_list = set(list_data)
Output: {1, 2, 3, 4, 5}
Description: Creating a set from an existing list. Duplicates from the list are removed.
When to Use: Use this method when you need to convert a list into a set to eliminate duplicates.
set_copy = set1.copy()
Output: {1, 2, 3, 6, 7, 8}
When to Use: Use copy() to create a duplicate set without affecting the original set.
Example 18: Using Sets for Fast Membership Testing
if 2 in set1:
Description: Using a set for fast membership testing (checking if an element exists).
When to Use: Use sets when you need fast lookups and do not need to store ordered elements.
set1.isdisjoint(set2)
Output: False
Description: Checking if two sets are disjoint (i.e., have no common elements).
When to Use: Use isdisjoint() to test if two sets have no common elements.
set1.issubset({1, 2, 3, 4, 5})
Output: True
When to Use: Use issubset() to check if one set contains all elements of another set.
Practice Questions:
6. How can you combine two sets into one set without duplicates?
9. What will happen if you try to add a mutable object (like a list) to a set?
10. Write a code to get the symmetric difference between two sets.
15. Can you perform set operations (like union and intersection) on sets with different data types? Why or why not?
20. How would you find the unique elements in two lists using sets?
Dictionaries
Dictionary Examples and Descriptions
1. Example:
Output:
Description:
A dictionary containing key-value pairs, where each key is unique, and values can be of any type.
When to Use:
Use a dictionary when you need to store data in key-value pairs, like in databases or configurations.
3. Example:
Output:
Description:
A simple dictionary with two key-value pairs: name as the key and Alice as the value.
When to Use:
Use dictionaries for structured data with unique keys for fast access to values.
5. Example:
6. empty_dict = {}
Output:
{}
Description:
An empty dictionary can be created using empty curly braces {}.
When to Use:
Use an empty dictionary when you plan to add data later.
7. Example:
8. student['name']
Output:
'John'
Description:
Accessing the value of a specific key in the dictionary.
When to Use:
Use keys to retrieve specific values from a dictionary.
9. Example:
10. student.get('age')
Output:
20
Description:
Accessing the value of the age key using the get() method. It returns None if the key doesn't exist.
When to Use:
Use get() when you want a safe way to access a key's value without raising an error.
11. Example:
12. student['age'] = 21
Output:
Description:
Updating the value of the age key to 21.
When to Use:
Use assignment to modify or update the value of an existing key.
13. Example:
Output:
Description:
Adding a new key-value pair to the dictionary.
When to Use:
Use this method to add new key-value pairs to an existing dictionary.
15. Example:
Description:
Removing an item from the dictionary using the del keyword.
When to Use:
Use del to remove key-value pairs that are no longer needed.
17. Example:
18. student.pop('course')
Output:
'Python'
Description:
Removes the item with the specified key and returns its value.
When to Use:
Use pop() to remove an item and retrieve its value.
19. Example:
20. student.keys()
Output:
Description:
Returns a view object containing the dictionary's keys.
When to Use:
Use keys() to get all the keys from the dictionary.
21. Example:
22. student.values()
Output:
Description:
Returns a view object containing the dictionary's values.
When to Use:
Use values() to get all the values from the dictionary.
23. Example:
24. student.items()
Output:
Description:
Returns a view object containing a list of tuples, each tuple containing a key-value pair.
When to Use:
Use items() to iterate over both keys and values together.
25. Example:
26. student.clear()
Output:
{}
Description:
Clears all the items from the dictionary.
When to Use:
Use clear() to remove all key-value pairs from the dictionary.
Practice Questions:
2. What does get() method do when a key does not exist in a dictionary?
5. What is returned by student.keys() for a dictionary student = {'name': 'John', 'age': 20}?
7. What does student['name'] return for the dictionary student = {'name': 'John', 'age': 20}?
9. How would you access the value of the key 'age' in a dictionary student = {'name': 'John', 'age': 20}?
10. Can you store multiple values in a dictionary under the same key? Why or why not?
11. How would you remove the item with key 'age' from the dictionary student = {'name': 'John', 'age': 20, 'course': 'Python'}?
13. Write a Python code to remove the key-value pair 'course': 'Python' from the dictionary.
14. How can you get the list of all values in a dictionary student = {'name': 'John', 'age': 20, 'course': 'Python'}?
15. How would you safely access a dictionary key without raising an error if the key does not exist?
19. What is the output of student.get('address', 'Unknown') if the key 'address' does not exist?
22. Write a Python code to iterate through the dictionary and print each key and value.
Booleans
Example, Output, Description, and When to Use
1. Example:
2. x = True
Output:
True
Description:
A boolean value representing True.
When to Use:
Use True when you want to represent truth or a valid condition in logical operations.
3. Example:
4. y = False
Output:
False
Description:
A boolean value representing False.
When to Use:
Use False when you want to represent a false condition or an invalid state.
5. Example:
6. 5 > 3
Output:
True
Description:
The result of a comparison operation that evaluates if 5 is greater than 3.
When to Use:
Use comparisons like >, <, == to produce boolean values for conditional checks.
7. Example:
8. 5 == 5
Output:
True
Description:
The result of a comparison operation that checks if 5 is equal to 5.
When to Use:
Use == to check if two values are equal in conditional statements.
9. Example:
10. 4 == 5
Output:
False
Description:
The result of a comparison operation that checks if 4 is equal to 5.
When to Use:
Use == to compare values for equality.
11. Example:
Output:
False
Description:
The negation of a boolean value, where not reverses the boolean value.
When to Use:
Use not to negate a boolean value in conditional statements.
13. Example:
Output:
True
Description:
The negation of a boolean value, reversing False to True.
When to Use:
Use not for logical negation in conditional statements.
15. Example:
Output:
False
Description:
The result of a logical AND operation between two boolean values.
When to Use:
Use and to check if both conditions are true.
17. Example:
Output:
True
Description:
The result of a logical OR operation between two boolean values.
When to Use:
Use or to check if at least one of the conditions is true.
19. Example:
20. bool(5)
Output:
True
Description:
Converts a value to its corresponding boolean value, where any non-zero number is considered True.
When to Use:
Use bool() to convert values like numbers or strings to boolean types.
21. Example:
22. bool(0)
Output:
False
Description:
Converts 0 to its corresponding boolean value, False.
When to Use:
Use bool() to evaluate the truthiness of a value (e.g., 0 is False, and non-zero is True).
Practice Questions
1. Create a boolean variable that checks if a number is greater than 10 and print the result.
Solution:
number = 12
2. Write a program that uses the not operator to negate a boolean value.
Solution:
is_valid = True
3. Develop a program that checks if two conditions are both true using the and operator.
Solution:
x=5
y=8
4. Create a program that checks if at least one of two conditions is true using the or operator.
Solution:
a=2
b = 12
5. Convert a number to its boolean equivalent using the bool() function and print the result.
Solution:
num = 5
6. Write a program that checks if a variable is True or False and prints the result.
Solution:
flag = False
if flag:
print("True")
else:
These practice questions will help you apply the boolean concepts and understand how they work in various scenarios.
NoneType
Example, Output, Description, and When to Use
1. Example:
2. x = None
Output:
None
Description:
The special constant None is used to represent the absence of a value or a null state.
When to Use:
Use None to represent a variable that has no value assigned to it or to indicate a missing value.
3. Example:
4. print(type(None))
Output:
<class 'NoneType'>
Description:
The type of None is NoneType, indicating that it is a special built-in constant.
When to Use:
Use None to initialize variables that may be assigned values later or to check for absence.
5. Example:
6. a = None
7. if a is None:
8. print("No value")
Output:
No value
Description:
The is operator is used to check if a variable is None.
When to Use:
Use is None to check if a variable has no value (i.e., if it is None).
9. Example:
Output:
None
Description:
A function returning None indicates that it does not return a meaningful value.
When to Use:
Use None as a default return value for functions that don't need to return anything.
12. Example:
13. x = None
14. y = None
15. print(x == y)
Output:
True
Description:
None values are considered equal when compared with each other.
When to Use:
Use None for uninitialized variables or in cases where no return value is necessary.
16. Example:
17. x = None
Output:
False
Description:
The is not operator is used to check that a variable is not None.
When to Use:
Use is not None to check if a variable has been assigned a value (i.e., it is not None).
19. Example:
20. x = None
21. print(bool(x))
Output:
False
Description:
When None is converted to a boolean value, it evaluates as False.
When to Use:
Use bool(None) to evaluate the truthiness of None, which is always False.
Practice Questions
1. Write a program that checks if a variable is None and prints an appropriate message.
Solution:
variable = None
if variable is None:
else:
2. Create a function that returns None and explain its use case.
Solution:
def my_function():
return None
result = my_function()
Explanation:
The function my_function does not return any meaningful value, so it returns None. This can be useful in cases where the function's main purpose is to
perform an action without needing to return data (e.g., logging or updating a database).
3. Write a Python program that assigns None to a variable and later assigns a value to it. Print both values.
Solution:
x = None
x = 10
print(x) # Output: 10
4. Create a program that uses the is not None condition to check if a variable has a value assigned to it.
Solution:
y=5
if y is not None:
else:
print("y is None.")
5. Develop a program that prints the boolean value of a variable set to None.
Solution:
z = None
6. Write a Python function that returns None and then prints the result of calling the function.
Solution:
def no_return():
return None
These practice questions will help you gain a deeper understanding of None and its usage in Python.
Operators
Arithmetic Operators
Examp Outpu
Description When to Use
le t
x=5+ Use + when you need to sum
8 The addition operator (+) adds two numbers.
3 numbers or concatenate strings.
x=5- The subtraction operator (-) subtracts one Use - to subtract values, whether they
2
3 number from another. are numbers or elements from a list.
Use * when you need to multiply
x=5* The multiplication operator (*) multiplies two
15 numbers or repeat sequences like lists
3 numbers.
or strings.
x=5/ The division operator (/) divides one number
2.5 Use / for floating point division.
2 by another, returning a float.
The floor division operator (//) divides one
x = 5 // Use // when you want to perform
2 number by another and returns the largest
2 division and get an integer result.
integer less than or equal to the result.
x=5% The modulus operator (%) returns the Use % when you need the remainder
1
2 remainder of division. of a division.
x = 5 ** The exponentiation operator (**) raises a Use ** when you need to calculate
25
2 number to the power of another number. powers or exponents.
Operator precedence follows PEMDAS Use arithmetic operators to perform
x = 10 /
15.0 (Parentheses, Exponents, Multiplication, combined operations with respect to
2*3
Division, Addition, Subtraction). operator precedence.
Use parentheses to group operations
x = (10 Parentheses can be used to change the order
36 and change the precedence of
+ 2) * 3 of operations in expressions.
operations.
Practice Questions
1. Write a program that adds two numbers and prints the result.
2. Create a program that performs subtraction of two numbers and displays the result.
3. Develop a Python program that multiplies two numbers and prints the result.
4. Write a program to divide two numbers and display the quotient.
5. Create a Python program that calculates the modulus of two numbers.
6. Write a program that raises one number to the power of another number and prints the result.
7. Create a program that uses floor division to divide two numbers and display the result.
8. Write a program that uses the combination of multiple arithmetic operators and prints the final result.
9. Develop a program to check if the result of division is an integer or a float.
10. Create a program that calculates and prints the result of an expression involving multiple arithmetic operators and parentheses.
Relational/Comparison Operators
Example Output Description When to Use
The greater than operator (>) checks if
Use > to check if one value is larger
x=5>3 True the left operand is greater than the right
than another.
operand.
The less than operator (<) checks if the Use < to check if one value is
x=5<3 False
left operand is less than the right operand. smaller than another.
The equality operator (==) checks if both Use == to compare if two values are
x = 5 == 5 True
operands are equal. equal.
The not equal operator (!=) checks if two Use != to check if two values are not
x = 5 != 3 True
operands are not equal. equal.
The greater than or equal to operator (>=)
Use >= to check if one value is
x = 5 >= 3 True checks if the left operand is greater than
greater than or equal to another.
or equal to the right operand.
The less than or equal to operator (<=)
Use <= to check if one value is
x = 5 <= 3 False checks if the left operand is less than or
smaller than or equal to another.
equal to the right operand.
The equality operator (==) works for Use == to check equality between
x = 5 ==
True comparing integers and floating-point different data types (e.g., integer vs
5.0
numbers. float).
x = (5 > 3) True Relational operators can be combined Use logical operators to combine
and (8 < using logical operators like and and or. relational expressions.
10)
Use combined operators when you
x = 5 == 5 A combination of comparison and logical
True need to check multiple conditions
and 3 > 2 operators.
simultaneously.
The != operator returns False when both Use != to ensure two values are not
x = 5 != 5 False
operands are equal. equal.
Practice Questions
1. Write a program that checks if one number is greater than another and prints the result.
2. Create a program to compare if two numbers are equal and print a message.
3. Develop a Python program that checks if a number is not equal to another number.
4. Write a program to check if a number is greater than or equal to another number.
5. Create a Python program that checks if a number is less than or equal to another number.
6. Write a program that checks if two numbers are equal but considers data types (e.g., integer vs float).
7. Develop a program that uses multiple relational operators combined with logical operators like and and or.
8. Write a program to evaluate complex relational expressions like (x > y) and (a < b).
9. Create a program that compares multiple conditions using relational operators and prints the results.
10. Develop a program that checks if a variable is neither less than nor greater than another value.
Logical Operators
Outpu
Example Description When to Use
t
The greater than operator (>) checks if the left operand is
x=5>3 True Use > to check if one value is larger than another.
greater than the right operand.
x=5<3 False The less than operator (<) checks if the left operand is less Use < to check if one value is smaller than another.
than the right operand.
x = 5 == 5 True The equality operator (==) checks if both operands are equal. Use == to compare if two values are equal.
The not equal operator (!=) checks if two operands are not
x = 5 != 3 True Use != to check if two values are not equal.
equal.
The greater than or equal to operator (>=) checks if the left
x = 5 >= 3 True Use >= to check if one value is greater than or equal to another.
operand is greater than or equal to the right operand.
The less than or equal to operator (<=) checks if the left
x = 5 <= 3 False Use <= to check if one value is smaller than or equal to another.
operand is less than or equal to the right operand.
x = 5 == The equality operator (==) works for comparing integers and Use == to check equality between different data types (e.g.,
True
5.0 floating-point numbers. integer vs float).
x = (5 > 3)
Relational operators can be combined using logical operators
and (8 < True Use logical operators to combine relational expressions.
like and and or.
10)
x = 5 == 5 Use combined operators when you need to check multiple
True A combination of comparison and logical operators.
and 3 > 2 conditions simultaneously.
x = 5 != 5 False The != operator returns False when both operands are equal. Use != to ensure two values are not equal.
Practice Questions
1. Write a program that checks if one number is greater than another and prints the result.
2. Create a program to compare if two numbers are equal and print a message.
3. Develop a Python program that checks if a number is not equal to another number.
4. Write a program to check if a number is greater than or equal to another number.
5. Create a Python program that checks if a number is less than or equal to another number.
6. Write a program that checks if two numbers are equal but considers data types (e.g., integer vs float).
7. Develop a program that uses multiple relational operators combined with logical operators like and and or.
8. Write a program to evaluate complex relational expressions like (x > y) and (a < b).
9. Create a program that compares multiple conditions using relational operators and prints the results.
10. Develop a program that checks if a variable is neither less than nor greater than another value.
Practice Questions
1. Write a program that checks if both conditions (e.g., x > 5 and y < 10) are true using the and operator.
2. Create a program that checks if at least one of the conditions (e.g., x == 5 or y != 10) is true using the or operator.
3. Write a program that negates the result of a condition using the not operator.
4. Develop a Python program that combines multiple and and or operators to evaluate a set of conditions.
5. Write a program that checks if a number is both greater than 10 and less than 20 using logical operators.
6. Create a program that uses logical operators to check if a variable is either equal to one value or less than another.
7. Write a program to combine and, or, and not operators to create a complex logical condition.
8. Develop a Python program that returns True if a condition is false, using the not operator.
9. Create a program that combines multiple conditions using logical operators and prints a result based on those conditions.
10. Write a program that checks if a condition is True when using combinations of or and not.
Assignment Operators
Exampl Outpu
Description When to Use
e t
Use this to assign a value to a
x=5 5 The = operator is used to assign a value to a variable.
variable.
The += operator adds the right-hand operand to the left-hand operand and assigns the result Use += to increment the value of a
x += 3 8
to the left operand. variable.
The -= operator subtracts the right-hand operand from the left-hand operand and assigns the Use -= to decrement the value of a
x -= 2 3
result. variable.
The *= operator multiplies the left-hand operand by the right-hand operand and assigns the
x *= 4 12 Use *= to multiply and assign.
result.
The /= operator divides the left-hand operand by the right-hand operand and assigns the
x /= 2 6.0 Use /= to divide and assign.
result.
The %= operator calculates the modulus of the left-hand operand by the right-hand operand Use %= to find the remainder and
x %= 5 2
and assigns the result. assign it.
Use //= to divide and assign the
x //= 2 1 The //= operator performs floor division and assigns the result.
integer quotient.
The **= operator raises the left-hand operand to the power of the right-hand operand and
x **= 3 8 Use **= to exponentiate and assign.
assigns the result.
x &= 3 0 The &= operator applies the bitwise AND operation and assigns the result. Use &= for bitwise AND assignment.
`x = 3` 3 The `
x ^= 3 3 The ^= operator applies the bitwise XOR operation and assigns the result. Use ^= for bitwise XOR assignment.
x <<=
8 The <<= operator performs a bitwise left shift and assigns the result. Use <<= for left shift assignment.
2
x >>=
1 The >>= operator performs a bitwise right shift and assigns the result. Use >>= for right shift assignment.
2
Practice Questions
1. Write a Python program that initializes a variable x to 10 and then uses the += operator to add 5 to it.
2. Create a program that uses the -= operator to subtract 3 from a variable y initialized to 8.
3. Write a program that multiplies a variable z by 2 using the *= operator.
4. Implement a Python script that divides a number a = 15 by 3 using the /= operator.
5. Create a Python program that calculates the modulus of a number b = 17 by 4 using the %= operator.
6. Write a Python program that raises a number x = 2 to the power of 3 using the **= operator.
7. Use the //= operator to perform floor division of a number x = 7 by 2.
8. Write a Python program that applies bitwise AND to a number x = 7 and assigns the result using the &= operator.
9. Create a Python program that applies a bitwise OR operation between x = 6 and 3 using the |= operator.
10. Implement a Python program that uses bitwise XOR (^=) to modify the value of x based on the value of 3.
11. Use the left shift assignment <<= to shift bits of a number x = 5 by 2.
12. Implement a Python program to shift bits of a number x = 8 to the right by 2 using the >>= operator.
Bitwise Operators
Operato Outpu
Example Description When to Use
r t
x = 5; y = 3; z = Performs a bitwise AND operation on the bits Use & when you need to perform bitwise AND between two
& (AND) 1
x&y of two integers. numbers.
` ` (OR) `x = 5; y = 3; z = x y` Performs a bitwise OR operation on the bits of two integers.
x = 5; y = 3; z = Performs a bitwise XOR operation on the bits Use ^ when you need to perform bitwise XOR between two
^ (XOR) 6
x^y of two integers. numbers.
Performs a bitwise NOT operation on an
~ (NOT) x = 5; z = ~x -6 Use ~ when you need to invert the bits of a number.
integer, inverts all the bits.
<< (Left x = 5; z = x << Shifts the bits of the number to the left by Use << to shift the bits of a number to the left, effectively
20
Shift) 2 the specified number of positions. multiplying by powers of two.
>>
x = 5; z = x >> Shifts the bits of the number to the right by Use >> to shift the bits of a number to the right, effectively
(Right 2
1 the specified number of positions. dividing by powers of two.
Shift)
Practice Questions
Membership Operators
Operato Outpu
Example Description When to Use
r t
'apple' in
Returns True if a value is found in
['apple', Use in when you need to check if an element is present in a sequence like a list,
in a sequence (e.g., list, string, True
'banana', string, or tuple.
tuple, set).
'cherry']
'grape' not in
['apple', Returns True if a value is not Use not in when you need to check if an element is NOT present in a sequence
not in True
'banana', found in a sequence. like a list, string, etc.
'cherry']
Practice Questions
1. Check if the element 'apple' exists in the list ['apple', 'banana', 'cherry'] using the in operator.
2. Use the not in operator to check if 'grape' is NOT in the list ['apple', 'banana', 'cherry'].
3. Check if the letter 'a' is present in the string 'apple' using the in operator.
4. Write a program to check if the number 3 is not in the tuple (1, 2, 4, 5) using the not in operator.
5. Check if 'cat' is present in the set {'dog', 'cat', 'mouse'} using the in operator.
6. Use the in operator to verify if the string 'b' is in the list ['a', 'b', 'c', 'd'].
7. Write a Python program that checks if the number 7 is present in the range of numbers from 1 to 10 using in.
8. Check if the word 'hello' is in the sentence 'hello world' using the in operator.
9. Use the not in operator to check if 'orange' is NOT in the list ['apple', 'banana', 'cherry'].
10. Write a program to check if the letter 'z' is not in the string 'alphabet' using the not in operator.
Identity Operators
Operat Outpu
Example Description When to Use
or t
x = [1, 2, 3]; y
Returns True if both variables refer to the Use is when you want to check if two variables point to the exact
is = [1, 2, 3]; x is False
same object in memory. same object in memory.
y
x = [1, 2, 3]; y
Returns True if both variables do not refer to Use is not when you want to check if two variables point to different
is not = [1, 2, 3]; x is True
the same object in memory. objects in memory.
not y
Practice Questions
1. Check if two lists x = [1, 2, 3] and y = [1, 2, 3] are the same object using the is operator.
2. Check if the integer 5 and 5 refer to the same object using the is operator.
3. Use the is not operator to check if x = [1, 2, 3] and y = [4, 5, 6] refer to different objects in memory.
4. Write a Python program to compare two variables x = "hello" and y = "hello" using the is operator.
5. Check if two tuples (1, 2) and (1, 2) are the same object using the is not operator.
6. Verify if the string 'apple' and 'apple' refer to the same object in memory using the is operator.
7. Check if two dictionaries x = {'a': 1} and y = {'a': 1} are different objects in memory using the is not operator.
8. Use the is operator to compare two sets x = {1, 2, 3} and y = {1, 2, 3} and see if they refer to the same object.
9. Check if the variables x = None and y = None point to the same object in memory using is.
10. Use the is not operator to check if two integers x = 10 and y = 20 are not referring to the same object in memory.
Control Flow
If-Else Statements
Keywor
Example Description Output When to Use
d
if x > 10:
The if statement evaluates the condition, and if it Use if to test conditions and execute a block of code
if print("Greater Greater than 10
is True, executes the indented code. when the condition is True.
than 10")
else:
The else statement provides an alternative block Not greater than Use else to handle the case when the if condition is
else print("Not greater
of code that runs if the if condition is False. 10 not True.
than 10")
elif x == 10:
The elif (else-if) statement allows testing multiple Use elif when you want to check for multiple
elif print("Equal to Equal to 10
conditions in sequence. conditions, and only the first True one executes.
10")
Practice Questions
Nested If
Keywo
Example Description Output When to Use
rd
if x > 10:
A nested if statement means having an if Use nested if statements when you need to
if x < 20: x is between 10 and
if statement inside another if statement. It check multiple conditions that depend on each
print("x is between 20
checks for multiple conditions. other.
10 and 20")
else: The else statement can also be used with
x is greater than or Use else to handle the cases where the inner
else print("x is greater nested if to define an alternative action when
equal to 20 condition fails.
than or equal to 20") the condition is false.
Practice Questions
1. Write a program that checks if a number is positive, and if it is, checks if it's even or odd using nested if statements.
2. Create a program that checks if a number is between 10 and 100, and then checks if it's divisible by 5 using nested if.
3. Write a program that checks if a number is between 50 and 100. If it is, then check if the number is a multiple of 10.
4. Using nested if, write a program that checks if a student’s marks are above 50, and if so, checks if they passed with distinction (above 75).
5. Write a program that checks if a user is an adult (age above 18), and then checks if they have a driving license using nested if.
6. Write a program that checks if the input year is a leap year. If it is, check if it is divisible by 400 using nested if.
7. Create a program that checks if a person is eligible for a senior citizen discount (age above 60) and if they are a member of the loyalty program.
8. Check if a given number is positive and if it's a multiple of both 2 and 3 using nested if statements.
9. Write a program that checks if a person can go for a hike (age greater than 18) and if the weather is good (temperature above 20°C).
10. Create a program that checks if a number is divisible by 5 and if the number is greater than 50 using a nested if statement.
Elif Statement
Keywor
Example Description Output When to Use
d
if x > 20:
print("x is
greater than The elif statement is short for "else if" and is used to Use elif when you need to check multiple, mutually
x is equal to
elif 20") check multiple conditions after an if statement. If the if exclusive conditions, and want to handle them
20
elif x == 20: condition is false, elif checks the next condition. separately.
print("x is
equal to 20")
else:
The else statement is used when none of the previous if x is less than Use else to handle the case when none of the if or
else print("x is less
or elif conditions are met. 20 elif conditions are True.
than 20")
Practice Questions
1. Write a program that checks the age of a person and prints a message based on age groups:
a. Under 18: "You are a minor."
b. 18 to 35: "You are a young adult."
c. 36 to 60: "You are an adult."
d. Above 60: "You are a senior citizen."
2. Write a program that checks if a number is:
a. Greater than 50: Print "High"
b. Between 20 and 50: Print "Medium"
c. Less than 20: Print "Low"
3. Check the time of day and print:
a. "Good Morning" if the time is between 5 AM and 12 PM.
b. "Good Afternoon" if the time is between 12 PM and 5 PM.
c. "Good Evening" if the time is between 5 PM and 9 PM.
d. "Good Night" for any other time.
4. Create a program to check the day of the week. If it's a weekend (Saturday/Sunday), print "It's weekend", otherwise print "It's a weekday."
5. Write a program that checks if a student’s score falls under one of the following categories:
a. 90 to 100: "Excellent"
b. 75 to 89: "Good"
c. 50 to 74: "Average"
d. Below 50: "Fail"
6. Write a program to check if a number is:
a. Positive and divisible by 5: "Multiple of 5"
b. Positive but not divisible by 5: "Not a multiple of 5"
c. Negative: "Negative number"
7. Write a program that checks whether a person is eligible for a driver's license:
a. If age is greater than or equal to 18 and the person has passed the driving test, print "Eligible for a license."
b. Otherwise, print "Not eligible."
8. Check if a given year is a leap year:
a. Divisible by 4 and not divisible by 100: "Leap Year"
b. Divisible by 400: "Leap Year"
c. Otherwise, "Not a Leap Year."
9. Write a program that prints:
a. "Winter" if the temperature is below 0°C.
b. "Spring" if the temperature is between 0°C and 20°C.
c. "Summer" if the temperature is above 20°C.
10. Write a program that checks if a number is divisible by:
a. 2: "Even"
b. 3: "Multiple of 3"
c. Neither: "Not a multiple of 2 or 3"
Loops
Loops are used to repeatedly execute a block of code as long as a specified condition is True. Python provides two types of loops:
1. for loop – Used to iterate over a sequence (like a list, tuple, string, or range).
2. while loop – Repeats as long as a given condition is True.
1. For Loop
2. While Loop
Practice Questions
Ke
yw Exampl Out
Description When to Use
or e put
d
for i in
range(1
0): The break statement causes the loop to exit 0, 1,
bre Use break when you need to exit a loop when a specific condition is met, avoiding
if i == immediately when the condition is satisfied. 2, 3,
ak further iterations.
5: It breaks out of the current loop. 4
break
print(i)
python
Copy code
for i in range(10):
if i == 5:
break # Exit the loop when i is 5
print(i)
Output:
Copy code
0
1
2
3
4
In this example, the loop runs from 0 to 9, but when i == 5, the break statement is executed, and the loop exits immediately without printing 5 or
continuing to higher values.
When to Use:
Search in a List: To stop the loop as soon as you find the desired item.
Efficient Execution: Avoid unnecessary iterations once the desired outcome is achieved.
Control Loop Flow: Break out of nested loops or large loops when a condition is met.
Practice Questions:
1. Write a program that uses a for loop to print numbers from 1 to 10 but stop if the number is 7.
2. Use a while loop to print numbers starting from 1, but exit the loop if the number is divisible by 5.
3. Write a program that asks the user for a password and breaks the loop if the password is correct.
4. Create a program that prints all numbers from 1 to 20 and stops when it encounters a prime number.
5. Use break to exit a loop when a certain word (e.g., "exit") is entered by the user.
Continue Statement
The continue statement is used in loops to skip the current iteration and move on to the next one. When the continue statement is encountered, the rest
of the code inside the loop for the current iteration is skipped, and the loop proceeds with the next iteration.
python
Copy code
for i in range(10):
if i == 5:
continue # Skip the iteration when i is 5
print(i)
Output:
Copy code
0
1
2
3
4
6
7
8
9
In this example, the continue statement causes the loop to skip the iteration where i == 5. As a result, the number 5 is not printed.
When to Use:
Skip Specific Values: If you want to skip certain values or conditions while processing the data in the loop.
Efficient Looping: Use continue when you want to avoid executing specific code for certain iterations but continue with the rest of the loop.
Condition-Based Skipping: When certain iterations are irrelevant or should be excluded based on a condition.
Practice Questions:
1. Write a program that prints numbers from 1 to 10, but skips the number 7.
2. Use a while loop to print numbers from 1 to 20, but skip the multiples of 3.
3. Create a program that prints all numbers from 1 to 20, but skips the numbers divisible by 5.
4. Write a program that uses continue to skip the even numbers from 1 to 20 and print only odd numbers.
5. Implement a program that checks for prime numbers in a given range and skips non-prime numbers.
6. Use continue to ignore negative numbers while processing a list of integers.
Pass Statement
The pass statement is a null operation in Python. It is used as a placeholder in loops, functions, classes, or conditionals where you have syntactically
required code but don't want to execute anything. It allows you to define a structure without implementation and prevents errors caused by empty code
blocks.
Keywor
Example Description Output When to Use
d
for i in The pass statement does
range(5): nothing but satisfies the
Use pass when you need a placeholder for an empty code block or
pass if i == 3: syntax requirements. It is 01234
temporarily leave an implementation unfinished.
pass a placeholder for future
print(i) code.
python
Copy code
for i in range(5):
if i == 3:
pass # Do nothing when i is 3
else:
print(i)
Output:
Copy code
0
1
2
4
In this example, the pass statement is used when i == 3. It does nothing and allows the loop to continue, so the number 3 is skipped in the output.
When to Use:
Empty Functions or Methods: When defining a function or method that you haven’t implemented yet but need to define to prevent errors.
Empty Loops or Conditionals: When you have an empty loop or conditional statement but still need a syntactically valid structure.
Class Definitions: To define a class with no content, where functionality will be added later.
Prototyping: While testing and prototyping code, use pass to temporarily avoid execution of a code block.
Practice Questions:
1. Write a program that uses the pass statement to skip a specific number while iterating through a range from 1 to 10.
2. Define a function that doesn’t do anything using the pass statement.
3. Use pass in a loop to skip over a specific word in a list of strings.
4. Create a program that checks if a number is prime but leaves the logic empty with pass for now.
5. Implement a placeholder function that will later handle exception handling with pass.
6. Write a program that defines a class with no methods using the pass statement.
Functions
Defining Functions
A function in Python is a block of reusable code that performs a specific task. It helps organize code into smaller, manageable, and logically grouped
pieces. Functions can take input parameters and return a value after performing an operation. They allow for modular and efficient code development.
Keywor
Example Description Output When to Use
d
The def keyword is used to
def Use def to define reusable blocks
define a function in Python. A Hello John
greet(name): of code that perform a task.
def function can accept (if name =
return "Hello Functions enhance code
parameters and return a "John")
" + name modularity.
value.
Example: Defining and Using Functions
Defining a Function:
python
Copy code
def greet(name):
return "Hello, " + name + "!"
python
Copy code
print(greet("John"))
Output:
Copy code
Hello, John!
In this example, we define a function greet that takes one parameter name and returns a greeting message. The function is then called with "John" as an
argument, and the result is printed.
When to Use:
Reusability: Functions allow you to avoid repetitive code by grouping tasks you want to perform multiple times.
Modularity: Functions break down complex problems into smaller, easier-to-understand tasks.
Organizing Code: Functions help structure code logically and make it easier to maintain.
Testing and Debugging: Functions can be tested and debugged independently, making code maintenance easier.
Practice Questions:
1. Define a function that takes two numbers and returns their sum.
2. Create a function that accepts a string and prints it in uppercase.
3. Define a function to find the factorial of a number.
4. Write a function that checks if a given number is even or odd.
5. Create a function that takes a list of numbers and returns the largest number.
6. Define a function that takes a string as input and returns the reverse of the string.
Function Arguments
In Python, function arguments are values that you pass to a function when calling it. These arguments are used to provide input data to the function so
that it can perform its task. Python supports various types of function arguments, which allow flexibility when defining functions.
1. Positional Arguments:
python
Copy code
def add(a, b):
return a + b
print(add(3, 5))
Output:
Copy code
8
In this example, the arguments 3 and 5 are passed in the same order as the parameters a and b.
2. Keyword Arguments:
python
Copy code
def greet(name, message):
return message + ", " + name
print(greet(name="John", message="Hello"))
Output:
Copy code
Hello, John
In this example, we use keyword arguments, so the order of parameters doesn't matter.
3. Default Arguments:
python
Copy code
def greet(name, message="Hello"):
return message + ", " + name
print(greet("John"))
Output:
Copy code
Hello, John
Here, message has a default value of "Hello", so it doesn't need to be provided when calling the function.
python
Copy code
def sum_all(*args):
return sum(args)
print(sum_all(1, 2, 3, 4, 5))
Output:
Copy code
15
In this example, we pass multiple arguments to the function sum_all() using *args.
python
Copy code
def greet(**kwargs):
return kwargs
print(greet(name="John", age=30))
Output:
arduino
Copy code
{'name': 'John', 'age': 30}
In this example, we pass multiple keyword arguments to the function using **kwargs.
When to Use:
Positional Arguments: Use when the order of arguments is important and there are a fixed number of parameters.
Keyword Arguments: Use when the order of arguments is not important, and you want to specify arguments explicitly.
Default Arguments: Use when some parameters have common values, and you want to provide a default value for them.
Variable-length Arguments: Use when you don't know how many arguments will be passed to the function (e.g., summing numbers).
Keyword Variable-length Arguments: Use when you want to accept an arbitrary number of keyword arguments (e.g., when handling different
configurations or settings).
Practice Questions:
1. Define a function that accepts two numbers and returns their product. Use positional arguments.
2. Create a function that prints a greeting message with a default message if no message is provided.
3. Write a function that accepts any number of integer arguments and returns their average.
4. Define a function that takes keyword arguments and prints them in a formatted string.
5. Write a function that accepts both positional and keyword arguments and returns a formatted sentence.
6. Create a function using variable-length arguments (*args) to calculate the sum of multiple numbers.
7. Define a function that takes both a default and a non-default argument and demonstrates how to call it in various ways.
Example:
python
Copy code
def sum_all(*args):
return sum(args)
In the function sum_all(), *args accepts any number of positional arguments, and the sum() function calculates their sum.
Example:
python
Copy code
def greet(**kwargs):
return kwargs
In the function greet(), **kwargs captures the keyword arguments as a dictionary, which can be easily accessed and manipulated.
python
Copy code
def display_info(name, *args, **kwargs):
print(f"Name: {name}")
print(f"Additional Info: {args}")
print(f"Keyword Info: {kwargs}")
Output:
css
Copy code
Name: John
Additional Info: (25, 'Engineer')
Keyword Info: {'city': 'New York', 'country': 'USA'}
When to Use:
*args: Use when you don't know how many positional arguments will be passed to the function.
**kwargs: Use when you don't know how many keyword arguments will be passed to the function.
Both *args and **kwargs: Use when your function needs to handle both variable-length positional and keyword arguments.
Practical Examples:
python
Copy code
def sum_all(*args):
return sum(args)
python
Copy code
def display_user_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
python
Copy code
def profile(name, *args, **kwargs):
print(f"Name: {name}")
print("Hobbies:", ", ".join(args))
print("Details:", kwargs)
Summary:
Use *args when you need to accept a variable number of positional arguments.
Use **kwargs when you need to accept a variable number of keyword arguments.
You can combine both in one function, but remember the order: *args first, followed by **kwargs.
Return Statement
The return statement in Python is used to exit a function and optionally pass a value back to the caller. When a function is called, it performs its
operations, and the return statement can be used to send back the result of those operations to the caller.
Key Points:
Exits the function: When the return statement is executed, the function immediately stops executing, and no further code in the function is run.
Optional value: The return statement can return a value (like a number, string, list, etc.), or it can simply be used without returning anything
(implicitly returns None).
Syntax:
python
Copy code
return [expression]
Examples:
python
Copy code
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
In this example, the add function calculates the sum of a and b and returns the result. The value 8 is returned to the caller and assigned to result.
2. Returning Multiple Values:
You can return multiple values from a function. Python will return these values as a tuple by default.
python
Copy code
def multiply_and_divide(a, b):
return a * b, a / b
Here, the function returns both the product and the quotient of a and b, and they are unpacked into the variables product and quotient.
If there is no return statement, or the return statement does not specify a value, the function will return None by default.
python
Copy code
def print_message(message):
print(message)
In this case, the print_message() function prints the message but doesn't return anything, so the result is None.
4. Return with Condition:
The return statement is often used with if conditions to return values based on specific conditions.
python
Copy code
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
The function returns either "Even" or "Odd" based on the input number.
You can use the return statement to exit a function early, before reaching the end of the function's code.
python
Copy code
def check_positive(number):
if number <= 0:
return "Number is not positive"
return "Number is positive"
The function exits as soon as the condition number <= 0 is true, returning a message without executing the rest of the code.
To return a value: If your function performs some calculation or operation, use return to send the result back to the caller.
To stop execution early: Use return to exit the function when a condition is met or when you no longer need to execute further code.
Returning multiple results: You can return multiple values, typically as a tuple, if your function produces more than one output.
Summary:
Basic Concepts
Type Casting
Type casting is the process of converting one data type into another. In Python, this is often required when you need to perform operations that involve
different data types. Python provides built-in functions to cast variables from one type to another.
1. Implicit Type Casting (Coercion): This happens automatically when Python converts one type to another without explicit instruction.
2. Explicit Type Casting (Type Conversion): This happens when you manually convert one type to another using the built-in functions.
Implicit Type Casting
Implicit type casting is performed by Python automatically when there is no risk of data loss. For example, if you perform an operation with an integer and
a floating-point number, Python automatically converts the integer to a float to ensure no loss of data.
python
Copy code
a=5 # Integer
b = 2.5 # Float
In this example, a is an integer, and b is a float. Python automatically converts a to a float before performing the addition to avoid losing precision.
Explicit type casting is done by using Python's built-in functions to convert from one data type to another. These functions are:
Example:
python
Copy code
a = 10.75
b = int(a) # Converts float to integer
print(b) # Output: 10
Example:
python
Copy code
a = 10
b = float(a) # Converts integer to float
print(b) # Output: 10.0
python
Copy code
a = 100
b = str(a) # Converts integer to string
print(b) # Output: '100'
Example:
python
Copy code
a=0
b = bool(a) # Converts integer 0 to boolean
print(b) # Output: False
python
Copy code
a = (1, 2, 3) # Tuple
b = list(a) # Converts tuple to list
print(b) # Output: [1, 2, 3]
python
Copy code
a = [1, 2, 2, 3] # List
b = set(a) # Converts list to set (removes duplicates)
print(b) # Output: {1, 2, 3}
When you take user input using the input() function, the returned value is always a string. To perform operations, you often need to cast the input into the
required type.
python
Copy code
age = input("Enter your age: ") # User input is a string
age = int(age) # Convert to integer
print(age + 5) # Output will be age + 5 as an integer
Here, age is cast from a string to an integer so that arithmetic operations can be performed.
Type Casting with Invalid Values
If the value cannot be converted to the target type, Python will raise a ValueError.
python
Copy code
a = "hello"
b = int(a) # This will raise a ValueError
In this case, the string "hello" cannot be converted to an integer, so Python raises an error.
Implicit Casting: Automatically done by Python when no data loss will occur.
Explicit Casting: Done manually using functions like int(), float(), str(), etc.
Use int() to convert to an integer, float() to convert to a float, and str() to convert to a string.
Type conversion is commonly needed when dealing with user input or performing arithmetic operations with different data types.
String Formatting
String formatting in Python allows you to insert values into a string dynamically. This is useful for creating messages or reports where certain values need
to be embedded in predefined text. Python offers several ways to format strings, each with different use cases.
The % operator is the older way to format strings in Python. You use placeholders, represented by %, in the string where you want to insert the value,
followed by the corresponding value to be inserted.
Syntax:
python
Copy code
"string % (values)"
Example:
python
Copy code
name = "Alice"
age = 30
formatted_string = "Name: %s, Age: %d" % (name, age)
print(formatted_string)
Output:
yaml
Copy code
Name: Alice, Age: 30
%s: String
%d: Integer
%f: Floating-point number
%x: Hexadecimal representation
The str.format() method provides more flexibility and is considered a more modern way to format strings. You can use curly braces {} as placeholders
within the string and call .format() to pass values into those placeholders.
Syntax:
python
Copy code
"string {}".format(values)
Example:
python
Copy code
name = "Alice"
age = 30
formatted_string = "Name: {}, Age: {}".format(name, age)
print(formatted_string)
Output:
yaml
Copy code
Name: Alice, Age: 30
You can also use positional and keyword arguments to make the formatting more flexible:
python
Copy code
formatted_string = "Name: {0}, Age: {1}".format(name, age)
print(formatted_string)
Output:
yaml
Copy code
Name: Alice, Age: 30
Name: Alice, Age: 30
3. Using f-strings (Literal String Interpolation) - Python 3.6+
f-strings (formatted string literals) are the most modern and concise way to format strings in Python. You can embed expressions inside string literals
using curly braces {} and prefix the string with f.
Syntax:
python
Copy code
f"string {expression}"
Example:
python
Copy code
name = "Alice"
age = 30
formatted_string = f"Name: {name}, Age: {age}"
print(formatted_string)
Output:
yaml
Copy code
Name: Alice, Age: 30
f-strings allow you to embed any valid Python expression inside the curly braces, including mathematical operations:
python
Copy code
a = 10
b=5
formatted_string = f"The sum of {a} and {b} is {a + b}"
print(formatted_string)
Output:
python
Copy code
The sum of 10 and 5 is 15
The string.Template class from the string module provides a way to substitute values into a string using placeholders marked by $. This method is
particularly useful when you want to use strings as templates that may be filled with user input or external data.
Syntax:
python
Copy code
from string import Template
template = Template("string $variable")
Example:
python
Copy code
from string import Template
name = "Alice"
age = 30
template = Template("Name: $name, Age: $age")
formatted_string = template.substitute(name=name, age=age)
print(formatted_string)
Output:
yaml
Copy code
Name: Alice, Age: 30
Template is a simpler and more restrictive way of formatting, as it only supports basic variable substitution.
% Operator: Use when maintaining old code or when working with basic formatting needs.
str.format(): Useful for when you need more control over formatting with named or positional parameters.
f-strings: The preferred and most efficient way for string formatting in modern Python (Python 3.6+).
string.Template: Ideal for cases where you need simple substitution and template-based strings.
Practice Questions:
1. Write a program that takes the user's name and age and formats a string like "Hello, my name is [name] and I am [age] years old."
2. Create a program that calculates the area of a circle (use the formula: π * r^2) and formats it into a string like "The area of the circle with radius [r]
is [area]."
3. Use f-strings to format a string that shows the result of a division between two numbers with 2 decimal places.
4. Write a function that accepts a name and a number, and return a formatted string with "Hello, [name]. You have [number] messages."
5. Format a string using str.format() to show the multiplication table for a given number.
String formatting is an essential feature for creating clean, readable, and dynamic Python code. The method you choose depends on your version of
Python, the level of flexibility required, and personal preference.
List Comprehensions
List comprehensions provide a concise and efficient way to create lists in Python. They allow you to create new lists by applying an expression to each
item in an existing iterable (such as a list, tuple, or string). List comprehensions are often used to make code more readable and compact, and they can
improve performance compared to using traditional loops.
Basic Syntax
python
Copy code
[expression for item in iterable]
python
Copy code
squares = [x**2 for x in range(10)]
print(squares)
Output:
csharp
Copy code
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Here, the list comprehension iterates through each number from 0 to 9 (x), calculates its square (x**2), and stores the result in the list squares.
Example 2: Using Conditional Statements
You can also add conditions to your list comprehensions to filter elements based on some criteria.
python
Copy code
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares)
Output:
csharp
Copy code
[0, 4, 16, 36, 64]
In this example, only even numbers (those that satisfy x % 2 == 0) are included in the list, and then their squares are calculated.
List comprehensions can also be nested. This is useful when dealing with multi-dimensional lists (e.g., matrices).
python
Copy code
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [element for row in matrix for element in row]
print(flattened)
Output:
csharp
Copy code
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Here, we iterate over each row in the matrix and then over each element in that row to flatten the list.
python
Copy code
words = ["hello", "world", "python"]
uppercase_words = [word.upper() for word in words]
print(uppercase_words)
Output:
css
Copy code
['HELLO', 'WORLD', 'PYTHON']
In this example, the upper() method is applied to each string in the list to convert it to uppercase.
You can use multiple conditions inside a list comprehension to filter elements more selectively.
python
Copy code
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15]
divisible_by_2_and_3 = [x for x in numbers if x % 2 == 0 and x % 3 == 0]
print(divisible_by_2_and_3)
Output:
csharp
Copy code
[6, 12]
Here, the list comprehension filters out numbers that are divisible by both 2 and 3.
Example 6: List Comprehension with a Tuple or Set
List comprehensions are not limited to lists; they can also be used to create other types of collections like sets or tuples.
python
Copy code
squares_set = {x**2 for x in range(10)}
print(squares_set)
Output:
Copy code
{0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
In this example, a set is created instead of a list, ensuring that duplicate values are not included.
You can use list comprehensions in combination with dictionary comprehensions to create dictionaries.
python
Copy code
squares_dict = {x: x**2 for x in range(5)}
print(squares_dict)
Output:
yaml
Copy code
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
In this example, a dictionary is created with numbers as keys and their squares as values.
Performance Considerations
List comprehensions are often faster than using regular for loops to create lists, as they are optimized for this purpose in Python. However, for very large
datasets or complex operations, you might want to consider performance trade-offs between list comprehensions and other methods like map() or
generator expressions (which are more memory efficient).
1. Simple transformations: When you need to apply a simple operation to every item in an iterable.
2. Filtering: When you need to include only certain items from an iterable based on a condition.
3. Combining iterables: When you want to work with nested iterables or perform operations that involve more than one iterable.
4. Creating dictionaries and sets: For building dictionaries and sets from iterables using comprehensions.
Practice Questions:
1. Use list comprehension to create a list of squares of even numbers from 0 to 20.
2. Write a list comprehension to extract all vowels from a string and store them in a list.
3. Given a list of numbers, use list comprehension to create a new list that contains only the prime numbers.
4. Create a list of tuples where each tuple contains a number and its corresponding cube.
5. Use list comprehension to create a list of words that are longer than 4 characters from a given list of strings.
Conclusion
List comprehensions are a powerful and concise tool in Python. They can simplify your code and improve performance for creating and manipulating lists.
Understanding how and when to use list comprehensions will make your code more Pythonic and efficient.
Dictionary Comprehensions
Dictionary comprehensions in Python provide a concise and efficient way to create dictionaries. Like list comprehensions, dictionary comprehensions allow
you to construct a dictionary by applying an expression to each item in an iterable, optionally applying conditions to filter elements.
Basic Syntax
The syntax of a dictionary comprehension is similar to list comprehensions, but with key-value pairs:
python
Copy code
{key_expression: value_expression for item in iterable}
Create a dictionary where the keys are numbers and the values are their squares:
python
Copy code
squares = {x: x**2 for x in range(5)}
print(squares)
Output:
yaml
Copy code
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Here, the comprehension creates a dictionary with numbers as keys and their squares as values.
You can add conditions to the dictionary comprehension to filter the elements being added.
python
Copy code
even_squares = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares)
Output:
yaml
Copy code
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
In this case, the dictionary comprehension includes only even numbers as keys and their squares as values.
You can loop over more than one iterable in a dictionary comprehension. However, note that when using multiple iterables, you will need to pair them
together in some way.
python
Copy code
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = {keys[i]: values[i] for i in range(len(keys))}
print(dictionary)
Output:
css
Copy code
{'a': 1, 'b': 2, 'c': 3}
In this example, the dictionary comprehension combines two lists, keys and values, into a dictionary by using their indices.
You can create dictionaries where the values are tuples or more complex data structures.
Example: Create a dictionary where the key is a number and the value is a tuple (number, its square)
python
Copy code
numbers = [1, 2, 3, 4]
number_dict = {x: (x, x**2) for x in numbers}
print(number_dict)
Output:
yaml
Copy code
{1: (1, 1), 2: (2, 4), 3: (3, 9), 4: (4, 16)}
In this example, each key is paired with a tuple containing the number and its square.
Example 5: Dictionary Comprehension with Expressions for Both Key and Value
Both the key and the value can be expressions derived from the iterable elements.
Example: Create a dictionary with string lengths as keys and the corresponding strings as values
python
Copy code
words = ['apple', 'banana', 'cherry']
word_length_dict = {len(word): word for word in words}
print(word_length_dict)
Output:
css
Copy code
{5: 'apple', 6: 'banana', 6: 'cherry'}
Here, the dictionary comprehension creates a dictionary where the key is the length of each string, and the value is the string itself.
Example: Create a dictionary of numbers and their squares for even numbers only
python
Copy code
even_numbers = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_numbers)
Output:
yaml
Copy code
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
This is similar to previous examples but allows you to filter out odd numbers while creating key-value pairs with numbers and their squares.
You can use dictionary comprehension to invert the keys and values in a dictionary.
python
Copy code
original_dict = {'a': 1, 'b': 2, 'c': 3}
swapped_dict = {value: key for key, value in original_dict.items()}
print(swapped_dict)
Output:
css
Copy code
{1: 'a', 2: 'b', 3: 'c'}
This example swaps the keys and values of original_dict, creating a new dictionary where the original values become keys.
Example 8: Dictionary Comprehension with Default Values
You can use dict.get() or a ternary operator to provide default values for keys that may not exist.
python
Copy code
keys = ['a', 'b', 'c', 'd']
default_value = 0
dict_with_defaults = {key: (default_value if key not in ['a', 'b'] else 1) for key in keys}
print(dict_with_defaults)
Output:
css
Copy code
{'a': 1, 'b': 1, 'c': 0, 'd': 0}
This example assigns a default value of 0 for all keys except 'a' and 'b', which have a value of 1.
Performance Considerations
Dictionary comprehensions are generally faster than traditional loops when creating dictionaries because they are optimized in Python. However, just like
list comprehensions, you should avoid using them for very complex expressions that might reduce readability.
When to Use Dictionary Comprehensions:
1. When creating a dictionary with key-value pairs: Especially when the keys and values are based on operations or conditions.
2. To transform or filter data: You can apply conditions or transformations to both keys and values to create dictionaries that meet specific
requirements.
3. When working with multiple iterables: If you need to pair data from multiple lists or tuples, dictionary comprehensions provide a quick and
elegant solution.
Practice Questions:
1. Use dictionary comprehension to create a dictionary where the key is a number and the value is its cube.
2. Create a dictionary where each key is a word and the value is the length of that word, but only for words that are longer than 3 characters.
3. Given a list of tuples, use dictionary comprehension to create a dictionary where the first element of each tuple is the key and the second element is
the value.
4. Create a dictionary of the squares of numbers, but exclude numbers that are divisible by 5.
5. Create a dictionary from a list of strings, where the key is the string and the value is its first letter.
Conclusion
Dictionary comprehensions are a powerful feature in Python, allowing you to quickly create and manipulate dictionaries in a clean and efficient way. By
applying expressions and conditions, you can transform data, filter values, and create dictionaries with minimal code. Understanding how and when to use
dictionary comprehensions will help you write Pythonic and efficient code.
python
Copy code
try:
# Code that might raise an exception
except ExceptionType:
# Code that runs if the exception is raised
else:
# Code that runs if no exception occurs
finally:
# Code that always runs, regardless of whether an exception occurred
try: Contains the block of code that may potentially raise an exception.
except: Handles the exception if one is raised in the try block.
else: (Optional) Runs if no exceptions are raised in the try block.
finally: (Optional) Runs no matter what, after the try block (even if an exception is raised or not).
Let's say you want to handle a situation where dividing by zero might occur:
python
Copy code
try:
a = 10
b=0
result = a / b
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
Output:
vbnet
Copy code
Error: Division by zero is not allowed.
In this example, the code inside the try block attempts to divide 10 by 0. Since division by zero raises a ZeroDivisionError, the code inside the except block
is executed.
You can handle multiple types of exceptions in a single try-except block by using multiple except clauses.
python
Copy code
try:
x = int(input("Enter a number: "))
result = 10 / x
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Invalid input. Please enter an integer.")
Output:
less
Copy code
Enter a number: abc
Error: Invalid input. Please enter an integer.
Here, if the user enters a non-integer value, a ValueError will be raised, and the appropriate message will be printed. If the user enters 0, a
ZeroDivisionError will be raised.
You can also catch all exceptions (generic exceptions) using the base Exception class. While this is not recommended for specific error handling, it can be
useful for logging or debugging.
python
Copy code
try:
a = 10
b = "hello"
result = a + b
except Exception as e:
print(f"An error occurred: {e}")
Output:
go
Copy code
An error occurred: unsupported operand type(s) for +: 'int' and 'str'
In this example, a TypeError occurs because Python cannot add an integer and a string. The Exception class catches any kind of exception, and the error
message is printed.
The else block is executed only if no exception is raised in the try block. It can be used for code that should run when everything goes as expected.
python
Copy code
try:
x = int(input("Enter a number: "))
result = 10 / x
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except ValueError:
print("Error: Invalid input. Please enter an integer.")
else:
print(f"The result is {result}")
Output:
csharp
Copy code
Enter a number: 2
The result is 5.0
If the user enters a valid number that is not zero, the division proceeds, and the result is printed from the else block.
Example 5: Using finally Block
The finally block is always executed, regardless of whether an exception occurred or not. It is often used for cleanup actions, such as closing files or
releasing resources.
python
Copy code
try:
f = open("file.txt", "r")
data = f.read()
except FileNotFoundError:
print("Error: The file was not found.")
else:
print("File read successfully.")
finally:
f.close()
print("File has been closed.")
Output:
arduino
Copy code
Error: The file was not found.
File has been closed.
In this case, even though the file could not be found and an exception was raised, the finally block ensures that the file (if it was opened) is closed.
Example 6: Nested Exception Handling
You can have try-except blocks inside each other to handle multiple levels of errors.
python
Copy code
try:
x = int(input("Enter a number: "))
try:
result = 10 / x
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
else:
print(f"The result is {result}")
except ValueError:
print("Error: Invalid input. Please enter an integer.")
Output:
csharp
Copy code
Enter a number: 2
The result is 5.0
Here, the outer try-except handles a ValueError, while the inner try-except handles division by zero.
Example 7: Custom Exceptions
You can define your own custom exceptions by creating a new class that inherits from the Exception class.
python
Copy code
class CustomError(Exception):
pass
try:
raise CustomError("Something went wrong")
except CustomError as e:
print(f"Custom error: {e}")
Output:
vbnet
Copy code
Custom error: Something went wrong
In this case, a CustomError is raised and caught, and the corresponding message is printed.
1. Be specific with exceptions: Catch only the exceptions you expect to handle. Avoid using a broad except block unless you're logging or
debugging.
2. Use else and finally appropriately: Use else for code that runs only when no exception occurs. Use finally for cleanup tasks that should always
be executed, such as closing files or releasing resources.
3. Don't overuse exceptions: Exceptions should be used for exceptional conditions, not for regular flow control. For normal situations, use
conditionals (e.g., if-else).
4. Log errors: Instead of just printing errors, consider logging them for debugging and future reference.
Practice Questions:
1. Write a program that asks the user for a number and calculates its square root. Handle ValueError if the user inputs something other than a number
and NegativeNumberError if the number is negative.
2. Modify the previous example to include a finally block that prints a message regardless of the exception raised.
3. Create a custom exception class NegativeNumberError to raise an exception if a negative number is entered in the square root program.
4. Write a program that handles both IndexError and KeyError in a dictionary lookup operation.
Conclusion
Exception handling with try, except, else, and finally blocks is a powerful feature of Python that allows you to write more reliable, fault-tolerant programs.
Properly handling exceptions helps prevent your program from crashing and allows you to provide meaningful error messages or perform cleanup tasks.
Using these constructs appropriately will make your code more robust and maintainable.
The standard library covers many areas of programming such as file I/O, networking, system operations, text processing, and more. It saves time and
effort by offering pre-built modules that handle common tasks, so you don't have to reinvent the wheel.
Key Modules in the Python Standard Library
Here are some essential and commonly used modules from the Python Standard Library:
Example:
python
Copy code
import os
# Create a directory
os.mkdir('new_folder')
Provides access to variables and functions that interact with the Python runtime environment.
Common functions: command-line arguments, exiting programs, and interacting with the interpreter.
Example:
python
Copy code
import sys
Provides mathematical functions such as trigonometric, logarithmic, and constants like pi and e.
Example:
python
Copy code
import math
Provides functions to generate random numbers, random selections, and shuffling items.
Example:
python
Copy code
import random
# Shuffle a list
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)
python
Copy code
import datetime
Provides functions for working with time, including delays and performance timing.
Example:
python
Copy code
import time
# Delay execution for 2 seconds
time.sleep(2)
Used to parse JSON strings, convert Python objects to JSON, and vice versa.
Example:
python
Copy code
import json
Provides support for regular expressions, useful for searching and manipulating strings based on patterns.
Example:
python
Copy code
import re
Contains specialized container types like namedtuple, deque, Counter, and defaultdict.
Example:
python
Copy code
import collections
Provides functions to create iterators that work with loops and sequences efficiently.
Example:
python
Copy code
import itertools
Example:
python
Copy code
import socket
# Connect to a server
s.connect(('www.python.org', 80))
Used for reading and writing CSV (Comma Separated Values) files.
Example:
python
Copy code
import csv
Provides functions to work with URLs, such as fetching web content, parsing URLs, etc.
Example:
python
Copy code
import urllib.request
# Fetch a webpage
response = urllib.request.urlopen('https://www.python.org')
html = response.read()
print(html[:100]) # Print the first 100 characters of the page
Example:
python
Copy code
import email.mime.text
print(msg.as_string())
Conclusion
The Python Standard Library is an essential part of Python that provides a wide array of built-in modules to handle common tasks, making it easier for
developers to write efficient and readable code without needing third-party libraries. Whether you're working with files, dates, networking, or data
structures, the standard library offers well-tested solutions.
By familiarizing yourself with these modules, you can save time and effort when coding and focus more on solving your specific problems.
Practice Suggestions:
Task 1: Use the os and shutil modules to automate the process of organizing files in a folder based on their extension.
Task 2: Create a program that fetches weather data from an API using the requests module (not part of the standard library but commonly used
alongside it).
Task 3: Write a function that reads a CSV file and calculates the average of a specific numeric column.
Data Structures
Nested Lists
A nested list in Python refers to a list that contains other lists as its elements. This allows you to create complex, multidimensional structures like
matrices, grids, or even hierarchical data, which can be accessed and manipulated efficiently.
Nested lists are useful when you need to store and work with more than one dimension of data. For example, you might use a nested list to represent a
grid of cells in a game or a table of data.
A nested list can be created by simply including lists inside another list. Here is a basic example:
python
Copy code
# A nested list with 2 inner lists
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list)
In this example, nested_list contains three inner lists, and each inner list has three elements.
To access elements inside a nested list, you need to specify both the index of the outer list and the index of the inner list.
The first index accesses the outer list (the list itself).
The second index accesses the element within the inner list.
python
Copy code
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can iterate over a nested list using loops. The outer loop will go through the main list, and the inner loop will iterate through the individual elements of
each sublist.
python
Copy code
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output:
Copy code
123
456
789
Modifying Elements in a Nested List
You can modify the elements of a nested list by accessing them via their indexes and then assigning a new value.
python
Copy code
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can add or remove elements to/from nested lists in the same way you would do with a regular list.
python
Copy code
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Adding a new sublist
nested_list.append([10, 11, 12])
print(nested_list) # Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# Removing a sublist
nested_list.remove([4, 5, 6])
print(nested_list) # Output: [[1, 2, 3], [7, 8, 9], [10, 11, 12]]
A common use case for nested lists is representing a matrix (2D array), where each sublist represents a row of the matrix. You can perform various
matrix operations like addition, multiplication, or transposition using nested lists.
python
Copy code
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(transpose)
Output:
lua
Copy code
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
You can use nested lists to represent multidimensional data structures. For example, a 3D list (a list of lists of lists) can represent 3D grids or volumes.
python
Copy code
# A 3D nested list representing a cube of size 2x2x2
cube = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
# Access the first element of the first inner list in the first sublist
print(cube[0][0][0]) # Output: 1
Conclusion
Nested lists in Python allow for the storage and manipulation of complex, multidimensional data. They are an essential feature for many real-world
applications, such as handling matrices, grids, or hierarchical data. You can access, modify, and iterate over nested lists, making them highly versatile for
various tasks.
Practice Suggestions:
Task 1: Create a nested list that represents a seating arrangement in a movie theater. Access and modify individual seats.
Task 2: Write a function that transposes a 3x3 matrix using nested lists.
Task 3: Create a 3D list and perform operations like adding or updating elements at specific positions in the grid.
Nested Dictionaries
A nested dictionary in Python is a dictionary where the values are themselves dictionaries. This allows you to create complex hierarchical data
structures, such as storing attributes for multiple entities or representing a tree-like structure.
A nested dictionary can be useful for tasks like storing information about various users, products, or more complex data, where each entry might have
sub-properties or sub-objects.
A nested dictionary can be created by simply placing one dictionary inside another. Here's an example:
nested_dict = {
'person1': {
'name': 'John',
'age': 30,
'city': 'New York'
},
'person2': {
'name': 'Alice',
'age': 25,
'city': 'Los Angeles'
},
'person3': {
'name': 'Bob',
'age': 35,
'city': 'Chicago'
}
}
print(nested_dict)
In this example, the outer dictionary has three keys (person1, person2, and person3), and each of these keys maps to another dictionary that contains the
name, age, and city for each person.
To access elements in a nested dictionary, you need to use multiple keys: one for the outer dictionary and another for the inner dictionary.
nested_dict = {
'person1': {'name': 'John', 'age': 30, 'city': 'New York'},
'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'},
}
You can modify the values in a nested dictionary by accessing the appropriate key and assigning a new value.
nested_dict = {
'person1': {'name': 'John', 'age': 30, 'city': 'New York'},
'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'},
}
Output:
{'person1': {'name': 'John', 'age': 30, 'city': 'San Francisco'}, 'person2': {'name': 'Alice', 'age': 26, 'city': 'Los Angeles'}}
You can add new key-value pairs to either the outer dictionary or the inner dictionary.
Example: Adding New Keys
nested_dict = {
'person1': {'name': 'John', 'age': 30, 'city': 'New York'},
'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'},
}
Output:
{'person1': {'name': 'John', 'age': 30, 'city': 'New York', 'occupation': 'Engineer'}, 'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'}, 'person3':
{'name': 'Bob', 'age': 35, 'city': 'Chicago'}}
You can delete elements from a nested dictionary by using the del keyword for both the outer and inner dictionaries.
Example: Deleting Elements
nested_dict = {
'person1': {'name': 'John', 'age': 30, 'city': 'New York'},
'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'},
}
Output:
{'person1': {'name': 'John', 'city': 'New York'}, 'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'}}
{'person1': {'name': 'John', 'city': 'New York'}}
You can iterate over both the outer dictionary (for the main keys) and the inner dictionaries (for their respective key-value pairs).
Example: Iterating with Nested Loops
nested_dict = {
'person1': {'name': 'John', 'age': 30, 'city': 'New York'},
'person2': {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'},
}
Output:
person1:
name: John
age: 30
city: New York
person2:
name: Alice
age: 25
city: Los Angeles
Example Use Case: Nested Dictionary for Storing Student Grades
A common use case for a nested dictionary is storing student grades for different subjects.
grades = {
'John': {'Math': 90, 'Science': 85, 'English': 88},
'Alice': {'Math': 75, 'Science': 80, 'English': 82},
'Bob': {'Math': 88, 'Science': 92, 'English': 85},
}
Output:
85
{'Math': 75, 'Science': 80, 'English': 90}
{'John': {'Math': 90, 'Science': 85, 'English': 88}, 'Alice': {'Math': 75, 'Science': 80, 'English': 90}, 'Bob': {'Math': 88, 'Science': 92, 'English': 85}, 'Charlie':
{'Math': 70, 'Science': 78, 'English': 80}}
Conclusion
Nested dictionaries in Python are a powerful feature that allows you to store and manipulate hierarchical data. They provide a flexible way to handle
complex data structures, such as storing data for multiple entities or handling multi-level data. You can access, modify, and iterate through nested
dictionaries, making them very useful for many real-world applications.
Practice Suggestions:
Task 1: Create a nested dictionary that stores information about multiple books (title, author, genre, and price).
Task 2: Write a program that uses a nested dictionary to store and display employee information in a company (name, department, and salary).
Task 3: Use a nested dictionary to represent a small inventory system with categories (electronics, clothing, groceries) and their respective items.
Immutable Types
An immutable type is a type of object whose state or value cannot be changed after it is created. Once an object is created, it cannot be altered.
python
Copy code
# Immutable types
# String is immutable
s = "Hello"
# Trying to change a character (will result in error)
# s[0] = "h" # This will raise a TypeError
# Tuple is immutable
t = (1, 2, 3)
# t[0] = 0 # This will raise a TypeError
Explanation:
A mutable type is a type of object whose state or value can be modified after it is created. This means that you can change the content of the object
without creating a new one.
Lists
Dictionaries
Sets
Bytearrays
python
Copy code
# Mutable types
# List is mutable
lst = [1, 2, 3]
lst[0] = 0 # Modifying the first element of the list
print(lst) # Output: [0, 2, 3]
# Dictionary is mutable
d = {"name": "Alice", "age": 25}
d["age"] = 26 # Modifying a value in the dictionary
print(d) # Output: {'name': 'Alice', 'age': 26}
# Set is mutable
s = {1, 2, 3}
s.add(4) # Adding an element to the set
print(s) # Output: {1, 2, 3, 4}
Explanation:
Key Differences
For mutable types (e.g., lists, dictionaries): When you assign an object to a variable, both variables refer to the same object in memory. If you
modify one, the changes will be visible in both variables.
Example:
python
Copy code
lst1 = [1, 2, 3]
lst2 = lst1
lst2[0] = 0
print(lst1) # Output: [0, 2, 3]
print(lst2) # Output: [0, 2, 3]
For immutable types (e.g., strings, tuples): When you assign an object to a variable, a new object is created if you modify it. The original object
remains unchanged.
Example:
python
Copy code
s1 = "hello"
s2 = s1
s2 = "world" # New string created
print(s1) # Output: hello
print(s2) # Output: world
Practical Considerations
1. Function Arguments: When passing mutable objects to functions, the changes made within the function will affect the original object. For
immutable objects, a new object will be created inside the function, leaving the original object unchanged.
python
Copy code
def modify_list(lst):
lst[0] = 100 # Will affect the original list
def modify_string(s):
s = "World" # Creates a new string, does not affect the original string
return s
my_list = [1, 2, 3]
my_string = "Hello"
modify_list(my_list)
modify_string(my_string)
2. Using Mutable Objects as Dictionary Keys: Mutable objects like lists or dictionaries cannot be used as keys in a dictionary because they are not
hashable. Immutable objects like strings, tuples, and numbers can be used as dictionary keys because they are hashable.
python
Copy code
# This will raise a TypeError because lists are mutable and not hashable
my_dict = {[1, 2]: "value"} # TypeError: unhashable type: 'list'
Conclusion
Mutable types are useful when you want to modify the data in place without creating new objects, making them efficient for certain operations,
such as appending to lists or modifying dictionary values.
Immutable types provide more security and predictability since their data cannot be changed accidentally, which is especially helpful when you
want to ensure that the original object remains unchanged, for instance, when used as a key in a dictionary.
Understanding the difference between mutable and immutable types will help you write more efficient, predictable, and bug-free code, especially when
working with complex data structures and algorithms.
String Operations
String Methods (len, upper, lower, strip, split, etc.)
Method Description Example
text = "Hello, World!"
len() Returns the number of characters in a string.
print(len(text)) # Output: 13
text = "hello"
upper() Converts all characters in the string to uppercase.
print(text.upper()) # Output: "HELLO"
lower() Converts all characters in the string to lowercase. text = "HELLO"
print(text.lower()) # Output: "hello"
text = " Hello, World! "
strip() Removes leading and trailing whitespace from the string.
print(text.strip()) # Output: "Hello, World!"
text = "apple,banana,orange"
split() Splits a string into a list using a specified delimiter (default is whitespace). print(text.split(',')) # Output: ['apple',
'banana', 'orange']
text = "I like Python"
replace() Replaces all occurrences of a substring with another substring. print(text.replace("Python", "Java")) # Output:
"I like Java"
text = "I love Python"
find() Returns the index of the first occurrence of a substring (returns -1 if not found).
print(text.find("love")) # Output: 2
text = "apple apple apple"
count() Returns the number of occurrences of a substring in the string.
print(text.count("apple")) # Output: 3
text = "Hello, World!"
startswith() Checks if the string starts with a specified prefix.
print(text.startswith("Hello")) # Output: True
text = "Hello, World!"
endswith() Checks if the string ends with a specified suffix.
print(text.endswith("!")) # Output: True
text = "Hello"
isalpha() Returns True if all characters in the string are alphabetic (letters).
print(text.isalpha()) # Output: True
text = "12345"
isdigit() Returns True if all characters in the string are digits.
print(text.isdigit()) # Output: True
text = " "
isspace() Returns True if all characters in the string are whitespace.
print(text.isspace()) # Output: True
Joins the elements of an iterable (like a list) into a single string with a specified words = ["Hello", "World"]
join()
separator. print(" ".join(words)) # Output: "Hello World"
text = "hello world"
title() Capitalizes the first letter of each word in the string.
print(text.title()) # Output: "Hello World"
swapcase() Swaps the case of all characters in the string (lowercase becomes uppercase and vice text = "Hello, World!"
print(text.swapcase()) # Output: "hELLO,
versa).
wORLD!"
text = "hello"
capitalize() Capitalizes the first character of the string and lowercases the rest.
print(text.capitalize()) # Output: "Hello"
name = "Alice", age = 30
text = "My name is {} and I am {} years
format() Allows you to format a string with placeholders {} that are replaced by variables.
old.".format(name, age)
print(text)
text = "Hello, World! Hello!"
rfind() Returns the index of the last occurrence of a substring (returns -1 if not found).
print(text.rfind("Hello")) # Output: 13
text = " Hello "
lstrip() Removes leading whitespace from the string.
print(text.lstrip()) # Output: "Hello "
text = " Hello "
rstrip() Removes trailing whitespace from the string.
print(text.rstrip()) # Output: " Hello"
1. Indexing:
a. Accessing individual characters in a string can be done using indices.
b. Positive indices start from 0 (left to right), and negative indices start from -1 (right to left).
2. Slicing:
a. Slicing allows you to extract a portion of the string.
b. The slice notation follows: string[start:end:step].
i. Start is the index to begin the slice (inclusive).
ii. End is the index to stop the slice (exclusive).
iii. Step determines how many characters to skip (default is 1).
3. Examples:
a. text[0:5] extracts characters from index 0 to 4 (i.e., Hello).
b. text[7:12] extracts characters from index 7 to 11 (i.e., World).
c. text[::2] selects every second character in the string, giving Hoo ol!.
d. text[::-1] reverses the string.
Practical Applications:
Escape Characters
Escap
e
Description Example Output
Chara
cter
text = "This is a backslash: \\"
\\ Backslash itself. Used to insert a backslash in a string. This is a backslash: \
print(text)
text = 'It\'s a great day!'
\' Single quote. Used to insert a single quote in a string. It's a great day!
print(text)
text = "She said, \"Hello!\""
\" Double quote. Used to insert a double quote in a string. She said, "Hello!"
print(text)
text = "Hello\nWorld!" Hello
\n Newline. Inserts a line break or new line.
print(text) World!
text = "Name:\tJohn"
\t Tab. Inserts a tab space. Name: John
print(text)
text = "Hello\rWorld!"
\r Carriage Return. Moves the cursor to the beginning of the line. World! (overwrites "Hello")
print(text)
text = "Hello\b!"
\b Backspace. Moves the cursor one position backward. Hell!
print(text)
\f Form feed. A page-breaking character, often used in printing text = "Hello\fWorld!" Hello (may cause page break in some
documents. print(text) contexts)
Vertical tab. Similar to \t but for vertical tabbing (less text = "Hello\vWorld!" Hello (may cause vertical tab depending on
\v
commonly used). print(text) environment)
text = "\u0048\u0065\u006C\
Unicode character escape. Inserts the Unicode character for
\uXXXX u006C\u006F" Hello
the given hexadecimal value.
print(text)
Hexadecimal character escape. Inserts the character text = "\x48\x65\x6c\x6c\x6f"
\xXX Hello
corresponding to the given hexadecimal value. print(text)
1. Backslash \\:
a. Used to escape the backslash character itself. Essential when working with file paths in Windows (e.g., "C:\\Program Files").
2. Single Quote \':
a. Allows insertion of a single quote within single-quoted strings, preventing syntax errors. Example: 'It\'s a great day!'.
3. Double Quote \":
a. Allows insertion of a double quote within double-quoted strings. Example: "She said, \"Hello!\"".
4. Newline \n:
a. Represents a new line, causing the output to start from the next line. Example: "Hello\nWorld!" prints:
Copy code
Hello
World!
5. Tab \t:
a. Inserts a tab space. Commonly used for formatting output. Example: "Name:\tJohn" will output Name: John.
6. Carriage Return \r:
a. Moves the cursor to the beginning of the line. It can overwrite text when used. Example: "Hello\rWorld!" will result in World! because "World!"
overwrites "Hello".
7. Backspace \b:
a. Moves the cursor one position back, effectively deleting the previous character. Example: "Hello\b!" will output Hell!.
8. Form Feed \f:
a. Creates a page break (not commonly used in modern applications, mostly seen in legacy systems). Example: "Hello\fWorld!".
9. Vertical Tab \v:
a. Similar to the tab character but for vertical tabbing. Example: "Hello\vWorld!".
10. Unicode Escape \uXXXX:
a. Represents a Unicode character using a four-digit hexadecimal number. Example: \u0048\u0065\u006C\u006C\u006F corresponds to "Hello".
11. Hexadecimal Escape \xXX:
a. Represents a character in hexadecimal. Example: \x48\x65\x6c\x6c\x6f corresponds to "Hello".
Practical Uses:
Formatting Output: Escape characters like \n, \t, and \r are commonly used to format output text for better readability.
Handling Quotes: Using \' and \" allows quotes to be included in strings without causing syntax errors.
Unicode: \uXXXX is useful for representing special characters from different languages or symbols that aren’t directly available on a keyboard.
String Concatenation
Operation Description Example Output
text = "Hello" + " " + "World!"
Using + Operator The + operator is used to concatenate two or more strings. Hello World!
print(text)
text = " ".join(["Hello",
The join() method joins the elements of an iterable (e.g., list) into a
Using join() Method "World!"]) Hello World!
single string.
print(text)
Using f-strings (Python f-strings (formatted string literals) allow embedding expressions name = "John" Hello, John!
greeting = f"Hello, {name}!"
3.6+) inside string literals.
print(greeting)
Using format() Method greeting = "Hello, Hello, John!
The format() method allows positional or keyword arguments to be
{}!".format("John")
inserted into the string.
print(greeting)
The % operator (old-style string formatting) is used to insert variables greeting = "Hello, %s!" % "John"
Using % Operator Hello, John!
into a string. print(greeting)
1. Using + Operator:
a. This is the most straightforward way to concatenate strings in Python. It simply combines the strings and returns a new string.
b. Example:
python
Copy code
text = "Hello" + " " + "World!"
print(text)
Output:
Copy code
Hello World!
Output:
Copy code
Hello World!
Output:
Copy code
Hello, John!
Output:
Copy code
Hello, John!
5. Using % Operator:
a. This is an older method of string formatting in Python, but still commonly used in legacy code. The % operator works similarly to C-style string
formatting.
b. Example:
python
Copy code
greeting = "Hello, %s!" % "John"
print(greeting)
Output:
Copy code
Hello, John!
Practical Uses:
Combining strings: Concatenation is helpful when you need to combine multiple strings into one, like when building a sentence, message, or file
path.
String Formatting: Methods like f-strings and format() are often used for more complex string formatting tasks, allowing variables to be easily
inserted into the string.
f-Strings
Feature Description Example Output
name = "John"
Basic f- Embed expressions inside string literals, prefixed with
greeting = f"Hello, {name}!" Hello, John!
strings an f.
print(greeting)
age = 30
Expressions
Any valid Python expression can be used inside {}. message = f"Next year, you will be {age + 1} Next year, you will be 31
inside f-
This includes operations and function calls. years old!" years old!
strings
print(message)
pi = 3.14159
Format Use format specifiers inside f-strings to control the
formatted = f"Pi is approximately {pi:.2f}" Pi is approximately 3.14
Specifiers output format of values.
print(formatted)
Escape Use double curly braces {{ and }} to include literal message = f"{{Hello}} {name}"
{Hello} John
Braces braces in the string. print(message)
Using f-
Use the = sign inside f-strings to output both the value = 10
strings for value=10
expression and its value (debugging feature). print(f"{value=}")
debugging
greeting = f"""Hello, {name}.
Multiline f- f-strings can span multiple lines, making it easier to Hello, John. How are you
How are you today?"""
strings format long strings. today?
print(greeting)
Explanation of f-Strings:
1. Basic f-strings:
a. f-strings allow you to embed expressions inside string literals, prefixed with f. The variables or expressions inside {} are evaluated at runtime
and formatted as part of the string.
b. Example:
python
Copy code
name = "John"
greeting = f"Hello, {name}!"
print(greeting)
Output:
Copy code
Hello, John!
Output:
sql
Copy code
Next year, you will be 31 years old!
3. Format Specifiers:
a. You can specify how the values should be formatted using format specifiers, similar to how you would format numbers in the format() method.
b. Example:
python
Copy code
pi = 3.14159
formatted = f"Pi is approximately {pi:.2f}"
print(formatted)
Output:
csharp
Copy code
Pi is approximately 3.14
4. Escape Braces:
a. If you need to include actual curly braces { or } in the output, you can escape them by doubling the braces {{ and }}.
b. Example:
python
Copy code
message = f"{{Hello}} {name}"
print(message)
Output:
Copy code
{Hello} John
Output:
makefile
Copy code
value=10
6. Multiline f-strings:
a. f-strings can span multiple lines to make formatting easier for longer strings, especially when concatenating multiple pieces of data or
formatting output.
b. Example:
python
Copy code
greeting = f"""Hello, {name}.
How are you today?"""
print(greeting)
Output:
sql
Copy code
Hello, John.
How are you today?
Practical Uses:
Dynamic String Construction: f-strings are ideal for building strings dynamically with variable content.
Improved Readability: They provide a cleaner and more readable way to concatenate strings and variables.
Performance: f-strings are faster than using string concatenation (+) or the format() method, making them the preferred choice for string
formatting in Python.
Use f-strings when you need to insert variables or expressions into strings.
Use them for efficient and readable string formatting, especially when dealing with calculations, function calls, or when formatting large blocks of
text.
Loops Enhancements
Nested Loops
A nested loop is a loop inside another loop. Python allows you to nest multiple loops, such as for loops or while loops, within each other. Nested loops are
often used to handle multi-dimensional data, such as matrices or lists of lists, and they can also be used for complex iterations.
Output:
Copy code
00
01
02
10
11
12
20
21
22
Output:
Copy code
1
2
3
4
5
6
7
8
9
Output:
Copy code
00
01
02
10
11
12
20
21
22
Output:
Copy code
00
01
10
11
20
21
Output:
Copy code
00
02
10
12
20
22
Working with Multi-dimensional Data: Use nested loops when you need to process multi-dimensional data structures such as matrices (2D
arrays), grids, or lists of lists.
Handling Combinations/Permutations: If you need to calculate combinations or permutations, nested loops are useful.
Iterating over Complex Data: Nested loops are ideal for situations where you need to iterate over multiple levels of data, for example, nested
dictionaries or more complex nested structures.
Performance Consideration:
Nested loops can become inefficient for large datasets or deep nesting due to the increased time complexity. Each additional nested loop multiplies the
iterations, making the algorithm slower (e.g., O(n^2) for two nested loops). Always consider whether there is a more efficient approach when working with
large datasets.
Conclusion:
Nested loops are a powerful feature in Python that allow you to handle complex iterations over multi-dimensional data or perform repetitive tasks at
multiple levels. However, be mindful of performance issues when working with large datasets or deep levels of nesting.
Loop with else
In Python, you can use an else statement with loops (for and while). The else block will be executed after the loop has finished running if the loop wasn't
terminated by a break statement. If the loop terminates early due to a break, the else block is skipped.
For a for loop: The else block will execute after all iterations of the loop are completed unless the loop is interrupted by a break statement.
For a while loop: The else block will execute when the loop condition becomes False and the loop finishes without interruption.
The else block will execute when the for loop completes all iterations.
python
Copy code
for i in range(3):
print(i)
else:
print("Loop completed")
Output:
vbnet
Copy code
0
1
2
Loop completed
Explanation: The loop runs for i = 0, 1, 2, and after the loop finishes all iterations, the else block is executed.
Output:
Copy code
0
Explanation: The loop breaks when i == 1, so the else block is not executed.
The else block will execute after the while loop completes its condition.
python
Copy code
i=0
while i < 3:
print(i)
i += 1
else:
print("Loop completed")
Output:
vbnet
Copy code
0
1
2
Loop completed
Explanation: The while loop runs while i is less than 3. After the loop ends, the else block is executed.
python
Copy code
i=0
while i < 3:
if i == 1:
break
print(i)
i += 1
else:
print("Loop completed")
Output:
Copy code
0
Explanation: The loop breaks when i == 1, so the else block is not executed.
Key Points:
The else block runs after the loop completes all its iterations unless a break statement is used to interrupt the loop.
If the loop is terminated with break, the else block is skipped.
No break means the else block will always execute, even if the loop condition fails.
Searching: When searching for an item in a list, you can use the else block to confirm that the item was not found if the loop completed without a
break.
Checking Conditions: Use else to confirm that a condition was met after looping through a dataset, such as finding a matching entry in a list.
This feature of Python helps to make your code more readable, especially when handling search and condition verification operations in loops.
python
Copy code
range(start, stop, step)
When only one argument is passed to range(), it generates numbers from 0 to the given number (exclusive).
python
Copy code
for i in range(5):
print(i)
Output:
Copy code
0
1
2
3
4
Explanation: The loop will iterate from 0 to 4 (because the stop is 5).
You can specify both the start and stop values. The range will start at the start value and stop just before the stop value.
python
Copy code
for i in range(2, 6):
print(i)
Output:
Copy code
2
3
4
5
Explanation: The range starts from 2 and goes up to, but does not include, 6.
By adding a step argument, you can control the increment between the values in the range.
python
Copy code
for i in range(1, 10, 2):
print(i)
Output:
Copy code
1
3
5
7
9
Explanation: The range starts from 1, stops before 10, and increments by 2 at each step.
python
Copy code
for i in range(10, 0, -2):
print(i)
Output:
Copy code
10
8
6
4
2
Explanation: The range starts from 10, stops before 0, and decrements by 2 at each step.
Summary of range() Arguments:
Argume
Default Value Description
nt
start 0 The starting value (inclusive) of the sequence.
stop Required The stopping value (exclusive) of the sequence.
step 1 The increment or decrement between each value in the range.
Fixed Number of Iterations: Use range() when you need to loop over a specific number of iterations.
Counting by Steps: Use range() when you need to iterate through numbers in a specific step, such as skipping values or iterating backwards.
Indexing in Loops: range() is commonly used when you need the index of an iterable, like when accessing items in a list or string.
Conclusion:
The range() function is an essential tool in Python that allows you to control how many times a loop runs and by what increments. It's simple yet powerful
for iterating over a sequence of numbers, making it useful for a variety of tasks like looping through arrays, performing repetitive tasks, or even counting.
python
Copy code
enumerate(iterable, start=0)
iterable: The collection (list, tuple, etc.) that you want to loop through.
start: The starting index value (default is 0).
The enumerate() function returns an iterator that produces pairs of an index and the corresponding element from the iterable.
Examples:
Here, enumerate() is used to loop through a list while also keeping track of the index.
python
Copy code
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
Output:
yaml
Copy code
Index 0: apple
Index 1: banana
Index 2: cherry
Explanation: enumerate() returns two values for each iteration: the index and the corresponding element. In this case, the index is printed along with the
fruit.
You can specify a custom starting index with the start argument.
python
Copy code
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits, start=1):
print(f"Index {index}: {fruit}")
Output:
yaml
Copy code
Index 1: apple
Index 2: banana
Index 3: cherry
Explanation: The starting index is now 1, so the index values will begin from 1 instead of the default 0.
python
Copy code
word = "python"
for index, letter in enumerate(word):
print(f"Index {index}: {letter}")
Output:
yaml
Copy code
Index 0: p
Index 1: y
Index 2: t
Index 3: h
Index 4: o
Index 5: n
Explanation: Here, enumerate() iterates through the string "python", providing both the index and each letter.
enumerate() can also be used inside a list comprehension for more complex operations.
python
Copy code
fruits = ['apple', 'banana', 'cherry']
result = [f"Fruit {index}: {fruit}" for index, fruit in enumerate(fruits)]
print(result)
Output:
css
Copy code
['Fruit 0: apple', 'Fruit 1: banana', 'Fruit 2: cherry']
Explanation: In this example, a list comprehension is used to create a new list where each item includes the index and the corresponding fruit.
Summary of enumerate():
Tracking Indexes: When you need both the index and the item in a loop, enumerate() is very useful. Without it, you would need to manually track
the index.
Cleaner Code: It eliminates the need for a separate index variable and simplifies the loop syntax.
When Iterating Over Collections: enumerate() can be used with any iterable (e.g., lists, tuples, strings), making it versatile for various use cases.
Conclusion:
The enumerate() function is a powerful tool in Python that makes it easy to keep track of both the element and its index when iterating through a
sequence. It simplifies your code, especially when you need both the item and its position in the collection.
Dictionary Structure:
A dictionary in Python is an unordered collection of items. Each item is a key-value pair, where the key is unique, and the value can be of any data type.
Example:
python
Copy code
person = {'name': 'John', 'age': 25, 'city': 'New York'}
In the above dictionary, 'name', 'age', and 'city' are keys, and 'John', 25, and 'New York' are their corresponding values.
There are several methods to iterate over dictionaries depending on whether you want to access the keys, values, or both keys and values.
To iterate over the keys of a dictionary, you can simply use the for loop directly on the dictionary.
Example:
python
Copy code
person = {'name': 'John', 'age': 25, 'city': 'New York'}
for key in person:
print(key)
Output:
Copy code
name
age
city
Explanation: In this example, the for loop automatically iterates over the keys of the dictionary.
python
Copy code
for key in person.keys():
print(key)
This also gives the same output. The .keys() method explicitly returns a view object that displays all the keys in the dictionary.
To iterate over the values in the dictionary, you can use the .values() method.
Example:
python
Copy code
for value in person.values():
print(value)
Output:
sql
Copy code
John
25
New York
Explanation: The .values() method returns a view object containing all the values in the dictionary.
If you need both the key and the value during iteration, you can use the .items() method, which returns a tuple containing the key and the value.
Example:
python
Copy code
for key, value in person.items():
print(f"Key: {key}, Value: {value}")
Output:
yaml
Copy code
Key: name, Value: John
Key: age, Value: 25
Key: city, Value: New York
Explanation: The .items() method returns an iterable of key-value pairs, which allows you to unpack the tuple into key and value during iteration.
You can also apply conditions during iteration, for example, filtering based on values.
Example:
python
Copy code
for key, value in person.items():
if isinstance(value, int): # Check if the value is an integer
print(f"Key: {key}, Value: {value}")
Output:
yaml
Copy code
Key: age, Value: 25
Explanation: This loop prints only the key-value pairs where the value is an integer.
List comprehensions can also be used for iterating over dictionaries to create a new list based on certain conditions.
Example:
python
Copy code
keys_starting_with_a = [key for key in person if key.startswith('a')]
print(keys_starting_with_a)
Output:
css
Copy code
['age']
Explanation: This list comprehension filters the keys that start with the letter 'a'.
Use .keys(): When you only need the keys of the dictionary.
Use .values(): When you only need the values.
Use .items(): When you need both the keys and values in a single loop.
Use List Comprehension: For more complex filtering and transformation of dictionary items.
Use Conditionals: When you want to apply a filter or condition based on key or value during iteration.
Conclusion:
Iterating over dictionaries in Python is very flexible and powerful. Depending on whether you need the keys, values, or both, Python provides several
methods like .keys(), .values(), and .items() to help you efficiently loop through dictionary items.
Functions Enhancements
Lambda Functions
A lambda function in Python is a small anonymous function defined using the lambda keyword. Unlike regular functions defined with the def keyword,
lambda functions are typically used for short, simple operations and are written in a single line of code.
Lambda functions are also known as anonymous functions because they are not bound to a name unless assigned to a variable.
python
Copy code
lambda arguments: expression
python
Copy code
# Lambda function that adds two numbers
add = lambda x, y: x + y
Explanation:
lambda x, y: x + y defines an anonymous function that takes two arguments, x and y, and returns their sum.
The result of add(5, 3) is 8.
1. Short and Simple: Lambda functions are concise and typically used for small operations that can be written in one line.
2. No Return Statement: Lambda functions don't require an explicit return keyword because the expression is automatically returned.
3. Anonymous: Lambda functions are usually not assigned to variables, but they can be if needed.
1. With Higher-Order Functions: Lambda functions are often used with functions like map(), filter(), and reduce(), which expect a function as an
argument.
The map() function applies a given function to each item of an iterable (like a list) and returns an iterator.
python
Copy code
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)
Explanation:
The lambda function lambda x: x ** 2 squares each element in the numbers list.
python
Copy code
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
Explanation:
The lambda x: x % 2 == 0 condition filters out odd numbers and only keeps even numbers from the numbers list.
The reduce() function from the functools module reduces a list of items into a single value using a binary function (a function that takes two arguments).
python
Copy code
from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24
Explanation:
The lambda x, y: x * y function multiplies all elements of the numbers list together.
Lambda functions are also commonly used to sort data, especially when sorting by a specific attribute in a list of tuples or dictionaries.
python
Copy code
# List of tuples (name, age)
people = [("Alice", 25), ("Bob", 30), ("Charlie", 20)]
Explanation:
lambda x: x[1] specifies that sorting should be done based on the second element (age) of each tuple.
python
Copy code
# List of dictionaries
students = [{"name": "Alice", "score": 85}, {"name": "Bob", "score": 90}, {"name": "Charlie", "score": 80}]
print(sorted_students)
# Output: [{'name': 'Charlie', 'score': 80}, {'name': 'Alice', 'score': 85}, {'name': 'Bob', 'score': 90}]
Explanation:
lambda student: student['score'] sorts the list of dictionaries based on the 'score' key.
Lambda functions can also be used with conditional expressions, allowing you to write simple conditional logic in one line.
Example: Using lambda with if-else
python
Copy code
# Lambda function with a conditional expression
max_value = lambda x, y: x if x > y else y
Explanation:
lambda x, y: x if x > y else y compares two numbers and returns the larger of the two.
Conclusion
Lambda functions are useful for small, quick operations that can be written in a single line. They are widely used in higher-order functions like map(),
filter(), and reduce(), as well as in sorting operations and conditional logic. While they are compact and concise, lambda functions are limited in
functionality compared to regular functions defined with def, making them ideal for simple tasks.
Map, Filter, and Reduce
In Python, the map(), filter(), and reduce() functions are used to perform operations on collections like lists, tuples, or any other iterables. These
functions are often used in combination with lambda functions to create concise and efficient solutions for common tasks.
1. map() Function
The map() function applies a given function to all items in an iterable (like a list, tuple, etc.) and returns an iterator (or map object) that yields the results.
Syntax:
python
Copy code
map(function, iterable)
The result of the map() function is a map object, which is an iterator. You can convert it to a list or other data types for better readability.
python
Copy code
numbers = [1, 2, 3, 4, 5]
Explanation:
The lambda function lambda x: x ** 2 squares each element of the numbers list.
2. filter() Function
The filter() function is used to filter elements from an iterable based on a function that returns either True or False. Only elements that evaluate to True
will be included in the result.
Syntax:
python
Copy code
filter(function, iterable)
function: A function that tests each item in the iterable. The function should return True or False.
iterable: An iterable (list, tuple, etc.) that will be filtered.
Like map(), the result is an iterator. You can convert it to a list for easier reading.
Example: Using filter() with a Lambda Function
python
Copy code
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
The lambda function lambda x: x % 2 == 0 returns True for even numbers and False for odd numbers. The filter() function then returns only the
even numbers.
3. reduce() Function
The reduce() function, which is part of the functools module, applies a binary function (a function that takes two arguments) cumulatively to the items of
an iterable. This reduces the iterable to a single value.
Syntax:
python
Copy code
from functools import reduce
python
Copy code
from functools import reduce
numbers = [1, 2, 3, 4]
print(product) # Output: 24
Explanation:
The lambda function lambda x, y: x * y takes two arguments and returns their product. The reduce() function applies this operation cumulatively to
the list [1, 2, 3, 4], which results in the product 1 * 2 * 3 * 4 = 24.
Key Differences Between map(), filter(), and reduce()
1. map():
a. Converting temperatures from Celsius to Fahrenheit for a list of temperatures.
b. Converting strings to uppercase.
2. filter():
a. Extracting all positive numbers from a list.
b. Finding all strings in a list that have more than 5 characters.
3. reduce():
a. Calculating the total sum or product of a list.
b. Combining a list of strings into a single sentence.
Conclusion
map() applies a function to each item in an iterable and returns an iterable of the results.
filter() filters the iterable by applying a function that returns True or False, returning only the elements that evaluate to True.
reduce() cumulatively applies a binary function to an iterable, reducing it to a single result.
These functions are highly effective when combined with lambda functions and are part of the functional programming approach in Python.
Recursion
Recursion is a technique in programming where a function calls itself in order to solve a problem. A recursive function typically solves a small portion of
the problem and then calls itself with modified parameters, gradually reducing the problem until a base case is reached.
1. Base Case: The condition under which the function stops calling itself, preventing an infinite loop.
2. Recursive Case: The part where the function calls itself with a modified argument, progressively breaking down the problem.
Syntax:
python
Copy code
def recursive_function(parameters):
# Base case
if condition:
return result
else:
# Recursive case
return recursive_function(modified_parameters)
Example 1: Factorial Calculation
A common example of recursion is calculating the factorial of a number, where the factorial of a number n is the product of all positive integers less than
or equal to n.
Factorial Formula:
For example:
5! = 5 * 4 * 3 * 2 * 1 = 120
0! = 1 (by definition)
Python Code:
python
Copy code
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: n * factorial of (n-1)
return n * factorial(n - 1)
Explanation:
The Fibonacci sequence is another classic example of recursion. In the Fibonacci sequence, each number is the sum of the two preceding ones, starting
from 0 and 1.
Fibonacci Formula:
Python Code:
python
Copy code
def fibonacci(n):
# Base case: the first two Fibonacci numbers
if n == 0:
return 0
elif n == 1:
return 1
else:
# Recursive case: sum of the previous two Fibonacci numbers
return fibonacci(n - 1) + fibonacci(n - 2)
Explanation:
You can also use recursion to sum the elements of a list. The idea is to sum the first element and then recursively sum the rest of the list.
Python Code:
python
Copy code
def sum_list(lst):
# Base case: if the list is empty, the sum is 0
if not lst:
return 0
else:
# Recursive case: sum of the first element and the sum of the rest of the list
return lst[0] + sum_list(lst[1:])
Explanation:
Advantages of Recursion:
1. Simplicity: Recursion can make certain problems easier to solve, especially those that involve breaking down problems into similar subproblems
(e.g., tree traversals, searching).
2. Elegant Solutions: Some problems are more naturally expressed through recursion, reducing the complexity of the code.
Disadvantages of Recursion:
1. Performance Issues: Recursive calls add overhead due to repeated function calls. It can lead to inefficiency if not handled properly (e.g.,
recalculating the same values in Fibonacci).
2. Stack Overflow: Python has a recursion depth limit (default is 1000), and deep recursion can lead to a RecursionError or stack overflow.
Python has a default recursion depth limit of 1000. If the recursion goes beyond this limit, Python raises a RecursionError. You can check or change this
limit using the sys module.
Example:
python
Copy code
import sys
print(sys.getrecursionlimit()) # Output: 1000
Tail Recursion
Tail recursion is a form of recursion where the recursive call is the last operation in the function. Some languages optimize tail recursion to avoid adding
new frames to the stack. However, Python does not optimize tail recursion, so even tail-recursive functions can lead to stack overflow if the recursion is
deep.
Conclusion
Recursion is a powerful concept where a function solves a problem by calling itself. It’s useful for problems that have a repetitive structure (like factorials,
Fibonacci sequence, tree traversals). However, it’s important to be mindful of recursion limits and potential inefficiencies.
File Handling
Python provides built-in functions to work with files, such as reading from, writing to, and manipulating files. Below are the common operations and file
modes used in file handling.
Reading Files
Reading files allows you to extract data stored in a file for processing in your program. The most commonly used method to read files is the read()
method, which reads the entire file, or readline() for reading line by line.
python
Copy code
with open("example.txt", "r") as file:
content = file.read()
print(content)
Explanation:
Writing Files
Writing to files allows you to modify or add data to a file. You can use the write() method to write strings to the file.
Example - Writing to a File:
python
Copy code
with open("example.txt", "w") as file:
file.write("This is a test line.")
Explanation:
"w": File mode for writing. If the file doesn't exist, it will be created. If it exists, it will be overwritten.
File Modes
File modes define the way in which a file is opened. Here are some of the common file modes in Python:
Mod
Description
e
r Read (default mode). Opens a file for reading only. The file must exist.
Write. Opens a file for writing. If the file exists, it will be overwritten. If not, a new file is
w
created.
Append. Opens a file for appending. Data is written at the end of the file. If the file
a
doesn’t exist, it’s created.
rb Read binary. Opens a file for reading in binary mode.
Write binary. Opens a file for writing in binary mode. If the file doesn’t exist, it’s
wb
created.
ab Append binary. Opens a file for appending in binary mode. If the file doesn’t exist, it’s
created.
r+ Read and write. Opens a file for both reading and writing. The file must exist.
Write and read. Opens a file for both writing and reading. If the file exists, it is
w+
overwritten.
Append and read. Opens a file for both appending and reading. If the file doesn’t exist,
a+
it’s created.
rb+ Read and write binary. Opens a file for both reading and writing in binary mode.
wb+ Write and read binary. Opens a file for both writing and reading in binary mode.
The with statement in Python is used for resource management. It ensures that resources, such as files, are properly closed after being used, even if an
error occurs. This is known as context management.
python
Copy code
# Writing to a file using 'with'
with open("example.txt", "w") as file:
file.write("This is an example text.")
with statement: Automatically manages the opening and closing of the file.
open() function: Opens the file, and the file is automatically closed after the block is executed.
This approach helps avoid errors like forgetting to close the file or opening too many files simultaneously.
Conclusion
Using proper file modes and the with statement ensures efficient file handling in Python. Here's a summary of the most important concepts:
Mode
Operation Example Code Description
Used
Read a File with open("file.txt", "r") as file: ... r Opens a file for reading.
with open("file.txt", "w") as
Write to a File w Opens a file for writing.
file: ...
with open("file.txt", "a") as
Append to a File a Opens a file for appending.
file: ...
with open("file.bin", "rb") as
Read Binary rb Opens a file for binary reading.
file: ...
with open("file.bin", "wb") as file:
Write Binary wb Opens a file for binary writing.
...
with open("file.txt", "r+") as Opens a file for both reading and
Read and Write r+
file: ... writing.
Write and Read with open("file.txt", "w+") as file: Opens a file for writing and reading,
w+
(Overwrite) ... overwriting the file.
Modules and Packages
Modules in Python allow you to organize your code into reusable components. Python has built-in modules, and you can also install and use external
libraries to extend its functionality.
Importing Modules
Modules can be imported into your Python code to use functions, classes, and variables defined within them.
Example:
python
Copy code
import math
print(math.sqrt(16)) # Using math module to calculate the square root of 16
Built-in Modules
Python provides several built-in modules that you can use without needing to install anything. Some commonly used built-in modules are:
python
Copy code
import math
import random
# Using math module to find the square root of a number
print(math.sqrt(25)) # Output: 5.0
External libraries can be installed using package managers like pip. Once installed, they can be imported and used just like built-in modules.
bash
Copy code
pip install requests
2. Using the Library in Python:
python
Copy code
import requests
Summary of Modules
By understanding how to import built-in modules, use external libraries, and install them with pip, you can significantly extend the functionality of your
Python programs.
Miscellaneous Basics
Python Keywords
Python keywords are reserved words that have special meaning in the Python programming language. They cannot be used as identifiers (such as
variable names, function names, or class names) because they are part of the Python syntax. These keywords define the structure and behavior of the
Python language.
Keyword Description
False Represents a boolean value of false.
None Represents the absence of a value or a null value.
True Represents a boolean value of true.
and A logical operator used to combine conditional statements.
as Used to create an alias while importing modules or handle exceptions.
assert Used for debugging purposes to test if a condition is true.
break Exits the current loop.
class Used to define a class.
continue Skips the current iteration of the loop and moves to the next iteration.
def Used to define a function.
del Deletes a reference to an object.
elif Used in conditional statements to specify an additional condition.
else Used in conditional statements to provide an alternative block of code.
except Used in exception handling to define a block of code that handles errors.
Used in exception handling to define a block of code that is always
finally
executed.
for Used to start a for loop.
from Used to import specific parts of a module.
global Declares a variable as global.
if Used to make a conditional statement.
import Used to import a module or specific components of a module.
in Used to check if a value is present in an iterable (list, tuple, etc.).
is Tests if two variables point to the same object.
lambda Defines an anonymous function.
Declares a variable as nonlocal, meaning it refers to a variable in an
nonlocal
enclosing scope.
not A logical operator used to invert a boolean value.
or A logical operator used to combine conditional statements.
pass A placeholder for code that is syntactically required but doesn't do anything.
raise Used to raise an exception manually.
return Exits a function and optionally returns a value.
try Used to start a block of code that will be tested for exceptions.
while Used to start a while loop.
with Used to simplify exception handling, especially for file handling.
Used in a function to return a value to the caller, but allows the function to
yield
continue where it left off when called again.
Example Usage
python
Copy code
# Example of some Python keywords in action
# Output:
# You can't divide by zero!
# This will always be executed.
Global and Local Variables
In Python, variables can be classified based on their scope, which refers to where the variable is accessible in the code. The two main types of variables in
Python are global variables and local variables.
Local Variables
A local variable is a variable that is defined inside a function or a block of code and is only accessible within that function or block. It is created when the
function is called and destroyed when the function execution is completed.
python
Copy code
def my_function():
x = 10 # x is a local variable
print(x)
my_function() # Output: 10
# print(x) # This will give an error because x is local to my_function()
Global Variables
A global variable is a variable that is defined outside any function and is accessible throughout the entire program. It can be accessed by any function in
the code unless shadowed by a local variable of the same name.
Scope: Global, meaning it can be accessed anywhere in the program after it is defined.
Lifetime: Exists as long as the program is running.
python
Copy code
x = 20 # x is a global variable
def my_function():
print(x) # x is accessible because it is global
my_function() # Output: 20
To modify a global variable from inside a function, the global keyword must be used. Without it, Python treats the variable as a local variable within the
function.
Example:
python
Copy code
x = 10 # Global variable
def my_function():
global x # Tells Python to use the global variable x
x = 20 # Modify the global variable
my_function()
print(x) # Output: 20
If we don't use the global keyword, the function will create a new local variable with the same name, leaving the global variable unchanged.
python
Copy code
x = 10 # Global variable
def my_function():
x = 20 # Local variable, this doesn't affect the global x
my_function()
print(x) # Output: 10 (Global x remains unchanged)
Modifying a Global Variable: When a variable is defined globally but needs to be updated inside a function.
Accessing Global Variables: Global variables can be accessed in functions without the global keyword, but they cannot be changed unless
declared global.
python
Copy code
x = 10 # Global variable
def my_function():
print(x) # Accessing global variable x without modification
my_function() # Output: 10
A local variable is destroyed when the function finishes executing, and it cannot be accessed outside the function.
A global variable persists throughout the lifetime of the program and can be accessed from any function in the program.
python
Copy code
x = 30 # Global variable
def outer_function():
y = 40 # Local variable in outer_function
def inner_function():
z = 50 # Local variable in inner_function
print(f"Global x: {x}, Local y: {y}, Local z: {z}")
inner_function()
outer_function() # Output: Global x: 30, Local y: 40, Local z: 50
Key Points:
Python Identifiers
In Python, an identifier is a name used to identify variables, functions, classes, modules, and other objects. The name of any entity (variable, function,
etc.) in Python is called an identifier.
1. Must start with a letter or an underscore (_): Identifiers can begin with an uppercase or lowercase letter (a-z, A-Z) or an underscore (_).
a. Example: my_variable, sum, _count
2. Can include numbers after the first character: You can include numbers (0-9) after the first character, but not at the beginning.
a. Example: var1, total_2
3. Cannot use Python keywords: Identifiers cannot be the same as any of Python's reserved keywords (like if, else, while, for, class, etc.).
a. Example: if, class are not valid identifiers.
4. Case-sensitive: Python identifiers are case-sensitive. For example, myVariable and myvariable are two different identifiers.
a. Example: MyVariable is different from myVariable.
5. Cannot contain special characters: Identifiers can only include letters, numbers, and underscores. They cannot contain spaces or special
characters like @, #, $, etc.
a. Example: my@var or my var are invalid identifiers.
6. No length limit: Identifiers in Python can be of any length, though it is common practice to keep them reasonably short yet descriptive.
Valid
Description
Identifier
Starts with a letter, contains an
my_var
underscore
totalSum Starts with a letter, no numbers
_counter Starts with an underscore
var123 Contains letters and numbers
MyFunction Mixed case, valid identifier
Invalid
Reason
Identifier
123abc Starts with a number
my-var Contains a hyphen, which is not allowed
class A reserved Python keyword
total sum Contains a space, which is not allowed
my@var Contains an invalid special character @
Python has a set of reserved words or keywords that cannot be used as identifiers. These are predefined by Python and have special meanings in the
language. Some of the most common Python keywords are:
You can view the complete list of keywords in Python by using the following:
python
Copy code
import keyword
print(keyword.kwlist)
Naming Conventions for Python Identifiers
While Python allows you to name your variables and functions freely, it is important to follow some best practices for readability and maintainability:
1. Use meaningful names: Choose descriptive names for your variables, functions, and classes. For example, student_age is better than sa.
2. Use underscores for multi-word identifiers: For variables and functions with multiple words, separate the words with an underscore (_),
following the PEP 8 style guide.
a. Example: calculate_total_price, get_user_input
3. Class names: Class names should follow the CamelCase convention, where the first letter of each word is capitalized and no underscores are
used.
a. Example: EmployeeDetails, StudentRecord
4. Constants: Constants (variables whose values should not change) are usually written in uppercase letters with underscores between words.
a. Example: PI, MAX_LENGTH
5. Avoid using single character names: Except for counters or iterators, avoid using single-letter variable names.
a. Example: x or i should be avoided unless in short loops.
python
Copy code
class UserProfile:
def __init__(self, username, email):
self.username = username
self.email = email
def display_profile(self):
print(f"Username: {self.username}, Email: {self.email}")
Feature Details
Start Character Letter (a-z, A-Z) or underscore (_)
Subsequent
Letters, numbers (0-9), or underscores (_)
Characters
Case Sensitivity Identifiers are case-sensitive
Length No length limit (but should be reasonable)
Special
Cannot include special characters (e.g., @, #, $)
Characters
Keywords Cannot use Python reserved keywords
Conventions Use meaningful names, follow snake_case for variables, CamelCase for classes
Conclusion:
Python identifiers must follow specific rules and guidelines to be valid. Choosing descriptive, readable, and consistent names for identifiers is important for
writing clean and maintainable Python code.
The del Statement
The del statement in Python is used to delete objects, variables, or elements from data structures. It can be used to remove variables, items in a
list, keys in a dictionary, or even entire data structures.
General Usage:
python
Copy code
del object
Where object can be any variable, list element, dictionary key, or slice.
1. Deleting Variables: You can use del to remove a variable from the namespace, essentially deleting it.
python
Copy code
x = 10
del x # This deletes the variable 'x'
print(x) # Raises NameError: name 'x' is not defined
2. Deleting List Elements: You can delete specific elements from a list by specifying their index.
python
Copy code
my_list = [10, 20, 30, 40, 50]
del my_list[2] # Removes the element at index 2 (30)
print(my_list) # Output: [10, 20, 40, 50]
3. Deleting Dictionary Keys: You can delete a key-value pair from a dictionary by specifying the key.
python
Copy code
my_dict = {'name': 'Alice', 'age': 25}
del my_dict['age'] # Removes the key 'age' and its associated value
print(my_dict) # Output: {'name': 'Alice'}
4. Deleting Slices from Lists: You can delete a slice (range of elements) from a list using the del statement.
python
Copy code
my_list = [1, 2, 3, 4, 5, 6]
del my_list[1:4] # Removes elements from index 1 to 3 (2, 3, 4)
print(my_list) # Output: [1, 5, 6]
5. Deleting Entire Data Structures: You can delete entire data structures like lists, dictionaries, or even modules.
python
Copy code
my_list = [10, 20, 30]
del my_list # Deletes the entire list
print(my_list) # Raises NameError: name 'my_list' is not defined
6. Deleting Object Attributes: You can use del to remove an attribute from an object.
python
Copy code
class MyClass:
def __init__(self):
self.attribute = 10
obj = MyClass()
del obj.attribute # Deletes the attribute 'attribute' from the object
# print(obj.attribute) # Raises AttributeError: 'MyClass' object has no attribute 'attribute'
Reference Count: In Python, when you delete a variable or object, it reduces the reference count for that object. If no other reference to the object
exists, Python's garbage collector will reclaim the memory.
Deletion of Non-Existent Items: If you try to delete a variable, list element, or dictionary key that does not exist, it will raise a KeyError or
IndexError.
python
Copy code
my_list = [1, 2, 3]
del my_list[5] # Raises IndexError: list index out of range
Memory Management: Deleting objects explicitly can be useful for memory management, but in most cases, Python automatically handles
memory allocation and deallocation (via garbage collection).
Example Code
python
Copy code
# Deleting a variable
x=5
print(x) # Output: 5
del x
# print(x) # NameError: name 'x' is not defined
Summary Table
Conclusion:
The del statement in Python is a versatile tool for removing variables, list elements, dictionary keys, and even entire data structures. It helps manage the
memory by removing references to objects, and it is important to ensure you are deleting the right items to avoid errors.
Syntax:
python
Copy code
id(object)
Where object is the object for which you want to obtain the unique identity.
The value returned by id() is unique for each object during its lifetime.
It helps determine whether two variables point to the same object in memory (i.e., have the same identity).
Once an object is deleted or goes out of scope, its id() value may be reused for new objects.
Example Code:
python
Copy code
# Example 1: Using id() with integers
a = 10
b = 10
print(id(a)) # Output: Some unique integer (memory address of a)
print(id(b)) # Output: Same as id(a), because Python caches small integers
Explanation:
1. Integer Caching:
a. In Python, small integers (typically in the range -5 to 256) are cached for performance reasons. This means that when you create two
variables with the same small integer value, they may point to the same memory location, and their id() values will be the same.
b. In the example above, both a and b point to the same object in memory because of integer caching.
2. Mutable vs Immutable Objects:
a. Immutable objects like integers, strings, and tuples might have the same id() value when assigned the same value, due to interning or
caching. This is not true for mutable objects like lists or dictionaries.
b. In the case of lists (list1 and list2), both variables point to the same object in memory, so they share the same id(). However, list3 is a new list,
so its id() is different.
You can use id() to check whether two variables refer to the same object in memory:
python
Copy code
a = [1, 2, 3]
b=a
c = [1, 2, 3]
1. Checking Object Identity: You can compare the id() values to check if two variables are pointing to the same object.
2. Debugging: id() is useful for debugging when you want to know if two variables are referring to the same object in memory.
3. Memory Management: Helps understand object reuse and references in memory.
Summary Table:
Conclusion:
The id() function is a powerful tool to check the identity of objects in Python. By returning the unique memory address of an object, it allows you to track
and compare objects, especially in cases where you need to verify whether two variables refer to the same object.
Advanced Basics
A virtual environment in Python is a self-contained directory that contains a Python installation and additional packages required for a project. It
ensures that dependencies for different projects do not interfere with each other.
Steps to Set Up a Python Virtual Environment:
1. Install Python:
a. Ensure that Python is installed on your system. You can check by running:
bash
Copy code
python --version
b. If Python is not installed, you can download and install it from python.org.
2. Create a Virtual Environment:
a. Open your terminal or command prompt.
b. Navigate to the project directory where you want to set up the virtual environment.
c. Run the following command to create a virtual environment:
bash
Copy code
python -m venv myenv
b. On macOS/Linux:
bash
Copy code
source myenv/bin/activate
4. Install Packages:
a. With the virtual environment activated, you can install packages using pip:
bash
Copy code
pip install package-name
Python Installation and IDE Setup (e.g., VS Code, PyCharm, Jupyter Notebook)
Installing Python:
Setting up an IDE:
1. VS Code:
a. Install VS Code.
b. Install the Python extension from the Visual Studio Code marketplace to enable Python-specific features like linting, debugging, and
IntelliSense.
2. PyCharm:
a. Install PyCharm.
b. PyCharm is a full-featured IDE that is excellent for Python development, offering powerful debugging tools, code navigation, and project
management.
3. Jupyter Notebook:
a. Install Jupyter Notebook using the following command in your terminal (within your virtual environment):
bash
Copy code
pip install notebook
c. This will open a web interface where you can write and execute Python code interactively.
The Python REPL (Read-Eval-Print Loop) is an interactive environment that allows you to write and execute Python code one line at a time. It is useful for
experimenting with Python code snippets and testing functionality.
This will launch the Python REPL, and you can begin entering Python commands interactively.
Example:
python
Copy code
>>> 5 + 3
8
>>> print("Hello, Python!")
Hello, Python!
A common and simple way to debug your Python code is by inserting print() statements in your code to display values and track the flow of
execution.
Example:
python
Copy code
def add_numbers(a, b):
print(f"Adding {a} and {b}") # Debug print
return a + b
result = add_numbers(2, 3)
print(f"Result: {result}")
2. Using a Debugger:
Python includes a built-in debugger called pdb. You can invoke it to set breakpoints and step through your code.
Example:
python
Copy code
import pdb
divide_numbers(4, 2)
When running the script, execution will pause at pdb.set_trace(), and you can use commands like n (next), c (continue), or q (quit) to control
execution.
PEP 8 is the official style guide for Python code, ensuring consistency and readability across Python projects.
1. Indentation:
a. Use 4 spaces per indentation level (avoid tabs).
b. Consistently indent nested code blocks.
2. Line Length:
a. Limit all lines to 79 characters.
3. Blank Lines:
a. Use blank lines to separate functions and classes.
b. Use two blank lines to separate top-level functions and class definitions.
4. Naming Conventions:
a. Use lowercase with underscores for variable and function names (snake_case).
b. Use CapitalizedWords for class names (CamelCase).
c. Constants should be in uppercase with underscores (CONSTANT_NAME).
5. Import Statements:
a. Imports should be on separate lines:
python
Copy code
import os
import sys
b. Imports should be ordered: standard library imports first, followed by third-party imports, and then local imports.
6. Whitespace:
a. Avoid extra spaces inside parentheses, brackets, and braces.
b. Avoid unnecessary whitespace in expressions and statements.
7. Docstrings:
a. Use triple quotes for docstrings.
b. Use docstrings for all public methods and functions.
python
Copy code
import math
class Circle:
"""This class represents a circle."""
def area(self):
"""Calculate the area of the circle."""
return math.pi * self.radius ** 2
def circumference(self):
"""Calculate the circumference of the circle."""
return 2 * math.pi * self.radius
# Example usage:
circle = Circle(5)
print(f"Area: {circle.area()}") # Prints the area
Conclusion:
Virtual environments help manage dependencies for each Python project separately.
Python installation and IDE setup enable a robust development environment (VS Code, PyCharm, Jupyter).
The Python REPL is a quick way to interactively execute Python commands.
Debugging techniques like print and using a debugger (pdb) help identify issues in your code.
PEP 8 is a coding style guide that improves the readability and consistency of Python code. Following PEP 8 ensures your code is clean and easy for
others to understand.
Syntax:
python
Copy code
iter(object)
python
Copy code
my_list = [1, 2, 3, 4]
iterator = iter(my_list)
You can also use the for loop with the iterator, which automatically calls next() until the sequence is exhausted.
python
Copy code
for item in my_list:
print(item)
The next() function is used to retrieve the next item from an iterator. When the iterator is exhausted, it raises a StopIteration exception unless a default
value is provided.
Syntax:
python
Copy code
next(iterator, default)
Example:
python
Copy code
my_list = [1, 2, 3]
iterator = iter(my_list)
print(next(iterator)) # Output: 1
print(next(iterator)) # Output: 2
print(next(iterator)) # Output: 3
print(next(iterator)) # Raises StopIteration error
python
Copy code
print(next(iterator, 'End')) # Output: 'End'
Custom Iterators
In Python, you can define your own iterator by creating a class that implements the __iter__() and __next__() methods. The __iter__() method returns the
iterator object (usually self), and the __next__() method returns the next item from the sequence, raising StopIteration when the sequence is exhausted.
Example:
python
Copy code
class MyRange:
def __init__(self, start, end):
self.start = start
self.end = end
def __iter__(self):
self.current = self.start
return self
def __next__(self):
if self.current >= self.end:
raise StopIteration
self.current += 1
return self.current - 1
Copy code
1
2
3
4
A generator is a special type of iterator in Python that is defined using a function with the yield keyword. Unlike a regular function that returns a value
and exits, a generator function yields values one at a time and can resume from where it left off when the next value is requested.
Example:
python
Copy code
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
print(next(gen)) # Output: 1
print(next(gen)) # Output: 2
print(next(gen)) # Output: 3
Generators are memory efficient because they generate values on the fly rather than storing them in memory.
python
Copy code
def large_range():
for i in range(1000000):
yield i
gen = large_range()
for i in range(5):
print(next(gen)) # Prints 0, 1, 2, 3, 4
Generator Expressions
A generator expression is a concise way to create generators. It is similar to a list comprehension, but it uses parentheses () instead of square brackets
[], and it creates a generator object rather than a full list in memory.
Syntax:
python
Copy code
(expression for item in iterable if condition)
Example:
python
Copy code
# Generator expression to generate squares of numbers
gen_expr = (x**2 for x in range(5))
Output:
Copy code
0
1
4
9
16
Unlike list comprehensions, generator expressions don’t create a list in memory. Instead, they produce values one at a time and are more memory
efficient, especially when dealing with large datasets.
python
Copy code
gen_expr = (x for x in range(1000))
print(sum(gen_expr)) # Output: 499500
Summary
These concepts help with efficient handling of large datasets by generating values on demand, thereby reducing memory usage and improving
performance.
Collections Module
The Counter is a subclass of Python’s dict that is used for counting hashable objects. It provides a convenient way to count occurrences of elements in an
iterable (such as a list or string) or from a set of values.
Syntax:
python
Copy code
from collections import Counter
Example:
python
Copy code
from collections import Counter
print(counter)
Output:
css
Copy code
Counter({'a': 3, 'b': 2, 'c': 1})
Key Methods:
defaultdict
The defaultdict is a subclass of the built-in dict class. It provides a default value for a nonexistent key, which avoids KeyError exceptions.
Syntax:
python
Copy code
from collections import defaultdict
Example:
python
Copy code
from collections import defaultdict
print(my_dict)
Output:
arduino
Copy code
defaultdict(<class 'int'>, {'a': 1, 'b': 2})
Key Methods:
You can define a default factory function that returns a default value for missing keys (like int, list, or a custom function).
python
Copy code
# Using defaultdict with list as the default factory
my_dict = defaultdict(list)
my_dict['a'].append(1)
my_dict['b'].append(2)
print(my_dict)
Output:
less
Copy code
defaultdict(<class 'list'>, {'a': [1], 'b': [2]})
OrderedDict
OrderedDict is a subclass of dict that maintains the order of elements. Unlike the standard dict, which doesn't guarantee the order of keys, OrderedDict
preserves the insertion order of keys.
Syntax:
python
Copy code
from collections import OrderedDict
Example:
python
Copy code
from collections import OrderedDict
# Creating an OrderedDict
ordered_dict = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
Output:
css
Copy code
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
Key Methods:
move_to_end(key): Moves the specified key to the end (or the start if last=False).
popitem(last=True): Removes and returns a key-value pair from the dictionary. By default, it removes the last item.
python
Copy code
ordered_dict.move_to_end('a')
print(ordered_dict) # [('b', 2), ('c', 3), ('d', 4), ('a', 1)]
namedtuple
namedtuple is a function for creating tuple subclasses with named fields. It helps in creating immutable objects that can be accessed using attributes
(instead of using indices like regular tuples).
Syntax:
python
Copy code
from collections import namedtuple
Example:
python
Copy code
from collections import namedtuple
Output:
Copy code
Alice
25
Key Features:
deque
The deque (short for "double-ended queue") is a data structure from the collections module. It allows appending and popping elements from both ends
efficiently. Unlike lists, deque provides fast append and pop operations from both sides.
Syntax:
python
Copy code
from collections import deque
Example:
python
Copy code
from collections import deque
Output:
scss
Copy code
deque([1, 2, 3])
Key Methods:
These data structures are especially useful when handling large datasets or when you need efficient operations on collections, offering flexibility beyond
the basic Python data types.
You can unpack lists and tuples by assigning their elements to variables.
python
Copy code
# Unpacking a list
my_list = [1, 2, 3]
print(a, b, c)
Output:
Copy code
123
python
Copy code
# Unpacking a tuple
my_tuple = (10, 20, 30)
print(x, y, z)
Output:
Copy code
10 20 30
python
Copy code
# Unpacking with *
my_list = [1, 2, 3, 4, 5]
a, *b, c = my_list
print(a) # Output: 1
print(b) # Output: [2, 3, 4]
print(c) # Output: 5
Unpacking Dictionaries
python
Copy code
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Unpacking keys and values
for key, value in my_dict.items():
print(key, value)
Output:
css
Copy code
a1
b2
c3
Copying objects in Python can be done in two ways: shallow copy and deep copy. The choice between shallow and deep copy depends on whether the
original object contains other objects (like lists or dictionaries).
Shallow Copy
A shallow copy creates a new object, but the nested objects (like lists or dictionaries within the original object) are still references to the same objects.
python
Copy code
import copy
Explanation: The inner list [3, 4] is shared between both original and shallow_copy, so modifying it affects both.
Deep Copy
A deep copy creates a new object and recursively copies all nested objects, so that no references to the original objects remain.
python
Copy code
import copy
Explanation: The deep copy creates a fully independent copy, and changes to the nested objects in deep_copy do not affect the original object.
Python provides several ways to sort data, including using the built-in sorted() function and customizing sorting behavior with key functions.
sorted() Function
The sorted() function returns a new sorted list from any iterable, such as lists, tuples, and strings. It does not modify the original object.
python
Copy code
my_list = [3, 1, 4, 1, 5, 9, 2]
python
Copy code
my_list = [3, 1, 4, 1, 5, 9, 2]
sorted_list = sorted(my_list, reverse=True)
The key parameter allows you to sort the elements based on a specific criterion, such as sorting strings by their lengths.
python
Copy code
my_list = ['apple', 'banana', 'cherry', 'date']
python
Copy code
my_list = [('apple', 3), ('banana', 1), ('cherry', 2)]
The zip() function takes two or more iterables (such as lists or tuples) and aggregates them into tuples, where each tuple contains elements from the
corresponding position in each iterable.
Syntax:
python
Copy code
zip(iterable1, iterable2, ...)
Basic Example:
python
Copy code
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
python
Copy code
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = ['apple', 'banana', 'cherry']
print(list(zipped)) # Output: [(1, 'a', 'apple'), (2, 'b', 'banana'), (3, 'c', 'cherry')]
These concepts are fundamental when handling data in Python, offering flexibility in managing and processing collections, performing custom sorting, and
handling object copies efficiently.
Time and Date
The time module in Python provides functions for working with time-related tasks, such as getting the current time, pausing the program, and measuring
code execution time.
1. time() - Returns the current time in seconds since the epoch (Jan 1, 1970).
python
Copy code
import time
current_time = time.time()
print(current_time) # Output: (e.g., 1609459200.123456)
python
Copy code
import time
print("Start")
time.sleep(2) # Sleep for 2 seconds
print("End") # This will print after a 2-second delay
3. ctime() - Converts a time (in seconds since the epoch) to a human-readable string.
python
Copy code
import time
print(time.ctime()) # Output: (e.g., 'Tue Jan 1 00:00:00 1970')
python
Copy code
import time
local_time = time.localtime()
print(local_time) # Output: time.struct_time(tm_year=2024, tm_mon=11, tm_mday=27, ...)
python
Copy code
import time
current_time = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)
print(formatted_time) # Output: 2024-11-27 13:45:20
The datetime module provides classes for manipulating dates and times in both simple and complex ways. It offers more advanced capabilities compared
to the time module.
Key Classes in the datetime Module:
python
Copy code
from datetime import datetime
now = datetime.now()
print(now) # Output: (e.g., 2024-11-27 14:05:30.123456)
python
Copy code
from datetime import date
today = date.today()
print(today) # Output: (e.g., 2024-11-27)
python
Copy code
from datetime import time
t = time(14, 30, 45)
print(t) # Output: 14:30:45
python
Copy code
from datetime import datetime
now = datetime.now()
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_time) # Output: 2024-11-27 14:05:30
python
Copy code
from datetime import datetime
date_str = "2024-11-27 14:05:30"
date_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(date_obj) # Output: 2024-11-27 14:05:30
Formatting Dates and Times
You can format datetime and time objects into human-readable strings using the strftime() method.
Cod Exampl
Description
e e
%Y Four-digit year 2024
%m Two-digit month (01-12) 11
Two-digit day of the month
%d 27
(01-31)
%H Hour (00-23) 14
%M Minute (00-59) 05
%S Second (00-59) 30
Microsecond (000000-
%f 123456
999999)
%a Abbreviated weekday name Tue
%A Full weekday name Tuesday
%b Abbreviated month name Nov
Novemb
%B Full month name
er
python
Copy code
from datetime import datetime
now = datetime.now()
You can measure how long a specific part of your code takes to execute using the time module or the datetime module.
python
Copy code
import time
start_time = time.time()
# Code to measure
for i in range(1000000):
pass
end_time = time.time()
time.perf_counter() gives a higher precision timer than time.time() for performance measurement.
python
Copy code
import time
start_time = time.perf_counter()
# Code to measure
for i in range(1000000):
pass
end_time = time.perf_counter()
These modules are essential for working with time-related tasks in Python, from handling dates and times to measuring performance and formatting.
1. re.match()
The re.match() function checks if the pattern matches at the beginning of the string. If it does, it returns a match object, otherwise it returns None.
python
Copy code
import re
result = re.match(r'\d{3}', '123abc')
if result:
print("Match found:", result.group())
else:
print("No match")
# Output: Match found: 123
2. re.search()
The re.search() function scans the entire string looking for a match. If it finds a match anywhere in the string, it returns a match object, otherwise
None.
python
Copy code
import re
result = re.search(r'\d{3}', 'abc123def')
if result:
print("Match found:", result.group())
else:
print("No match")
# Output: Match found: 123
3. re.findall()
The re.findall() function returns all non-overlapping matches of the pattern as a list of strings.
python
Copy code
import re
matches = re.findall(r'\d+', 'abc 123 def 456')
print(matches) # Output: ['123', '456']
4. re.finditer()
The re.finditer() function returns an iterator yielding match objects for all non-overlapping matches.
python
Copy code
import re
matches = re.finditer(r'\d+', 'abc 123 def 456')
for match in matches:
print(match.group())
# Output:
# 123
# 456
1. re.sub()
The re.sub() function is used to replace occurrences of a pattern with a specified replacement string.
python
Copy code
import re
new_string = re.sub(r'\d+', 'number', 'abc 123 def 456')
print(new_string) # Output: abc number def number
def replace_with_square(match):
return str(int(match.group()) ** 2)
Here's an example combining multiple functions from the re module to search, match, and replace patterns in a string:
python
Copy code
import re
# Sample string
text = "My phone numbers are 123-456-7890 and 987-654-3210."
Regular expressions in Python can be powerful for tasks such as pattern matching, data validation, and text manipulation. The re module provides a rich
set of tools to work with these patterns effectively.
Defining a Class
A class in Python is defined using the class keyword, and it serves as a blueprint for creating objects.
python
Copy code
class Dog:
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age # Instance variable
def bark(self):
print(f"{self.name} says Woof!")
An object is created by calling the class as if it were a function, passing any required arguments to the __init__ method.
python
Copy code
dog1 = Dog("Buddy", 3)
dog1.bark() # Output: Buddy says Woof!
The __init__ method in Python is a special method, called a constructor, used to initialize the attributes of an object when it is created. It runs
automatically when an object is instantiated.
python
Copy code
class Car:
def __init__(self, model, year):
self.model = model # Instance variable
self.year = year # Instance variable
def display_info(self):
print(f"Car Model: {self.model}, Year: {self.year}")
Instance Variables: These are variables that belong to a specific object (instance of the class). They are defined using self.
Class Variables: These are variables that belong to the class itself and are shared by all instances of the class. They are defined outside the
__init__ method.
python
Copy code
class Student:
school_name = "ABC School" # Class variable
# Creating instances
student1 = Student("John", 15)
student2 = Student("Alice", 16)
Basic Inheritance
Inheritance allows a class (child class) to inherit attributes and methods from another class (parent class).
python
Copy code
class Animal:
def __init__(self, species):
self.species = species
def sound(self):
print("Animal sound")
class Dog(Animal):
def __init__(self, name, species):
super().__init__(species) # Calling the constructor of the parent class
self.name = name
def bark(self):
print(f"{self.name} says Woof!")
The super() function allows you to call methods from the parent class. It is commonly used in inheritance to access methods from the parent class.
python
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name}.")
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # Calls the constructor of Person
self.grade = grade
def greet(self):
super().greet() # Calls the greet method of Person
print(f"I am in grade {self.grade}.")
In the Student class, the super() function is used to call the parent class's __init__() method and the greet() method.
Method Overriding
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class.
python
Copy code
class Animal:
def speak(self):
print("Animal is making a sound")
class Dog(Animal):
def speak(self): # Overriding the speak method
print("Dog says Woof!")
# Creating an instance of Dog
dog1 = Dog()
dog1.speak() # Output: Dog says Woof!
The Dog class overrides the speak() method of the Animal class, providing its own version.
Summary
Conclusion
Python's object-oriented programming features such as classes, objects, inheritance, and method overriding provide powerful tools for designing and
structuring programs. Using these concepts effectively can help in organizing and reusing code, making it more modular and easier to maintain.
Command-Line Arguments
The sys.argv list in Python is used to handle command-line arguments passed to a script. It is part of the sys module and allows you to access arguments
provided when the script is executed.
python
Copy code
import sys
Run Command
bash
Copy code
python script.py hello world
Output
sql
Copy code
Arguments: ['script.py', 'hello', 'world']
Script Name: script.py
First Argument: hello
python
Copy code
import sys
Run Command
bash
Copy code
python script.py 5 10 15
Output
makefile
Copy code
Sum: 30
The argparse module in Python provides a more sophisticated way to handle command-line arguments. It automatically generates help messages and
validates input.
Basic Usage of argparse
python
Copy code
import argparse
# Add arguments
parser.add_argument("numbers", type=int, nargs="+", help="List of numbers to sum")
Run Command
bash
Copy code
python script.py 5 10 15
Output
makefile
Copy code
Sum: 30
python
Copy code
import argparse
# Parse arguments
args = parser.parse_args()
bash
Copy code
python script.py "Hello, World!" -u -r 3
Output
Copy code
HELLO, WORLD!HELLO, WORLD!HELLO, WORLD!
The argparse module automatically generates a help message. Run with the -h or --help flag.
Command
bash
Copy code
python script.py -h
Output
vbnet
Copy code
usage: script.py [-h] [-u] [-r REPEAT] text
positional arguments:
text The text to process
optional arguments:
-h, --help Show this help message and exit
-u, --uppercase Convert text to uppercase
-r REPEAT Number of times to repeat the text (default: 1)
You can define argument groups, mutually exclusive arguments, and more with argparse.
python
Copy code
import argparse
args = parser.parse_args()
if args.add:
print("Sum:", sum(args.numbers))
elif args.subtract:
print("Difference:", args.numbers[0] - args.numbers[1])
else:
print("No operation specified.")
Run Command
bash
Copy code
python script.py 10 5 -a
Output
makefile
Copy code
Sum: 15
Run Command
bash
Copy code
python script.py 10 5 -s
Output
makefile
Copy code
Difference: 5
Use sys.argv for simple, lightweight scripts with minimal argument parsing.
Use argparse for robust applications that require advanced argument handling, validation, and help messages.
Data Serialization
The json module in Python is used to work with JSON (JavaScript Object Notation), a lightweight data format often used for data interchange. It allows
conversion between Python objects and JSON strings.
Python JSON
Object Equivalent
dict Object
list Array
str String
int/float Number
None null
True/False true/false
Example: JSON Encoding and Decoding
python
Copy code
import json
# Python dictionary
data = {
"name": "Alice",
"age": 25,
"is_student": False,
"subjects": ["Math", "Science"],
}
Output
javascript
Copy code
JSON String: {"name": "Alice", "age": 25, "is_student": false, "subjects": ["Math", "Science"]}
Decoded Python Object: {'name': 'Alice', 'age': 25, 'is_student': False, 'subjects': ['Math', 'Science']}
python
Copy code
# Writing JSON to a file
with open("data.json", "w") as file:
json.dump(data, file)
You can use parameters in dumps for pretty-printing and custom formatting.
python
Copy code
json_string = json.dumps(data, indent=4, separators=(",", ": "))
print("Pretty JSON:\n", json_string)
python
Copy code
from datetime import datetime
The pickle module in Python is used to serialize (convert to byte stream) and deserialize (reconstruct from byte stream) Python objects. Unlike JSON, it can
handle Python-specific data types, such as classes and functions.
python
Copy code
import pickle
# Python object
data = {"name": "Alice", "age": 25, "is_student": False}
css
Copy code
Deserialized Data: {'name': 'Alice', 'age': 25, 'is_student': False}
You can serialize almost any Python object using pickle. However, the object must be defined in the same module when deserialized.
python
Copy code
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
# Serialize object
with open("student.pkl", "wb") as file:
pickle.dump(student, file)
# Deserialize object
with open("student.pkl", "rb") as file:
loaded_student = pickle.load(file)
print(f"Name: {loaded_student.name}, Age: {loaded_student.age}")
Pickle Protocols
Pickle supports multiple protocols. Higher protocol versions are more efficient but may not be compatible with older Python versions.
python
Copy code
# Using a specific protocol
pickle.dump(data, file, protocol=pickle.HIGHEST_PROTOCOL)
Be cautious when unpickling data from untrusted sources, as it can execute arbitrary code. Always validate the source of the serialized file.
Miscellaneous Utilities
The random module in Python is used for generating random numbers and performing random operations.
Example:
python
Copy code
import random
Example:
python
Copy code
import math
You can import specific functions or variables from a module to save memory and improve readability.
python
Copy code
from math import sqrt, pi
The dir() function lists all attributes (methods, variables) of an object, module, or type.
python
Copy code
import math
python
Copy code
import math
When a Python script is run, the special variable __name__ is set to "__main__". This allows code to be conditionally executed only when the script is run
directly, not when imported.
Example:
python
Copy code
# script.py
def main():
print("This script is running directly.")
if __name__ == "__main__":
main()
arduino
Copy code
This script is running directly.
Here's a well-structured Python learning outline, grouped into sections for a more organized progression:
Section 9
1. Understanding APIs
3. Building a Portfolio
This outline organizes the course into easily digestible sections, covering everything from the basics to more advanced Python concepts, along with
hands-on practice projects. Each section builds upon the previous one, guiding students through a logical progression of learning.
Here is a comprehensive list of topics that fall under basic Python programming:
Python Basics
1. Introduction to Python
2. Python Syntax
3. Variables
4. Constants
5. Comments
Data Types
8. Strings
9. Lists
10. Tuples
11. Sets
12. Dictionaries
13. Booleans
14. NoneType
Operators
Control Flow
23. Nested If
25. Loops
o For Loop
o While Loop
Functions
o Positional Arguments
o Keyword Arguments
o Default Arguments
o Variable-length Arguments (*args, **kwargs)
Basic Concepts
Here is a comprehensive list of topics that fall under basic Python programming:
Python Basics
Data Types
Operators
Control Flow
Functions
Basic Concepts
Here is a comprehensive list of topics that fall under basic Python programming: