0% found this document useful (0 votes)
63 views

Python Module 3 SIQ

Uploaded by

syedfarhan3599
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Python Module 3 SIQ

Uploaded by

syedfarhan3599
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Module 3 Super Important Questions From Exam POV

1. Explain Python string handling methods with examples.?


Answer:
Python offers a robust set of string handling methods that facilitate
various operations on strings. These methods empower developers to
manipulate, analyze, and format strings efficiently.

Examples of Python String Handling Methods:

format() Method:
The format() method helps format strings by inserting values into
placeholders within a string template.

Example :

name = "Alice"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)

# Output:
My name is Alice and I am 30 years old.

startswith() and endswith() Methods:

These methods check if a string starts or ends with a specified substring.

Example:

text = "Hello, World!"


starts_with_hello = text.startswith('Hello')
ends_with_world = text.endswith('World')
print(starts_with_hello)
print(ends_with_world)

# Output: True (for starts_with_hello), False (for ends_with_world)

split() and join() Methods:

split() divides a string into a list based on a specified separator, while join()
concatenates elements of a list into a string using a separator.

Example:

text = "apple,banana,orange"
result = text.split(',')
print(result)

# Output: ['apple', 'banana', 'orange']

fruits = ['apple', 'banana', 'orange']


result = ','.join(fruits)
print(result)

# Output: apple,banana,orange

upper() and lower() Methods:

upper() converts a string to uppercase, while lower() converts a string to


lowercase.

Example:

text = "hello world"


result = text.upper()
print(result)
# Output: HELLO WORLD

text = "Hello World"


result = text.lower()
print(result)

# Output: hello world

find() Method:

The find() method returns the index of the first occurrence of a substring
within the string. If the substring is not found, it returns -1.

Example:

text = "Hello World"


index = text.find('World')
print(index)

# Output: 6

Python's string handling methods are versatile and cater to various string
manipulation needs in tasks like data processing, text formatting, pattern
matching, and more. Understanding and leveraging these methods can significantly
enhance string manipulation capabilities in Python programs.

2. List out all the useful string methods which are supported in python.
Explain with an example for each method.
Answer:
upper() Method:

○ Description: Converts a string to uppercase.


○ Example:
text = "hello world" result = text.upper()
print(result) # Output: HELLO WORLD

2. lower() Method:

○ Description: Converts a string to lowercase.

○ Example:
text = "Hello World" result = text.lower()
print(result) # Output: hello world

3. strip() Method:

○ Description: Removes leading and trailing whitespace.

○ Example:
text = " Hello World " result = text.strip()
print(result) # Output: Hello World

4. split() Method:

○ Description: Splits a string into a list based on a delimiter.

○ Example:
text = "apple,banana,orange" result = text.split(',')
print(result) # Output: ['apple', 'banana', 'orange']

5. join() Method:
○ Description: Joins elements of a list into a string using a specified
separator.

○ Example:
fruits = ['apple', 'banana', 'orange'] result =
','.join(fruits) print(result) # Output:
apple,banana,orange

6. find() Method:

○ Description: Returns the index of the first occurrence of a substring.

○ Example:
text = "Hello World" index = text.find('World')
print(index) # Output: 6

Use Cases:

● upper() and lower() methods are handy for case manipulation in text
processing.
● strip() method is useful for sanitizing user inputs by removing
unnecessary whitespace.
● split() and join() methods aid in splitting and joining strings based on
specific patterns or delimiters.
● find() method assists in searching and locating substrings within strings.

These string methods play pivotal roles in data cleaning, formatting, searching, and
text manipulation tasks in Python programs, providing flexibility and efficiency in
handling strings.

3.Describe the difference between Python os and os.path modules. Also,


discuss the following methods of os module
a) chdir() b) rmdir() c) walk() d) listdir() e) getcwd()
Answer:

Difference between os and os.path modules:

