0% found this document useful (0 votes)
1 views6 pages

Python Exam Prep

The document provides a comprehensive overview of Python basics, including string methods, tuples vs. lists, file handling, assertions, lists, and dictionary methods. It includes explanations, examples, and key differences between data types, emphasizing their usage and functionality. The content is structured into modules, making it a useful study guide for Python exam preparation.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Python Exam Prep

The document provides a comprehensive overview of Python basics, including string methods, tuples vs. lists, file handling, assertions, lists, and dictionary methods. It includes explanations, examples, and key differences between data types, emphasizing their usage and functionality. The content is structured into modules, making it a useful study guide for Python exam preparation.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Exam Prep – Power Packed

Module 1 – Python Basics


Q1. Explain the following string methods with suitable examples:

a) upper()\ b) lower()\ c) isupper()\ d) islower()

🔍 Explanation:

String methods in Python help manipulate or check properties of string values. These four methods are
commonly used to convert case or check if the case of a string matches certain criteria.

a) ``

• Converts all lowercase characters in the string to uppercase.


• Returns a new string; does not modify the original.

Example:

text = "hello world"


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

b) ``

• Converts all uppercase characters to lowercase.

Example:

text = "HELLO WORLD"


print(text.lower())
# Output: hello world

c) ``

• Checks if all characters in the string are uppercase.


• Returns True or False .

Example:

1
text = "HELLO"
print(text.isupper()) # True

d) ``

• Checks if all characters in the string are lowercase.

Example:

text = "hello"
print(text.islower()) # True

These methods are useful for validating user input or formatting output text consistently.

Q2. How is a Tuple different from a List? How can we convert a list to a tuple and vice
versa?

🔍 Explanation:

Tuples and Lists are both sequence data types used to store collections of items. However, they differ in
behavior and use cases.

Feature List ( [] ) Tuple ( () )

Mutability Mutable (can change) Immutable (cannot change)

Syntax Square brackets [ ] Round brackets ( )

Use-case When data needs to change For fixed data (e.g., coordinates)

Performance Slower due to mutability Slightly faster

🔁 Conversion Examples:

Convert List to Tuple:

my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)
# Output: (1, 2, 3)

Convert Tuple to List:

2
your_tuple = (4, 5, 6)
your_list = list(your_tuple)
print(your_list)
# Output: [4, 5, 6]

📌 Use tuple when you want data safety (e.g., constants or database rows).

Module 2 – File Handling and Assertions


Q3. Explain how to open, read, and write files using **, ** , and `` functions.

🔍 Explanation:

Python allows file handling using the open() function. Files can be opened in different modes like read
( 'r' ), write ( 'w' ), append ( 'a' ), etc.

Opening a File:

f = open("myfile.txt", "r") # open file for reading

Reading the File:

content = f.read()
print(content)
f.close()

Writing to a File:

f = open("myfile.txt", "w")
f.write("Hello World")
f.close()

🔐 Important Tips:

• Always close the file using f.close() to save changes.


• Use with open(...) for safer file handling.

Example using ``:

3
with open("data.txt", "a") as f:
f.write("More content\n")

Q4. What is an assertion? Write the content of an `` statement with an example.

🔍 Explanation:

An assertion is a sanity-check that you can turn on or turn off when debugging your code.

• assert checks a condition.


• If True : program continues.
• If False : program stops and throws an AssertionError .

Syntax:

assert condition, "Error message if condition is False"

Example:

x = 10
assert x > 5, "x is too small"
print("x is valid")

✔️ Output: x is valid

If x were 3, you'd get:

AssertionError: x is too small

Assertions are mainly used for debugging and ensuring code correctness.

Module 3 – Collections
Q5. What are Lists in Python? How can you add, remove, or update elements in a list?

🔍 Explanation:

A List in Python is an ordered collection of items that can be modified (mutable).

4
Example:

fruits = ["apple", "banana", "cherry"]

🔨 Common Methods:

Adding Elements:

fruits.append("orange") # adds at end


fruits.insert(1, "grape") # inserts at index 1

Removing Elements:

fruits.remove("banana") # removes by value


fruits.pop(0) # removes first element

Updating Elements:

fruits[0] = "mango"

Final List:

print(fruits)
# Output: ['mango', 'grape', 'cherry', 'orange']

Lists are dynamic and can hold mixed types: [1, "hello", 3.14] .

Q6. What are Dictionary methods in Python? Give examples of each.

🔍 Explanation:

A Dictionary is a collection of key-value pairs. Keys must be unique and immutable.

person = {"name": "Ali", "age": 22, "city": "Mumbai"}

🔧 Dictionary Methods:

1. `` – returns all keys

5
print(person.keys())
# Output: dict_keys(['name', 'age', 'city'])

2. `` – returns all values

print(person.values())
# Output: dict_values(['Ali', 22, 'Mumbai'])

3. `` – returns key-value pairs as tuples

print(person.items())
# Output: dict_items([('name', 'Ali'), ('age', 22), ('city', 'Mumbai')])

4. `` – safely get value

print(person.get("name")) # Ali
print(person.get("salary", "Not Found")) # Not Found

5. `` – update dictionary with new key-value pair

person.update({"age": 23})

6. `` – remove a key

person.pop("city")

Dictionaries are great for structured data (e.g., JSON).

Need more modules or revision notes? Just say the word and we’ll make it bigger, bolder, and exam-proof 💪
📚

You might also like