○ os module:
■ The os module provides a wide range of operating
system-related functionalities, such as interacting with the file
system, managing processes, and handling environment variables.
■ It includes methods for file and directory operations, working
with processes, permissions, and more.
○ os.path module:
■ The os.path module, on the other hand, focuses specifically on
path manipulation-related functions. It deals with operations
related to paths, such as joining paths, splitting paths, checking
existence, and file name manipulations.
■ It doesn’t offer file I/O or specific file/directory manipulation
methods but concentrates on path-related functionalities.

Explanation of os Module Methods:

a) chdir() Method:

○ Description: Changes the current working directory.


○ Example:
import os os.chdir('/path/to/directory')

○ Use: Useful for navigating and switching the current working


directory within a Python script.

b) rmdir() Method:
○ Description: Removes a directory.
○ Example:
import os os.rmdir('/path/to/directory')

○ Use: Deletes an empty directory specified by the path.

c) walk() Method:

○ Description: Generates the file names in a directory tree by walking


the tree.
○ Example:
import os for dirpath, dirnames, filenames in
os.walk('/path/to/directory'): print(f"Current Path:
{dirpath}") print(f"Directories: {dirnames}")
print(f"Files: {filenames}")

○ Use: Useful for traversing directory trees and performing operations


on files/folders.

d) listdir() Method:

○ Description: Returns a list of entries in a directory.


○ Example:
import os entries = os.listdir('/path/to/directory')
print(entries)

○ Use: Retrieves the list of files and directories in the specified path.

e) getcwd() Method:
○ Description: Returns the current working directory.
○ Example:
import os cwd = os.getcwd() print(cwd)

○ Use: Retrieves the current working directory path.

Use Cases:

○ The os module's chdir() and getcwd() methods are helpful for


navigating and determining the current working directory within a
script.
○ rmdir() allows deleting empty directories, while listdir() and
walk() facilitate directory listing and traversal, aiding in file
manipulation, backup operations, and directory tree analysis.

Understanding the distinction between os and os.path and utilizing their


respective methods appropriately enables effective file system interaction
and path manipulations in Python programs.

4.What are the key properties of a file? Explain in detail the file
reading/writing process with an example of a python program.
Answer:
Key Properties of a File:

○ Name: The name of the file.


○ Size: The size of the file in bytes.
○ Type: Whether the file is text-based or binary.
○ Location: The path or location of the file in the file system.
○ Permissions: Access rights determining who can read, write, or
execute the file.
File Reading/Writing Process in Python:

Python provides built-in functions and methods for reading from and writing
to files. The process typically involves the following steps:

1. Opening a File:

To read from or write to a file, you first need to open it using the open()
function. It requires the file path and the mode ('r' for reading, 'w' for
writing, 'a' for appending, and more).

2. Performing File Operations:

○ Reading from a File:

■ Use methods like read(), readline(), or readlines() to


read content from the file.
○ Writing to a File:

■ Use methods like write() to add content to the file.

3. Closing the File:

Always close the file using the close() method to release system
resources.

Example of File Reading/Writing in Python:

Reading from a File:


# Open a file in read mode file = open('example.txt', 'r')
# Read the entire content content = file.read()
print(content) # Output the content of the file # Close the
file file.close()
Writing to a File:
# Open a file in write mode file = open('example.txt', 'w')
# Write to the file file.write("This is a new line.") #
Close the file file.close()

Use Cases:

○ Reading from Files: Useful for data extraction, configuration loading,


or reading logs.
○ Writing to Files: Commonly used for storing program output, logging,
or saving data for future use.

Best Practices:

○ Always close files after operations to prevent memory leaks.


○ Use error handling mechanisms (e.g., try-except-finally) to
manage exceptions while working with files.
○ Consider using with statement (context managers) for automatic file
closing.

Understanding file properties and the file reading/writing process is crucial


for various applications, such as data processing, logging, configuration
management, and more in Python programming.

5.Explain Password Locker Project.


6.What are the different steps in the project Adding Bullets to Wiki
Markup.

You might also like